1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/menu.mm
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
19 #include "wx/wxprec.h"
28 #include "wx/menuitem.h"
31 #include "wx/osx/private.h"
33 // other standard headers
34 // ----------------------
39 @interface wxNSMenu : NSMenu
44 - (void) setImplementation:(wxMenuImpl*) item;
45 - (wxMenuImpl*) implementation;
49 @implementation wxNSMenu
57 - (void)setImplementation: (wxMenuImpl *) theImplementation
59 impl = theImplementation;
62 - (wxMenuImpl*) implementation
69 @interface wxNSMenuController : NSObject
73 - (void)menuWillOpen:(NSMenu *)menu;
74 - (void)menuDidClose:(NSMenu *)menu;
75 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
79 @implementation wxNSMenuController
87 - (void)menuWillOpen:(NSMenu *)smenu
89 wxNSMenu* menu = (wxNSMenu*) smenu;
90 wxMenuImpl* menuimpl = [menu implementation];
93 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
95 wxpeer->HandleMenuOpened();
99 - (void)menuDidClose:(NSMenu *)smenu
101 wxNSMenu* menu = (wxNSMenu*) smenu;
102 wxMenuImpl* menuimpl = [menu implementation];
105 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
107 wxpeer->HandleMenuClosed();
111 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
113 wxNSMenu* menu = (wxNSMenu*) smenu;
114 wxMenuImpl* menuimpl = [menu implementation];
117 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
118 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
120 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
121 if ( wxpeer && menuitemimpl )
123 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
131 class wxMenuCocoaImpl : public wxMenuImpl
134 wxMenuCocoaImpl( wxMenu* peer , NSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
138 virtual ~wxMenuCocoaImpl();
140 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
142 if ( pos == (size_t) -1 )
143 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
145 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
148 virtual void Remove( wxMenuItem *pItem )
150 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
153 virtual void MakeRoot()
155 [NSApp setMainMenu:m_osxMenu];
156 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
159 virtual void Enable( bool enable )
163 virtual void SetTitle( const wxString& text )
165 wxCFStringRef cfText(text);
166 [m_osxMenu setTitle:cfText.AsNSString()];
169 virtual void PopUp( wxWindow *win, int x, int y )
171 win->ScreenToClient( &x , &y ) ;
172 NSView *view = win->GetPeer()->GetWXWidget();
173 NSRect frame = [view frame];
176 frame.size.width = 1;
177 frame.size.height = 1;
178 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
179 [popUpButtonCell setAutoenablesItems:NO];
180 [popUpButtonCell setAltersStateOfSelectedItem:NO];
181 [popUpButtonCell setMenu:m_osxMenu];
182 [popUpButtonCell selectItem:nil];
183 [popUpButtonCell performClickWithFrame:frame inView:view];
184 [popUpButtonCell release];
187 WXHMENU GetHMenu() { return m_osxMenu; }
189 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
190 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
195 wxMenuCocoaImpl::~wxMenuCocoaImpl()
200 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
202 static wxNSMenuController* controller = NULL;
203 if ( controller == NULL )
205 controller = [[wxNSMenuController alloc] init];
207 wxCFStringRef cfText( title );
208 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
209 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
210 [menu setDelegate:controller];
211 [menu setImplementation:c];