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