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"
22 #include "wx/osx/private.h"
24 @implementation wxNSMenuItem
32 - (void) clickedAction: (id) sender
36 impl->GetWXPeer()->GetMenu()->HandleCommandProcess(impl->GetWXPeer());
40 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
44 impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
45 return impl->GetWXPeer()->IsEnabled();
50 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
52 impl = theImplementation;
55 - (wxMenuItemImpl*) implementation
62 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
64 unsigned int modifiers = 0 ;
65 int key = entry->GetKeyCode() ;
68 if (entry->GetFlags() & wxACCEL_CTRL);
69 modifiers |= NSCommandKeyMask;
71 if (entry->GetFlags() & wxACCEL_ALT)
72 modifiers |= NSAlternateKeyMask ;
74 // this may be ignored later for alpha chars
76 if (entry->GetFlags() & wxACCEL_SHIFT)
77 modifiers |= NSShiftKeyMask ;
80 if ( key >= WXK_F1 && key <= WXK_F15 )
82 modifiers |= NSFunctionKeyMask ;
83 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
90 // standard function keys from here
92 modifiers |= NSFunctionKeyMask ;
93 shortcut = NSTabCharacter ;
97 modifiers |= NSFunctionKeyMask ;
98 cocoaKey = NSTabCharacter ;
102 modifiers |= NSFunctionKeyMask ;
103 cocoaKey = NSTabCharacter ;
107 modifiers |= NSFunctionKeyMask ;
108 cocoaKey = kEscapeCharCode ;
117 cocoaKey = kClearCharCode ;
121 cocoaKey = kPageUpCharCode ;
125 cocoaKey = kPageDownCharCode ;
129 cocoaKey = kLeftArrowCharCode ;
133 cocoaKey = kUpArrowCharCode ;
137 cocoaKey = kRightArrowCharCode ;
141 cocoaKey = kDownArrowCharCode ;
145 cocoaKey = kHomeCharCode ;
149 cocoaKey = kEndCharCode ;
152 // TODO Test all above with their function key equiv.
155 if(entry->GetFlags() & wxACCEL_SHIFT)
156 shortcut = toupper(key);
158 shortcut = tolower(key);
163 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
164 [menuItem setKeyEquivalentModifierMask:modifiers];
168 class wxMenuItemCocoaImpl : public wxMenuItemImpl
171 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
175 ~wxMenuItemCocoaImpl();
177 void SetBitmap( const wxBitmap& bitmap )
179 [m_osxMenuItem setImage:bitmap.GetNSImage()];
182 void Enable( bool enable )
184 [m_osxMenuItem setEnabled:enable];
187 void Check( bool check )
189 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
192 void Hide( bool hide )
194 [m_osxMenuItem setHidden:hide ];
197 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
199 wxCFStringRef cfText(text);
200 [m_osxMenuItem setTitle:cfText.AsNSString()];
203 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
207 void * GetHMenuItem() { return m_osxMenuItem; }
210 NSMenuItem* m_osxMenuItem ;
213 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
218 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
220 const wxString& text,
221 wxAcceleratorEntry *entry,
222 const wxString& strHelp,
226 wxMenuItemImpl* c = NULL;
227 NSMenuItem* item = nil;
229 if ( kind == wxITEM_SEPARATOR )
231 item = [[NSMenuItem separatorItem] retain];
235 wxCFStringRef cfText(text);
236 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
237 if ( ! pParentMenu->GetNoEventsMode() )
239 [temp setTarget: temp];
240 [temp setAction: @selector(clickedAction:)];
242 [temp setTitle:cfText.AsNSString()];
245 pSubMenu->GetPeer()->SetTitle( text );
246 [temp setSubmenu:pSubMenu->GetHMenu()];
251 wxMacCocoaMenuItemSetAccelerator( temp, entry );
255 c = new wxMenuItemCocoaImpl( peer, item );
256 if ( kind != wxITEM_SEPARATOR )
258 [(wxNSMenuItem*)item setImplementation:c];