]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/menuitem.mm
define MIIM_BITMAP &c in wx/msw/missing.h instead of msw/menu.cpp as menuitem.cpp...
[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"
19 #include "wx/menu.h"
20#endif // WX_PRECOMP
21
22#include "wx/osx/private.h"
23
dbeddfb9
SC
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
62void 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
168class wxMenuItemCocoaImpl : public wxMenuItemImpl
169{
170public :
171 wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
172 {
173 }
174
175 ~wxMenuItemCocoaImpl();
176
177 void SetBitmap( const wxBitmap& bitmap )
178 {
179 [m_osxMenuItem setImage:bitmap.GetNSImage()];
180 }
181
182 void Enable( bool enable )
183 {
184 [m_osxMenuItem setEnabled:enable];
185 }
186
187 void Check( bool check )
188 {
189 [m_osxMenuItem setState:( check ? NSOnState : NSOffState) ];
190 }
191
192 void Hide( bool hide )
193 {
194 [m_osxMenuItem setHidden:hide ];
195 }
196
197 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
198 {
199 wxCFStringRef cfText(text);
200 [m_osxMenuItem setTitle:cfText.AsNSString()];
201
202 if ( entry )
203 wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
204
205 }
206
207 void * GetHMenuItem() { return m_osxMenuItem; }
208
209protected :
210 NSMenuItem* m_osxMenuItem ;
211} ;
212
213wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
214{
215}
216
217
218wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
219 int id,
220 const wxString& text,
221 wxAcceleratorEntry *entry,
222 const wxString& strHelp,
223 wxItemKind kind,
224 wxMenu *pSubMenu )
225{
226 wxMenuItemImpl* c = NULL;
227 NSMenuItem* item = nil;
228
229 if ( kind == wxITEM_SEPARATOR )
230 {
231 item = [[NSMenuItem separatorItem] retain];
232 }
233 else
234 {
235 wxCFStringRef cfText(text);
236 wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
237 if ( ! pParentMenu->GetNoEventsMode() )
238 {
239 [temp setTarget: temp];
240 [temp setAction: @selector(clickedAction:)];
241 }
242 [temp setTitle:cfText.AsNSString()];
243 if ( pSubMenu )
244 {
245 pSubMenu->GetPeer()->SetTitle( text );
246 [temp setSubmenu:pSubMenu->GetHMenu()];
247 }
248 else
249 {
250 if ( entry )
251 wxMacCocoaMenuItemSetAccelerator( temp, entry );
252 }
253 item = temp;
254 }
255 c = new wxMenuItemCocoaImpl( peer, item );
256 if ( kind != wxITEM_SEPARATOR )
257 {
258 [(wxNSMenuItem*)item setImplementation:c];
259 }
260 return c;
261}