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 // ----------------------
37 @implementation wxNSMenu
45 - (void)setImplementation: (wxMenuImpl *) theImplementation
47 impl = theImplementation;
50 - (wxMenuImpl*) implementation
57 @interface wxNSMenuController : NSObject
61 - (void)menuWillOpen:(NSMenu *)menu;
62 - (void)menuDidClose:(NSMenu *)menu;
63 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
67 @implementation wxNSMenuController
75 - (void)menuWillOpen:(NSMenu *)smenu
77 wxNSMenu* menu = (wxNSMenu*) smenu;
78 wxMenuImpl* menuimpl = [menu implementation];
81 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
83 wxpeer->HandleMenuOpened();
87 - (void)menuDidClose:(NSMenu *)smenu
89 wxNSMenu* menu = (wxNSMenu*) smenu;
90 wxMenuImpl* menuimpl = [menu implementation];
93 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
95 wxpeer->HandleMenuClosed();
99 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
101 wxNSMenu* menu = (wxNSMenu*) smenu;
102 wxMenuImpl* menuimpl = [menu implementation];
105 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
106 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
108 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
109 if ( wxpeer && menuitemimpl )
111 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
119 @interface NSApplication(MissingAppleMenuCall)
120 - (void)setAppleMenu:(NSMenu *)menu;
123 class wxMenuCocoaImpl : public wxMenuImpl
126 wxMenuCocoaImpl( wxMenu* peer , NSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
130 virtual ~wxMenuCocoaImpl();
132 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
134 if ( pos == (size_t) -1 )
135 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
137 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
140 virtual void Remove( wxMenuItem *pItem )
142 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
145 virtual void MakeRoot()
147 [NSApp setMainMenu:m_osxMenu];
148 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
151 virtual void Enable( bool enable )
155 virtual void SetTitle( const wxString& text )
157 wxCFStringRef cfText(text);
158 [m_osxMenu setTitle:cfText.AsNSString()];
161 virtual void PopUp( wxWindow *win, int x, int y )
163 win->ScreenToClient( &x , &y ) ;
164 NSView *view = win->GetPeer()->GetWXWidget();
165 NSRect frame = [view frame];
168 frame.size.width = 1;
169 frame.size.height = 1;
170 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
171 [popUpButtonCell setAutoenablesItems:NO];
172 [popUpButtonCell setAltersStateOfSelectedItem:NO];
173 [popUpButtonCell setMenu:m_osxMenu];
174 [popUpButtonCell selectItem:nil];
175 [popUpButtonCell performClickWithFrame:frame inView:view];
176 [popUpButtonCell release];
179 WXHMENU GetHMenu() { return m_osxMenu; }
181 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
182 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
187 wxMenuCocoaImpl::~wxMenuCocoaImpl()
192 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
194 static wxNSMenuController* controller = NULL;
195 if ( controller == NULL )
197 controller = [[wxNSMenuController alloc] init];
199 wxCFStringRef cfText( title );
200 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
201 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
202 [menu setDelegate:controller];
203 [menu setImplementation:c];