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;
71 - (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();
110 - (void)menuDidClose:(NSMenu *)smenu
112 wxNSMenu* menu = (wxNSMenu*) smenu;
113 wxMenuImpl* menuimpl = [menu implementation];
116 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
118 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];
164 virtual ~wxMenuCocoaImpl();
166 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
168 NSMenuItem* nsmenuitem = (NSMenuItem*) pItem->GetPeer()->GetHMenuItem();
169 // make sure a call of SetSubMenu is also reflected (occurring after Create)
170 // update the native menu item accordingly
172 if ( pItem->IsSubMenu() )
174 wxMenu* wxsubmenu = pItem->GetSubMenu();
175 WXHMENU nssubmenu = wxsubmenu->GetHMenu();
176 if ( [nsmenuitem submenu] != nssubmenu )
178 wxsubmenu->GetPeer()->SetTitle( pItem->GetItemLabelText() );
179 [nsmenuitem setSubmenu:nssubmenu];
183 if ( pos == (size_t) -1 )
184 [m_osxMenu addItem:nsmenuitem ];
186 [m_osxMenu insertItem:nsmenuitem atIndex:pos];
189 virtual void Remove( wxMenuItem *pItem )
191 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
194 virtual void MakeRoot()
196 [NSApp setMainMenu:m_osxMenu];
197 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
200 virtual void Enable( bool WXUNUSED(enable) )
204 virtual void SetTitle( const wxString& text )
206 wxCFStringRef cfText(text);
207 [m_osxMenu setTitle:cfText.AsNSString()];
210 virtual void PopUp( wxWindow *win, int x, int y )
212 win->ScreenToClient( &x , &y ) ;
213 NSView *view = win->GetPeer()->GetWXWidget();
214 NSRect frame = [view frame];
217 frame.size.width = 1;
218 frame.size.height = 1;
219 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
220 [popUpButtonCell setAutoenablesItems:NO];
221 [popUpButtonCell setAltersStateOfSelectedItem:NO];
222 [popUpButtonCell setMenu:m_osxMenu];
223 [popUpButtonCell selectItem:nil];
224 [popUpButtonCell performClickWithFrame:frame inView:view];
225 [popUpButtonCell release];
228 WXHMENU GetHMenu() { return m_osxMenu; }
230 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
231 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
236 wxMenuCocoaImpl::~wxMenuCocoaImpl()
238 [m_osxMenu setDelegate:nil];
239 [m_osxMenu setImplementation:nil];
242 CFRelease(m_osxMenu);
245 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
247 wxCFStringRef cfText( title );
248 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
249 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );