X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/524c47aa3adf2af11a3069fd5da035a604f08f66..6178debcd342963974dbdce6ac2ddb2fbe89e42a:/src/osx/carbon/toolbar.cpp diff --git a/src/osx/carbon/toolbar.cpp b/src/osx/carbon/toolbar.cpp index d2e2b5cccb..35b9bd4ff8 100644 --- a/src/osx/carbon/toolbar.cpp +++ b/src/osx/carbon/toolbar.cpp @@ -145,7 +145,7 @@ public: { // separator size curSize = GetToolBar()->GetToolSize(); - if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL ) + if ( GetToolBar()->GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) ) curSize.y /= 4; else curSize.x /= 4; @@ -159,19 +159,22 @@ public: return wxPoint( m_x, m_y ); } - bool DoEnable( bool enable ); + virtual bool Enable( bool enable ); void UpdateToggleImage( bool toggle ); -#if wxOSX_USE_NATIVE_TOOLBAR - void SetToolbarItemRef( HIToolbarItemRef ref ) + virtual bool Toggle(bool toggle) { - if ( m_controlHandle ) - HideControl( m_controlHandle ); - if ( m_toolbarItemRef ) - CFRelease( m_toolbarItemRef ); + if ( wxToolBarToolBase::Toggle( toggle ) == false ) + return false; - m_toolbarItemRef = ref; + UpdateToggleImage(toggle); + return true; + } + + void UpdateHelpStrings() + { +#if wxOSX_USE_NATIVE_TOOLBAR if ( m_toolbarItemRef ) { wxFontEncoding enc = GetToolBarFontEncoding(); @@ -181,27 +184,37 @@ public: wxCFStringRef( GetShortHelp(), enc ), wxCFStringRef( GetLongHelp(), enc ) ); } +#endif } - HIToolbarItemRef GetToolbarItemRef() const + virtual bool SetShortHelp(const wxString& help) { - return m_toolbarItemRef; + if ( wxToolBarToolBase::SetShortHelp( help ) == false ) + return false; + + UpdateHelpStrings(); + return true; } - void SetIndex( CFIndex idx ) + virtual bool SetLongHelp(const wxString& help) { - m_index = idx; + if ( wxToolBarToolBase::SetLongHelp( help ) == false ) + return false; + + UpdateHelpStrings(); + return true; } - CFIndex GetIndex() const + virtual void SetNormalBitmap(const wxBitmap& bmp) { - return m_index; + wxToolBarToolBase::SetNormalBitmap(bmp); + UpdateToggleImage(CanBeToggled() && IsToggled()); } virtual void SetLabel(const wxString& label) { wxToolBarToolBase::SetLabel(label); - +#if wxOSX_USE_NATIVE_TOOLBAR if ( m_toolbarItemRef ) { // strip mnemonics from the label for compatibility with the usual @@ -212,6 +225,34 @@ public: m_toolbarItemRef, wxCFStringRef(labelStr, GetToolBarFontEncoding()) ); } +#endif + } + +#if wxOSX_USE_NATIVE_TOOLBAR + void SetToolbarItemRef( HIToolbarItemRef ref ) + { + if ( m_controlHandle ) + HideControl( m_controlHandle ); + if ( m_toolbarItemRef ) + CFRelease( m_toolbarItemRef ); + + m_toolbarItemRef = ref; + UpdateHelpStrings(); + } + + HIToolbarItemRef GetToolbarItemRef() const + { + return m_toolbarItemRef; + } + + void SetIndex( CFIndex idx ) + { + m_index = idx; + } + + CFIndex GetIndex() const + { + return m_index; } #endif // wxOSX_USE_NATIVE_TOOLBAR @@ -384,8 +425,11 @@ DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler ) #endif -bool wxToolBarTool::DoEnable( bool enable ) +bool wxToolBarTool::Enable( bool enable ) { + if ( wxToolBarToolBase::Enable( enable ) == false ) + return false; + if ( IsControl() ) { GetControl()->Enable( enable ); @@ -457,16 +501,20 @@ void wxToolBarTool::UpdateToggleImage( bool toggle ) { if ( toggle ) { - int w = m_bmpNormal.GetWidth(); - int h = m_bmpNormal.GetHeight(); + int w = m_bmpNormal.GetWidth() + 6; + int h = m_bmpNormal.GetHeight() + 6; wxBitmap bmp( w, h ); wxMemoryDC dc; dc.SelectObject( bmp ); - dc.SetPen( wxPen(*wxBLACK) ); - dc.SetBrush( wxBrush( *wxLIGHT_GREY )); - dc.DrawRectangle( 0, 0, w, h ); - dc.DrawBitmap( m_bmpNormal, 0, 0, true ); + wxColour mid_grey_75 = wxColour(128, 128, 128, 196); + wxColour light_grey_75 = wxColour(196, 196, 196, 196); + dc.GradientFillLinear( wxRect(1, 1, w - 1, h-1), + light_grey_75, mid_grey_75, wxNORTH); + wxColour black_50 = wxColour(0, 0, 0, 127); + dc.SetPen( wxPen(black_50) ); + dc.DrawRoundedRectangle( 0, 0, w, h, 1.5 ); + dc.DrawBitmap( m_bmpNormal, 3, 3, true ); dc.SelectObject( wxNullBitmap ); ControlButtonContentInfo info; wxMacCreateBitmapButton( &info, bmp ); @@ -879,25 +927,23 @@ bool wxToolBar::Create( wxToolBar::~wxToolBar() { #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macToolbar != NULL) - { - // if this is the installed toolbar, then deinstall it - if (m_macUsesNativeToolbar) - MacInstallNativeToolbar( false ); + // We could be not using a native tool bar at all, this happens when we're + // created with something other than the frame as parent for example. + if ( !m_macToolbar ) + return; - CFIndex count = CFGetRetainCount( m_macToolbar ) ; - // Leopard seems to have one refcount more, so we cannot check reliably at the moment - if ( UMAGetSystemVersion() < 0x1050 ) + CFIndex count = CFGetRetainCount( m_macToolbar ) ; + // Leopard seems to have one refcount more, so we cannot check reliably at the moment + if ( UMAGetSystemVersion() < 0x1050 ) + { + if ( count != 1 ) { - if ( count != 1 ) - { - wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor"); - } + wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor"); } - CFRelease( (HIToolbarRef)m_macToolbar ); - m_macToolbar = NULL; } -#endif + CFRelease( (HIToolbarRef)m_macToolbar ); + m_macToolbar = NULL; +#endif // wxOSX_USE_NATIVE_TOOLBAR } bool wxToolBar::Show( bool show ) @@ -1037,7 +1083,7 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) if (usesNative && (m_macToolbar == NULL)) return bResult; - if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0)) + if (usesNative && ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM)) != 0)) return bResult; WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); @@ -1060,8 +1106,16 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) bResult = true; SetWindowToolbar( tlw, (HIToolbarRef) m_macToolbar ); + + // ShowHideWindowToolbar will make the wxFrame grow + // which we don't want in this case + wxSize sz = GetParent()->GetSize(); ShowHideWindowToolbar( tlw, true, false ); + // Restore the orginal size + GetParent()->SetSize( sz ); + ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 ); + SetAutomaticControlDragTrackingEnabledForWindow( tlw, true ); m_peer->Move(0,0,0,0 ); @@ -1079,7 +1133,7 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) ShowHideWindowToolbar( tlw, false, false ); ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute ); - SetWindowToolbar( tlw, NULL ); + MacUninstallNativeToolbar(); m_peer->SetVisibility( true ); } @@ -1091,13 +1145,25 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") ); return bResult; } + +void wxToolBar::MacUninstallNativeToolbar() +{ + if (!m_macToolbar) + return; + + WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); + if (tlw) + SetWindowToolbar( tlw, NULL ); +} #endif bool wxToolBar::Realize() { - if (m_tools.GetCount() == 0) + if ( !wxToolBarBase::Realize() ) return false; + wxSize tlw_sz = GetParent()->GetSize(); + int maxWidth = 0; int maxHeight = 0; @@ -1164,7 +1230,7 @@ bool wxToolBar::Realize() if ( y + cursize.y > maxHeight ) maxHeight = y + cursize.y; - if ( GetWindowStyleFlag() & wxTB_VERTICAL ) + if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) ) { int x1 = x + ( maxToolWidth - cursize.x ) / 2; tool->SetPosition( wxPoint(x1, y) ); @@ -1176,7 +1242,7 @@ bool wxToolBar::Realize() } // update the item positioning state - if ( GetWindowStyleFlag() & wxTB_VERTICAL ) + if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) ) y += cursize.y + kwxMacToolSpacing; else x += cursize.x + kwxMacToolSpacing; @@ -1190,10 +1256,6 @@ bool wxToolBar::Realize() { // since setting the help texts is non-virtual we have to update // the strings now - HIToolbarItemSetHelpText( hiItemRef, - wxCFStringRef( tool->GetShortHelp(), enc ), - wxCFStringRef( tool->GetLongHelp(), enc ) ); - if ( insertAll || (tool->GetIndex() != currentPosition) ) { OSStatus err = noErr; @@ -1260,6 +1322,10 @@ bool wxToolBar::Realize() wxFAIL_MSG("Reference count of native tool was illegal before removal"); } wxASSERT( IsValidControlHandle(tool->GetControl()->GetPeer()->GetControlRef() )) ; + + wxString label = tool->GetLabel(); + if ( !label.empty() ) + HIToolbarItemSetLabel( hiItemRef, wxCFStringRef(label, GetFont().GetEncoding()) ); } } @@ -1309,14 +1375,16 @@ bool wxToolBar::Realize() node = node->GetNext(); } - if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) + if (m_macUsesNativeToolbar) + GetParent()->SetSize( tlw_sz ); + + if ( GetWindowStyleFlag() & (wxTB_TOP|wxTB_BOTTOM) ) { // 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; } @@ -1327,7 +1395,6 @@ bool wxToolBar::Realize() SetRows( GetToolsCount() ); m_minHeight = maxHeight; - maxHeight = th; maxWidth += m_xMargin + kwxMacToolBarLeftMargin; m_minWidth = m_maxWidth = maxWidth; } @@ -1338,7 +1405,7 @@ bool wxToolBar::Realize() bool wantNativeToolbar, ownToolbarInstalled; // attempt to install the native toolbar - wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0); + wantNativeToolbar = ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_BOTTOM|wxTB_RIGHT)) == 0); MacInstallNativeToolbar( wantNativeToolbar ); (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); if (!ownToolbarInstalled) @@ -1407,7 +1474,7 @@ void wxToolBar::MacSuperChangedPosition() void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) { - wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); + wxToolBarTool* tool = static_cast(FindById(id)); if ( tool ) { wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); @@ -1421,7 +1488,7 @@ void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) { - wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); + wxToolBarTool* tool = static_cast(FindById(id)); if ( tool ) { wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); @@ -1449,7 +1516,7 @@ wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const node = node->GetNext(); } - return (wxToolBarToolBase*)NULL; + return NULL; } wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) @@ -1461,22 +1528,19 @@ wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) return wxEmptyString; } -void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) +void wxToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(t), bool WXUNUSED(enable)) { - if ( t != NULL ) - ((wxToolBarTool*)t)->DoEnable( enable ); + // everything already done in the tool's implementation } -void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) +void wxToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(t), bool WXUNUSED(toggle)) { - wxToolBarTool *tool = (wxToolBarTool *)t; - if ( ( tool != NULL ) && tool->IsButton() ) - tool->UpdateToggleImage( toggle ); + // everything already done in the tool's implementation } bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) { - wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase ); + wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase ); if (tool == NULL) return false; @@ -1503,7 +1567,7 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) wxASSERT( tool->GetControlHandle() == NULL ); toolSize.x /= 4; toolSize.y /= 4; - if ( GetWindowStyleFlag() & wxTB_VERTICAL ) + if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) ) toolrect.bottom = toolSize.y; else toolrect.right = toolSize.x; @@ -1601,7 +1665,7 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) #if wxOSX_USE_NATIVE_TOOLBAR if (m_macToolbar != NULL) { - wxCHECK_MSG( tool->GetControl(), false, _T("control must be non-NULL") ); + wxCHECK_MSG( tool->GetControl(), false, wxT("control must be non-NULL") ); HIToolbarItemRef item; HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ; CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ; @@ -1655,12 +1719,12 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) { - wxFAIL_MSG( wxT("not implemented") ); + // nothing to do } bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) { - wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase ); + wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase ); wxToolBarToolsList::compatibility_iterator node; for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) { @@ -1700,7 +1764,7 @@ bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); wxPoint pt = tool2->GetPosition(); - if ( GetWindowStyleFlag() & wxTB_VERTICAL ) + if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) ) pt.y -= sz.y; else pt.x -= sz.x;