X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5bf565977f9ec437b29ac6fd8ab77fce365f55f2..bd235295fb3b35fdb29326084b87893cf5432eb5:/src/osx/cocoa/toolbar.mm diff --git a/src/osx/cocoa/toolbar.mm b/src/osx/cocoa/toolbar.mm index 1b8b4866fe..0add59ad00 100644 --- a/src/osx/cocoa/toolbar.mm +++ b/src/osx/cocoa/toolbar.mm @@ -39,10 +39,6 @@ END_EVENT_TABLE() #pragma mark - #pragma mark Tool Implementation -#if wxOSX_USE_COCOA -#define wxOSX_USE_NATIVE_TOOLBAR 1 -#endif - // ---------------------------------------------------------------------------- // private classes // ---------------------------------------------------------------------------- @@ -140,7 +136,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; @@ -157,11 +153,12 @@ public: bool DoEnable( bool enable ); void UpdateImages(); - + void UpdateToggleImage( bool toggle ); - + void UpdateLabel() { +#if wxOSX_USE_NATIVE_TOOLBAR if ( m_toolbarItem ) { // strip mnemonics from the label for compatibility with the usual @@ -170,10 +167,11 @@ public: wxCFStringRef l(labelStr, GetToolBarFontEncoding()); [m_toolbarItem setLabel:l.AsNSString()]; - + wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() ); [m_toolbarItem setToolTip:sh.AsNSString()]; } +#endif } void Action() @@ -189,7 +187,7 @@ public: tbar->OnLeftClick( GetId(), IsToggled() ); } - + #if wxOSX_USE_NATIVE_TOOLBAR void SetToolbarItemRef( NSToolbarItem* ref ) { @@ -256,6 +254,8 @@ private: #endif }; +#if wxOSX_USE_NATIVE_TOOLBAR + @interface wxNSToolbarItem : NSToolbarItem { wxToolBarTool* impl; @@ -269,12 +269,12 @@ private: @end -@interface wxNSToolbarDelegate : NSObject +@interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER() { } - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag; - + - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar; - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar; @@ -284,6 +284,8 @@ private: @end +#endif + @interface wxNSToolBarButton : NSButton { @@ -298,6 +300,8 @@ private: @end +#if wxOSX_USE_NATIVE_TOOLBAR + @implementation wxNSToolbarItem - (id)initWithItemIdentifier: (NSString*) identifier @@ -311,6 +315,7 @@ private: - (void) clickedAction: (id) sender { + wxUnusedVar(sender); if ( impl ) { impl->Action(); @@ -333,21 +338,25 @@ private: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar { + wxUnusedVar(toolbar); return nil; } - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar { + wxUnusedVar(toolbar); return nil; } - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar { + wxUnusedVar(toolbar); return nil; } - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag { + wxUnusedVar(toolbar); #ifdef __LP64__ wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue]; #else @@ -374,6 +383,8 @@ private: @end +#endif + @implementation wxNSToolBarButton - (id)initWithFrame:(NSRect)frame @@ -387,6 +398,7 @@ private: - (void) clickedAction: (id) sender { + wxUnusedVar(sender); if ( impl ) { impl->Action(); @@ -467,7 +479,7 @@ void wxToolBarTool::SetPosition( const wxPoint& position ) frame.origin.y = mac_y; [m_controlHandle setFrame:frame]; } - } + } } void wxToolBarTool::UpdateImages() @@ -487,7 +499,7 @@ void wxToolBarTool::UpdateImages() dc.DrawRectangle( 0, 0, w, h ); dc.DrawBitmap( m_bmpNormal, 0, 0, true ); dc.SelectObject( wxNullBitmap ); - + [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()]; } UpdateToggleImage( CanBeToggled() && IsToggled() ); @@ -586,19 +598,19 @@ bool wxToolBar::Create( if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1) { static wxNSToolbarDelegate* controller = nil; - + if ( controller == nil ) controller = [[wxNSToolbarDelegate alloc] init]; wxString identifier = wxString::Format( wxT("%p"), this ); wxCFStringRef cfidentifier(identifier); NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()]; - + m_macToolbar = tb ; - + if (m_macToolbar != NULL) { [tb setDelegate:controller]; - + NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault; NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall; @@ -619,18 +631,10 @@ 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 ); - - [(NSToolbar*)m_macToolbar release]; - m_macToolbar = NULL; - } -#endif +{ + [(NSToolbar*)m_macToolbar setDelegate:nil]; + [(NSToolbar*)m_macToolbar release]; + m_macToolbar = NULL; } bool wxToolBar::Show( bool show ) @@ -692,8 +696,8 @@ void wxToolBar::DoGetSize( int *width, int *height ) const { WXWindow tlw = MacGetTopLevelWindowRef(); float toolbarHeight = 0.0; - NSRect windowFrame; - + NSRect windowFrame = NSMakeRect(0, 0, 0, 0); + if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible]) { windowFrame = [NSWindow contentRectForFrameRect:[tlw frame] @@ -701,11 +705,11 @@ void wxToolBar::DoGetSize( int *width, int *height ) const toolbarHeight = NSHeight(windowFrame) - NSHeight([[tlw contentView] frame]); } - + if ( width != NULL ) - *width = windowFrame.size.width; + *width = (int)windowFrame.size.width; if ( height != NULL ) - *height = toolbarHeight; + *height = (int)toolbarHeight; } else wxToolBarBase::DoGetSize( width, height ); @@ -777,7 +781,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; WXWindow tlw = MacGetTopLevelWindowRef(); @@ -797,7 +801,7 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) bResult = true; [tlw setToolbar:(NSToolbar*) m_macToolbar]; [(NSToolbar*) m_macToolbar setVisible:YES]; - + m_peer->Move(0,0,0,0 ); SetSize( wxSIZE_AUTO_WIDTH, 0 ); m_peer->SetVisibility( false ); @@ -811,7 +815,7 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) { bResult = true; [(NSToolbar*) m_macToolbar setVisible:NO]; - [tlw setToolbar:nil]; + MacUninstallNativeToolbar(); m_peer->SetVisibility( true ); } } @@ -822,11 +826,21 @@ 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; + + WXWindow tlw = MacGetTopLevelWindowRef(); + if (tlw) + [tlw setToolbar:nil]; +} #endif bool wxToolBar::Realize() { - if (m_tools.GetCount() == 0) + if ( !wxToolBarBase::Realize() ) return false; int maxWidth = 0; @@ -895,7 +909,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) ); @@ -907,7 +921,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; @@ -947,10 +961,21 @@ bool wxToolBar::Realize() } } } - wxString identifier = wxString::Format( wxT("%ld"), (long) tool ); - wxCFStringRef cfidentifier(identifier); - [refTB insertItemWithItemIdentifier:cfidentifier.AsNSString() atIndex:currentPosition]; + wxCFStringRef cfidentifier; + const NSString *nsItemId; + if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR) + { + nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier + : NSToolbarSeparatorItemIdentifier; + } + else + { + cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool)); + nsItemId = cfidentifier.AsNSString(); + } + + [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition]; tool->SetIndex( currentPosition ); } @@ -1000,14 +1025,14 @@ bool wxToolBar::Realize() node = node->GetNext(); } - if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) + 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 = th; maxHeight += m_yMargin + kwxMacToolBarTopMargin; m_minHeight = m_maxHeight = maxHeight; } @@ -1018,7 +1043,7 @@ bool wxToolBar::Realize() SetRows( GetToolsCount() ); m_minHeight = maxHeight; - maxHeight = th; + // maxWidth = tw; maxWidth += m_xMargin + kwxMacToolBarLeftMargin; m_minWidth = m_maxWidth = maxWidth; } @@ -1029,7 +1054,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) @@ -1098,7 +1123,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.")); @@ -1112,7 +1137,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.")); @@ -1140,7 +1165,7 @@ wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const node = node->GetNext(); } - return (wxToolBarToolBase*)NULL; + return NULL; } wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) @@ -1167,7 +1192,7 @@ void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) 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; @@ -1192,7 +1217,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.size.height = toolSize.y; else toolrect.size.width = toolSize.x; @@ -1201,7 +1226,10 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) #if wxOSX_USE_NATIVE_TOOLBAR if (m_macToolbar != NULL) { - NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarSeparatorItemIdentifier]; + const NSString * const + nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier + : NSToolbarSeparatorItemIdentifier; + NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId]; tool->SetToolbarItemRef( item ); } #endif // wxOSX_USE_NATIVE_TOOLBAR @@ -1223,9 +1251,9 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) [v setBordered:NO]; [v setButtonType: ( tool->CanBeToggled() ? NSOnOffButton : NSMomentaryPushInButton )]; [v setImplementation:tool]; - + controlHandle = v; - + #if wxOSX_USE_NATIVE_TOOLBAR if (m_macToolbar != NULL) { @@ -1258,7 +1286,7 @@ bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) if (m_macToolbar != NULL) { WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ; - wxCHECK_MSG( view, false, _T("control must be non-NULL") ); + wxCHECK_MSG( view, false, wxT("control must be non-NULL") ); wxString identifier = wxString::Format(wxT("%ld"), (long) tool); wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() ); @@ -1299,7 +1327,7 @@ void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(tog 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() ) { @@ -1339,7 +1367,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;