1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "menubase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxMenuList
);
48 WX_DEFINE_LIST(wxMenuItemList
);
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
67 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
69 m_parentMenu
= parentMenu
;
77 if (m_id
== wxID_SEPARATOR
)
78 m_kind
= wxITEM_SEPARATOR
;
81 wxMenuItemBase::~wxMenuItemBase()
88 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
91 return str
== accel
|| str
== wxGetTranslation(accel
);
97 // return wxAcceleratorEntry for the given menu string or NULL if none
99 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
101 // wxPrintf( wxT("label %s\n"), label.c_str() );
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
108 int accelFlags
= wxACCEL_NORMAL
;
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
;
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() )
127 // skip clearing it below
132 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
140 current
+= (wxChar
) wxTolower(label
[n
]);
144 if ( current
.empty() ) {
145 wxLogDebug(wxT("No accel key found, accel string ignored."));
148 if ( current
.Len() == 1 ) {
150 keyCode
= current
[0U];
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
);
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;
166 // several special cases
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") )
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") )
182 else if ( current
== wxT("PGDN") )
184 else if ( current
== wxT("LEFT") )
186 else if ( current
== wxT("RIGHT") )
188 else if ( current
== wxT("UP") )
190 else if ( current
== wxT("DOWN") )
192 else if ( current
== wxT("HOME") )
194 else if ( current
== wxT("END") )
196 else if ( current
== wxT("SPACE") )
198 else if ( current
== wxT("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") )
206 else if ( current
== wxT("MENU") )
208 else if ( current
== wxT("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") )
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") )
222 else if ( current
== wxT("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;
300 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
309 // we do have something
310 return new wxAcceleratorEntry(accelFlags
, keyCode
);
314 return (wxAcceleratorEntry
*)NULL
;
317 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
319 return wxGetAccelFromString(GetText());
322 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
324 wxString text
= m_text
.BeforeFirst(wxT('\t'));
329 int flags
= accel
->GetFlags();
330 if ( flags
& wxACCEL_ALT
)
332 if ( flags
& wxACCEL_CTRL
)
333 text
+= wxT("Ctrl-");
334 if ( flags
& wxACCEL_SHIFT
)
335 text
+= wxT("Shift-");
337 int code
= accel
->GetKeyCode();
352 text
<< wxT('F') << code
- WXK_F1
+ 1;
355 // if there are any other keys wxGetAccelFromString() may return,
356 // we should process them here
359 if ( wxIsalnum(code
) )
361 text
<< (wxChar
)code
;
366 wxFAIL_MSG( wxT("unknown keyboard accel") );
373 #endif // wxUSE_ACCEL
375 bool wxMenuBase::ms_locked
= true;
377 // ----------------------------------------------------------------------------
378 // wxMenu ctor and dtor
379 // ----------------------------------------------------------------------------
381 void wxMenuBase::Init(long style
)
383 m_menuBar
= (wxMenuBar
*)NULL
;
384 m_menuParent
= (wxMenu
*)NULL
;
386 m_invokingWindow
= (wxWindow
*)NULL
;
388 m_clientData
= (void *)NULL
;
389 m_eventHandler
= this;
392 wxMenuBase::~wxMenuBase()
394 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
396 // Actually, in GTK, the submenus have to get deleted first.
399 // ----------------------------------------------------------------------------
400 // wxMenu item adding/removing
401 // ----------------------------------------------------------------------------
403 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
405 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
407 submenu
->SetParent((wxMenu
*)this);
410 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
412 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
414 m_items
.Append(item
);
415 item
->SetMenu((wxMenu
*)this);
416 if ( item
->IsSubMenu() )
418 AddSubMenu(item
->GetSubMenu());
424 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
426 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
428 if ( pos
== GetMenuItemCount() )
430 return DoAppend(item
);
434 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
435 wxT("invalid index in wxMenu::Insert") );
437 return DoInsert(pos
, item
);
441 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
443 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
445 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
446 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
448 m_items
.Insert(node
, item
);
449 item
->SetMenu((wxMenu
*)this);
450 if ( item
->IsSubMenu() )
452 AddSubMenu(item
->GetSubMenu());
458 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
460 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
462 return DoRemove(item
);
465 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
467 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
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") );
472 // we detach the item, but we do delete the list node (i.e. don't call
473 // DetachNode() here!)
476 // item isn't attached to anything any more
477 item
->SetMenu((wxMenu
*)NULL
);
478 wxMenu
*submenu
= item
->GetSubMenu();
481 submenu
->SetParent((wxMenu
*)NULL
);
482 if ( submenu
->IsAttached() )
489 bool wxMenuBase::Delete(wxMenuItem
*item
)
491 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
493 return DoDelete(item
);
496 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
498 wxMenuItem
*item2
= DoRemove(item
);
499 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
501 // don't delete the submenu
502 item2
->SetSubMenu((wxMenu
*)NULL
);
509 bool wxMenuBase::Destroy(wxMenuItem
*item
)
511 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
513 return DoDestroy(item
);
516 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
518 wxMenuItem
*item2
= DoRemove(item
);
519 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
526 // ----------------------------------------------------------------------------
527 // wxMenu searching for items
528 // ----------------------------------------------------------------------------
530 // Finds the item id matching the given string, wxNOT_FOUND if not found.
531 int wxMenuBase::FindItem(const wxString
& text
) const
533 wxString label
= wxMenuItem::GetLabelFromText(text
);
534 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
536 node
= node
->GetNext() )
538 wxMenuItem
*item
= node
->GetData();
539 if ( item
->IsSubMenu() )
541 int rc
= item
->GetSubMenu()->FindItem(label
);
542 if ( rc
!= wxNOT_FOUND
)
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() )
550 if ( item
->GetLabel() == label
)
551 return item
->GetId();
558 // recursive search for item by id
559 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
564 wxMenuItem
*item
= NULL
;
565 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
567 node
= node
->GetNext() )
569 item
= node
->GetData();
571 if ( item
->GetId() == itemId
)
574 *itemMenu
= (wxMenu
*)this;
576 else if ( item
->IsSubMenu() )
578 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
582 // don't exit the loop
590 // non recursive search
591 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
593 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
594 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
597 for ( pos
= 0; node
; pos
++ )
599 if ( node
->GetData()->GetId() == id
)
601 item
= node
->GetData();
606 node
= node
->GetNext();
611 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
618 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
620 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
621 _T("wxMenu::FindItemByPosition(): invalid menu index") );
623 return m_items
.Item( position
)->GetData();
626 // ----------------------------------------------------------------------------
627 // wxMenu helpers used by derived classes
628 // ----------------------------------------------------------------------------
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
)
635 if (GetInvokingWindow())
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
))
644 if ( !source
&& GetInvokingWindow() )
645 source
= GetInvokingWindow()->GetEventHandler();
647 source
= GetEventHandler();
651 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
654 wxMenuItem
* item
= node
->GetData();
655 if ( !item
->IsSeparator() )
657 wxWindowID id
= item
->GetId();
658 wxUpdateUIEvent
event(id
);
659 event
.SetEventObject( source
);
661 if ( source
->ProcessEvent(event
) )
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());
672 // recurse to the submenus
673 if ( item
->GetSubMenu() )
674 item
->GetSubMenu()->UpdateUI(source
);
676 //else: item is a separator (which doesn't process update UI events)
678 node
= node
->GetNext();
682 bool wxMenuBase::SendEvent(int id
, int checked
)
684 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
685 event
.SetEventObject(this);
686 event
.SetInt(checked
);
688 bool processed
= false;
690 // Try the menu's event handler
693 wxEvtHandler
*handler
= GetEventHandler();
695 processed
= handler
->ProcessEvent(event
);
698 // Try the window the menu was popped up from (and up through the
702 const wxMenuBase
*menu
= this;
705 wxWindow
*win
= menu
->GetInvokingWindow();
708 processed
= win
->GetEventHandler()->ProcessEvent(event
);
712 menu
= menu
->GetParent();
719 // ----------------------------------------------------------------------------
720 // wxMenu attaching/detaching to/from menu bar
721 // ----------------------------------------------------------------------------
723 wxMenuBar
* wxMenuBase::GetMenuBar() const
726 return GetParent()->GetMenuBar();
730 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
732 // use Detach() instead!
733 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
735 // use IsAttached() to prevent this from happening
736 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
738 m_menuBar
= (wxMenuBar
*)menubar
;
741 void wxMenuBase::Detach()
743 // use IsAttached() to prevent this from happening
744 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
749 // ----------------------------------------------------------------------------
750 // wxMenu functions forwarded to wxMenuItem
751 // ----------------------------------------------------------------------------
753 void wxMenuBase::Enable( int id
, bool enable
)
755 wxMenuItem
*item
= FindItem(id
);
757 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
759 item
->Enable(enable
);
762 bool wxMenuBase::IsEnabled( int id
) const
764 wxMenuItem
*item
= FindItem(id
);
766 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
768 return item
->IsEnabled();
771 void wxMenuBase::Check( int id
, bool enable
)
773 wxMenuItem
*item
= FindItem(id
);
775 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
780 bool wxMenuBase::IsChecked( int id
) const
782 wxMenuItem
*item
= FindItem(id
);
784 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
786 return item
->IsChecked();
789 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
791 wxMenuItem
*item
= FindItem(id
);
793 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
795 item
->SetText(label
);
798 wxString
wxMenuBase::GetLabel( int id
) const
800 wxMenuItem
*item
= FindItem(id
);
802 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
804 return item
->GetText();
807 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
809 wxMenuItem
*item
= FindItem(id
);
811 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
813 item
->SetHelp( helpString
);
816 wxString
wxMenuBase::GetHelpString( int id
) const
818 wxMenuItem
*item
= FindItem(id
);
820 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
822 return item
->GetHelp();
825 // ----------------------------------------------------------------------------
826 // wxMenuBarBase ctor and dtor
827 // ----------------------------------------------------------------------------
829 wxMenuBarBase::wxMenuBarBase()
832 m_menuBarFrame
= NULL
;
835 wxMenuBarBase::~wxMenuBarBase()
837 WX_CLEAR_LIST(wxMenuList
, m_menus
);
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 // ----------------------------------------------------------------------------
845 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
847 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
848 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
850 return node
->GetData();
853 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
855 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
857 m_menus
.Append(menu
);
863 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
864 const wxString
& title
)
866 if ( pos
== m_menus
.GetCount() )
868 return wxMenuBarBase::Append(menu
, title
);
870 else // not at the end
872 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
874 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
875 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
877 m_menus
.Insert(node
, menu
);
884 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
885 const wxString
& WXUNUSED(title
))
887 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
889 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
890 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
892 wxMenu
*menuOld
= node
->GetData();
901 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
903 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
904 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
906 wxMenu
*menu
= node
->GetData();
913 int wxMenuBarBase::FindMenu(const wxString
& title
) const
915 wxString label
= wxMenuItem::GetLabelFromText(title
);
917 size_t count
= GetMenuCount();
918 for ( size_t i
= 0; i
< count
; i
++ )
920 wxString title2
= GetLabelTop(i
);
921 if ( (title2
== title
) ||
922 (wxMenuItem::GetLabelFromText(title2
) == label
) )
933 // ----------------------------------------------------------------------------
934 // wxMenuBar attaching/detaching to/from the frame
935 // ----------------------------------------------------------------------------
937 void wxMenuBarBase::Attach(wxFrame
*frame
)
939 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
941 m_menuBarFrame
= frame
;
944 void wxMenuBarBase::Detach()
946 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
948 m_menuBarFrame
= NULL
;
951 // ----------------------------------------------------------------------------
952 // wxMenuBar searching for items
953 // ----------------------------------------------------------------------------
955 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
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
++ )
965 item
= (*it
)->FindItem(id
, menu
);
971 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
973 wxString label
= wxMenuItem::GetLabelFromText(menu
);
976 wxMenuList::compatibility_iterator node
;
977 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
979 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
980 return node
->GetData()->FindItem(item
);
986 // ---------------------------------------------------------------------------
987 // wxMenuBar functions forwarded to wxMenuItem
988 // ---------------------------------------------------------------------------
990 void wxMenuBarBase::Enable(int id
, bool enable
)
992 wxMenuItem
*item
= FindItem(id
);
994 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
996 item
->Enable(enable
);
999 void wxMenuBarBase::Check(int id
, bool check
)
1001 wxMenuItem
*item
= FindItem(id
);
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") );
1009 bool wxMenuBarBase::IsChecked(int id
) const
1011 wxMenuItem
*item
= FindItem(id
);
1013 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
1015 return item
->IsChecked();
1018 bool wxMenuBarBase::IsEnabled(int id
) const
1020 wxMenuItem
*item
= FindItem(id
);
1022 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1024 return item
->IsEnabled();
1027 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1029 wxMenuItem
*item
= FindItem(id
);
1031 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1033 item
->SetText(label
);
1036 wxString
wxMenuBarBase::GetLabel(int id
) const
1038 wxMenuItem
*item
= FindItem(id
);
1040 wxCHECK_MSG( item
, wxEmptyString
,
1041 wxT("wxMenuBar::GetLabel(): no such item") );
1043 return item
->GetText();
1046 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1048 wxMenuItem
*item
= FindItem(id
);
1050 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1052 item
->SetHelp(helpString
);
1055 wxString
wxMenuBarBase::GetHelpString(int id
) const
1057 wxMenuItem
*item
= FindItem(id
);
1059 wxCHECK_MSG( item
, wxEmptyString
,
1060 wxT("wxMenuBar::GetHelpString(): no such item") );
1062 return item
->GetHelp();
1065 #endif // wxUSE_MENUS