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();
94 wxpeer->HandleMenuOpened();
98 - (void)menuDidClose:(NSMenu *)smenu
100 wxNSMenu* menu = (wxNSMenu*) smenu;
101 wxMenuImpl* menuimpl = [menu implementation];
104 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
105 wxpeer->HandleMenuClosed();
109 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
111 wxNSMenu* menu = (wxNSMenu*) smenu;
112 wxMenuImpl* menuimpl = [menu implementation];
115 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
116 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
118 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
119 if ( wxpeer && menuitemimpl )
121 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
129 class wxMenuCocoaImpl : public wxMenuImpl
132 wxMenuCocoaImpl( wxMenu* peer , NSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
136 virtual ~wxMenuCocoaImpl();
138 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
140 if ( pos == (size_t) -1 )
141 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
143 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
146 virtual void Remove( wxMenuItem *pItem )
148 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
151 virtual void MakeRoot()
153 [NSApp setMainMenu:m_osxMenu];
154 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
157 virtual void Enable( bool enable )
161 virtual void SetTitle( const wxString& text )
163 wxCFStringRef cfText(text);
164 [m_osxMenu setTitle:cfText.AsNSString()];
167 WXHMENU GetHMenu() { return m_osxMenu; }
169 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
170 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
175 wxMenuCocoaImpl::~wxMenuCocoaImpl()
180 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
182 static wxNSMenuController* controller = NULL;
183 if ( controller == NULL )
185 controller = [[wxNSMenuController alloc] init];
187 wxCFStringRef cfText( title );
188 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
189 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
190 [menu setDelegate:controller];
191 [menu setImplementation:c];