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 @implementation wxNSMenuItem
33 - (void) clickedAction: (id) sender
38 wxMenuItem* menuitem = impl->GetWXPeer();
39 if ( menuitem->GetMenu()->HandleCommandProcess(menuitem) == false )
45 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
47 wxUnusedVar(menuItem);
50 impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
51 return impl->GetWXPeer()->IsEnabled();
56 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
58 impl = theImplementation;
61 - (wxMenuItemImpl*) implementation
68 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
70 unsigned int modifiers = 0 ;
71 int key = entry->GetKeyCode() ;
74 if (entry->GetFlags() & wxACCEL_CTRL)
75 modifiers |= NSCommandKeyMask;
77 if (entry->GetFlags() & wxACCEL_ALT)
78 modifiers |= NSAlternateKeyMask ;
80 // this may be ignored later for alpha chars
82 if (entry->GetFlags() & wxACCEL_SHIFT)
83 modifiers |= NSShiftKeyMask ;
86 if ( key >= WXK_F1 && key <= WXK_F15 )
88 modifiers |= NSFunctionKeyMask ;
89 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
96 // standard function keys from here
98 modifiers |= NSFunctionKeyMask ;
99 shortcut = NSTabCharacter ;
102 case kEnterCharCode :
103 modifiers |= NSFunctionKeyMask ;
104 cocoaKey = NSTabCharacter ;
108 modifiers |= NSFunctionKeyMask ;
109 cocoaKey = NSTabCharacter ;
113 modifiers |= NSFunctionKeyMask ;
114 cocoaKey = kEscapeCharCode ;
123 cocoaKey = kClearCharCode ;
127 cocoaKey = kPageUpCharCode ;
131 cocoaKey = kPageDownCharCode ;
135 cocoaKey = kLeftArrowCharCode ;
139 cocoaKey = kUpArrowCharCode ;
143 cocoaKey = kRightArrowCharCode ;
147 cocoaKey = kDownArrowCharCode ;
151 cocoaKey = kHomeCharCode ;
155 cocoaKey = kEndCharCode ;
158 // TODO Test all above with their function key equiv.
161 if(entry->GetFlags() & wxACCEL_SHIFT)
162 shortcut = toupper(key);
164 shortcut = tolower(key);
169 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
170 [menuItem setKeyEquivalentModifierMask:modifiers];
174 class wxMenuItemCocoaImpl : public wxMenuItemImpl
177 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
179 if ( ![m_osxMenuItem isSeparatorItem] )
180 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
183 ~wxMenuItemCocoaImpl();
185 void SetBitmap( const wxBitmap& bitmap )
187 [m_osxMenuItem setImage:bitmap.GetNSImage()];
190 void Enable( bool enable )
192 [m_osxMenuItem setEnabled:enable];
195 void Check( bool check )
197 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
200 void Hide( bool hide )
202 // NB: setHidden is new as of 10.5 so we should not call it below there
203 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
204 [m_osxMenuItem setHidden:hide ];
206 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
209 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
211 wxCFStringRef cfText(text);
212 [m_osxMenuItem setTitle:cfText.AsNSString()];
215 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
221 void * GetHMenuItem() { return m_osxMenuItem; }
224 NSMenuItem* m_osxMenuItem ;
227 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
229 if ( ![m_osxMenuItem isSeparatorItem] )
230 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
231 [m_osxMenuItem release];
234 bool wxMenuItemCocoaImpl::DoDefault()
237 int menuid = m_peer->GetId();
239 NSApplication *theNSApplication = [NSApplication sharedApplication];
240 if (menuid == wxID_OSX_HIDE)
242 [theNSApplication hide:nil];
245 else if (menuid == wxID_OSX_HIDEOTHERS)
247 [theNSApplication hideOtherApplications:nil];
250 else if (menuid == wxID_OSX_SHOWALL)
252 [theNSApplication unhideAllApplications:nil];
258 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
260 const wxString& text,
261 wxAcceleratorEntry *entry,
262 const wxString& WXUNUSED(strHelp),
266 wxMenuItemImpl* c = NULL;
267 NSMenuItem* item = nil;
269 if ( kind == wxITEM_SEPARATOR )
271 item = [[NSMenuItem separatorItem] retain];
275 wxCFStringRef cfText(text);
276 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
277 if ( ! pParentMenu->GetNoEventsMode() )
279 [temp setTarget: temp];
280 [temp setAction: @selector(clickedAction:)];
282 [temp setTitle:cfText.AsNSString()];
285 pSubMenu->GetPeer()->SetTitle( text );
286 [temp setSubmenu:pSubMenu->GetHMenu()];
291 wxMacCocoaMenuItemSetAccelerator( temp, entry );
295 c = new wxMenuItemCocoaImpl( peer, item );