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