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