ecere/gui/controls/ListBox: Fixes for moving row in tree views
[sdk] / ecere / src / gui / controls / ListBox.ec
index dab9bdd..7ae0fd0 100644 (file)
@@ -119,7 +119,7 @@ public:
       }
    };
    property int sortOrder { get { return this ? sortOrder : 0; } };
-   property char * header
+   property const char * header
    {
       set
       {
@@ -150,9 +150,6 @@ public:
    {
       if(prev != after)
       {
-         int position = 0;
-         DataField field;
-
          listBox.fields.Move(this, after);
 
          // Fix up positions
@@ -181,12 +178,12 @@ public:
             if(cell && cell.isSet && dataType)
             {
                static char tempString[4096];
-               String string;
+               const String string;
                int tw = 0;
                if(dataType.type == normalClass || dataType.type == noHeadClass)
-                  string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, cell.data[0], tempString, userData, null);
+                  string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, cell.data[0], tempString, userData, null);
                else
-                  string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, cell.data, tempString, userData, null);
+                  string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, cell.data, tempString, userData, null);
                if(string)
                   display.FontExtent(row.header ? boldFont : font, string, strlen(string), &tw, null);
                else
@@ -235,7 +232,7 @@ private:
    }
 
    DataField prev, next;
-   char * header;
+   const char * header;
    Class dataType;
    int width;
    uint index;
@@ -258,22 +255,22 @@ public class DataRow
    bool skipCheck;
 #endif
 public:
-   property int64 tag { set { tag = value; } get { return tag; } };
+   property int64 tag { set { if(this) tag = value; } get { return this ? tag : 0; } };
    property DataRow previous { get { return prev; } };
    property DataRow next { get { return next; } };
    property int index { get { return (this && (!parent || parent.IsExpanded())) ? index : -1; } };
-   property char * string
+   property const char * string
    {
       set { SetData(listBox.fields.first, value); }
       get { return GetData(listBox.fields.first); }
    };
    property bool isHeader { set { header = value; } get { return this ? header : false; } };
-   property BitmapResource icon { set { icon = value; } get { return icon; } };
+   property BitmapResource icon { set { if(this) icon = value; } get { return this ? icon : null; } };
    property bool collapsed
    {
       set
       {
-         if(collapsed != value)
+         if(this && collapsed != value)
          {
             collapsed = value;
             if(parent.IsExpanded())
@@ -321,7 +318,7 @@ public:
             }
          }
 #ifdef _DEBUG
-         if(!skipCheck)
+         if(this && !skipCheck)
             listBox.CheckConsistency();
 #endif
       }
@@ -486,20 +483,19 @@ public:
          if(listBox && prev != after)
          {
             DataRow search;
-            int afterIndex = -1;
             int headerSize = ((listBox.style.header) ? listBox.rowHeight : 0);
             int height = listBox.clientSize.h + 1 - headerSize;
             int ixCount = (!collapsed && subRows.count) ? GetLastRow().index - index + 1 : 1;
 
             if(!after || after.index < index)
             {
-               if(after == listBox.firstRowShown.prev)
+               if((after && after == listBox.firstRowShown.prev) || (!after && !parent && listBox.firstRowShown.prev))
                   listBox.firstRowShown = this;
 
                // All rows between AFTER (exclusive) and ROW (exclusive) are incremented by one
                // ROW is equal to AFTER's index + 1
 
-               for(search = after ? after.next : listBox.rows.first; search && search != this; search = search.GetNextRow())
+               for(search = after ? after.next : (parent ? parent.subRows.first : listBox.rows.first); search && search != this; search = search.GetNextRow())
                   search.index += ixCount;
 
                if(after && after.subRows.first && !after.collapsed)
@@ -508,7 +504,7 @@ public:
                   index = search.index + 1;
                }
                else
-                  index = after ? (after.index + 1) : 0;
+                  index = after ? (after.index + 1) : parent ? parent.index + 1 : 0;
 
                // Fix indices of sub rows
                if(!collapsed)
@@ -520,10 +516,12 @@ public:
             }
             else
             {
-               DataRow nextRow = GetNextRow();
+               DataRow nextRow;
+               int afterIXCount = 1;
+
+               nextRow = GetNextRow();
                if(this == listBox.firstRowShown)
                   listBox.firstRowShown = nextRow;
-               index = after.index;
 
                // All rows between ROW (exclusive) and AFTER (inclusive) are decremented by one
                // ROW is equal to AFTER's index
@@ -533,8 +531,33 @@ public:
                   search.index -= ixCount;
                   if(search == after) break;
                }
+
+               // Fix up's after's sub rows
+               if(after && !after.collapsed)
+               {
+                  DataRow last = after.GetLastRow();
+                  if(last != after)
+                  {
+                     int ix = after.index+1;
+                     for(search = after.GetNextRow(); search; search = search.GetNextRow())
+                     {
+                        afterIXCount++;
+                        search.index = ix++;
+                        if(search == last) break;
+                     }
+                  }
+               }
+               index = after.index + afterIXCount;
+
+               // Fix indices of sub rows
+               if(!collapsed)
+               {
+                  int c, ix = index+1;
+                  for(c = 1, search = GetNextRow(); search && c < ixCount; c++, search = search.GetNextRow())
+                     search.index = ix++;
+               }
             }
-            listBox.rows.Move(this, after);
+            (parent ? parent.subRows : listBox.rows).Move(this, after);
 
 #ifdef _DEBUG
             listBox.CheckConsistency();
@@ -573,7 +596,7 @@ public:
             if((field.dataType.type == normalClass || field.dataType.type == noHeadClass))
                return cell.data[0];
             else
-               return (void *)cell.data;   // Cast for MemoryGuard
+               return cell.data;
          }
       }
       return null;
@@ -611,7 +634,7 @@ public:
             listBox.modifiedDocument = true;
             listBox.Update(null);
             if(dataType && (dataType.type == normalClass || dataType.type == noHeadClass))
-               return (void *)cell.data;     // Cast for MemoryGuard
+               return cell.data;
             else
                return &cell.data;
          }
@@ -702,7 +725,7 @@ public:
                      (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
                   ListBoxCell cell = (ListBoxCell)new0 byte[size];
                   row.cells.Add(cell);
-                  FillBytes(cell.data, 0, size - (uint)&((ListBoxCell)0).data);
+                  FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
                   cell.isSet = false;
                }
             }
@@ -751,7 +774,7 @@ public:
       return null;
    }
 
-   DataRow AddStringf(char * format, ...)
+   DataRow AddStringf(const char * format, ...)
    {
       if(this)
       {
@@ -770,7 +793,7 @@ public:
       return null;
    }
 
-   DataRow AddString(char * string)
+   DataRow AddString(const char * string)
    {
       if(this)
       {
@@ -786,7 +809,7 @@ public:
 private:
    DataRow()
    {
-      subRows.offset = (uint)&((DataRow)0).prev;
+      subRows.offset = (uint)(uintptr)&((DataRow)0).prev;
    }
 
    ~DataRow()
@@ -964,7 +987,7 @@ public class ListBox : CommonControl
 public:
    // Properties
    property bool freeSelect { property_category $"Behavior" set { style.freeSelect = value; } get { return style.freeSelect; } };
-   property DataRow currentRow { property_category $"Private" /*"Behavior"*/ set { SetCurrentRow(value, false); } get { return currentRow; } };
+   property DataRow currentRow { property_category $"Private" /*"Behavior"*/ set { if(this) SetCurrentRow(value, false); } get { return this ? currentRow : null; } };
    property DataField currentField
    {
       get { return currentField; }
@@ -997,7 +1020,7 @@ public:
             OnApplyGraphics();
          }
       }
-      get { return rowHeight; }
+      get { return this ? rowHeight : 0; }
    };
    property Seconds typingTimeout
    {
@@ -1108,6 +1131,7 @@ public:
    virtual bool Window::NotifyKeyHit(ListBox listBox, DataRow row, Key key, unichar ch);
    virtual bool Window::NotifyModified(ListBox listBox, DataRow row);
    virtual bool Window::NotifyEditing(ListBox listBox, DataRow row);
+   virtual void Window::NotifyMoved(ListBox listBox, DataRow row, Modifiers mods);
 
 #ifdef _DEBUG
    private void CheckConsistency()
@@ -1158,7 +1182,7 @@ public:
                stayOnTop = true;
                inactive = true;
                dontScrollVert = true;
-               id = (uint64)addedField;
+               id = (int64)(intptr)addedField;
                text = addedField.header;
                bevel = (!guiApp.textMode && !style.clearHeader);
                ellipsis = true;
@@ -1185,7 +1209,7 @@ public:
                   (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
                ListBoxCell cell = (ListBoxCell)new0 byte[size];
                row.cells.Add(cell);
-               FillBytes(cell.data, 0, size - (uint)&((ListBoxCell)0).data);
+               FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
                cell.isSet = false;
 
                if(row.subRows.first)
@@ -1281,8 +1305,6 @@ public:
       if(row)
       {
          DataRow search;
-         DataField field;
-         int c;
 
          row.index = 0;
          rows.Insert(null, row);
@@ -1325,8 +1347,7 @@ public:
                // Find very last row
                {
                   DataRow lastRow;
-                  for(lastRow = rows.last; lastRow && !lastRow.collapsed && lastRow.subRows.last; lastRow)
-                     lastRow = lastRow.subRows.last;
+                  for(lastRow = rows.last; lastRow && !lastRow.collapsed && lastRow.subRows.last; lastRow = lastRow.subRows.last);
                   row.index = lastRow ? (lastRow.index + 1) : 0;
                }
 
@@ -1346,7 +1367,7 @@ public:
                         (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
                      ListBoxCell cell = (ListBoxCell) new0 byte[size];
                      row.cells.Add(cell);
-                     FillBytes(cell.data, 0, size - (uint)&((ListBoxCell)0).data);
+                     FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
                      cell.isSet = false;
                   }
                }
@@ -1414,7 +1435,7 @@ public:
                      (sizeof(class ListBoxCell) + field.dataType.typeSize - sizeof(void *)) : sizeof(class ListBoxCell);
                   ListBoxCell cell = (ListBoxCell) new0 byte[size];
                   row.cells.Add(cell);
-                  FillBytes(cell.data, 0, size - (uint)&((ListBoxCell)0).data);
+                  FillBytes(cell.data, 0, size - (uint)(uintptr)&((ListBoxCell)0).data);
                   cell.isSet = false;
                }
             }
@@ -1440,7 +1461,7 @@ public:
       return null;
    }
 
-   DataRow AddStringf(char * format, ...)
+   DataRow AddStringf(const char * format, ...)
    {
       if(this)
       {
@@ -1461,7 +1482,7 @@ public:
       return null;
    }
 
-   DataRow AddString(char * string)
+   DataRow AddString(const char * string)
    {
       if(this)
       {
@@ -1560,11 +1581,10 @@ public:
       return null;
    }
 
-   DataRow FindString(char * searchedString)
+   DataRow FindString(const char * searchedString)
    {
       DataField field;
       bool checkNextField = true;
-      int len = searchedString ? strlen(searchedString) : 0;
 
       for(field = fields.first; field; field = field.next)
       {
@@ -1578,7 +1598,7 @@ public:
                   void * data = row.GetData(field);
                   char tempString[1024] = "";
                   bool needClass = false;
-                  char * string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
+                  const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
 
                   if(string && string[0])
                      checkNextField = false;
@@ -1592,7 +1612,7 @@ public:
       return null;
    }
 
-   DataRow FindSubString(char * subString)
+   DataRow FindSubString(const char * subString)
    {
       DataField field;
       bool checkNextField = true;
@@ -1612,7 +1632,7 @@ public:
                      void * data = row.GetData(field);
                      char tempString[1024] = "";
                      bool needClass = false;
-                     char * string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
+                     const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
 
                      if(string && string[0])
                         checkNextField = false;
@@ -1627,13 +1647,13 @@ public:
       return null;
    }
 
-   DataRow FindSubStringi(char * subString)
+   DataRow FindSubStringi(const char * subString)
    {
       DataField field;
       bool checkNextField = true;
       int len = subString ? strlen(subString) : 0;
       DataRow result = null;
-      char * bestResult = null;
+      const char * bestResult = null;
       int bestLen = 0;
 
       if(len)
@@ -1650,7 +1670,7 @@ public:
                      void * data = row.GetData(field);
                      char tempString[1024] = "";
                      bool needClass = false;
-                     char * string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
+                     const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
 
                      if(string && string[0])
                         checkNextField = false;
@@ -1679,7 +1699,7 @@ public:
       return result;
    }
 
-   DataRow FindSubStringAfter(DataRow after, char * subString)
+   DataRow FindSubStringAfter(DataRow after, const char * subString)
    {
       DataField field;
       bool checkNextField = true;
@@ -1699,7 +1719,7 @@ public:
                      void * data = row.GetData(field);
                      char tempString[1024] = "";
                      bool needClass = false;
-                     char * string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
+                     const char * string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass);
 
                      if(string && string[0])
                         checkNextField = false;
@@ -1847,8 +1867,8 @@ private:
    ListBox()
    {
       DataField defaultField { };
-      rows.offset = (uint)&((DataRow)0).prev;
-      fields.offset = (uint)&((DataField)0).prev;
+      rows.offset = (uint)(uintptr)&((DataRow)0).prev;
+      fields.offset = (uint)(uintptr)&((DataField)0).prev;
       style.fullRowSelect = true;
       style.fillLastField = true;
       style.expandOnAdd = true;
@@ -1922,7 +1942,6 @@ private:
    {
       if(editData && editData.visible)
       {
-         Class dataType = currentField.dataType;
          if(save)
             editData.SaveData();
 
@@ -1957,8 +1976,6 @@ private:
             */
             PopupEditBox(currentField, repositionOnly);
          }
-         else
-            printf("");
 
          /*else
             currentField = null;*/
@@ -1967,7 +1984,7 @@ private:
 
    void SetCurrentRow(DataRow row, bool notify)
    {
-      if(currentRow != row || (currentRow && currentRow.selectedFlag == unselected))
+      if(this && (currentRow != row || (currentRow && currentRow.selectedFlag == unselected)))
       {
          int headerSize = ((style.header) ? rowHeight : 0);
          int height = clientSize.h + 1 - headerSize;
@@ -2029,7 +2046,7 @@ private:
          int height = rowHeight - (style.alwaysEdit ? 1 : 0);
          int x = 0;
          int y = currentRow.index * rowHeight + (style.header ? rowHeight : 0);
-         int width;
+         int width = 0;
          DataField field;
 
          if(style.collapse && !(style.treeBranch))
@@ -2065,7 +2082,7 @@ private:
             int height = rowHeight - (style.alwaysEdit ? 1 : 0);
             int x = 0;
             int y = currentRow.index * rowHeight + (style.header ? rowHeight : 0);
-            int width;
+            int width = 0;
             //void * data = currentRow.GetData(whichField);
             DataField field;
             DataRow row = null;
@@ -2093,7 +2110,7 @@ private:
                   background = dataBoxBackground;
                   foreground = dataBoxForeground;
 
-                  bool NotifyChanged(bool closingDropDown)
+                  bool NotifyChanged(DataBox dataBox, bool closingDropDown)
                   {
                      DataRow row = null;
                      DataField field = null;
@@ -2323,7 +2340,7 @@ private:
          int indent = 0;
          DataRow parent;
          Bitmap icon = row.icon ? row.icon.bitmap : null;
-         int collapseRowStart;
+         int collapseRowStart = 0;
          bool lastWasHeader = row.header;
 
          for(parent = row.parent; parent; parent = parent.parent)
@@ -2387,8 +2404,8 @@ private:
             Color colors[] = { formColor, azure, mistyRose, linen, floralWhite, lavender, lavenderBlush, lemonChiffon };
             int level = 0;
             DataRow p = row;
-            while(p = p.parent) level++;
-            background = colors[(level % (sizeof(colors)/sizeof(colors[0]))];
+            while((p = p.parent)) level++;
+            background = colors[level % (sizeof(colors)/sizeof(colors[0]))];
             surface.SetBackground(background);
             surface.Area(rowStart, y, clientSize.w, (y + rowHeight) - 1);
             foreground = branchesColor;
@@ -2436,11 +2453,11 @@ private:
             surface.SetForeground(foreground);
             surface.SetBackground(background);
 
-            ((void (*)(void *, 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);
+            ((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);
          }
          else
          {
-            if(!opacity) surface.TextOpacity(false);
+            if(opacity < 1) surface.TextOpacity(false);
             // Draw the rows
             for(field = fields.first; field; field = field.next)
             {
@@ -2504,6 +2521,10 @@ private:
                   foreground = this.background;
                }
 
+#ifdef _DEBUG
+               // surface.WriteTextf(x + 100, y,  "ix: %d", row.index);
+#endif
+
                x += width;// + EXTRA_SPACE;
 
                if(row.header) break;
@@ -2739,7 +2760,7 @@ private:
 
    bool HeaderPushed(Button control, int x, int y, Modifiers mods)
    {
-      DataField field = (DataField)control.id;
+      DataField field = (DataField)(intptr)control.id;
       // false: dont destroy edit box
       HideEditBox(true, false, true);
       if(style.resizable && ((!field && x < RESIZE_BORDER && fields.last) ||
@@ -2852,7 +2873,7 @@ private:
       }
       else if(style.resizable)
       {
-         DataField field = (DataField)control.id;
+         DataField field = (DataField)(intptr)control.id;
          if(field)
          {
             if(x < RESIZE_BORDER && field.prev)
@@ -2942,7 +2963,7 @@ private:
    {
       if(style.header && !this.dropField && style.sortable)
       {
-         DataField field = (DataField)control.id;
+         DataField field = (DataField)(intptr)control.id;
          if(sortField == field)
             field.sortOrder *= -1;
          else
@@ -2960,7 +2981,7 @@ private:
    {
       if(style.resizable)
       {
-         DataField field = (DataField)control.id;
+         DataField field = (DataField)(intptr)control.id;
          if(field)
          {
             if(x < RESIZE_BORDER && field.prev)
@@ -2989,7 +3010,7 @@ private:
          if(!visible)
          {
             ReleaseCapture();
-            this.rolledOver = this.dragging = false;
+            rolledOver = dragging = false;
          }
          /*else
             Capture();*/
@@ -3077,12 +3098,12 @@ private:
                      if(cell && cell.isSet && field.dataType)
                      {
                         static char tempString[4096];
-                        char * string;
+                        const char * string;
                         int tw, th;
                         if(field.dataType.type == normalClass || field.dataType.type == noHeadClass)
-                           string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, cell.data[0], tempString, field.userData, null);
+                           string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, cell.data[0], tempString, field.userData, null);
                         else
-                           string = ((char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, cell.data, tempString, field.userData, null);
+                           string = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, cell.data, tempString, field.userData, null);
                         /* GCC-4.4 Bug!
                        if(!string) string = "";
                         display.FontExtent(row.header ? boldFont : font, string, strlen(string), &tw, &th);
@@ -3185,16 +3206,16 @@ private:
       }
 
       // ADDED THIS CHECK FOR FieldDropBox LEAKS
-      if(/*!mods.isSideEffect && */(this.rolledOver || !this.dragging))
+      if(/*!mods.isSideEffect && */(rolledOver || !dragging))
       {
          int rowY = (style.header) ? rowHeight : 0;
-         DataRow row = null;
+         DataRow row = null, nextRow;
          int rowIndex;
 
          mouseX = x;
          mouseY = y;
 
-         if(this.dragging &&
+         if(dragging &&
             ((vertScroll && vertScroll.visible &&
              (y < 0 || y >= clientSize.h)) ||
              (horzScroll && horzScroll.visible &&
@@ -3217,15 +3238,20 @@ private:
          // This must be done after the scrolling took place
          rowIndex = firstRowShown ? firstRowShown.index : -1;
          y = Max(y, 0);
-         y = Min(y, clientSize.h-1);
-         for(row = firstRowShown; row; row = row.GetNextRow(), rowIndex ++)
+         y = Min(y, clientSize.h-rowHeight-1);
+         for(row = firstRowShown; row; row = nextRow, rowIndex ++)
          {
+            nextRow = row.GetNextRow();
             rowY += rowHeight;
-            if(rowY > y)
+            if(rowY > y || !nextRow)
             {
                break;
             }
          }
+         if(row && row == currentRow)
+            row = row.GetNextRow();
+         if(row && row.parent == currentRow)
+            row = row.GetNextRow();
 
          if(row && currentRow != row)
          {
@@ -3251,7 +3277,7 @@ private:
                   this.movedRow = true;
                }
             }
-            else if((style.freeSelect  || this.dragging) && ((realX>= 0 && realY >= 0 && realX< clientSize.w && realY < clientSize.h) || this.rolledOver))
+            else if((style.freeSelect  || this.dragging) && ((realX>= 0 && realY >= 0 && realX< clientSize.w && realY < clientSize.h) || rolledOver))
             {
                if(!(style.multiSelect))
                {
@@ -3309,8 +3335,8 @@ private:
 
    bool OnMouseOver(int x, int y, Modifiers mods)
    {
-      if(this.dragging)
-         this.rolledOver = true;
+      if(dragging)
+         rolledOver = true;
       return true;
    }
 
@@ -3377,7 +3403,6 @@ private:
          int rowIndex = firstRowShown ? firstRowShown.index : -1;
          DataRow previousRow = currentRow;
          DataRow newCurrentRow = null;
-         DataField newCurrentField = null;
          bool moveMultiple = false;
          int numSelected = 0;
          int rowStart = -scroll.x;
@@ -3629,6 +3654,8 @@ private:
                      this.dragRow = currentRow;
                      this.dropIndex = -1;
                      this.movedRow = false;
+
+                     Capture();
                   }
                   if(editData && editData.visible && style.alwaysEdit)
                   {
@@ -3675,15 +3702,13 @@ private:
                   // activate it
                   if(editData && editData.visible && newCurrentRow)
                   {
-                     DataField field, whichField;
+                     DataField field;
                      int sx = -scroll.x;
                      int indent = 0;
 
                      if(style.collapse && !(style.treeBranch))
                         sx += 15;
 
-                     whichField = currentField;
-
                      {
                         DataRow parent;
                         for(parent = newCurrentRow.parent; parent; parent = parent.parent)
@@ -3793,11 +3818,11 @@ private:
       {
          if(!style.noDragging)
          {
-            this.dragging = true;
+            dragging = true;
             Capture();
          }
          if(x >= 0 && y >= 0 && x < clientSize.w && y < clientSize.h)
-            this.rolledOver = true;
+            rolledOver = true;
          Update(null);
       }
       else
@@ -3826,6 +3851,7 @@ private:
       {
          DataRow row, switchRow = rows.last;
          int rowY = (style.header) ? rowHeight : 0;
+         while(switchRow.lastRow) switchRow = switchRow.lastRow;
          for(row = firstRowShown; row; row = row.GetNextRow())
          {
             rowY += rowHeight;
@@ -3934,18 +3960,50 @@ private:
                   // Switch row first: move before
                   if(row == switchRow)
                   {
-                     if(NotifyMove(master, this, switchRow.prev, mods))
-                        dragRow.Move(switchRow.prev);
+                     DataRow actualMoveRow;
+
+                     if(!switchRow.prev && switchRow.parent == dragRow.parent)
+                        actualMoveRow = null;
+                     else
+                     {
+                        actualMoveRow = switchRow.prev ? switchRow.prev : switchRow;
+                        while(actualMoveRow && actualMoveRow.parent != dragRow.parent && actualMoveRow.parent)
+                           actualMoveRow = actualMoveRow.parent;
+                     }
+
+                     if(!actualMoveRow || (actualMoveRow && actualMoveRow.parent == dragRow.parent))
+                        if(NotifyMove(master, this, actualMoveRow, mods))
+                        {
+                           dragRow.Move(actualMoveRow);
+                           NotifyMoved(master, this, actualMoveRow, mods);
+                        }
                   }
                   // Dragged row first: move after
                   else
                   {
-                     if(NotifyMove(master, this, switchRow, mods))
-                        dragRow.Move(switchRow);
+                     DataRow actualMoveRow = switchRow;
+                     DataRow nextRow = switchRow.GetNextRow();
+
+                     while(nextRow && nextRow.parent != dragRow.parent)
+                        nextRow = nextRow.parent ? nextRow.parent.next : null;
+                     if(nextRow)
+                        actualMoveRow = nextRow.prev;
+
+                     if(!nextRow)
+                        actualMoveRow = dragRow.parent ? dragRow.parent.subRows.last : rows.last;
+
+                     if(!actualMoveRow || (actualMoveRow != dragRow && actualMoveRow.parent == dragRow.parent))
+                        if(NotifyMove(master, this, actualMoveRow, mods))
+                        {
+                           dragRow.Move(actualMoveRow);
+                           NotifyMoved(master, this, actualMoveRow, mods);
+                        }
                   }
                }
             }
          }
+         if(dragRow)
+            ReleaseCapture();
          dragRow = null;
          editRow = null;
          movedRow = false;
@@ -3954,11 +4012,11 @@ private:
       }
 
       timer.Stop();
-      if(this.dragging || style.freeSelect)
+      if(dragging || style.freeSelect)
       {
-         if(this.dragging)
+         if(dragging)
          {
-            this.rolledOver = this.dragging = false;
+            rolledOver = dragging = false;
          }
          if(x >= 0 && y >= 0 && x < clientSize.w && y < clientSize.h && currentRow && style.freeSelect)
          {
@@ -4066,7 +4124,7 @@ private:
                void * data = row.GetData(field);
                char tempString[1024] = "";
                bool needClass = false;
-               char * string = data ? ((char *(*)(void *, void *, char *, void *, bool *))(void *)field.dataType._vTbl[__ecereVMethodID_class_OnGetString])(field.dataType, data, tempString, null, &needClass) : null;
+               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;
 
                if(string && string[0])
                   checkNextField = false;
@@ -4075,16 +4133,10 @@ private:
                   if(style.multiSelect)
                   {
                      DataRow selRow;
-                     bool foundRow = false;
 
-                     //this.clickedRowIndex = 0;
                      clickedRow = row;
                      for(selRow = rows.first; selRow; selRow = selRow.GetNextRow())
-                     {
-                        if(selRow == row) foundRow = true;
                         selRow.selectedFlag = unselected;
-                        //if(!foundRow) this.clickedRowIndex++;
-                     }
                      row.selectedFlag = selected;
                   }
                   SetCurrentRow(row, true);
@@ -4118,7 +4170,7 @@ private:
       }
       else if(key == escape)
       {
-         if(resizingField || this.movingFields || (editData && editData.visible) || this.dragRow)
+         if(resizingField || this.movingFields || (editData && editData.visible) || dragRow)
          {
             if(editData && editData.visible && style.alwaysEdit && !editData.active)
                return true;
@@ -4131,10 +4183,13 @@ private:
                resizingField = null;
                ReleaseCapture();
             }
-            this.dragRow = null;
-            if(this.dragging)
+            if(dragRow)
+               ReleaseCapture();
+
+            dragRow = null;
+            if(dragging)
             {
-               this.dragging = false;
+               dragging = false;
                ReleaseCapture();
             }
 
@@ -4528,7 +4583,7 @@ private:
    int rowCount;
    int rowHeight;
    int fontH;
-   double typingTimeOut;
+   public double typingTimeOut;
    char * typedString;
 
    int mouseX, mouseY;