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("HELP") )
224 else if ( current
== wxT("ADD") )
226 else if ( current
== wxT("SEPARATOR") )
227 keyCode
= WXK_SEPARATOR
;
228 else if ( current
== wxT("SUBTRACT") )
229 keyCode
= WXK_SUBTRACT
;
230 else if ( current
== wxT("DECIMAL") )
231 keyCode
= WXK_DECIMAL
;
232 else if ( current
== wxT("DIVIDE") )
233 keyCode
= WXK_DIVIDE
;
234 else if ( current
== wxT("NUM_LOCK") )
235 keyCode
= WXK_NUMLOCK
;
236 else if ( current
== wxT("SCROLL_LOCK") )
237 keyCode
= WXK_SCROLL
;
238 else if ( current
== wxT("PAGEUP") )
239 keyCode
= WXK_PAGEUP
;
240 else if ( current
== wxT("PAGEDOWN") )
241 keyCode
= WXK_PAGEDOWN
;
242 else if ( current
== wxT("KP_SPACE") )
243 keyCode
= WXK_NUMPAD_SPACE
;
244 else if ( current
== wxT("KP_TAB") )
245 keyCode
= WXK_NUMPAD_TAB
;
246 else if ( current
== wxT("KP_ENTER") )
247 keyCode
= WXK_NUMPAD_ENTER
;
248 else if ( current
== wxT("KP_HOME") )
249 keyCode
= WXK_NUMPAD_HOME
;
250 else if ( current
== wxT("KP_LEFT") )
251 keyCode
= WXK_NUMPAD_LEFT
;
252 else if ( current
== wxT("KP_UP") )
253 keyCode
= WXK_NUMPAD_UP
;
254 else if ( current
== wxT("KP_RIGHT") )
255 keyCode
= WXK_NUMPAD_RIGHT
;
256 else if ( current
== wxT("KP_DOWN") )
257 keyCode
= WXK_NUMPAD_DOWN
;
258 else if ( current
== wxT("KP_PRIOR") )
259 keyCode
= WXK_NUMPAD_PRIOR
;
260 else if ( current
== wxT("KP_PAGEUP") )
261 keyCode
= WXK_NUMPAD_PAGEUP
;
262 else if ( current
== wxT("KP_NEXT;") )
263 keyCode
= WXK_NUMPAD_NEXT
;
264 else if ( current
== wxT("KP_PAGEDOWN") )
265 keyCode
= WXK_NUMPAD_PAGEDOWN
;
266 else if ( current
== wxT("KP_END") )
267 keyCode
= WXK_NUMPAD_END
;
268 else if ( current
== wxT("KP_BEGIN") )
269 keyCode
= WXK_NUMPAD_BEGIN
;
270 else if ( current
== wxT("KP_INSERT") )
271 keyCode
= WXK_NUMPAD_INSERT
;
272 else if ( current
== wxT("KP_DELETE") )
273 keyCode
= WXK_NUMPAD_DELETE
;
274 else if ( current
== wxT("KP_EQUAL") )
275 keyCode
= WXK_NUMPAD_EQUAL
;
276 else if ( current
== wxT("KP_MULTIPLY") )
277 keyCode
= WXK_NUMPAD_MULTIPLY
;
278 else if ( current
== wxT("KP_ADD") )
279 keyCode
= WXK_NUMPAD_ADD
;
280 else if ( current
== wxT("KP_SEPARATOR") )
281 keyCode
= WXK_NUMPAD_SEPARATOR
;
282 else if ( current
== wxT("KP_SUBTRACT") )
283 keyCode
= WXK_NUMPAD_SUBTRACT
;
284 else if ( current
== wxT("KP_DECIMAL") )
285 keyCode
= WXK_NUMPAD_DECIMAL
;
286 else if ( current
== wxT("KP_DIVIDE") )
287 keyCode
= WXK_NUMPAD_DIVIDE
;
288 else if ( current
== wxT("WINDOWS_LEFT") )
289 keyCode
= WXK_WINDOWS_LEFT
;
290 else if ( current
== wxT("WINDOWS_RIGHT") )
291 keyCode
= WXK_WINDOWS_RIGHT
;
292 else if ( current
== wxT("WINDOWS_MENU") )
293 keyCode
= WXK_WINDOWS_MENU
;
294 else if ( current
== wxT("COMMAND") )
295 keyCode
= WXK_COMMAND
;
296 else if ( current
.Left(3) == wxT("KP_") && wxIsdigit(current
[3U]) ) {
297 keyCode
= WXK_NUMPAD0
+ wxAtoi(current
.c_str() + 3); }
298 else if ( current
.Left(7) == wxT("SPECIAL") && wxIsdigit(current
[7U]) ) {
299 keyCode
= WXK_SPECIAL1
+ wxAtoi(current
.c_str() + 7) - 1; }
302 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
311 // we do have something
312 return new wxAcceleratorEntry(accelFlags
, keyCode
);
316 return (wxAcceleratorEntry
*)NULL
;
319 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
321 return wxGetAccelFromString(GetText());
324 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
326 wxString text
= m_text
.BeforeFirst(wxT('\t'));
331 int flags
= accel
->GetFlags();
332 if ( flags
& wxACCEL_ALT
)
334 if ( flags
& wxACCEL_CTRL
)
335 text
+= wxT("Ctrl-");
336 if ( flags
& wxACCEL_SHIFT
)
337 text
+= wxT("Shift-");
339 int code
= accel
->GetKeyCode();
354 text
<< wxT('F') << code
- WXK_F1
+ 1;
357 // if there are any other keys wxGetAccelFromString() may return,
358 // we should process them here
361 if ( wxIsalnum(code
) )
363 text
<< (wxChar
)code
;
368 wxFAIL_MSG( wxT("unknown keyboard accel") );
375 #endif // wxUSE_ACCEL
377 bool wxMenuBase::ms_locked
= true;
379 // ----------------------------------------------------------------------------
380 // wxMenu ctor and dtor
381 // ----------------------------------------------------------------------------
383 void wxMenuBase::Init(long style
)
385 m_menuBar
= (wxMenuBar
*)NULL
;
386 m_menuParent
= (wxMenu
*)NULL
;
388 m_invokingWindow
= (wxWindow
*)NULL
;
390 m_clientData
= (void *)NULL
;
391 m_eventHandler
= this;
394 wxMenuBase::~wxMenuBase()
396 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
398 // Actually, in GTK, the submenus have to get deleted first.
401 // ----------------------------------------------------------------------------
402 // wxMenu item adding/removing
403 // ----------------------------------------------------------------------------
405 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
407 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
409 submenu
->SetParent((wxMenu
*)this);
412 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
414 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
416 m_items
.Append(item
);
417 item
->SetMenu((wxMenu
*)this);
418 if ( item
->IsSubMenu() )
420 AddSubMenu(item
->GetSubMenu());
426 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
428 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
430 if ( pos
== GetMenuItemCount() )
432 return DoAppend(item
);
436 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
437 wxT("invalid index in wxMenu::Insert") );
439 return DoInsert(pos
, item
);
443 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
445 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
447 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
448 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
450 m_items
.Insert(node
, item
);
451 item
->SetMenu((wxMenu
*)this);
452 if ( item
->IsSubMenu() )
454 AddSubMenu(item
->GetSubMenu());
460 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
462 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
464 return DoRemove(item
);
467 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
469 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
471 // if we get here, the item is valid or one of Remove() functions is broken
472 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
474 // we detach the item, but we do delete the list node (i.e. don't call
475 // DetachNode() here!)
478 // item isn't attached to anything any more
479 item
->SetMenu((wxMenu
*)NULL
);
480 wxMenu
*submenu
= item
->GetSubMenu();
483 submenu
->SetParent((wxMenu
*)NULL
);
484 if ( submenu
->IsAttached() )
491 bool wxMenuBase::Delete(wxMenuItem
*item
)
493 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
495 return DoDelete(item
);
498 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
500 wxMenuItem
*item2
= DoRemove(item
);
501 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
503 // don't delete the submenu
504 item2
->SetSubMenu((wxMenu
*)NULL
);
511 bool wxMenuBase::Destroy(wxMenuItem
*item
)
513 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
515 return DoDestroy(item
);
518 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
520 wxMenuItem
*item2
= DoRemove(item
);
521 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
528 // ----------------------------------------------------------------------------
529 // wxMenu searching for items
530 // ----------------------------------------------------------------------------
532 // Finds the item id matching the given string, wxNOT_FOUND if not found.
533 int wxMenuBase::FindItem(const wxString
& text
) const
535 wxString label
= wxMenuItem::GetLabelFromText(text
);
536 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
538 node
= node
->GetNext() )
540 wxMenuItem
*item
= node
->GetData();
541 if ( item
->IsSubMenu() )
543 int rc
= item
->GetSubMenu()->FindItem(label
);
544 if ( rc
!= wxNOT_FOUND
)
548 // we execute this code for submenus as well to alllow finding them by
549 // name just like the ordinary items
550 if ( !item
->IsSeparator() )
552 if ( item
->GetLabel() == label
)
553 return item
->GetId();
560 // recursive search for item by id
561 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
566 wxMenuItem
*item
= NULL
;
567 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
569 node
= node
->GetNext() )
571 item
= node
->GetData();
573 if ( item
->GetId() == itemId
)
576 *itemMenu
= (wxMenu
*)this;
578 else if ( item
->IsSubMenu() )
580 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
584 // don't exit the loop
592 // non recursive search
593 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
595 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
596 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
599 for ( pos
= 0; node
; pos
++ )
601 if ( node
->GetData()->GetId() == id
)
603 item
= node
->GetData();
608 node
= node
->GetNext();
613 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
620 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
622 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
623 _T("wxMenu::FindItemByPosition(): invalid menu index") );
625 return m_items
.Item( position
)->GetData();
628 // ----------------------------------------------------------------------------
629 // wxMenu helpers used by derived classes
630 // ----------------------------------------------------------------------------
632 // Update a menu and all submenus recursively. source is the object that has
633 // the update event handlers defined for it. If NULL, the menu or associated
634 // window will be used.
635 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
637 if (GetInvokingWindow())
639 // Don't update menus if the parent
640 // frame is about to get deleted
641 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
642 if (tlw
&& wxPendingDelete
.Member(tlw
))
646 if ( !source
&& GetInvokingWindow() )
647 source
= GetInvokingWindow()->GetEventHandler();
649 source
= GetEventHandler();
653 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
656 wxMenuItem
* item
= node
->GetData();
657 if ( !item
->IsSeparator() )
659 wxWindowID id
= item
->GetId();
660 wxUpdateUIEvent
event(id
);
661 event
.SetEventObject( source
);
663 if ( source
->ProcessEvent(event
) )
665 // if anything changed, update the changed attribute
666 if (event
.GetSetText())
667 SetLabel(id
, event
.GetText());
668 if (event
.GetSetChecked())
669 Check(id
, event
.GetChecked());
670 if (event
.GetSetEnabled())
671 Enable(id
, event
.GetEnabled());
674 // recurse to the submenus
675 if ( item
->GetSubMenu() )
676 item
->GetSubMenu()->UpdateUI(source
);
678 //else: item is a separator (which doesn't process update UI events)
680 node
= node
->GetNext();
684 bool wxMenuBase::SendEvent(int id
, int checked
)
686 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
687 event
.SetEventObject(this);
688 event
.SetInt(checked
);
690 bool processed
= false;
692 // Try the menu's event handler
695 wxEvtHandler
*handler
= GetEventHandler();
697 processed
= handler
->ProcessEvent(event
);
700 // Try the window the menu was popped up from (and up through the
704 const wxMenuBase
*menu
= this;
707 wxWindow
*win
= menu
->GetInvokingWindow();
710 processed
= win
->GetEventHandler()->ProcessEvent(event
);
714 menu
= menu
->GetParent();
721 // ----------------------------------------------------------------------------
722 // wxMenu attaching/detaching to/from menu bar
723 // ----------------------------------------------------------------------------
725 wxMenuBar
* wxMenuBase::GetMenuBar() const
728 return GetParent()->GetMenuBar();
732 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
734 // use Detach() instead!
735 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
737 // use IsAttached() to prevent this from happening
738 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
740 m_menuBar
= (wxMenuBar
*)menubar
;
743 void wxMenuBase::Detach()
745 // use IsAttached() to prevent this from happening
746 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
751 // ----------------------------------------------------------------------------
752 // wxMenu functions forwarded to wxMenuItem
753 // ----------------------------------------------------------------------------
755 void wxMenuBase::Enable( int id
, bool enable
)
757 wxMenuItem
*item
= FindItem(id
);
759 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
761 item
->Enable(enable
);
764 bool wxMenuBase::IsEnabled( int id
) const
766 wxMenuItem
*item
= FindItem(id
);
768 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
770 return item
->IsEnabled();
773 void wxMenuBase::Check( int id
, bool enable
)
775 wxMenuItem
*item
= FindItem(id
);
777 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
782 bool wxMenuBase::IsChecked( int id
) const
784 wxMenuItem
*item
= FindItem(id
);
786 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
788 return item
->IsChecked();
791 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
793 wxMenuItem
*item
= FindItem(id
);
795 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
797 item
->SetText(label
);
800 wxString
wxMenuBase::GetLabel( int id
) const
802 wxMenuItem
*item
= FindItem(id
);
804 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
806 return item
->GetText();
809 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
811 wxMenuItem
*item
= FindItem(id
);
813 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
815 item
->SetHelp( helpString
);
818 wxString
wxMenuBase::GetHelpString( int id
) const
820 wxMenuItem
*item
= FindItem(id
);
822 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
824 return item
->GetHelp();
827 // ----------------------------------------------------------------------------
828 // wxMenuBarBase ctor and dtor
829 // ----------------------------------------------------------------------------
831 wxMenuBarBase::wxMenuBarBase()
834 m_menuBarFrame
= NULL
;
837 wxMenuBarBase::~wxMenuBarBase()
839 WX_CLEAR_LIST(wxMenuList
, m_menus
);
842 // ----------------------------------------------------------------------------
843 // wxMenuBar item access: the base class versions manage m_menus list, the
844 // derived class should reflect the changes in the real menubar
845 // ----------------------------------------------------------------------------
847 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
849 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
850 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
852 return node
->GetData();
855 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
857 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
859 m_menus
.Append(menu
);
865 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
866 const wxString
& title
)
868 if ( pos
== m_menus
.GetCount() )
870 return wxMenuBarBase::Append(menu
, title
);
872 else // not at the end
874 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
876 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
877 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
879 m_menus
.Insert(node
, menu
);
886 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
887 const wxString
& WXUNUSED(title
))
889 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
891 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
892 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
894 wxMenu
*menuOld
= node
->GetData();
903 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
905 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
906 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
908 wxMenu
*menu
= node
->GetData();
915 int wxMenuBarBase::FindMenu(const wxString
& title
) const
917 wxString label
= wxMenuItem::GetLabelFromText(title
);
919 size_t count
= GetMenuCount();
920 for ( size_t i
= 0; i
< count
; i
++ )
922 wxString title2
= GetLabelTop(i
);
923 if ( (title2
== title
) ||
924 (wxMenuItem::GetLabelFromText(title2
) == label
) )
935 // ----------------------------------------------------------------------------
936 // wxMenuBar attaching/detaching to/from the frame
937 // ----------------------------------------------------------------------------
939 void wxMenuBarBase::Attach(wxFrame
*frame
)
941 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
943 m_menuBarFrame
= frame
;
946 void wxMenuBarBase::Detach()
948 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
950 m_menuBarFrame
= NULL
;
953 // ----------------------------------------------------------------------------
954 // wxMenuBar searching for items
955 // ----------------------------------------------------------------------------
957 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
962 wxMenuItem
*item
= NULL
;
963 size_t count
= GetMenuCount(), i
;
964 wxMenuList::const_iterator it
;
965 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
967 item
= (*it
)->FindItem(id
, menu
);
973 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
975 wxString label
= wxMenuItem::GetLabelFromText(menu
);
978 wxMenuList::compatibility_iterator node
;
979 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
981 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
982 return node
->GetData()->FindItem(item
);
988 // ---------------------------------------------------------------------------
989 // wxMenuBar functions forwarded to wxMenuItem
990 // ---------------------------------------------------------------------------
992 void wxMenuBarBase::Enable(int id
, bool enable
)
994 wxMenuItem
*item
= FindItem(id
);
996 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
998 item
->Enable(enable
);
1001 void wxMenuBarBase::Check(int id
, bool check
)
1003 wxMenuItem
*item
= FindItem(id
);
1005 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
1006 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
1011 bool wxMenuBarBase::IsChecked(int id
) const
1013 wxMenuItem
*item
= FindItem(id
);
1015 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
1017 return item
->IsChecked();
1020 bool wxMenuBarBase::IsEnabled(int id
) const
1022 wxMenuItem
*item
= FindItem(id
);
1024 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1026 return item
->IsEnabled();
1029 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1031 wxMenuItem
*item
= FindItem(id
);
1033 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1035 item
->SetText(label
);
1038 wxString
wxMenuBarBase::GetLabel(int id
) const
1040 wxMenuItem
*item
= FindItem(id
);
1042 wxCHECK_MSG( item
, wxEmptyString
,
1043 wxT("wxMenuBar::GetLabel(): no such item") );
1045 return item
->GetText();
1048 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1050 wxMenuItem
*item
= FindItem(id
);
1052 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1054 item
->SetHelp(helpString
);
1057 wxString
wxMenuBarBase::GetHelpString(int id
) const
1059 wxMenuItem
*item
= FindItem(id
);
1061 wxCHECK_MSG( item
, wxEmptyString
,
1062 wxT("wxMenuBar::GetHelpString(): no such item") );
1064 return item
->GetHelp();
1067 #endif // wxUSE_MENUS