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 [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_ALT)
141 modifiers |= NSAlternateKeyMask ;
143 // this may be ignored later for alpha chars
145 if (entry->GetFlags() & wxACCEL_SHIFT)
146 modifiers |= NSShiftKeyMask ;
148 unichar shortcut = 0;
149 if ( key >= WXK_F1 && key <= WXK_F15 )
151 modifiers |= NSFunctionKeyMask ;
152 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
159 modifiers |= NSFunctionKeyMask;
160 shortcut = NSDeleteCharacter ;
164 modifiers |= NSFunctionKeyMask;
165 shortcut = NSPageUpFunctionKey ;
169 modifiers |= NSFunctionKeyMask;
170 shortcut = NSPageDownFunctionKey ;
174 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
175 shortcut = NSLeftArrowFunctionKey ;
179 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
180 shortcut = NSUpArrowFunctionKey ;
184 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
185 shortcut = NSRightArrowFunctionKey ;
189 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
190 shortcut = NSDownArrowFunctionKey ;
194 modifiers |= NSFunctionKeyMask;
195 shortcut = NSHomeFunctionKey ;
199 modifiers |= NSFunctionKeyMask;
200 shortcut = NSEndFunctionKey ;
203 case WXK_NUMPAD_ENTER :
204 shortcut = NSEnterCharacter;
212 if(entry->GetFlags() & wxACCEL_SHIFT)
213 shortcut = toupper(key);
215 shortcut = tolower(key);
220 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
221 [menuItem setKeyEquivalentModifierMask:modifiers];
225 @interface NSMenuItem(PossibleMethods)
226 - (void)setHidden:(BOOL)hidden;
229 class wxMenuItemCocoaImpl : public wxMenuItemImpl
232 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
234 if ( ![m_osxMenuItem isSeparatorItem] )
235 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
238 ~wxMenuItemCocoaImpl();
240 void SetBitmap( const wxBitmap& bitmap )
242 [m_osxMenuItem setImage:bitmap.GetNSImage()];
245 void Enable( bool enable )
247 [m_osxMenuItem setEnabled:enable];
250 void Check( bool check )
252 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
255 void Hide( bool hide )
257 // NB: setHidden is new as of 10.5 so we should not call it below there
258 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
259 [m_osxMenuItem setHidden:hide ];
261 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
264 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
266 wxCFStringRef cfText(text);
267 [m_osxMenuItem setTitle:cfText.AsNSString()];
269 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
274 void * GetHMenuItem() { return m_osxMenuItem; }
277 NSMenuItem* m_osxMenuItem ;
280 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
282 if ( ![m_osxMenuItem isSeparatorItem] )
283 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
284 [m_osxMenuItem release];
287 bool wxMenuItemCocoaImpl::DoDefault()
290 int menuid = m_peer->GetId();
292 NSApplication *theNSApplication = [NSApplication sharedApplication];
293 if (menuid == wxID_OSX_HIDE)
295 [theNSApplication hide:nil];
298 else if (menuid == wxID_OSX_HIDEOTHERS)
300 [theNSApplication hideOtherApplications:nil];
303 else if (menuid == wxID_OSX_SHOWALL)
305 [theNSApplication unhideAllApplications:nil];
311 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
313 const wxString& text,
314 wxAcceleratorEntry *entry,
315 const wxString& WXUNUSED(strHelp),
319 wxMenuItemImpl* c = NULL;
320 NSMenuItem* item = nil;
322 if ( kind == wxITEM_SEPARATOR )
324 item = [[NSMenuItem separatorItem] retain];
328 wxCFStringRef cfText(text);
330 bool targetSelf = false;
331 if ( ! pParentMenu->GetNoEventsMode() && pSubMenu == NULL )
333 selector = wxOSXGetSelectorFromID(menuid);
335 if ( selector == nil )
337 selector = @selector(clickedAction:);
342 wxNSMenuItem* menuitem = [ [ wxNSMenuItem alloc ] initWithTitle:cfText.AsNSString() action:selector keyEquivalent:@""];
344 [menuitem setTarget:menuitem];
348 pSubMenu->GetPeer()->SetTitle( text );
349 [menuitem setSubmenu:pSubMenu->GetHMenu()];
353 wxMacCocoaMenuItemSetAccelerator( menuitem, entry );
357 c = new wxMenuItemCocoaImpl( peer, item );