Initial git commit -- Transition from CodeGuard repository
[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    char * name;
19    char * string;
20
21    void OnFree()
22    {
23       delete name;
24       delete string;
25    }
26 }
27
28 public class StringsBox : CommonControl
29 {
30    opacity = 0;
31
32    watch(foreground) { list.foreground = foreground; };
33    watch(background) { list.background = background; };
34
35 public:
36    property Array<String> strings
37    {
38       set
39       {
40          list.Clear();
41          if(value)
42          {
43             for(s : value)
44             {
45                char temp[MAX_LOCATION];
46                list.AddString(s);
47             }
48          }
49          list.AddString("");
50          list.currentRow = list.firstRow;
51          list.modifiedDocument = false;
52       }
53       get
54       {
55          Array<String> array { };
56          DataRow row;
57          for(row = list.firstRow; row; row = row.next)
58          {
59             String string = row.string;
60             if(string && string[0])
61                array.Add(CopyUnixPath(string));
62          }
63          return array;
64       }
65    }
66
67    virtual bool Window::NotifyModified(StringsBox dirsBox);
68
69    bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
70    {
71       if(!active)
72       {
73          list.StopEditing(true);
74          if(list.modifiedDocument)
75          {
76             NotifyModified(master, this);
77             list.modifiedDocument = false;
78             modifiedDocument = true;
79          }
80       }
81       return true;
82    }
83
84    Button add
85    {
86       parent = this, bevelOver = true, inactive = true;
87       position = { 265, 0 }, size = { 22, 22 };
88       anchor = { top = 0, right = 77 };
89       hotKey = plus, bitmap = BitmapResource { fileName = "<:ecere>actions/listAdd.png", alphaBlend = true };
90       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
91       {
92          list.StopEditing(true);
93          list.lastRow.Edit(null);
94          //list.modifiedDocument = true;
95          return true;
96       }
97    };
98    Button remove
99    {
100       parent = this, bevelOver = true, inactive = true;
101       position = { 290, 0 }, size = { 22, 22 };
102       anchor = { top = 0, right = 54 };
103       hotKey = del, bitmap = BitmapResource { fileName = "<:ecere>actions/listRemove.png", alphaBlend = true };
104       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
105       {
106          list.StopEditing(true);
107          if(list.currentRow != list.lastRow)
108          {
109             list.DeleteRow(null);
110             //list.modifiedDocument = true;
111          }
112          return true;
113       }
114    };
115    Button up
116    {
117       parent = this, bevelOver = true, inactive = true;
118       position = { 315, 0 }, size = { 22, 22 };
119       anchor = { top = 0, right = 31 };
120       hotKey = ctrlUp, bitmap = BitmapResource { fileName = "<:ecere>actions/goUp.png", alphaBlend = true };
121       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
122       {
123          if(list.currentRow != list.lastRow)
124          {
125             DataRow current = list.currentRow, row;
126             if(current)
127             {
128                row = current.previous;
129                if(row)
130                {
131                   row = row.previous;
132                   current.Move(row);
133                   //list.modifiedDocument = true;
134                }
135             }
136          }
137          return true;
138       }
139    };
140    Button down
141    {
142       parent = this, bevelOver = true, inactive = true;
143       position = { 340, 0 }, size = { 22, 22 };
144       anchor = { top = 0, right = 8 };
145       hotKey = ctrlDown, bitmap = BitmapResource { fileName = "<:ecere>actions/goDown.png", alphaBlend = true };
146       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
147       {
148          DataRow current = list.currentRow, row;
149          if(current)
150          {
151             row = current.next;
152             if(row && row != list.lastRow)
153                current.Move(row);
154          }
155          return true;
156       }
157    };
158    ListBox list
159    {
160       this, moveRows = true, hasVertScroll = true, dontHideScroll = true;
161       borderStyle = deep, position = { 0, 22 }, size = { 300, 60 };
162       anchor = { left = 0, top = 22, right = 0, bottom = 0 };
163
164       bool OnRightButtonDown(int x, int y, Modifiers mods)
165       {
166          return parent.OnRightButtonDown(x + position.x + parent.clientStart.x, y + position.y + parent.clientStart.y, mods);
167       }
168
169       bool NotifyChanged(ListBox listBox, DataRow row)
170       {
171          char * string = listBox.GetData(null);
172          if(string && string[0])
173          {
174             if(listBox.currentRow == listBox.lastRow)
175             {
176                DataRow r = listBox.lastRow;
177                char * s = r.string;
178                listBox.currentRow = listBox.AddString("");
179             }
180          }
181          else if(listBox.currentRow != listBox.lastRow)
182             listBox.DeleteRow(null);
183          //listBox.modifiedDocument = true;
184          return true;
185       }
186
187       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
188       {
189          if(key == del)
190          {
191             listBox.StopEditing(true);
192             if(listBox.currentRow != listBox.lastRow)
193                listBox.DeleteRow(null);
194             return false;
195          }
196          return true;
197       }
198
199       bool NotifyMove(ListBox listBox, DataRow row, Modifiers mods)
200       {
201          if(listBox.currentRow == listBox.lastRow)
202             return false;
203          else if(row == listBox.lastRow)
204          {
205             listBox.currentRow.Move(row.previous);
206             return false;
207          }
208          return true;
209       }
210
211       bool NotifyReclick(ListBox listBox, DataRow row, Modifiers mods)
212       {
213          row.Edit(null);
214          return true;
215       }
216    };
217    DataField stringField { dataType = class(char *), editable = true };
218    StringsBox()
219    {
220       list.AddField(stringField);
221       list.AddString("");
222       list.modifiedDocument = false;
223    }
224 }
225
226 class NamedStringsBox : CommonControl
227 {
228    opacity = 0;
229
230    watch(foreground) { list.foreground = foreground; };
231    watch(background) { list.background = background; };
232
233    property Array<NamedString> namedStrings
234    {
235       set
236       {
237          DataRow row;
238          list.Clear();
239          if(value)
240          {
241             for(s : value)
242             {
243                char temp[MAX_LOCATION];
244                row = list.AddRow();
245                row.SetData(nameField, s.name);
246                row.SetData(stringField, s.string);
247             }
248          }
249          row = list.AddRow();
250          //row.SetData(nameField, null);
251          //row.SetData(stringField, null);
252          list.currentRow = list.firstRow;
253          list.modifiedDocument = false;
254       }
255       get
256       {
257          Array<NamedString> array { };
258          DataRow row;
259          NamedString s;
260          for(row = list.firstRow; row; row = row.next)
261          {
262             //String string = row.string;
263             s.name = row.GetData(nameField);
264             s.string = row.GetData(stringField);
265             if(s.name && s.name[0] && s.string && s.string[0])
266                array.Add(s);
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          NamedString s;
372          s.name = row.GetData(nameField);
373          s.string = row.GetData(stringField);
374          if((s.name && s.name[0]) || (s.string && s.string[0]))
375          {
376             if(listBox.currentRow == listBox.lastRow)
377             {
378                DataRow r = listBox.lastRow;
379                char * s = r.string;
380                listBox.currentRow = list.AddRow();
381                //row.SetData(nameField, null);
382                //row.SetData(stringField, null);
383             }
384          }
385          else if((!s.name || s.name[0]) && (!s.string || !s.string[0]) && listBox.currentRow != listBox.lastRow)
386             listBox.DeleteRow(null);
387          listBox.modifiedDocument = true;
388          return true;
389       }
390
391       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
392       {
393          if(key == del)
394          {
395             listBox.StopEditing(true);
396             if(listBox.currentRow != listBox.lastRow)
397                listBox.DeleteRow(null);
398             return false;
399          }
400          return true;
401       }
402
403       bool NotifyMove(ListBox listBox, DataRow row, Modifiers mods)
404       {
405          if(listBox.currentRow == listBox.lastRow)
406             return false;
407          else if(row == listBox.lastRow)
408          {
409             listBox.currentRow.Move(row.previous);
410             return false;
411          }
412          return true;
413       }
414
415       bool NotifyReclick(ListBox listBox, DataRow row, Modifiers mods)
416       {
417          row.Edit(null);
418          return true;
419       }
420    };
421    DataField nameField { dataType = class(char *), editable = true, freeData = false, width = 120 };
422    DataField stringField { dataType = class(char *), editable = true, freeData = false };
423    
424    NamedStringsBox()
425    {
426       //DataRow row;
427       list.AddField(nameField);
428       list.AddField(stringField);
429       /*row = */list.AddRow();
430       //row.SetData(nameField, null);
431       //row.SetData(stringField, null);
432       list.modifiedDocument = false;
433    }
434 }