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