]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/menuitem.mm
under cocoa a too-small static box leads to erroneous layout information, therefore...
[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 // a mapping from wx ids to standard osx actions in order to support the native menu item handling
26 // if a new mapping is added, make sure the wxNonOwnedWindowController has a handler for this action as well
27
28 struct Mapping
29 {
30 int menuid;
31 SEL action;
32 };
33
34 Mapping sActionToWXMapping[] =
35 {
36 { wxID_UNDO, @selector(undo:) },
37 { wxID_REDO, @selector(redo:) },
38 { wxID_CUT, @selector(cut:) },
39 { wxID_COPY, @selector(copy:) },
40 { wxID_PASTE, @selector(paste:) },
41 { wxID_CLEAR, @selector(delete:) },
42 { wxID_SELECTALL, @selector(selectAll:) },
43 { 0, NULL }
44 };
45
46 int wxOSXGetIdFromSelector(SEL action )
47 {
48 int i = 0 ;
49 while ( sActionToWXMapping[i].action != nil )
50 {
51 if ( sActionToWXMapping[i].action == action )
52 return sActionToWXMapping[i].menuid;
53 ++i;
54 }
55
56 return 0;
57 }
58
59 SEL wxOSXGetSelectorFromID(int menuId )
60 {
61 int i = 0 ;
62 while ( sActionToWXMapping[i].action != nil )
63 {
64 if ( sActionToWXMapping[i].menuid == menuId )
65 return sActionToWXMapping[i].action;
66 ++i;
67 }
68
69 return nil;
70 }
71
72
73 @implementation wxNSMenuItem
74
75 - (id) initWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)charCode
76 {
77 [super initWithTitle:aString action:aSelector keyEquivalent:charCode];
78 return self;
79 }
80
81 - (void) clickedAction: (id) sender
82 {
83 wxUnusedVar(sender);
84 if ( impl )
85 {
86 wxMenuItem* menuitem = impl->GetWXPeer();
87 if ( menuitem->GetMenu()->HandleCommandProcess(menuitem) == false )
88 {
89 }
90 }
91 }
92
93 - (void) setEnabled:(BOOL) flag
94 {
95 [super setEnabled:flag];
96 }
97
98 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
99 {
100 wxUnusedVar(menuItem);
101 if( impl )
102 {
103 if ( impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer()) )
104 return impl->GetWXPeer()->IsEnabled();
105 }
106 return YES ;
107 }
108
109 - (void)setImplementation: (wxMenuItemImpl *) theImplementation
110 {
111 impl = theImplementation;
112 }
113
114 - (wxMenuItemImpl*) implementation
115 {
116 return impl;
117 }
118
119 @end
120
121 void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry* entry )
122 {
123 unsigned int modifiers = 0 ;
124 int key = entry->GetKeyCode() ;
125 if ( key )
126 {
127 if (entry->GetFlags() & wxACCEL_CTRL)
128 modifiers |= NSCommandKeyMask;
129
130 if (entry->GetFlags() & wxACCEL_ALT)
131 modifiers |= NSAlternateKeyMask ;
132
133 // this may be ignored later for alpha chars
134
135 if (entry->GetFlags() & wxACCEL_SHIFT)
136 modifiers |= NSShiftKeyMask ;
137
138 unichar shortcut = 0;
139 if ( key >= WXK_F1 && key <= WXK_F15 )
140 {
141 modifiers |= NSFunctionKeyMask ;
142 shortcut = NSF1FunctionKey + ( key - WXK_F1 );
143 }
144 else
145 {
146 switch ( key )
147 {
148 case WXK_CLEAR :
149 modifiers |= NSFunctionKeyMask;
150 shortcut = NSDeleteCharacter ;
151 break ;
152
153 case WXK_PAGEUP :
154 modifiers |= NSFunctionKeyMask;
155 shortcut = NSPageUpFunctionKey ;
156 break ;
157
158 case WXK_PAGEDOWN :
159 modifiers |= NSFunctionKeyMask;
160 shortcut = NSPageDownFunctionKey ;
161 break ;
162
163 case WXK_LEFT :
164 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
165 shortcut = NSLeftArrowFunctionKey ;
166 break ;
167
168 case WXK_UP :
169 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
170 shortcut = NSUpArrowFunctionKey ;
171 break ;
172
173 case WXK_RIGHT :
174 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
175 shortcut = NSRightArrowFunctionKey ;
176 break ;
177
178 case WXK_DOWN :
179 modifiers |= NSNumericPadKeyMask | NSFunctionKeyMask;
180 shortcut = NSDownArrowFunctionKey ;
181 break ;
182
183 case WXK_HOME :
184 modifiers |= NSFunctionKeyMask;
185 shortcut = NSHomeFunctionKey ;
186 break ;
187
188 case WXK_END :
189 modifiers |= NSFunctionKeyMask;
190 shortcut = NSEndFunctionKey ;
191 break ;
192
193 case WXK_NUMPAD_ENTER :
194 shortcut = NSEnterCharacter;
195 break;
196
197 case WXK_BACK :
198 case WXK_RETURN :
199 case WXK_TAB :
200 case WXK_ESCAPE :
201 default :
202 if(entry->GetFlags() & wxACCEL_SHIFT)
203 shortcut = toupper(key);
204 else
205 shortcut = tolower(key);
206 break ;
207 }
208 }
209
210 [menuItem setKeyEquivalent:[NSString stringWithCharacters:&shortcut length:1]];
211 [menuItem setKeyEquivalentModifierMask:modifiers];
212 }
213 }
214
215 @interface NSMenuItem(PossibleMethods)
216 - (void)setHidden:(BOOL)hidden;
217 @end
218
219 class wxMenuItemCocoaImpl : public wxMenuItemImpl
220 {
221 public :
222 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
223 {
224 if ( ![m_osxMenuItem isSeparatorItem] )
225 [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
226 }
227
228 ~wxMenuItemCocoaImpl();
229
230 void SetBitmap( const wxBitmap& bitmap )
231 {
232 [m_osxMenuItem setImage:bitmap.GetNSImage()];
233 }
234
235 void Enable( bool enable )
236 {
237 [m_osxMenuItem setEnabled:enable];
238 }
239
240 void Check( bool check )
241 {
242 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
243 }
244
245 void Hide( bool hide )
246 {
247 // NB: setHidden is new as of 10.5 so we should not call it below there
248 if ([m_osxMenuItem respondsToSelector:@selector(setHidden:)])
249 [m_osxMenuItem setHidden:hide ];
250 else
251 wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
252 }
253
254 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
255 {
256 wxCFStringRef cfText(text);
257 [m_osxMenuItem setTitle:cfText.AsNSString()];
258
259 if ( entry )
260 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
261
262 }
263
264 bool DoDefault();
265
266 void * GetHMenuItem() { return m_osxMenuItem; }
267
268 protected :
269 NSMenuItem* m_osxMenuItem ;
270 } ;
271
272 wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
273 {
274 if ( ![m_osxMenuItem isSeparatorItem] )
275 [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
276 [m_osxMenuItem release];
277 }
278
279 bool wxMenuItemCocoaImpl::DoDefault()
280 {
281 bool handled=false;
282 int menuid = m_peer->GetId();
283
284 NSApplication *theNSApplication = [NSApplication sharedApplication];
285 if (menuid == wxID_OSX_HIDE)
286 {
287 [theNSApplication hide:nil];
288 handled=true;
289 }
290 else if (menuid == wxID_OSX_HIDEOTHERS)
291 {
292 [theNSApplication hideOtherApplications:nil];
293 handled=true;
294 }
295 else if (menuid == wxID_OSX_SHOWALL)
296 {
297 [theNSApplication unhideAllApplications:nil];
298 handled=true;
299 }
300 return handled;
301 }
302
303 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
304 int menuid,
305 const wxString& text,
306 wxAcceleratorEntry *entry,
307 const wxString& WXUNUSED(strHelp),
308 wxItemKind kind,
309 wxMenu *pSubMenu )
310 {
311 wxMenuItemImpl* c = NULL;
312 NSMenuItem* item = nil;
313
314 if ( kind == wxITEM_SEPARATOR )
315 {
316 item = [[NSMenuItem separatorItem] retain];
317 }
318 else
319 {
320 wxCFStringRef cfText(text);
321 SEL selector = nil;
322 bool targetSelf = false;
323 if ( ! pParentMenu->GetNoEventsMode() && pSubMenu == NULL )
324 {
325 selector = wxOSXGetSelectorFromID(menuid);
326
327 if ( selector == nil )
328 {
329 selector = @selector(clickedAction:);
330 targetSelf = true;
331 }
332 }
333
334 wxNSMenuItem* menuitem = [ [ wxNSMenuItem alloc ] initWithTitle:cfText.AsNSString() action:selector keyEquivalent:@""];
335 if ( targetSelf )
336 [menuitem setTarget:menuitem];
337
338 if ( pSubMenu )
339 {
340 pSubMenu->GetPeer()->SetTitle( text );
341 [menuitem setSubmenu:pSubMenu->GetHMenu()];
342 }
343 else
344 {
345 if ( entry )
346 wxMacCocoaMenuItemSetAccelerator( menuitem, entry );
347 }
348 item = menuitem;
349 }
350 c = new wxMenuItemCocoaImpl( peer, item );
351 return c;
352 }