]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/menuitem.mm
update button style to include BS_MULTILINE when a multiline label is set
[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#ifndef WX_PRECOMP
27 #include "wx/menu.h"
28 #include "wx/utils.h"
29 #include "wx/frame.h"
30 #include "wx/log.h"
31#endif
32
33#include "wx/cocoa/autorelease.h"
34#include "wx/cocoa/string.h"
35
36#import <AppKit/NSMenuItem.h>
37#import <AppKit/NSMenu.h>
38#import <Foundation/NSString.h>
39#import <AppKit/NSCell.h> // NSOnState, NSOffState
40#import <AppKit/NSEvent.h> // modifier key masks
41
42// ----------------------------------------------------------------------------
43// functions prototypes
44// ----------------------------------------------------------------------------
45
46// ============================================================================
47// @class wxNSMenuItemTarget
48// ============================================================================
49@interface wxNSMenuItemTarget : NSObject
50{
51}
52
53- (void)wxMenuItemAction: (id)sender;
54- (BOOL)validateMenuItem: (id)menuItem;
55@end //interface wxNSMenuItemTarget
56
57@implementation wxNSMenuItemTarget : NSObject
58
59- (void)wxMenuItemAction: (id)sender
60{
61 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
62 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
63 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
64 item->CocoaItemSelected();
65}
66
67- (BOOL)validateMenuItem: (id)menuItem
68{
69 // TODO: Do wxWidgets validation here and avoid sending during idle time
70 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
71 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
72 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
73 return item->Cocoa_validateMenuItem();
74}
75
76@end //implementation wxNSMenuItemTarget
77
78// ============================================================================
79// wxMenuItemCocoa implementation
80// ============================================================================
81IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
82wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
83
84wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
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
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
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{
155 wxAutoNSAutoreleasePool pool;
156 if(m_kind == wxITEM_SEPARATOR)
157 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
158 else
159 {
160 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
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:@""];
167 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
168 if(pSubMenu)
169 {
170 wxASSERT(pSubMenu->GetNSMenu());
171 [pSubMenu->GetNSMenu() setTitle:menuTitle];
172 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
173 }
174 else
175 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
176 [menuTitle release];
177 CocoaSetKeyEquivalent();
178 }
179}
180
181wxMenuItem::~wxMenuItem()
182{
183 sm_cocoaHash.erase(m_cocoaNSMenuItem);
184 [m_cocoaNSMenuItem release];
185}
186
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
213// ----------------------------------------------------------------------------
214// misc
215// ----------------------------------------------------------------------------
216
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
236// change item state
237// -----------------
238
239void wxMenuItem::Enable(bool bDoEnable)
240{
241 wxMenuItemBase::Enable(bDoEnable);
242 // NOTE: Nothing to do, we respond to validateMenuItem instead
243}
244
245void wxMenuItem::Check(bool check)
246{
247 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
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 }
282}
283
284void wxMenuItem::SetText(const wxString& label)
285{
286 wxMenuItemBase::SetText(label);
287 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
288 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
289 CocoaSetKeyEquivalent();
290}
291
292void wxMenuItem::SetCheckable(bool checkable)
293{
294 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
295 wxMenuItemBase::SetCheckable(checkable);
296 // NOTE: Cocoa does not discern between unchecked and normal items
297}
298
299#endif // wxUSE_MENUS