c74845420acab5ee6c5fea1e2c1aba087ba40586
[sdk] / samples / guiAndGfx / eNotepad / eNotepad.ec
1 import "ecere"
2
3 static FileFilter txtFilters[] =
4 {
5    { "Text Files (*.txt)", "txt" },
6    { "All files", null }
7 };
8 static FileType txtTypes[] =
9 {
10    { "Text Files", "txt", always },
11 };
12
13 eNotepad app;
14 eNodepadWindow appWindow;
15
16 class eNodepadWindow : Window
17 {
18    text = "eNotepad";
19    borderStyle = sizable;
20    hasMaximize = true;
21    hasMinimize = true;
22    hasClose = true;
23    hasMenuBar = true;
24
25    size = { 640, 480 };
26    isDocument = true;
27
28    Menu fileMenu { menu, "File", f };
29    MenuItem openItem
30    {
31       fileMenu, "Open", o, ctrlO;
32
33       bool NotifySelect(MenuItem selection, Modifiers mods)
34       {
35          if(openDialog.Modal() == ok)
36          {
37             File f = FileOpen(openDialog.filePath, read);
38             if(f)
39             {
40                fileName = openDialog.filePath;
41                editBox.Load(f);
42                delete(f);
43             }
44          }
45       }
46    }
47    MenuDivider { fileMenu };
48    MenuItem saveItem { fileMenu, "Save", s, ctrlS, NotifySelect = MenuFileSave };
49    MenuItem saveItemAs { fileMenu, "Save As...", a, NotifySelect = MenuFileSaveAs };
50    MenuDivider { fileMenu };
51    MenuItem exitItem { fileMenu, "Exit", x, altF4, NotifySelect = MenuFileExit };
52    MenuPlacement { menu, "Edit", e };
53    FileDialog fileSaveDialog
54    {
55       master = this, type = save, text = "Save File...",
56       types = txtTypes, sizeTypes = sizeof(txtTypes), filters = txtFilters, sizeFilters = sizeof(txtFilters)
57    };
58    FileDialog openDialog
59    {
60       master = this, type = open, text = "Load File...",
61       types = txtTypes, sizeTypes = sizeof(txtTypes), filters = txtFilters, sizeFilters = sizeof(txtFilters)
62    };
63    saveDialog = fileSaveDialog;
64
65    bool OnSaveFile(char * fileName)
66    {
67       File f = FileOpen(fileName, write);
68       if(f)
69       {
70          editBox.Save(f, false);
71          modifiedDocument = false;
72          delete(f);
73          return true;
74       }
75       return false;
76    }
77    EditBox editBox 
78    {
79       this, font = { "Lucida Console", 10 }, anchor = { left = 0.0, top = 0.0, right = 0.0, bottom = 0.0 }, hasHorzScroll = true, hasVertScroll = true, multiLine = true;
80
81       bool OnFileModified(FileChange fileChange, char * param)
82       {
83          
84          return true;
85       }
86
87       bool NotifyModified(EditBox editBox)
88       {
89          modifiedDocument = true;
90          return true;
91       }
92    };
93
94    bool OnCreate(void)
95    {
96       if(app.paramLocation[0])
97          LoadFile(app.paramLocation);
98       return true;
99    }
100
101    bool LoadFile(char * filePath)
102    {
103       File f;
104       f = FileOpen(filePath, read);
105       if(f)
106       {
107          fileName = filePath;
108          editBox.Load(f);
109          delete f;
110          return true;
111       }
112       return false;
113    }
114
115    bool OnFileModified(FileChange fileChange, char * param)
116    {
117
118       return true;
119    }
120 }
121
122 class eNotepad : GuiApplication
123 {
124    char paramLocation[MAX_LOCATION];
125    bool Init()
126    {
127       app = this;
128       
129       if(argc == 2)
130          strcpy(paramLocation, argv[1]); // how to check if the path is valid?
131       else
132          paramLocation[0] = '\0';
133
134       appWindow = eNodepadWindow { };
135       
136       return true;
137    }
138 }