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