+#if wxMAC_USE_NATIVE_TOOLBAR
+ Rect boundsR;
+ bool ownToolbarInstalled;
+
+ MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
+ if ( ownToolbarInstalled )
+ {
+ // TODO: is this really a control ?
+ GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
+ if ( width != NULL )
+ *width = boundsR.right - boundsR.left;
+ if ( height != NULL )
+ *height = boundsR.bottom - boundsR.top;
+ }
+ else
+ wxToolBarBase::DoGetSize( width, height );
+
+#else
+ wxToolBarBase::DoGetSize( width, height );
+#endif
+}
+
+wxSize wxToolBar::DoGetBestSize() const
+{
+ int width, height;
+
+ DoGetSize( &width, &height );
+
+ return wxSize( width, height );
+}
+
+void wxToolBar::SetWindowStyleFlag( long style )
+{
+ wxToolBarBase::SetWindowStyleFlag( style );
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if (m_macHIToolbarRef != NULL)
+ {
+ HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
+
+ if ( style & wxTB_NOICONS )
+ mode = kHIToolbarDisplayModeLabelOnly;
+ else if ( style & wxTB_TEXT )
+ mode = kHIToolbarDisplayModeIconAndLabel;
+ else
+ mode = kHIToolbarDisplayModeIconOnly;
+
+ HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
+ }
+#endif
+}
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+bool wxToolBar::MacWantsNativeToolbar()
+{
+ return m_macUsesNativeToolbar;
+}
+
+bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
+{
+ bool bResultV = false;
+
+ if (ownToolbarInstalled != NULL)
+ *ownToolbarInstalled = false;
+
+ WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
+ if (tlw != NULL)
+ {
+ HIToolbarRef curToolbarRef = NULL;
+ OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
+ bResultV = ((err == noErr) && (curToolbarRef != NULL));
+ if (bResultV && (ownToolbarInstalled != NULL))
+ *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
+ }
+
+ return bResultV;
+}
+
+bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
+{
+ bool bResult = false;
+
+ if (usesNative && (m_macHIToolbarRef == NULL))
+ return bResult;
+
+ if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
+ return bResult;
+
+ WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
+ if (tlw == NULL)
+ return bResult;
+
+ // check the existing toolbar
+ HIToolbarRef curToolbarRef = NULL;
+ OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
+ if (err != noErr)
+ curToolbarRef = NULL;
+
+ m_macUsesNativeToolbar = usesNative;
+
+ if (m_macUsesNativeToolbar)
+ {
+ // only install toolbar if there isn't one installed already
+ if (curToolbarRef == NULL)
+ {
+ bResult = true;
+
+ SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
+ ShowHideWindowToolbar( tlw, true, false );
+ ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
+ SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
+
+ Rect r = { 0, 0, 0, 0 };
+ m_peer->SetRect( &r );
+ SetSize( wxSIZE_AUTO_WIDTH, 0 );
+ m_peer->SetVisibility( false, true );
+ wxToolBarBase::Show( false );
+ }
+ }
+ else
+ {
+ // only deinstall toolbar if this is the installed one
+ if (m_macHIToolbarRef == curToolbarRef)
+ {
+ bResult = true;
+
+ ShowHideWindowToolbar( tlw, false, false );
+ ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
+ SetWindowToolbar( tlw, NULL );
+
+ m_peer->SetVisibility( true, true );
+ }
+ }
+
+ if (bResult)
+ InvalidateBestSize();
+
+// wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
+ return bResult;
+}
+#endif
+
+bool wxToolBar::Realize()
+{
+ if (m_tools.GetCount() == 0)
+ return false;
+
+ int maxWidth = 0;
+ int maxHeight = 0;
+
+ int maxToolWidth = 0;
+ int maxToolHeight = 0;
+
+ int x = m_xMargin + kwxMacToolBarLeftMargin;
+ int y = m_yMargin + kwxMacToolBarTopMargin;
+
+ int tw, th;
+ GetSize( &tw, &th );
+
+ // find the maximum tool width and height
+ wxToolBarTool *tool;
+ wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
+ while ( node )
+ {
+ tool = (wxToolBarTool *) node->GetData();
+ if ( tool != NULL )
+ {
+ wxSize sz = tool->GetSize();
+
+ if ( sz.x > maxToolWidth )
+ maxToolWidth = sz.x;
+ if ( sz.y > maxToolHeight )
+ maxToolHeight = sz.y;
+ }
+
+ node = node->GetNext();
+ }
+
+ bool lastIsRadio = false;
+ bool curIsRadio = false;
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ CFIndex currentPosition = 0;
+ bool insertAll = false;
+
+ HIToolbarRef refTB = (HIToolbarRef)m_macHIToolbarRef;
+#endif
+
+ node = m_tools.GetFirst();
+ while ( node )
+ {
+ tool = (wxToolBarTool*) node->GetData();
+ if ( tool == NULL )
+ {
+ node = node->GetNext();
+ continue;
+ }
+
+ // set tool position:
+ // for the moment just perform a single row/column alignment
+ wxSize cursize = tool->GetSize();
+ if ( x + cursize.x > maxWidth )
+ maxWidth = x + cursize.x;
+ if ( y + cursize.y > maxHeight )
+ maxHeight = y + cursize.y;
+
+ if ( GetWindowStyleFlag() & wxTB_VERTICAL )
+ {
+ int x1 = x + ( maxToolWidth - cursize.x ) / 2;
+ tool->SetPosition( wxPoint(x1, y) );
+ }
+ else
+ {
+ int y1 = y + ( maxToolHeight - cursize.y ) / 2;
+ tool->SetPosition( wxPoint(x, y1) );
+ }
+
+ // update the item positioning state
+ if ( GetWindowStyleFlag() & wxTB_VERTICAL )
+ y += cursize.y + kwxMacToolSpacing;
+ else
+ x += cursize.x + kwxMacToolSpacing;
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ // install in native HIToolbar
+ if ( refTB )
+ {
+ HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
+ if ( hiItemRef != NULL )
+ {
+ if ( insertAll || (tool->GetIndex() != currentPosition) )
+ {
+ OSStatus err = noErr;
+ if ( !insertAll )
+ {
+ insertAll = true;
+
+ // if this is the first tool that gets newly inserted or repositioned
+ // first remove all 'old' tools from here to the right, because of this
+ // all following tools will have to be reinserted (insertAll).
+ for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
+ node2 != node;
+ node2 = node2->GetPrevious() )
+ {
+ wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
+
+ const long idx = tool2->GetIndex();
+ if ( idx != -1 )
+ {
+ if ( tool2->IsControl() )
+ {
+ CFIndex count = CFGetRetainCount( tool2->GetControl()->GetPeer()->GetControlRef() ) ;
+ wxASSERT_MSG( count == 3 || count == 2 , wxT("Reference Count of native tool was illegal before removal") );
+ wxASSERT( IsValidControlHandle(tool2->GetControl()->GetPeer()->GetControlRef() )) ;
+ }
+ err = HIToolbarRemoveItemAtIndex(refTB, idx);
+ if ( err != noErr )
+ {
+ wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
+ idx, (long)err);
+ }
+ if ( tool2->IsControl() )
+ {
+ CFIndex count = CFGetRetainCount( tool2->GetControl()->GetPeer()->GetControlRef() ) ;
+ wxASSERT_MSG( count == 2 , wxT("Reference Count of native tool was not 2 after removal") );
+ wxASSERT( IsValidControlHandle(tool2->GetControl()->GetPeer()->GetControlRef() )) ;
+ }
+
+ tool2->SetIndex(-1);
+ }
+ }
+ }
+
+ err = HIToolbarInsertItemAtIndex( refTB, hiItemRef, currentPosition );
+ if (err != noErr)
+ {
+ wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
+ }
+
+ tool->SetIndex( currentPosition );
+ if ( tool->IsControl() )
+ {
+ CFIndex count = CFGetRetainCount( tool->GetControl()->GetPeer()->GetControlRef() ) ;
+ wxASSERT_MSG( count == 3 || count == 2, wxT("Reference Count of native tool was illegal after insertion") );
+ wxASSERT( IsValidControlHandle(tool->GetControl()->GetPeer()->GetControlRef() )) ;
+ }
+ }
+
+ currentPosition++;
+ }
+ }
+#endif
+
+ // update radio button (and group) state
+ lastIsRadio = curIsRadio;
+ curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
+
+ if ( !curIsRadio )
+ {
+ if ( tool->IsToggled() )
+ DoToggleTool( tool, true );
+ }
+ else
+ {
+ if ( !lastIsRadio )
+ {
+ if ( tool->Toggle( true ) )
+ {
+ DoToggleTool( tool, true );
+ }
+ }
+ else if ( tool->IsToggled() )
+ {
+ if ( tool->IsToggled() )
+ DoToggleTool( tool, true );
+
+ wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
+ while ( nodePrev )
+ {
+ wxToolBarToolBase *toggleTool = nodePrev->GetData();
+ if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
+ break;
+
+ if ( toggleTool->Toggle( false ) )
+ DoToggleTool( toggleTool, false );
+
+ nodePrev = nodePrev->GetPrevious();
+ }
+ }
+ }
+
+ node = node->GetNext();
+ }
+
+ if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
+ {
+ // if not set yet, only one row
+ if ( m_maxRows <= 0 )
+ SetRows( 1 );
+
+ m_minWidth = maxWidth;
+ maxWidth = tw;
+ maxHeight += m_yMargin + kwxMacToolBarTopMargin;
+ m_minHeight = m_maxHeight = maxHeight;
+ }
+ else
+ {
+ // if not set yet, have one column
+ if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
+ SetRows( GetToolsCount() );
+
+ m_minHeight = maxHeight;
+ maxHeight = th;
+ maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
+ m_minWidth = m_maxWidth = maxWidth;
+ }
+
+#if 0
+ // FIXME: should this be OSX-only?
+ {
+ bool wantNativeToolbar, ownToolbarInstalled;
+
+ // attempt to install the native toolbar
+ wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
+ MacInstallNativeToolbar( wantNativeToolbar );
+ (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
+ if (!ownToolbarInstalled)
+ {
+ SetSize( maxWidth, maxHeight );
+ InvalidateBestSize();
+ }
+ }
+#else
+ SetSize( maxWidth, maxHeight );
+ InvalidateBestSize();
+#endif
+
+ SetInitialSize();
+
+ return true;
+}
+
+void wxToolBar::SetToolBitmapSize(const wxSize& size)
+{
+ m_defaultWidth = size.x + kwxMacToolBorder;
+ m_defaultHeight = size.y + kwxMacToolBorder;
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if (m_macHIToolbarRef != NULL)
+ {
+ int maxs = wxMax( size.x, size.y );
+ HIToolbarDisplaySize sizeSpec;
+ if ( maxs > 32 )
+ sizeSpec = kHIToolbarDisplaySizeNormal;
+ else if ( maxs > 24 )
+ sizeSpec = kHIToolbarDisplaySizeDefault;
+ else
+ sizeSpec = kHIToolbarDisplaySizeSmall;
+
+ HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
+ }
+#endif
+}
+
+// The button size is bigger than the bitmap size
+wxSize wxToolBar::GetToolSize() const
+{
+ return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
+}
+
+void wxToolBar::SetRows(int nRows)
+{
+ // avoid resizing the frame uselessly
+ if ( nRows != m_maxRows )
+ m_maxRows = nRows;
+}
+
+void wxToolBar::MacSuperChangedPosition()
+{
+ wxWindow::MacSuperChangedPosition();
+
+#if wxMAC_USE_NATIVE_TOOLBAR
+ if (! m_macUsesNativeToolbar )
+ Realize();
+#else
+
+ Realize();
+#endif
+}
+
+void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
+{
+ wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
+ if ( tool )
+ {
+ wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
+
+ tool->SetNormalBitmap(bitmap);
+
+ // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
+ tool->UpdateToggleImage( tool->CanBeToggled() && tool->IsToggled() );
+ }
+}
+
+void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
+{
+ wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));