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 @interface wxNSMenuItem : NSMenuItem
29 - (void) setImplementation:(wxMenuItemImpl*) item;
30 - (wxMenuItemImpl*) implementation;
32 - (void) clickedAction: (id) sender;
33 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem;
37 @implementation wxNSMenuItem
45 - (void) clickedAction: (id) sender
49 impl->GetWXPeer()->GetMenu()->HandleCommandProcess(impl->GetWXPeer());
53 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
57 impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
58 return impl->GetWXPeer()->IsEnabled();
63 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
65 impl = theImplementation;
68 - (wxMenuItemImpl*) implementation
75 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
77 unsigned int modifiers = 0 ;
78 int key = entry->GetKeyCode() ;
81 if (entry->GetFlags() & wxACCEL_CTRL);
82 modifiers |= NSCommandKeyMask;
84 if (entry->GetFlags() & wxACCEL_ALT)
85 modifiers |= NSAlternateKeyMask ;
87 // this may be ignored later for alpha chars
89 if (entry->GetFlags() & wxACCEL_SHIFT)
90 modifiers |= NSShiftKeyMask ;
93 if ( key >= WXK_F1 && key <= WXK_F15 )
95 modifiers |= NSFunctionKeyMask ;
96 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
103 // standard function keys from here
105 modifiers |= NSFunctionKeyMask ;
106 shortcut = NSTabCharacter ;
109 case kEnterCharCode :
110 modifiers |= NSFunctionKeyMask ;
111 cocoaKey = NSTabCharacter ;
115 modifiers |= NSFunctionKeyMask ;
116 cocoaKey = NSTabCharacter ;
120 modifiers |= NSFunctionKeyMask ;
121 cocoaKey = kEscapeCharCode ;
130 cocoaKey = kClearCharCode ;
134 cocoaKey = kPageUpCharCode ;
138 cocoaKey = kPageDownCharCode ;
142 cocoaKey = kLeftArrowCharCode ;
146 cocoaKey = kUpArrowCharCode ;
150 cocoaKey = kRightArrowCharCode ;
154 cocoaKey = kDownArrowCharCode ;
158 cocoaKey = kHomeCharCode ;
162 cocoaKey = kEndCharCode ;
165 // TODO Test all above with their function key equiv.
168 if(entry->GetFlags() & wxACCEL_SHIFT)
169 shortcut = toupper(key);
171 shortcut = tolower(key);
176 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
177 [menuItem setKeyEquivalentModifierMask:modifiers];
181 class wxMenuItemCocoaImpl : public wxMenuItemImpl
184 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
188 ~wxMenuItemCocoaImpl();
190 void SetBitmap( const wxBitmap& bitmap )
192 [m_osxMenuItem setImage:bitmap.GetNSImage()];
195 void Enable( bool enable )
197 [m_osxMenuItem setEnabled:enable];
200 void Check( bool check )
202 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
205 void Hide( bool hide )
207 [m_osxMenuItem setHidden:hide ];
210 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
212 wxCFStringRef cfText(text);
213 [m_osxMenuItem setTitle:cfText.AsNSString()];
216 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
220 void * GetHMenuItem() { return m_osxMenuItem; }
223 NSMenuItem* m_osxMenuItem ;
226 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
231 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
233 const wxString& text,
234 wxAcceleratorEntry *entry,
235 const wxString& strHelp,
239 wxMenuItemImpl* c = NULL;
240 NSMenuItem* item = nil;
242 if ( kind == wxITEM_SEPARATOR )
244 item = [[NSMenuItem separatorItem] retain];
248 wxCFStringRef cfText(text);
249 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
250 if ( ! pParentMenu->GetNoEventsMode() )
252 [temp setTarget: temp];
253 [temp setAction: @selector(clickedAction:)];
255 [temp setTitle:cfText.AsNSString()];
258 pSubMenu->GetPeer()->SetTitle( text );
259 [temp setSubmenu:pSubMenu->GetHMenu()];
264 wxMacCocoaMenuItemSetAccelerator( temp, entry );
268 c = new wxMenuItemCocoaImpl( peer, item );
269 if ( kind != wxITEM_SEPARATOR )
271 [(wxNSMenuItem*)item setImplementation:c];