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