1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/menucmn.cpp
3 // Purpose: wxMenu and wxMenuBar methods common to all ports
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxMenuList
)
44 WX_DEFINE_LIST(wxMenuItemList
)
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 static const struct wxKeyName
63 { WXK_DELETE
, wxTRANSLATE("DEL") },
64 { WXK_DELETE
, wxTRANSLATE("DELETE") },
65 { WXK_BACK
, wxTRANSLATE("BACK") },
66 { WXK_INSERT
, wxTRANSLATE("INS") },
67 { WXK_INSERT
, wxTRANSLATE("INSERT") },
68 { WXK_RETURN
, wxTRANSLATE("ENTER") },
69 { WXK_RETURN
, wxTRANSLATE("RETURN") },
70 { WXK_PAGEUP
, wxTRANSLATE("PGUP") },
71 { WXK_PAGEDOWN
, wxTRANSLATE("PGDN") },
72 { WXK_LEFT
, wxTRANSLATE("LEFT") },
73 { WXK_RIGHT
, wxTRANSLATE("RIGHT") },
74 { WXK_UP
, wxTRANSLATE("UP") },
75 { WXK_DOWN
, wxTRANSLATE("DOWN") },
76 { WXK_HOME
, wxTRANSLATE("HOME") },
77 { WXK_END
, wxTRANSLATE("END") },
78 { WXK_SPACE
, wxTRANSLATE("SPACE") },
79 { WXK_TAB
, wxTRANSLATE("TAB") },
80 { WXK_ESCAPE
, wxTRANSLATE("ESC") },
81 { WXK_ESCAPE
, wxTRANSLATE("ESCAPE") },
82 { WXK_CANCEL
, wxTRANSLATE("CANCEL") },
83 { WXK_CLEAR
, wxTRANSLATE("CLEAR") },
84 { WXK_MENU
, wxTRANSLATE("MENU") },
85 { WXK_PAUSE
, wxTRANSLATE("PAUSE") },
86 { WXK_CAPITAL
, wxTRANSLATE("CAPITAL") },
87 { WXK_SELECT
, wxTRANSLATE("SELECT") },
88 { WXK_PRINT
, wxTRANSLATE("PRINT") },
89 { WXK_EXECUTE
, wxTRANSLATE("EXECUTE") },
90 { WXK_SNAPSHOT
, wxTRANSLATE("SNAPSHOT") },
91 { WXK_HELP
, wxTRANSLATE("HELP") },
92 { WXK_ADD
, wxTRANSLATE("ADD") },
93 { WXK_SEPARATOR
, wxTRANSLATE("SEPARATOR") },
94 { WXK_SUBTRACT
, wxTRANSLATE("SUBTRACT") },
95 { WXK_DECIMAL
, wxTRANSLATE("DECIMAL") },
96 { WXK_DIVIDE
, wxTRANSLATE("DIVIDE") },
97 { WXK_NUMLOCK
, wxTRANSLATE("NUM_LOCK") },
98 { WXK_SCROLL
, wxTRANSLATE("SCROLL_LOCK") },
99 { WXK_PAGEUP
, wxTRANSLATE("PAGEUP") },
100 { WXK_PAGEDOWN
, wxTRANSLATE("PAGEDOWN") },
101 { WXK_NUMPAD_SPACE
, wxTRANSLATE("KP_SPACE") },
102 { WXK_NUMPAD_TAB
, wxTRANSLATE("KP_TAB") },
103 { WXK_NUMPAD_ENTER
, wxTRANSLATE("KP_ENTER") },
104 { WXK_NUMPAD_HOME
, wxTRANSLATE("KP_HOME") },
105 { WXK_NUMPAD_LEFT
, wxTRANSLATE("KP_LEFT") },
106 { WXK_NUMPAD_UP
, wxTRANSLATE("KP_UP") },
107 { WXK_NUMPAD_RIGHT
, wxTRANSLATE("KP_RIGHT") },
108 { WXK_NUMPAD_DOWN
, wxTRANSLATE("KP_DOWN") },
109 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PRIOR") },
110 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PAGEUP") },
111 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_NEXT") },
112 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_PAGEDOWN") },
113 { WXK_NUMPAD_END
, wxTRANSLATE("KP_END") },
114 { WXK_NUMPAD_BEGIN
, wxTRANSLATE("KP_BEGIN") },
115 { WXK_NUMPAD_INSERT
, wxTRANSLATE("KP_INSERT") },
116 { WXK_NUMPAD_DELETE
, wxTRANSLATE("KP_DELETE") },
117 { WXK_NUMPAD_EQUAL
, wxTRANSLATE("KP_EQUAL") },
118 { WXK_NUMPAD_MULTIPLY
, wxTRANSLATE("KP_MULTIPLY") },
119 { WXK_NUMPAD_ADD
, wxTRANSLATE("KP_ADD") },
120 { WXK_NUMPAD_SEPARATOR
, wxTRANSLATE("KP_SEPARATOR") },
121 { WXK_NUMPAD_SUBTRACT
, wxTRANSLATE("KP_SUBTRACT") },
122 { WXK_NUMPAD_DECIMAL
, wxTRANSLATE("KP_DECIMAL") },
123 { WXK_NUMPAD_DIVIDE
, wxTRANSLATE("KP_DIVIDE") },
124 { WXK_WINDOWS_LEFT
, wxTRANSLATE("WINDOWS_LEFT") },
125 { WXK_WINDOWS_RIGHT
, wxTRANSLATE("WINDOWS_RIGHT") },
126 { WXK_WINDOWS_MENU
, wxTRANSLATE("WINDOWS_MENU") },
127 { WXK_COMMAND
, wxTRANSLATE("COMMAND") },
130 // return true if the 2 strings refer to the same accel
132 // as accels can be either translated or not, check for both possibilities and
133 // also compare case-insensitively as the key names case doesn't count
134 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
136 return str
.CmpNoCase(accel
) == 0
138 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
143 // return prefixCode+number if the string is of the form "<prefix><number>" and
146 // first and last parameter specify the valid domain for "number" part
148 IsNumberedAccelKey(const wxString
& str
,
149 const wxChar
*prefix
,
150 wxKeyCode prefixCode
,
154 const size_t lenPrefix
= wxStrlen(prefix
);
155 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
159 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
162 if ( num
< first
|| num
> last
)
164 // this must be a mistake, chances that this is a valid name of another
165 // key are vanishingly small
166 wxLogDebug(_T("Invalid key string \"%s\""), str
.c_str());
170 return prefixCode
+ num
- first
;
173 bool wxAcceleratorEntry::FromString(const wxString
& text
)
175 // the parser won't like leading/trailing spaces
176 wxString label
= text
.Strip(wxString::both
);
178 // set to invalid state:
182 // check for accelerators: they are given after '\t'
183 int posTab
= label
.Find(wxT('\t'));
184 if ( posTab
== wxNOT_FOUND
)
187 // parse the accelerator string
188 int accelFlags
= wxACCEL_NORMAL
;
190 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.length(); n
++ )
192 if ( (label
[n
] == '+') || (label
[n
] == '-') )
194 if ( CompareAccelString(current
, wxTRANSLATE("ctrl")) )
195 accelFlags
|= wxACCEL_CTRL
;
196 else if ( CompareAccelString(current
, wxTRANSLATE("alt")) )
197 accelFlags
|= wxACCEL_ALT
;
198 else if ( CompareAccelString(current
, wxTRANSLATE("shift")) )
199 accelFlags
|= wxACCEL_SHIFT
;
200 else // not a recognized modifier name
202 // we may have "Ctrl-+", for example, but we still want to
203 // catch typos like "Crtl-A" so only give the warning if we
204 // have something before the current '+' or '-', else take
205 // it as a literal symbol
206 if ( current
.empty() )
210 // skip clearing it below
215 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
222 else // not special character
224 current
+= (wxChar
) wxTolower(label
[n
]);
229 const size_t len
= current
.length();
233 wxLogDebug(wxT("No accel key found, accel string ignored."));
237 // it's just a letter
238 keyCode
= current
[0U];
240 // if the key is used with any modifiers, make it an uppercase one
241 // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's
242 // used alone as 'a' and 'A' are different
243 if ( accelFlags
!= wxACCEL_NORMAL
)
244 keyCode
= wxToupper(keyCode
);
248 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
252 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
254 const wxKeyName
& kn
= wxKeyNames
[n
];
255 if ( CompareAccelString(current
, kn
.name
) )
264 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
267 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
268 WXK_SPECIAL1
, 1, 20);
272 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
279 wxASSERT_MSG( keyCode
, _T("logic error: should have key code here") );
281 m_flags
= accelFlags
;
286 wxString
wxAcceleratorEntry::ToString() const
290 int flags
= GetFlags();
291 if ( flags
& wxACCEL_ALT
)
293 if ( flags
& wxACCEL_CTRL
)
295 if ( flags
& wxACCEL_SHIFT
)
298 const int code
= GetKeyCode();
300 if ( wxIsalnum(code
) )
301 text
<< (wxChar
)code
;
302 else if ( code
>= WXK_F1
&& code
<= WXK_F12
)
303 text
<< _("F") << code
- WXK_F1
+ 1;
304 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
305 text
<< _("KP_") << code
- WXK_NUMPAD0
;
306 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
307 text
<< _("SPECIAL") << code
- WXK_SPECIAL1
+ 1;
308 else // check the named keys
311 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
313 const wxKeyName
& kn
= wxKeyNames
[n
];
314 if ( code
== kn
.code
)
316 text
<< wxGetTranslation(kn
.name
);
321 wxASSERT_MSG( n
!= WXSIZEOF(wxKeyNames
),
322 wxT("unknown keyboard accelerator code") );
328 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
330 wxAcceleratorEntry
*ret
= new wxAcceleratorEntry();
331 if (ret
->FromString(label
))
338 #endif // wxUSE_ACCEL
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
347 const wxString
& text
,
348 const wxString
& help
,
354 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
356 m_parentMenu
= parentMenu
;
362 if (m_id
== wxID_ANY
)
364 if (m_id
== wxID_SEPARATOR
)
365 m_kind
= wxITEM_SEPARATOR
;
368 wxMenuItemBase::~wxMenuItemBase()
375 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
377 return wxGetAccelFromString(GetText());
380 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
382 wxString text
= m_text
.BeforeFirst(wxT('\t'));
386 text
+= accel
->ToString();
392 #endif // wxUSE_ACCEL
394 bool wxMenuBase::ms_locked
= true;
396 // ----------------------------------------------------------------------------
397 // wxMenu ctor and dtor
398 // ----------------------------------------------------------------------------
400 void wxMenuBase::Init(long style
)
402 m_menuBar
= (wxMenuBar
*)NULL
;
403 m_menuParent
= (wxMenu
*)NULL
;
405 m_invokingWindow
= (wxWindow
*)NULL
;
407 m_clientData
= (void *)NULL
;
408 m_eventHandler
= this;
411 wxMenuBase::~wxMenuBase()
413 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
415 // Actually, in GTK, the submenus have to get deleted first.
418 // ----------------------------------------------------------------------------
419 // wxMenu item adding/removing
420 // ----------------------------------------------------------------------------
422 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
424 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
426 submenu
->SetParent((wxMenu
*)this);
429 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
431 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
433 m_items
.Append(item
);
434 item
->SetMenu((wxMenu
*)this);
435 if ( item
->IsSubMenu() )
437 AddSubMenu(item
->GetSubMenu());
443 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
445 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
447 if ( pos
== GetMenuItemCount() )
449 return DoAppend(item
);
453 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
454 wxT("invalid index in wxMenu::Insert") );
456 return DoInsert(pos
, item
);
460 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
462 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
464 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
465 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
467 m_items
.Insert(node
, item
);
468 item
->SetMenu((wxMenu
*)this);
469 if ( item
->IsSubMenu() )
471 AddSubMenu(item
->GetSubMenu());
477 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
479 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
481 return DoRemove(item
);
484 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
486 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
488 // if we get here, the item is valid or one of Remove() functions is broken
489 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
491 // we detach the item, but we do delete the list node (i.e. don't call
492 // DetachNode() here!)
495 // item isn't attached to anything any more
496 item
->SetMenu((wxMenu
*)NULL
);
497 wxMenu
*submenu
= item
->GetSubMenu();
500 submenu
->SetParent((wxMenu
*)NULL
);
501 if ( submenu
->IsAttached() )
508 bool wxMenuBase::Delete(wxMenuItem
*item
)
510 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
512 return DoDelete(item
);
515 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
517 wxMenuItem
*item2
= DoRemove(item
);
518 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
520 // don't delete the submenu
521 item2
->SetSubMenu((wxMenu
*)NULL
);
528 bool wxMenuBase::Destroy(wxMenuItem
*item
)
530 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
532 return DoDestroy(item
);
535 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
537 wxMenuItem
*item2
= DoRemove(item
);
538 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
545 // ----------------------------------------------------------------------------
546 // wxMenu searching for items
547 // ----------------------------------------------------------------------------
549 // Finds the item id matching the given string, wxNOT_FOUND if not found.
550 int wxMenuBase::FindItem(const wxString
& text
) const
552 wxString label
= wxMenuItem::GetLabelFromText(text
);
553 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
555 node
= node
->GetNext() )
557 wxMenuItem
*item
= node
->GetData();
558 if ( item
->IsSubMenu() )
560 int rc
= item
->GetSubMenu()->FindItem(label
);
561 if ( rc
!= wxNOT_FOUND
)
565 // we execute this code for submenus as well to alllow finding them by
566 // name just like the ordinary items
567 if ( !item
->IsSeparator() )
569 if ( item
->GetLabel() == label
)
570 return item
->GetId();
577 // recursive search for item by id
578 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
583 wxMenuItem
*item
= NULL
;
584 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
586 node
= node
->GetNext() )
588 item
= node
->GetData();
590 if ( item
->GetId() == itemId
)
593 *itemMenu
= (wxMenu
*)this;
595 else if ( item
->IsSubMenu() )
597 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
601 // don't exit the loop
609 // non recursive search
610 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
612 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
613 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
616 for ( pos
= 0; node
; pos
++ )
618 if ( node
->GetData()->GetId() == id
)
620 item
= node
->GetData();
625 node
= node
->GetNext();
630 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
637 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
639 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
640 _T("wxMenu::FindItemByPosition(): invalid menu index") );
642 return m_items
.Item( position
)->GetData();
645 // ----------------------------------------------------------------------------
646 // wxMenu helpers used by derived classes
647 // ----------------------------------------------------------------------------
649 // Update a menu and all submenus recursively. source is the object that has
650 // the update event handlers defined for it. If NULL, the menu or associated
651 // window will be used.
652 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
654 if (GetInvokingWindow())
656 // Don't update menus if the parent
657 // frame is about to get deleted
658 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
659 if (tlw
&& wxPendingDelete
.Member(tlw
))
663 if ( !source
&& GetInvokingWindow() )
664 source
= GetInvokingWindow()->GetEventHandler();
666 source
= GetEventHandler();
670 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
673 wxMenuItem
* item
= node
->GetData();
674 if ( !item
->IsSeparator() )
676 wxWindowID id
= item
->GetId();
677 wxUpdateUIEvent
event(id
);
678 event
.SetEventObject( source
);
680 if ( source
->ProcessEvent(event
) )
682 // if anything changed, update the changed attribute
683 if (event
.GetSetText())
684 SetLabel(id
, event
.GetText());
685 if (event
.GetSetChecked())
686 Check(id
, event
.GetChecked());
687 if (event
.GetSetEnabled())
688 Enable(id
, event
.GetEnabled());
691 // recurse to the submenus
692 if ( item
->GetSubMenu() )
693 item
->GetSubMenu()->UpdateUI(source
);
695 //else: item is a separator (which doesn't process update UI events)
697 node
= node
->GetNext();
701 bool wxMenuBase::SendEvent(int id
, int checked
)
703 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
704 event
.SetEventObject(this);
705 event
.SetInt(checked
);
707 bool processed
= false;
709 // Try the menu's event handler
712 wxEvtHandler
*handler
= GetEventHandler();
714 processed
= handler
->ProcessEvent(event
);
717 // Try the window the menu was popped up from (and up through the
721 const wxMenuBase
*menu
= this;
724 wxWindow
*win
= menu
->GetInvokingWindow();
727 processed
= win
->GetEventHandler()->ProcessEvent(event
);
731 menu
= menu
->GetParent();
738 // ----------------------------------------------------------------------------
739 // wxMenu attaching/detaching to/from menu bar
740 // ----------------------------------------------------------------------------
742 wxMenuBar
* wxMenuBase::GetMenuBar() const
745 return GetParent()->GetMenuBar();
749 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
751 // use Detach() instead!
752 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
754 // use IsAttached() to prevent this from happening
755 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
757 m_menuBar
= (wxMenuBar
*)menubar
;
760 void wxMenuBase::Detach()
762 // use IsAttached() to prevent this from happening
763 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
768 // ----------------------------------------------------------------------------
769 // wxMenu functions forwarded to wxMenuItem
770 // ----------------------------------------------------------------------------
772 void wxMenuBase::Enable( int id
, bool enable
)
774 wxMenuItem
*item
= FindItem(id
);
776 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
778 item
->Enable(enable
);
781 bool wxMenuBase::IsEnabled( int id
) const
783 wxMenuItem
*item
= FindItem(id
);
785 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
787 return item
->IsEnabled();
790 void wxMenuBase::Check( int id
, bool enable
)
792 wxMenuItem
*item
= FindItem(id
);
794 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
799 bool wxMenuBase::IsChecked( int id
) const
801 wxMenuItem
*item
= FindItem(id
);
803 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
805 return item
->IsChecked();
808 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
810 wxMenuItem
*item
= FindItem(id
);
812 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
814 item
->SetText(label
);
817 wxString
wxMenuBase::GetLabel( int id
) const
819 wxMenuItem
*item
= FindItem(id
);
821 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
823 return item
->GetText();
826 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
828 wxMenuItem
*item
= FindItem(id
);
830 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
832 item
->SetHelp( helpString
);
835 wxString
wxMenuBase::GetHelpString( int id
) const
837 wxMenuItem
*item
= FindItem(id
);
839 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
841 return item
->GetHelp();
844 // ----------------------------------------------------------------------------
845 // wxMenuBarBase ctor and dtor
846 // ----------------------------------------------------------------------------
848 wxMenuBarBase::wxMenuBarBase()
851 m_menuBarFrame
= NULL
;
854 wxMenuBarBase::~wxMenuBarBase()
856 WX_CLEAR_LIST(wxMenuList
, m_menus
);
859 // ----------------------------------------------------------------------------
860 // wxMenuBar item access: the base class versions manage m_menus list, the
861 // derived class should reflect the changes in the real menubar
862 // ----------------------------------------------------------------------------
864 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
866 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
867 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
869 return node
->GetData();
872 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
874 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
876 m_menus
.Append(menu
);
882 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
883 const wxString
& title
)
885 if ( pos
== m_menus
.GetCount() )
887 return wxMenuBarBase::Append(menu
, title
);
889 else // not at the end
891 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
893 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
894 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
896 m_menus
.Insert(node
, menu
);
903 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
904 const wxString
& WXUNUSED(title
))
906 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
908 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
909 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
911 wxMenu
*menuOld
= node
->GetData();
920 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
922 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
923 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
925 wxMenu
*menu
= node
->GetData();
932 int wxMenuBarBase::FindMenu(const wxString
& title
) const
934 wxString label
= wxMenuItem::GetLabelFromText(title
);
936 size_t count
= GetMenuCount();
937 for ( size_t i
= 0; i
< count
; i
++ )
939 wxString title2
= GetLabelTop(i
);
940 if ( (title2
== title
) ||
941 (wxMenuItem::GetLabelFromText(title2
) == label
) )
952 // ----------------------------------------------------------------------------
953 // wxMenuBar attaching/detaching to/from the frame
954 // ----------------------------------------------------------------------------
956 void wxMenuBarBase::Attach(wxFrame
*frame
)
958 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
960 m_menuBarFrame
= frame
;
963 void wxMenuBarBase::Detach()
965 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
967 m_menuBarFrame
= NULL
;
970 // ----------------------------------------------------------------------------
971 // wxMenuBar searching for items
972 // ----------------------------------------------------------------------------
974 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
979 wxMenuItem
*item
= NULL
;
980 size_t count
= GetMenuCount(), i
;
981 wxMenuList::const_iterator it
;
982 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
984 item
= (*it
)->FindItem(id
, menu
);
990 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
992 wxString label
= wxMenuItem::GetLabelFromText(menu
);
995 wxMenuList::compatibility_iterator node
;
996 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
998 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
999 return node
->GetData()->FindItem(item
);
1005 // ---------------------------------------------------------------------------
1006 // wxMenuBar functions forwarded to wxMenuItem
1007 // ---------------------------------------------------------------------------
1009 void wxMenuBarBase::Enable(int id
, bool enable
)
1011 wxMenuItem
*item
= FindItem(id
);
1013 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
1015 item
->Enable(enable
);
1018 void wxMenuBarBase::Check(int id
, bool check
)
1020 wxMenuItem
*item
= FindItem(id
);
1022 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
1023 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
1028 bool wxMenuBarBase::IsChecked(int id
) const
1030 wxMenuItem
*item
= FindItem(id
);
1032 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
1034 return item
->IsChecked();
1037 bool wxMenuBarBase::IsEnabled(int id
) const
1039 wxMenuItem
*item
= FindItem(id
);
1041 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1043 return item
->IsEnabled();
1046 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1048 wxMenuItem
*item
= FindItem(id
);
1050 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1052 item
->SetText(label
);
1055 wxString
wxMenuBarBase::GetLabel(int id
) const
1057 wxMenuItem
*item
= FindItem(id
);
1059 wxCHECK_MSG( item
, wxEmptyString
,
1060 wxT("wxMenuBar::GetLabel(): no such item") );
1062 return item
->GetText();
1065 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1067 wxMenuItem
*item
= FindItem(id
);
1069 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1071 item
->SetHelp(helpString
);
1074 wxString
wxMenuBarBase::GetHelpString(int id
) const
1076 wxMenuItem
*item
= FindItem(id
);
1078 wxCHECK_MSG( item
, wxEmptyString
,
1079 wxT("wxMenuBar::GetHelpString(): no such item") );
1081 return item
->GetHelp();
1084 void wxMenuBarBase::UpdateMenus( void )
1086 wxEvtHandler
* source
;
1088 int nCount
= GetMenuCount();
1089 for (int n
= 0; n
< nCount
; n
++)
1091 menu
= GetMenu( n
);
1094 source
= menu
->GetEventHandler();
1096 menu
->UpdateUI( source
);
1101 #endif // wxUSE_MENUS