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)
173 if ( ![m_osxMenuItem isSeparatorItem] )
174 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
177 ~wxMenuItemCocoaImpl();
179 void SetBitmap( const wxBitmap& bitmap )
181 [m_osxMenuItem setImage:bitmap.GetNSImage()];
184 void Enable( bool enable )
186 [m_osxMenuItem setEnabled:enable];
189 void Check( bool check )
191 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
194 void Hide( bool hide )
196 [m_osxMenuItem setHidden:hide ];
199 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
201 wxCFStringRef cfText(text);
202 [m_osxMenuItem setTitle:cfText.AsNSString()];
205 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
209 void * GetHMenuItem() { return m_osxMenuItem; }
212 NSMenuItem* m_osxMenuItem ;
215 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
217 if ( ![m_osxMenuItem isSeparatorItem] )
218 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
222 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
224 const wxString& text,
225 wxAcceleratorEntry *entry,
226 const wxString& strHelp,
230 wxMenuItemImpl* c = NULL;
231 NSMenuItem* item = nil;
233 if ( kind == wxITEM_SEPARATOR )
235 item = [[NSMenuItem separatorItem] retain];
239 wxCFStringRef cfText(text);
240 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
241 if ( ! pParentMenu->GetNoEventsMode() )
243 [temp setTarget: temp];
244 [temp setAction: @selector(clickedAction:)];
246 [temp setTitle:cfText.AsNSString()];
249 pSubMenu->GetPeer()->SetTitle( text );
250 [temp setSubmenu:pSubMenu->GetHMenu()];
255 wxMacCocoaMenuItemSetAccelerator( temp, entry );
259 c = new wxMenuItemCocoaImpl( peer, item );