gui/TabControl: Fixed memory leaks
[sdk] / ecere / src / gui / controls / TabControl.ec
index 9c0d6b0..aa5dfa5 100644 (file)
@@ -231,7 +231,7 @@ static void PlaceButton(TabButton button, TabsPlacement placement, bool selected
 static define skinMainColor = Color { 0, 71, 128 };
 static define skinBackground = Color { 255, 255, 255 };
 static define skinTextColor = skinMainColor;
-static define skinInactiveTextColor = Color { skinMainColor.r - 20, skinMainColor.g - 20, skinMainColor.b - 20 };
+static define skinInactiveTextColor = Color { 0, 51, 108 };
 
 #define CAPTION      14
 #define NAME_OFFSET   2
@@ -301,7 +301,7 @@ public class TabControl : Window
       return false;
    }
 
-   void ShowDecorations(Font captionFont, Surface surface, char * name, bool active, bool moving)
+   void ShowDecorations(Font captionFont, Surface surface, const char * name, bool active, bool moving)
    {
       if(placement == bottom && ((BorderBits)borderStyle).fixed)
       {
@@ -540,7 +540,7 @@ public class TabControl : Window
 
    bool NotifyClicked(Button button, int x, int y, Modifiers mods)
    {
-      if(curTab == (Tab)button.id)
+      if(curTab == (Tab)(intptr)button.id)
          return true;
       //curButton.Activate();
       curButton.MakeActive();
@@ -549,7 +549,7 @@ public class TabControl : Window
       {
          curButton.checked = false;
          button.checked = true;
-         curTab = (Tab)button.id;
+         curTab = (Tab)(intptr)button.id;
 
          if(curButton)
             PlaceButton(curButton, placement, false, buttonsOffset);
@@ -572,23 +572,32 @@ public class TabControl : Window
 
    public void AddTab(Tab tab)
    {
-      tab.parent = this;
+      if(tab.parent != this)
+      {
+         tab.parent = this;
+         return;
+      }
       tab.autoCreate = false;
       tab.id = numTabs;
-      tab.button = TabButton
+      if(!tab.button)
       {
-         parent = tabButtons,
-         master = this, stayDown = true,
-         text = tab.text, id = (int64)tab, NotifyClicked = NotifyClicked,
-         tab = tab,
-         background = background;
-      };
-      incref tab.button;
+         tab.button = TabButton
+         {
+            parent = tabButtons,
+            master = this, stayDown = true,
+            text = tab.text, id = (int64)(intptr)tab, NotifyClicked = NotifyClicked,
+            tab = tab,
+            background = background;
+         };
+         incref tab.button;
+      }
 
       if(created)
       {
          tab.button.Create();
-         incref tab;
+         // This was causing leaks with RemoveTab / AddTab usage in Cartographer...
+         // RemoveTab() does not dec'ref...
+         // incref tab;
       }
 
       numTabs++;
@@ -614,16 +623,19 @@ public class TabControl : Window
    {
       Window child;
       Tab fallbackTab = null;
-      tab.parent = null;
+      // tab.parent = null;
       for(child = tabButtons.children.first; child; child = child.next)
       {
          if(child._class == class(TabButton))
          {
             TabButton button = (TabButton)child;
-            if(button.id == (int64)tab)
+            if(button.id == (int64)(intptr)tab)
             {
                if(button.created)
+               {
                   button.Destroy(0);
+                  numTabs--;
+               }
                break;
             }
             else
@@ -636,12 +648,13 @@ public class TabControl : Window
             fallbackTab = tabButtons.children.first ? ((TabButton)tabButtons.children.first).tab : null;
          if(fallbackTab)
             fallbackTab.SelectTab();
+         else
+            curTab = null;
          /*curTab = fallbackTab;
          curButton = curTab.button;
          curButton.checked = true;
          curTab.autoCreate = true;*/
       }
-      numTabs--;
    }
 
    ~TabControl()