]> git.saurik.com Git - wxWidgets.git/blob - src/common/menucmn.cpp
Include compilation of tests for OpenVMS (part1)
[wxWidgets.git] / src / common / menucmn.cpp
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 #ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #include "wx/menu.h"
33 #include "wx/frame.h"
34 #endif
35
36 #include "wx/stockitem.h"
37
38 // ----------------------------------------------------------------------------
39 // template lists
40 // ----------------------------------------------------------------------------
41
42 #include "wx/listimpl.cpp"
43
44 WX_DEFINE_LIST(wxMenuList)
45 WX_DEFINE_LIST(wxMenuItemList)
46
47 // ============================================================================
48 // implementation
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // wxMenuItemBase
53 // ----------------------------------------------------------------------------
54
55 wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu,
56 int id,
57 const wxString& text,
58 const wxString& help,
59 wxItemKind kind,
60 wxMenu *subMenu)
61 {
62 switch ( id )
63 {
64 case wxID_ANY:
65 m_id = wxWindow::NewControlId();
66 break;
67
68 case wxID_SEPARATOR:
69 m_id = wxID_SEPARATOR;
70
71 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
72 // and it makes sense to omit the following optional parameters,
73 // including the kind one which doesn't default to wxITEM_SEPARATOR,
74 // of course, so override it here
75 kind = wxITEM_SEPARATOR;
76 break;
77
78 case wxID_NONE:
79 // (popup) menu titles in wxMSW use this ID to indicate that
80 // it's not a real menu item, so we don't want the check below to
81 // apply to it
82 m_id = id;
83 break;
84
85 default:
86 // ids are limited to 16 bits under MSW so portable code shouldn't
87 // use ids outside of this range (negative ids generated by wx are
88 // fine though)
89 wxASSERT_MSG( (id >= 0 && id < SHRT_MAX) ||
90 (id >= wxID_AUTO_LOWEST && id <= wxID_AUTO_HIGHEST),
91 wxS("invalid id value") );
92 m_id = id;
93 }
94
95 // notice that parentMenu can be NULL: the item can be attached to the menu
96 // later with SetMenu()
97
98 m_parentMenu = parentMenu;
99 m_subMenu = subMenu;
100 m_isEnabled = true;
101 m_isChecked = false;
102 m_kind = kind;
103
104 SetItemLabel(text);
105 SetHelp(help);
106 }
107
108 wxMenuItemBase::~wxMenuItemBase()
109 {
110 delete m_subMenu;
111 }
112
113 #if wxUSE_ACCEL
114
115 wxAcceleratorEntry *wxMenuItemBase::GetAccel() const
116 {
117 const wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
118
119 return accel.empty() ? NULL : wxAcceleratorEntry::Create(accel);
120 }
121
122 void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel)
123 {
124 wxString text = m_text.BeforeFirst(wxT('\t'));
125 if ( accel )
126 {
127 text += wxT('\t');
128 text += accel->ToString();
129 }
130
131 SetItemLabel(text);
132 }
133
134 #endif // wxUSE_ACCEL
135
136 void wxMenuItemBase::SetItemLabel(const wxString& str)
137 {
138 m_text = str;
139
140 if ( m_text.empty() && !IsSeparator() )
141 {
142 wxASSERT_MSG( wxIsStockID(GetId()),
143 wxT("A non-stock menu item with an empty label?") );
144 m_text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR |
145 wxSTOCK_WITH_MNEMONIC);
146 }
147 }
148
149 void wxMenuItemBase::SetHelp(const wxString& str)
150 {
151 m_help = str;
152
153 if ( m_help.empty() && !IsSeparator() && wxIsStockID(GetId()) )
154 {
155 // get a stock help string
156 m_help = wxGetStockHelpString(GetId());
157 }
158 }
159
160 #ifndef __WXPM__
161 wxString wxMenuItemBase::GetLabelText(const wxString& text)
162 {
163 return wxStripMenuCodes(text);
164 }
165 #endif
166
167 #if WXWIN_COMPATIBILITY_2_8
168 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
169 {
170 return GetLabelText(text);
171 }
172 #endif
173
174 bool wxMenuBase::ms_locked = true;
175
176 // ----------------------------------------------------------------------------
177 // wxMenu ctor and dtor
178 // ----------------------------------------------------------------------------
179
180 void wxMenuBase::Init(long style)
181 {
182 m_menuBar = NULL;
183 m_menuParent = NULL;
184
185 m_invokingWindow = NULL;
186 m_style = style;
187 m_clientData = NULL;
188 m_eventHandler = this;
189 }
190
191 wxMenuBase::~wxMenuBase()
192 {
193 WX_CLEAR_LIST(wxMenuItemList, m_items);
194 }
195
196 // ----------------------------------------------------------------------------
197 // wxMenu item adding/removing
198 // ----------------------------------------------------------------------------
199
200 void wxMenuBase::AddSubMenu(wxMenu *submenu)
201 {
202 wxCHECK_RET( submenu, wxT("can't add a NULL submenu") );
203
204 submenu->SetParent((wxMenu *)this);
205 }
206
207 wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item)
208 {
209 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") );
210
211 m_items.Append(item);
212 item->SetMenu((wxMenu*)this);
213 if ( item->IsSubMenu() )
214 {
215 AddSubMenu(item->GetSubMenu());
216 }
217
218 return item;
219 }
220
221 wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item)
222 {
223 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") );
224
225 if ( pos == GetMenuItemCount() )
226 {
227 return DoAppend(item);
228 }
229 else
230 {
231 wxCHECK_MSG( pos < GetMenuItemCount(), NULL,
232 wxT("invalid index in wxMenu::Insert") );
233
234 return DoInsert(pos, item);
235 }
236 }
237
238 wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item)
239 {
240 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") );
241
242 wxMenuItemList::compatibility_iterator node = m_items.Item(pos);
243 wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") );
244
245 m_items.Insert(node, item);
246 item->SetMenu((wxMenu*)this);
247 if ( item->IsSubMenu() )
248 {
249 AddSubMenu(item->GetSubMenu());
250 }
251
252 return item;
253 }
254
255 wxMenuItem *wxMenuBase::Remove(wxMenuItem *item)
256 {
257 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") );
258
259 return DoRemove(item);
260 }
261
262 wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item)
263 {
264 wxMenuItemList::compatibility_iterator node = m_items.Find(item);
265
266 // if we get here, the item is valid or one of Remove() functions is broken
267 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
268
269 // we detach the item, but we do delete the list node (i.e. don't call
270 // DetachNode() here!)
271 m_items.Erase(node);
272
273 // item isn't attached to anything any more
274 item->SetMenu(NULL);
275 wxMenu *submenu = item->GetSubMenu();
276 if ( submenu )
277 {
278 submenu->SetParent(NULL);
279 if ( submenu->IsAttached() )
280 submenu->Detach();
281 }
282
283 return item;
284 }
285
286 bool wxMenuBase::Delete(wxMenuItem *item)
287 {
288 wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") );
289
290 return DoDelete(item);
291 }
292
293 bool wxMenuBase::DoDelete(wxMenuItem *item)
294 {
295 wxMenuItem *item2 = DoRemove(item);
296 wxCHECK_MSG( item2, false, wxT("failed to delete menu item") );
297
298 // don't delete the submenu
299 item2->SetSubMenu(NULL);
300
301 delete item2;
302
303 return true;
304 }
305
306 bool wxMenuBase::Destroy(wxMenuItem *item)
307 {
308 wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") );
309
310 return DoDestroy(item);
311 }
312
313 bool wxMenuBase::DoDestroy(wxMenuItem *item)
314 {
315 wxMenuItem *item2 = DoRemove(item);
316 wxCHECK_MSG( item2, false, wxT("failed to delete menu item") );
317
318 delete item2;
319
320 return true;
321 }
322
323 // ----------------------------------------------------------------------------
324 // wxMenu searching for items
325 // ----------------------------------------------------------------------------
326
327 // Finds the item id matching the given string, wxNOT_FOUND if not found.
328 int wxMenuBase::FindItem(const wxString& text) const
329 {
330 wxString label = wxMenuItem::GetLabelText(text);
331 for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
332 node;
333 node = node->GetNext() )
334 {
335 wxMenuItem *item = node->GetData();
336 if ( item->IsSubMenu() )
337 {
338 int rc = item->GetSubMenu()->FindItem(label);
339 if ( rc != wxNOT_FOUND )
340 return rc;
341 }
342
343 // we execute this code for submenus as well to alllow finding them by
344 // name just like the ordinary items
345 if ( !item->IsSeparator() )
346 {
347 if ( item->GetItemLabelText() == label )
348 return item->GetId();
349 }
350 }
351
352 return wxNOT_FOUND;
353 }
354
355 // recursive search for item by id
356 wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const
357 {
358 if ( itemMenu )
359 *itemMenu = NULL;
360
361 wxMenuItem *item = NULL;
362 for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
363 node && !item;
364 node = node->GetNext() )
365 {
366 item = node->GetData();
367
368 if ( item->GetId() == itemId )
369 {
370 if ( itemMenu )
371 *itemMenu = (wxMenu *)this;
372 }
373 else if ( item->IsSubMenu() )
374 {
375 item = item->GetSubMenu()->FindItem(itemId, itemMenu);
376 }
377 else
378 {
379 // don't exit the loop
380 item = NULL;
381 }
382 }
383
384 return item;
385 }
386
387 // non recursive search
388 wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const
389 {
390 wxMenuItem *item = NULL;
391 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
392
393 size_t pos;
394 for ( pos = 0; node; pos++ )
395 {
396 if ( node->GetData()->GetId() == id )
397 {
398 item = node->GetData();
399
400 break;
401 }
402
403 node = node->GetNext();
404 }
405
406 if ( ppos )
407 {
408 *ppos = item ? pos : (size_t)wxNOT_FOUND;
409 }
410
411 return item;
412 }
413
414 // find by position
415 wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const
416 {
417 wxCHECK_MSG( position < m_items.GetCount(), NULL,
418 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
419
420 return m_items.Item( position )->GetData();
421 }
422
423 // ----------------------------------------------------------------------------
424 // wxMenu helpers used by derived classes
425 // ----------------------------------------------------------------------------
426
427 // Update a menu and all submenus recursively. source is the object that has
428 // the update event handlers defined for it. If NULL, the menu or associated
429 // window will be used.
430 void wxMenuBase::UpdateUI(wxEvtHandler* source)
431 {
432 if (GetInvokingWindow())
433 {
434 // Don't update menus if the parent
435 // frame is about to get deleted
436 wxWindow *tlw = wxGetTopLevelParent( GetInvokingWindow() );
437 if (tlw && wxPendingDelete.Member(tlw))
438 return;
439 }
440
441 if ( !source && GetInvokingWindow() )
442 source = GetInvokingWindow()->GetEventHandler();
443 if ( !source )
444 source = GetEventHandler();
445 if ( !source )
446 source = this;
447
448 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
449 while ( node )
450 {
451 wxMenuItem* item = node->GetData();
452 if ( !item->IsSeparator() )
453 {
454 wxWindowID id = item->GetId();
455 wxUpdateUIEvent event(id);
456 event.SetEventObject( source );
457
458 if ( source->ProcessEvent(event) )
459 {
460 // if anything changed, update the changed attribute
461 if (event.GetSetText())
462 SetLabel(id, event.GetText());
463 if (event.GetSetChecked())
464 Check(id, event.GetChecked());
465 if (event.GetSetEnabled())
466 Enable(id, event.GetEnabled());
467 }
468
469 // recurse to the submenus
470 if ( item->GetSubMenu() )
471 item->GetSubMenu()->UpdateUI(source);
472 }
473 //else: item is a separator (which doesn't process update UI events)
474
475 node = node->GetNext();
476 }
477 }
478
479 bool wxMenuBase::SendEvent(int id, int checked)
480 {
481 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id);
482 event.SetEventObject(this);
483 event.SetInt(checked);
484
485 bool processed = false;
486
487 // Try the menu's event handler first
488 wxEvtHandler *handler = GetEventHandler();
489 if ( handler )
490 processed = handler->SafelyProcessEvent(event);
491
492 // Try the window the menu was popped up from or its menu bar belongs to
493 if ( !processed )
494 {
495 wxWindow * const win = GetWindow();
496 if ( win )
497 processed = win->HandleWindowEvent(event);
498 }
499
500 return processed;
501 }
502
503 // ----------------------------------------------------------------------------
504 // wxMenu attaching/detaching to/from menu bar
505 // ----------------------------------------------------------------------------
506
507 wxMenuBar* wxMenuBase::GetMenuBar() const
508 {
509 if(GetParent())
510 return GetParent()->GetMenuBar();
511 return m_menuBar;
512 }
513
514 void wxMenuBase::Attach(wxMenuBarBase *menubar)
515 {
516 // use Detach() instead!
517 wxASSERT_MSG( menubar, wxT("menu can't be attached to NULL menubar") );
518
519 // use IsAttached() to prevent this from happening
520 wxASSERT_MSG( !m_menuBar, wxT("attaching menu twice?") );
521
522 m_menuBar = (wxMenuBar *)menubar;
523 }
524
525 void wxMenuBase::Detach()
526 {
527 // use IsAttached() to prevent this from happening
528 wxASSERT_MSG( m_menuBar, wxT("detaching unattached menu?") );
529
530 m_menuBar = NULL;
531 }
532
533 // ----------------------------------------------------------------------------
534 // wxMenu invoking window handling
535 // ----------------------------------------------------------------------------
536
537 void wxMenuBase::SetInvokingWindow(wxWindow *win)
538 {
539 wxASSERT_MSG( !GetParent(),
540 "should only be called for top level popup menus" );
541 wxASSERT_MSG( !IsAttached(),
542 "menus attached to menu bar can't have invoking window" );
543
544 m_invokingWindow = win;
545 }
546
547 wxWindow *wxMenuBase::GetWindow() const
548 {
549 // only the top level menus have non-NULL invoking window or a pointer to
550 // the menu bar so recurse upwards until we find it
551 const wxMenuBase *menu = this;
552 while ( menu->GetParent() )
553 {
554 menu = menu->GetParent();
555 }
556
557 return menu->GetMenuBar() ? menu->GetMenuBar()->GetFrame()
558 : menu->GetInvokingWindow();
559 }
560
561 // ----------------------------------------------------------------------------
562 // wxMenu functions forwarded to wxMenuItem
563 // ----------------------------------------------------------------------------
564
565 void wxMenuBase::Enable( int id, bool enable )
566 {
567 wxMenuItem *item = FindItem(id);
568
569 wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") );
570
571 item->Enable(enable);
572 }
573
574 bool wxMenuBase::IsEnabled( int id ) const
575 {
576 wxMenuItem *item = FindItem(id);
577
578 wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") );
579
580 return item->IsEnabled();
581 }
582
583 void wxMenuBase::Check( int id, bool enable )
584 {
585 wxMenuItem *item = FindItem(id);
586
587 wxCHECK_RET( item, wxT("wxMenu::Check: no such item") );
588
589 item->Check(enable);
590 }
591
592 bool wxMenuBase::IsChecked( int id ) const
593 {
594 wxMenuItem *item = FindItem(id);
595
596 wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") );
597
598 return item->IsChecked();
599 }
600
601 void wxMenuBase::SetLabel( int id, const wxString &label )
602 {
603 wxMenuItem *item = FindItem(id);
604
605 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
606
607 item->SetItemLabel(label);
608 }
609
610 wxString wxMenuBase::GetLabel( int id ) const
611 {
612 wxMenuItem *item = FindItem(id);
613
614 wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") );
615
616 return item->GetItemLabel();
617 }
618
619 void wxMenuBase::SetHelpString( int id, const wxString& helpString )
620 {
621 wxMenuItem *item = FindItem(id);
622
623 wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") );
624
625 item->SetHelp( helpString );
626 }
627
628 wxString wxMenuBase::GetHelpString( int id ) const
629 {
630 wxMenuItem *item = FindItem(id);
631
632 wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") );
633
634 return item->GetHelp();
635 }
636
637 // ----------------------------------------------------------------------------
638 // wxMenuBarBase ctor and dtor
639 // ----------------------------------------------------------------------------
640
641 wxMenuBarBase::wxMenuBarBase()
642 {
643 // not attached yet
644 m_menuBarFrame = NULL;
645 }
646
647 wxMenuBarBase::~wxMenuBarBase()
648 {
649 WX_CLEAR_LIST(wxMenuList, m_menus);
650 }
651
652 // ----------------------------------------------------------------------------
653 // wxMenuBar item access: the base class versions manage m_menus list, the
654 // derived class should reflect the changes in the real menubar
655 // ----------------------------------------------------------------------------
656
657 wxMenu *wxMenuBarBase::GetMenu(size_t pos) const
658 {
659 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
660 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") );
661
662 return node->GetData();
663 }
664
665 bool wxMenuBarBase::Append(wxMenu *menu, const wxString& title)
666 {
667 wxCHECK_MSG( menu, false, wxT("can't append NULL menu") );
668 wxCHECK_MSG( !title.empty(), false, wxT("can't append menu with empty title") );
669
670 m_menus.Append(menu);
671 menu->Attach(this);
672
673 return true;
674 }
675
676 bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu,
677 const wxString& title)
678 {
679 if ( pos == m_menus.GetCount() )
680 {
681 return wxMenuBarBase::Append(menu, title);
682 }
683 else // not at the end
684 {
685 wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") );
686
687 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
688 wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") );
689
690 m_menus.Insert(node, menu);
691 menu->Attach(this);
692
693 return true;
694 }
695 }
696
697 wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu,
698 const wxString& WXUNUSED(title))
699 {
700 wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") );
701
702 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
703 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") );
704
705 wxMenu *menuOld = node->GetData();
706 node->SetData(menu);
707
708 menu->Attach(this);
709 menuOld->Detach();
710
711 return menuOld;
712 }
713
714 wxMenu *wxMenuBarBase::Remove(size_t pos)
715 {
716 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
717 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") );
718
719 wxMenu *menu = node->GetData();
720 m_menus.Erase(node);
721 menu->Detach();
722
723 return menu;
724 }
725
726 int wxMenuBarBase::FindMenu(const wxString& title) const
727 {
728 wxString label = wxMenuItem::GetLabelText(title);
729
730 size_t count = GetMenuCount();
731 for ( size_t i = 0; i < count; i++ )
732 {
733 wxString title2 = GetMenuLabel(i);
734 if ( (title2 == title) ||
735 (wxMenuItem::GetLabelText(title2) == label) )
736 {
737 // found
738 return (int)i;
739 }
740 }
741
742 return wxNOT_FOUND;
743
744 }
745
746 // ----------------------------------------------------------------------------
747 // wxMenuBar attaching/detaching to/from the frame
748 // ----------------------------------------------------------------------------
749
750 void wxMenuBarBase::Attach(wxFrame *frame)
751 {
752 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
753
754 m_menuBarFrame = frame;
755 }
756
757 void wxMenuBarBase::Detach()
758 {
759 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
760
761 m_menuBarFrame = NULL;
762 }
763
764 // ----------------------------------------------------------------------------
765 // wxMenuBar searching for items
766 // ----------------------------------------------------------------------------
767
768 wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const
769 {
770 if ( menu )
771 *menu = NULL;
772
773 wxMenuItem *item = NULL;
774 size_t count = GetMenuCount(), i;
775 wxMenuList::const_iterator it;
776 for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ )
777 {
778 item = (*it)->FindItem(id, menu);
779 }
780
781 return item;
782 }
783
784 int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const
785 {
786 wxString label = wxMenuItem::GetLabelText(menu);
787
788 int i = 0;
789 wxMenuList::compatibility_iterator node;
790 for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ )
791 {
792 if ( label == wxMenuItem::GetLabelText(GetMenuLabel(i)) )
793 return node->GetData()->FindItem(item);
794 }
795
796 return wxNOT_FOUND;
797 }
798
799 // ---------------------------------------------------------------------------
800 // wxMenuBar functions forwarded to wxMenuItem
801 // ---------------------------------------------------------------------------
802
803 void wxMenuBarBase::Enable(int id, bool enable)
804 {
805 wxMenuItem *item = FindItem(id);
806
807 wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
808
809 item->Enable(enable);
810 }
811
812 void wxMenuBarBase::Check(int id, bool check)
813 {
814 wxMenuItem *item = FindItem(id);
815
816 wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
817 wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
818
819 item->Check(check);
820 }
821
822 bool wxMenuBarBase::IsChecked(int id) const
823 {
824 wxMenuItem *item = FindItem(id);
825
826 wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") );
827
828 return item->IsChecked();
829 }
830
831 bool wxMenuBarBase::IsEnabled(int id) const
832 {
833 wxMenuItem *item = FindItem(id);
834
835 wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") );
836
837 return item->IsEnabled();
838 }
839
840 void wxMenuBarBase::SetLabel(int id, const wxString& label)
841 {
842 wxMenuItem *item = FindItem(id);
843
844 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
845
846 item->SetItemLabel(label);
847 }
848
849 wxString wxMenuBarBase::GetLabel(int id) const
850 {
851 wxMenuItem *item = FindItem(id);
852
853 wxCHECK_MSG( item, wxEmptyString,
854 wxT("wxMenuBar::GetLabel(): no such item") );
855
856 return item->GetItemLabel();
857 }
858
859 void wxMenuBarBase::SetHelpString(int id, const wxString& helpString)
860 {
861 wxMenuItem *item = FindItem(id);
862
863 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
864
865 item->SetHelp(helpString);
866 }
867
868 wxString wxMenuBarBase::GetHelpString(int id) const
869 {
870 wxMenuItem *item = FindItem(id);
871
872 wxCHECK_MSG( item, wxEmptyString,
873 wxT("wxMenuBar::GetHelpString(): no such item") );
874
875 return item->GetHelp();
876 }
877
878 void wxMenuBarBase::UpdateMenus()
879 {
880 wxEvtHandler* source;
881 wxMenu* menu;
882 int nCount = GetMenuCount();
883 for (int n = 0; n < nCount; n++)
884 {
885 menu = GetMenu( n );
886 if (menu != NULL)
887 {
888 source = menu->GetEventHandler();
889 if (source != NULL)
890 menu->UpdateUI( source );
891 }
892 }
893 }
894
895 #if WXWIN_COMPATIBILITY_2_8
896 // get or change the label of the menu at given position
897 void wxMenuBarBase::SetLabelTop(size_t pos, const wxString& label)
898 {
899 SetMenuLabel(pos, label);
900 }
901
902 wxString wxMenuBarBase::GetLabelTop(size_t pos) const
903 {
904 return GetMenuLabelText(pos);
905 }
906 #endif
907
908 #endif // wxUSE_MENUS