]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/menuitem.mm
moving OnInit back
[wxWidgets.git] / src / osx / cocoa / menuitem.mm
CommitLineData
dbeddfb9
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/menuitem.mm
3// Purpose: wxMenuItem implementation
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id: menuitem.cpp 54129 2008-06-11 19:30:52Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/menuitem.h"
15#include "wx/stockitem.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/app.h"
70412bd4 19 #include "wx/log.h"
dbeddfb9
SC
20 #include "wx/menu.h"
21#endif // WX_PRECOMP
22
23#include "wx/osx/private.h"
24
dbeddfb9
SC
25@implementation wxNSMenuItem
26
27- (id) init
28{
29 [super init];
30 return self;
31}
32
33- (void) clickedAction: (id) sender
34{
d8207702 35 wxUnusedVar(sender);
dbeddfb9
SC
36 if ( impl )
37 {
38 impl->GetWXPeer()->GetMenu()->HandleCommandProcess(impl->GetWXPeer());
39 }
40}
41
42- (BOOL)validateMenuItem:(NSMenuItem *) menuItem
43{
d8207702 44 wxUnusedVar(menuItem);
dbeddfb9
SC
45 if( impl )
46 {
47 impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
48 return impl->GetWXPeer()->IsEnabled();
49 }
50 return YES ;
51}
52
53- (void)setImplementation: (wxMenuItemImpl *) theImplementation
54{
55 impl = theImplementation;
56}
57
58- (wxMenuItemImpl*) implementation
59{
60 return impl;
61}
62
63@end
64
65void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
66{
67 unsigned int modifiers = 0 ;
68 int key = entry->GetKeyCode() ;
69 if ( key )
70 {
71 if (entry->GetFlags() & wxACCEL_CTRL);
72 modifiers |= NSCommandKeyMask;
73
74 if (entry->GetFlags() & wxACCEL_ALT)
75 modifiers |= NSAlternateKeyMask ;
76
77 // this may be ignored later for alpha chars
78
79 if (entry->GetFlags() & wxACCEL_SHIFT)
80 modifiers |= NSShiftKeyMask ;
81
82 unichar shortcut = 0;
83 if ( key >= WXK_F1 && key <= WXK_F15 )
84 {
85 modifiers |= NSFunctionKeyMask ;
86 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
87 }
88 else
89 {
90 switch ( key )
91 {
92/*
93 // standard function keys from here
94 case WXK_TAB :
95 modifiers |= NSFunctionKeyMask ;
96 shortcut = NSTabCharacter ;
97 break ;
98
99 case kEnterCharCode :
100 modifiers |= NSFunctionKeyMask ;
101 cocoaKey = NSTabCharacter ;
102 break ;
103
104 case WXK_RETURN :
105 modifiers |= NSFunctionKeyMask ;
106 cocoaKey = NSTabCharacter ;
107 break ;
108
109 case WXK_ESCAPE :
110 modifiers |= NSFunctionKeyMask ;
111 cocoaKey = kEscapeCharCode ;
112 break ;
113
114 case WXK_SPACE :
115 shortcut = ' ' ;
116 break ;
117
118
119 case WXK_CLEAR :
120 cocoaKey = kClearCharCode ;
121 break ;
122
123 case WXK_PAGEUP :
124 cocoaKey = kPageUpCharCode ;
125 break ;
126
127 case WXK_PAGEDOWN :
128 cocoaKey = kPageDownCharCode ;
129 break ;
130
131 case WXK_LEFT :
132 cocoaKey = kLeftArrowCharCode ;
133 break ;
134
135 case WXK_UP :
136 cocoaKey = kUpArrowCharCode ;
137 break ;
138
139 case WXK_RIGHT :
140 cocoaKey = kRightArrowCharCode ;
141 break ;
142
143 case WXK_DOWN :
144 cocoaKey = kDownArrowCharCode ;
145 break ;
146
147 case WXK_HOME :
148 cocoaKey = kHomeCharCode ;
149 break ;
150
151 case WXK_END :
152 cocoaKey = kEndCharCode ;
153 break ;
154*/
155 // TODO Test all above with their function key equiv.
156 // from NSEvent.h
157 default :
158 if(entry->GetFlags() & wxACCEL_SHIFT)
159 shortcut = toupper(key);
160 else
161 shortcut = tolower(key);
162 break ;
163 }
164 }
165
166 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
167 [menuItem setKeyEquivalentModifierMask:modifiers];
168 }
169}
170
03647350 171class wxMenuItemCocoaImpl : public wxMenuItemImpl
dbeddfb9
SC
172{
173public :
174 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
175 {
be136f07
SC
176 if ( ![m_osxMenuItem isSeparatorItem] )
177 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
dbeddfb9 178 }
03647350 179
dbeddfb9 180 ~wxMenuItemCocoaImpl();
03647350
VZ
181
182 void SetBitmap( const wxBitmap& bitmap )
dbeddfb9
SC
183 {
184 [m_osxMenuItem setImage:bitmap.GetNSImage()];
185 }
03647350
VZ
186
187 void Enable( bool enable )
dbeddfb9
SC
188 {
189 [m_osxMenuItem setEnabled:enable];
190 }
03647350
VZ
191
192 void Check( bool check )
dbeddfb9
SC
193 {
194 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
195 }
03647350 196
dbeddfb9
SC
197 void Hide( bool hide )
198 {
70412bd4
KO
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 ];
202 else
203 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
dbeddfb9 204 }
03647350
VZ
205
206 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
dbeddfb9
SC
207 {
208 wxCFStringRef cfText(text);
209 [m_osxMenuItem setTitle:cfText.AsNSString()];
03647350 210
dbeddfb9
SC
211 if ( entry )
212 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
213
214 }
03647350 215
dbeddfb9
SC
216 void * GetHMenuItem() { return m_osxMenuItem; }
217
218protected :
219 NSMenuItem* m_osxMenuItem ;
220} ;
221
222wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
223{
be136f07
SC
224 if ( ![m_osxMenuItem isSeparatorItem] )
225 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
3c9413b7 226 [m_osxMenuItem release];
dbeddfb9
SC
227}
228
229
230wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
d8207702 231 int WXUNUSED(id),
dbeddfb9
SC
232 const wxString& text,
233 wxAcceleratorEntry *entry,
d8207702 234 const wxString& WXUNUSED(strHelp),
dbeddfb9
SC
235 wxItemKind kind,
236 wxMenu *pSubMenu )
237{
238 wxMenuItemImpl* c = NULL;
239 NSMenuItem* item = nil;
03647350 240
dbeddfb9
SC
241 if ( kind == wxITEM_SEPARATOR )
242 {
243 item = [[NSMenuItem separatorItem] retain];
244 }
245 else
246 {
247 wxCFStringRef cfText(text);
248 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
249 if ( ! pParentMenu->GetNoEventsMode() )
250 {
251 [temp setTarget: temp];
252 [temp setAction: @selector(clickedAction:)];
253 }
254 [temp setTitle:cfText.AsNSString()];
255 if ( pSubMenu )
256 {
257 pSubMenu->GetPeer()->SetTitle( text );
258 [temp setSubmenu:pSubMenu->GetHMenu()];
259 }
260 else
261 {
262 if ( entry )
263 wxMacCocoaMenuItemSetAccelerator( temp, entry );
264 }
265 item = temp;
266 }
267 c = new wxMenuItemCocoaImpl( peer, item );
dbeddfb9
SC
268 return c;
269}