1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/menu.mm
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
19 #include "wx/wxprec.h"
26 #include "wx/menuitem.h"
31 #include "wx/osx/private.h"
33 // other standard headers
34 // ----------------------
37 @implementation wxNSMenu
39 - (id) initWithTitle:(NSString*) title
41 self = [super initWithTitle:title];
46 - (void)setImplementation: (wxMenuImpl *) theImplementation
48 impl = theImplementation;
51 - (wxMenuImpl*) implementation
58 // this is more compatible, as it is also called for command-key shortcuts
59 // and under 10.4, we are not getting a 'close' event however...
60 #define wxOSX_USE_NEEDSUPDATE_HOOK 1
62 @interface wxNSMenuController : NSObject wxOSX_10_6_AND_LATER(<NSMenuDelegate>)
66 #if wxOSX_USE_NEEDSUPDATE_HOOK
67 - (void)menuNeedsUpdate:(NSMenu*)smenu;
69 - (void)menuWillOpen:(NSMenu *)menu;
70 - (void)menuDidClose:(NSMenu *)menu;
72 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
76 @implementation wxNSMenuController
84 #if wxOSX_USE_NEEDSUPDATE_HOOK
85 - (void)menuNeedsUpdate:(NSMenu*)smenu
87 wxNSMenu* menu = (wxNSMenu*) smenu;
88 wxMenuImpl* menuimpl = [menu implementation];
91 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
93 wxpeer->HandleMenuOpened();
97 - (void)menuWillOpen:(NSMenu *)smenu
99 wxNSMenu* menu = (wxNSMenu*) smenu;
100 wxMenuImpl* menuimpl = [menu implementation];
103 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
105 wxpeer->HandleMenuOpened();
109 - (void)menuDidClose:(NSMenu *)smenu
111 wxNSMenu* menu = (wxNSMenu*) smenu;
112 wxMenuImpl* menuimpl = [menu implementation];
115 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
117 wxpeer->HandleMenuClosed();
122 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
124 wxNSMenu* menu = (wxNSMenu*) smenu;
125 wxMenuImpl* menuimpl = [menu implementation];
128 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
129 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
131 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
132 if ( wxpeer && menuitemimpl )
134 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
142 @interface NSApplication(MissingAppleMenuCall)
143 - (void)setAppleMenu:(NSMenu *)menu;
146 class wxMenuCocoaImpl : public wxMenuImpl
149 wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
151 static wxNSMenuController* controller = NULL;
152 if ( controller == NULL )
154 controller = [[wxNSMenuController alloc] init];
156 [menu setDelegate:controller];
157 [m_osxMenu setImplementation:this];
158 [menu setAutoenablesItems:NO];
165 virtual ~wxMenuCocoaImpl();
167 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
169 NSMenuItem* nsmenuitem = (NSMenuItem*) pItem->GetPeer()->GetHMenuItem();
170 // make sure a call of SetSubMenu is also reflected (occuring after Create)
171 // update the native menu item accordingly
173 if ( pItem->IsSubMenu() )
175 wxMenu* wxsubmenu = pItem->GetSubMenu();
176 WXHMENU nssubmenu = wxsubmenu->GetHMenu();
177 if ( [nsmenuitem submenu] != nssubmenu )
179 wxsubmenu->GetPeer()->SetTitle( pItem->GetItemLabelText() );
180 [nsmenuitem setSubmenu:nssubmenu];
184 if ( pos == (size_t) -1 )
185 [m_osxMenu addItem:nsmenuitem ];
187 [m_osxMenu insertItem:nsmenuitem atIndex:pos];
190 virtual void Remove( wxMenuItem *pItem )
192 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
195 virtual void MakeRoot()
197 [NSApp setMainMenu:m_osxMenu];
198 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
201 virtual void Enable( bool WXUNUSED(enable) )
205 virtual void SetTitle( const wxString& text )
207 wxCFStringRef cfText(text);
208 [m_osxMenu setTitle:cfText.AsNSString()];
211 virtual void PopUp( wxWindow *win, int x, int y )
213 win->ScreenToClient( &x , &y ) ;
214 NSView *view = win->GetPeer()->GetWXWidget();
215 NSRect frame = [view frame];
218 frame.size.width = 1;
219 frame.size.height = 1;
220 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
221 [popUpButtonCell setAutoenablesItems:NO];
222 [popUpButtonCell setAltersStateOfSelectedItem:NO];
223 [popUpButtonCell setMenu:m_osxMenu];
224 [popUpButtonCell selectItem:nil];
225 [popUpButtonCell performClickWithFrame:frame inView:view];
226 [popUpButtonCell release];
229 WXHMENU GetHMenu() { return m_osxMenu; }
231 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
232 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
237 wxMenuCocoaImpl::~wxMenuCocoaImpl()
239 [m_osxMenu setDelegate:nil];
240 [m_osxMenu setImplementation:nil];
243 CFRelease(m_osxMenu);
246 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
248 wxCFStringRef cfText( title );
249 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
250 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );