]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menuitem.mm
Use wxObjcAutoRefFromAlloc for sm_cocoaTarget.
[wxWidgets.git] / src / cocoa / menuitem.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: menuitem.cpp
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 #ifndef WX_PRECOMP
22 #include "wx/menu.h"
23 #include "wx/menuitem.h"
24 #include "wx/utils.h"
25 #include "wx/frame.h"
26 #include "wx/log.h"
27 #endif
28
29 #include "wx/cocoa/ObjcPose.h"
30 #include "wx/cocoa/autorelease.h"
31 #include "wx/cocoa/string.h"
32
33 #import <AppKit/NSMenuItem.h>
34 #import <AppKit/NSMenu.h>
35 #import <Foundation/NSString.h>
36 #import <AppKit/NSCell.h> // NSOnState, NSOffState
37
38 #if wxUSE_MENUS
39
40 // ----------------------------------------------------------------------------
41 // functions prototypes
42 // ----------------------------------------------------------------------------
43
44 // ============================================================================
45 // @class wxNSMenuItemTarget
46 // ============================================================================
47 @interface wxNSMenuItemTarget : NSObject
48 {
49 }
50
51 - (void)wxMenuItemAction: (id)sender;
52 - (BOOL)validateMenuItem: (id)menuItem;
53 @end //interface wxNSMenuItemTarget
54
55 @implementation wxNSMenuItemTarget : NSObject
56
57 - (void)wxMenuItemAction: (id)sender
58 {
59 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
60 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
61 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
62 item->CocoaItemSelected();
63 }
64
65 - (BOOL)validateMenuItem: (id)menuItem
66 {
67 // TODO: Do wxWidgets validation here and avoid sending during idle time
68 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
69 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
70 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
71 return item->Cocoa_validateMenuItem();
72 }
73
74 @end //implementation wxNSMenuItemTarget
75
76 // ============================================================================
77 // wxMenuItemCocoa implementation
78 // ============================================================================
79 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
80 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
81
82 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
83
84 // ----------------------------------------------------------------------------
85 // wxMenuItemBase
86 // ----------------------------------------------------------------------------
87
88 wxMenuItem *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 */
99 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
100 {
101 return wxStripMenuCodes(text);
102 }
103
104 // ----------------------------------------------------------------------------
105 // ctor & dtor
106 // ----------------------------------------------------------------------------
107 wxMenuItemCocoa::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 {
115 wxAutoNSAutoreleasePool pool;
116 if(m_kind == wxITEM_SEPARATOR)
117 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
118 else
119 {
120 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
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:@""];
127 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
128 if(pSubMenu)
129 {
130 wxASSERT(pSubMenu->GetNSMenu());
131 [pSubMenu->GetNSMenu() setTitle:menuTitle];
132 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
133 }
134 else
135 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
136 [menuTitle release];
137 }
138 }
139
140 wxMenuItem::~wxMenuItem()
141 {
142 sm_cocoaHash.erase(m_cocoaNSMenuItem);
143 [m_cocoaNSMenuItem release];
144 }
145
146 void 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
165 bool 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
172 // ----------------------------------------------------------------------------
173 // misc
174 // ----------------------------------------------------------------------------
175
176 void 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
195 // change item state
196 // -----------------
197
198 void wxMenuItem::Enable(bool bDoEnable)
199 {
200 wxMenuItemBase::Enable(bDoEnable);
201 // NOTE: Nothing to do, we respond to validateMenuItem instead
202 }
203
204 void wxMenuItem::Check(bool check)
205 {
206 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
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 }
241 }
242
243 void wxMenuItem::SetText(const wxString& label)
244 {
245 wxMenuItemBase::SetText(label);
246 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
247 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
248 }
249
250 void wxMenuItem::SetCheckable(bool checkable)
251 {
252 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
253 wxMenuItemBase::SetCheckable(checkable);
254 // NOTE: Cocoa does not discern between unchecked and normal items
255 }
256
257 #endif // wxUSE_MENUS