ecere/ide: Added DirPath/FilePath support in DataBox (tweaked PathBox), Merged PathBo...
[sdk] / extras / gui / controls / StringsBox.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 public class NamedString : struct
14 {
15 public:
16    property String name { set { delete name; name = CopyString(value); } get { return name; } }
17    property String string { set { delete string; string = CopyString(value); } get { return string; } }
18 private:
19    String name;
20    String string;
21
22    ~NamedString()
23    {
24       delete name;
25       delete string;
26    }
27 }
28
29 public class StringsBox : CommonControl
30 {
31    opacity = 0;
32
33    watch(foreground) { list.foreground = foreground; };
34    watch(background) { list.background = background; };
35
36 public:
37    property Array<String> strings
38    {
39       set
40       {
41          list.Clear();
42          if(value)
43          {
44             for(s : value)
45             {
46                char temp[MAX_LOCATION];
47                list.AddString(s);
48             }
49          }
50          list.AddString("");
51          list.currentRow = list.firstRow;
52          list.modifiedDocument = false;
53       }
54       get
55       {
56          Array<String> array { };
57          DataRow row;
58          for(row = list.firstRow; row; row = row.next)
59          {
60             String string = row.string;
61             if(string && string[0])
62                array.Add(CopyUnixPath(string));
63          }
64          return array;
65       }
66    }
67
68    virtual bool Window::NotifyModified(StringsBox dirsBox);
69
70    bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
71    {
72       if(!active)
73       {
74          list.StopEditing(true);
75          if(list.modifiedDocument)
76          {
77             NotifyModified(master, this);
78             list.modifiedDocument = false;
79             modifiedDocument = true;
80          }
81       }
82       return true;
83    }
84
85    Button add
86    {
87       parent = this, bevelOver = true, inactive = true;
88       position = { 265, 0 }, size = { 22, 22 };
89       anchor = { top = 0, right = 77 };
90       hotKey = plus, bitmap = BitmapResource { fileName = "<:ecere>actions/listAdd.png", alphaBlend = true };
91       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
92       {
93          list.StopEditing(true);
94          list.lastRow.Edit(null);
95          //list.modifiedDocument = true;
96          return true;
97       }
98    };
99    Button remove
100    {
101       parent = this, bevelOver = true, inactive = true;
102       position = { 290, 0 }, size = { 22, 22 };
103       anchor = { top = 0, right = 54 };
104       hotKey = del, bitmap = BitmapResource { fileName = "<:ecere>actions/listRemove.png", alphaBlend = true };
105       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
106       {
107          list.StopEditing(true);
108          if(list.currentRow != list.lastRow)
109          {
110             list.DeleteRow(null);
111             //list.modifiedDocument = true;
112          }
113          return true;
114       }
115    };
116    Button up
117    {
118       parent = this, bevelOver = true, inactive = true;
119       position = { 315, 0 }, size = { 22, 22 };
120       anchor = { top = 0, right = 31 };
121       hotKey = ctrlUp, bitmap = BitmapResource { fileName = "<:ecere>actions/goUp.png", alphaBlend = true };
122       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
123       {
124          if(list.currentRow != list.lastRow)
125          {
126             DataRow current = list.currentRow, row;
127             if(current)
128             {
129                row = current.previous;
130                if(row)
131                {
132                   row = row.previous;
133                   current.Move(row);
134                   //list.modifiedDocument = true;
135                }
136             }
137          }
138          return true;
139       }
140    };
141    Button down
142    {
143       parent = this, bevelOver = true, inactive = true;
144       position = { 340, 0 }, size = { 22, 22 };
145       anchor = { top = 0, right = 8 };
146       hotKey = ctrlDown, bitmap = BitmapResource { fileName = "<:ecere>actions/goDown.png", alphaBlend = true };
147       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
148       {
149          DataRow current = list.currentRow, row;
150          if(current)
151          {
152             row = current.next;
153             if(row && row != list.lastRow)
154                current.Move(row);
155          }
156          return true;
157       }
158    };
159    ListBox list
160    {
161       this, moveRows = true, hasVertScroll = true, dontHideScroll = true;
162       borderStyle = deep, position = { 0, 22 }, size = { 300, 60 };
163       anchor = { left = 0, top = 22, right = 0, bottom = 0 };
164
165       bool OnRightButtonDown(int x, int y, Modifiers mods)
166       {
167          return parent.OnRightButtonDown(x + position.x + parent.clientStart.x, y + position.y + parent.clientStart.y, mods);
168       }
169
170       bool NotifyChanged(ListBox listBox, DataRow row)
171       {
172          char * string = listBox.GetData(null);
173          if(string && string[0])
174          {
175             if(listBox.currentRow == listBox.lastRow)
176             {
177                DataRow r = listBox.lastRow;
178                char * s = r.string;
179                listBox.currentRow = listBox.AddString("");
180             }
181          }
182          else if(listBox.currentRow != listBox.lastRow)
183             listBox.DeleteRow(null);
184          //listBox.modifiedDocument = true;
185          return true;
186       }
187
188       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
189       {
190          if(key == del)
191          {
192             listBox.StopEditing(true);
193             if(listBox.currentRow != listBox.lastRow)
194                listBox.DeleteRow(null);
195             return false;
196          }
197          return true;
198       }
199
200       bool NotifyMove(ListBox listBox, DataRow row, Modifiers mods)
201       {
202          if(listBox.currentRow == listBox.lastRow)
203             return false;
204          else if(row == listBox.lastRow)
205          {
206             listBox.currentRow.Move(row.previous);
207             return false;
208          }
209          return true;
210       }
211
212       bool NotifyReclick(ListBox listBox, DataRow row, Modifiers mods)
213       {
214          row.Edit(null);
215          return true;
216       }
217    };
218    DataField stringField { dataType = class(char *), editable = true };
219    StringsBox()
220    {
221       list.AddField(stringField);
222       list.AddString("");
223       list.modifiedDocument = false;
224    }
225 }
226
227 class NamedStringsBox : CommonControl
228 {
229    opacity = 0;
230
231    watch(foreground) { list.foreground = foreground; };
232    watch(background) { list.background = background; };
233
234    property Array<NamedString> namedStrings
235    {
236       set
237       {
238          DataRow row;
239          list.Clear();
240          if(value)
241          {
242             for(s : value)
243             {
244                char temp[MAX_LOCATION];
245                row = list.AddRow();
246                row.SetData(nameField, s.name);
247                row.SetData(stringField, s.string);
248             }
249          }
250          row = list.AddRow();
251          //row.SetData(nameField, null);
252          //row.SetData(stringField, null);
253          list.currentRow = list.firstRow;
254          list.modifiedDocument = false;
255       }
256       get
257       {
258          Array<NamedString> array { };
259          DataRow row;
260          for(row = list.firstRow; row; row = row.next)
261          {
262             // NamedString is a class:struct, need to instantiate!!
263             String name = row.GetData(nameField);
264             String string = row.GetData(stringField);
265             if(name && name[0] && string && string[0])
266                array.Add({ name, string });
267          }
268          return array;
269       }
270    }
271
272    virtual bool Window::NotifyModified(NamedStringsBox stringsBox);
273
274    bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
275    {
276       if(!active)
277       {
278          list.StopEditing(true);
279          if(list.modifiedDocument)
280          {
281             NotifyModified(master, this);
282             list.modifiedDocument = false;
283             modifiedDocument = true;
284          }
285       }
286       return true;
287    }
288
289    Button add
290    {
291       parent = this, bevelOver = true, inactive = true;
292       position = { 265, 0 }, size = { 22, 22 };
293       anchor = { top = 0, right = 77 };
294       hotKey = plus, bitmap = BitmapResource { fileName = "<:ecere>actions/listAdd.png", alphaBlend = true };
295       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
296       {
297          list.StopEditing(true);
298          list.lastRow.Edit(null);
299          return true;
300       }
301    };
302    Button remove
303    {
304       parent = this, bevelOver = true, inactive = true;
305       position = { 290, 0 }, size = { 22, 22 };
306       anchor = { top = 0, right = 54 };
307       hotKey = del, bitmap = BitmapResource { fileName = "<:ecere>actions/listRemove.png", alphaBlend = true };
308       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
309       {
310          list.StopEditing(true);
311          if(list.currentRow != list.lastRow)
312             list.DeleteRow(null);
313          return true;
314       }
315    };
316    Button up
317    {
318       parent = this, bevelOver = true, inactive = true;
319       position = { 315, 0 }, size = { 22, 22 };
320       anchor = { top = 0, right = 31 };
321       hotKey = ctrlUp, bitmap = BitmapResource { fileName = "<:ecere>actions/goUp.png", alphaBlend = true };
322       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
323       {
324          if(list.currentRow != list.lastRow)
325          {
326             DataRow current = list.currentRow, row;
327             if(current)
328             {
329                row = current.previous;
330                if(row)
331                {
332                   row = row.previous;
333                   current.Move(row);
334                }
335             }
336          }
337          return true;
338       }
339    };
340    Button down
341    {
342       parent = this, bevelOver = true, inactive = true;
343       position = { 340, 0 }, size = { 22, 22 };
344       anchor = { top = 0, right = 8 };
345       hotKey = ctrlDown, bitmap = BitmapResource { fileName = "<:ecere>actions/goDown.png", alphaBlend = true };
346       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
347       {
348          DataRow current = list.currentRow, row;
349          if(current)
350          {
351             row = current.next;
352             if(row && row != list.lastRow)
353                current.Move(row);
354          }
355          return true;
356       }
357    };
358    ListBox list
359    {
360       this, moveRows = true, hasVertScroll = true, dontHideScroll = true;
361       borderStyle = deep, position = { 0, 22 }, size = { 300, 60 };
362       anchor = { left = 0, top = 22, right = 0, bottom = 0 };
363
364       bool OnRightButtonDown(int x, int y, Modifiers mods)
365       {
366          return parent.OnRightButtonDown(x + position.x + parent.clientStart.x, y + position.y + parent.clientStart.y, mods);
367       }
368
369       bool NotifyChanged(ListBox listBox, DataRow row)
370       {
371          String name = row.GetData(nameField);
372          String string = row.GetData(stringField);
373          if((name && name[0]) || (string && string[0]))
374          {
375             if(listBox.currentRow == listBox.lastRow)
376             {
377                DataRow r = listBox.lastRow;
378                char * s = r.string;
379                listBox.currentRow = list.AddRow();
380                //row.SetData(nameField, null);
381                //row.SetData(stringField, null);
382             }
383          }
384          else if(listBox.currentRow != listBox.lastRow)
385             listBox.DeleteRow(null);
386          listBox.modifiedDocument = true;
387          return true;
388       }
389
390       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
391       {
392          if(key == del)
393          {
394             listBox.StopEditing(true);
395             if(listBox.currentRow != listBox.lastRow)
396                listBox.DeleteRow(null);
397             return false;
398          }
399          return true;
400       }
401
402       bool NotifyMove(ListBox listBox, DataRow row, Modifiers mods)
403       {
404          if(listBox.currentRow == listBox.lastRow)
405             return false;
406          else if(row == listBox.lastRow)
407          {
408             listBox.currentRow.Move(row.previous);
409             return false;
410          }
411          return true;
412       }
413
414       bool NotifyReclick(ListBox listBox, DataRow row, Modifiers mods)
415       {
416          row.Edit(null);
417          return true;
418       }
419    };
420    DataField nameField { dataType = class(char *), editable = true, freeData = false, width = 120 };
421    DataField stringField { dataType = class(char *), editable = true, freeData = false };
422    
423    NamedStringsBox()
424    {
425       //DataRow row;
426       list.AddField(nameField);
427       list.AddField(stringField);
428       /*row = */list.AddRow();
429       //row.SetData(nameField, null);
430       //row.SetData(stringField, null);
431       list.modifiedDocument = false;
432    }
433 }