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 <NSMenuDelegate>
62 - (void)menuWillOpen:(NSMenu *)menu;
63 - (void)menuDidClose:(NSMenu *)menu;
64 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
68 @implementation wxNSMenuController
76 - (void)menuWillOpen:(NSMenu *)smenu
78 wxNSMenu* menu = (wxNSMenu*) smenu;
79 wxMenuImpl* menuimpl = [menu implementation];
82 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
84 wxpeer->HandleMenuOpened();
88 - (void)menuDidClose:(NSMenu *)smenu
90 wxNSMenu* menu = (wxNSMenu*) smenu;
91 wxMenuImpl* menuimpl = [menu implementation];
94 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
96 wxpeer->HandleMenuClosed();
100 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
102 wxNSMenu* menu = (wxNSMenu*) smenu;
103 wxMenuImpl* menuimpl = [menu implementation];
106 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
107 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
109 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
110 if ( wxpeer && menuitemimpl )
112 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
120 @interface NSApplication(MissingAppleMenuCall)
121 - (void)setAppleMenu:(NSMenu *)menu;
124 class wxMenuCocoaImpl : public wxMenuImpl
127 wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
129 static wxNSMenuController* controller = NULL;
130 if ( controller == NULL )
132 controller = [[wxNSMenuController alloc] init];
134 [menu setDelegate:controller];
135 [m_osxMenu setImplementation:this];
142 virtual ~wxMenuCocoaImpl();
144 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
146 if ( pos == (size_t) -1 )
147 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
149 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
152 virtual void Remove( wxMenuItem *pItem )
154 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
157 virtual void MakeRoot()
159 [NSApp setMainMenu:m_osxMenu];
160 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
163 virtual void Enable( bool WXUNUSED(enable) )
167 virtual void SetTitle( const wxString& text )
169 wxCFStringRef cfText(text);
170 [m_osxMenu setTitle:cfText.AsNSString()];
173 virtual void PopUp( wxWindow *win, int x, int y )
175 win->ScreenToClient( &x , &y ) ;
176 NSView *view = win->GetPeer()->GetWXWidget();
177 NSRect frame = [view frame];
180 frame.size.width = 1;
181 frame.size.height = 1;
182 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
183 [popUpButtonCell setAutoenablesItems:NO];
184 [popUpButtonCell setAltersStateOfSelectedItem:NO];
185 [popUpButtonCell setMenu:m_osxMenu];
186 [popUpButtonCell selectItem:nil];
187 [popUpButtonCell performClickWithFrame:frame inView:view];
188 [popUpButtonCell release];
191 WXHMENU GetHMenu() { return m_osxMenu; }
193 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
194 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
199 wxMenuCocoaImpl::~wxMenuCocoaImpl()
201 [m_osxMenu setDelegate:nil];
202 [m_osxMenu setImplementation:nil];
205 CFRelease(m_osxMenu);
208 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
210 wxCFStringRef cfText( title );
211 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
212 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );