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