cfc9d6b8d76707a56debbfafd50106434f446ee5
[sdk] / extras / gui / controls / DirectoriesBox.ec
1 #ifdef BUILDING_ECERE_COM
2 namespace gui::controls;
3 import "Window"
4 import "Array"
5 #else
6 #ifdef ECERE_STATIC
7 public import static "ecere"
8 #else
9 public import "ecere"
10 #endif
11 #endif
12
13 import "PathBox"
14
15 FileDialog browseFileDialog { type = selectDir, text = "Select directory" };
16
17 public class DirectoriesBox : CommonControl
18 {
19 public:
20
21    bool browsing;
22
23    opacity = 0;
24
25    virtual bool OnChangedDir(char ** directory)
26    {
27       return true;
28    }
29    virtual bool OnPrepareBrowseDir(char ** directory)
30    {
31       return true;
32    }
33    virtual bool OnBrowsedDir(char ** directory)
34    {
35       return true;
36    }
37
38    watch(foreground) { list.foreground = foreground; };
39    watch(background) { list.background = background; };
40
41    property Array<String> strings
42    {
43       set
44       {
45          list.Clear();
46          if(value)
47          {
48             for(s : value)
49             {
50                char temp[MAX_LOCATION];
51                list.AddString(GetSystemPathBuffer(temp, s));
52             }
53          }
54          list.AddString("");
55          list.currentRow = list.firstRow;
56          list.modifiedDocument = false;
57       }
58       get
59       {
60          Array<String> array { };
61          DataRow row;
62          for(row = list.firstRow; row; row = row.next)
63          {
64             String string = row.string;
65             if(string && string[0])
66                array.Add(CopyUnixPath(string));
67          }
68          return array;
69       }
70    }
71
72    virtual bool Window::NotifyModified(DirectoriesBox dirsBox);
73
74    // TOCHECK: Is this not working?! :S
75    bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
76    {
77       // Browsing was not being set, fixed by introducing dependency to this class to PathBox
78       if(!active && !browsing)
79       {
80          list.StopEditing(true);
81          if(list.modifiedDocument)
82          {
83             NotifyModified(master, this);
84             list.modifiedDocument = false;
85             modifiedDocument = true;
86          }
87       }
88       return true;
89    }
90
91    Button add
92    {
93       parent = this, bevelOver = true, inactive = true;
94       position = { 265, 0 }, size = { 22, 22 };
95       anchor = { top = 0, right = 77 };
96       hotKey = plus, bitmap = BitmapResource { fileName = "<:ecere>actions/listAdd.png", alphaBlend = true };
97       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
98       {
99          list.StopEditing(true);
100          list.lastRow.Edit(null);
101          list.modifiedDocument = true;
102          return true;
103       }
104    };
105    Button remove
106    {
107       parent = this, bevelOver = true, inactive = true;
108       position = { 290, 0 }, size = { 22, 22 };
109       anchor = { top = 0, right = 54 };
110       hotKey = del, bitmap = BitmapResource { fileName = "<:ecere>actions/listRemove.png", alphaBlend = true };
111       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
112       {
113          list.StopEditing(true);
114          if(list.currentRow != list.lastRow)
115          {
116             list.DeleteRow(null);
117             list.modifiedDocument = true;
118          }
119          return true;
120       }
121    };
122    Button up
123    {
124       parent = this, bevelOver = true, inactive = true;
125       position = { 315, 0 }, size = { 22, 22 };
126       anchor = { top = 0, right = 31 };
127       hotKey = ctrlUp, bitmap = BitmapResource { fileName = "<:ecere>actions/goUp.png", alphaBlend = true };
128       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
129       {
130          if(list.currentRow != list.lastRow)
131          {
132             DataRow current = list.currentRow, row;
133             if(current)
134             {
135                row = current.previous;
136                if(row)
137                {
138                   row = row.previous;
139                   current.Move(row);
140                   list.modifiedDocument = true;
141                }
142             }
143          }
144          return true;
145       }
146    };
147    Button down
148    {
149       parent = this, bevelOver = true, inactive = true;
150       position = { 340, 0 }, size = { 22, 22 };
151       anchor = { top = 0, right = 8 };
152       hotKey = ctrlDown, bitmap = BitmapResource { fileName = "<:ecere>actions/goDown.png", alphaBlend = true };
153       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
154       {
155          DataRow current = list.currentRow, row;
156          if(current)
157          {
158             row = current.next;
159             if(row && row != list.lastRow)
160             {
161                current.Move(row);
162                list.modifiedDocument = true;
163             }
164          }
165          return true;
166       }
167    };
168    ListBox list
169    {
170       this, moveRows = true, hasVertScroll = true, dontHideScroll = true;
171       borderStyle = deep, position = { 0, 22 }, size = { 300, 60 };
172       anchor = { left = 0, top = 22, right = 0, bottom = 0 };
173
174       bool OnRightButtonDown(int x, int y, Modifiers mods)
175       {
176          return parent.OnRightButtonDown(x + position.x + parent.clientStart.x, y + position.y + parent.clientStart.y, mods);
177       }
178
179       bool NotifyChanged(ListBox listBox, DataRow row)
180       {
181          char * directory = listBox.GetData(null);
182          if(directory && directory[0])
183          {
184             char * dir = CopyString(directory);
185             if(OnChangedDir(&dir))
186             {
187                listBox.SetData(null, dir);
188                listBox.modifiedDocument = true;
189                if(listBox.currentRow == listBox.lastRow && listBox.lastRow.string)
190                {
191                   DataRow r = listBox.lastRow;
192                   char * s = r.string;
193                   listBox.currentRow = listBox.AddString("");
194                }
195             }
196             delete dir;
197          }
198          else if(listBox.currentRow != listBox.lastRow)
199          {
200             listBox.DeleteRow(null);
201             listBox.modifiedDocument = true;
202          }
203          return true;
204       }
205
206       bool NotifyEditDone(ListBox listBox, DataRow row)
207       {
208          //browseDir.Destroy(0);
209          return true;
210       }
211
212       /*
213       bool NotifyEdited(ListBox listBox, DataRow row)
214       {
215          browseDir.anchor = Anchor { right = 0, top = listBox.currentIndex * listBox.rowHeight - 2 };
216          browseDir.size = { 30, listBox.rowHeight + 3 };
217
218          browseDir.Create();
219          return true;
220       }*/
221
222       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
223       {
224          if(key == del)
225          {
226             listBox.StopEditing(true);
227             if(listBox.currentRow != listBox.lastRow)
228                listBox.DeleteRow(null);
229             return false;
230          }
231          return true;
232       }
233
234       bool NotifyMove(ListBox listBox, DataRow row, Modifiers mods)
235       {
236          if(listBox.currentRow == listBox.lastRow)
237             return false;
238          else if(row == listBox.lastRow)
239          {
240             if(listBox.currentRow == row.previous)
241                return false;
242             listBox.currentRow.Move(row.previous);
243             return false;
244          }
245          return true;
246       }
247
248       bool NotifyReclick(ListBox listBox, DataRow row, Modifiers mods)
249       {
250          row.Edit(null);
251          return true;
252       }
253    };
254    DataField dirField { dataType = class(DirPath), editable = true, userData = browseFileDialog };
255    /*
256    Button browseDir
257    {
258       master = this, parent = list, autoCreate = false, inactive = true, hotKey = f2, text = "...";
259      
260       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
261       {
262          DataRow row;
263          char * directory;
264          
265          list.StopEditing(true);
266          
267          row = list.currentRow;
268          directory = CopyString(row.string ? row.string : "");
269          if(OnPrepareBrowseDir(&directory))
270          {
271             browseFileDialog.filePath = directory;
272             StripLastDirectory(directory, directory);
273             browseFileDialog.currentDirectory = directory;
274             //browseFileDialog.master = rootWindow;
275             browsing = true;
276             if(browseFileDialog.Modal())
277             {
278                char * newDir = CopyString(browseFileDialog.filePath);
279                if(OnBrowsedDir(&newDir))
280                {
281                   row.string = newDir;
282                   if(list.lastRow.string && list.lastRow.string[0])
283                   {
284                      list.AddString("");
285                      list.currentRow = list.lastRow;
286                   }
287                   list.modifiedDocument = true;
288                }
289                delete newDir;
290             }
291             browsing = false;
292          }
293          delete directory;
294          return true;
295       }
296    };*/
297
298    DirectoriesBox()
299    {
300       list.AddField(dirField);
301       list.AddString("");
302       list.modifiedDocument = false;
303    }
304 }