1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/menuitem.mm
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/menuitem.h"
15 #include "wx/stockitem.h"
23 #include "wx/osx/private.h"
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
34 Mapping sActionToWXMapping[] =
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:) },
46 int wxOSXGetIdFromSelector(SEL action )
49 while ( sActionToWXMapping[i].action != nil )
51 if ( sActionToWXMapping[i].action == action )
52 return sActionToWXMapping[i].menuid;
59 SEL wxOSXGetSelectorFromID(int menuId )
62 while ( sActionToWXMapping[i].action != nil )
64 if ( sActionToWXMapping[i].menuid == menuId )
65 return sActionToWXMapping[i].action;
73 @implementation wxNSMenuItem
75 - (id) initWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)charCode
77 self = [super initWithTitle:aString action:aSelector keyEquivalent:charCode];
81 - (void) clickedAction: (id) sender
86 wxMenuItem* menuitem = impl->GetWXPeer();
87 if ( menuitem->GetMenu()->HandleCommandProcess(menuitem) == false )
93 - (void) setEnabled:(BOOL) flag
95 [super setEnabled:flag];
98 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
100 wxUnusedVar(menuItem);
103 wxMenuItem* wxmenuitem = impl->GetWXPeer();
106 wxmenuitem->GetMenu()->HandleCommandUpdateStatus(wxmenuitem);
107 return wxmenuitem->IsEnabled();
113 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
115 impl = theImplementation;
118 - (wxMenuItemImpl*) implementation
125 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
129 [menuItem setKeyEquivalent:@""];
133 unsigned int modifiers = 0 ;
134 int key = entry->GetKeyCode() ;
137 if (entry->GetFlags() & wxACCEL_CTRL)
138 modifiers |= NSCommandKeyMask;
140 if (entry->GetFlags() & wxACCEL_RAW_CTRL)
141 modifiers |= NSControlKeyMask;
143 if (entry->GetFlags() & wxACCEL_ALT)
144 modifiers |= NSAlternateKeyMask ;
146 // this may be ignored later for alpha chars
148 if (entry->GetFlags() & wxACCEL_SHIFT)
149 modifiers |= NSShiftKeyMask ;
151 unichar shortcut = 0;
152 if ( key >= WXK_F1 && key <= WXK_F15 )
154 modifiers |= NSFunctionKeyMask ;
155 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
162 modifiers |= NSFunctionKeyMask;
163 shortcut = NSDeleteCharacter ;
167 modifiers |= NSFunctionKeyMask;
168 shortcut = NSPageUpFunctionKey ;
172 modifiers |= NSFunctionKeyMask;
173 shortcut = NSPageDownFunctionKey ;
177 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
178 shortcut = NSLeftArrowFunctionKey ;
182 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
183 shortcut = NSUpArrowFunctionKey ;
187 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
188 shortcut = NSRightArrowFunctionKey ;
192 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
193 shortcut = NSDownArrowFunctionKey ;
197 modifiers |= NSFunctionKeyMask;
198 shortcut = NSHomeFunctionKey ;
202 modifiers |= NSFunctionKeyMask;
203 shortcut = NSEndFunctionKey ;
206 case WXK_NUMPAD_ENTER :
207 shortcut = NSEnterCharacter;
215 if(entry->GetFlags() & wxACCEL_SHIFT)
216 shortcut = toupper(key);
218 shortcut = tolower(key);
223 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
224 [menuItem setKeyEquivalentModifierMask:modifiers];
228 @interface NSMenuItem(PossibleMethods)
229 - (void)setHidden:(BOOL)hidden;
232 class wxMenuItemCocoaImpl : public wxMenuItemImpl
235 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
237 if ( ![m_osxMenuItem isSeparatorItem] )
238 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
241 ~wxMenuItemCocoaImpl();
243 void SetBitmap( const wxBitmap& bitmap )
245 [m_osxMenuItem setImage:bitmap.GetNSImage()];
248 void Enable( bool enable )
250 [m_osxMenuItem setEnabled:enable];
253 void Check( bool check )
255 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
258 void Hide( bool hide )
260 // NB: setHidden is new as of 10.5 so we should not call it below there
261 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
262 [m_osxMenuItem setHidden:hide ];
264 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
267 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
269 wxCFStringRef cfText(text);
270 [m_osxMenuItem setTitle:cfText.AsNSString()];
272 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
277 void * GetHMenuItem() { return m_osxMenuItem; }
280 NSMenuItem* m_osxMenuItem ;
283 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
285 if ( ![m_osxMenuItem isSeparatorItem] )
286 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
287 [m_osxMenuItem release];
290 bool wxMenuItemCocoaImpl::DoDefault()
293 int menuid = m_peer->GetId();
295 NSApplication *theNSApplication = [NSApplication sharedApplication];
296 if (menuid == wxID_OSX_HIDE)
298 [theNSApplication hide:nil];
301 else if (menuid == wxID_OSX_HIDEOTHERS)
303 [theNSApplication hideOtherApplications:nil];
306 else if (menuid == wxID_OSX_SHOWALL)
308 [theNSApplication unhideAllApplications:nil];
314 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
316 const wxString& text,
317 wxAcceleratorEntry *entry,
318 const wxString& WXUNUSED(strHelp),
322 wxMenuItemImpl* c = NULL;
323 NSMenuItem* item = nil;
325 if ( kind == wxITEM_SEPARATOR )
327 item = [[NSMenuItem separatorItem] retain];
331 wxCFStringRef cfText(text);
333 bool targetSelf = false;
334 if ( (pParentMenu == NULL || !pParentMenu->GetNoEventsMode()) && pSubMenu == NULL )
336 selector = wxOSXGetSelectorFromID(menuid);
338 if ( selector == nil )
340 selector = @selector(clickedAction:);
345 wxNSMenuItem* menuitem = [ [ wxNSMenuItem alloc ] initWithTitle:cfText.AsNSString() action:selector keyEquivalent:@""];
347 [menuitem setTarget:menuitem];
351 pSubMenu->GetPeer()->SetTitle( text );
352 [menuitem setSubmenu:pSubMenu->GetHMenu()];
356 wxMacCocoaMenuItemSetAccelerator( menuitem, entry );
360 c = new wxMenuItemCocoaImpl( peer, item );