]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/carbon/toolbar.cpp
Add middle-item click support.
[wxWidgets.git] / src / mac / carbon / toolbar.cpp
index 7614e623124c412aec03d1abf1f5b3a91f02613a..61fdc4325ab3b00124bddcbb82e21a8259bc744d 100644 (file)
@@ -19,6 +19,7 @@
     #include "wx/wx.h"
 #endif
 
+#include "wx/app.h"
 #include "wx/mac/uma.h"
 #include "wx/geometry.h"
 
@@ -82,11 +83,6 @@ public:
     virtual ~wxToolBarTool()
     {
         ClearControl();
-
-#if wxMAC_USE_NATIVE_TOOLBAR
-        if ( m_toolbarItemRef )
-            CFRelease( m_toolbarItemRef );
-#endif
     }
 
     WXWidget GetControlHandle()
@@ -116,7 +112,14 @@ public:
         }
 
 #if wxMAC_USE_NATIVE_TOOLBAR
-        m_toolbarItemRef = NULL;
+        if ( m_toolbarItemRef )
+        {
+            CFIndex count = CFGetRetainCount( m_toolbarItemRef ) ;
+            wxASSERT_MSG( count == 1 , wxT("Reference Count of native tool was not 1 in wxToolBarTool destructor") );
+            wxTheApp->MacAddToAutorelease(m_toolbarItemRef);
+            CFRelease(m_toolbarItemRef);
+            m_toolbarItemRef = NULL;
+        }
 #endif
     }
 
@@ -610,8 +613,19 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef,
                     break;
 
                 case kEventHIObjectDestruct:
-                    free( object ) ;
-                    result = noErr;
+                    {
+                        // we've increased the ref count when creating this, so we decrease manually again in case
+                        // it was never really installed and deinstalled
+                        HIViewRef viewRef = object->viewRef ;
+                        if( viewRef && IsValidControlHandle( viewRef)  )
+                        {
+                            CFIndex count =  CFGetRetainCount( viewRef ) ; 
+                            if ( count >= 1 )
+                                CFRelease( viewRef ) ;
+                        }
+                           free( object ) ;
+                       result = noErr;
+                    }
                     break;
             }
             break;
@@ -834,6 +848,9 @@ wxToolBar::~wxToolBar()
         if (m_macUsesNativeToolbar)
             MacInstallNativeToolbar( false );
 
+        CFIndex count = CFGetRetainCount( m_macHIToolbarRef ) ;
+        wxASSERT_MSG( count == 1 , wxT("Reference Count of native control was not 1 in wxToolBar destructor") );
+
         CFRelease( (HIToolbarRef)m_macHIToolbarRef );
         m_macHIToolbarRef = NULL;
     }
@@ -1518,34 +1535,29 @@ bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
     CFIndex removeIndex = tool->GetIndex();
 #endif
 
+#if wxMAC_USE_NATIVE_TOOLBAR
+    if ( removeIndex != -1 && m_macHIToolbarRef )
+    {
+        HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
+        tool->SetIndex( -1 );
+    }
+#endif
     switch ( tool->GetStyle() )
     {
         case wxTOOL_STYLE_CONTROL:
-            {
+            if ( tool->GetControl() )
                 tool->GetControl()->Destroy();
-                tool->ClearControl();
-            }
             break;
 
         case wxTOOL_STYLE_BUTTON:
         case wxTOOL_STYLE_SEPARATOR:
-            if ( tool->GetControlHandle() )
-            {
-#if wxMAC_USE_NATIVE_TOOLBAR
-                if ( removeIndex != -1 && m_macHIToolbarRef )
-                {
-                    HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
-                    tool->SetIndex( -1 );
-                }
-#endif
-
-                tool->ClearControl();
-            }
+            // nothing special
             break;
 
         default:
             break;
     }
+    tool->ClearControl();
 
     // and finally reposition all the controls after this one