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