X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0f9b48d1e1535f8b92a73031be8cceff39123d27..58110007c76a4b1596f166d7de43dc107c6c7c0c:/src/osx/cocoa/toolbar.mm diff --git a/src/osx/cocoa/toolbar.mm b/src/osx/cocoa/toolbar.mm index 674971e1a1..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,22 +338,30 @@ 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 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue]; +#endif if ( tool ) { wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef(); @@ -370,6 +383,8 @@ private: @end +#endif + @implementation wxNSToolBarButton - (id)initWithFrame:(NSRect)frame @@ -383,6 +398,7 @@ private: - (void) clickedAction: (id) sender { + wxUnusedVar(sender); if ( impl ) { impl->Action(); @@ -463,7 +479,7 @@ void wxToolBarTool::SetPosition( const wxPoint& position ) frame.origin.y = mac_y; [m_controlHandle setFrame:frame]; } - } + } } void wxToolBarTool::UpdateImages() @@ -483,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() ); @@ -582,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; @@ -615,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 ) @@ -688,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] @@ -697,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 ); @@ -773,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(); @@ -793,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 ); @@ -807,7 +815,7 @@ bool wxToolBar::MacInstallNativeToolbar(bool usesNative) { bResult = true; [(NSToolbar*) m_macToolbar setVisible:NO]; - [tlw setToolbar:nil]; + MacUninstallNativeToolbar(); m_peer->SetVisibility( true ); } } @@ -818,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; @@ -891,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) ); @@ -903,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; @@ -943,10 +961,21 @@ bool wxToolBar::Realize() } } } - wxString identifier = wxString::Format( wxT("%d"), (int) 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 ); } @@ -996,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; } @@ -1014,7 +1043,7 @@ bool wxToolBar::Realize() SetRows( GetToolsCount() ); m_minHeight = maxHeight; - maxHeight = th; + // maxWidth = tw; maxWidth += m_xMargin + kwxMacToolBarLeftMargin; m_minWidth = m_maxWidth = maxWidth; } @@ -1025,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) @@ -1094,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.")); @@ -1108,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.")); @@ -1136,7 +1165,7 @@ wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const node = node->GetNext(); } - return (wxToolBarToolBase*)NULL; + return NULL; } wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) @@ -1163,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; @@ -1188,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; @@ -1197,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 @@ -1219,13 +1251,13 @@ 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) { - wxString identifier = wxString::Format(wxT("%d"), (int) tool); + wxString identifier = wxString::Format(wxT("%ld"), (long) tool); wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() ); wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ]; [item setImplementation:tool]; @@ -1254,9 +1286,9 @@ 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("%d"), (int) tool); + wxString identifier = wxString::Format(wxT("%ld"), (long) tool); wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() ); wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ]; [item setImplementation:tool]; @@ -1295,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() ) { @@ -1335,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;