]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menuitem.mm
guard against nullptr
[wxWidgets.git] / src / cocoa / menuitem.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/menuitem.mm
3 // Purpose: wxMenuItem implementation
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/12/15
7 // RCS-ID: $Id$
8 // Copyright: 2002-2004 David Elliott
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #if wxUSE_MENUS
23
24 #include "wx/menuitem.h"
25
26 #include "wx/cocoa/objc/objc_uniquifying.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/menu.h"
30 #include "wx/utils.h"
31 #include "wx/frame.h"
32 #include "wx/log.h"
33 #endif
34
35 #include "wx/cocoa/autorelease.h"
36 #include "wx/cocoa/string.h"
37
38 #import <AppKit/NSMenuItem.h>
39 #import <AppKit/NSMenu.h>
40 #import <Foundation/NSString.h>
41 #import <AppKit/NSCell.h> // NSOnState, NSOffState
42 #import <AppKit/NSEvent.h> // modifier key masks
43
44 // ----------------------------------------------------------------------------
45 // functions prototypes
46 // ----------------------------------------------------------------------------
47
48 // ============================================================================
49 // @class wxNSMenuItemTarget
50 // ============================================================================
51 @interface wxNSMenuItemTarget : NSObject
52 {
53 }
54
55 - (void)wxMenuItemAction: (id)sender;
56 - (BOOL)validateMenuItem: (id)menuItem;
57 @end //interface wxNSMenuItemTarget
58 WX_DECLARE_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
59
60 @implementation wxNSMenuItemTarget : NSObject
61
62 - (void)wxMenuItemAction: (id)sender
63 {
64 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
65 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
66 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
67 item->CocoaItemSelected();
68 }
69
70 - (BOOL)validateMenuItem: (id)menuItem
71 {
72 // TODO: Do wxWidgets validation here and avoid sending during idle time
73 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
74 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
75 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
76 return item->Cocoa_validateMenuItem();
77 }
78
79 @end //implementation wxNSMenuItemTarget
80 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
81
82 // ============================================================================
83 // wxMenuItemCocoa implementation
84 // ============================================================================
85 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
86
87 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[WX_GET_OBJC_CLASS(wxNSMenuItemTarget) alloc] init];
88
89 // ----------------------------------------------------------------------------
90 // wxMenuItemBase
91 // ----------------------------------------------------------------------------
92
93 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
94 int itemid,
95 const wxString& name,
96 const wxString& help,
97 wxItemKind kind,
98 wxMenu *subMenu)
99 {
100 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
101 }
102
103 void wxMenuItemCocoa::CocoaSetKeyEquivalent()
104 {
105 wxAcceleratorEntry *accel = GetAccel();
106 if(!accel)
107 return;
108
109 int accelFlags = accel->GetFlags();
110 int keyModifierMask = 0;
111 if(accelFlags & wxACCEL_ALT)
112 keyModifierMask |= NSAlternateKeyMask;
113 if(accelFlags & wxACCEL_CTRL)
114 keyModifierMask |= NSCommandKeyMask;
115 int keyCode = accel->GetKeyCode();
116 if(isalpha(keyCode))
117 { // For alpha characters use upper/lower rather than NSShiftKeyMask
118 char alphaChar;
119 if(accelFlags & wxACCEL_SHIFT)
120 alphaChar = toupper(keyCode);
121 else
122 alphaChar = tolower(keyCode);
123 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
124 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
125 }
126 else
127 {
128 if(accelFlags & wxACCEL_SHIFT)
129 keyModifierMask |= NSShiftKeyMask;
130 if(keyCode < 128) // low ASCII includes backspace/tab/etc.
131 { char alphaChar = keyCode;
132 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
133 }
134 else
135 { // TODO
136 }
137 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
138 }
139 }
140
141 // ----------------------------------------------------------------------------
142 // ctor & dtor
143 // ----------------------------------------------------------------------------
144 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
145 int itemid,
146 const wxString& strName,
147 const wxString& strHelp,
148 wxItemKind kind,
149 wxMenu *pSubMenu)
150 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
151 {
152 wxAutoNSAutoreleasePool pool;
153 if(m_kind == wxITEM_SEPARATOR)
154 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
155 else
156 {
157 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
158 SEL action;
159 if(pSubMenu)
160 action = nil;
161 else
162 action = @selector(wxMenuItemAction:);
163 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
164 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
165 if(pSubMenu)
166 {
167 wxASSERT(pSubMenu->GetNSMenu());
168 [pSubMenu->GetNSMenu() setTitle:menuTitle];
169 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
170 }
171 else
172 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
173 [menuTitle release];
174 CocoaSetKeyEquivalent();
175 }
176 }
177
178 wxMenuItem::~wxMenuItem()
179 {
180 sm_cocoaHash.erase(m_cocoaNSMenuItem);
181 [m_cocoaNSMenuItem release];
182 }
183
184 void wxMenuItem::CocoaItemSelected()
185 {
186 wxMenu *menu = GetMenu();
187 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
188 wxMenuBar *menubar = menu->GetMenuBar();
189 if(menubar)
190 {
191 wxFrame *frame = menubar->GetFrame();
192 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
193 frame->ProcessCommand(GetId());
194 }
195 else
196 {
197 if(IsCheckable())
198 Toggle();
199 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
200 }
201 }
202
203 bool wxMenuItem::Cocoa_validateMenuItem()
204 {
205 // TODO: do more sanity checking
206 // TODO: Do wxWindows validation here and avoid sending during idle time
207 return IsEnabled();
208 }
209
210 // ----------------------------------------------------------------------------
211 // misc
212 // ----------------------------------------------------------------------------
213
214 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
215 const wxBitmap& bmpUnchecked)
216 {
217 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
218 wxAutoNSAutoreleasePool pool;
219 m_bmpChecked = bmpChecked;
220 m_bmpUnchecked = bmpUnchecked;
221 if(IsCheckable())
222 {
223 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
224 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
225 }
226 else
227 {
228 wxASSERT_MSG(!bmpUnchecked.IsOk(),wxT("Normal menu items should only have one bitmap"));
229 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
230 }
231 }
232
233 // change item state
234 // -----------------
235
236 void wxMenuItem::Enable(bool bDoEnable)
237 {
238 wxMenuItemBase::Enable(bDoEnable);
239 // NOTE: Nothing to do, we respond to validateMenuItem instead
240 }
241
242 void wxMenuItem::Check(bool check)
243 {
244 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
245 if(m_isChecked == check)
246 return;
247 wxAutoNSAutoreleasePool pool;
248 if(GetKind() == wxITEM_RADIO)
249 {
250 // it doesn't make sense to uncheck a radio item - what would this do?
251 if(!check)
252 return;
253 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
254 // First search backwards for other radio items
255 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
256 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
257 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
258 prevNode = prevNode->GetPrevious())
259 {
260 radioStart = prevNode;
261 }
262 // Now starting there set the state of every item until we're
263 // out of radio items to set.
264 for(wxMenuItemList::compatibility_iterator node = radioStart;
265 node && (node->GetData()->GetKind() == wxITEM_RADIO);
266 node = node->GetNext())
267 {
268 wxMenuItem *item = node->GetData();
269 bool checkItem = (item == this);
270 item->wxMenuItemBase::Check(checkItem);
271 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
272 }
273 }
274 else // normal check (non-radio) item
275 {
276 wxMenuItemBase::Check(check);
277 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
278 }
279 }
280
281 void wxMenuItem::SetItemLabel(const wxString& label)
282 {
283 wxMenuItemBase::SetItemLabel(label);
284 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
285 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
286 CocoaSetKeyEquivalent();
287 }
288
289 void wxMenuItem::SetCheckable(bool checkable)
290 {
291 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
292 wxMenuItemBase::SetCheckable(checkable);
293 // NOTE: Cocoa does not discern between unchecked and normal items
294 }
295
296 #endif // wxUSE_MENUS