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