controls/TabControl: Modifications to solve Cartographer's styles editor issues
[sdk] / ecere / src / gui / controls / TabControl.ec
index e1e2e91..94a7cc1 100644 (file)
@@ -540,12 +540,12 @@ public class TabControl : Window
 
    bool NotifyClicked(Button button, int x, int y, Modifiers mods)
    {
-      if(curTab == (Tab)(intptr)button.id)
+      if(curTab == (Tab)(intptr)button.id && curTab.created)
          return true;
       //curButton.Activate();
       curButton.MakeActive();
 
-      if(curTab.Destroy(0))
+      if(!curTab || curTab == (Tab)(intptr)button.id || curTab.Destroy(0))
       {
          curButton.checked = false;
          button.checked = true;
@@ -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)(intptr)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,7 +623,7 @@ 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))
@@ -623,25 +632,32 @@ public class TabControl : Window
             if(button.id == (int64)(intptr)tab)
             {
                if(button.created)
+               {
                   button.Destroy(0);
+                  numTabs--;
+               }
                break;
             }
-            else
+            else if(button.created)
                fallbackTab = button.tab;
          }
       }
-      if(curTab == tab)
+      if(curTab == tab && curTab.created)
       {
          if(!fallbackTab)
             fallbackTab = tabButtons.children.first ? ((TabButton)tabButtons.children.first).tab : null;
-         if(fallbackTab)
+         if(fallbackTab && fallbackTab.button.created)
             fallbackTab.SelectTab();
+         else
+         {
+            curTab.Destroy(0);
+            curTab = null;
+         }
          /*curTab = fallbackTab;
          curButton = curTab.button;
          curButton.checked = true;
          curTab.autoCreate = true;*/
       }
-      numTabs--;
    }
 
    ~TabControl()
@@ -737,6 +753,11 @@ public class Tab : Window
       button.NotifyClicked(button.master, button, 0, 0, 0);
    }
 
+   public property bool isAdded
+   {
+      get { return button && button.created; }
+   }
+
    watch(caption)
    {
       if(button)