]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/menuitem.mm
aui dock renumbering fix
[wxWidgets.git] / src / cocoa / menuitem.mm
... / ...
CommitLineData
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
58WX_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
80WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
81
82// ============================================================================
83// wxMenuItemCocoa implementation
84// ============================================================================
85IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
86wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
87
88wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[WX_GET_OBJC_CLASS(wxNSMenuItemTarget) alloc] init];
89
90// ----------------------------------------------------------------------------
91// wxMenuItemBase
92// ----------------------------------------------------------------------------
93
94wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
95 int itemid,
96 const wxString& name,
97 const wxString& help,
98 wxItemKind kind,
99 wxMenu *subMenu)
100{
101 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
102}
103
104/* static */
105wxString wxMenuItemBase::GetLabelText(const wxString& text)
106{
107 return wxStripMenuCodes(text);
108}
109
110void wxMenuItemCocoa::CocoaSetKeyEquivalent()
111{
112 wxAcceleratorEntry *accel = GetAccel();
113 if(!accel)
114 return;
115
116 int accelFlags = accel->GetFlags();
117 int keyModifierMask = 0;
118 if(accelFlags & wxACCEL_ALT)
119 keyModifierMask |= NSAlternateKeyMask;
120 if(accelFlags & wxACCEL_CTRL)
121 keyModifierMask |= NSCommandKeyMask;
122 int keyCode = accel->GetKeyCode();
123 if(isalpha(keyCode))
124 { // For alpha characters use upper/lower rather than NSShiftKeyMask
125 char alphaChar;
126 if(accelFlags & wxACCEL_SHIFT)
127 alphaChar = toupper(keyCode);
128 else
129 alphaChar = tolower(keyCode);
130 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
131 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
132 }
133 else
134 {
135 if(accelFlags & wxACCEL_SHIFT)
136 keyModifierMask |= NSShiftKeyMask;
137 if(keyCode < 128) // low ASCII includes backspace/tab/etc.
138 { char alphaChar = keyCode;
139 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
140 }
141 else
142 { // TODO
143 }
144 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
145 }
146}
147
148// ----------------------------------------------------------------------------
149// ctor & dtor
150// ----------------------------------------------------------------------------
151wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
152 int itemid,
153 const wxString& strName,
154 const wxString& strHelp,
155 wxItemKind kind,
156 wxMenu *pSubMenu)
157 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
158{
159 wxAutoNSAutoreleasePool pool;
160 if(m_kind == wxITEM_SEPARATOR)
161 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
162 else
163 {
164 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
165 SEL action;
166 if(pSubMenu)
167 action = nil;
168 else
169 action = @selector(wxMenuItemAction:);
170 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
171 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
172 if(pSubMenu)
173 {
174 wxASSERT(pSubMenu->GetNSMenu());
175 [pSubMenu->GetNSMenu() setTitle:menuTitle];
176 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
177 }
178 else
179 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
180 [menuTitle release];
181 CocoaSetKeyEquivalent();
182 }
183}
184
185wxMenuItem::~wxMenuItem()
186{
187 sm_cocoaHash.erase(m_cocoaNSMenuItem);
188 [m_cocoaNSMenuItem release];
189}
190
191void wxMenuItem::CocoaItemSelected()
192{
193 wxMenu *menu = GetMenu();
194 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
195 wxMenuBar *menubar = menu->GetMenuBar();
196 if(menubar)
197 {
198 wxFrame *frame = menubar->GetFrame();
199 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
200 frame->ProcessCommand(GetId());
201 }
202 else
203 {
204 if(IsCheckable())
205 Toggle();
206 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
207 }
208}
209
210bool wxMenuItem::Cocoa_validateMenuItem()
211{
212 // TODO: do more sanity checking
213 // TODO: Do wxWindows validation here and avoid sending during idle time
214 return IsEnabled();
215}
216
217// ----------------------------------------------------------------------------
218// misc
219// ----------------------------------------------------------------------------
220
221void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
222 const wxBitmap& bmpUnchecked)
223{
224 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
225 wxAutoNSAutoreleasePool pool;
226 m_bmpChecked = bmpChecked;
227 m_bmpUnchecked = bmpUnchecked;
228 if(IsCheckable())
229 {
230 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
231 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
232 }
233 else
234 {
235 wxASSERT_MSG(!bmpUnchecked.Ok(),wxT("Normal menu items should only have one bitmap"));
236 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
237 }
238}
239
240// change item state
241// -----------------
242
243void wxMenuItem::Enable(bool bDoEnable)
244{
245 wxMenuItemBase::Enable(bDoEnable);
246 // NOTE: Nothing to do, we respond to validateMenuItem instead
247}
248
249void wxMenuItem::Check(bool check)
250{
251 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
252 if(m_isChecked == check)
253 return;
254 wxAutoNSAutoreleasePool pool;
255 if(GetKind() == wxITEM_RADIO)
256 {
257 // it doesn't make sense to uncheck a radio item - what would this do?
258 if(!check)
259 return;
260 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
261 // First search backwards for other radio items
262 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
263 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
264 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
265 prevNode = prevNode->GetPrevious())
266 {
267 radioStart = prevNode;
268 }
269 // Now starting there set the state of every item until we're
270 // out of radio items to set.
271 for(wxMenuItemList::compatibility_iterator node = radioStart;
272 node && (node->GetData()->GetKind() == wxITEM_RADIO);
273 node = node->GetNext())
274 {
275 wxMenuItem *item = node->GetData();
276 bool checkItem = (item == this);
277 item->wxMenuItemBase::Check(checkItem);
278 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
279 }
280 }
281 else // normal check (non-radio) item
282 {
283 wxMenuItemBase::Check(check);
284 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
285 }
286}
287
288void wxMenuItem::SetItemLabel(const wxString& label)
289{
290 wxMenuItemBase::SetItemLabel(label);
291 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
292 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
293 CocoaSetKeyEquivalent();
294}
295
296void wxMenuItem::SetCheckable(bool checkable)
297{
298 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
299 wxMenuItemBase::SetCheckable(checkable);
300 // NOTE: Cocoa does not discern between unchecked and normal items
301}
302
303#endif // wxUSE_MENUS