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
46 - (void)setImplementation: (wxMenuImpl *) theImplementation
48 impl = theImplementation;
51 - (wxMenuImpl*) implementation
58 @interface wxNSMenuController : NSObject
59 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
65 - (void)menuWillOpen:(NSMenu *)menu;
66 - (void)menuDidClose:(NSMenu *)menu;
67 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
71 @implementation wxNSMenuController
79 - (void)menuWillOpen:(NSMenu *)smenu
81 wxNSMenu* menu = (wxNSMenu*) smenu;
82 wxMenuImpl* menuimpl = [menu implementation];
85 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
87 wxpeer->HandleMenuOpened();
91 - (void)menuDidClose:(NSMenu *)smenu
93 wxNSMenu* menu = (wxNSMenu*) smenu;
94 wxMenuImpl* menuimpl = [menu implementation];
97 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
99 wxpeer->HandleMenuClosed();
103 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
105 wxNSMenu* menu = (wxNSMenu*) smenu;
106 wxMenuImpl* menuimpl = [menu implementation];
109 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
110 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
112 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
113 if ( wxpeer && menuitemimpl )
115 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
123 @interface NSApplication(MissingAppleMenuCall)
124 - (void)setAppleMenu:(NSMenu *)menu;
127 class wxMenuCocoaImpl : public wxMenuImpl
130 wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
132 static wxNSMenuController* controller = NULL;
133 if ( controller == NULL )
135 controller = [[wxNSMenuController alloc] init];
137 [menu setDelegate:controller];
138 [m_osxMenu setImplementation:this];
145 virtual ~wxMenuCocoaImpl();
147 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
149 if ( pos == (size_t) -1 )
150 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
152 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
155 virtual void Remove( wxMenuItem *pItem )
157 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
160 virtual void MakeRoot()
162 [NSApp setMainMenu:m_osxMenu];
163 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
166 virtual void Enable( bool WXUNUSED(enable) )
170 virtual void SetTitle( const wxString& text )
172 wxCFStringRef cfText(text);
173 [m_osxMenu setTitle:cfText.AsNSString()];
176 virtual void PopUp( wxWindow *win, int x, int y )
178 win->ScreenToClient( &x , &y ) ;
179 NSView *view = win->GetPeer()->GetWXWidget();
180 NSRect frame = [view frame];
183 frame.size.width = 1;
184 frame.size.height = 1;
185 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
186 [popUpButtonCell setAutoenablesItems:NO];
187 [popUpButtonCell setAltersStateOfSelectedItem:NO];
188 [popUpButtonCell setMenu:m_osxMenu];
189 [popUpButtonCell selectItem:nil];
190 [popUpButtonCell performClickWithFrame:frame inView:view];
191 [popUpButtonCell release];
194 WXHMENU GetHMenu() { return m_osxMenu; }
196 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
197 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
202 wxMenuCocoaImpl::~wxMenuCocoaImpl()
204 [m_osxMenu setDelegate:nil];
205 [m_osxMenu setImplementation:nil];
208 CFRelease(m_osxMenu);
211 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
213 wxCFStringRef cfText( title );
214 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
215 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );