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 impl->GetWXPeer()->GetMenu()->HandleCommandProcess(impl->GetWXPeer());
42 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
44 wxUnusedVar(menuItem);
47 impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
48 return impl->GetWXPeer()->IsEnabled();
53 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
55 impl = theImplementation;
58 - (wxMenuItemImpl*) implementation
65 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
67 unsigned int modifiers = 0 ;
68 int key = entry->GetKeyCode() ;
71 if (entry->GetFlags() & wxACCEL_CTRL)
72 modifiers |= NSCommandKeyMask;
74 if (entry->GetFlags() & wxACCEL_ALT)
75 modifiers |= NSAlternateKeyMask ;
77 // this may be ignored later for alpha chars
79 if (entry->GetFlags() & wxACCEL_SHIFT)
80 modifiers |= NSShiftKeyMask ;
83 if ( key >= WXK_F1 && key <= WXK_F15 )
85 modifiers |= NSFunctionKeyMask ;
86 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
93 // standard function keys from here
95 modifiers |= NSFunctionKeyMask ;
96 shortcut = NSTabCharacter ;
100 modifiers |= NSFunctionKeyMask ;
101 cocoaKey = NSTabCharacter ;
105 modifiers |= NSFunctionKeyMask ;
106 cocoaKey = NSTabCharacter ;
110 modifiers |= NSFunctionKeyMask ;
111 cocoaKey = kEscapeCharCode ;
120 cocoaKey = kClearCharCode ;
124 cocoaKey = kPageUpCharCode ;
128 cocoaKey = kPageDownCharCode ;
132 cocoaKey = kLeftArrowCharCode ;
136 cocoaKey = kUpArrowCharCode ;
140 cocoaKey = kRightArrowCharCode ;
144 cocoaKey = kDownArrowCharCode ;
148 cocoaKey = kHomeCharCode ;
152 cocoaKey = kEndCharCode ;
155 // TODO Test all above with their function key equiv.
158 if(entry->GetFlags() & wxACCEL_SHIFT)
159 shortcut = toupper(key);
161 shortcut = tolower(key);
166 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
167 [menuItem setKeyEquivalentModifierMask:modifiers];
171 class wxMenuItemCocoaImpl : public wxMenuItemImpl
174 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
176 if ( ![m_osxMenuItem isSeparatorItem] )
177 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
180 ~wxMenuItemCocoaImpl();
182 void SetBitmap( const wxBitmap& bitmap )
184 [m_osxMenuItem setImage:bitmap.GetNSImage()];
187 void Enable( bool enable )
189 [m_osxMenuItem setEnabled:enable];
192 void Check( bool check )
194 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
197 void Hide( bool hide )
199 // NB: setHidden is new as of 10.5 so we should not call it below there
200 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
201 [m_osxMenuItem setHidden:hide ];
203 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
206 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
208 wxCFStringRef cfText(text);
209 [m_osxMenuItem setTitle:cfText.AsNSString()];
212 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
216 void * GetHMenuItem() { return m_osxMenuItem; }
219 NSMenuItem* m_osxMenuItem ;
222 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
224 if ( ![m_osxMenuItem isSeparatorItem] )
225 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
226 [m_osxMenuItem release];
230 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
232 const wxString& text,
233 wxAcceleratorEntry *entry,
234 const wxString& WXUNUSED(strHelp),
238 wxMenuItemImpl* c = NULL;
239 NSMenuItem* item = nil;
241 if ( kind == wxITEM_SEPARATOR )
243 item = [[NSMenuItem separatorItem] retain];
247 wxCFStringRef cfText(text);
248 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
249 if ( ! pParentMenu->GetNoEventsMode() )
251 [temp setTarget: temp];
252 [temp setAction: @selector(clickedAction:)];
254 [temp setTitle:cfText.AsNSString()];
257 pSubMenu->GetPeer()->SetTitle( text );
258 [temp setSubmenu:pSubMenu->GetHMenu()];
263 wxMacCocoaMenuItemSetAccelerator( temp, entry );
267 c = new wxMenuItemCocoaImpl( peer, item );