]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/menu.mm
Add missing WXK constants for the control keys
[wxWidgets.git] / src / osx / cocoa / menu.mm
CommitLineData
dbeddfb9
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/menu.mm
3// Purpose: wxMenu, wxMenuBar, wxMenuItem
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
a9a4f229 7// RCS-ID: $Id$
dbeddfb9
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
16// wxWidgets headers
17// -----------------
18
19#include "wx/wxprec.h"
20
dbeddfb9 21#ifndef WX_PRECOMP
5f4bed8a
SC
22#include "wx/log.h"
23#include "wx/app.h"
24#include "wx/utils.h"
25#include "wx/frame.h"
26#include "wx/menuitem.h"
dbeddfb9
SC
27#endif
28
5f4bed8a
SC
29#include "wx/menu.h"
30
dbeddfb9
SC
31#include "wx/osx/private.h"
32
33// other standard headers
34// ----------------------
35#include <string.h>
36
dbeddfb9
SC
37@implementation wxNSMenu
38
fbbe829a 39- (id) initWithTitle:(NSString*) title
dbeddfb9 40{
a985b2c8 41 self = [super initWithTitle:title];
be136f07 42 impl = NULL;
dbeddfb9
SC
43 return self;
44}
45
46- (void)setImplementation: (wxMenuImpl *) theImplementation
47{
48 impl = theImplementation;
49}
50
51- (wxMenuImpl*) implementation
52{
53 return impl;
54}
55
56@end
57
5f4bed8a
SC
58// this is more compatible, as it is also called for command-key shortcuts
59// and under 10.4, we are not getting a 'close' event however...
60#define wxOSX_USE_NEEDSUPDATE_HOOK 1
61
030495ec 62@interface wxNSMenuController : NSObject wxOSX_10_6_AND_LATER(<NSMenuDelegate>)
dbeddfb9
SC
63{
64}
65
5f4bed8a
SC
66#if wxOSX_USE_NEEDSUPDATE_HOOK
67- (void)menuNeedsUpdate:(NSMenu*)smenu;
68#else
dbeddfb9
SC
69- (void)menuWillOpen:(NSMenu *)menu;
70- (void)menuDidClose:(NSMenu *)menu;
5f4bed8a 71#endif
dbeddfb9
SC
72- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
73
74@end
75
76@implementation wxNSMenuController
77
78- (id) init
79{
a985b2c8 80 self = [super init];
dbeddfb9
SC
81 return self;
82}
83
5f4bed8a
SC
84#if wxOSX_USE_NEEDSUPDATE_HOOK
85- (void)menuNeedsUpdate:(NSMenu*)smenu
86{
87 wxNSMenu* menu = (wxNSMenu*) smenu;
88 wxMenuImpl* menuimpl = [menu implementation];
89 if ( menuimpl )
90 {
91 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
92 if ( wxpeer )
93 wxpeer->HandleMenuOpened();
94 }
95}
96#else
dbeddfb9
SC
97- (void)menuWillOpen:(NSMenu *)smenu
98{
99 wxNSMenu* menu = (wxNSMenu*) smenu;
100 wxMenuImpl* menuimpl = [menu implementation];
101 if ( menuimpl )
102 {
103 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
3fe48198
SC
104 if ( wxpeer )
105 wxpeer->HandleMenuOpened();
dbeddfb9
SC
106 }
107}
108
109- (void)menuDidClose:(NSMenu *)smenu
110{
111 wxNSMenu* menu = (wxNSMenu*) smenu;
112 wxMenuImpl* menuimpl = [menu implementation];
113 if ( menuimpl )
114 {
115 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
3fe48198
SC
116 if ( wxpeer )
117 wxpeer->HandleMenuClosed();
dbeddfb9
SC
118 }
119}
5f4bed8a 120#endif
dbeddfb9
SC
121
122- (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
123{
124 wxNSMenu* menu = (wxNSMenu*) smenu;
125 wxMenuImpl* menuimpl = [menu implementation];
126 if ( menuimpl )
127 {
128 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
129 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
130 {
131 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
132 if ( wxpeer && menuitemimpl )
133 {
134 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
135 }
136 }
137 }
138}
139
140@end
141
03647350
VZ
142@interface NSApplication(MissingAppleMenuCall)
143- (void)setAppleMenu:(NSMenu *)menu;
144@end
ffad7b0d 145
03647350 146class wxMenuCocoaImpl : public wxMenuImpl
dbeddfb9
SC
147{
148public :
be136f07 149 wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
dbeddfb9 150 {
be136f07
SC
151 static wxNSMenuController* controller = NULL;
152 if ( controller == NULL )
153 {
154 controller = [[wxNSMenuController alloc] init];
155 }
156 [menu setDelegate:controller];
157 [m_osxMenu setImplementation:this];
9a038ddc
SC
158 // gc aware
159 if ( m_osxMenu )
160 CFRetain(m_osxMenu);
161 [m_osxMenu release];
dbeddfb9 162 }
03647350 163
dbeddfb9 164 virtual ~wxMenuCocoaImpl();
03647350
VZ
165
166 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
dbeddfb9 167 {
b7a015cb 168 NSMenuItem* nsmenuitem = (NSMenuItem*) pItem->GetPeer()->GetHMenuItem();
58b1ae5e 169 // make sure a call of SetSubMenu is also reflected (occurring after Create)
b7a015cb
SC
170 // update the native menu item accordingly
171
172 if ( pItem->IsSubMenu() )
173 {
174 wxMenu* wxsubmenu = pItem->GetSubMenu();
175 WXHMENU nssubmenu = wxsubmenu->GetHMenu();
176 if ( [nsmenuitem submenu] != nssubmenu )
177 {
178 wxsubmenu->GetPeer()->SetTitle( pItem->GetItemLabelText() );
179 [nsmenuitem setSubmenu:nssubmenu];
180 }
181 }
182
dbeddfb9 183 if ( pos == (size_t) -1 )
b7a015cb 184 [m_osxMenu addItem:nsmenuitem ];
dbeddfb9 185 else
b7a015cb 186 [m_osxMenu insertItem:nsmenuitem atIndex:pos];
dbeddfb9 187 }
03647350
VZ
188
189 virtual void Remove( wxMenuItem *pItem )
dbeddfb9
SC
190 {
191 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
192 }
03647350 193
dbeddfb9
SC
194 virtual void MakeRoot()
195 {
196 [NSApp setMainMenu:m_osxMenu];
197 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
198 }
199
d8207702 200 virtual void Enable( bool WXUNUSED(enable) )
dbeddfb9
SC
201 {
202 }
03647350 203
dbeddfb9
SC
204 virtual void SetTitle( const wxString& text )
205 {
206 wxCFStringRef cfText(text);
207 [m_osxMenu setTitle:cfText.AsNSString()];
208 }
209
2cb5d2d2
SC
210 virtual void PopUp( wxWindow *win, int x, int y )
211 {
212 win->ScreenToClient( &x , &y ) ;
213 NSView *view = win->GetPeer()->GetWXWidget();
214 NSRect frame = [view frame];
215 frame.origin.x = x;
216 frame.origin.y = y;
217 frame.size.width = 1;
218 frame.size.height = 1;
219 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
220 [popUpButtonCell setAutoenablesItems:NO];
221 [popUpButtonCell setAltersStateOfSelectedItem:NO];
222 [popUpButtonCell setMenu:m_osxMenu];
223 [popUpButtonCell selectItem:nil];
224 [popUpButtonCell performClickWithFrame:frame inView:view];
225 [popUpButtonCell release];
226 }
227
dbeddfb9
SC
228 WXHMENU GetHMenu() { return m_osxMenu; }
229
230 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
231 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
232protected :
be136f07 233 wxNSMenu* m_osxMenu;
dbeddfb9
SC
234} ;
235
236wxMenuCocoaImpl::~wxMenuCocoaImpl()
237{
42c2b729 238 [m_osxMenu setDelegate:nil];
be136f07 239 [m_osxMenu setImplementation:nil];
9a038ddc
SC
240 // gc aware
241 if ( m_osxMenu )
242 CFRelease(m_osxMenu);
dbeddfb9
SC
243}
244
245wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
246{
dbeddfb9
SC
247 wxCFStringRef cfText( title );
248 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
249 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
dbeddfb9
SC
250 return c;
251}