]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/menuitem.mm
Misc XRC format docs corrections.
[wxWidgets.git] / src / cocoa / menuitem.mm
CommitLineData
fb896a32 1///////////////////////////////////////////////////////////////////////////////
25466131 2// Name: src/cocoa/menuitem.mm
fb896a32
DE
3// Purpose: wxMenuItem implementation
4// Author: David Elliott
5// Modified by:
6// Created: 2002/12/15
3ad32dc5
DE
7// Copyright: 2002-2004 David Elliott
8// Licence: wxWindows licence
fb896a32
DE
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19#include "wx/wxprec.h"
25466131
WS
20
21#if wxUSE_MENUS
22
23#include "wx/menuitem.h"
24
e7e1ad7d
DE
25#include "wx/cocoa/objc/objc_uniquifying.h"
26
fb896a32
DE
27#ifndef WX_PRECOMP
28 #include "wx/menu.h"
fb896a32
DE
29 #include "wx/utils.h"
30 #include "wx/frame.h"
2fc2d511 31 #include "wx/log.h"
fb896a32
DE
32#endif
33
7fc77f30 34#include "wx/cocoa/autorelease.h"
b0c0a393 35#include "wx/cocoa/string.h"
fb896a32
DE
36
37#import <AppKit/NSMenuItem.h>
38#import <AppKit/NSMenu.h>
39#import <Foundation/NSString.h>
1ee4156a 40#import <AppKit/NSCell.h> // NSOnState, NSOffState
1e85b547 41#import <AppKit/NSEvent.h> // modifier key masks
fb896a32 42
fb896a32
DE
43// ----------------------------------------------------------------------------
44// functions prototypes
45// ----------------------------------------------------------------------------
46
2fc2d511
DE
47// ============================================================================
48// @class wxNSMenuItemTarget
49// ============================================================================
50@interface wxNSMenuItemTarget : NSObject
51{
52}
53
54- (void)wxMenuItemAction: (id)sender;
42036ca8 55- (BOOL)validateMenuItem: (id)menuItem;
2fc2d511 56@end //interface wxNSMenuItemTarget
e7e1ad7d 57WX_DECLARE_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
2fc2d511
DE
58
59@implementation wxNSMenuItemTarget : NSObject
60
61- (void)wxMenuItemAction: (id)sender
62{
48580976 63 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
2fc2d511 64 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
2b030203 65 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
d04995b3 66 item->CocoaItemSelected();
2fc2d511
DE
67}
68
42036ca8
DE
69- (BOOL)validateMenuItem: (id)menuItem
70{
065e208e 71 // TODO: Do wxWidgets validation here and avoid sending during idle time
48580976 72 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
42036ca8 73 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
2b030203 74 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
d04995b3 75 return item->Cocoa_validateMenuItem();
42036ca8
DE
76}
77
2fc2d511 78@end //implementation wxNSMenuItemTarget
e7e1ad7d 79WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
2fc2d511 80
fb896a32
DE
81// ============================================================================
82// wxMenuItemCocoa implementation
83// ============================================================================
2fc2d511
DE
84wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
85
e7e1ad7d 86wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[WX_GET_OBJC_CLASS(wxNSMenuItemTarget) alloc] init];
fb896a32
DE
87
88// ----------------------------------------------------------------------------
89// wxMenuItemBase
90// ----------------------------------------------------------------------------
91
92wxMenuItem *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
1e85b547
DE
102void 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
fb896a32
DE
140// ----------------------------------------------------------------------------
141// ctor & dtor
142// ----------------------------------------------------------------------------
143wxMenuItemCocoa::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{
7fc77f30 151 wxAutoNSAutoreleasePool pool;
67352554
DE
152 if(m_kind == wxITEM_SEPARATOR)
153 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
154 else
fb896a32 155 {
67352554 156 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
d04995b3
DE
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:@""];
67352554 163 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
67352554
DE
164 if(pSubMenu)
165 {
166 wxASSERT(pSubMenu->GetNSMenu());
167 [pSubMenu->GetNSMenu() setTitle:menuTitle];
168 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
169 }
d04995b3
DE
170 else
171 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
67352554 172 [menuTitle release];
1e85b547 173 CocoaSetKeyEquivalent();
fb896a32 174 }
fb896a32
DE
175}
176
177wxMenuItem::~wxMenuItem()
178{
179 sm_cocoaHash.erase(m_cocoaNSMenuItem);
180 [m_cocoaNSMenuItem release];
181}
182
d04995b3
DE
183void 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
202bool 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
fb896a32
DE
209// ----------------------------------------------------------------------------
210// misc
211// ----------------------------------------------------------------------------
212
982fc427
DE
213void 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 {
a1b806b9 227 wxASSERT_MSG(!bmpUnchecked.IsOk(),wxT("Normal menu items should only have one bitmap"));
982fc427
DE
228 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
229 }
230}
231
fb896a32
DE
232// change item state
233// -----------------
234
235void wxMenuItem::Enable(bool bDoEnable)
236{
237 wxMenuItemBase::Enable(bDoEnable);
e03a77c6 238 // NOTE: Nothing to do, we respond to validateMenuItem instead
fb896a32
DE
239}
240
1ee4156a 241void wxMenuItem::Check(bool check)
fb896a32 242{
2b030203 243 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1ee4156a
DE
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 }
fb896a32
DE
278}
279
52af3158 280void wxMenuItem::SetItemLabel(const wxString& label)
fb896a32 281{
52af3158 282 wxMenuItemBase::SetItemLabel(label);
e03a77c6
DE
283 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
284 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
1e85b547 285 CocoaSetKeyEquivalent();
fb896a32
DE
286}
287
288void wxMenuItem::SetCheckable(bool checkable)
289{
1ee4156a 290 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
fb896a32 291 wxMenuItemBase::SetCheckable(checkable);
1ee4156a 292 // NOTE: Cocoa does not discern between unchecked and normal items
fb896a32
DE
293}
294
295#endif // wxUSE_MENUS