1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/menuitem.mm
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: menuitem.cpp 54129 2008-06-11 19:30:52Z SC $
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 if ( impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer()) )
104 return impl->GetWXPeer()->IsEnabled();
109 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
111 impl = theImplementation;
114 - (wxMenuItemImpl*) implementation
121 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
123 unsigned int modifiers = 0 ;
124 int key = entry->GetKeyCode() ;
127 if (entry->GetFlags() & wxACCEL_CTRL)
128 modifiers |= NSCommandKeyMask;
130 if (entry->GetFlags() & wxACCEL_ALT)
131 modifiers |= NSAlternateKeyMask ;
133 // this may be ignored later for alpha chars
135 if (entry->GetFlags() & wxACCEL_SHIFT)
136 modifiers |= NSShiftKeyMask ;
138 unichar shortcut = 0;
139 if ( key >= WXK_F1 && key <= WXK_F15 )
141 modifiers |= NSFunctionKeyMask ;
142 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
149 // standard function keys from here
151 modifiers |= NSFunctionKeyMask ;
152 shortcut = NSTabCharacter ;
155 case kEnterCharCode :
156 modifiers |= NSFunctionKeyMask ;
157 cocoaKey = NSTabCharacter ;
161 modifiers |= NSFunctionKeyMask ;
162 cocoaKey = NSTabCharacter ;
166 modifiers |= NSFunctionKeyMask ;
167 cocoaKey = kEscapeCharCode ;
176 cocoaKey = kClearCharCode ;
180 cocoaKey = kPageUpCharCode ;
184 cocoaKey = kPageDownCharCode ;
188 cocoaKey = kLeftArrowCharCode ;
192 cocoaKey = kUpArrowCharCode ;
196 cocoaKey = kRightArrowCharCode ;
200 cocoaKey = kDownArrowCharCode ;
204 cocoaKey = kHomeCharCode ;
208 cocoaKey = kEndCharCode ;
211 // TODO Test all above with their function key equiv.
214 if(entry->GetFlags() & wxACCEL_SHIFT)
215 shortcut = toupper(key);
217 shortcut = tolower(key);
222 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
223 [menuItem setKeyEquivalentModifierMask:modifiers];
227 class wxMenuItemCocoaImpl : public wxMenuItemImpl
230 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
232 if ( ![m_osxMenuItem isSeparatorItem] )
233 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
236 ~wxMenuItemCocoaImpl();
238 void SetBitmap( const wxBitmap& bitmap )
240 [m_osxMenuItem setImage:bitmap.GetNSImage()];
243 void Enable( bool enable )
245 [m_osxMenuItem setEnabled:enable];
248 void Check( bool check )
250 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
253 void Hide( bool hide )
255 // NB: setHidden is new as of 10.5 so we should not call it below there
256 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
257 [m_osxMenuItem setHidden:hide ];
259 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
262 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
264 wxCFStringRef cfText(text);
265 [m_osxMenuItem setTitle:cfText.AsNSString()];
268 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()];
354 wxMacCocoaMenuItemSetAccelerator( menuitem, entry );
358 c = new wxMenuItemCocoaImpl( peer, item );