]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/menuitem.mm
use logical operator &&, not bitwise &; indentation fix
[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// RCS-ID: $Id$
8// Copyright: 2002-2004 David Elliott
9// Licence: wxWindows licence
fb896a32
DE
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
25466131
WS
21
22#if wxUSE_MENUS
23
24#include "wx/menuitem.h"
25
e7e1ad7d
DE
26#include "wx/cocoa/objc/objc_uniquifying.h"
27
fb896a32
DE
28#ifndef WX_PRECOMP
29 #include "wx/menu.h"
fb896a32
DE
30 #include "wx/utils.h"
31 #include "wx/frame.h"
2fc2d511 32 #include "wx/log.h"
fb896a32
DE
33#endif
34
7fc77f30 35#include "wx/cocoa/autorelease.h"
b0c0a393 36#include "wx/cocoa/string.h"
fb896a32
DE
37
38#import <AppKit/NSMenuItem.h>
39#import <AppKit/NSMenu.h>
40#import <Foundation/NSString.h>
1ee4156a 41#import <AppKit/NSCell.h> // NSOnState, NSOffState
1e85b547 42#import <AppKit/NSEvent.h> // modifier key masks
fb896a32 43
fb896a32
DE
44// ----------------------------------------------------------------------------
45// functions prototypes
46// ----------------------------------------------------------------------------
47
2fc2d511
DE
48// ============================================================================
49// @class wxNSMenuItemTarget
50// ============================================================================
51@interface wxNSMenuItemTarget : NSObject
52{
53}
54
55- (void)wxMenuItemAction: (id)sender;
42036ca8 56- (BOOL)validateMenuItem: (id)menuItem;
2fc2d511 57@end //interface wxNSMenuItemTarget
e7e1ad7d 58WX_DECLARE_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
2fc2d511
DE
59
60@implementation wxNSMenuItemTarget : NSObject
61
62- (void)wxMenuItemAction: (id)sender
63{
48580976 64 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
2fc2d511 65 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
2b030203 66 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
d04995b3 67 item->CocoaItemSelected();
2fc2d511
DE
68}
69
42036ca8
DE
70- (BOOL)validateMenuItem: (id)menuItem
71{
065e208e 72 // TODO: Do wxWidgets validation here and avoid sending during idle time
48580976 73 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
42036ca8 74 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
2b030203 75 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
d04995b3 76 return item->Cocoa_validateMenuItem();
42036ca8
DE
77}
78
2fc2d511 79@end //implementation wxNSMenuItemTarget
e7e1ad7d 80WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
2fc2d511 81
fb896a32
DE
82// ============================================================================
83// wxMenuItemCocoa implementation
84// ============================================================================
85IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
2fc2d511
DE
86wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
87
e7e1ad7d 88wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[WX_GET_OBJC_CLASS(wxNSMenuItemTarget) alloc] init];
fb896a32
DE
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 */
52af3158 105wxString wxMenuItemBase::GetLabelText(const wxString& text)
fb896a32
DE
106{
107 return wxStripMenuCodes(text);
108}
109
1e85b547
DE
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
fb896a32
DE
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{
7fc77f30 159 wxAutoNSAutoreleasePool pool;
67352554
DE
160 if(m_kind == wxITEM_SEPARATOR)
161 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
162 else
fb896a32 163 {
67352554 164 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
d04995b3
DE
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:@""];
67352554 171 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
67352554
DE
172 if(pSubMenu)
173 {
174 wxASSERT(pSubMenu->GetNSMenu());
175 [pSubMenu->GetNSMenu() setTitle:menuTitle];
176 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
177 }
d04995b3
DE
178 else
179 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
67352554 180 [menuTitle release];
1e85b547 181 CocoaSetKeyEquivalent();
fb896a32 182 }
fb896a32
DE
183}
184
185wxMenuItem::~wxMenuItem()
186{
187 sm_cocoaHash.erase(m_cocoaNSMenuItem);
188 [m_cocoaNSMenuItem release];
189}
190
d04995b3
DE
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
fb896a32
DE
217// ----------------------------------------------------------------------------
218// misc
219// ----------------------------------------------------------------------------
220
982fc427
DE
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
fb896a32
DE
240// change item state
241// -----------------
242
243void wxMenuItem::Enable(bool bDoEnable)
244{
245 wxMenuItemBase::Enable(bDoEnable);
e03a77c6 246 // NOTE: Nothing to do, we respond to validateMenuItem instead
fb896a32
DE
247}
248
1ee4156a 249void wxMenuItem::Check(bool check)
fb896a32 250{
2b030203 251 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1ee4156a
DE
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 }
fb896a32
DE
286}
287
52af3158 288void wxMenuItem::SetItemLabel(const wxString& label)
fb896a32 289{
52af3158 290 wxMenuItemBase::SetItemLabel(label);
e03a77c6
DE
291 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
292 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
1e85b547 293 CocoaSetKeyEquivalent();
fb896a32
DE
294}
295
296void wxMenuItem::SetCheckable(bool checkable)
297{
1ee4156a 298 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
fb896a32 299 wxMenuItemBase::SetCheckable(checkable);
1ee4156a 300 // NOTE: Cocoa does not discern between unchecked and normal items
fb896a32
DE
301}
302
303#endif // wxUSE_MENUS