X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/402c3347d26736250a43550ee8253c03fe9052e0..55410bb4f67febe1ca20654f078ea4fb9a6223ae:/src/osx/carbon/toolbar.cpp?ds=inline diff --git a/src/osx/carbon/toolbar.cpp b/src/osx/carbon/toolbar.cpp index a3aedf7db6..43248edb9c 100644 --- a/src/osx/carbon/toolbar.cpp +++ b/src/osx/carbon/toolbar.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: src/mac/carbon/toolbar.cpp +// Name: src/osx/carbon/toolbar.cpp // Purpose: wxToolBar // Author: Stefan Csomor // Modified by: @@ -20,7 +20,7 @@ #endif #include "wx/app.h" -#include "wx/osx/uma.h" +#include "wx/osx/private.h" #include "wx/geometry.h" #include "wx/sysopt.h" @@ -32,9 +32,6 @@ const short kwxMacToolBarLeftMargin = 4; const short kwxMacToolBorder = 0; const short kwxMacToolSpacing = 6; - -IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) - BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) EVT_PAINT( wxToolBar::OnPaint ) END_EVENT_TABLE() @@ -43,7 +40,6 @@ END_EVENT_TABLE() #pragma mark - #pragma mark Tool Implementation - // ---------------------------------------------------------------------------- // private classes // ---------------------------------------------------------------------------- @@ -163,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 ); - - m_toolbarItemRef = ref; + if ( wxToolBarToolBase::Toggle( toggle ) == false ) + return false; + + UpdateToggleImage(toggle); + return true; + } + + void UpdateHelpStrings() + { +#if wxOSX_USE_NATIVE_TOOLBAR if ( m_toolbarItemRef ) { wxFontEncoding enc = GetToolBarFontEncoding(); @@ -185,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 @@ -216,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 @@ -224,7 +261,6 @@ private: wxFontEncoding GetToolBarFontEncoding() const { wxFont f; - wxFontEncoding enc; if ( GetToolBar() ) f = GetToolBar()->GetFont(); return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding(); @@ -389,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 ); @@ -462,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 ); @@ -562,7 +605,7 @@ void wxToolBar::Init() m_defaultHeight = kwxMacToolBarToolDefaultHeight; #if wxOSX_USE_NATIVE_TOOLBAR - m_macHIToolbarRef = NULL; + m_macToolbar = NULL; m_macUsesNativeToolbar = false; #endif } @@ -652,8 +695,6 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, CFIndex count = CFGetRetainCount( viewRef ) ; if ( count >= 1 ) { - wxFAIL_MSG("Reference count of native tool was illegal before removal"); - CFRelease( viewRef ) ; } } @@ -857,11 +898,11 @@ bool wxToolBar::Create( wxString labelStr = wxString::Format( wxT("%p"), this ); err = HIToolbarCreate( wxCFStringRef( labelStr, wxFont::GetDefaultEncoding() ), 0, - (HIToolbarRef*) &m_macHIToolbarRef ); + (HIToolbarRef*) &m_macToolbar ); - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { - InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler, + InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macToolbar ), ToolbarDelegateHandler, GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL ); HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault; @@ -874,8 +915,8 @@ bool wxToolBar::Create( else mode = kHIToolbarDisplayModeIconOnly; - HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode ); - HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize ); + HIToolbarSetDisplayMode( (HIToolbarRef) m_macToolbar, mode ); + HIToolbarSetDisplaySize( (HIToolbarRef) m_macToolbar, displaySize ); } } #endif // wxOSX_USE_NATIVE_TOOLBAR @@ -886,13 +927,13 @@ bool wxToolBar::Create( wxToolBar::~wxToolBar() { #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { // if this is the installed toolbar, then deinstall it if (m_macUsesNativeToolbar) MacInstallNativeToolbar( false ); - CFIndex count = CFGetRetainCount( m_macHIToolbarRef ) ; + CFIndex count = CFGetRetainCount( m_macToolbar ) ; // Leopard seems to have one refcount more, so we cannot check reliably at the moment if ( UMAGetSystemVersion() < 0x1050 ) { @@ -901,8 +942,8 @@ wxToolBar::~wxToolBar() wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor"); } } - CFRelease( (HIToolbarRef)m_macHIToolbarRef ); - m_macHIToolbarRef = NULL; + CFRelease( (HIToolbarRef)m_macToolbar ); + m_macToolbar = NULL; } #endif } @@ -967,7 +1008,7 @@ void wxToolBar::DoGetSize( int *width, int *height ) const if ( ownToolbarInstalled ) { // TODO: is this really a control ? - GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR ); + GetControlBounds( (ControlRef) m_macToolbar, &boundsR ); if ( width != NULL ) *width = boundsR.right - boundsR.left; if ( height != NULL ) @@ -995,7 +1036,7 @@ void wxToolBar::SetWindowStyleFlag( long style ) wxToolBarBase::SetWindowStyleFlag( style ); #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault; @@ -1006,7 +1047,7 @@ void wxToolBar::SetWindowStyleFlag( long style ) else mode = kHIToolbarDisplayModeIconOnly; - HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode ); + HIToolbarSetDisplayMode( (HIToolbarRef) m_macToolbar, mode ); } #endif } @@ -1031,7 +1072,7 @@ bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const OSStatus err = GetWindowToolbar( tlw, &curToolbarRef ); bResultV = ((err == noErr) && (curToolbarRef != NULL)); if (bResultV && (ownToolbarInstalled != NULL)) - *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef); + *ownToolbarInstalled = (curToolbarRef == m_macToolbar); } return bResultV; @@ -1041,7 +1082,7 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) { bool bResult = false; - if (usesNative && (m_macHIToolbarRef == NULL)) + if (usesNative && (m_macToolbar == NULL)) return bResult; if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0)) @@ -1066,11 +1107,19 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) { bResult = true; - SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef ); + 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 ); SetSize( wxSIZE_AUTO_WIDTH, 0 ); m_peer->SetVisibility( false ); @@ -1080,7 +1129,7 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) else { // only deinstall toolbar if this is the installed one - if (m_macHIToolbarRef == curToolbarRef) + if (m_macToolbar == curToolbarRef) { bResult = true; @@ -1104,6 +1153,8 @@ bool wxToolBar::Realize() { if (m_tools.GetCount() == 0) return false; + + wxSize tlw_sz = GetParent()->GetSize(); int maxWidth = 0; int maxHeight = 0; @@ -1143,7 +1194,7 @@ bool wxToolBar::Realize() CFIndex currentPosition = 0; bool insertAll = false; - HIToolbarRef refTB = (HIToolbarRef)m_macHIToolbarRef; + HIToolbarRef refTB = (HIToolbarRef)m_macToolbar; wxFont f; wxFontEncoding enc; f = GetFont(); @@ -1197,10 +1248,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; @@ -1316,6 +1363,9 @@ bool wxToolBar::Realize() node = node->GetNext(); } + if (m_macUsesNativeToolbar) + GetParent()->SetSize( tlw_sz ); + if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) { // if not set yet, only one row @@ -1370,7 +1420,7 @@ void wxToolBar::SetToolBitmapSize(const wxSize& size) m_defaultHeight = size.y + kwxMacToolBorder; #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { int maxs = wxMax( size.x, size.y ); HIToolbarDisplaySize sizeSpec; @@ -1381,7 +1431,7 @@ void wxToolBar::SetToolBitmapSize(const wxSize& size) else sizeSpec = kHIToolbarDisplaySizeSmall; - HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec ); + HIToolbarSetDisplaySize( (HIToolbarRef) m_macToolbar, sizeSpec ); } #endif } @@ -1414,7 +1464,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.")); @@ -1428,7 +1478,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.")); @@ -1456,7 +1506,7 @@ wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const node = node->GetNext(); } - return (wxToolBarToolBase*)NULL; + return NULL; } wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) @@ -1468,22 +1518,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) { - 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; @@ -1495,7 +1542,7 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) #if wxOSX_USE_NATIVE_TOOLBAR wxString label = tool->GetLabel(); - if (m_macHIToolbarRef && !label.empty() ) + if (m_macToolbar && !label.empty() ) { // strip mnemonics from the label for compatibility // with the usual labels in wxStaticText sense @@ -1517,7 +1564,7 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) // in flat style we need a visual separator #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { HIToolbarItemRef item; err = HIToolbarItemCreate( @@ -1561,7 +1608,7 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) } #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { HIToolbarItemRef item; wxString labelStr = wxString::Format(wxT("%p"), tool); @@ -1606,13 +1653,13 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) case wxTOOL_STYLE_CONTROL: #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { wxCHECK_MSG( tool->GetControl(), false, _T("control must be non-NULL") ); HIToolbarItemRef item; HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ; CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ; - err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID, + err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macToolbar,kControlToolbarItemClassID, data , &item ) ; if (err == noErr) @@ -1662,12 +1709,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() ) { @@ -1688,11 +1735,11 @@ bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) #endif #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { - if ( removeIndex != -1 && m_macHIToolbarRef ) + if ( removeIndex != -1 && m_macToolbar ) { - HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex ); + HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macToolbar, removeIndex ); tool->SetIndex( -1 ); } } @@ -1715,7 +1762,7 @@ bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) tool2->SetPosition( pt ); #if wxOSX_USE_NATIVE_TOOLBAR - if (m_macHIToolbarRef != NULL) + if (m_macToolbar != NULL) { if ( removeIndex != -1 && tool2->GetIndex() > removeIndex ) tool2->SetIndex( tool2->GetIndex() - 1 );