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
37 impl->GetWXPeer()->GetMenu()->HandleCommandProcess(impl->GetWXPeer());
41 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
45 impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
46 return impl->GetWXPeer()->IsEnabled();
51 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
53 impl = theImplementation;
56 - (wxMenuItemImpl*) implementation
63 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
65 unsigned int modifiers = 0 ;
66 int key = entry->GetKeyCode() ;
69 if (entry->GetFlags() & wxACCEL_CTRL);
70 modifiers |= NSCommandKeyMask;
72 if (entry->GetFlags() & wxACCEL_ALT)
73 modifiers |= NSAlternateKeyMask ;
75 // this may be ignored later for alpha chars
77 if (entry->GetFlags() & wxACCEL_SHIFT)
78 modifiers |= NSShiftKeyMask ;
81 if ( key >= WXK_F1 && key <= WXK_F15 )
83 modifiers |= NSFunctionKeyMask ;
84 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
91 // standard function keys from here
93 modifiers |= NSFunctionKeyMask ;
94 shortcut = NSTabCharacter ;
98 modifiers |= NSFunctionKeyMask ;
99 cocoaKey = NSTabCharacter ;
103 modifiers |= NSFunctionKeyMask ;
104 cocoaKey = NSTabCharacter ;
108 modifiers |= NSFunctionKeyMask ;
109 cocoaKey = kEscapeCharCode ;
118 cocoaKey = kClearCharCode ;
122 cocoaKey = kPageUpCharCode ;
126 cocoaKey = kPageDownCharCode ;
130 cocoaKey = kLeftArrowCharCode ;
134 cocoaKey = kUpArrowCharCode ;
138 cocoaKey = kRightArrowCharCode ;
142 cocoaKey = kDownArrowCharCode ;
146 cocoaKey = kHomeCharCode ;
150 cocoaKey = kEndCharCode ;
153 // TODO Test all above with their function key equiv.
156 if(entry->GetFlags() & wxACCEL_SHIFT)
157 shortcut = toupper(key);
159 shortcut = tolower(key);
164 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
165 [menuItem setKeyEquivalentModifierMask:modifiers];
169 class wxMenuItemCocoaImpl : public wxMenuItemImpl
172 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
174 if ( ![m_osxMenuItem isSeparatorItem] )
175 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
178 ~wxMenuItemCocoaImpl();
180 void SetBitmap( const wxBitmap& bitmap )
182 [m_osxMenuItem setImage:bitmap.GetNSImage()];
185 void Enable( bool enable )
187 [m_osxMenuItem setEnabled:enable];
190 void Check( bool check )
192 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
195 void Hide( bool hide )
197 // NB: setHidden is new as of 10.5 so we should not call it below there
198 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
199 [m_osxMenuItem setHidden:hide ];
201 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
204 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
206 wxCFStringRef cfText(text);
207 [m_osxMenuItem setTitle:cfText.AsNSString()];
210 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
214 void * GetHMenuItem() { return m_osxMenuItem; }
217 NSMenuItem* m_osxMenuItem ;
220 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
222 if ( ![m_osxMenuItem isSeparatorItem] )
223 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
227 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
229 const wxString& text,
230 wxAcceleratorEntry *entry,
231 const wxString& strHelp,
235 wxMenuItemImpl* c = NULL;
236 NSMenuItem* item = nil;
238 if ( kind == wxITEM_SEPARATOR )
240 item = [[NSMenuItem separatorItem] retain];
244 wxCFStringRef cfText(text);
245 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
246 if ( ! pParentMenu->GetNoEventsMode() )
248 [temp setTarget: temp];
249 [temp setAction: @selector(clickedAction:)];
251 [temp setTitle:cfText.AsNSString()];
254 pSubMenu->GetPeer()->SetTitle( text );
255 [temp setSubmenu:pSubMenu->GetHMenu()];
260 wxMacCocoaMenuItemSetAccelerator( temp, entry );
264 c = new wxMenuItemCocoaImpl( peer, item );