X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9a038ddca4ae6ddff438695760d94d6fa935bfd4..a51e79e87db3512823258d487849bb7e7947b700:/src/osx/cocoa/menu.mm?ds=sidebyside diff --git a/src/osx/cocoa/menu.mm b/src/osx/cocoa/menu.mm index 34cd2079e2..760f159263 100644 --- a/src/osx/cocoa/menu.mm +++ b/src/osx/cocoa/menu.mm @@ -4,7 +4,7 @@ // Author: Stefan Csomor // Modified by: // Created: 1998-01-01 -// RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $ +// RCS-ID: $Id$ // Copyright: (c) Stefan Csomor // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -18,16 +18,16 @@ #include "wx/wxprec.h" -#include "wx/menu.h" - #ifndef WX_PRECOMP - #include "wx/log.h" - #include "wx/app.h" - #include "wx/utils.h" - #include "wx/frame.h" - #include "wx/menuitem.h" +#include "wx/log.h" +#include "wx/app.h" +#include "wx/utils.h" +#include "wx/frame.h" +#include "wx/menuitem.h" #endif +#include "wx/menu.h" + #include "wx/osx/private.h" // other standard headers @@ -36,9 +36,9 @@ @implementation wxNSMenu -- (id) init +- (id) initWithTitle:(NSString*) title { - [super init]; + self = [super initWithTitle:title]; impl = NULL; return self; } @@ -55,12 +55,20 @@ @end -@interface wxNSMenuController : NSObject +// this is more compatible, as it is also called for command-key shortcuts +// and under 10.4, we are not getting a 'close' event however... +#define wxOSX_USE_NEEDSUPDATE_HOOK 1 + +@interface wxNSMenuController : NSObject wxOSX_10_6_AND_LATER() { } +#if wxOSX_USE_NEEDSUPDATE_HOOK +- (void)menuNeedsUpdate:(NSMenu*)smenu; +#else - (void)menuWillOpen:(NSMenu *)menu; - (void)menuDidClose:(NSMenu *)menu; +#endif - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item; @end @@ -69,10 +77,23 @@ - (id) init { - [super init]; + self = [super init]; return self; } +#if wxOSX_USE_NEEDSUPDATE_HOOK +- (void)menuNeedsUpdate:(NSMenu*)smenu +{ + wxNSMenu* menu = (wxNSMenu*) smenu; + wxMenuImpl* menuimpl = [menu implementation]; + if ( menuimpl ) + { + wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer(); + if ( wxpeer ) + wxpeer->HandleMenuOpened(); + } +} +#else - (void)menuWillOpen:(NSMenu *)smenu { wxNSMenu* menu = (wxNSMenu*) smenu; @@ -96,6 +117,7 @@ wxpeer->HandleMenuClosed(); } } +#endif - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item { @@ -117,11 +139,11 @@ @end -@interface NSApplication(MissingAppleMenuCall) -- (void)setAppleMenu:(NSMenu *)menu; -@end +@interface NSApplication(MissingAppleMenuCall) +- (void)setAppleMenu:(NSMenu *)menu; +@end -class wxMenuCocoaImpl : public wxMenuImpl +class wxMenuCocoaImpl : public wxMenuImpl { public : wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu) @@ -138,22 +160,37 @@ public : CFRetain(m_osxMenu); [m_osxMenu release]; } - + virtual ~wxMenuCocoaImpl(); - - virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos) + + virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos) { + NSMenuItem* nsmenuitem = (NSMenuItem*) pItem->GetPeer()->GetHMenuItem(); + // make sure a call of SetSubMenu is also reflected (occurring after Create) + // update the native menu item accordingly + + if ( pItem->IsSubMenu() ) + { + wxMenu* wxsubmenu = pItem->GetSubMenu(); + WXHMENU nssubmenu = wxsubmenu->GetHMenu(); + if ( [nsmenuitem submenu] != nssubmenu ) + { + wxsubmenu->GetPeer()->SetTitle( pItem->GetItemLabelText() ); + [nsmenuitem setSubmenu:nssubmenu]; + } + } + if ( pos == (size_t) -1 ) - [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ]; + [m_osxMenu addItem:nsmenuitem ]; else - [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos]; + [m_osxMenu insertItem:nsmenuitem atIndex:pos]; } - - virtual void Remove( wxMenuItem *pItem ) + + virtual void Remove( wxMenuItem *pItem ) { [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()]; } - + virtual void MakeRoot() { [NSApp setMainMenu:m_osxMenu]; @@ -163,7 +200,7 @@ public : virtual void Enable( bool WXUNUSED(enable) ) { } - + virtual void SetTitle( const wxString& text ) { wxCFStringRef cfText(text);