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