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