]>
Commit | Line | Data |
---|---|---|
3dfac970 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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) wxWindows team | |
55d99c7a | 9 | // Licence: wxWindows licence |
3dfac970 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "menubase.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_MENUS |
32 | ||
33 | #include <ctype.h> | |
34 | ||
3dfac970 | 35 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
36 | #include "wx/intl.h" |
37 | #include "wx/log.h" | |
3dfac970 VZ |
38 | #include "wx/menu.h" |
39 | #endif | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // template lists | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | #include "wx/listimpl.cpp" | |
717a57c2 | 46 | |
3dfac970 | 47 | WX_DEFINE_LIST(wxMenuList); |
717a57c2 | 48 | WX_DEFINE_LIST(wxMenuItemList); |
3dfac970 VZ |
49 | |
50 | // ============================================================================ | |
51 | // implementation | |
52 | // ============================================================================ | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
717a57c2 VZ |
55 | // wxMenuItem |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
d65c269b VZ |
58 | wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu, |
59 | int id, | |
60 | const wxString& text, | |
61 | const wxString& help, | |
62 | wxItemKind kind, | |
63 | wxMenu *subMenu) | |
64 | : m_text(text), | |
65 | m_help(help) | |
66 | { | |
67 | wxASSERT_MSG( parentMenu != NULL, wxT("menuitem should have a menu") ); | |
68 | ||
69 | m_parentMenu = parentMenu; | |
70 | m_subMenu = subMenu; | |
71 | m_isEnabled = TRUE; | |
72 | m_isChecked = FALSE; | |
73 | m_id = id; | |
74 | m_kind = kind; | |
75 | } | |
76 | ||
717a57c2 VZ |
77 | wxMenuItemBase::~wxMenuItemBase() |
78 | { | |
1e6feb95 | 79 | delete m_subMenu; |
717a57c2 VZ |
80 | } |
81 | ||
82 | #if wxUSE_ACCEL | |
83 | ||
c53207a5 VS |
84 | static inline bool CompareAccelString(const wxString& str, const wxChar *accel) |
85 | { | |
86 | #if wxUSE_INTL | |
87 | return str == accel || str == wxGetTranslation(accel); | |
88 | #else | |
89 | return str == accel; | |
90 | #endif | |
91 | } | |
92 | ||
1e6feb95 VZ |
93 | // return wxAcceleratorEntry for the given menu string or NULL if none |
94 | // specified | |
95 | wxAcceleratorEntry *wxGetAccelFromString(const wxString& label) | |
96 | { | |
97 | // check for accelerators: they are given after '\t' | |
98 | int posTab = label.Find(wxT('\t')); | |
99 | if ( posTab != wxNOT_FOUND ) { | |
100 | // parse the accelerator string | |
101 | int keyCode = 0; | |
102 | int accelFlags = wxACCEL_NORMAL; | |
103 | wxString current; | |
104 | for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) { | |
105 | if ( (label[n] == '+') || (label[n] == '-') ) { | |
c53207a5 | 106 | if ( CompareAccelString(current, wxTRANSLATE("ctrl")) ) |
1e6feb95 | 107 | accelFlags |= wxACCEL_CTRL; |
c53207a5 | 108 | else if ( CompareAccelString(current, wxTRANSLATE("alt")) ) |
1e6feb95 | 109 | accelFlags |= wxACCEL_ALT; |
c53207a5 | 110 | else if ( CompareAccelString(current, wxTRANSLATE("shift")) ) |
1e6feb95 VZ |
111 | accelFlags |= wxACCEL_SHIFT; |
112 | else { | |
a070d8ce VZ |
113 | // we may have "Ctrl-+", for example, but we still want to |
114 | // catch typos like "Crtl-A" so only give the warning if we | |
115 | // have something before the current '+' or '-', else take | |
116 | // it as a literal symbol | |
117 | if ( current.empty() ) | |
118 | { | |
119 | current += label[n]; | |
120 | ||
121 | // skip clearing it below | |
122 | continue; | |
123 | } | |
124 | else | |
125 | { | |
126 | wxLogDebug(wxT("Unknown accel modifier: '%s'"), | |
127 | current.c_str()); | |
128 | } | |
1e6feb95 VZ |
129 | } |
130 | ||
a070d8ce | 131 | current.clear(); |
1e6feb95 VZ |
132 | } |
133 | else { | |
134 | current += wxTolower(label[n]); | |
135 | } | |
136 | } | |
137 | ||
138 | if ( current.IsEmpty() ) { | |
139 | wxLogDebug(wxT("No accel key found, accel string ignored.")); | |
140 | } | |
141 | else { | |
142 | if ( current.Len() == 1 ) { | |
143 | // it's a letter | |
c921fd59 GD |
144 | keyCode = current[0U]; |
145 | ||
146 | // Only call wxToupper if control, alt, or shift is held down, | |
147 | // otherwise lower case accelerators won't work. | |
148 | if (accelFlags != wxACCEL_NORMAL) { | |
149 | keyCode = wxToupper(keyCode); | |
150 | } | |
1e6feb95 VZ |
151 | } |
152 | else { | |
153 | // is it a function key? | |
154 | if ( current[0U] == 'f' && isdigit(current[1U]) && | |
155 | (current.Len() == 2 || | |
156 | (current.Len() == 3 && isdigit(current[2U]))) ) { | |
157 | int n; | |
158 | wxSscanf(current.c_str() + 1, wxT("%d"), &n); | |
159 | ||
160 | keyCode = WXK_F1 + n - 1; | |
161 | } | |
162 | else { | |
163 | // several special cases | |
164 | current.MakeUpper(); | |
41b78190 | 165 | if ( current == wxT("DEL") ) |
1e6feb95 | 166 | keyCode = WXK_DELETE; |
41b78190 | 167 | else if ( current == wxT("DELETE") ) |
1e6feb95 | 168 | keyCode = WXK_DELETE; |
41b78190 | 169 | else if ( current == wxT("INS") ) |
1e6feb95 | 170 | keyCode = WXK_INSERT; |
41b78190 | 171 | else if ( current == wxT("INSERT") ) |
1e6feb95 | 172 | keyCode = WXK_INSERT; |
41b78190 | 173 | else if ( current == wxT("ENTER") || current == wxT("RETURN") ) |
2b5f62a0 | 174 | keyCode = WXK_RETURN; |
41b78190 | 175 | else if ( current == wxT("PGUP") ) |
2b5f62a0 | 176 | keyCode = WXK_PRIOR; |
41b78190 | 177 | else if ( current == wxT("PGDN") ) |
2b5f62a0 | 178 | keyCode = WXK_NEXT; |
41b78190 | 179 | else if ( current == wxT("LEFT") ) |
2b5f62a0 | 180 | keyCode = WXK_LEFT; |
41b78190 | 181 | else if ( current == wxT("RIGHT") ) |
2b5f62a0 | 182 | keyCode = WXK_RIGHT; |
41b78190 | 183 | else if ( current == wxT("UP") ) |
2b5f62a0 | 184 | keyCode = WXK_UP; |
41b78190 | 185 | else if ( current == wxT("DOWN") ) |
2b5f62a0 | 186 | keyCode = WXK_DOWN; |
41b78190 | 187 | else if ( current == wxT("HOME") ) |
2b5f62a0 | 188 | keyCode = WXK_HOME; |
41b78190 | 189 | else if ( current == wxT("END") ) |
2b5f62a0 | 190 | keyCode = WXK_END; |
41b78190 | 191 | else if ( current == wxT("SPACE") ) |
2b5f62a0 | 192 | keyCode = WXK_SPACE; |
41b78190 | 193 | else if ( current == wxT("TAB") ) |
2b5f62a0 | 194 | keyCode = WXK_TAB; |
73baf01a | 195 | else if ( current == wxT("ESC") || current == wxT("ESCAPE") ) |
41b78190 | 196 | keyCode = WXK_ESCAPE; |
1e6feb95 VZ |
197 | else |
198 | { | |
199 | wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."), | |
200 | current.c_str()); | |
201 | } | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | if ( keyCode ) { | |
207 | // we do have something | |
208 | return new wxAcceleratorEntry(accelFlags, keyCode); | |
209 | } | |
210 | } | |
211 | ||
212 | return (wxAcceleratorEntry *)NULL; | |
213 | } | |
214 | ||
215 | wxAcceleratorEntry *wxMenuItemBase::GetAccel() const | |
216 | { | |
217 | return wxGetAccelFromString(GetText()); | |
218 | } | |
219 | ||
717a57c2 VZ |
220 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) |
221 | { | |
222 | wxString text = m_text.BeforeFirst(wxT('\t')); | |
223 | if ( accel ) | |
224 | { | |
225 | text += wxT('\t'); | |
226 | ||
227 | int flags = accel->GetFlags(); | |
228 | if ( flags & wxACCEL_ALT ) | |
229 | text += wxT("Alt-"); | |
230 | if ( flags & wxACCEL_CTRL ) | |
231 | text += wxT("Ctrl-"); | |
232 | if ( flags & wxACCEL_SHIFT ) | |
233 | text += wxT("Shift-"); | |
234 | ||
235 | int code = accel->GetKeyCode(); | |
236 | switch ( code ) | |
237 | { | |
238 | case WXK_F1: | |
239 | case WXK_F2: | |
240 | case WXK_F3: | |
241 | case WXK_F4: | |
242 | case WXK_F5: | |
243 | case WXK_F6: | |
244 | case WXK_F7: | |
245 | case WXK_F8: | |
246 | case WXK_F9: | |
247 | case WXK_F10: | |
248 | case WXK_F11: | |
249 | case WXK_F12: | |
250 | text << wxT('F') << code - WXK_F1 + 1; | |
251 | break; | |
252 | ||
253 | // if there are any other keys wxGetAccelFromString() may return, | |
254 | // we should process them here | |
255 | ||
256 | default: | |
257 | if ( wxIsalnum(code) ) | |
258 | { | |
259 | text << (wxChar)code; | |
260 | ||
261 | break; | |
262 | } | |
263 | ||
264 | wxFAIL_MSG( wxT("unknown keyboard accel") ); | |
265 | } | |
266 | } | |
267 | ||
268 | SetText(text); | |
269 | } | |
270 | ||
271 | #endif // wxUSE_ACCEL | |
272 | ||
273 | // ---------------------------------------------------------------------------- | |
274 | // wxMenu ctor and dtor | |
275 | // ---------------------------------------------------------------------------- | |
276 | ||
277 | void wxMenuBase::Init(long style) | |
278 | { | |
279 | m_items.DeleteContents(TRUE); | |
280 | ||
281 | m_menuBar = (wxMenuBar *)NULL; | |
282 | m_menuParent = (wxMenu *)NULL; | |
283 | ||
284 | m_invokingWindow = (wxWindow *)NULL; | |
285 | m_style = style; | |
286 | m_clientData = (void *)NULL; | |
287 | m_eventHandler = this; | |
288 | } | |
289 | ||
290 | wxMenuBase::~wxMenuBase() | |
291 | { | |
a583e623 | 292 | // nothing to do, wxMenuItemList dtor will delete the menu items. |
1e6feb95 VZ |
293 | |
294 | // Actually, in GTK, the submenus have to get deleted first. | |
717a57c2 VZ |
295 | } |
296 | ||
297 | // ---------------------------------------------------------------------------- | |
298 | // wxMenu item adding/removing | |
299 | // ---------------------------------------------------------------------------- | |
300 | ||
1e6feb95 VZ |
301 | void wxMenuBase::AddSubMenu(wxMenu *submenu) |
302 | { | |
303 | wxCHECK_RET( submenu, _T("can't add a NULL submenu") ); | |
304 | ||
305 | if ( m_menuBar ) | |
306 | { | |
307 | submenu->Attach(m_menuBar); | |
308 | } | |
309 | ||
310 | submenu->SetParent((wxMenu *)this); | |
311 | } | |
312 | ||
717a57c2 VZ |
313 | bool wxMenuBase::DoAppend(wxMenuItem *item) |
314 | { | |
315 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Append()") ); | |
316 | ||
317 | m_items.Append(item); | |
1e93ca17 | 318 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
319 | if ( item->IsSubMenu() ) |
320 | { | |
321 | AddSubMenu(item->GetSubMenu()); | |
322 | } | |
717a57c2 VZ |
323 | |
324 | return TRUE; | |
325 | } | |
326 | ||
327 | bool wxMenuBase::Insert(size_t pos, wxMenuItem *item) | |
328 | { | |
329 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert") ); | |
717a57c2 | 330 | |
32db328c VZ |
331 | if ( pos == GetMenuItemCount() ) |
332 | { | |
333 | return DoAppend(item); | |
334 | } | |
335 | else | |
336 | { | |
337 | wxCHECK_MSG( pos < GetMenuItemCount(), FALSE, | |
338 | wxT("invalid index in wxMenu::Insert") ); | |
339 | ||
340 | return DoInsert(pos, item); | |
341 | } | |
717a57c2 VZ |
342 | } |
343 | ||
344 | bool wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) | |
345 | { | |
346 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert()") ); | |
347 | ||
348 | wxMenuItemList::Node *node = m_items.Item(pos); | |
349 | wxCHECK_MSG( node, FALSE, wxT("invalid index in wxMenu::Insert()") ); | |
350 | ||
351 | m_items.Insert(node, item); | |
1e93ca17 | 352 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
353 | if ( item->IsSubMenu() ) |
354 | { | |
355 | AddSubMenu(item->GetSubMenu()); | |
356 | } | |
717a57c2 VZ |
357 | |
358 | return TRUE; | |
359 | } | |
360 | ||
361 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) | |
362 | { | |
363 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); | |
364 | ||
365 | return DoRemove(item); | |
366 | } | |
367 | ||
368 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) | |
369 | { | |
370 | wxMenuItemList::Node *node = m_items.Find(item); | |
371 | ||
372 | // if we get here, the item is valid or one of Remove() functions is broken | |
373 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
374 | ||
375 | // we detach the item, but we do delete the list node (i.e. don't call | |
376 | // DetachNode() here!) | |
377 | node->SetData((wxMenuItem *)NULL); // to prevent it from deleting the item | |
378 | m_items.DeleteNode(node); | |
379 | ||
380 | // item isn't attached to anything any more | |
1e93ca17 | 381 | item->SetMenu((wxMenu *)NULL); |
717a57c2 VZ |
382 | wxMenu *submenu = item->GetSubMenu(); |
383 | if ( submenu ) | |
384 | { | |
385 | submenu->SetParent((wxMenu *)NULL); | |
386 | } | |
387 | ||
388 | return item; | |
389 | } | |
390 | ||
391 | bool wxMenuBase::Delete(wxMenuItem *item) | |
392 | { | |
39d03faf | 393 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Delete") ); |
717a57c2 VZ |
394 | |
395 | return DoDelete(item); | |
396 | } | |
397 | ||
398 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
399 | { | |
400 | wxMenuItem *item2 = DoRemove(item); | |
401 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); | |
402 | ||
403 | // don't delete the submenu | |
404 | item2->SetSubMenu((wxMenu *)NULL); | |
405 | ||
406 | delete item2; | |
407 | ||
408 | return TRUE; | |
409 | } | |
410 | ||
411 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
412 | { | |
39d03faf | 413 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Destroy") ); |
717a57c2 VZ |
414 | |
415 | return DoDestroy(item); | |
416 | } | |
417 | ||
418 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
419 | { | |
420 | wxMenuItem *item2 = DoRemove(item); | |
421 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); | |
422 | ||
423 | delete item2; | |
424 | ||
425 | return TRUE; | |
426 | } | |
427 | ||
428 | // ---------------------------------------------------------------------------- | |
429 | // wxMenu searching for items | |
430 | // ---------------------------------------------------------------------------- | |
431 | ||
432 | // Finds the item id matching the given string, -1 if not found. | |
433 | int wxMenuBase::FindItem(const wxString& text) const | |
434 | { | |
3b59cdbf | 435 | wxString label = wxMenuItem::GetLabelFromText(text); |
717a57c2 VZ |
436 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); |
437 | node; | |
438 | node = node->GetNext() ) | |
439 | { | |
440 | wxMenuItem *item = node->GetData(); | |
441 | if ( item->IsSubMenu() ) | |
442 | { | |
443 | int rc = item->GetSubMenu()->FindItem(label); | |
444 | if ( rc != wxNOT_FOUND ) | |
445 | return rc; | |
446 | } | |
adb21613 VZ |
447 | |
448 | // we execute this code for submenus as well to alllow finding them by | |
449 | // name just like the ordinary items | |
450 | if ( !item->IsSeparator() ) | |
717a57c2 VZ |
451 | { |
452 | if ( item->GetLabel() == label ) | |
453 | return item->GetId(); | |
454 | } | |
455 | } | |
456 | ||
457 | return wxNOT_FOUND; | |
458 | } | |
459 | ||
460 | // recursive search for item by id | |
461 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const | |
462 | { | |
463 | if ( itemMenu ) | |
464 | *itemMenu = NULL; | |
465 | ||
466 | wxMenuItem *item = NULL; | |
467 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); | |
468 | node && !item; | |
469 | node = node->GetNext() ) | |
470 | { | |
471 | item = node->GetData(); | |
472 | ||
473 | if ( item->GetId() == itemId ) | |
474 | { | |
475 | if ( itemMenu ) | |
476 | *itemMenu = (wxMenu *)this; | |
477 | } | |
478 | else if ( item->IsSubMenu() ) | |
479 | { | |
480 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); | |
481 | } | |
482 | else | |
483 | { | |
484 | // don't exit the loop | |
485 | item = NULL; | |
486 | } | |
487 | } | |
488 | ||
489 | return item; | |
490 | } | |
491 | ||
492 | // non recursive search | |
493 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const | |
494 | { | |
495 | wxMenuItem *item = (wxMenuItem *)NULL; | |
496 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
497 | ||
498 | size_t pos; | |
499 | for ( pos = 0; node; pos++ ) | |
500 | { | |
1dddf838 VZ |
501 | if ( node->GetData()->GetId() == id ) |
502 | { | |
503 | item = node->GetData(); | |
504 | ||
717a57c2 | 505 | break; |
1dddf838 | 506 | } |
717a57c2 VZ |
507 | |
508 | node = node->GetNext(); | |
509 | } | |
510 | ||
511 | if ( ppos ) | |
512 | { | |
1987af7e | 513 | *ppos = item ? pos : (size_t)wxNOT_FOUND; |
717a57c2 VZ |
514 | } |
515 | ||
516 | return item; | |
517 | } | |
518 | ||
01ebf752 JS |
519 | // find by position |
520 | wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const | |
521 | { | |
20aed026 VZ |
522 | wxCHECK_MSG( position < m_items.GetCount(), NULL, |
523 | _T("wxMenu::FindItemByPosition(): invalid menu index") ); | |
524 | ||
525 | return m_items.Item( position )->GetData(); | |
01ebf752 JS |
526 | } |
527 | ||
717a57c2 | 528 | // ---------------------------------------------------------------------------- |
1e6feb95 | 529 | // wxMenu helpers used by derived classes |
717a57c2 VZ |
530 | // ---------------------------------------------------------------------------- |
531 | ||
532 | // Update a menu and all submenus recursively. source is the object that has | |
533 | // the update event handlers defined for it. If NULL, the menu or associated | |
534 | // window will be used. | |
535 | void wxMenuBase::UpdateUI(wxEvtHandler* source) | |
536 | { | |
537 | if ( !source && GetInvokingWindow() ) | |
538 | source = GetInvokingWindow()->GetEventHandler(); | |
539 | if ( !source ) | |
540 | source = GetEventHandler(); | |
541 | if ( !source ) | |
542 | source = this; | |
543 | ||
544 | wxMenuItemList::Node* node = GetMenuItems().GetFirst(); | |
545 | while ( node ) | |
546 | { | |
547 | wxMenuItem* item = node->GetData(); | |
548 | if ( !item->IsSeparator() ) | |
549 | { | |
550 | wxWindowID id = item->GetId(); | |
551 | wxUpdateUIEvent event(id); | |
552 | event.SetEventObject( source ); | |
553 | ||
554 | if ( source->ProcessEvent(event) ) | |
555 | { | |
18afa2ac | 556 | // if anything changed, update the changed attribute |
717a57c2 VZ |
557 | if (event.GetSetText()) |
558 | SetLabel(id, event.GetText()); | |
559 | if (event.GetSetChecked()) | |
560 | Check(id, event.GetChecked()); | |
561 | if (event.GetSetEnabled()) | |
562 | Enable(id, event.GetEnabled()); | |
563 | } | |
564 | ||
565 | // recurse to the submenus | |
566 | if ( item->GetSubMenu() ) | |
567 | item->GetSubMenu()->UpdateUI(source); | |
568 | } | |
18afa2ac | 569 | //else: item is a separator (which doesn't process update UI events) |
717a57c2 VZ |
570 | |
571 | node = node->GetNext(); | |
572 | } | |
573 | } | |
574 | ||
1e6feb95 VZ |
575 | bool wxMenuBase::SendEvent(int id, int checked) |
576 | { | |
577 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id); | |
578 | event.SetEventObject(this); | |
579 | event.SetInt(checked); | |
580 | ||
581 | bool processed = FALSE; | |
582 | ||
1e6feb95 VZ |
583 | // Try the menu's event handler |
584 | if ( !processed ) | |
585 | { | |
586 | wxEvtHandler *handler = GetEventHandler(); | |
587 | if ( handler ) | |
588 | processed = handler->ProcessEvent(event); | |
589 | } | |
590 | ||
591 | // Try the window the menu was popped up from (and up through the | |
592 | // hierarchy) | |
593 | if ( !processed ) | |
594 | { | |
595 | const wxMenuBase *menu = this; | |
596 | while ( menu ) | |
597 | { | |
598 | wxWindow *win = menu->GetInvokingWindow(); | |
599 | if ( win ) | |
600 | { | |
601 | processed = win->GetEventHandler()->ProcessEvent(event); | |
602 | break; | |
603 | } | |
604 | ||
605 | menu = menu->GetParent(); | |
606 | } | |
607 | } | |
608 | ||
609 | return processed; | |
610 | } | |
611 | ||
612 | // ---------------------------------------------------------------------------- | |
613 | // wxMenu attaching/detaching to/from menu bar | |
614 | // ---------------------------------------------------------------------------- | |
615 | ||
616 | void wxMenuBase::Attach(wxMenuBarBase *menubar) | |
617 | { | |
618 | // use Detach() instead! | |
619 | wxASSERT_MSG( menubar, _T("menu can't be attached to NULL menubar") ); | |
620 | ||
621 | // use IsAttached() to prevent this from happening | |
622 | wxASSERT_MSG( !m_menuBar, _T("attaching menu twice?") ); | |
623 | ||
624 | m_menuBar = (wxMenuBar *)menubar; | |
625 | } | |
626 | ||
627 | void wxMenuBase::Detach() | |
628 | { | |
629 | // use IsAttached() to prevent this from happening | |
630 | wxASSERT_MSG( m_menuBar, _T("detaching unattached menu?") ); | |
631 | ||
632 | m_menuBar = NULL; | |
633 | } | |
634 | ||
717a57c2 VZ |
635 | // ---------------------------------------------------------------------------- |
636 | // wxMenu functions forwarded to wxMenuItem | |
637 | // ---------------------------------------------------------------------------- | |
638 | ||
639 | void wxMenuBase::Enable( int id, bool enable ) | |
640 | { | |
641 | wxMenuItem *item = FindItem(id); | |
642 | ||
643 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
644 | ||
645 | item->Enable(enable); | |
646 | } | |
647 | ||
648 | bool wxMenuBase::IsEnabled( int id ) const | |
649 | { | |
650 | wxMenuItem *item = FindItem(id); | |
651 | ||
652 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") ); | |
653 | ||
654 | return item->IsEnabled(); | |
655 | } | |
656 | ||
657 | void wxMenuBase::Check( int id, bool enable ) | |
658 | { | |
659 | wxMenuItem *item = FindItem(id); | |
660 | ||
661 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
662 | ||
663 | item->Check(enable); | |
664 | } | |
665 | ||
666 | bool wxMenuBase::IsChecked( int id ) const | |
667 | { | |
668 | wxMenuItem *item = FindItem(id); | |
669 | ||
670 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") ); | |
671 | ||
672 | return item->IsChecked(); | |
673 | } | |
674 | ||
675 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
676 | { | |
677 | wxMenuItem *item = FindItem(id); | |
678 | ||
679 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
680 | ||
681 | item->SetText(label); | |
682 | } | |
683 | ||
684 | wxString wxMenuBase::GetLabel( int id ) const | |
685 | { | |
686 | wxMenuItem *item = FindItem(id); | |
687 | ||
688 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") ); | |
689 | ||
690 | return item->GetText(); | |
691 | } | |
692 | ||
693 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
694 | { | |
695 | wxMenuItem *item = FindItem(id); | |
696 | ||
697 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
698 | ||
699 | item->SetHelp( helpString ); | |
700 | } | |
701 | ||
702 | wxString wxMenuBase::GetHelpString( int id ) const | |
703 | { | |
704 | wxMenuItem *item = FindItem(id); | |
705 | ||
706 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") ); | |
707 | ||
708 | return item->GetHelp(); | |
709 | } | |
710 | ||
711 | // ---------------------------------------------------------------------------- | |
712 | // wxMenuBarBase ctor and dtor | |
3dfac970 VZ |
713 | // ---------------------------------------------------------------------------- |
714 | ||
715 | wxMenuBarBase::wxMenuBarBase() | |
716 | { | |
717 | // we own the menus when we get them | |
718 | m_menus.DeleteContents(TRUE); | |
1e6feb95 VZ |
719 | |
720 | // not attached yet | |
721 | m_menuBarFrame = NULL; | |
3dfac970 VZ |
722 | } |
723 | ||
724 | wxMenuBarBase::~wxMenuBarBase() | |
725 | { | |
726 | // nothing to do, the list will delete the menus because of the call to | |
727 | // DeleteContents() above | |
728 | } | |
729 | ||
730 | // ---------------------------------------------------------------------------- | |
731 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
732 | // derived class should reflect the changes in the real menubar | |
733 | // ---------------------------------------------------------------------------- | |
734 | ||
735 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
736 | { | |
737 | wxMenuList::Node *node = m_menus.Item(pos); | |
738 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); | |
739 | ||
740 | return node->GetData(); | |
741 | } | |
742 | ||
743 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title)) | |
744 | { | |
745 | wxCHECK_MSG( menu, FALSE, wxT("can't append NULL menu") ); | |
746 | ||
747 | m_menus.Append(menu); | |
1e6feb95 | 748 | menu->Attach(this); |
3dfac970 VZ |
749 | |
750 | return TRUE; | |
751 | } | |
752 | ||
753 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
32db328c | 754 | const wxString& title) |
3dfac970 | 755 | { |
32db328c VZ |
756 | if ( pos == m_menus.GetCount() ) |
757 | { | |
186baeb2 | 758 | return wxMenuBarBase::Append(menu, title); |
32db328c | 759 | } |
1e6feb95 | 760 | else // not at the end |
32db328c VZ |
761 | { |
762 | wxCHECK_MSG( menu, FALSE, wxT("can't insert NULL menu") ); | |
3dfac970 | 763 | |
32db328c VZ |
764 | wxMenuList::Node *node = m_menus.Item(pos); |
765 | wxCHECK_MSG( node, FALSE, wxT("bad index in wxMenuBar::Insert()") ); | |
3dfac970 | 766 | |
32db328c | 767 | m_menus.Insert(node, menu); |
1e6feb95 | 768 | menu->Attach(this); |
3dfac970 | 769 | |
32db328c VZ |
770 | return TRUE; |
771 | } | |
3dfac970 VZ |
772 | } |
773 | ||
774 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
775 | const wxString& WXUNUSED(title)) | |
776 | { | |
777 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
778 | ||
779 | wxMenuList::Node *node = m_menus.Item(pos); | |
780 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); | |
781 | ||
782 | wxMenu *menuOld = node->GetData(); | |
783 | node->SetData(menu); | |
784 | ||
1e6feb95 VZ |
785 | menu->Attach(this); |
786 | menuOld->Detach(); | |
787 | ||
3dfac970 VZ |
788 | return menuOld; |
789 | } | |
790 | ||
791 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
792 | { | |
793 | wxMenuList::Node *node = m_menus.Item(pos); | |
794 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); | |
795 | ||
796 | node = m_menus.DetachNode(node); | |
797 | wxCHECK( node, NULL ); // unexpected | |
798 | wxMenu *menu = node->GetData(); | |
1e6feb95 | 799 | menu->Detach(); |
3dfac970 VZ |
800 | |
801 | delete node; | |
802 | ||
803 | return menu; | |
804 | } | |
805 | ||
270e8b6a | 806 | int wxMenuBarBase::FindMenu(const wxString& title) const |
52130557 VZ |
807 | { |
808 | wxString label = wxMenuItem::GetLabelFromText(title); | |
809 | ||
810 | size_t count = GetMenuCount(); | |
811 | for ( size_t i = 0; i < count; i++ ) | |
812 | { | |
813 | wxString title2 = GetLabelTop(i); | |
814 | if ( (title2 == title) || | |
815 | (wxMenuItem::GetLabelFromText(title2) == label) ) | |
816 | { | |
817 | // found | |
2b5f62a0 | 818 | return (int)i; |
52130557 VZ |
819 | } |
820 | } | |
821 | ||
822 | return wxNOT_FOUND; | |
823 | ||
824 | } | |
825 | ||
1e6feb95 VZ |
826 | // ---------------------------------------------------------------------------- |
827 | // wxMenuBar attaching/detaching to/from the frame | |
828 | // ---------------------------------------------------------------------------- | |
829 | ||
830 | void wxMenuBarBase::Attach(wxFrame *frame) | |
831 | { | |
832 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
833 | ||
834 | m_menuBarFrame = frame; | |
835 | } | |
836 | ||
837 | void wxMenuBarBase::Detach() | |
838 | { | |
839 | wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") ); | |
840 | ||
841 | m_menuBarFrame = NULL; | |
842 | } | |
843 | ||
844 | // ---------------------------------------------------------------------------- | |
845 | // wxMenuBar searching for items | |
846 | // ---------------------------------------------------------------------------- | |
847 | ||
848 | wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const | |
849 | { | |
850 | if ( menu ) | |
851 | *menu = NULL; | |
852 | ||
853 | wxMenuItem *item = NULL; | |
854 | size_t count = GetMenuCount(); | |
855 | for ( size_t i = 0; !item && (i < count); i++ ) | |
856 | { | |
857 | item = m_menus[i]->FindItem(id, menu); | |
858 | } | |
859 | ||
860 | return item; | |
861 | } | |
862 | ||
863 | int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const | |
864 | { | |
865 | wxString label = wxMenuItem::GetLabelFromText(menu); | |
866 | ||
867 | int i = 0; | |
868 | wxMenuList::Node *node; | |
869 | for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ ) | |
870 | { | |
871 | if ( label == wxMenuItem::GetLabelFromText(GetLabelTop(i)) ) | |
872 | return node->GetData()->FindItem(item); | |
873 | } | |
874 | ||
875 | return wxNOT_FOUND; | |
876 | } | |
877 | ||
3dfac970 VZ |
878 | // --------------------------------------------------------------------------- |
879 | // wxMenuBar functions forwarded to wxMenuItem | |
880 | // --------------------------------------------------------------------------- | |
881 | ||
882 | void wxMenuBarBase::Enable(int id, bool enable) | |
883 | { | |
884 | wxMenuItem *item = FindItem(id); | |
885 | ||
886 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
887 | ||
888 | item->Enable(enable); | |
889 | } | |
890 | ||
891 | void wxMenuBarBase::Check(int id, bool check) | |
892 | { | |
893 | wxMenuItem *item = FindItem(id); | |
894 | ||
895 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
896 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
897 | ||
898 | item->Check(check); | |
899 | } | |
900 | ||
901 | bool wxMenuBarBase::IsChecked(int id) const | |
902 | { | |
903 | wxMenuItem *item = FindItem(id); | |
904 | ||
905 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") ); | |
906 | ||
907 | return item->IsChecked(); | |
908 | } | |
909 | ||
910 | bool wxMenuBarBase::IsEnabled(int id) const | |
911 | { | |
912 | wxMenuItem *item = FindItem(id); | |
913 | ||
914 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") ); | |
915 | ||
916 | return item->IsEnabled(); | |
917 | } | |
918 | ||
919 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
920 | { | |
921 | wxMenuItem *item = FindItem(id); | |
922 | ||
923 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
924 | ||
925 | item->SetText(label); | |
926 | } | |
927 | ||
928 | wxString wxMenuBarBase::GetLabel(int id) const | |
929 | { | |
930 | wxMenuItem *item = FindItem(id); | |
931 | ||
932 | wxCHECK_MSG( item, wxEmptyString, | |
933 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
934 | ||
935 | return item->GetText(); | |
936 | } | |
937 | ||
938 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
939 | { | |
940 | wxMenuItem *item = FindItem(id); | |
941 | ||
942 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
943 | ||
944 | item->SetHelp(helpString); | |
945 | } | |
946 | ||
947 | wxString wxMenuBarBase::GetHelpString(int id) const | |
948 | { | |
949 | wxMenuItem *item = FindItem(id); | |
950 | ||
951 | wxCHECK_MSG( item, wxEmptyString, | |
952 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
953 | ||
954 | return item->GetHelp(); | |
955 | } | |
956 | ||
1e6feb95 | 957 | #endif // wxUSE_MENUS |