ecere/gui/controls/ListBox: Fixed remaining issue caused by moving fix
[sdk] / ecere / src / gui / controls / ListBox.ec
1 namespace gui::controls;
2
3 import "Window"
4
5 static define branchesColor = Color { 85, 85, 85 };
6 static define headerCollapseForeground = Color { 135, 135, 135 };
7 static define dataBoxBackground = Color { 67, 134, 198 };
8 static define dataBoxForeground = lightYellow;
9
10 #define SNAPDOWN(x, d) \
11       if(Abs(x) % (d)) \
12       { \
13          if(x < 0) x -= ((d) - Abs(x) % (d)); else x -= x % (d); \
14       }
15
16 #include <stdarg.h>
17
18 static class ListBoxCell : struct
19 {
20    ListBoxCell prev, next;
21    bool isSet;
22    // data follows
23    void * data[1];
24 };
25
26 #define RESIZE_BORDER   5
27 #define PLUSY  4
28 #define EXTRA_SPACE 8
29
30 class ListBoxBits
31 {
32    bool header:1, freeSelect:1 /*Exclusive to ELS_MULTISELECT*/, fullRowSelect:1, multiSelect:1, autoScroll:1, alwaysHL : 1, moveRows:1, resizable:1;
33    bool moveFields:1, clearHeader:1, alwaysEdit:1, collapse:1, treeBranch:1, rootCollapse:1, heightSet:1;
34    bool sortable:1, noDragging:1, fillLastField:1, expandOnAdd:1;
35 };
36
37 public class DataDisplayFlags
38 {
39    public bool selected:1, fullRow:1, current:1, active:1, dropBox:1, header:1, firstField:1;
40 };
41
42 enum SelectedFlag { unselected, selected, tempSelected, tempUnselected };
43
44 default:
45 extern int __ecereVMethodID_class_OnEdit;
46 extern int __ecereVMethodID_class_OnDisplay;
47 extern int __ecereVMethodID_class_OnGetString;
48 extern int __ecereVMethodID_class_OnFree;
49 extern int __ecereVMethodID_class_OnCompare;
50 extern int __ecereVMethodID_class_OnCopy;
51 extern int __ecereVMethodID_class_OnSaveEdit;
52
53 private:
54
55 public class DataField
56 {
57    class_no_expansion;
58 public:
59    property Class dataType
60    {
61       set { dataType = value; if(value) { alignment = (Alignment)value.defaultAlignment; } }  // NOTE: Class::defaultAlignment does not seem to be used anywhere
62       get { return dataType; }
63    }
64    property bool editable { set { editable = value; } };
65    property bool fixed { set { fixed = value; } get { return fixed; } };
66    property Alignment alignment
67    {
68       set
69       {
70          alignment = value;
71          if(headButton) headButton.alignment = value;
72          if(listBox) listBox.Update(null);
73       }
74       get { return alignment; }
75    };
76    property int width
77    {
78       set
79       {
80          width = Max(value, -EXTRA_SPACE);
81          if(listBox)
82             listBox.AdaptToFieldWidth(this, false);
83       }
84       get { return width; }
85    };
86    property int index { get { return this ? index : 0; } };
87    property int position
88    {
89       set
90       {
91          if(listBox)
92          {
93             int index = 0;
94             DataField field;
95             for(field = listBox.fields.first; field; field = field.next)
96             {
97                if(index == value - 2)
98                   break;
99                index++;
100             }
101             Move(field);
102          }
103       }
104       get
105       {
106          if(listBox)
107          {
108             int index = 0;
109             DataField field;
110             for(field = listBox.fields.first; field; field = field.next)
111             {
112                if(this == field)
113                {
114                   return index + 1;
115                }
116                index++;
117             }
118          }
119          return -1;
120       }
121    };
122    property int sortOrder { get { return this ? sortOrder : 0; } };
123    property const char * header
124    {
125       set
126       {
127          header = value;
128          if(headButton)
129             headButton.text = header;
130       }
131    };
132    property void * userData
133    {
134       set
135       {
136          userData = value;
137       }
138       get
139       {
140          return this ? userData : null;
141       }
142    };
143    property bool freeData
144    {
145       set { freeData = value; } get { return freeData; }
146    };
147    property DataField prev { get { return prev; } };
148    property DataField next { get { return next; } };
149
150    void Move(DataField after)
151    {
152       if(prev != after)
153       {
154          listBox.fields.Move(this, after);
155
156          // Fix up positions
157          listBox.OnResize(listBox.clientSize.w, listBox.clientSize.h);
158          listBox.Update(null);
159       }
160    }
161
162    void AutoSize()
163    {
164       if(listBox && dataType)
165       {
166          Display display = listBox.display;
167          Font boldFont = listBox.boldFont.font;
168          Font font = listBox.fontObject;
169          DataRow row;
170          int width = 0;
171          if(header)
172             display.FontExtent(boldFont, header, strlen(header), &width, null);
173          width += EXTRA_SPACE;
174          for(row = listBox.firstRow; row; row = row.GetNextRow())
175          {
176             ListBoxCell cell;
177             uint i;
178             for(i = 0, cell = row.cells.first; i != index; i++, cell = cell.next);
179             if(cell && cell.isSet && dataType)
180             {
181                static char tempString[4096];
182                const String string;
183                int tw = 0;
184                if(dataType.type == normalClass || dataType.type == noHeadClass)
185                   string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, cell.data[0], tempString, userData, null);
186                else
187                   string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, cell.data, tempString, userData, null);
188                if(string)
189                   display.FontExtent(row.header ? boldFont : font, string, strlen(string), &tw, null);
190                else
191                   display.FontExtent(row.header ? boldFont : font, "", 0, &tw, null);
192                if(tw > width) width = tw;
193             }
194          }
195          if(width)
196             property::width = width;
197       }
198    }
199
200 private:
201    DataField()
202    {
203       // property::dataType = "String";
204       property::dataType = "char *";
205       // width = 80;
206       freeData = true;
207    }
208
209    ~DataField()
210    {
211       Free();
212    }
213
214    void Free()
215    {
216       headButton.Destroy(0);
217       delete headButton;
218       if(listBox)
219       {
220          DataField field;
221          listBox.fields.Remove(this);
222          for(field = listBox.fields.first; field; field = field.next)
223          {
224             if(field.index >= index)
225                field.index--;
226          }
227          if(listBox.currentField == this)
228             listBox.currentField = null;
229          listBox.numFields--;
230          listBox.OnResize(listBox.clientSize.w, listBox.clientSize.h);
231          listBox = null;
232       }
233    }
234
235    DataField prev, next;
236    const char * header;
237    Class dataType;
238    int width;
239    uint index;
240    int x;
241    Button headButton;
242    int sortOrder;
243    Alignment alignment;
244    bool editable;
245    ListBox listBox;
246    bool defaultField;
247    void * userData;
248    bool freeData;
249    bool fixed;
250 };
251
252 public class DataRow
253 {
254    class_no_expansion
255 #ifdef _DEBUG
256    bool skipCheck;
257 #endif
258 public:
259    property int64 tag { set { if(this) tag = value; } get { return this ? tag : 0; } };
260    property DataRow previous { get { return prev; } };
261    property DataRow next { get { return next; } };
262    property int index { get { return (this && (!parent || parent.IsExpanded())) ? index : -1; } };
263    property const char * string
264    {
265       set { SetData(listBox.fields.first, value); }
266       get { return GetData(listBox.fields.first); }
267    };
268    property bool isHeader { set { header = value; } get { return this ? header : false; } };
269    property BitmapResource icon { set { if(this) icon = value; } get { return this ? icon : null; } };
270    property bool collapsed
271    {
272       set
273       {
274          if(this && collapsed != value)
275          {
276             collapsed = value;
277             if(parent.IsExpanded())
278             {
279                DataRow search;
280                int ix;
281
282                if(value)
283                {
284                   for(search = subRows.first; search; search = search.next)
285                      search.selectedFlag = 0;
286
287                   if(listBox.clickedRow && !listBox.clickedRow.parent.IsExpanded())
288                   {
289                      listBox.clickedRow = GetNextRow();
290                      if(!listBox.clickedRow)
291                         listBox.clickedRow = this;
292                   }
293                   if(listBox.currentRow && !listBox.currentRow.parent.IsExpanded())
294                   {
295                      listBox.SetCurrentRow(this, true);
296                   }
297                   if(listBox.firstRowShown && !listBox.firstRowShown.parent.IsExpanded())
298                   {
299                      listBox.firstRowShown = GetPrevRow();
300                      if(!listBox.firstRowShown)
301                         listBox.firstRowShown = this;
302                   }
303                }
304
305                ix = index+1;
306                for(search = GetNextRow(); search; search = search.GetNextRow())
307                   search.index = ix++;
308                listBox.rowCount = ix;
309
310                listBox.HideEditBox(false, false, true);
311
312                listBox.SetScrollArea(
313                   listBox.width,
314                   (listBox.rowCount * listBox.rowHeight) +
315                   ((listBox.style.header) ? listBox.rowHeight : 0) -
316                   ((!((listBox.clientSize.h+1) % listBox.rowHeight)) ? listBox.rowHeight : 0), true);
317                listBox.Update(null);
318                listBox.NotifyCollapse(listBox.master, listBox, this, value);
319             }
320          }
321 #ifdef _DEBUG
322          if(this && !skipCheck)
323             listBox.CheckConsistency();
324 #endif
325       }
326       get { return this ? collapsed : false; }
327    };
328    property bool selected
329    {
330       set
331       {
332          if(this)
333          {
334             selectedFlag = value ? selected : unselected;
335             listBox.Update(null);
336          }
337       }
338       get { return selectedFlag == selected || selectedFlag == tempSelected; }
339    };
340    property DataRow parent
341    {
342       set
343       {
344          if(this && (value != this))
345          {
346             DataRow search;
347             DataRow after = value ? value.subRows.last : listBox.rows.last;
348             int ixCount = (!collapsed && subRows.count) ? GetLastRow().index - index + 1 : 1;
349             if(parent.IsExpanded())
350             {
351                for(search = GetNextRow(); search; search = search.GetNextRow())
352                   search.index -= ixCount;
353                listBox.rowCount -= ixCount;
354             }
355
356             listBox.HideEditBox(false, false, true);
357
358             /*
359             if(this == listBox.clickedRow)
360             {
361                listBox.clickedRow = GetNextRow();
362                if(!listBox.clickedRow)
363                   listBox.clickedRow = GetPrevRow();
364             }
365
366             if(this == listBox.currentRow)
367             {
368                DataRow newCurrentRow = GetNextRow();
369                if(!listBox.newCurrentRow)
370                   listBox.newCurrentRow = GetPrevRow();
371                listBox.SetCurrentRow(newCurrentRow, true);
372             }
373
374             if(this == listBox.firstRowShown)
375             {
376                listBox.firstRowShown = GetPrevRow();
377                if(!listBox.firstRowShown)
378                   listBox.firstRowShown = GetNextRow();
379             }
380             */
381
382             (parent ? parent.subRows : listBox.rows).Remove(this);
383
384             if(value)
385                value.subRows.Insert(after, this);
386             else
387                listBox.rows.Insert(after, this);
388
389             parent = value;
390
391             if(value && listBox.style.expandOnAdd)
392             {
393 #ifdef _DEBUG
394                value.skipCheck = true;
395 #endif
396                value.collapsed = false;
397 #ifdef _DEBUG
398                value.skipCheck = false;
399 #endif
400             }
401
402             if(value.IsExpanded())
403             {
404                DataRow search;
405
406                if(after && after.subRows.first && !after.collapsed)
407                {
408                   search = after.GetLastRow();
409                   index = search.index + 1;
410                }
411                else
412                   index = after ? (after.index + 1) : (index + 1);
413
414                listBox.rowCount += ixCount;
415
416                {
417                   int ix = index+1;
418                   for(search = GetNextRow(); search; search = search.GetNextRow())
419                      search.index = ix++;
420                }
421
422                listBox.SetScrollArea(
423                   listBox.width,
424                   (listBox.rowCount * listBox.rowHeight) +
425                   ((listBox.style.header) ? listBox.rowHeight : 0) -
426                   ((!((listBox.clientSize.h+1) % listBox.rowHeight)) ? listBox.rowHeight : 0), true);
427                if(listBox.style.autoScroll)
428                   listBox.SetScrollPosition(0, MAXINT - listBox.rowHeight);
429             }
430             // TESTING THIS HERE...
431             if(listBox.created)
432                listBox.Sort(listBox.sortField, listBox.sortField.sortOrder);
433
434             {
435                int headerSize = ((listBox.style.header) ? listBox.rowHeight : 0);
436                int height = listBox.clientSize.h + 1 - headerSize;
437                if(listBox.currentRow && listBox.currentRow.index * listBox.rowHeight > listBox.scroll.y + height - listBox.rowHeight)
438                   listBox.SetScrollPosition(listBox.scroll.x, listBox.currentRow.index * listBox.rowHeight - height + listBox.rowHeight);
439                else if(!listBox.currentRow || listBox.currentRow.index * listBox.rowHeight < listBox.scroll.y)
440                {
441                   int line = listBox.currentRow ? listBox.currentRow.index * listBox.rowHeight : 0;
442                   listBox.SetScrollPosition(listBox.scroll.x, line);
443                }
444                else
445                   listBox.OnVScroll(0, listBox.scroll.y, 0);
446             }
447             listBox.Update(null);
448          }
449       }
450       get
451       {
452          return parent;
453       }
454    };
455    property DataRow lastRow { get { return this ? subRows.last : null; } };
456    property DataRow firstRow { get { return this ? subRows.first : null; } };
457
458    void Edit(DataField field)
459    {
460       if(this)
461       {
462          if(listBox)
463          {
464             if(!field || !field.editable)
465             {
466                for(field = listBox.fields.first; field; field = field.next)
467                   if(field.editable) break;
468             }
469             if(field && field.editable)
470             {
471                listBox.SetCurrentRow(this, true);
472                listBox.PopupEditBox(field, false);
473             }
474          }
475       }
476    }
477
478    // NOTE: This does not support reparenting (Use row.parent = first)
479    void Move(DataRow after)
480    {
481       if(this)
482       {
483          incref this;
484          if(listBox && prev != after)
485          {
486             DataRow search;
487             int headerSize = ((listBox.style.header) ? listBox.rowHeight : 0);
488             int height = listBox.clientSize.h + 1 - headerSize;
489             int ixCount = (!collapsed && subRows.count) ? GetLastRow().index - index + 1 : 1;
490
491             if(!after || after.index < index)
492             {
493                if((after && after == listBox.firstRowShown.prev) || (!after && !parent /*&& listBox.firstRowShown.prev*/))
494                   listBox.firstRowShown = this;
495
496                // All rows between AFTER (exclusive) and ROW (exclusive) are incremented by one
497                // ROW is equal to AFTER's index + 1
498
499                for(search = after ? after.next : (parent ? parent.subRows.first : listBox.rows.first); search && search != this; search = search.GetNextRow())
500                   search.index += ixCount;
501
502                if(after && after.subRows.first && !after.collapsed)
503                {
504                   search = after.GetLastRow();
505                   index = search.index + 1;
506                }
507                else
508                   index = after ? (after.index + 1) : parent ? parent.index + 1 : 0;
509
510                // Fix indices of sub rows
511                if(!collapsed)
512                {
513                   int c, ix = index+1;
514                   for(c = 1, search = GetNextRow(); search && c < ixCount; c++, search = search.GetNextRow())
515                      search.index = ix++;
516                }
517             }
518             else
519             {
520                DataRow nextRow;
521                int afterIXCount = 1;
522
523                nextRow = GetNextRow();
524                if(this == listBox.firstRowShown)
525                   listBox.firstRowShown = nextRow;
526
527                // All rows between ROW (exclusive) and AFTER (inclusive) are decremented by one
528                // ROW is equal to AFTER's index
529
530                for(search = nextRow; search; search = search.GetNextRow())
531                {
532                   search.index -= ixCount;
533                   if(search == after) break;
534                }
535
536                // Fix up's after's sub rows
537                if(after && !after.collapsed)
538                {
539                   DataRow last = after.GetLastRow();
540                   if(last != after)
541                   {
542                      int ix = after.index+1;
543                      for(search = after.GetNextRow(); search; search = search.GetNextRow())
544                      {
545                         afterIXCount++;
546                         search.index = ix++;
547                         if(search == last) break;
548                      }
549                   }
550                }
551                index = after.index + afterIXCount;
552
553                // Fix indices of sub rows
554                if(!collapsed)
555                {
556                   int c, ix = index+1;
557                   for(c = 1, search = GetNextRow(); search && c < ixCount; c++, search = search.GetNextRow())
558                      search.index = ix++;
559                }
560             }
561             (parent ? parent.subRows : listBox.rows).Move(this, after);
562
563 #ifdef _DEBUG
564             listBox.CheckConsistency();
565 #endif
566
567             listBox.HideEditBox(true, false, true);
568             if(listBox)
569             {
570                if(listBox.currentRow && listBox.currentRow.index * listBox.rowHeight > listBox.scroll.y + height - listBox.rowHeight)
571                   listBox.SetScrollPosition(listBox.scroll.x, listBox.currentRow.index * listBox.rowHeight - height + listBox.rowHeight);
572                else if(!listBox.currentRow || listBox.currentRow.index * listBox.rowHeight < listBox.scroll.y)
573                {
574                   int line = listBox.currentRow ? listBox.currentRow.index * listBox.rowHeight : 0;
575                   //SNAPUP(line, listBox.rowHeight);
576                   listBox.SetScrollPosition(listBox.scroll.x, line);
577                }
578                else
579                   listBox.OnVScroll(0, listBox.scroll.y, 0);
580
581                listBox.modifiedDocument = true;
582
583                listBox.Update(null);
584             }
585          }
586          delete this;
587       }
588    }
589
590    any_object GetData(DataField field)
591    {
592       if(this)
593       {
594          ListBoxCell cell = listBox.GetCell(&this, &field);
595          if(cell && cell.isSet && cell.data)
596          {
597             if((field.dataType.type == normalClass || field.dataType.type == noHeadClass))
598                return cell.data[0];
599             else
600                return cell.data;
601          }
602       }
603       return null;
604    }
605
606    void * SetData(DataField field, any_object newData)
607    {
608       if(this)
609       {
610          ListBoxCell cell = listBox.GetCell(&this, &field);
611          if(cell)
612          {
613             Class dataType = field.dataType;
614
615             if(dataType)
616             {
617                if(dataType.type == normalClass || dataType.type == noHeadClass)
618                {
619                   if(cell.data[0] && field.freeData)
620                      ((void (*)(void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnFree])(dataType, cell.data[0]);
621
622                   if(eClass_IsDerived(dataType, class(char *)) && field.freeData)
623                      ((void (*)(void *, void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnCopy])(dataType, cell.data, newData);
624                   else
625                      cell.data[0] = (void *)newData;
626                }
627                else
628                {
629                   // Free old data first
630                   ((void (*)(void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnFree])(dataType, cell.data);
631                   ((void (*)(void *, void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnCopy])(dataType, cell.data, newData);
632                }
633             }
634             cell.isSet = true;
635             listBox.modifiedDocument = true;
636             listBox.Update(null);
637             if(dataType && (dataType.type == normalClass || dataType.type == noHeadClass))
638                return cell.data;
639             else
640                return &cell.data;
641          }
642       }
643       return null;
644    }
645
646    void UnsetData(DataField field)
647    {
648       if(this)
649       {
650          ListBoxCell cell = listBox.GetCell(&this, &field);
651          if(cell)
652          {
653             Class dataType = field.dataType;
654
655             if(dataType)
656             {
657                if(dataType.type == normalClass || dataType.type == noHeadClass)
658                {
659                   if(cell.data[0] && field.freeData)
660                      ((void (*)(void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnFree])(dataType, cell.data[0]);
661                   cell.data[0] = null;
662                }
663                else
664                {
665                   // Free old data first
666                   ((void (*)(void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnFree])(dataType, cell.data);
667                }
668             }
669             cell.isSet = false;
670             listBox.Update(null);
671          }
672       }
673    }
674
675    DataRow FindRow(int64 tag)
676    {
677       DataRow row = null;
678       for(row = subRows.first; row; row = row.next)
679       {
680          if(!row.noneRow && row.tag == tag)
681             break;
682       }
683       return row;
684    }
685
686    DataRow FindSubRow(int64 tag)
687    {
688       DataRow row = null;
689       for(row = subRows.first; row; row = row.next)
690       {
691          if(!row.noneRow && row.tag == tag)
692             break;
693          if(row.subRows.first)
694          {
695             DataRow subRow = row.FindSubRow(tag);
696             if(subRow)
697                return subRow;
698          }
699       }
700       return row;
701    }
702
703    DataRow AddRowAfter(DataRow after)
704    {
705       if(this)
706       {
707          DataRow row { };
708          incref row;
709          if(row)
710          {
711             DataField field;
712             int c;
713             subRows.Insert(after, row);
714             row.listBox = listBox;
715             *&row.parent = this;
716
717             row.cells.Clear();
718             for(c = 0; c<listBox.fields.count; c++)
719             {
720                for(field = listBox.fields.first; field; field = field.next)
721                   if((int)field.index == c)
722                      break;
723                if(field)
724                {
725                   int size = (field.dataType && field.dataType.typeSize) ?
726                      (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
727                   ListBoxCell cell = (ListBoxCell)new0 byte[size];
728                   row.cells.Add(cell);
729                   FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
730                   cell.isSet = false;
731                }
732             }
733
734             if(IsExpanded())
735             {
736                DataRow search;
737
738                if(after && after.subRows.first && !after.collapsed)
739                {
740                   for(search = after.subRows.last; !search.collapsed && search.subRows.last; )
741                      search = search.subRows.last;
742                   row.index = search.index + 1;
743                }
744                else
745                   row.index = after ? (after.index + 1) : (index + 1);
746
747                listBox.rowCount++;
748
749                for(search = row.GetNextRow(); search; search = search.GetNextRow())
750                   search.index++;
751
752                listBox.SetScrollArea(
753                   listBox.width,
754                   (listBox.rowCount * listBox.rowHeight) +
755                   ((listBox.style.header) ? listBox.rowHeight : 0) -
756                   ((!((listBox.clientSize.h+1) % listBox.rowHeight)) ? listBox.rowHeight : 0), true);
757                if(listBox.style.autoScroll)
758                   listBox.SetScrollPosition(0, MAXINT - listBox.rowHeight);
759             }
760
761             listBox.modifiedDocument = true;
762          }
763 #ifdef _DEBUG
764          listBox.CheckConsistency();
765 #endif
766          return row;
767       }
768       return null;
769    }
770
771    DataRow AddRow()
772    {
773       if(this)
774          return AddRowAfter(subRows.last);
775       return null;
776    }
777
778    DataRow AddStringf(const char * format, ...)
779    {
780       if(this)
781       {
782          DataRow row;
783          char string[MAX_F_STRING];
784          va_list args;
785          va_start(args, format);
786          vsnprintf(string, sizeof(string), format, args);
787          string[sizeof(string)-1] = 0;
788          va_end(args);
789
790          row = AddRow();
791          row.SetData(null, string);
792          return row;
793       }
794       return null;
795    }
796
797    DataRow AddString(const char * string)
798    {
799       if(this)
800       {
801          DataRow row;
802          row = AddRow();
803          row.SetData(listBox.fields.first, string);
804          return row;
805       }
806       return null;
807    }
808
809
810 private:
811    DataRow()
812    {
813       subRows.offset = (uint)(uintptr)&((DataRow)0).prev;
814    }
815
816    ~DataRow()
817    {
818       ListBoxCell cell, next;
819       uint cellIndex = 0;
820       DataRow subRow;
821
822       // Delete SubRows
823       while((subRow = subRows.first))
824       {
825          subRows.Remove(subRow);
826          delete subRow;
827       }
828
829       for(cell = cells.first; cell; cell = next, cellIndex++)
830       {
831          if(listBox)
832          {
833             DataField field;
834             for(field = listBox.fields.first; field && field.index != cellIndex; field = field.next);
835             if(field && field.dataType)
836             {
837                // TOCHECK: Is this check good? Will need incref/decref sometime?
838                if(field.dataType.type == normalClass || field.dataType.type == noHeadClass)
839                {
840                   if(cell.data[0] && field.freeData)
841                      ((void (*)(void *, void *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnFree])(field.dataType, cell.data[0]);
842                }
843                else
844                   ((void (*)(void *, void *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnFree])(field.dataType, cell.data);
845             }
846          }
847          next = cell.next;
848          delete cell;
849       }
850    }
851
852    bool IsExpanded()
853    {
854       return !this || (!collapsed && (!parent || parent.IsExpanded()));
855    }
856
857    int Compare(DataRow b, DataField sortField)
858    {
859       int result = 0;
860       ListBoxCell cell1, cell2;
861       uint index;
862       for(index = 0, cell1 = cells.first, cell2 = b.cells.first;
863           index != sortField.index;
864           index++, cell1 = cell1.next, cell2 = cell2.next);
865    /*
866       if(!cell1.isSet && !cell2.isSet)
867          result = 0;
868       else if(!cell1.isSet)
869          result = -1;
870       else if(!cell2.isSet)
871          result = 1;
872       else */
873       if(noneRow && !b.noneRow) return -1;
874       else if(!noneRow && b.noneRow) return 1;
875       else if(noneRow && b.noneRow) return 0;
876
877       if(sortField.dataType._vTbl[__ecereVMethodID_class_OnCompare])
878       {
879          if(sortField.dataType.type == normalClass || sortField.dataType.type == noHeadClass)
880          {
881             result = ((int (*)(void *, void *, void *))(void *)sortField.dataType._vTbl[__ecereVMethodID_class_OnCompare])(sortField.dataType,
882                (cell1.isSet && cell1.data) ? cell1.data[0] : null,
883                (cell2.isSet && cell2.data) ? cell2.data[0] : null);
884          }
885          else
886          {
887             result = ((int (*)(void *, void *, void *))(void *)sortField.dataType._vTbl[__ecereVMethodID_class_OnCompare])(sortField.dataType,
888                cell1.isSet ? cell1.data : null,
889                cell2.isSet ? cell2.data : null);
890          }
891       }
892       return sortField.sortOrder * result;
893    }
894
895    void _SortSubRows(DataField field, int order)
896    {
897       DataRow search;
898       for(search = subRows.first; search; search = search.next)
899          search._SortSubRows(field, order);
900       subRows.Sort(Compare, field);
901    }
902
903    public void SortSubRows(bool scrollToCurrent)
904    {
905       if(this && listBox && listBox.sortField)
906       {
907          _SortSubRows(listBox.sortField, listBox.sortField.sortOrder);
908
909          {
910             DataRow search;
911             int ix = index;
912             for(search = this; search; search = search.GetNextRow())
913                search.index = ix++;
914          }
915          if(scrollToCurrent)
916          {
917             int headerSize = ((listBox.style.header) ? listBox.rowHeight : 0);
918             int height = listBox.clientSize.h + 1 - headerSize;
919             if(listBox.currentRow && listBox.currentRow.index * listBox.rowHeight > listBox.scroll.y + height - listBox.rowHeight)
920                listBox.SetScrollPosition(listBox.scroll.x, listBox.currentRow.index * listBox.rowHeight - height + listBox.rowHeight);
921             else if(!listBox.currentRow || listBox.currentRow.index * listBox.rowHeight < listBox.scroll.y)
922                listBox.SetScrollPosition(listBox.scroll.x, listBox.currentRow ? listBox.currentRow.index * listBox.rowHeight : 0);
923             listBox.OnVScroll(0, listBox.scroll.y, 0);
924          }
925       }
926    }
927
928    public DataRow GetPrevRow()
929    {
930       DataRow row;
931       // Find Previous row
932       if(prev)
933       {
934          for(row = prev; row && !row.collapsed && row.subRows.last; row = row.subRows.last);
935       }
936       else
937          row = parent;
938       return row;
939    }
940
941    public DataRow GetNextRow()
942    {
943       DataRow row;
944       // Find Next row
945       if(subRows.first && !collapsed)
946          row = subRows.first;
947       else
948       {
949          for(row = this; row; row = row.parent)
950          {
951             if(row.next) { row = row.next; break; }
952          }
953       }
954       return row;
955    }
956
957    private DataRow GetLastRow()
958    {
959       DataRow row = this;
960       while(row && !row.collapsed && row.subRows.last)
961          row = row.subRows.last;
962       return row;
963    }
964
965    DataRow prev, next;
966    OldList cells;
967    int64 tag;
968    SelectedFlag selectedFlag;
969    ListBox listBox;
970    bool header;
971    OldList subRows;
972    DataRow parent;
973    bool collapsed;
974    BitmapResource icon;
975    int index;
976    bool noneRow;
977 };
978
979 public class ListBox : CommonControl
980 {
981    hasVertScroll = true;
982    // background = white;
983    borderStyle = deep;
984    snapVertScroll = true;
985
986    class_property(icon) = "<:ecere>controls/listBox.png";
987
988 public:
989    // Properties
990    property bool freeSelect { property_category $"Behavior" set { style.freeSelect = value; } get { return style.freeSelect; } };
991    property DataRow currentRow { property_category $"Private" /*"Behavior"*/ set { if(this) SetCurrentRow(value, false); } get { return this ? currentRow : null; } };
992    property DataField currentField
993    {
994       get { return currentField; }
995       // TODO: Document what this does
996       set
997       {
998          currentField = value;
999          HideEditBox(true, true, false);
1000          if(value && value.editable)
1001             PopupEditBox(currentField, false);
1002       }
1003    };
1004
1005    property int rowHeight
1006    {
1007       property_category $"Appearance"
1008       isset { return style.heightSet; }
1009       set
1010       {
1011          if(value)
1012          {
1013             style.heightSet = true;
1014             rowHeight = value;
1015             SetScrollLineStep(8, value);
1016          }
1017          else
1018          {
1019             style.heightSet = false;
1020             OnLoadGraphics();
1021             OnApplyGraphics();
1022          }
1023       }
1024       get { return this ? rowHeight : 0; }
1025    };
1026    property Seconds typingTimeout
1027    {
1028       property_category $"Behavior"
1029       set
1030       {
1031          typedString[0] = '\0';
1032          typingTimer.delay = value;
1033          typingTimeOut = value;
1034       }
1035       get { return typingTimeOut; }
1036    };
1037    property bool moveRows { property_category $"Behavior" set { style.moveRows = value; } get { return style.moveRows; } };
1038    property bool moveFields { property_category $"Behavior" set { style.moveFields = value; } get { return style.moveFields; } };
1039    property bool resizable { property_category $"Behavior" set { style.resizable = value; } get { return style.resizable; } };
1040    property bool autoScroll { property_category $"Behavior" set { style.autoScroll = value; } get { return style.autoScroll; } };
1041    property bool alwaysHighLight { property_category $"Appearance" set { style.alwaysHL = value; } get { return style.alwaysHL; } };
1042    property bool hasClearHeader { property_category $"Appearance" set { style.clearHeader = value; if(value) property::hasHeader = true; } get { return style.clearHeader; } };
1043    property bool hasHeader
1044    {
1045       property_category $"Appearance"
1046       set
1047       {
1048          if(value && !style.header)
1049          {
1050             endBevel = Button
1051             {
1052                this;
1053                stayOnTop = true;
1054                bevel = !guiApp.textMode && !style.clearHeader;
1055                dontScrollVert = true;
1056                inactive = true;
1057                size.h = rowHeight;
1058                NotifyPushed = HeaderPushed;
1059                NotifyClicked = HeaderClicked;
1060                NotifyDoubleClick = HeaderDoubleClicked;
1061                NotifyReleased = HeaderReleased;
1062                NotifyMouseMove = HeaderMouseMove;
1063             };
1064             incref endBevel;
1065             endBevel.Create();
1066             endBevel.visible = false;
1067          }
1068          style.header = value;
1069       }
1070       get { return style.header; }
1071    };
1072    property bool multiSelect { property_category $"Behavior" set { style.multiSelect = value; } get { return style.multiSelect; } };
1073    property bool alwaysEdit { property_category $"Behavior" set { style.alwaysEdit = value; } get { return style.alwaysEdit; } };
1074    property bool fullRowSelect { property_category $"Appearance" set { style.fullRowSelect = value; } get { return style.fullRowSelect; } };
1075    property bool collapseControl { property_category $"Appearance" set { style.collapse = value; } get { return style.collapse; } };
1076    property bool treeBranches { property_category $"Appearance" set { style.treeBranch = value; } get { return style.treeBranch; } };
1077    property bool rootCollapseButton { property_category $"Appearance" set { style.rootCollapse = value; } get { return style.rootCollapse; } };
1078    property bool sortable { property_category $"Behavior" set { style.sortable = value; } get { return style.sortable; } };
1079    property bool noDragging { property_category $"Behavior" set { style.noDragging = value; } get { return style.noDragging; } };
1080    property bool fillLastField
1081    {
1082       property_category $"Behavior"
1083       set
1084       {
1085          style.fillLastField = value;
1086       }
1087       get { return style.fillLastField; }
1088    };
1089    property int numSelections
1090    {
1091       get
1092       {
1093          int numSelections = 0;
1094          if(this && style.multiSelect)
1095          {
1096             DataRow row;
1097             for(row = rows.first; row; row = row.GetNextRow())
1098                if(row.selectedFlag == selected || row.selectedFlag == tempSelected)
1099                   numSelections++;
1100          }
1101          return numSelections;
1102       }
1103    };
1104    property int currentIndex
1105    {
1106       get { return currentRow ? currentRow.index : -1; }
1107    };
1108    property DataRow lastRow { get { return this ? rows.last : null; } };
1109    property DataRow firstRow { get { return this ? rows.first : null; } };
1110    property int rowCount { get { return rowCount; } };
1111    property DataField firstField { get { return this ? fields.first : null; } };
1112    property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
1113    property Color selectionText  { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
1114    property Color stippleColor { set { stippleColor = value; } get { return stippleColor; } };
1115    property bool expandOnAdd { set { style.expandOnAdd = value; } get { return style.expandOnAdd; } };
1116
1117    // Notifications
1118    virtual bool Window::NotifySelect(ListBox listBox, DataRow row, Modifiers mods);
1119    virtual bool Window::NotifyHighlight(ListBox listBox, DataRow row, Modifiers mods);
1120    virtual bool Window::NotifyReclick(ListBox listBox, DataRow row, Modifiers mods);
1121    virtual bool Window::NotifySort(ListBox listBox, DataField field, Modifiers mods);
1122    virtual bool Window::NotifyChanged(ListBox listBox, DataRow row);
1123    virtual bool Window::NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch);
1124    virtual bool Window::NotifyEdited(ListBox listBox, DataRow row);
1125    virtual bool Window::NotifyEditDone(ListBox listBox, DataRow row);
1126    virtual bool Window::NotifyMovedField(ListBox listBox, DataField field, Modifiers mods);
1127    virtual bool Window::NotifyMove(ListBox listBox, DataRow row, Modifiers mods);
1128    virtual bool Window::NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods);
1129    virtual bool Window::NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods);
1130    virtual bool Window::NotifyResized(ListBox listBox, DataField field, Modifiers mods);
1131    virtual bool Window::NotifyCollapse(ListBox listBox, DataRow row, bool collapsed);
1132    virtual bool Window::NotifyKeyHit(ListBox listBox, DataRow row, Key key, unichar ch);
1133    virtual bool Window::NotifyModified(ListBox listBox, DataRow row);
1134    virtual bool Window::NotifyEditing(ListBox listBox, DataRow row);
1135    virtual void Window::NotifyMoved(ListBox listBox, DataRow row, Modifiers mods);
1136
1137 #ifdef _DEBUG
1138    private void CheckConsistency()
1139    {
1140 #if 0
1141       DataRow r;
1142       int index = 0;
1143       for(r = rows.first; r; r = r.GetNextRow())
1144       {
1145          if(r.index != index++)
1146             PrintLn("bug");
1147       }
1148 #endif
1149    }
1150 #endif
1151
1152    // Methods
1153    void AddField(DataField addedField)
1154    {
1155       if(this)
1156       {
1157          DataField field;
1158          if(fields.first && ((DataField)fields.first).defaultField)
1159          {
1160             DataField defaultField = fields.first;
1161             defaultField.Free();
1162             delete defaultField;
1163          }
1164          if(!addedField)
1165          {
1166             addedField = DataField { };
1167          }
1168
1169          incref addedField;
1170          addedField.listBox = this;
1171          fields.Add(addedField);
1172
1173          addedField.sortOrder = 1;
1174          addedField.index = numFields;
1175          numFields++;
1176          if(style.header)
1177          {
1178             addedField.headButton.Destroy(0);
1179             delete addedField.headButton;
1180             addedField.headButton = Button
1181             {
1182                this;
1183                stayOnTop = true;
1184                inactive = true;
1185                dontScrollVert = true;
1186                id = (int64)(intptr)addedField;
1187                text = addedField.header;
1188                bevel = (!guiApp.textMode && !style.clearHeader);
1189                ellipsis = true;
1190                alignment = addedField.alignment;
1191                NotifyPushed = HeaderPushed;
1192                NotifyClicked = HeaderClicked;
1193                NotifyDoubleClick = HeaderDoubleClicked;
1194                NotifyReleased = HeaderReleased;
1195                NotifyMouseMove = HeaderMouseMove;
1196             };
1197             incref addedField.headButton;
1198             addedField.headButton.Create();
1199
1200             if(guiApp.textMode)
1201                addedField.headButton.background = Color { 0, 170, 0 };
1202          }
1203          if(rows.first)
1204          {
1205             DataRow row;
1206             field = addedField;
1207             for(row = rows.first; row; )
1208             {
1209                int size = (field.dataType && field.dataType.typeSize) ?
1210                   (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
1211                ListBoxCell cell = (ListBoxCell)new0 byte[size];
1212                row.cells.Add(cell);
1213                FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
1214                cell.isSet = false;
1215
1216                if(row.subRows.first)
1217                   row = row.subRows.first;
1218                else
1219                {
1220                   for(; row; row = row.parent)
1221                   {
1222                      if(row.next) { row = row.next; break; }
1223                   }
1224                }
1225             }
1226          }
1227          OnResize(clientSize.w, clientSize.h);
1228       }
1229    }
1230
1231    void ClearFields()
1232    {
1233       if(this)
1234       {
1235          DataField field;
1236          Clear();    // Ensure data is cleared first
1237          while((field = fields.first))
1238          {
1239             field.Free();
1240             delete field;
1241          }
1242          endBevel.visible = false;
1243          sortField = null;
1244       }
1245    }
1246
1247    void RemoveField(DataField field)
1248    {
1249       if(this)
1250       {
1251          if(field)
1252          {
1253             int index = field.index;
1254             DataRow row;
1255
1256             if(sortField == field)
1257                sortField = null;
1258
1259             for(row = rows.first; row; )
1260             {
1261                int c;
1262                ListBoxCell cell;
1263
1264                for(cell = row.cells.first, c = 0; c < index && cell; c++, cell = cell.next);
1265                if(cell && index == c)
1266                {
1267                   if(field.dataType)
1268                   {
1269                      // TOCHECK: Is this check good? Will need incref/decref sometime?
1270                      if(field.dataType.type == normalClass || field.dataType.type == noHeadClass)
1271                      {
1272                         if(cell.data[0] && field.freeData)
1273                            ((void (*)(void *, void *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnFree])(field.dataType, cell.data[0]);
1274                      }
1275                      else
1276                         ((void (*)(void *, void *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnFree])(field.dataType, cell.data);
1277                   }
1278
1279                   row.cells.Remove(cell);
1280                   delete cell;
1281                }
1282
1283                if(row.subRows.first)
1284                   row = row.subRows.first;
1285                else
1286                {
1287                   for(; row; row = row.parent)
1288                   {
1289                      if(row.next) { row = row.next; break; }
1290                   }
1291                }
1292             }
1293
1294             field.Free();
1295             delete field;
1296          }
1297          if(!fields.count)
1298             endBevel.visible = false;
1299       }
1300    }
1301
1302    DataRow AddRowNone()
1303    {
1304       DataRow row { noneRow = true };
1305       incref row;
1306       if(row)
1307       {
1308          DataRow search;
1309
1310          row.index = 0;
1311          rows.Insert(null, row);
1312          row.listBox = this;
1313
1314          for(search = row.GetNextRow(); search; search = search.GetNextRow())
1315             search.index++;
1316
1317          rowCount++;
1318          row.cells.Clear();
1319
1320          firstRowShown = row;
1321
1322          SetScrollArea(
1323             width,
1324             (rowCount * rowHeight) +
1325             ((style.header) ? rowHeight : 0) -
1326             ((!((clientSize.h+1) % rowHeight)) ? rowHeight : 0), true);
1327          if(style.autoScroll)
1328             SetScrollPosition(0, MAXINT - rowHeight);
1329          modifiedDocument = true;
1330          return row;
1331       }
1332       return null;
1333    }
1334
1335    DataRow AddRow()
1336    {
1337       if(this)
1338       {
1339          if(fields.first)
1340          {
1341             DataRow row { };
1342             incref row;
1343             if(row)
1344             {
1345                DataField field;
1346                int c;
1347
1348                // Find very last row
1349                {
1350                   DataRow lastRow;
1351                   for(lastRow = rows.last; lastRow && !lastRow.collapsed && lastRow.subRows.last; lastRow = lastRow.subRows.last);
1352                   row.index = lastRow ? (lastRow.index + 1) : 0;
1353                }
1354
1355                rows.Add(row);
1356                row.listBox = this;
1357                rowCount++;
1358
1359                row.cells.Clear();
1360                for(c = 0; c<fields.count; c++)
1361                {
1362                   for(field = fields.first; field; field = field.next)
1363                      if((int)field.index == c)
1364                         break;
1365                   if(field)
1366                   {
1367                      int size = (field.dataType && field.dataType.typeSize) ?
1368                         (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
1369                      ListBoxCell cell = (ListBoxCell) new0 byte[size];
1370                      row.cells.Add(cell);
1371                      FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
1372                      cell.isSet = false;
1373                   }
1374                }
1375
1376                if(!firstRowShown)
1377                {
1378                   firstRowShown = row;
1379                }
1380
1381                if(rowHeight)
1382                   SetScrollArea(
1383                      width,
1384                      (rowCount * rowHeight) +
1385                      ((style.header) ? rowHeight : 0) -
1386                      ((!((clientSize.h+1) % rowHeight)) ? rowHeight : 0), true);
1387                if(style.autoScroll)
1388                   SetScrollPosition(0, MAXINT - rowHeight);
1389                modifiedDocument = true;
1390             }
1391 #ifdef _DEBUG
1392             CheckConsistency();
1393 #endif
1394             return row;
1395          }
1396       }
1397       return null;
1398    }
1399
1400    DataRow AddRowAfter(DataRow after)
1401    {
1402       if(fields.first)
1403       {
1404          DataRow row { };
1405          incref row;
1406          if(row)
1407          {
1408             DataRow search;
1409             DataField field;
1410             int c;
1411
1412             if(after && after.subRows.first && !after.collapsed)
1413             {
1414                for(search = after.subRows.last; !search.collapsed && search.subRows.last; )
1415                   search = search.subRows.last;
1416                row.index = search.index + 1;
1417             }
1418             else
1419                row.index = after ? (after.index + 1) : 0;
1420             rows.Insert(after, row);
1421             row.listBox = this;
1422
1423             for(search = row.GetNextRow(); search; search = search.GetNextRow())
1424                search.index++;
1425
1426             rowCount++;
1427             row.cells.Clear();
1428             for(c = 0; c<fields.count; c++)
1429             {
1430                for(field = fields.first; field; field = field.next)
1431                   if((int)field.index == c)
1432                      break;
1433                if(field)
1434                {
1435                   int size = (field.dataType && field.dataType.typeSize) ?
1436                      (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
1437                   ListBoxCell cell = (ListBoxCell) new0 byte[size];
1438                   row.cells.Add(cell);
1439                   FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
1440                   cell.isSet = false;
1441                }
1442             }
1443             if(!firstRowShown || !after)
1444             {
1445                firstRowShown = row;
1446             }
1447
1448             SetScrollArea(
1449                width,
1450                (rowCount * rowHeight) +
1451                ((style.header) ? rowHeight : 0) -
1452                ((!((clientSize.h+1) % rowHeight)) ? rowHeight : 0), true);
1453             if(style.autoScroll)
1454                SetScrollPosition(0, MAXINT - rowHeight);
1455             modifiedDocument = true;
1456 #ifdef _DEBUG
1457             CheckConsistency();
1458 #endif
1459             return row;
1460          }
1461       }
1462       return null;
1463    }
1464
1465    DataRow AddStringf(const char * format, ...)
1466    {
1467       if(this)
1468       {
1469          DataRow row;
1470
1471          char string[MAX_F_STRING];
1472          va_list args;
1473
1474          va_start(args, format);
1475          vsnprintf(string, sizeof(string), format ? format : "", args);
1476          string[sizeof(string)-1] = 0;
1477          va_end(args);
1478
1479          row = AddRow();
1480          row.SetData(fields.first, string);
1481          return row;
1482       }
1483       return null;
1484    }
1485
1486    DataRow AddString(const char * string)
1487    {
1488       if(this)
1489       {
1490          DataRow row;
1491          row = AddRow();
1492          row.SetData(fields.first, string);
1493          return row;
1494       }
1495       return null;
1496    }
1497
1498    void SelectRow(DataRow row)
1499    {
1500       SetCurrentRow(row, true);
1501    }
1502
1503    void DeleteRow(DataRow row)
1504    {
1505       if(!row) row = currentRow;
1506       if(row)
1507       {
1508          DataRow sub, next, search;
1509          // Trying to move this here (Messed up deleting watches)
1510          //HideEditBox(false, false, true);
1511
1512          // Delete Sub Rows
1513          for(sub = row.subRows.first; sub; sub = next)
1514          {
1515             next = sub.next;
1516             DeleteRow(sub);
1517          }
1518
1519          if(row.parent.IsExpanded())
1520          {
1521             for(search = row.GetNextRow(); search; search = search.GetNextRow())
1522                search.index--;
1523             rowCount--;
1524          }
1525
1526          HideEditBox(false, false, true);
1527
1528          if(row == clickedRow)
1529          {
1530             clickedRow = row.GetNextRow();
1531             if(!clickedRow)
1532                clickedRow = row.GetPrevRow();
1533          }
1534
1535          if(row == currentRow)
1536          {
1537             DataRow newCurrentRow = row.GetNextRow();
1538             if(!newCurrentRow)
1539                newCurrentRow = row.GetPrevRow();
1540             SetCurrentRow(newCurrentRow, true);
1541          }
1542
1543          if(row == firstRowShown)
1544          {
1545             firstRowShown = row.GetPrevRow();
1546             if(!firstRowShown)
1547                firstRowShown = row.GetNextRow();
1548          }
1549
1550          (row.parent ? row.parent.subRows: rows).Remove(row);
1551          delete row;
1552
1553          //HideEditBox(false, false, true);
1554
1555          SetScrollArea(
1556             width,
1557             (rowCount * rowHeight) +
1558             ((style.header) ? rowHeight : 0) -
1559             ((!((clientSize.h+1) % rowHeight)) ? rowHeight : 0), true);
1560
1561          modifiedDocument = true;
1562
1563          Update(null);
1564 #ifdef _DEBUG
1565          CheckConsistency();
1566 #endif
1567       }
1568    }
1569
1570    DataRow FindRow(int64 tag)
1571    {
1572       if(this)
1573       {
1574          DataRow row = null;
1575          for(row = rows.first; row; row = row.next)
1576          {
1577             if(!row.noneRow && row.tag == tag)
1578                break;
1579          }
1580          return row;
1581       }
1582       return null;
1583    }
1584
1585    DataRow FindString(const char * searchedString)
1586    {
1587       DataField field;
1588       bool checkNextField = true;
1589
1590       for(field = fields.first; field; field = field.next)
1591       {
1592          if(field.dataType._vTbl[__ecereVMethodID_class_OnGetString])
1593          {
1594             DataRow row;
1595             for(row = rows.first; row; row = row.GetNextRow())
1596             {
1597                if(!row.noneRow)
1598                {
1599                   void * data = row.GetData(field);
1600                   char tempString[1024] = "";
1601                   bool needClass = false;
1602                   const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
1603
1604                   if(string && string[0])
1605                      checkNextField = false;
1606                   if(string && string[0] && !strcmp(string, searchedString))
1607                      return row;
1608                }
1609             }
1610          }
1611          if(!checkNextField) break;
1612       }
1613       return null;
1614    }
1615
1616    DataRow FindSubString(const char * subString)
1617    {
1618       DataField field;
1619       bool checkNextField = true;
1620       int len = subString ? strlen(subString) : 0;
1621
1622       if(len)
1623       {
1624          for(field = fields.first; field; field = field.next)
1625          {
1626             if(field.dataType._vTbl[__ecereVMethodID_class_OnGetString])
1627             {
1628                DataRow row;
1629                for(row = rows.first; row; row = row.GetNextRow())
1630                {
1631                   if(!row.noneRow)
1632                   {
1633                      void * data = row.GetData(field);
1634                      char tempString[1024] = "";
1635                      bool needClass = false;
1636                      const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
1637
1638                      if(string && string[0])
1639                         checkNextField = false;
1640                      if(string && string[0] && !strncmp(string, subString, len))
1641                         return row;
1642                   }
1643                }
1644             }
1645             if(!checkNextField) break;
1646          }
1647       }
1648       return null;
1649    }
1650
1651    DataRow FindSubStringi(const char * subString)
1652    {
1653       DataField field;
1654       bool checkNextField = true;
1655       int len = subString ? strlen(subString) : 0;
1656       DataRow result = null;
1657       const char * bestResult = null;
1658       int bestLen = 0;
1659
1660       if(len)
1661       {
1662          for(field = fields.first; field; field = field.next)
1663          {
1664             if(field.dataType._vTbl[__ecereVMethodID_class_OnGetString])
1665             {
1666                DataRow row;
1667                for(row = rows.first; row; row = row.GetNextRow())
1668                {
1669                   if(!row.noneRow)
1670                   {
1671                      void * data = row.GetData(field);
1672                      char tempString[1024] = "";
1673                      bool needClass = false;
1674                      const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
1675
1676                      if(string && string[0])
1677                         checkNextField = false;
1678                      if(string && string[0])
1679                      {
1680                         int stringLen = strlen(string);
1681
1682                         if(!strnicmp(string, subString, Min(len, stringLen)))
1683                         {
1684                            if(bestLen < Min(len, stringLen))
1685                               bestResult = 0;
1686                            if(!bestResult || strcmpi(string, bestResult) < 0)
1687                            {
1688                               bestLen = Min(len, stringLen);
1689                               bestResult = string;
1690                               result = row;
1691                            }
1692                         }
1693                      }
1694                   }
1695                }
1696             }
1697             if(!checkNextField) break;
1698          }
1699       }
1700       return result;
1701    }
1702
1703    DataRow FindSubStringAfter(DataRow after, const char * subString)
1704    {
1705       DataField field;
1706       bool checkNextField = true;
1707       int len = subString ? strlen(subString) : 0;
1708
1709       if(len)
1710       {
1711          for(field = fields.first; field; field = field.next)
1712          {
1713             if(field.dataType._vTbl[__ecereVMethodID_class_OnGetString])
1714             {
1715                DataRow row;
1716                for(row = after.GetNextRow(); row && row != after; row = row.GetNextRow(), (!row) ? (row = rows.first) : null)
1717                {
1718                   if(!row.noneRow)
1719                   {
1720                      void * data = row.GetData(field);
1721                      char tempString[1024] = "";
1722                      bool needClass = false;
1723                      const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
1724
1725                      if(string && string[0])
1726                         checkNextField = false;
1727                      if(string && string[0] && !strncmp(string, subString, len))
1728                         return row;
1729                   }
1730                }
1731             }
1732             if(!checkNextField) break;
1733          }
1734       }
1735       return null;
1736    }
1737
1738    DataRow FindSubRow(int64 tag)
1739    {
1740       if(this)
1741       {
1742          DataRow row = null;
1743
1744          for(row = rows.first; row; row = row.next)
1745          {
1746             if(!row.noneRow && row.tag == tag)
1747                break;
1748             if(!row.noneRow && row.subRows.first)
1749             {
1750                DataRow subRow = row.FindSubRow(tag);
1751                if(subRow)
1752                   return subRow;
1753             }
1754          }
1755          return row;
1756       }
1757       return null;
1758    }
1759
1760    void Clear()
1761    {
1762       if(this)
1763       {
1764          Window master = this.master;
1765
1766          HideEditBox(false, true, false);
1767          editData.Destroy(0);
1768
1769          firstRowShown = currentRow = null;
1770          ClearEx();
1771
1772          if(master)
1773          {
1774             if(style.freeSelect)
1775                NotifyHighlight(master, this, currentRow ? currentRow : null, 0);
1776             else
1777                NotifySelect(master, this, currentRow ? currentRow : null, 0);
1778          }
1779
1780          if(style.alwaysEdit && currentRow)
1781             currentRow.Edit(currentField);
1782
1783
1784          rowCount = 0;
1785
1786          SetScrollArea(
1787                width,
1788                (rowCount * rowHeight) +
1789                ((style.header) ? rowHeight : 0) -
1790                ((rowHeight && !((clientSize.h+1) % rowHeight)) ? rowHeight : 0), true);
1791          Update(null);
1792       }
1793    }
1794
1795    void Sort(DataField field, int order)
1796    {
1797       if(this)
1798       {
1799          DataRow search;
1800          int headerSize = ((style.header) ? rowHeight : 0);
1801          int height = clientSize.h + 1 - headerSize;
1802
1803          if(!field) field = fields.first;
1804          sortField = field;
1805          field.sortOrder = order ? order : 1;
1806          rows.Sort(DataRow::Compare, field);
1807
1808          for(search = rows.first; search; search = search.next)
1809             search._SortSubRows(field, order);
1810
1811          {
1812             int index = 0;
1813             for(search = rows.first; search; search = search.GetNextRow())
1814                search.index = index++;
1815          }
1816
1817          if(currentRow && currentRow.index * rowHeight > scroll.y + height - rowHeight)
1818             SetScrollPosition(scroll.x, currentRow.index * rowHeight - height + rowHeight);
1819          else if(!currentRow || currentRow.index * rowHeight < scroll.y)
1820             SetScrollPosition(scroll.x, currentRow ? currentRow.index * rowHeight : 0);
1821
1822          OnVScroll(0, scroll.y, 0);
1823
1824          // SetScrollPosition(0, scroll.y);
1825          // Update(null);
1826       }
1827    }
1828
1829    void StopEditing(bool save)
1830    {
1831       HideEditBox(save, false, true);
1832    }
1833
1834    void GetMultiSelection(OldList list)
1835    {
1836       list.Clear();
1837       if(this && style.multiSelect)
1838       {
1839          DataRow row;
1840
1841          for(row = rows.first; row; row = row.GetNextRow())
1842          {
1843             if(row.selectedFlag == selected || row.selectedFlag == tempSelected)
1844             {
1845                list.Add(OldLink { data = row });
1846             }
1847          }
1848       }
1849    }
1850
1851    // Convenience Current Row Methods
1852    void * SetData(DataField field, any_object data)
1853    {
1854       return currentRow.SetData(field, data);
1855    }
1856
1857    any_object GetData(DataField field)
1858    {
1859       return (void *)currentRow.GetData(field);
1860    }
1861
1862    int64 GetTag()
1863    {
1864       return currentRow ? currentRow.tag : 0;
1865    }
1866
1867 private:
1868    ListBox()
1869    {
1870       DataField defaultField { };
1871       rows.offset = (uint)(uintptr)&((DataRow)0).prev;
1872       fields.offset = (uint)(uintptr)&((DataField)0).prev;
1873       style.fullRowSelect = true;
1874       style.fillLastField = true;
1875       style.expandOnAdd = true;
1876       typingTimeOut = 0.5;
1877       rowHeight = 16;   // Stuff depending on creation and default property checking
1878       maxShown = 10;
1879
1880       defaultField.defaultField = true;
1881
1882       AddField(defaultField);
1883
1884       typedString = new char[1];
1885       typedString[0] = '\0';
1886       dropIndex = -1;
1887       return true;
1888    }
1889
1890    ~ListBox()
1891    {
1892       DataField field;
1893
1894       delete editData;
1895       delete typedString;
1896       delete endBevel;
1897
1898       ClearEx();
1899
1900       while((field = fields.first))
1901       {
1902          // fields.Remove(field);
1903          field.Free();
1904          delete field;
1905       }
1906    }
1907
1908    void ClearEx()
1909    {
1910       DataRow row;
1911       clickedRow = null;
1912       while((row = rows.first))
1913       {
1914          rows.Remove(row);
1915          delete row;
1916       }
1917    }
1918
1919    ListBoxCell GetCell(DataRow * row, DataField * field)
1920    {
1921       ListBoxCell cell = null;
1922       if(!*row) *row = currentRow;
1923       if(*row)
1924       {
1925          if(!*field) *field = this ? currentField : null;
1926          if(!*field && this)
1927          {
1928             for(*field = fields.first; (*field).index != 0; *field = (*field).next);
1929          }
1930          if(*field)
1931          {
1932             uint index;
1933             if(field->listBox == this)
1934             {
1935                for(index = 0, cell = (*row).cells.first; cell && index != (*field).index; index++, cell = cell.next);
1936             }
1937          }
1938       }
1939       return cell;
1940    }
1941
1942    void HideEditBox(bool save, bool alwaysStopEdit, bool repositionOnly)
1943    {
1944       if(editData && editData.visible)
1945       {
1946          if(save)
1947             editData.SaveData();
1948
1949          editData.visible = false;
1950          NotifyEditDone(master, this, currentRow);
1951
1952          // ENSURE DATA BOX IS NOT VISIBLE
1953          editData.visible = false;
1954
1955          if(style.alwaysEdit && !alwaysStopEdit)
1956          {
1957             /*
1958             int height = rowHeight - (style.alwaysEdit ? 1 : 0);
1959             int y = currentRow.index * rowHeight + (style.header ? rowHeight : 0);
1960             int x = currentField.x;
1961             int width = (!currentField.next && style.fillLastField && (!hasHorzScroll || clientSize.w - currentField.x > currentField.width + EXTRA_SPACE)) ?
1962                   clientSize.w - currentField.x : (currentField.width + EXTRA_SPACE);
1963
1964             if(!style.alwaysEdit)
1965             {
1966                editData.position = { x, y };
1967                editData.size = { width, height };
1968             }
1969             else
1970             {
1971                editData.position = { x, y - editData.clientStart.y };
1972                editData.size = { width, height + editData.clientStart.y * 2 };
1973             }
1974             editData.visible = true;
1975             if(style.alwaysEdit)
1976                editData.Deactivate();
1977             */
1978             PopupEditBox(currentField, repositionOnly);
1979          }
1980
1981          /*else
1982             currentField = null;*/
1983       }
1984    }
1985
1986    void SetCurrentRow(DataRow row, bool notify)
1987    {
1988       if(this && (currentRow != row || (currentRow && currentRow.selectedFlag == unselected)))
1989       {
1990          int headerSize = ((style.header) ? rowHeight : 0);
1991          int height = clientSize.h + 1 - headerSize;
1992
1993          // true: destroy edit box
1994          HideEditBox(true, true, false);
1995
1996          if(!(style.multiSelect) && currentRow)
1997             currentRow.selectedFlag = unselected;
1998
1999          currentRow = row;
2000
2001          if(style.multiSelect)
2002          {
2003             DataRow selRow;
2004
2005             for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
2006                selRow.selectedFlag = unselected;
2007             if(currentRow)
2008                currentRow.selectedFlag = selected;
2009
2010             clickedRow = row;
2011          }
2012          else if(currentRow)
2013             currentRow.selectedFlag = selected;
2014
2015          if(currentRow && currentRow.index * rowHeight > scroll.y + height - rowHeight)
2016             SetScrollPosition(scroll.x,
2017                currentRow.index * rowHeight - height + rowHeight);
2018          else if(!currentRow || currentRow.index * rowHeight < scroll.y)
2019          {
2020             int line = currentRow ? currentRow.index * rowHeight : 0;
2021             //SNAPUP(line, rowHeight);
2022             SetScrollPosition(scroll.x, line);
2023          }
2024
2025          if(notify)
2026          {
2027             Window master = this.master;
2028             if(master)
2029             {
2030                if(style.freeSelect && visible)
2031                   NotifyHighlight(master, this, currentRow ? currentRow : null, 0);
2032                else
2033                   NotifySelect(master, this, currentRow ? currentRow : null, 0);
2034                if(style.alwaysEdit && currentRow)
2035                   currentRow.Edit(currentField);
2036             }
2037          }
2038
2039          Update(null);
2040       }
2041    }
2042
2043    void RepositionFieldEditor()
2044    {
2045       if(editData && editData.visible)
2046       {
2047          int height = rowHeight - (style.alwaysEdit ? 1 : 0);
2048          int x = 0;
2049          int y = currentRow.index * rowHeight + (style.header ? rowHeight : 0);
2050          int width = 0;
2051          DataField field;
2052
2053          if(style.collapse && !(style.treeBranch))
2054             x += 15;
2055          for(field = fields.first; field; field = field.next)
2056          {
2057             width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
2058                clientSize.w - field.x : (field.width + EXTRA_SPACE);
2059             if(field == currentField) break;
2060             x += width;
2061          }
2062          if(!style.alwaysEdit)
2063          {
2064             editData.position = { x, y - editData.clientStart.y };
2065             editData.size = { width, height + editData.clientStart.y * 2 };
2066          }
2067          else
2068          {
2069             editData.position = { x, y };
2070             editData.size = { width, height };
2071          }
2072       }
2073    }
2074
2075    void PopupEditBox(DataField whichField, bool repositionOnly)
2076    {
2077       if((!editData || !editData.visible || currentField != whichField) && currentRow)
2078       {
2079          // true: destroy edit box
2080          HideEditBox(true, true, false);
2081          if(whichField)
2082          {
2083             int height = rowHeight - (style.alwaysEdit ? 1 : 0);
2084             int x = 0;
2085             int y = currentRow.index * rowHeight + (style.header ? rowHeight : 0);
2086             int width = 0;
2087             //void * data = currentRow.GetData(whichField);
2088             DataField field;
2089             DataRow row = null;
2090             ListBoxCell cell;
2091
2092             if(style.collapse && !(style.treeBranch))
2093                x += 15;
2094
2095             for(field = fields.first; field; field = field.next)
2096             {
2097                width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
2098                   clientSize.w - field.x : (field.width + EXTRA_SPACE);
2099                if(field == whichField) break;
2100                x += width;
2101             }
2102
2103             currentField = whichField;
2104             cell = GetCell(&row, &currentField);
2105
2106             if(!editData)
2107             {
2108                editData = DataBox
2109                {
2110                   this;
2111                   background = dataBoxBackground;
2112                   foreground = dataBoxForeground;
2113
2114                   bool NotifyChanged(DataBox dataBox, bool closingDropDown)
2115                   {
2116                      DataRow row = null;
2117                      DataField field = null;
2118                      ListBoxCell cell = GetCell(&row, &field);
2119                      if(cell)
2120                      {
2121                         cell.isSet = true;
2122                         modifiedDocument = true;
2123                         Update(null);
2124                         NotifyChanged(master, this, currentRow);
2125                      }
2126                      return true;
2127                   }
2128
2129                   bool NotifyModified()
2130                   {
2131                      //DataRow row = null;
2132                      //DataField field = null;
2133                      //ListBoxCell cell = GetCell(&row, &field);
2134                      //cell.isSet = true;
2135                      modifiedDocument = true;
2136                      //Update(null);
2137                      NotifyModified(master, this, currentRow);
2138                      return true;
2139                   }
2140
2141                   bool OnKeyDown(Key key, unichar ch)
2142                   {
2143                      bool result = DataBox::OnKeyDown(key, ch);
2144                      if(visible && active)   // Added this check here, because we will not use enter/escape otherwise, and lose DataBox's result
2145                      {
2146                         if((SmartKey)key == enter || (SmartKey)key == escape)
2147                            return true;
2148                      }
2149                      return result;
2150                   }
2151                };
2152                incref editData;
2153             }
2154             else
2155                editData.Destroy(0);
2156             editData.type = whichField.dataType;
2157             editData.fieldData = whichField.userData;
2158             editData.borderStyle = style.alwaysEdit ? 0 : deep;
2159             editData.data = cell ? cell.data : null;
2160
2161             if(!repositionOnly)
2162                // Might not really need this anymore...
2163                NotifyEditing(master, this, currentRow);
2164
2165             editData.Create();
2166             if(!style.alwaysEdit)
2167             {
2168                editData.position = { x, y - editData.clientStart.y };
2169                editData.size = { width, height + editData.clientStart.y * 2 };
2170             }
2171             else
2172             {
2173                editData.position = { x, y };
2174                editData.size = { width, height };
2175             }
2176             if(!repositionOnly)
2177                editData.Refresh();
2178             editData.visible = true;
2179
2180             if(style.alwaysEdit)
2181                editData.Deactivate();
2182
2183             //   MOVED THIS HIGHER FOR DATALIST EDITOR
2184             if(!repositionOnly)
2185                // Might not really need this anymore...
2186                NotifyEdited(master, this, currentRow);
2187          }
2188       }
2189    }
2190
2191    void OnRedraw(Surface surface)
2192    {
2193       DataRow row;
2194       int y = (style.header) ? rowHeight : 0;
2195       bool isActive = active;
2196       Font font = fontObject;
2197       Font boldFont = this.boldFont.font;
2198
2199       // Draw gray grid
2200       if(style.alwaysEdit && style.fullRowSelect)
2201       {
2202          // Horizontal lines
2203          int y = (style.header) ? rowHeight : 0;
2204          int x = -scroll.x - 1;// + EXTRA_SPACE;// / 2 - 1;
2205          int w = clientSize.w;
2206          int h = clientSize.h;
2207          DataRow row;
2208          DataField field;
2209
2210          // Fill out indent column
2211          if(style.collapse && !(style.treeBranch) && (style.header || rows.first))
2212          {
2213             x += 15;
2214             surface.SetBackground(formColor);
2215             surface.Area(-scroll.x, 0, x, clientSize.h);
2216          }
2217
2218          surface.SetForeground(formColor);
2219          for(row = firstRowShown; row; row = row.GetNextRow())
2220          {
2221             y += rowHeight;
2222             surface.HLine(x + 1, w-1, y-1);
2223             if(y >= h)
2224                break;
2225          }
2226
2227          // Vertical lines
2228          for(field = fields.first; field; field = field.next)
2229          {
2230             int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
2231                clientSize.w - field.x : (field.width + EXTRA_SPACE);
2232             if(field.prev && y > 0)
2233                surface.VLine(0, y-1, x);
2234             x += width;
2235          }
2236       }
2237
2238       surface.foreground = this.foreground;
2239       surface.TextOpacity(false);
2240
2241       // Draw the tree branches
2242       if(style.treeBranch)
2243       {
2244          int y = -scroll.y + ((style.header) ? rowHeight : 0);
2245          surface.LineStipple(0x5555);
2246          surface.SetForeground(branchesColor);
2247          for(row = rows.first; row; row = row.GetNextRow() )
2248          {
2249             int x = -scroll.x + EXTRA_SPACE / 2-1;
2250             int rowStart = -scroll.x;
2251             int indent = 0;
2252             DataRow parent;
2253             int plusIndent;
2254
2255             for(parent = row.parent; parent; parent = parent.parent)
2256                if(!parent.header) indent += 20;
2257             if(style.rootCollapse) indent += 20;
2258
2259             plusIndent = (style.treeBranch) ? (indent - 20 + 4) : indent;
2260
2261             x += indent;
2262
2263             // Vertical line
2264             if(row.subRows.first)
2265             {
2266                int numRows = 0;
2267                int y1 = y + PLUSY + 4;
2268                int y2;
2269                DataRow child;
2270
2271                for(child = row.collapsed ? null : row.subRows.first; child && child != row; )
2272                {
2273                   numRows++;
2274                   if(child.subRows.first && !child.collapsed && child != row.subRows.last)
2275                      child = child.subRows.first;
2276                   else if(child.next)
2277                      child = child.next;
2278                   else
2279                   {
2280                      for(; child && child != row; child = child.parent)
2281                      {
2282                         if(child.next)
2283                         {
2284                            child = child.next;
2285                            break;
2286                         }
2287                      }
2288                   }
2289                }
2290                y2 = y + numRows * rowHeight + PLUSY + 4;
2291                surface.VLine(y1, y2, rowStart + plusIndent + 7 + 20);
2292             }
2293             surface.HLine(rowStart + plusIndent + 7, rowStart + indent, y + PLUSY + 4);
2294
2295             y += rowHeight;
2296             if(y >= clientSize.h)
2297                break;
2298          }
2299          // Root Vertical Lines
2300          if(style.rootCollapse && rows.first)
2301          {
2302             int numRows = 0;
2303             int y1, y2;
2304             DataRow child;
2305             y = -scroll.y + ((style.header) ? rowHeight : 0);
2306             y1 = y + PLUSY + 4;
2307             for(child = rows.first; child && child != rows.last; )
2308             {
2309                numRows++;
2310                if(child.subRows.first && !child.collapsed && child != rows.last)
2311                   child = child.subRows.first;
2312                else if(child.next)
2313                   child = child.next;
2314                else
2315                {
2316                   for(; child; child = child.parent)
2317                   {
2318                      if(child.next)
2319                      {
2320                         child = child.next;
2321                         break;
2322                      }
2323                   }
2324                }
2325             }
2326             y2 = y + numRows * rowHeight + PLUSY + 4;
2327             surface.VLine(y1, y2, -scroll.x + 11);
2328          }
2329          surface.LineStipple(0);
2330       }
2331
2332       for(row = firstRowShown; row; row = row.GetNextRow() )
2333       {
2334          int x = -scroll.x + EXTRA_SPACE / 2-1;
2335          DataField field;
2336          ListBoxCell cell;
2337          Color foreground = this.foreground /*black*/, background = this.background /*white*/;
2338          DataDisplayFlags dataDisplayFlags = 0;
2339          int rowStart = -scroll.x;
2340          int indent = 0;
2341          DataRow parent;
2342          Bitmap icon = row.icon ? row.icon.bitmap : null;
2343          int collapseRowStart = 0;
2344          bool lastWasHeader = row.header;
2345
2346          for(parent = row.parent; parent; parent = parent.parent)
2347          {
2348             if(!parent.header || lastWasHeader)
2349             {
2350                if(style.treeBranch)
2351                   indent += 20;
2352                else
2353                   indent += 15;
2354             }
2355          }
2356          if(style.rootCollapse) indent += 20;
2357          x += indent;
2358
2359          dataDisplayFlags.fullRow = style.fullRowSelect;
2360          dataDisplayFlags.active = isActive;
2361          dataDisplayFlags.header = row.header;
2362
2363          surface.Clip(null);
2364          if(style.collapse)
2365          {
2366             collapseRowStart = rowStart;
2367
2368             if(!(style.treeBranch))
2369             {
2370                x += 15;
2371                rowStart += 15;
2372             }
2373          }
2374
2375          if(style.multiSelect)
2376          {
2377             dataDisplayFlags.selected = row.selectedFlag == selected || row.selectedFlag == tempSelected;
2378             dataDisplayFlags.current = row == currentRow || (!currentRow && row == firstRowShown);
2379          }
2380          else
2381          {
2382             dataDisplayFlags.selected = row.selectedFlag == selected || row.selectedFlag == tempSelected;
2383             dataDisplayFlags.current = row == currentRow || (!currentRow && row == firstRowShown);
2384             /*
2385             if(row == currentRow)
2386             {
2387                dataDisplayFlags.current = true;
2388                dataDisplayFlags.selectedFlag = true;
2389             }
2390             else if(!currentRow && row == firstRowShown)
2391             {
2392                dataDisplayFlags.current = true;
2393             }*/
2394          }
2395
2396          surface.TextOpacity(true);
2397
2398          background = this.background;
2399          foreground = this.foreground;
2400
2401          // Draw the current row background
2402          if(row.header)
2403          {
2404             Color colors[] = { formColor, azure, mistyRose, linen, floralWhite, lavender, lavenderBlush, lemonChiffon };
2405             int level = 0;
2406             DataRow p = row;
2407             while((p = p.parent)) level++;
2408             background = colors[level % (sizeof(colors)/sizeof(colors[0]))];
2409             surface.SetBackground(background);
2410             surface.Area(rowStart, y, clientSize.w, (y + rowHeight) - 1);
2411             foreground = branchesColor;
2412          }
2413          else if(dataDisplayFlags.selected)
2414          {
2415             if(dataDisplayFlags.selected && (isActive || style.alwaysHL || (style.alwaysEdit && style.fullRowSelect)))
2416             {
2417                if(!isActive && style.alwaysEdit)
2418                   background = formColor;
2419                else
2420                   background = selectionColor ? selectionColor : SELECTION_COLOR;
2421                if(style.fullRowSelect)
2422                {
2423                   int offset = (style.alwaysEdit) ? 2 : 1;
2424                   surface.SetBackground(background);
2425                   surface.Area(rowStart, y, clientSize.w, (y + rowHeight) - offset);
2426                }
2427                if(isActive || !(style.alwaysEdit))
2428                   foreground = selectionText ? selectionText : SELECTION_TEXT;
2429                else
2430                   foreground = branchesColor;
2431             }
2432          }
2433
2434          if(icon)
2435          {
2436             surface.Blit(icon, x + (20 - icon.width) /2,y + 2,0,0, icon.width, icon.height);
2437             x += 20;
2438          }
2439
2440          if(row.noneRow)
2441          {
2442             int width = clientSize.w;
2443             Box clip;
2444             dataDisplayFlags.firstField = true;
2445             clip.left = x - EXTRA_SPACE / 2+1;
2446             clip.top = y;
2447             clip.right = x + width - EXTRA_SPACE/2 - 0;
2448             clip.bottom = y + rowHeight - 1;
2449             surface.Clip(&clip);
2450
2451             surface.TextFont(font);
2452
2453             surface.SetForeground(foreground);
2454             surface.SetBackground(background);
2455
2456             ((void (*)(void *, const void *, void *, int, int, int, void *, uint, uint))(void *)class(String)._vTbl[__ecereVMethodID_class_OnDisplay])(class(String), "(none)", surface, x, y - 1 + (rowHeight - fontH)/2, width - EXTRA_SPACE/2, null, Alignment::left, dataDisplayFlags);
2457          }
2458          else
2459          {
2460             if(opacity < 1) surface.TextOpacity(false);
2461             // Draw the rows
2462             for(field = fields.first; field; field = field.next)
2463             {
2464                uint index;
2465                int width = ((!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) || row.header) ?
2466                   clientSize.w - field.x : (field.width + EXTRA_SPACE);
2467
2468                // Box clip = { x, y+1, x + field.width - EXTRA_SPACE - 1, y + rowHeight - 2 };
2469                Box clip;
2470
2471                //width -= EXTRA_SPACE;
2472
2473                if(!field.prev) width -= indent;
2474
2475
2476                dataDisplayFlags.firstField = field.prev ? false : true;
2477
2478                if(!dataDisplayFlags.firstField && !dataDisplayFlags.fullRow)
2479                {
2480                   background = this.background;
2481                   foreground = this.foreground;
2482                }
2483
2484                if(!isActive && dataDisplayFlags.selected && style.alwaysEdit && field.editable && opacity)
2485                {
2486                   surface.Clip(null);
2487                   surface.SetBackground(background);
2488                   surface.Area(x-3, y, x+width-1, y + rowHeight-2);
2489                }
2490
2491                clip.left = x - EXTRA_SPACE / 2+1;
2492                clip.top = y;
2493                clip.right = x + width - EXTRA_SPACE/2 - 0;
2494                clip.bottom = y + rowHeight - 1;
2495                surface.Clip(&clip);
2496
2497                for(index = 0, cell = row.cells.first; cell && index != field.index; index++, cell = cell.next);
2498                // Should always be as many cells in the row as fields in the listbox
2499                if(cell && cell.isSet && field.dataType)
2500                {
2501                   if(row.header)
2502                      surface.TextFont(boldFont);
2503                   else
2504                      surface.TextFont(font);
2505
2506                   surface.SetForeground(foreground);
2507                   surface.SetBackground(background);
2508
2509                   if(field.dataType.type == noHeadClass || field.dataType.type == normalClass)
2510                      ((void (*)(void *, void *, void *, int, int, int, void *, uint, uint))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnDisplay])(field.dataType, cell.data[0], surface, x, y - 1 + (rowHeight - fontH)/2, width - EXTRA_SPACE/2, field.userData, field.alignment, dataDisplayFlags);
2511                   else
2512                      ((void (*)(void *, void *, void *, int, int, int, void *, uint, uint))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnDisplay])(field.dataType, cell.data, surface, x, y - 1 + (rowHeight - fontH)/2, width - EXTRA_SPACE/2, field.userData, field.alignment, dataDisplayFlags);
2513                }
2514
2515                if(!isActive && dataDisplayFlags.selected && style.alwaysEdit && field.editable)
2516                   background = formColor;
2517
2518                if(!dataDisplayFlags.firstField && !dataDisplayFlags.fullRow)
2519                {
2520                   background = formColor;
2521                   foreground = this.background;
2522                }
2523
2524 #ifdef _DEBUG
2525                // surface.WriteTextf(x + 100, y,  "ix: %d", row.index);
2526 #endif
2527
2528                x += width;// + EXTRA_SPACE;
2529
2530                if(row.header) break;
2531             }
2532          }
2533
2534          if(style.collapse)
2535          {
2536             int plusIndent = (style.treeBranch) ? (indent - 20 + 4) : indent;
2537             surface.Clip(null);
2538             if(row.subRows.first && (row.parent || !(style.treeBranch) || (style.rootCollapse)))
2539             {
2540                surface.SetForeground(row.header ? headerCollapseForeground : this.foreground);
2541                surface.Rectangle(collapseRowStart + 3 + plusIndent, y + PLUSY, collapseRowStart + 11 + plusIndent, y + PLUSY + 8);
2542
2543                surface.SetBackground(row.header ? (formColor) : (this.background)); //white
2544                surface.Area(collapseRowStart + 4 + plusIndent, y + PLUSY + 1, collapseRowStart + 10 + plusIndent, y + PLUSY + 7);
2545
2546                surface.HLine(collapseRowStart + 5 + plusIndent, collapseRowStart + 9 + plusIndent, y+PLUSY+4);
2547                if(row.collapsed)
2548                   surface.VLine(y + PLUSY + 2, y + PLUSY + 6, collapseRowStart + 7 + plusIndent);
2549             }
2550
2551          }
2552
2553          // Draw the current row stipple
2554          if(style.fullRowSelect && !(style.alwaysEdit) && (dataDisplayFlags.current) && isActive)
2555          {
2556             surface.Clip(null);
2557             if(isActive)
2558             {
2559                surface.LineStipple(0x5555);
2560                if(dataDisplayFlags.selected)
2561                   surface.SetForeground(stippleColor);
2562                else
2563                   surface.SetForeground(this.foreground);
2564             }
2565             else
2566                surface.SetForeground(selectionColor ? selectionColor : SELECTION_COLOR);
2567             surface.Rectangle(0, y, clientSize.w-1, (y + rowHeight) - 1);
2568             surface.LineStipple(0);
2569          }
2570
2571          y += rowHeight;
2572          if(y >= clientSize.h)
2573             break;
2574       }
2575       if(firstRowShown) surface.Clip(null);
2576       if(dragRow && dropIndex != -1)
2577       {
2578          int ix = dropIndex;
2579          int y;
2580
2581          if(!style.multiSelect && currentRow.index < dropIndex)
2582             ix++;
2583          surface.DrawingChar(223);
2584
2585          y = style.header ? rowHeight : 0;
2586          y += ix * rowHeight - scroll.y;
2587
2588          surface.SetForeground(Color { 85, 85, 255 });
2589          surface.HLine(0, clientSize.w-1, y);
2590          surface.HLine(0, clientSize.w-1, y + 1);
2591       }
2592    }
2593
2594    void OnDrawOverChildren(Surface surface)
2595    {
2596       if(draggingField && dropField)
2597       {
2598          int position = dropField.x;
2599          if(draggingField.x < position)
2600             position += dropField.width + EXTRA_SPACE;
2601
2602          surface.SetForeground(Color { 85, 85, 255 });
2603          surface.VLine(0, rowHeight - 1, position - scroll.x - 2);
2604          surface.VLine(0, rowHeight - 1, position - scroll.x);
2605       }
2606       if(sortField && !style.clearHeader && style.header)
2607       {
2608          DataField field = sortField;
2609          int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
2610             clientSize.w - field.x : (field.width + EXTRA_SPACE);
2611          int tw = 0, th = 0;
2612          if(field.header)
2613             surface.TextExtent(field.header, strlen(field.header), &tw, &th);
2614          if(tw < width - EXTRA_SPACE)
2615          {
2616             bool up = field.sortOrder == 1;
2617             int x = 4, y = 4;
2618             Box clip =
2619             {
2620                field.x + 2 - scroll.x, 0,
2621                field.x + width + EXTRA_SPACE - 1 - scroll.x, rowHeight
2622             };
2623             surface.Clip(&clip);
2624             if(field.alignment == left || field.alignment == center)
2625             {
2626                if(field.alignment == center)
2627                   x = field.x + (width + EXTRA_SPACE - tw) / 2 + tw + EXTRA_SPACE + 4;
2628                else
2629                   x = field.x + tw + EXTRA_SPACE + 4;
2630
2631                x = Min(x, field.x + width - 4);
2632             }
2633             else if(field.alignment == right)
2634             {
2635                x = field.x + width - tw - 2*EXTRA_SPACE - 4;
2636                x = Max(x, field.x + 2);
2637             }
2638             x -= scroll.x;
2639
2640             if(guiApp.textMode)
2641             {
2642                // surface.SetForeground((wmenu.selectedFlag == item) ? white : black);
2643                // surface.WriteText(clientSize.w-8, y+(wmenu.rh - 8)/2, "\020", 1);
2644             }
2645             else
2646             {
2647                if(up)
2648                {
2649                   surface.SetForeground(Color { 128,128,128 } );
2650                   surface.DrawLine(x + 3, y, x, y + 5);
2651                   surface.PutPixel(x + 1, y + 5);
2652                   surface.PutPixel(x + 1, y + 3);
2653                   surface.PutPixel(x + 2, y + 1);
2654
2655                   surface.SetForeground(white);
2656                   surface.DrawLine(x + 4, y, x + 7, y + 5);
2657                   surface.PutPixel(x + 6, y + 5);
2658                   surface.PutPixel(x + 6, y + 3);
2659                   surface.PutPixel(x + 5, y + 1);
2660
2661                   surface.DrawLine(x, y + 6, x + 7, y + 6);
2662                }
2663                else
2664                {
2665                   surface.SetForeground(Color { 128,128,128 });
2666                   surface.DrawLine(x + 3, y+6, x, y+1);
2667                   surface.PutPixel(x + 1, y+1);
2668                   surface.PutPixel(x + 1, y+3);
2669                   surface.PutPixel(x + 2, y+5);
2670
2671                   surface.SetForeground(white);
2672                   surface.DrawLine(x + 4, y+6, x + 7, y+1);
2673                   surface.PutPixel(x + 6, y+1);
2674                   surface.PutPixel(x + 6, y+3);
2675                   surface.PutPixel(x + 5, y+5);
2676
2677                   surface.DrawLine(x, y, x + 7, y);
2678                }
2679             }
2680             surface.Clip(null);
2681          }
2682       }
2683
2684    }
2685
2686    void OnResize(int w, int h)
2687    {
2688       DataField field;
2689       bool showEndBevel = false;
2690       int x = 0;
2691       if(style.collapse && !style.treeBranch)
2692          x += 15;
2693       for(field = fields.first; field; field = field.next)
2694       {
2695          int width = field.width + EXTRA_SPACE;
2696          field.x = x;
2697          x += width;
2698       }
2699       width = x;
2700
2701       SetScrollArea(
2702          width,
2703          (rowCount * rowHeight) +
2704          ((style.header) ? rowHeight : 0) -
2705          ((!((clientSize.h+1) % rowHeight)) ? rowHeight : 0), true);
2706
2707       for(field = fields.first; field; field = field.next)
2708       {
2709          int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
2710             clientSize.w - field.x : (field.width + EXTRA_SPACE);
2711          if(style.header && field.headButton)
2712          {
2713             showEndBevel = true;
2714             if(width > 0)
2715             {
2716                field.headButton.position = { field.x, 0 };
2717                field.headButton.size = { width, rowHeight };
2718                field.headButton.visible = true;
2719             }
2720             else
2721                field.headButton.visible = false;
2722          }
2723       }
2724
2725       if(!style.fillLastField && showEndBevel && endBevel)
2726       {
2727          endBevel.position = { x, 0 };
2728          endBevel.size = { clientSize.w + 2 - x, rowHeight };
2729          endBevel.visible = true;
2730       }
2731       else if(endBevel)
2732          endBevel.visible = false;
2733
2734       if(style.alwaysEdit && editData && editData.visible)
2735       {
2736          HideEditBox(true, false, true);
2737       }
2738       else if(editData && editData.visible)
2739          RepositionFieldEditor();
2740    }
2741
2742    void AdaptToFieldWidth(DataField field, bool doScroll)
2743    {
2744       OnResize(clientSize.w, clientSize.h);
2745
2746       // Scroll appropriately
2747       if(doScroll)
2748       {
2749          if(field.x + field.width + EXTRA_SPACE - clientSize.w > scroll.x &&
2750                  field.x >= field.x + field.width + EXTRA_SPACE - clientSize.w)
2751          {
2752             SetScrollPosition(field.x + field.width + EXTRA_SPACE - clientSize.w, scroll.y);
2753          }
2754          else if(field.x + field.width + EXTRA_SPACE - clientSize.w > scroll.x ||
2755                  field.x < scroll.x)
2756             SetScrollPosition(field.x, scroll.y);
2757       }
2758       Update(null);
2759    }
2760
2761    bool HeaderPushed(Button control, int x, int y, Modifiers mods)
2762    {
2763       DataField field = (DataField)(intptr)control.id;
2764       // false: dont destroy edit box
2765       HideEditBox(true, false, true);
2766       if(style.resizable && ((!field && x < RESIZE_BORDER && fields.last) ||
2767          (field && x < RESIZE_BORDER && field.prev) ||
2768          (field && x >= control.clientSize.w - RESIZE_BORDER)))
2769       {
2770          if(!field)
2771             field = fields.last;
2772          else if(x < RESIZE_BORDER && field.prev)
2773             field = field.prev;
2774
2775          if(field.fixed) return false;
2776          resizingField = field;
2777          resizeX = x + control.position.x;
2778          startWidth = field.width;
2779          oldX = x - scroll.x;
2780          return false;
2781       }
2782       else if(field)
2783       {
2784          if(field.fixed) return false;
2785          draggingField = field;
2786          if(style.moveFields)
2787             field.headButton.stayDown = true;
2788          else if(!style.sortable)
2789             return false;
2790       }
2791       else
2792          return false;
2793       return true;
2794    }
2795
2796    bool HeaderMouseMove(Button control, int x, int y, Modifiers mods)
2797    {
2798       if(resizingField)
2799       {
2800          // Resize left
2801          DataField field = resizingField;
2802
2803          x += control.position.x;
2804
2805          // Tweak to prevent shrinking field if we're actually moving right
2806          if(x - scroll.x > oldX &&
2807             startWidth + x - resizeX < field.width)
2808          {
2809             oldX = x - scroll.x;
2810             return true;
2811          }
2812          oldX = x - scroll.x;
2813
2814          field.width = startWidth + x - resizeX;
2815          field.width = Max(field.width, - EXTRA_SPACE);
2816
2817          AdaptToFieldWidth(field, true);
2818       }
2819       else if(draggingField)
2820       {
2821          x += control.position.x;
2822          if(style.moveFields)
2823          {
2824             DataField field = fields.last;
2825             int fieldX = 0;
2826             for(field = fields.first; field; field = field.next)
2827             {
2828                fieldX += ((field.width || style.resizable) ?
2829                   field.width : clientSize.w) + EXTRA_SPACE;
2830                if(fieldX > x)
2831                   break;
2832             }
2833             if(draggingField == field)
2834             {
2835                // Reset scroll position
2836                if(field.x + field.width + EXTRA_SPACE - clientSize.w > scroll.x &&
2837                        field.x >= field.x + field.width + EXTRA_SPACE - clientSize.w)
2838                {
2839                   SetScrollPosition(
2840                      field.x + field.width + EXTRA_SPACE - clientSize.w,
2841                      scroll.y);
2842                }
2843                else if(field.x + field.width + EXTRA_SPACE - clientSize.w > scroll.x ||
2844                        field.x < scroll.x)
2845                   SetScrollPosition(field.x, scroll.y);
2846                field = null;
2847             }
2848             if(dropField != field)
2849             {
2850                dropField = field;
2851                if(field)
2852                {
2853                   int position = field.x;
2854                   // Moving right
2855                   if(draggingField.x < position)
2856                   {
2857                      position += field.width + EXTRA_SPACE - clientSize.w;
2858                      if(position > scroll.x)
2859                         SetScrollPosition(position, scroll.y);
2860                   }
2861                   // Moving Left
2862                   else
2863                   {
2864                      if(position < scroll.x)
2865                         SetScrollPosition(position, scroll.y);
2866                   }
2867
2868                   movingFields = true;
2869                }
2870                Update(null);
2871             }
2872          }
2873       }
2874       else if(style.resizable)
2875       {
2876          DataField field = (DataField)(intptr)control.id;
2877          if(field)
2878          {
2879             if(x < RESIZE_BORDER && field.prev)
2880             {
2881                if(!field.prev.fixed)
2882                   control.cursor = guiApp.GetCursor(sizeWE);
2883             }
2884             else if(x >= control.clientSize.w - RESIZE_BORDER)
2885                control.cursor = guiApp.GetCursor(sizeWE);
2886             else
2887                control.cursor = null;
2888          }
2889          else
2890          {
2891             if(x < RESIZE_BORDER && fields.last)
2892                control.cursor = guiApp.GetCursor(sizeWE);
2893             else
2894                control.cursor = null;
2895          }
2896       }
2897       return true;
2898    }
2899
2900    bool HeaderReleased(Button control, int x, int y, Modifiers mods)
2901    {
2902       if(resizingField)
2903       {
2904          NotifyResized(master, this, resizingField, mods);
2905          resizingField = null;
2906       }
2907
2908       if(draggingField)
2909       {
2910          bool result = true;
2911
2912          if(style.moveFields)
2913          {
2914             if(dropField)
2915             {
2916                // Find which field
2917                DataField switchField = fields.last;
2918                DataField field;
2919                int fieldX = 0;
2920
2921                x += draggingField.x;
2922                for(field = fields.first; field; field = field.next)
2923                {
2924                   fieldX += ((field.width || style.resizable) ?
2925                      field.width : clientSize.w) + EXTRA_SPACE;
2926                   if(fieldX > x)
2927                   {
2928                      switchField = field;
2929                      break;
2930                   }
2931                }
2932                if(switchField && draggingField != switchField && dropField)
2933                {
2934                   for(field = fields.first; field; field = field.next)
2935                   {
2936                      if(field == switchField || field == draggingField)
2937                         break;
2938                   }
2939
2940                   // Switch field first: move before
2941                   if(field == switchField)
2942                      draggingField.Move(switchField.prev);
2943                   // Dragged field first: move after
2944                   else
2945                      draggingField.Move(switchField);
2946
2947                   NotifyMovedField(master, this, draggingField, mods);
2948                }
2949                draggingField.headButton.stayDown = false;
2950             }
2951             if(movingFields)
2952                result = false;
2953             dropField = null;
2954             movingFields = false;
2955          }
2956          draggingField = null;
2957          return result;
2958       }
2959       return true;
2960    }
2961
2962    bool HeaderClicked(Button control, int x, int y, Modifiers mods)
2963    {
2964       if(style.header && !dropField && style.sortable)
2965       {
2966          DataField field = (DataField)(intptr)control.id;
2967          if(sortField == field)
2968             field.sortOrder *= -1;
2969          else
2970             sortField = field;
2971          if(field)
2972          {
2973             Sort(sortField, field.sortOrder);
2974             NotifySort(master, this, field, mods);
2975          }
2976       }
2977       return true;
2978    }
2979
2980    bool HeaderDoubleClicked(Button control, int x, int y, Modifiers mods)
2981    {
2982       if(style.resizable)
2983       {
2984          DataField field = (DataField)(intptr)control.id;
2985          if(field)
2986          {
2987             if(x < RESIZE_BORDER && field.prev)
2988                field = field.prev;
2989             else if(x >= control.clientSize.w - RESIZE_BORDER);
2990             else
2991                field = null;
2992          }
2993          else
2994          {
2995             if(x < RESIZE_BORDER && fields.last)
2996                field = fields.last;
2997             else
2998                field = null;
2999          }
3000          if(field)
3001             field.AutoSize();
3002       }
3003       return false;
3004    }
3005
3006    watch(visible)
3007    {
3008       if(style.freeSelect)
3009       {
3010          if(!visible)
3011          {
3012             ReleaseCapture();
3013             rolledOver = dragging = false;
3014          }
3015          /*else
3016             Capture();*/
3017       }
3018    };
3019
3020    bool OnLoadGraphics()
3021    {
3022       display.FontExtent(fontObject, "W", 1, null, &fontH);
3023       if(!style.heightSet)
3024       {
3025          rowHeight = Max(fontH + 2, 16) + (style.alwaysEdit ? 1 : 0);
3026          SetScrollLineStep(8, rowHeight);
3027       }
3028       return true;
3029    }
3030
3031    void OnApplyGraphics()
3032    {
3033       SetScrollLineStep(8, rowHeight);
3034       if(style.header)
3035       {
3036          DataField field;
3037          for(field = fields.first; field; field = field.next)
3038          {
3039             if(field.headButton)
3040             {
3041                field.headButton.bevel = (!guiApp.textMode && !style.clearHeader);
3042                if(guiApp.textMode)
3043                   field.headButton.background = Color { 0, 170, 0 };
3044             }
3045          }
3046       }
3047       OnResize(clientSize.w, clientSize.h);
3048    }
3049
3050    bool OnResizing(int *w, int *h)
3051    {
3052       if(rows.first)
3053       {
3054          if(!initSize.w && (!anchor.left.type || !anchor.right.type) && !*w)
3055          {
3056             // Use widest item
3057             DataRow row;
3058             int maxWidth = 0;
3059             Font font = fontObject;
3060             Font boldFont = this.boldFont.font;
3061             Display display = this.display;
3062
3063             for(row = rows.first; row; row = row.GetNextRow())
3064             {
3065                Bitmap icon = row.icon ? row.icon.bitmap : null;
3066                int x = -scroll.x + EXTRA_SPACE / 2-1;
3067                DataField field;
3068                int indent = 0;
3069                DataRow parent;
3070                for(parent = row.parent; parent; parent = parent.parent)
3071                {
3072                   if(!parent.header)
3073                   {
3074                      if(style.treeBranch)
3075                         indent += 20;
3076                      else
3077                         indent += 15;
3078                   }
3079                }
3080                if(style.rootCollapse) indent += 20;
3081                x += indent;
3082                if(style.collapse && !(style.treeBranch)) x += 15;
3083                if(icon)
3084                   x += 20;
3085
3086                // Compute the rows size
3087                for(field = fields.first; field; field = field.next)
3088                {
3089                   if(((style.resizable && (!(style.alwaysEdit) || field.next)) || field.width) && !row.header)
3090                      x += field.width - (field.prev ? 0 : indent);
3091                   else
3092                   {
3093                      ListBoxCell cell;
3094                      uint index;
3095                      for(index = 0, cell = row.cells.first; index != field.index; index++, cell = cell.next);
3096
3097                      // Should always be as many cells in the row as fields in the listbox
3098                      if(cell && cell.isSet && field.dataType)
3099                      {
3100                         static char tempString[4096];
3101                         const char * string;
3102                         int tw, th;
3103                         if(field.dataType.type == normalClass || field.dataType.type == noHeadClass)
3104                            string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, cell.data[0], tempString, field.userData, null);
3105                         else
3106                            string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, cell.data, tempString, field.userData, null);
3107                         /* GCC-4.4 Bug!
3108                         if(!string) string = "";
3109                         display.FontExtent(row.header ? boldFont : font, string, strlen(string), &tw, &th);
3110                         */
3111                         if(string)
3112                            display.FontExtent(row.header ? boldFont : font, string, strlen(string), &tw, &th);
3113                         else
3114                            display.FontExtent(row.header ? boldFont : font, "", 0, &tw, &th);
3115                         x += tw;
3116                      }
3117                      if(row.header) break;
3118                   }
3119                   x += EXTRA_SPACE;
3120                }
3121                maxWidth = Max(maxWidth, x);
3122             }
3123             *w = maxWidth;
3124          }
3125          if(!*h)
3126          {
3127             *h = Min(maxShown, rowCount) * rowHeight;
3128          }
3129       }
3130       else
3131       {
3132          if(!*w) *w = rowHeight * 5;
3133          if(!*h) *h = rowHeight * 5;
3134       }
3135       return true;
3136    }
3137
3138    watch(font)
3139    {
3140       FontResource font = this.font;
3141       FontResource boldFont
3142       {
3143          faceName = font.faceName, size = font.size, bold = true
3144       };
3145       AddResource(boldFont);
3146       RemoveResource(this.boldFont);
3147       this.boldFont = boldFont;
3148
3149       OnLoadGraphics();
3150
3151       SetInitSize(initSize);
3152    };
3153
3154    bool OnMouseMove(int x, int y, Modifiers mods)
3155    {
3156       bool isTimer = false;
3157       int realX = x, realY = y;
3158
3159       if(insideNotifySelect) return true;
3160
3161       if(style.alwaysEdit && style.resizable &&
3162          resizingField && !(mods.isSideEffect))
3163       {
3164         // Resize left
3165          DataField field = resizingField;
3166          field.width = startWidth + x - resizeX;
3167          field.width = Max(field.width, - EXTRA_SPACE);
3168
3169          AdaptToFieldWidth(field, true);
3170       }
3171
3172       cursor = null;
3173       if(style.alwaysEdit && style.resizable)
3174       {
3175          int vx = -scroll.x - 1;
3176          DataField field;
3177
3178          if(style.collapse && !(style.treeBranch))
3179             vx += 15;
3180
3181          for(field = fields.first; field; field = field.next)
3182          {
3183             int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
3184                clientSize.w - field.x : (field.width + EXTRA_SPACE);
3185
3186             if(field.prev)
3187             {
3188                if(Abs(x - vx) < 2)
3189                {
3190                   cursor = guiApp.GetCursor(sizeWE);
3191                   break;
3192                }
3193             }
3194             vx += width + EXTRA_SPACE;
3195          }
3196       }
3197
3198       if((editData && editData.visible) || (style.alwaysEdit))
3199          return true;
3200
3201       if(x == MAXINT && y == MAXINT)
3202       {
3203          x = mouseX;
3204          y = mouseY;
3205          isTimer = true;
3206       }
3207
3208       // ADDED THIS CHECK FOR FieldDropBox LEAKS
3209       if(/*!mods.isSideEffect && */(rolledOver || !dragging))
3210       {
3211          int rowY = (style.header) ? rowHeight : 0;
3212          DataRow row = null, nextRow;
3213          int rowIndex;
3214
3215          mouseX = x;
3216          mouseY = y;
3217
3218          if(dragging &&
3219             ((vertScroll && vertScroll.visible &&
3220              (y < 0 || y >= clientSize.h)) ||
3221              (horzScroll && horzScroll.visible &&
3222              (x < 0 || x >= clientSize.w))))
3223          {
3224             timer.Start();
3225             if(isTimer)
3226             {
3227                if(vertScroll && vertScroll.visible &&
3228                   (y < 0 || y >= clientSize.h))
3229                   vertScroll.Action((y<0)?up:down, 0, 0);
3230                if(horzScroll && horzScroll.visible &&
3231                   (x < 0 || x >= clientSize.w))
3232                   horzScroll.Action((x<0)?up:down, 0, 0);
3233             }
3234          }
3235          else
3236             timer.Stop();
3237
3238          // This must be done after the scrolling took place
3239          rowIndex = firstRowShown ? firstRowShown.index : -1;
3240          y = Max(y, 0);
3241          y = Min(y, clientSize.h - ((dragRow && style.moveRows) ? rowHeight : 0)-1);
3242          for(row = firstRowShown; row; row = nextRow, rowIndex ++)
3243          {
3244             nextRow = row.GetNextRow();
3245             rowY += rowHeight;
3246             if(rowY > y || !nextRow)
3247             {
3248                break;
3249             }
3250          }
3251
3252          if(dragRow && style.moveRows)
3253          {
3254             if(row && row == currentRow)
3255                row = row.GetNextRow();
3256
3257             if(row && row.parent == currentRow)
3258                row = row.GetNextRow();
3259          }
3260
3261          if(row && currentRow != row)
3262          {
3263             if(dragRow && style.moveRows)
3264             {
3265                if(row.selectedFlag == selected || row.selectedFlag == tempSelected)
3266                   rowIndex = -1;
3267                else if(style.multiSelect)
3268                {
3269                   DataRow thisRow;
3270                   for(thisRow = rows.first; thisRow; thisRow = thisRow.GetNextRow())
3271                      if((thisRow.selectedFlag == selected || thisRow.selectedFlag == tempSelected) ||
3272                         thisRow == row)
3273                         break;
3274                   if(thisRow != row)
3275                      rowIndex++;
3276                }
3277                if(dropIndex != rowIndex)
3278                {
3279                   dropIndex = rowIndex;
3280                   editRow = null;
3281                   Update(null);
3282                   movedRow = true;
3283                }
3284             }
3285             else if((style.freeSelect  || dragging) && ((realX>= 0 && realY >= 0 && realX< clientSize.w && realY < clientSize.h) || rolledOver))
3286             {
3287                if(!(style.multiSelect))
3288                {
3289                   if(currentRow)currentRow.selectedFlag = unselected;
3290                   if(row)row.selectedFlag = selected;
3291                }
3292                currentRow = row;
3293
3294                if(style.multiSelect)
3295                {
3296                   DataRow selRow;
3297
3298                   for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
3299                   {
3300                      if(selRow.selectedFlag == tempUnselected) selRow.selectedFlag = selected;
3301                      else if(selRow.selectedFlag == tempSelected) selRow.selectedFlag = unselected;
3302                   }
3303
3304                   if(rowIndex >= clickedRow.index)
3305                   {
3306                      for(selRow = clickedRow; selRow; selRow = selRow.GetNextRow())
3307                      {
3308                         selRow.selectedFlag = (selRow.selectedFlag == selected) ? tempUnselected : tempSelected;
3309                         if(selRow == row)
3310                            break;
3311                      }
3312                   }
3313                   else
3314                   {
3315                      for(selRow = row; selRow; selRow = selRow.GetNextRow())
3316                      {
3317                         selRow.selectedFlag = (selRow.selectedFlag == selected) ? tempUnselected : tempSelected;
3318                         if(selRow == clickedRow)
3319                            break;
3320                      }
3321                   }
3322                }
3323                Update(null);
3324                if(style.freeSelect)
3325                   NotifyHighlight(master, this, currentRow, mods);
3326                else
3327                {
3328                   insideNotifySelect = true;
3329                   NotifySelect(master, this, currentRow, mods);
3330                   insideNotifySelect = false;
3331                }
3332
3333                if(style.alwaysEdit && currentRow)
3334                   currentRow.Edit(currentField);
3335             }
3336          }
3337       }
3338       return true;
3339    }
3340
3341    bool OnMouseOver(int x, int y, Modifiers mods)
3342    {
3343       if(dragging)
3344          rolledOver = true;
3345       return true;
3346    }
3347
3348    bool OnActivate(bool active, Window swap, bool * goOnWithActivation, bool direct)
3349    {
3350       // TOCHECK: WAS THIS MISSING ? CHECK FOR SLOWDOWN
3351       if(!active) Update(null);
3352
3353       if(!active && (!swap || !swap.isModal))
3354       {
3355          // true: destroy edit box
3356          HideEditBox(true, true, false);
3357       }
3358       else if(!swap || !swap.isModal)
3359       {
3360          // Bring back edit box
3361          if(currentRow && style.alwaysEdit)
3362          {
3363             currentRow.Edit(currentField ? currentField : null);
3364          }
3365          Update(null);
3366       }
3367       return true; //NotifyActivate(master, this, active, swap, 0);
3368    }
3369
3370
3371    bool OnButtonDown(int x, int y, Modifiers mods, bool right)
3372    {
3373       bool result = true;
3374       // Check to see if we're dragging the vertical divider
3375       if(style.alwaysEdit && style.resizable && !right)
3376       {
3377          int vx = -scroll.x - 1;// + EXTRA_SPACE;// / 2 - 1;
3378          DataField field;
3379
3380          if(style.collapse && !(style.treeBranch))
3381             vx += 15;
3382
3383          for(field = fields.first; field; field = field.next)
3384          {
3385             int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
3386                clientSize.w - field.x : (field.width + EXTRA_SPACE);
3387
3388             if(field.prev)
3389             {
3390                if(Abs(x - vx) < 2)
3391                {
3392                   resizingField = field.prev;
3393                   resizeX = x;
3394                   startWidth = resizingField.width;
3395                   Capture();
3396                   SetMouseRangeToClient();
3397                   break;
3398                }
3399             }
3400             vx += width + EXTRA_SPACE;
3401          }
3402       }
3403
3404       if(!(style.freeSelect))
3405       {
3406          int rowY = (style.header) ? rowHeight : 0;
3407          DataRow row = null;
3408          int rowIndex = firstRowShown ? firstRowShown.index : -1;
3409          DataRow previousRow = currentRow;
3410          DataRow newCurrentRow = null;
3411          bool moveMultiple = false;
3412          int numSelected = 0;
3413          int rowStart = -scroll.x;
3414
3415          if(style.multiSelect)
3416          {
3417             if(!right)
3418             {
3419                DataRow row;
3420                if(!(mods.shift))
3421                {
3422                   for(row = rows.first; row; row = row.GetNextRow())
3423                   {
3424                      if(row.selectedFlag == tempSelected)
3425                         row.selectedFlag = selected;
3426                      else if(row.selectedFlag == tempUnselected)
3427                         row.selectedFlag = unselected;
3428                      if(row.selectedFlag == selected)
3429                         numSelected++;
3430                   }
3431                }
3432             }
3433          }
3434
3435          for(row = firstRowShown; row; row = row.GetNextRow(), rowIndex ++)
3436          {
3437             rowY += rowHeight;
3438             if(rowY > y || (style.multiSelect && !row.GetNextRow()))
3439             {
3440                int plusIndent = 0;
3441                if(style.treeBranch)
3442                {
3443                   DataRow parent;
3444                   for(parent = (style.rootCollapse) ? row.parent : (row.parent ? row.parent.parent : null); parent; parent = parent.parent)
3445                      if(!parent.header)
3446                         plusIndent += 20;
3447                   plusIndent += 4;
3448                }
3449
3450                /*    THIS WAS TOO STRICT:
3451                if(style.collapse && row.subRows.first && (row.parent || !(style.treeBranch) || (style.rootCollapse)) &&
3452                   (x >= rowStart + 3 + plusIndent && y >= rowY - rowHeight + PLUSY && x <= rowStart + 11 + plusIndent && y <= rowY - rowHeight + PLUSY + 8))
3453                */
3454                if(style.collapse &&
3455                   (x >= rowStart && y >= rowY - rowHeight && x <= rowStart + 18 + plusIndent && y <= rowY + rowHeight-1))
3456                {
3457                   if(row.subRows.first && (row.parent || !(style.treeBranch) || (style.rootCollapse)) && x >= plusIndent)
3458                      row.collapsed = !row.collapsed;
3459                   return false;
3460                }
3461                else
3462                {
3463                   if(rowY > y)
3464                   {
3465                      newCurrentRow = row;
3466                   }
3467                   if(style.multiSelect)
3468                   {
3469                      // Deselect everything if user didn't clicked on a row
3470                      if(y >= rowY)
3471                      {
3472                         DataRow selRow;
3473                         for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
3474                            selRow.selectedFlag = unselected;
3475                         clickedRow = row;
3476                         //clickedRowIndex = rowIndex;
3477                      }
3478                      else if(style.moveRows && !(mods.shift) &&
3479                         (row.selectedFlag == selected || row.selectedFlag == tempSelected) &&
3480                         !right && !(mods.isActivate))
3481                         moveMultiple = true;
3482                      else
3483                      {
3484                         DataRow selRow;
3485                         if(right)
3486                         {
3487                            if(row.selectedFlag == tempUnselected || row.selectedFlag == unselected)
3488                            {
3489                               for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
3490                                  selRow.selectedFlag = unselected;
3491                               row.selectedFlag = selected;
3492                            }
3493                            clickedRow = row;
3494                            //clickedRowIndex = rowIndex;
3495                         }
3496                         else
3497                         {
3498                            if(!(mods.ctrl))
3499                            {
3500                               for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
3501                                  selRow.selectedFlag = unselected;
3502                            }
3503                            else
3504                            {
3505                               for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
3506                               {
3507                                  if(selRow != clickedRow)
3508                                  {
3509                                     if(selRow.selectedFlag == tempUnselected) selRow.selectedFlag = selected;
3510                                     else if(selRow.selectedFlag == tempSelected) selRow.selectedFlag = unselected;
3511                                  }
3512                               }
3513                            }
3514
3515                            if(mods.shift)
3516                            {
3517                               if(rowIndex >= clickedRow.index)
3518                               {
3519                                  for(selRow = clickedRow; selRow; selRow = selRow.GetNextRow())
3520                                  {
3521                                     if(mods.ctrl)
3522                                     {
3523                                        if(selRow != clickedRow)
3524                                        {
3525                                           if(selRow.selectedFlag)
3526                                              selRow.selectedFlag = tempUnselected;
3527                                           else
3528                                              selRow.selectedFlag = tempSelected;
3529                                        }
3530                                     }
3531                                     else
3532                                        selRow.selectedFlag = selected;
3533                                     if(selRow == row)
3534                                        break;
3535                                  }
3536                               }
3537                               else
3538                               {
3539                                  for(selRow = row; selRow; selRow = selRow.GetNextRow())
3540                                  {
3541                                     if(mods.ctrl)
3542                                     {
3543                                        if(selRow != clickedRow)
3544                                        {
3545                                           if(selRow.selectedFlag)
3546                                              selRow.selectedFlag = tempUnselected;
3547                                           else
3548                                              selRow.selectedFlag = tempSelected;
3549                                        }
3550                                     }
3551                                     else
3552                                        selRow.selectedFlag = selected;
3553                                     if(selRow == clickedRow)
3554                                        break;
3555                                  }
3556                               }
3557                            }
3558                            else
3559                            {
3560                               if(mods.ctrl)
3561                               {
3562                                  if(row.selectedFlag)
3563                                     row.selectedFlag = tempUnselected;
3564                                  else row.selectedFlag = tempSelected;
3565                               }
3566                               else
3567                                  row.selectedFlag = tempSelected;
3568                               clickedRow = row;
3569                               //clickedRowIndex = rowIndex;
3570                            }
3571                         }
3572                      }
3573                   }
3574                }
3575                break;
3576             }
3577          }
3578
3579          // true: destroy edit box
3580          if(newCurrentRow)
3581          {
3582             incref newCurrentRow;
3583          }
3584
3585          if(currentRow != newCurrentRow)
3586             HideEditBox(true, true, false);
3587
3588          if(newCurrentRow)
3589          {
3590             if(newCurrentRow._refCount <= 1)
3591                delete newCurrentRow;
3592             else
3593                newCurrentRow._refCount--;
3594          }
3595
3596          if(newCurrentRow)
3597          {
3598             if(!(style.multiSelect))
3599             {
3600                if(currentRow) currentRow.selectedFlag = unselected;
3601                if(newCurrentRow) newCurrentRow.selectedFlag = selected;
3602             }
3603          }
3604
3605          if(currentRow != newCurrentRow)
3606          {
3607             /*
3608             // true: destroy edit box
3609             if(newCurrentRow)
3610             {
3611                //incref newCurrentRow;
3612                incref newCurrentRow;
3613             }
3614
3615             HideEditBox(true, true, false);
3616             */
3617
3618             if(newCurrentRow)
3619             {
3620                int headerSize = ((style.header) ? rowHeight : 0);
3621                int height = clientSize.h + 1 - headerSize;
3622
3623                /*if(newCurrentRow._refCount <= 1)
3624                   delete newCurrentRow;
3625                else
3626                {
3627                   newCurrentRow._refCount--;
3628                   //newCurrentRow._refCount--;
3629                }
3630                */
3631                currentRow = newCurrentRow;
3632
3633                if(currentRow && currentRow.index * rowHeight > scroll.y + height - rowHeight)
3634                   SetScrollPosition(scroll.x,
3635                      currentRow.index * rowHeight - height + rowHeight);
3636                else if(!currentRow || currentRow.index * rowHeight < scroll.y)
3637                {
3638                   int line = currentRow ? currentRow.index * rowHeight : 0;
3639                   //SNAPUP(line, rowHeight);
3640                   SetScrollPosition(scroll.x, line);
3641                }
3642
3643                // GO THROUGH SetCurrentRow eventually?
3644                // SetCurrentRow(newCurrentRow, true);
3645             }
3646          }
3647
3648          if(style.freeSelect)
3649             NotifyHighlight(master, this, currentRow, mods);
3650          else if((moveMultiple || (!(style.multiSelect) && previousRow == currentRow)) &&
3651             newCurrentRow && !(mods.shift))
3652          {
3653             if(!right)
3654             {
3655                if(!(mods.isActivate))
3656                {
3657                   if(style.moveRows)
3658                   {
3659                      dragRow = currentRow;
3660                      dropIndex = -1;
3661                      movedRow = false;
3662
3663                      Capture();
3664                   }
3665                   if(editData && editData.visible && style.alwaysEdit)
3666                   {
3667                      DataField field;
3668                      int sx = -scroll.x;
3669                      int indent = 0;
3670                      DataRow parent;
3671
3672                      if(style.collapse && !style.treeBranch)
3673                         sx += 15;
3674
3675                      for(parent = newCurrentRow.parent; parent; parent = parent.parent)
3676                         if(!parent.header)
3677                            indent += (style.treeBranch) ? 20 : 15;
3678                      sx += indent;
3679
3680                      for(field = fields.first; field; field = field.next)
3681                      {
3682                         int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
3683                            clientSize.w - field.x : (field.width + EXTRA_SPACE);
3684
3685                         if(!field.prev) width -= indent;
3686                         if(x >= sx && x < sx + width)
3687                            break;
3688                         sx += width;
3689                      }
3690                      if(field == currentField)
3691                         editData.Deactivate();
3692                      else
3693                      {
3694                         currentRow.Edit(field);
3695                         editData.Activate();
3696                      }
3697                   }
3698                   else if(!(mods.ctrl) && numSelected <= 1)
3699                      editRow = currentRow;
3700
3701                   if(style.noDragging && newCurrentRow)
3702                     NotifyReclick(master, this, newCurrentRow, mods);
3703                }
3704                else
3705                {
3706                   // If the user clicked exactly on the edited field,
3707                   // activate it
3708                   if(editData && editData.visible && newCurrentRow)
3709                   {
3710                      DataField field;
3711                      int sx = -scroll.x;
3712                      int indent = 0;
3713
3714                      if(style.collapse && !(style.treeBranch))
3715                         sx += 15;
3716
3717                      {
3718                         DataRow parent;
3719                         for(parent = newCurrentRow.parent; parent; parent = parent.parent)
3720                            if(!parent.header)
3721                               indent += (style.treeBranch) ? 20 : 15;
3722                         sx += indent;
3723                      }
3724
3725                      for(field = fields.first; field; field = field.next)
3726                      {
3727                         int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
3728                            clientSize.w - field.x : (field.width + EXTRA_SPACE);
3729                         if(!field.prev) width -= indent;
3730                         if(x >= sx && x < sx + width && newCurrentRow)
3731                            break;
3732                         sx += width;
3733                      }
3734
3735                      if(field) //x >= sx && x < sx + width && newCurrentRow)
3736                      {
3737                         if(field == currentField)
3738                            editData.Activate();
3739                         /*else
3740                            newCurrentRow.Edit(currentField);*/
3741                      }
3742                      else if(style.noDragging && newCurrentRow)
3743                        NotifyReclick(master, this, newCurrentRow, mods);
3744                   }
3745                   else if(style.noDragging && newCurrentRow)
3746                     NotifyReclick(master, this, newCurrentRow, mods);
3747                }
3748             }
3749          }
3750          else
3751          {
3752             DataField f = null;
3753             if(result && style.alwaysEdit && currentRow)
3754             {
3755                if(newCurrentRow)
3756                {
3757                   DataField field = null;
3758                   int sx = -scroll.x;
3759                   int indent = 0;
3760                   DataRow parent;
3761
3762                   if(style.collapse && !style.treeBranch)
3763                      sx += 15;
3764
3765                   for(parent = newCurrentRow.parent; parent; parent = parent.parent)
3766                      if(!parent.header)
3767                         indent += (style.treeBranch) ? 20 : 15;
3768                   sx += indent;
3769
3770                   for(field = fields.first; field; field = field.next)
3771                   {
3772                      int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
3773                         clientSize.w - field.x : (field.width + EXTRA_SPACE);
3774
3775                      if(!field.prev) width -= indent;
3776                      if(x >= sx && x < sx + width)
3777                      {
3778                         f = currentField = field;
3779                         break;
3780                      }
3781                      sx += width;
3782                   }
3783                }
3784             }
3785             // Moved NotifySelect after setting currentField for the NotifySelect implementation to be aware of which field is now selected (e.g. WatchesView)
3786             result = NotifySelect(master, this, currentRow, mods);
3787             if(result && style.alwaysEdit && currentRow)
3788             {
3789                // In case the user specifically clicked on a field (f is set), override any change to currentField that NotifySelect could have done
3790                currentRow.Edit(f ? f : currentField);
3791
3792                // If the user clicked exactly on the edited field,
3793                // activate it
3794                if(editData && editData.visible && newCurrentRow)
3795                {
3796                   if(currentField)
3797                   {
3798                      editData.Activate();
3799                   }
3800                   else if(style.noDragging && newCurrentRow)
3801                      NotifyReclick(master, this, newCurrentRow, mods);
3802                }
3803             }
3804             else if(style.noDragging && newCurrentRow)
3805               NotifyReclick(master, this, newCurrentRow, mods);
3806          }
3807       }
3808       /*
3809          For drop box to capture...
3810       else
3811       {
3812          if(x < 0 || y < 0 || x >= clientSize.w || y >= clientSize.h)
3813          {
3814             bool goOn = true;
3815             master.parent.Activate();
3816             Update(null);
3817             ReleaseCapture();
3818             return true;
3819          }
3820       }
3821       */
3822       if(result)
3823       {
3824          if(!style.noDragging)
3825          {
3826             dragging = true;
3827             Capture();
3828          }
3829          if(x >= 0 && y >= 0 && x < clientSize.w && y < clientSize.h)
3830             rolledOver = true;
3831          Update(null);
3832       }
3833       else
3834       {
3835          dragging = false;
3836          OnLeftButtonUp(x, y, mods);
3837       }
3838       return result;
3839    }
3840
3841    bool OnLeftButtonDown(int x, int y, Modifiers mods)
3842    {
3843       return OnButtonDown(x,y, mods, false);
3844    }
3845
3846    bool OnLeftButtonUp(int x, int y, Modifiers mods)
3847    {
3848       if(resizingField && style.alwaysEdit)
3849       {
3850          Window::FreeMouseRange();
3851          ReleaseCapture();
3852          resizingField = null;
3853       }
3854
3855       if(dragRow || editRow)
3856       {
3857          DataRow row, switchRow = rows.last;
3858          int rowY = (style.header) ? rowHeight : 0;
3859          while(switchRow.lastRow) switchRow = switchRow.lastRow;
3860          for(row = firstRowShown; row; row = row.GetNextRow())
3861          {
3862             rowY += rowHeight;
3863             if(rowY > y)
3864             {
3865                switchRow = row;
3866                break;
3867             }
3868          }
3869          if(editRow == switchRow && y >= 0)
3870          {
3871             // Find which field
3872             DataField field;
3873             int fieldX = 0;
3874             for(field = fields.first; field; field = field.next)
3875             {
3876                int width = (!field.next && style.fillLastField && (!hasHorzScroll || clientSize.w - field.x > field.width + EXTRA_SPACE)) ?
3877                   clientSize.w - field.x : (field.width + EXTRA_SPACE);
3878                fieldX += width;
3879
3880                if(fieldX > x + scroll.x)
3881                   break;
3882             }
3883
3884             if(field && field.editable)
3885             {
3886                // true: destroy edit box
3887                HideEditBox(true, true, false);
3888                PopupEditBox(field, false);
3889             }
3890             else if(!style.noDragging)
3891                NotifyReclick(master, this, currentRow, mods);
3892          }
3893          else if(style.moveRows && switchRow)
3894          {
3895             if(dragRow == switchRow && movedRow == false)
3896             {
3897                DataRow row = dragRow;
3898                DataRow selRow;
3899                if(!(mods.ctrl))
3900                {
3901                   for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
3902                      selRow.selectedFlag = unselected;
3903                }
3904                else
3905                {
3906                   for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
3907                   {
3908                      if(selRow != clickedRow)
3909                      {
3910                         if(selRow.selectedFlag == tempUnselected) selRow.selectedFlag = selected;
3911                         else if(selRow.selectedFlag == tempSelected) selRow.selectedFlag = unselected;
3912                      }
3913                   }
3914                }
3915                if(mods.ctrl)
3916                {
3917                   if(row.selectedFlag)
3918                      row.selectedFlag = tempUnselected;
3919                   else row.selectedFlag = tempSelected;
3920                }
3921                else
3922                   row.selectedFlag = tempSelected;
3923                clickedRow = row;
3924             }
3925             else
3926             {
3927                if(style.multiSelect)
3928                {
3929                   if(!switchRow.selectedFlag)
3930                   {
3931                      bool foundSwitch = false;
3932                      bool after = false;
3933                      DataRow next;
3934                      DataRow afterRow = switchRow.prev;
3935                      for(row = rows.first; row; row = next)
3936                      {
3937                         next = row.GetNextRow();
3938                         if(row.selectedFlag == selected || row.selectedFlag == tempSelected)
3939                         {
3940                            if(!foundSwitch && !after)
3941                            {
3942                               after = true;
3943                               afterRow = switchRow;
3944                            }
3945                            if(!after || !(row.selectedFlag == selected || row.selectedFlag == tempSelected) ||
3946                               !foundSwitch)
3947                            {
3948                               row.Move(afterRow);
3949                               afterRow = row;
3950                            }
3951                         }
3952                         else if(row == switchRow)
3953                            foundSwitch = true;
3954                      }
3955                   }
3956                }
3957                else
3958                {
3959                   for(row = rows.first; row; row = row.GetNextRow())
3960                   {
3961                      if(row == switchRow || row == dragRow)
3962                         break;
3963                   }
3964
3965                   // Switch row first: move before
3966                   if(row == switchRow)
3967                   {
3968                      DataRow actualMoveRow;
3969
3970                      if(!switchRow.prev && switchRow.parent == dragRow.parent)
3971                         actualMoveRow = null;
3972                      else
3973                      {
3974                         actualMoveRow = switchRow.prev ? switchRow.prev : switchRow;
3975                         while(actualMoveRow && actualMoveRow.parent != dragRow.parent && actualMoveRow.parent)
3976                            actualMoveRow = actualMoveRow.parent;
3977                      }
3978
3979                      if(!actualMoveRow || (actualMoveRow && actualMoveRow.parent == dragRow.parent))
3980                         if(NotifyMove(master, this, actualMoveRow, mods))
3981                         {
3982                            dragRow.Move(actualMoveRow);
3983                            NotifyMoved(master, this, actualMoveRow, mods);
3984                         }
3985                   }
3986                   // Dragged row first: move after
3987                   else
3988                   {
3989                      DataRow actualMoveRow = switchRow;
3990                      DataRow nextRow = switchRow.GetNextRow();
3991
3992                      while(nextRow && nextRow.parent != dragRow.parent)
3993                         nextRow = nextRow.parent ? nextRow.parent.next : null;
3994                      if(nextRow)
3995                         actualMoveRow = nextRow.prev;
3996
3997                      if(!nextRow)
3998                         actualMoveRow = dragRow.parent ? dragRow.parent.subRows.last : rows.last;
3999
4000                      if(!actualMoveRow || (actualMoveRow != dragRow && actualMoveRow.parent == dragRow.parent))
4001                         if(NotifyMove(master, this, actualMoveRow, mods))
4002                         {
4003                            dragRow.Move(actualMoveRow);
4004                            NotifyMoved(master, this, actualMoveRow, mods);
4005                         }
4006                   }
4007                }
4008             }
4009          }
4010          if(dragRow)
4011             ReleaseCapture();
4012          dragRow = null;
4013          editRow = null;
4014          movedRow = false;
4015          dropIndex = -1;
4016          Update(null);
4017       }
4018
4019       timer.Stop();
4020       if(dragging || style.freeSelect)
4021       {
4022          if(dragging)
4023          {
4024             rolledOver = dragging = false;
4025          }
4026          if(x >= 0 && y >= 0 && x < clientSize.w && y < clientSize.h && currentRow && style.freeSelect)
4027          {
4028             bool result;
4029             ReleaseCapture();
4030             result = NotifySelect(master, this, currentRow, mods);
4031             if(style.alwaysEdit)
4032                currentRow.Edit(currentField);
4033             return result;
4034
4035          }
4036          // if(!(style.freeSelect))
4037          ReleaseCapture();
4038       }
4039       return true;
4040    }
4041
4042    bool OnLeftDoubleClick(int x, int y, Modifiers mods)
4043    {
4044       int rowStart = -scroll.x;
4045       DataRow row;
4046       int rowY = (style.header) ? rowHeight : 0;
4047       int plusIndent = 0;
4048
4049       OnLeftButtonUp(x,y,mods);
4050       if(style.alwaysEdit)
4051       {
4052          if(!(style.collapse) || x > 15)
4053             if(editData && editData.visible)
4054             {
4055                editData.Activate();
4056                NotifyDoubleClick(master, this, x, y, mods);
4057                return false;
4058             }
4059       }
4060       for(row = firstRowShown; row; row = row.GetNextRow())
4061       {
4062          rowY += rowHeight;
4063          if(rowY > y || (style.multiSelect && !row.GetNextRow()))
4064          {
4065             if(style.treeBranch)
4066             {
4067                DataRow parent;
4068                for(parent = (style.rootCollapse) ? row.parent : (row.parent ? row.parent.parent : null); parent; parent = parent.parent)
4069                   if(!parent.header)
4070                      plusIndent += 20;
4071                plusIndent += 4;
4072             }
4073             break;
4074          }
4075       }
4076
4077       if((row && style.collapse && (x >= rowStart && x <= rowStart + 18 + plusIndent)) ||
4078          NotifyDoubleClick(master, this, x, y, mods))
4079       {
4080          if(style.collapse)
4081          {
4082             if(row && row.subRows.first)
4083             {
4084                row.collapsed = !row.collapsed;
4085                return false;
4086             }
4087          }
4088          // We need to return true here so that OnLeftButtonDown can popup the DataBox Editors
4089          return true;
4090       }
4091       return false;
4092    }
4093
4094    bool OnRightButtonDown(int x, int y, Modifiers mods)
4095    {
4096       return OnButtonDown(x,y, mods, true);
4097    }
4098
4099    bool OnRightButtonUp(int x, int y, Modifiers mods)
4100    {
4101       OnLeftButtonUp(x,y,mods);
4102       return NotifyRightClick(master, this, x, y, mods);
4103    }
4104
4105    bool GoToLetter(unichar ch, bool keyHit)
4106    {
4107       bool result = false;
4108       DataField field;
4109       bool checkNextField = true;
4110       int len = keyHit ? 0 : strlen(typedString);
4111
4112       typedString = renew typedString char[len + 2];
4113       typedString[len++] = (char)tolower(ch);         // TODO: FIX UNICODE
4114       typedString[len] = '\0';
4115
4116       for(field = fields.first; field; field = field.next)
4117       {
4118          DataRow startRow = currentRow ? currentRow : rows.first;
4119
4120          if(startRow && field.dataType && field.dataType._vTbl[__ecereVMethodID_class_OnGetString] && ch)
4121          {
4122             DataRow row, next;
4123             bool looped = false;
4124             if(len == 1 && currentRow)
4125                startRow = (next = startRow.GetNextRow(), (next ? next : rows.first));
4126
4127             for(row = startRow; row != startRow || !looped; next = row.GetNextRow(), row = next ? next : rows.first)
4128             {
4129                void * data = row.GetData(field);
4130                char tempString[1024] = "";
4131                bool needClass = false;
4132                const char * string = data ? ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass) : null;
4133
4134                if(string && string[0])
4135                   checkNextField = false;
4136                if(string && string[0] && !strnicmp(string, typedString, len))
4137                {
4138                   if(style.multiSelect)
4139                   {
4140                      DataRow selRow;
4141
4142                      clickedRow = row;
4143                      for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
4144                         selRow.selectedFlag = unselected;
4145                      row.selectedFlag = selected;
4146                   }
4147                   SetCurrentRow(row, true);
4148                   result = true;
4149                   break;
4150                }
4151                looped = true;
4152             }
4153             typingTimer.Stop();
4154             if(typingTimeOut && !keyHit)
4155                typingTimer.Start();
4156             if(!result || !typingTimeOut || keyHit)
4157                typedString[len-1] = '\0';
4158          }
4159          if(!checkNextField || result) break;
4160       }
4161       return result;
4162    }
4163
4164    bool OnKeyDown(Key key, unichar ch)
4165    {
4166       DataField field;
4167
4168       if(key == enter || key == keyPadEnter)
4169       {
4170          if(editData && editData.visible && editData.active)
4171          {
4172             HideEditBox(true, false, false);
4173             return false;
4174          }
4175       }
4176       else if(key == escape)
4177       {
4178          if(resizingField || movingFields || (editData && editData.visible) || dragRow)
4179          {
4180             if(editData && editData.visible && style.alwaysEdit && !editData.active)
4181                return true;
4182             // false: dont destroy edit box
4183             HideEditBox(false, false, false);
4184             if(resizingField)
4185             {
4186                resizingField.width = startWidth;
4187                AdaptToFieldWidth(resizingField, true);
4188                resizingField = null;
4189                ReleaseCapture();
4190             }
4191             if(dragRow)
4192                ReleaseCapture();
4193
4194             dragRow = null;
4195             if(dragging)
4196             {
4197                dragging = false;
4198                ReleaseCapture();
4199             }
4200
4201             movingFields = false;
4202             draggingField = null;
4203             Update(null);
4204             return false;
4205          }
4206       }
4207
4208       if(!currentField || !currentField.editable)
4209          for(field = fields.first; field; field = field.next)
4210          {
4211             if(field.editable)
4212             {
4213                currentField = field;
4214                break;
4215             }
4216          }
4217       if((key == f2 || (style.alwaysEdit && (key == ctrlV || key == ctrlC || key == ctrlX || key == shiftInsert || key == ctrlInsert || key == shiftDel))) &&
4218             currentField && currentField.editable)
4219       {
4220          PopupEditBox(currentField, false);
4221          if(editData && editData.visible)
4222          {
4223             if(style.alwaysEdit)
4224             {
4225                editData.Activate();
4226                if(key == ctrlV || key == ctrlC || key == ctrlX || key == shiftInsert || key == ctrlInsert || key == shiftDel)
4227                {
4228                   editData.OnKeyHit(key, ch);
4229                   StopEditing(true);
4230                }
4231                else
4232                   // For Installer to pop up file dialog
4233                   NotifyKeyDown(master, this, currentRow, key, ch);
4234             }
4235             return false;
4236          }
4237       }
4238
4239       if(!NotifyKeyDown(master, this, currentRow, key, ch))
4240          return false;
4241
4242       // Editable fields...
4243       if(currentField)
4244       {
4245          if(style.alwaysEdit && editData && editData.visible)
4246             return editData.OnKeyDown(key, ch);
4247          return true;   // We want to pick up the OnKeyHit to replace contents, but skip GoToLetter
4248       }
4249
4250       if(ch >=32 && ch != 128 && !key.alt && !key.ctrl && GoToLetter(ch, false))
4251       {
4252          /*if(inactive && window.state != Hidden)
4253             NotifyHighlight(master, this, currentRow, 0);
4254          else
4255          {
4256             NotifySelect(master, this, currentRow, 0);
4257          }*/
4258          return false;
4259       }
4260       return true;
4261    }
4262
4263    bool OnKeyHit(Key key, unichar ch)
4264    {
4265       if(!ch && !key.alt && !key.ctrl)
4266       {
4267          key.code = (SmartKey)key.code;
4268       }
4269       if(ch >= 32 && !key.alt && !key.ctrl && ch != 128)
4270       {
4271          DataField field;
4272          if(!currentField || !currentField.editable)
4273             for(field = fields.first; field; field = field.next)
4274             {
4275                if(field.editable)
4276                {
4277                   currentField = field;
4278                   break;
4279                }
4280             }
4281          if(currentField && currentField.editable)
4282          {
4283             if((!editData || !editData.visible) || !editData.active)
4284             {
4285                PopupEditBox(currentField, false);
4286                if(editData && editData.visible)
4287                {
4288                   editData.Activate();
4289                   editData.OnKeyHit(key, ch);
4290                }
4291             }
4292             return false;
4293          }
4294       }
4295
4296       if(editData && editData.visible && ch && !key.alt && !key.ctrl && editData.active && (key.code != tab || (editData._class == class(EditBox) && ((EditBox)editData).tabKey)))
4297          return false;
4298
4299       if(!key.alt && (style.multiSelect || !key.ctrl))
4300       {
4301          switch(key.code)
4302          {
4303             case left:
4304                if(style.alwaysEdit)
4305                {
4306                   if(currentField)
4307                   {
4308                      DataField field;
4309                      for(field = currentField.prev; field; field = field.prev)
4310                      {
4311                         if(field.editable)
4312                         {
4313                            currentField = field;
4314                            HideEditBox(true, true, false);
4315                            PopupEditBox(currentField, false);
4316                            return false;
4317                         }
4318                      }
4319                   }
4320                }
4321                if(style.collapse && currentRow /*&& !currentField*/)  // THIS PREVENTED COLLAPSING THE PROPERTY SHEET
4322                {
4323                   if(currentRow.subRows.first && !currentRow.collapsed)
4324                   {
4325                      currentRow.collapsed = true;
4326                   }
4327                   else if(currentRow.parent)
4328                      SetCurrentRow(currentRow.parent, true);
4329                   return false;
4330                }
4331                break;
4332             case right:
4333                if(style.collapse && currentRow && currentRow.subRows.first)
4334                {
4335                   if(currentRow.collapsed)
4336                      currentRow.collapsed = false;
4337                   else
4338                      SetCurrentRow(currentRow.subRows.first, true);
4339                   return false;
4340                }
4341                else if(style.alwaysEdit)
4342                {
4343                   if(currentField)
4344                   {
4345                      DataField field;
4346                      for(field = currentField.next; field; field = field.next)
4347                      {
4348                         if(field.editable)
4349                         {
4350                            currentField = field;
4351                            HideEditBox(true, true, false);
4352                            PopupEditBox(currentField, false);
4353                            break;
4354                         }
4355                      }
4356                   }
4357                }
4358                break;
4359             case down: case up:
4360             case pageDown: case pageUp:
4361             case end: case home:
4362             {
4363                int headerSize = ((style.header) ? rowHeight : 0);
4364                int height = clientSize.h + 1 - headerSize;
4365                DataRow oldRow;
4366
4367                // true: destroy edit box
4368                // !!! TESTING true HERE !!!
4369                HideEditBox(true, true, false);
4370                // HideEditBox(false, true, false);
4371
4372                oldRow = currentRow;
4373
4374                SNAPDOWN(height, rowHeight);
4375                if((!currentRow || key.code == home) && key.code != end)
4376                {
4377                   currentRow = rows.first;
4378                }
4379                else
4380                {
4381                   DataRow next;
4382                   switch(key.code)
4383                   {
4384                      case down:
4385                         if(!(style.multiSelect) && currentRow && !currentRow.selectedFlag)
4386                            next = currentRow;
4387                         else
4388                            next = currentRow.GetNextRow();
4389                         if(next)
4390                         {
4391                            currentRow = next;
4392                         }
4393                         break;
4394                      case up:
4395                         if(!(style.multiSelect) && currentRow && !currentRow.selectedFlag)
4396                            next = currentRow;
4397                         else
4398                            next = currentRow.GetPrevRow();
4399
4400                         if(next)
4401                         {
4402                            currentRow = next;
4403                         }
4404                         break;
4405                      case end:
4406                         currentRow = lastRow.GetLastRow();
4407                         break;
4408                      case pageUp:
4409                      {
4410                         int c;
4411                         for(c = 0;
4412                         currentRow && (next = currentRow.GetPrevRow()) && c < height / rowHeight;
4413                             c++, currentRow = next);
4414                         break;
4415                      }
4416                      case pageDown:
4417                      {
4418                         int c;
4419                         for(c = 0;
4420                             currentRow && (next = currentRow.GetNextRow()) && c < height / rowHeight;
4421                             c++, currentRow = next);
4422                         break;
4423                      }
4424                   }
4425                }
4426                if(currentRow && currentRow.index * rowHeight > scroll.y + height - rowHeight)
4427                   SetScrollPosition(scroll.x, currentRow.index * rowHeight - height + rowHeight);
4428                else if(!currentRow || currentRow.index * rowHeight < scroll.y)
4429                   SetScrollPosition(scroll.x, currentRow ? currentRow.index * rowHeight : 0);
4430
4431                if(style.multiSelect)
4432                {
4433                   DataRow selRow;
4434
4435                   if(!(key.shift) && (key.ctrl))
4436                   {
4437                      DataRow row;
4438                      for(row = rows.first; row; row = row.GetNextRow())
4439                      {
4440                         if(row.selectedFlag == tempSelected)
4441                            row.selectedFlag = selected;
4442                         else if(row.selectedFlag == tempUnselected)
4443                            row.selectedFlag = unselected;
4444                      }
4445                   }
4446
4447                   if(!(key.ctrl))
4448                   {
4449                      for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
4450                         selRow.selectedFlag = unselected;
4451                   }
4452                   else
4453                   {
4454                      for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
4455                      {
4456                         if(selRow.selectedFlag == tempUnselected) selRow.selectedFlag = selected;
4457                         else if(selRow.selectedFlag == tempSelected) selRow.selectedFlag = unselected;
4458                      }
4459                   }
4460
4461                   if(key.shift)
4462                   {
4463                      if(currentRow.index >= clickedRow.index)
4464                      {
4465                         for(selRow = clickedRow; selRow; selRow = selRow.GetNextRow())
4466                         {
4467                            if(key.ctrl)
4468                            {
4469                               if(selRow.selectedFlag) selRow.selectedFlag = tempUnselected; else selRow.selectedFlag = tempSelected;
4470                            }
4471                            else
4472                               selRow.selectedFlag = selected;
4473                            if(selRow == currentRow)
4474                               break;
4475                         }
4476                      }
4477                      else
4478                      {
4479                         for(selRow = currentRow; selRow; selRow = selRow.GetNextRow())
4480                         {
4481                            if(key.ctrl)
4482                            {
4483                               if(selRow.selectedFlag) selRow.selectedFlag = tempUnselected; else selRow.selectedFlag = tempSelected;
4484                            }
4485                            else
4486                               selRow.selectedFlag = selected;
4487                            if(selRow == clickedRow)
4488                               break;
4489                         }
4490                      }
4491                   }
4492                   else
4493                   {
4494                      if(!(key.ctrl) && currentRow)
4495                      {
4496                         currentRow.selectedFlag = selected;
4497                      }
4498
4499                      clickedRow = currentRow;
4500                   }
4501                }
4502                else
4503                {
4504                   if(oldRow) oldRow.selectedFlag = unselected;
4505                   if(currentRow) currentRow.selectedFlag = selected;
4506                }
4507
4508                if(currentRow)
4509                {
4510                   if(style.freeSelect)
4511                      NotifyHighlight(master, this, currentRow, 0);
4512                   else
4513                      NotifySelect(master, this, currentRow, 0);
4514
4515                   if(style.alwaysEdit && currentRow)
4516                      currentRow.Edit(currentField /*null*/);
4517                }
4518                Update(null);
4519                return false;
4520             }
4521             case space:
4522             {
4523                if(style.multiSelect && currentRow)
4524                {
4525                   if(currentRow.selectedFlag)
4526                   {
4527                      if(key.ctrl)
4528                         currentRow.selectedFlag = unselected;
4529                   }
4530                   else
4531                      currentRow.selectedFlag = selected;
4532                   Update(null);
4533
4534                   if(style.freeSelect)
4535                      NotifyHighlight(master, this, currentRow, 0);
4536                   else
4537                      NotifySelect(master, this, currentRow, 0);
4538                }
4539                break;
4540             }
4541          }
4542       }
4543
4544       if(!NotifyKeyHit(master, this, currentRow, key, ch))
4545          return false;
4546
4547       if(ch >=32 && ch != 128 && !key.alt && !key.ctrl && GoToLetter(ch, true))
4548       {
4549          /*if(inactive && window.state != Hidden)
4550             return NotifyHighlight(master, this, currentRow, 0);
4551          else
4552          {
4553             return NotifySelect(master, this, currentRow, 0);
4554          }*/
4555          return false;
4556       }
4557       return true;
4558    }
4559
4560    void OnHScroll(ScrollBarAction action, int position, Key key)
4561    {
4562       Update(null);
4563    }
4564
4565    void OnVScroll(ScrollBarAction action, int position, Key key)
4566    {
4567       int y = 0;
4568       DataRow next;
4569
4570       for(firstRowShown = rows.first; firstRowShown; firstRowShown = next)
4571       {
4572          next = firstRowShown.GetNextRow();
4573          if(y >= position || !next) break;
4574
4575          y += rowHeight;
4576       }
4577       Update(null);
4578    }
4579
4580    OldList fields;
4581    OldList rows;
4582    int numFields;
4583    DataRow firstRowShown;
4584    DataRow clickedRow;
4585    DataRow currentRow;
4586    int width;
4587    DataField sortField;
4588    int rowCount;
4589    int rowHeight;
4590    int fontH;
4591    double typingTimeOut;
4592    char * typedString;
4593
4594    int mouseX, mouseY;
4595
4596    Timer timer
4597    {
4598       delay = 0.1;
4599       userData = this;
4600
4601       bool DelayExpired()
4602       {
4603          Modifiers mods { };
4604          if(guiApp.GetKeyState(shift)) mods.shift = true;
4605          if(guiApp.GetKeyState(alt)) mods.alt = true;
4606          if(guiApp.GetKeyState(control)) mods.ctrl = true;
4607          OnMouseMove(MAXINT, MAXINT, mods);
4608
4609          return true;
4610       }
4611    };
4612
4613    Timer typingTimer
4614    {
4615       delay = 0.5; // typingTimeOut
4616       userData = this;
4617
4618       bool DelayExpired()
4619       {
4620          typedString[0] = '\0';
4621
4622          // The next line was commented... Why? When commented typing words stops working ( only first character jumps )
4623          typingTimer.Stop();
4624          return true;
4625       }
4626    };
4627
4628    bool dragging, rolledOver;
4629    int numSelections;
4630    Button endBevel;
4631
4632    // For moving rows
4633    DataRow dragRow;
4634    int dropIndex;
4635    bool movedRow;
4636
4637    // For editing fields
4638    DataBox editData;
4639    DataField currentField;
4640    DataRow editRow;
4641
4642    // For moving fields
4643    DataField draggingField, dropField;
4644    bool movingFields;
4645
4646    // For resizing fields
4647    DataField resizingField;
4648    int resizeX, oldX, startWidth;
4649
4650    ListBoxBits style;
4651    FontResource boldFont;
4652    int maxShown;
4653
4654    // Only used for OnMouseMove so far, for avoiding problems with consequential mouse moves
4655    bool insideNotifySelect;
4656    ColorAlpha selectionColor, selectionText, stippleColor;
4657    stippleColor = 0xFFFFFF80;
4658 };