]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/menuitem.mm
making conversion explicit silences deprecation warning
[wxWidgets.git] / src / osx / cocoa / menuitem.mm
CommitLineData
dbeddfb9
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/menuitem.mm
3// Purpose: wxMenuItem implementation
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id: menuitem.cpp 54129 2008-06-11 19:30:52Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/menuitem.h"
15#include "wx/stockitem.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/app.h"
70412bd4 19 #include "wx/log.h"
dbeddfb9
SC
20 #include "wx/menu.h"
21#endif // WX_PRECOMP
22
23#include "wx/osx/private.h"
24
fbbe829a
SC
25// a mapping from wx ids to standard osx actions in order to support the native menu item handling
26// if a new mapping is added, make sure the wxNonOwnedWindowController has a handler for this action as well
27
28struct Mapping
29{
30 int menuid;
31 SEL action;
32};
33
34Mapping sActionToWXMapping[] =
35{
2daf63c4
SC
36 { wxID_UNDO, @selector(undo:) },
37 { wxID_REDO, @selector(redo:) },
38 { wxID_CUT, @selector(cut:) },
39 { wxID_COPY, @selector(copy:) },
40 { wxID_PASTE, @selector(paste:) },
41 { wxID_CLEAR, @selector(delete:) },
42 { wxID_SELECTALL, @selector(selectAll:) },
43 { 0, NULL }
fbbe829a
SC
44};
45
46int wxOSXGetIdFromSelector(SEL action )
47{
48 int i = 0 ;
49 while ( sActionToWXMapping[i].action != nil )
50 {
51 if ( sActionToWXMapping[i].action == action )
52 return sActionToWXMapping[i].menuid;
53 ++i;
54 }
55
56 return 0;
57}
58
59SEL wxOSXGetSelectorFromID(int menuId )
60{
61 int i = 0 ;
62 while ( sActionToWXMapping[i].action != nil )
63 {
64 if ( sActionToWXMapping[i].menuid == menuId )
65 return sActionToWXMapping[i].action;
66 ++i;
67 }
68
69 return nil;
70}
71
72
dbeddfb9
SC
73@implementation wxNSMenuItem
74
fbbe829a 75- (id) initWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)charCode
dbeddfb9 76{
fbbe829a 77 [super initWithTitle:aString action:aSelector keyEquivalent:charCode];
dbeddfb9
SC
78 return self;
79}
80
81- (void) clickedAction: (id) sender
82{
d8207702 83 wxUnusedVar(sender);
dbeddfb9
SC
84 if ( impl )
85 {
c46d0503
SC
86 wxMenuItem* menuitem = impl->GetWXPeer();
87 if ( menuitem->GetMenu()->HandleCommandProcess(menuitem) == false )
88 {
89 }
dbeddfb9
SC
90 }
91}
92
fbbe829a
SC
93- (void) setEnabled:(BOOL) flag
94{
95 [super setEnabled:flag];
96}
97
dbeddfb9
SC
98- (BOOL)validateMenuItem:(NSMenuItem *) menuItem
99{
d8207702 100 wxUnusedVar(menuItem);
dbeddfb9
SC
101 if( impl )
102 {
fbbe829a
SC
103 if ( impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer()) )
104 return impl->GetWXPeer()->IsEnabled();
dbeddfb9
SC
105 }
106 return YES ;
107}
108
109- (void)setImplementation: (wxMenuItemImpl *) theImplementation
110{
111 impl = theImplementation;
112}
113
114- (wxMenuItemImpl*) implementation
115{
116 return impl;
117}
118
119@end
120
121void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
122{
123 unsigned int modifiers = 0 ;
124 int key = entry->GetKeyCode() ;
125 if ( key )
126 {
fa6ff5f1 127 if (entry->GetFlags() & wxACCEL_CTRL)
dbeddfb9
SC
128 modifiers |= NSCommandKeyMask;
129
130 if (entry->GetFlags() & wxACCEL_ALT)
131 modifiers |= NSAlternateKeyMask ;
132
133 // this may be ignored later for alpha chars
134
135 if (entry->GetFlags() & wxACCEL_SHIFT)
136 modifiers |= NSShiftKeyMask ;
137
138 unichar shortcut = 0;
139 if ( key >= WXK_F1 && key <= WXK_F15 )
140 {
141 modifiers |= NSFunctionKeyMask ;
142 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
143 }
144 else
145 {
146 switch ( key )
147 {
dbeddfb9 148 case WXK_CLEAR :
9c894932
SC
149 modifiers |= NSFunctionKeyMask;
150 shortcut = NSDeleteCharacter ;
dbeddfb9
SC
151 break ;
152
153 case WXK_PAGEUP :
9c894932
SC
154 modifiers |= NSFunctionKeyMask;
155 shortcut = NSPageUpFunctionKey ;
dbeddfb9
SC
156 break ;
157
158 case WXK_PAGEDOWN :
9c894932
SC
159 modifiers |= NSFunctionKeyMask;
160 shortcut = NSPageDownFunctionKey ;
dbeddfb9
SC
161 break ;
162
163 case WXK_LEFT :
9c894932
SC
164 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
165 shortcut = NSLeftArrowFunctionKey ;
dbeddfb9
SC
166 break ;
167
168 case WXK_UP :
9c894932
SC
169 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
170 shortcut = NSUpArrowFunctionKey ;
dbeddfb9
SC
171 break ;
172
173 case WXK_RIGHT :
9c894932
SC
174 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
175 shortcut = NSRightArrowFunctionKey ;
dbeddfb9
SC
176 break ;
177
178 case WXK_DOWN :
9c894932
SC
179 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
180 shortcut = NSDownArrowFunctionKey ;
dbeddfb9
SC
181 break ;
182
183 case WXK_HOME :
9c894932
SC
184 modifiers |= NSFunctionKeyMask;
185 shortcut = NSHomeFunctionKey ;
dbeddfb9
SC
186 break ;
187
188 case WXK_END :
9c894932
SC
189 modifiers |= NSFunctionKeyMask;
190 shortcut = NSEndFunctionKey ;
dbeddfb9 191 break ;
9c894932
SC
192
193 case WXK_NUMPAD_ENTER :
194 shortcut = NSEnterCharacter;
195 break;
196
197 case WXK_BACK :
198 case WXK_RETURN :
199 case WXK_TAB :
200 case WXK_ESCAPE :
dbeddfb9
SC
201 default :
202 if(entry->GetFlags() & wxACCEL_SHIFT)
203 shortcut = toupper(key);
204 else
205 shortcut = tolower(key);
206 break ;
207 }
208 }
209
210 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
211 [menuItem setKeyEquivalentModifierMask:modifiers];
212 }
213}
214
9c894932
SC
215@interface NSMenuItem(PossibleMethods)
216- (void)setHidden:(BOOL)hidden;
217@end
218
03647350 219class wxMenuItemCocoaImpl : public wxMenuItemImpl
dbeddfb9
SC
220{
221public :
222 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
223 {
be136f07
SC
224 if ( ![m_osxMenuItem isSeparatorItem] )
225 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
dbeddfb9 226 }
03647350 227
dbeddfb9 228 ~wxMenuItemCocoaImpl();
03647350
VZ
229
230 void SetBitmap( const wxBitmap& bitmap )
dbeddfb9
SC
231 {
232 [m_osxMenuItem setImage:bitmap.GetNSImage()];
233 }
03647350
VZ
234
235 void Enable( bool enable )
dbeddfb9
SC
236 {
237 [m_osxMenuItem setEnabled:enable];
238 }
03647350
VZ
239
240 void Check( bool check )
dbeddfb9
SC
241 {
242 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
243 }
03647350 244
dbeddfb9
SC
245 void Hide( bool hide )
246 {
70412bd4
KO
247 // NB: setHidden is new as of 10.5 so we should not call it below there
248 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
249 [m_osxMenuItem setHidden:hide ];
250 else
251 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
dbeddfb9 252 }
03647350
VZ
253
254 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
dbeddfb9
SC
255 {
256 wxCFStringRef cfText(text);
257 [m_osxMenuItem setTitle:cfText.AsNSString()];
03647350 258
dbeddfb9
SC
259 if ( entry )
260 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
261
262 }
c46d0503
SC
263
264 bool DoDefault();
03647350 265
dbeddfb9
SC
266 void * GetHMenuItem() { return m_osxMenuItem; }
267
268protected :
269 NSMenuItem* m_osxMenuItem ;
270} ;
271
272wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
273{
be136f07
SC
274 if ( ![m_osxMenuItem isSeparatorItem] )
275 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
3c9413b7 276 [m_osxMenuItem release];
dbeddfb9
SC
277}
278
c46d0503
SC
279bool wxMenuItemCocoaImpl::DoDefault()
280{
281 bool handled=false;
282 int menuid = m_peer->GetId();
283
284 NSApplication *theNSApplication = [NSApplication sharedApplication];
285 if (menuid == wxID_OSX_HIDE)
286 {
287 [theNSApplication hide:nil];
288 handled=true;
289 }
290 else if (menuid == wxID_OSX_HIDEOTHERS)
291 {
292 [theNSApplication hideOtherApplications:nil];
293 handled=true;
294 }
295 else if (menuid == wxID_OSX_SHOWALL)
296 {
297 [theNSApplication unhideAllApplications:nil];
298 handled=true;
299 }
300 return handled;
301}
dbeddfb9
SC
302
303wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
fbbe829a 304 int menuid,
dbeddfb9
SC
305 const wxString& text,
306 wxAcceleratorEntry *entry,
d8207702 307 const wxString& WXUNUSED(strHelp),
dbeddfb9
SC
308 wxItemKind kind,
309 wxMenu *pSubMenu )
310{
311 wxMenuItemImpl* c = NULL;
312 NSMenuItem* item = nil;
03647350 313
dbeddfb9
SC
314 if ( kind == wxITEM_SEPARATOR )
315 {
316 item = [[NSMenuItem separatorItem] retain];
317 }
318 else
319 {
320 wxCFStringRef cfText(text);
fbbe829a
SC
321 SEL selector = nil;
322 bool targetSelf = false;
323 if ( ! pParentMenu->GetNoEventsMode() && pSubMenu == NULL )
dbeddfb9 324 {
fbbe829a
SC
325 selector = wxOSXGetSelectorFromID(menuid);
326
327 if ( selector == nil )
328 {
329 selector = @selector(clickedAction:);
330 targetSelf = true;
331 }
dbeddfb9 332 }
fbbe829a
SC
333
334 wxNSMenuItem* menuitem = [ [ wxNSMenuItem alloc ] initWithTitle:cfText.AsNSString() action:selector keyEquivalent:@""];
335 if ( targetSelf )
336 [menuitem setTarget:menuitem];
337
dbeddfb9
SC
338 if ( pSubMenu )
339 {
340 pSubMenu->GetPeer()->SetTitle( text );
fbbe829a 341 [menuitem setSubmenu:pSubMenu->GetHMenu()];
dbeddfb9
SC
342 }
343 else
344 {
345 if ( entry )
fbbe829a 346 wxMacCocoaMenuItemSetAccelerator( menuitem, entry );
dbeddfb9 347 }
fbbe829a 348 item = menuitem;
dbeddfb9
SC
349 }
350 c = new wxMenuItemCocoaImpl( peer, item );
dbeddfb9
SC
351 return c;
352}