]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/menuitem.mm
support for hints, fixing textfield implementation on iOS
[wxWidgets.git] / src / osx / cocoa / menuitem.mm
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"
19 #include "wx/log.h"
20 #include "wx/menu.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/osx/private.h"
24
25 @implementation wxNSMenuItem
26
27 - (id) init
28 {
29 [super init];
30 return self;
31 }
32
33 - (void) clickedAction: (id) sender
34 {
35 wxUnusedVar(sender);
36 if ( impl )
37 {
38 wxMenuItem* menuitem = impl->GetWXPeer();
39 if ( menuitem->GetMenu()->HandleCommandProcess(menuitem) == false )
40 {
41 }
42 }
43 }
44
45 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
46 {
47 wxUnusedVar(menuItem);
48 if( impl )
49 {
50 impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
51 return impl->GetWXPeer()->IsEnabled();
52 }
53 return YES ;
54 }
55
56 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
57 {
58 impl = theImplementation;
59 }
60
61 - (wxMenuItemImpl*) implementation
62 {
63 return impl;
64 }
65
66 @end
67
68 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
69 {
70 unsigned int modifiers = 0 ;
71 int key = entry->GetKeyCode() ;
72 if ( key )
73 {
74 if (entry->GetFlags() & wxACCEL_CTRL)
75 modifiers |= NSCommandKeyMask;
76
77 if (entry->GetFlags() & wxACCEL_ALT)
78 modifiers |= NSAlternateKeyMask ;
79
80 // this may be ignored later for alpha chars
81
82 if (entry->GetFlags() & wxACCEL_SHIFT)
83 modifiers |= NSShiftKeyMask ;
84
85 unichar shortcut = 0;
86 if ( key >= WXK_F1 && key <= WXK_F15 )
87 {
88 modifiers |= NSFunctionKeyMask ;
89 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
90 }
91 else
92 {
93 switch ( key )
94 {
95 /*
96 // standard function keys from here
97 case WXK_TAB :
98 modifiers |= NSFunctionKeyMask ;
99 shortcut = NSTabCharacter ;
100 break ;
101
102 case kEnterCharCode :
103 modifiers |= NSFunctionKeyMask ;
104 cocoaKey = NSTabCharacter ;
105 break ;
106
107 case WXK_RETURN :
108 modifiers |= NSFunctionKeyMask ;
109 cocoaKey = NSTabCharacter ;
110 break ;
111
112 case WXK_ESCAPE :
113 modifiers |= NSFunctionKeyMask ;
114 cocoaKey = kEscapeCharCode ;
115 break ;
116
117 case WXK_SPACE :
118 shortcut = ' ' ;
119 break ;
120
121
122 case WXK_CLEAR :
123 cocoaKey = kClearCharCode ;
124 break ;
125
126 case WXK_PAGEUP :
127 cocoaKey = kPageUpCharCode ;
128 break ;
129
130 case WXK_PAGEDOWN :
131 cocoaKey = kPageDownCharCode ;
132 break ;
133
134 case WXK_LEFT :
135 cocoaKey = kLeftArrowCharCode ;
136 break ;
137
138 case WXK_UP :
139 cocoaKey = kUpArrowCharCode ;
140 break ;
141
142 case WXK_RIGHT :
143 cocoaKey = kRightArrowCharCode ;
144 break ;
145
146 case WXK_DOWN :
147 cocoaKey = kDownArrowCharCode ;
148 break ;
149
150 case WXK_HOME :
151 cocoaKey = kHomeCharCode ;
152 break ;
153
154 case WXK_END :
155 cocoaKey = kEndCharCode ;
156 break ;
157 */
158 // TODO Test all above with their function key equiv.
159 // from NSEvent.h
160 default :
161 if(entry->GetFlags() & wxACCEL_SHIFT)
162 shortcut = toupper(key);
163 else
164 shortcut = tolower(key);
165 break ;
166 }
167 }
168
169 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
170 [menuItem setKeyEquivalentModifierMask:modifiers];
171 }
172 }
173
174 class wxMenuItemCocoaImpl : public wxMenuItemImpl
175 {
176 public :
177 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
178 {
179 if ( ![m_osxMenuItem isSeparatorItem] )
180 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
181 }
182
183 ~wxMenuItemCocoaImpl();
184
185 void SetBitmap( const wxBitmap& bitmap )
186 {
187 [m_osxMenuItem setImage:bitmap.GetNSImage()];
188 }
189
190 void Enable( bool enable )
191 {
192 [m_osxMenuItem setEnabled:enable];
193 }
194
195 void Check( bool check )
196 {
197 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
198 }
199
200 void Hide( bool hide )
201 {
202 // NB: setHidden is new as of 10.5 so we should not call it below there
203 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
204 [m_osxMenuItem setHidden:hide ];
205 else
206 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
207 }
208
209 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
210 {
211 wxCFStringRef cfText(text);
212 [m_osxMenuItem setTitle:cfText.AsNSString()];
213
214 if ( entry )
215 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
216
217 }
218
219 bool DoDefault();
220
221 void * GetHMenuItem() { return m_osxMenuItem; }
222
223 protected :
224 NSMenuItem* m_osxMenuItem ;
225 } ;
226
227 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
228 {
229 if ( ![m_osxMenuItem isSeparatorItem] )
230 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
231 [m_osxMenuItem release];
232 }
233
234 bool wxMenuItemCocoaImpl::DoDefault()
235 {
236 bool handled=false;
237 int menuid = m_peer->GetId();
238
239 NSApplication *theNSApplication = [NSApplication sharedApplication];
240 if (menuid == wxID_OSX_HIDE)
241 {
242 [theNSApplication hide:nil];
243 handled=true;
244 }
245 else if (menuid == wxID_OSX_HIDEOTHERS)
246 {
247 [theNSApplication hideOtherApplications:nil];
248 handled=true;
249 }
250 else if (menuid == wxID_OSX_SHOWALL)
251 {
252 [theNSApplication unhideAllApplications:nil];
253 handled=true;
254 }
255 return handled;
256 }
257
258 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
259 int WXUNUSED(id),
260 const wxString& text,
261 wxAcceleratorEntry *entry,
262 const wxString& WXUNUSED(strHelp),
263 wxItemKind kind,
264 wxMenu *pSubMenu )
265 {
266 wxMenuItemImpl* c = NULL;
267 NSMenuItem* item = nil;
268
269 if ( kind == wxITEM_SEPARATOR )
270 {
271 item = [[NSMenuItem separatorItem] retain];
272 }
273 else
274 {
275 wxCFStringRef cfText(text);
276 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
277 if ( ! pParentMenu->GetNoEventsMode() )
278 {
279 [temp setTarget: temp];
280 [temp setAction: @selector(clickedAction:)];
281 }
282 [temp setTitle:cfText.AsNSString()];
283 if ( pSubMenu )
284 {
285 pSubMenu->GetPeer()->SetTitle( text );
286 [temp setSubmenu:pSubMenu->GetHMenu()];
287 }
288 else
289 {
290 if ( entry )
291 wxMacCocoaMenuItemSetAccelerator( temp, entry );
292 }
293 item = temp;
294 }
295 c = new wxMenuItemCocoaImpl( peer, item );
296 return c;
297 }