]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/menucmn.cpp | |
3 | // Purpose: wxMenu and wxMenuBar methods common to all ports | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 26.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets team | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_MENUS | |
28 | ||
29 | #include <ctype.h> | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/intl.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/menu.h" | |
35 | #endif | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // template lists | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | #include "wx/listimpl.cpp" | |
42 | ||
43 | WX_DEFINE_LIST(wxMenuList) | |
44 | WX_DEFINE_LIST(wxMenuItemList) | |
45 | ||
46 | // ============================================================================ | |
47 | // implementation | |
48 | // ============================================================================ | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // wxMenuItem | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu, | |
55 | int id, | |
56 | const wxString& text, | |
57 | const wxString& help, | |
58 | wxItemKind kind, | |
59 | wxMenu *subMenu) | |
60 | : m_text(text), | |
61 | m_help(help) | |
62 | { | |
63 | wxASSERT_MSG( parentMenu != NULL, wxT("menuitem should have a menu") ); | |
64 | ||
65 | m_parentMenu = parentMenu; | |
66 | m_subMenu = subMenu; | |
67 | m_isEnabled = true; | |
68 | m_isChecked = false; | |
69 | m_id = id; | |
70 | m_kind = kind; | |
71 | if (m_id == wxID_ANY) | |
72 | m_id = wxNewId(); | |
73 | if (m_id == wxID_SEPARATOR) | |
74 | m_kind = wxITEM_SEPARATOR; | |
75 | } | |
76 | ||
77 | wxMenuItemBase::~wxMenuItemBase() | |
78 | { | |
79 | delete m_subMenu; | |
80 | } | |
81 | ||
82 | #if wxUSE_ACCEL | |
83 | ||
84 | static const struct wxKeyName | |
85 | { | |
86 | wxKeyCode code; | |
87 | const wxChar *name; | |
88 | } wxKeyNames[] = | |
89 | { | |
90 | { WXK_DELETE, wxTRANSLATE("DEL") }, | |
91 | { WXK_DELETE, wxTRANSLATE("DELETE") }, | |
92 | { WXK_BACK, wxTRANSLATE("BACK") }, | |
93 | { WXK_INSERT, wxTRANSLATE("INS") }, | |
94 | { WXK_INSERT, wxTRANSLATE("INSERT") }, | |
95 | { WXK_RETURN, wxTRANSLATE("ENTER") }, | |
96 | { WXK_RETURN, wxTRANSLATE("RETURN") }, | |
97 | { WXK_PAGEUP, wxTRANSLATE("PGUP") }, | |
98 | { WXK_PAGEDOWN, wxTRANSLATE("PGDN") }, | |
99 | { WXK_LEFT, wxTRANSLATE("LEFT") }, | |
100 | { WXK_RIGHT, wxTRANSLATE("RIGHT") }, | |
101 | { WXK_UP, wxTRANSLATE("UP") }, | |
102 | { WXK_DOWN, wxTRANSLATE("DOWN") }, | |
103 | { WXK_HOME, wxTRANSLATE("HOME") }, | |
104 | { WXK_END, wxTRANSLATE("END") }, | |
105 | { WXK_SPACE, wxTRANSLATE("SPACE") }, | |
106 | { WXK_TAB, wxTRANSLATE("TAB") }, | |
107 | { WXK_ESCAPE, wxTRANSLATE("ESC") }, | |
108 | { WXK_ESCAPE, wxTRANSLATE("ESCAPE") }, | |
109 | { WXK_CANCEL, wxTRANSLATE("CANCEL") }, | |
110 | { WXK_CLEAR, wxTRANSLATE("CLEAR") }, | |
111 | { WXK_MENU, wxTRANSLATE("MENU") }, | |
112 | { WXK_PAUSE, wxTRANSLATE("PAUSE") }, | |
113 | { WXK_CAPITAL, wxTRANSLATE("CAPITAL") }, | |
114 | { WXK_SELECT, wxTRANSLATE("SELECT") }, | |
115 | { WXK_PRINT, wxTRANSLATE("PRINT") }, | |
116 | { WXK_EXECUTE, wxTRANSLATE("EXECUTE") }, | |
117 | { WXK_SNAPSHOT, wxTRANSLATE("SNAPSHOT") }, | |
118 | { WXK_HELP, wxTRANSLATE("HELP") }, | |
119 | { WXK_ADD, wxTRANSLATE("ADD") }, | |
120 | { WXK_SEPARATOR, wxTRANSLATE("SEPARATOR") }, | |
121 | { WXK_SUBTRACT, wxTRANSLATE("SUBTRACT") }, | |
122 | { WXK_DECIMAL, wxTRANSLATE("DECIMAL") }, | |
123 | { WXK_DIVIDE, wxTRANSLATE("DIVIDE") }, | |
124 | { WXK_NUMLOCK, wxTRANSLATE("NUM_LOCK") }, | |
125 | { WXK_SCROLL, wxTRANSLATE("SCROLL_LOCK") }, | |
126 | { WXK_PAGEUP, wxTRANSLATE("PAGEUP") }, | |
127 | { WXK_PAGEDOWN, wxTRANSLATE("PAGEDOWN") }, | |
128 | { WXK_NUMPAD_SPACE, wxTRANSLATE("KP_SPACE") }, | |
129 | { WXK_NUMPAD_TAB, wxTRANSLATE("KP_TAB") }, | |
130 | { WXK_NUMPAD_ENTER, wxTRANSLATE("KP_ENTER") }, | |
131 | { WXK_NUMPAD_HOME, wxTRANSLATE("KP_HOME") }, | |
132 | { WXK_NUMPAD_LEFT, wxTRANSLATE("KP_LEFT") }, | |
133 | { WXK_NUMPAD_UP, wxTRANSLATE("KP_UP") }, | |
134 | { WXK_NUMPAD_RIGHT, wxTRANSLATE("KP_RIGHT") }, | |
135 | { WXK_NUMPAD_DOWN, wxTRANSLATE("KP_DOWN") }, | |
136 | { WXK_NUMPAD_PAGEUP, wxTRANSLATE("KP_PRIOR") }, | |
137 | { WXK_NUMPAD_PAGEUP, wxTRANSLATE("KP_PAGEUP") }, | |
138 | { WXK_NUMPAD_PAGEDOWN, wxTRANSLATE("KP_NEXT") }, | |
139 | { WXK_NUMPAD_PAGEDOWN, wxTRANSLATE("KP_PAGEDOWN") }, | |
140 | { WXK_NUMPAD_END, wxTRANSLATE("KP_END") }, | |
141 | { WXK_NUMPAD_BEGIN, wxTRANSLATE("KP_BEGIN") }, | |
142 | { WXK_NUMPAD_INSERT, wxTRANSLATE("KP_INSERT") }, | |
143 | { WXK_NUMPAD_DELETE, wxTRANSLATE("KP_DELETE") }, | |
144 | { WXK_NUMPAD_EQUAL, wxTRANSLATE("KP_EQUAL") }, | |
145 | { WXK_NUMPAD_MULTIPLY, wxTRANSLATE("KP_MULTIPLY") }, | |
146 | { WXK_NUMPAD_ADD, wxTRANSLATE("KP_ADD") }, | |
147 | { WXK_NUMPAD_SEPARATOR, wxTRANSLATE("KP_SEPARATOR") }, | |
148 | { WXK_NUMPAD_SUBTRACT, wxTRANSLATE("KP_SUBTRACT") }, | |
149 | { WXK_NUMPAD_DECIMAL, wxTRANSLATE("KP_DECIMAL") }, | |
150 | { WXK_NUMPAD_DIVIDE, wxTRANSLATE("KP_DIVIDE") }, | |
151 | { WXK_WINDOWS_LEFT, wxTRANSLATE("WINDOWS_LEFT") }, | |
152 | { WXK_WINDOWS_RIGHT, wxTRANSLATE("WINDOWS_RIGHT") }, | |
153 | { WXK_WINDOWS_MENU, wxTRANSLATE("WINDOWS_MENU") }, | |
154 | { WXK_COMMAND, wxTRANSLATE("COMMAND") }, | |
155 | }; | |
156 | ||
157 | // return true if the 2 strings refer to the same accel | |
158 | // | |
159 | // as accels can be either translated or not, check for both possibilities and | |
160 | // also compare case-insensitively as the key names case doesn't count | |
161 | static inline bool CompareAccelString(const wxString& str, const wxChar *accel) | |
162 | { | |
163 | return str.CmpNoCase(accel) == 0 | |
164 | #if wxUSE_INTL | |
165 | || str.CmpNoCase(wxGetTranslation(accel)) == 0 | |
166 | #endif | |
167 | ; | |
168 | } | |
169 | ||
170 | // return prefixCode+number if the string is of the form "<prefix><number>" and | |
171 | // 0 if it isn't | |
172 | // | |
173 | // first and last parameter specify the valid domain for "number" part | |
174 | static int | |
175 | IsNumberedAccelKey(const wxString& str, | |
176 | const wxChar *prefix, | |
177 | wxKeyCode prefixCode, | |
178 | unsigned first, | |
179 | unsigned last) | |
180 | { | |
181 | const size_t lenPrefix = wxStrlen(prefix); | |
182 | if ( !CompareAccelString(str.Left(lenPrefix), prefix) ) | |
183 | return 0; | |
184 | ||
185 | unsigned long num; | |
186 | if ( !str.Mid(lenPrefix).ToULong(&num) ) | |
187 | return 0; | |
188 | ||
189 | if ( num < first || num > last ) | |
190 | { | |
191 | // this must be a mistake, chances that this is a valid name of another | |
192 | // key are vanishingly small | |
193 | wxLogDebug(_T("Invalid key string \"%s\""), str.c_str()); | |
194 | return 0; | |
195 | } | |
196 | ||
197 | return prefixCode + num - first; | |
198 | } | |
199 | ||
200 | // return wxAcceleratorEntry for the given menu string or NULL if none | |
201 | wxAcceleratorEntry *wxGetAccelFromString(const wxString& label) | |
202 | { | |
203 | // check for accelerators: they are given after '\t' | |
204 | int posTab = label.Find(wxT('\t')); | |
205 | if ( posTab == wxNOT_FOUND ) | |
206 | return NULL; | |
207 | ||
208 | // parse the accelerator string | |
209 | int accelFlags = wxACCEL_NORMAL; | |
210 | wxString current; | |
211 | for ( size_t n = (size_t)posTab + 1; n < label.length(); n++ ) | |
212 | { | |
213 | if ( (label[n] == '+') || (label[n] == '-') ) | |
214 | { | |
215 | if ( CompareAccelString(current, wxTRANSLATE("ctrl")) ) | |
216 | accelFlags |= wxACCEL_CTRL; | |
217 | else if ( CompareAccelString(current, wxTRANSLATE("alt")) ) | |
218 | accelFlags |= wxACCEL_ALT; | |
219 | else if ( CompareAccelString(current, wxTRANSLATE("shift")) ) | |
220 | accelFlags |= wxACCEL_SHIFT; | |
221 | else // not a recognized modifier name | |
222 | { | |
223 | // we may have "Ctrl-+", for example, but we still want to | |
224 | // catch typos like "Crtl-A" so only give the warning if we | |
225 | // have something before the current '+' or '-', else take | |
226 | // it as a literal symbol | |
227 | if ( current.empty() ) | |
228 | { | |
229 | current += label[n]; | |
230 | ||
231 | // skip clearing it below | |
232 | continue; | |
233 | } | |
234 | else | |
235 | { | |
236 | wxLogDebug(wxT("Unknown accel modifier: '%s'"), | |
237 | current.c_str()); | |
238 | } | |
239 | } | |
240 | ||
241 | current.clear(); | |
242 | } | |
243 | else // not special character | |
244 | { | |
245 | current += (wxChar) wxTolower(label[n]); | |
246 | } | |
247 | } | |
248 | ||
249 | int keyCode; | |
250 | const size_t len = current.length(); | |
251 | switch ( len ) | |
252 | { | |
253 | case 0: | |
254 | wxLogDebug(wxT("No accel key found, accel string ignored.")); | |
255 | return NULL; | |
256 | ||
257 | case 1: | |
258 | // it's just a letter | |
259 | keyCode = current[0U]; | |
260 | ||
261 | // if the key is used with any modifiers, make it an uppercase one | |
262 | // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's | |
263 | // used alone as 'a' and 'A' are different | |
264 | if ( accelFlags != wxACCEL_NORMAL ) | |
265 | keyCode = wxToupper(keyCode); | |
266 | break; | |
267 | ||
268 | default: | |
269 | keyCode = IsNumberedAccelKey(current, wxTRANSLATE("F"), | |
270 | WXK_F1, 1, 12); | |
271 | if ( !keyCode ) | |
272 | { | |
273 | for ( size_t n = 0; n < WXSIZEOF(wxKeyNames); n++ ) | |
274 | { | |
275 | const wxKeyName& kn = wxKeyNames[n]; | |
276 | if ( CompareAccelString(current, kn.name) ) | |
277 | { | |
278 | keyCode = kn.code; | |
279 | break; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | if ( !keyCode ) | |
285 | keyCode = IsNumberedAccelKey(current, wxTRANSLATE("KP_"), | |
286 | WXK_NUMPAD0, 0, 9); | |
287 | if ( !keyCode ) | |
288 | keyCode = IsNumberedAccelKey(current, wxTRANSLATE("SPECIAL"), | |
289 | WXK_SPECIAL1, 1, 20); | |
290 | ||
291 | if ( !keyCode ) | |
292 | { | |
293 | wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."), | |
294 | current.c_str()); | |
295 | return NULL; | |
296 | } | |
297 | } | |
298 | ||
299 | ||
300 | wxASSERT_MSG( keyCode, _T("logic error: should have key code here") ); | |
301 | ||
302 | return new wxAcceleratorEntry(accelFlags, keyCode); | |
303 | } | |
304 | ||
305 | wxAcceleratorEntry *wxMenuItemBase::GetAccel() const | |
306 | { | |
307 | return wxGetAccelFromString(GetText()); | |
308 | } | |
309 | ||
310 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) | |
311 | { | |
312 | wxString text = m_text.BeforeFirst(wxT('\t')); | |
313 | if ( accel ) | |
314 | { | |
315 | text += wxT('\t'); | |
316 | ||
317 | int flags = accel->GetFlags(); | |
318 | if ( flags & wxACCEL_ALT ) | |
319 | text += _("Alt-"); | |
320 | if ( flags & wxACCEL_CTRL ) | |
321 | text += _("Ctrl-"); | |
322 | if ( flags & wxACCEL_SHIFT ) | |
323 | text += _("Shift-"); | |
324 | ||
325 | const int code = accel->GetKeyCode(); | |
326 | ||
327 | if ( wxIsalnum(code) ) | |
328 | text << (wxChar)code; | |
329 | else if ( code >= WXK_F1 && code <= WXK_F12 ) | |
330 | text << _("F") << code - WXK_F1 + 1; | |
331 | else if ( code >= WXK_NUMPAD0 && code <= WXK_NUMPAD9 ) | |
332 | text << _("KP_") << code - WXK_NUMPAD0; | |
333 | else if ( code >= WXK_SPECIAL1 && code <= WXK_SPECIAL20 ) | |
334 | text << _("SPECIAL") << code - WXK_SPECIAL1 + 1; | |
335 | else // check the named keys | |
336 | { | |
337 | size_t n; | |
338 | for ( n = 0; n < WXSIZEOF(wxKeyNames); n++ ) | |
339 | { | |
340 | const wxKeyName& kn = wxKeyNames[n]; | |
341 | if ( code == kn.code ) | |
342 | { | |
343 | text << wxGetTranslation(kn.name); | |
344 | break; | |
345 | } | |
346 | } | |
347 | ||
348 | wxASSERT_MSG( n != WXSIZEOF(wxKeyNames), | |
349 | wxT("unknown keyboard accelerator code") ); | |
350 | } | |
351 | } | |
352 | ||
353 | SetText(text); | |
354 | } | |
355 | ||
356 | #endif // wxUSE_ACCEL | |
357 | ||
358 | bool wxMenuBase::ms_locked = true; | |
359 | ||
360 | // ---------------------------------------------------------------------------- | |
361 | // wxMenu ctor and dtor | |
362 | // ---------------------------------------------------------------------------- | |
363 | ||
364 | void wxMenuBase::Init(long style) | |
365 | { | |
366 | m_menuBar = (wxMenuBar *)NULL; | |
367 | m_menuParent = (wxMenu *)NULL; | |
368 | ||
369 | m_invokingWindow = (wxWindow *)NULL; | |
370 | m_style = style; | |
371 | m_clientData = (void *)NULL; | |
372 | m_eventHandler = this; | |
373 | } | |
374 | ||
375 | wxMenuBase::~wxMenuBase() | |
376 | { | |
377 | WX_CLEAR_LIST(wxMenuItemList, m_items); | |
378 | ||
379 | // Actually, in GTK, the submenus have to get deleted first. | |
380 | } | |
381 | ||
382 | // ---------------------------------------------------------------------------- | |
383 | // wxMenu item adding/removing | |
384 | // ---------------------------------------------------------------------------- | |
385 | ||
386 | void wxMenuBase::AddSubMenu(wxMenu *submenu) | |
387 | { | |
388 | wxCHECK_RET( submenu, _T("can't add a NULL submenu") ); | |
389 | ||
390 | submenu->SetParent((wxMenu *)this); | |
391 | } | |
392 | ||
393 | wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item) | |
394 | { | |
395 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") ); | |
396 | ||
397 | m_items.Append(item); | |
398 | item->SetMenu((wxMenu*)this); | |
399 | if ( item->IsSubMenu() ) | |
400 | { | |
401 | AddSubMenu(item->GetSubMenu()); | |
402 | } | |
403 | ||
404 | return item; | |
405 | } | |
406 | ||
407 | wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item) | |
408 | { | |
409 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") ); | |
410 | ||
411 | if ( pos == GetMenuItemCount() ) | |
412 | { | |
413 | return DoAppend(item); | |
414 | } | |
415 | else | |
416 | { | |
417 | wxCHECK_MSG( pos < GetMenuItemCount(), NULL, | |
418 | wxT("invalid index in wxMenu::Insert") ); | |
419 | ||
420 | return DoInsert(pos, item); | |
421 | } | |
422 | } | |
423 | ||
424 | wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) | |
425 | { | |
426 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") ); | |
427 | ||
428 | wxMenuItemList::compatibility_iterator node = m_items.Item(pos); | |
429 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") ); | |
430 | ||
431 | m_items.Insert(node, item); | |
432 | item->SetMenu((wxMenu*)this); | |
433 | if ( item->IsSubMenu() ) | |
434 | { | |
435 | AddSubMenu(item->GetSubMenu()); | |
436 | } | |
437 | ||
438 | return item; | |
439 | } | |
440 | ||
441 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) | |
442 | { | |
443 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); | |
444 | ||
445 | return DoRemove(item); | |
446 | } | |
447 | ||
448 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) | |
449 | { | |
450 | wxMenuItemList::compatibility_iterator node = m_items.Find(item); | |
451 | ||
452 | // if we get here, the item is valid or one of Remove() functions is broken | |
453 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
454 | ||
455 | // we detach the item, but we do delete the list node (i.e. don't call | |
456 | // DetachNode() here!) | |
457 | m_items.Erase(node); | |
458 | ||
459 | // item isn't attached to anything any more | |
460 | item->SetMenu((wxMenu *)NULL); | |
461 | wxMenu *submenu = item->GetSubMenu(); | |
462 | if ( submenu ) | |
463 | { | |
464 | submenu->SetParent((wxMenu *)NULL); | |
465 | if ( submenu->IsAttached() ) | |
466 | submenu->Detach(); | |
467 | } | |
468 | ||
469 | return item; | |
470 | } | |
471 | ||
472 | bool wxMenuBase::Delete(wxMenuItem *item) | |
473 | { | |
474 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") ); | |
475 | ||
476 | return DoDelete(item); | |
477 | } | |
478 | ||
479 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
480 | { | |
481 | wxMenuItem *item2 = DoRemove(item); | |
482 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); | |
483 | ||
484 | // don't delete the submenu | |
485 | item2->SetSubMenu((wxMenu *)NULL); | |
486 | ||
487 | delete item2; | |
488 | ||
489 | return true; | |
490 | } | |
491 | ||
492 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
493 | { | |
494 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") ); | |
495 | ||
496 | return DoDestroy(item); | |
497 | } | |
498 | ||
499 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
500 | { | |
501 | wxMenuItem *item2 = DoRemove(item); | |
502 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); | |
503 | ||
504 | delete item2; | |
505 | ||
506 | return true; | |
507 | } | |
508 | ||
509 | // ---------------------------------------------------------------------------- | |
510 | // wxMenu searching for items | |
511 | // ---------------------------------------------------------------------------- | |
512 | ||
513 | // Finds the item id matching the given string, wxNOT_FOUND if not found. | |
514 | int wxMenuBase::FindItem(const wxString& text) const | |
515 | { | |
516 | wxString label = wxMenuItem::GetLabelFromText(text); | |
517 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); | |
518 | node; | |
519 | node = node->GetNext() ) | |
520 | { | |
521 | wxMenuItem *item = node->GetData(); | |
522 | if ( item->IsSubMenu() ) | |
523 | { | |
524 | int rc = item->GetSubMenu()->FindItem(label); | |
525 | if ( rc != wxNOT_FOUND ) | |
526 | return rc; | |
527 | } | |
528 | ||
529 | // we execute this code for submenus as well to alllow finding them by | |
530 | // name just like the ordinary items | |
531 | if ( !item->IsSeparator() ) | |
532 | { | |
533 | if ( item->GetLabel() == label ) | |
534 | return item->GetId(); | |
535 | } | |
536 | } | |
537 | ||
538 | return wxNOT_FOUND; | |
539 | } | |
540 | ||
541 | // recursive search for item by id | |
542 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const | |
543 | { | |
544 | if ( itemMenu ) | |
545 | *itemMenu = NULL; | |
546 | ||
547 | wxMenuItem *item = NULL; | |
548 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); | |
549 | node && !item; | |
550 | node = node->GetNext() ) | |
551 | { | |
552 | item = node->GetData(); | |
553 | ||
554 | if ( item->GetId() == itemId ) | |
555 | { | |
556 | if ( itemMenu ) | |
557 | *itemMenu = (wxMenu *)this; | |
558 | } | |
559 | else if ( item->IsSubMenu() ) | |
560 | { | |
561 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); | |
562 | } | |
563 | else | |
564 | { | |
565 | // don't exit the loop | |
566 | item = NULL; | |
567 | } | |
568 | } | |
569 | ||
570 | return item; | |
571 | } | |
572 | ||
573 | // non recursive search | |
574 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const | |
575 | { | |
576 | wxMenuItem *item = (wxMenuItem *)NULL; | |
577 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
578 | ||
579 | size_t pos; | |
580 | for ( pos = 0; node; pos++ ) | |
581 | { | |
582 | if ( node->GetData()->GetId() == id ) | |
583 | { | |
584 | item = node->GetData(); | |
585 | ||
586 | break; | |
587 | } | |
588 | ||
589 | node = node->GetNext(); | |
590 | } | |
591 | ||
592 | if ( ppos ) | |
593 | { | |
594 | *ppos = item ? pos : (size_t)wxNOT_FOUND; | |
595 | } | |
596 | ||
597 | return item; | |
598 | } | |
599 | ||
600 | // find by position | |
601 | wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const | |
602 | { | |
603 | wxCHECK_MSG( position < m_items.GetCount(), NULL, | |
604 | _T("wxMenu::FindItemByPosition(): invalid menu index") ); | |
605 | ||
606 | return m_items.Item( position )->GetData(); | |
607 | } | |
608 | ||
609 | // ---------------------------------------------------------------------------- | |
610 | // wxMenu helpers used by derived classes | |
611 | // ---------------------------------------------------------------------------- | |
612 | ||
613 | // Update a menu and all submenus recursively. source is the object that has | |
614 | // the update event handlers defined for it. If NULL, the menu or associated | |
615 | // window will be used. | |
616 | void wxMenuBase::UpdateUI(wxEvtHandler* source) | |
617 | { | |
618 | if (GetInvokingWindow()) | |
619 | { | |
620 | // Don't update menus if the parent | |
621 | // frame is about to get deleted | |
622 | wxWindow *tlw = wxGetTopLevelParent( GetInvokingWindow() ); | |
623 | if (tlw && wxPendingDelete.Member(tlw)) | |
624 | return; | |
625 | } | |
626 | ||
627 | if ( !source && GetInvokingWindow() ) | |
628 | source = GetInvokingWindow()->GetEventHandler(); | |
629 | if ( !source ) | |
630 | source = GetEventHandler(); | |
631 | if ( !source ) | |
632 | source = this; | |
633 | ||
634 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
635 | while ( node ) | |
636 | { | |
637 | wxMenuItem* item = node->GetData(); | |
638 | if ( !item->IsSeparator() ) | |
639 | { | |
640 | wxWindowID id = item->GetId(); | |
641 | wxUpdateUIEvent event(id); | |
642 | event.SetEventObject( source ); | |
643 | ||
644 | if ( source->ProcessEvent(event) ) | |
645 | { | |
646 | // if anything changed, update the changed attribute | |
647 | if (event.GetSetText()) | |
648 | SetLabel(id, event.GetText()); | |
649 | if (event.GetSetChecked()) | |
650 | Check(id, event.GetChecked()); | |
651 | if (event.GetSetEnabled()) | |
652 | Enable(id, event.GetEnabled()); | |
653 | } | |
654 | ||
655 | // recurse to the submenus | |
656 | if ( item->GetSubMenu() ) | |
657 | item->GetSubMenu()->UpdateUI(source); | |
658 | } | |
659 | //else: item is a separator (which doesn't process update UI events) | |
660 | ||
661 | node = node->GetNext(); | |
662 | } | |
663 | } | |
664 | ||
665 | bool wxMenuBase::SendEvent(int id, int checked) | |
666 | { | |
667 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id); | |
668 | event.SetEventObject(this); | |
669 | event.SetInt(checked); | |
670 | ||
671 | bool processed = false; | |
672 | ||
673 | // Try the menu's event handler | |
674 | // if ( !processed ) | |
675 | { | |
676 | wxEvtHandler *handler = GetEventHandler(); | |
677 | if ( handler ) | |
678 | processed = handler->ProcessEvent(event); | |
679 | } | |
680 | ||
681 | // Try the window the menu was popped up from (and up through the | |
682 | // hierarchy) | |
683 | if ( !processed ) | |
684 | { | |
685 | const wxMenuBase *menu = this; | |
686 | while ( menu ) | |
687 | { | |
688 | wxWindow *win = menu->GetInvokingWindow(); | |
689 | if ( win ) | |
690 | { | |
691 | processed = win->GetEventHandler()->ProcessEvent(event); | |
692 | break; | |
693 | } | |
694 | ||
695 | menu = menu->GetParent(); | |
696 | } | |
697 | } | |
698 | ||
699 | return processed; | |
700 | } | |
701 | ||
702 | // ---------------------------------------------------------------------------- | |
703 | // wxMenu attaching/detaching to/from menu bar | |
704 | // ---------------------------------------------------------------------------- | |
705 | ||
706 | wxMenuBar* wxMenuBase::GetMenuBar() const | |
707 | { | |
708 | if(GetParent()) | |
709 | return GetParent()->GetMenuBar(); | |
710 | return m_menuBar; | |
711 | } | |
712 | ||
713 | void wxMenuBase::Attach(wxMenuBarBase *menubar) | |
714 | { | |
715 | // use Detach() instead! | |
716 | wxASSERT_MSG( menubar, _T("menu can't be attached to NULL menubar") ); | |
717 | ||
718 | // use IsAttached() to prevent this from happening | |
719 | wxASSERT_MSG( !m_menuBar, _T("attaching menu twice?") ); | |
720 | ||
721 | m_menuBar = (wxMenuBar *)menubar; | |
722 | } | |
723 | ||
724 | void wxMenuBase::Detach() | |
725 | { | |
726 | // use IsAttached() to prevent this from happening | |
727 | wxASSERT_MSG( m_menuBar, _T("detaching unattached menu?") ); | |
728 | ||
729 | m_menuBar = NULL; | |
730 | } | |
731 | ||
732 | // ---------------------------------------------------------------------------- | |
733 | // wxMenu functions forwarded to wxMenuItem | |
734 | // ---------------------------------------------------------------------------- | |
735 | ||
736 | void wxMenuBase::Enable( int id, bool enable ) | |
737 | { | |
738 | wxMenuItem *item = FindItem(id); | |
739 | ||
740 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
741 | ||
742 | item->Enable(enable); | |
743 | } | |
744 | ||
745 | bool wxMenuBase::IsEnabled( int id ) const | |
746 | { | |
747 | wxMenuItem *item = FindItem(id); | |
748 | ||
749 | wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") ); | |
750 | ||
751 | return item->IsEnabled(); | |
752 | } | |
753 | ||
754 | void wxMenuBase::Check( int id, bool enable ) | |
755 | { | |
756 | wxMenuItem *item = FindItem(id); | |
757 | ||
758 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
759 | ||
760 | item->Check(enable); | |
761 | } | |
762 | ||
763 | bool wxMenuBase::IsChecked( int id ) const | |
764 | { | |
765 | wxMenuItem *item = FindItem(id); | |
766 | ||
767 | wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") ); | |
768 | ||
769 | return item->IsChecked(); | |
770 | } | |
771 | ||
772 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
773 | { | |
774 | wxMenuItem *item = FindItem(id); | |
775 | ||
776 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
777 | ||
778 | item->SetText(label); | |
779 | } | |
780 | ||
781 | wxString wxMenuBase::GetLabel( int id ) const | |
782 | { | |
783 | wxMenuItem *item = FindItem(id); | |
784 | ||
785 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") ); | |
786 | ||
787 | return item->GetText(); | |
788 | } | |
789 | ||
790 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
791 | { | |
792 | wxMenuItem *item = FindItem(id); | |
793 | ||
794 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
795 | ||
796 | item->SetHelp( helpString ); | |
797 | } | |
798 | ||
799 | wxString wxMenuBase::GetHelpString( int id ) const | |
800 | { | |
801 | wxMenuItem *item = FindItem(id); | |
802 | ||
803 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") ); | |
804 | ||
805 | return item->GetHelp(); | |
806 | } | |
807 | ||
808 | // ---------------------------------------------------------------------------- | |
809 | // wxMenuBarBase ctor and dtor | |
810 | // ---------------------------------------------------------------------------- | |
811 | ||
812 | wxMenuBarBase::wxMenuBarBase() | |
813 | { | |
814 | // not attached yet | |
815 | m_menuBarFrame = NULL; | |
816 | } | |
817 | ||
818 | wxMenuBarBase::~wxMenuBarBase() | |
819 | { | |
820 | WX_CLEAR_LIST(wxMenuList, m_menus); | |
821 | } | |
822 | ||
823 | // ---------------------------------------------------------------------------- | |
824 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
825 | // derived class should reflect the changes in the real menubar | |
826 | // ---------------------------------------------------------------------------- | |
827 | ||
828 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
829 | { | |
830 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
831 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); | |
832 | ||
833 | return node->GetData(); | |
834 | } | |
835 | ||
836 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title)) | |
837 | { | |
838 | wxCHECK_MSG( menu, false, wxT("can't append NULL menu") ); | |
839 | ||
840 | m_menus.Append(menu); | |
841 | menu->Attach(this); | |
842 | ||
843 | return true; | |
844 | } | |
845 | ||
846 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
847 | const wxString& title) | |
848 | { | |
849 | if ( pos == m_menus.GetCount() ) | |
850 | { | |
851 | return wxMenuBarBase::Append(menu, title); | |
852 | } | |
853 | else // not at the end | |
854 | { | |
855 | wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") ); | |
856 | ||
857 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
858 | wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") ); | |
859 | ||
860 | m_menus.Insert(node, menu); | |
861 | menu->Attach(this); | |
862 | ||
863 | return true; | |
864 | } | |
865 | } | |
866 | ||
867 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
868 | const wxString& WXUNUSED(title)) | |
869 | { | |
870 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
871 | ||
872 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
873 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); | |
874 | ||
875 | wxMenu *menuOld = node->GetData(); | |
876 | node->SetData(menu); | |
877 | ||
878 | menu->Attach(this); | |
879 | menuOld->Detach(); | |
880 | ||
881 | return menuOld; | |
882 | } | |
883 | ||
884 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
885 | { | |
886 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
887 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); | |
888 | ||
889 | wxMenu *menu = node->GetData(); | |
890 | m_menus.Erase(node); | |
891 | menu->Detach(); | |
892 | ||
893 | return menu; | |
894 | } | |
895 | ||
896 | int wxMenuBarBase::FindMenu(const wxString& title) const | |
897 | { | |
898 | wxString label = wxMenuItem::GetLabelFromText(title); | |
899 | ||
900 | size_t count = GetMenuCount(); | |
901 | for ( size_t i = 0; i < count; i++ ) | |
902 | { | |
903 | wxString title2 = GetLabelTop(i); | |
904 | if ( (title2 == title) || | |
905 | (wxMenuItem::GetLabelFromText(title2) == label) ) | |
906 | { | |
907 | // found | |
908 | return (int)i; | |
909 | } | |
910 | } | |
911 | ||
912 | return wxNOT_FOUND; | |
913 | ||
914 | } | |
915 | ||
916 | // ---------------------------------------------------------------------------- | |
917 | // wxMenuBar attaching/detaching to/from the frame | |
918 | // ---------------------------------------------------------------------------- | |
919 | ||
920 | void wxMenuBarBase::Attach(wxFrame *frame) | |
921 | { | |
922 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
923 | ||
924 | m_menuBarFrame = frame; | |
925 | } | |
926 | ||
927 | void wxMenuBarBase::Detach() | |
928 | { | |
929 | wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") ); | |
930 | ||
931 | m_menuBarFrame = NULL; | |
932 | } | |
933 | ||
934 | // ---------------------------------------------------------------------------- | |
935 | // wxMenuBar searching for items | |
936 | // ---------------------------------------------------------------------------- | |
937 | ||
938 | wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const | |
939 | { | |
940 | if ( menu ) | |
941 | *menu = NULL; | |
942 | ||
943 | wxMenuItem *item = NULL; | |
944 | size_t count = GetMenuCount(), i; | |
945 | wxMenuList::const_iterator it; | |
946 | for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ ) | |
947 | { | |
948 | item = (*it)->FindItem(id, menu); | |
949 | } | |
950 | ||
951 | return item; | |
952 | } | |
953 | ||
954 | int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const | |
955 | { | |
956 | wxString label = wxMenuItem::GetLabelFromText(menu); | |
957 | ||
958 | int i = 0; | |
959 | wxMenuList::compatibility_iterator node; | |
960 | for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ ) | |
961 | { | |
962 | if ( label == wxMenuItem::GetLabelFromText(GetLabelTop(i)) ) | |
963 | return node->GetData()->FindItem(item); | |
964 | } | |
965 | ||
966 | return wxNOT_FOUND; | |
967 | } | |
968 | ||
969 | // --------------------------------------------------------------------------- | |
970 | // wxMenuBar functions forwarded to wxMenuItem | |
971 | // --------------------------------------------------------------------------- | |
972 | ||
973 | void wxMenuBarBase::Enable(int id, bool enable) | |
974 | { | |
975 | wxMenuItem *item = FindItem(id); | |
976 | ||
977 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
978 | ||
979 | item->Enable(enable); | |
980 | } | |
981 | ||
982 | void wxMenuBarBase::Check(int id, bool check) | |
983 | { | |
984 | wxMenuItem *item = FindItem(id); | |
985 | ||
986 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
987 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
988 | ||
989 | item->Check(check); | |
990 | } | |
991 | ||
992 | bool wxMenuBarBase::IsChecked(int id) const | |
993 | { | |
994 | wxMenuItem *item = FindItem(id); | |
995 | ||
996 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") ); | |
997 | ||
998 | return item->IsChecked(); | |
999 | } | |
1000 | ||
1001 | bool wxMenuBarBase::IsEnabled(int id) const | |
1002 | { | |
1003 | wxMenuItem *item = FindItem(id); | |
1004 | ||
1005 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") ); | |
1006 | ||
1007 | return item->IsEnabled(); | |
1008 | } | |
1009 | ||
1010 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
1011 | { | |
1012 | wxMenuItem *item = FindItem(id); | |
1013 | ||
1014 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
1015 | ||
1016 | item->SetText(label); | |
1017 | } | |
1018 | ||
1019 | wxString wxMenuBarBase::GetLabel(int id) const | |
1020 | { | |
1021 | wxMenuItem *item = FindItem(id); | |
1022 | ||
1023 | wxCHECK_MSG( item, wxEmptyString, | |
1024 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
1025 | ||
1026 | return item->GetText(); | |
1027 | } | |
1028 | ||
1029 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
1030 | { | |
1031 | wxMenuItem *item = FindItem(id); | |
1032 | ||
1033 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
1034 | ||
1035 | item->SetHelp(helpString); | |
1036 | } | |
1037 | ||
1038 | wxString wxMenuBarBase::GetHelpString(int id) const | |
1039 | { | |
1040 | wxMenuItem *item = FindItem(id); | |
1041 | ||
1042 | wxCHECK_MSG( item, wxEmptyString, | |
1043 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
1044 | ||
1045 | return item->GetHelp(); | |
1046 | } | |
1047 | ||
1048 | void wxMenuBarBase::UpdateMenus( void ) | |
1049 | { | |
1050 | wxEvtHandler* source; | |
1051 | wxMenu* menu; | |
1052 | int nCount = GetMenuCount(); | |
1053 | for (int n = 0; n < nCount; n++) | |
1054 | { | |
1055 | menu = GetMenu( n ); | |
1056 | if (menu != NULL) | |
1057 | { | |
1058 | source = menu->GetEventHandler(); | |
1059 | if (source != NULL) | |
1060 | menu->UpdateUI( source ); | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | #endif // wxUSE_MENUS |