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 // ----------------------------------------------------------------------------
54 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
63 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
65 m_parentMenu
= parentMenu
;
73 if (m_id
== wxID_SEPARATOR
)
74 m_kind
= wxITEM_SEPARATOR
;
77 wxMenuItemBase::~wxMenuItemBase()
84 static const struct wxKeyName
90 { WXK_DELETE
, wxTRANSLATE("DEL") },
91 { WXK_DELETE
, wxTRANSLATE("DELETE") },
92 { WXK_BACK
, wxTRANSLATE("BACK") },
93 { WXK_INSERT
, wxTRANSLATE("INS") },
94 { WXK_INSERT
, wxTRANSLATE("INSERT") },
95 { WXK_RETURN
, wxTRANSLATE("ENTER") },
96 { WXK_RETURN
, wxTRANSLATE("RETURN") },
97 { WXK_PAGEUP
, wxTRANSLATE("PGUP") },
98 { WXK_PAGEDOWN
, wxTRANSLATE("PGDN") },
99 { WXK_LEFT
, wxTRANSLATE("LEFT") },
100 { WXK_RIGHT
, wxTRANSLATE("RIGHT") },
101 { WXK_UP
, wxTRANSLATE("UP") },
102 { WXK_DOWN
, wxTRANSLATE("DOWN") },
103 { WXK_HOME
, wxTRANSLATE("HOME") },
104 { WXK_END
, wxTRANSLATE("END") },
105 { WXK_SPACE
, wxTRANSLATE("SPACE") },
106 { WXK_TAB
, wxTRANSLATE("TAB") },
107 { WXK_ESCAPE
, wxTRANSLATE("ESC") },
108 { WXK_ESCAPE
, wxTRANSLATE("ESCAPE") },
109 { WXK_CANCEL
, wxTRANSLATE("CANCEL") },
110 { WXK_CLEAR
, wxTRANSLATE("CLEAR") },
111 { WXK_MENU
, wxTRANSLATE("MENU") },
112 { WXK_PAUSE
, wxTRANSLATE("PAUSE") },
113 { WXK_CAPITAL
, wxTRANSLATE("CAPITAL") },
114 { WXK_SELECT
, wxTRANSLATE("SELECT") },
115 { WXK_PRINT
, wxTRANSLATE("PRINT") },
116 { WXK_EXECUTE
, wxTRANSLATE("EXECUTE") },
117 { WXK_SNAPSHOT
, wxTRANSLATE("SNAPSHOT") },
118 { WXK_HELP
, wxTRANSLATE("HELP") },
119 { WXK_ADD
, wxTRANSLATE("ADD") },
120 { WXK_SEPARATOR
, wxTRANSLATE("SEPARATOR") },
121 { WXK_SUBTRACT
, wxTRANSLATE("SUBTRACT") },
122 { WXK_DECIMAL
, wxTRANSLATE("DECIMAL") },
123 { WXK_DIVIDE
, wxTRANSLATE("DIVIDE") },
124 { WXK_NUMLOCK
, wxTRANSLATE("NUM_LOCK") },
125 { WXK_SCROLL
, wxTRANSLATE("SCROLL_LOCK") },
126 { WXK_PAGEUP
, wxTRANSLATE("PAGEUP") },
127 { WXK_PAGEDOWN
, wxTRANSLATE("PAGEDOWN") },
128 { WXK_NUMPAD_SPACE
, wxTRANSLATE("KP_SPACE") },
129 { WXK_NUMPAD_TAB
, wxTRANSLATE("KP_TAB") },
130 { WXK_NUMPAD_ENTER
, wxTRANSLATE("KP_ENTER") },
131 { WXK_NUMPAD_HOME
, wxTRANSLATE("KP_HOME") },
132 { WXK_NUMPAD_LEFT
, wxTRANSLATE("KP_LEFT") },
133 { WXK_NUMPAD_UP
, wxTRANSLATE("KP_UP") },
134 { WXK_NUMPAD_RIGHT
, wxTRANSLATE("KP_RIGHT") },
135 { WXK_NUMPAD_DOWN
, wxTRANSLATE("KP_DOWN") },
136 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PRIOR") },
137 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PAGEUP") },
138 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_NEXT") },
139 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_PAGEDOWN") },
140 { WXK_NUMPAD_END
, wxTRANSLATE("KP_END") },
141 { WXK_NUMPAD_BEGIN
, wxTRANSLATE("KP_BEGIN") },
142 { WXK_NUMPAD_INSERT
, wxTRANSLATE("KP_INSERT") },
143 { WXK_NUMPAD_DELETE
, wxTRANSLATE("KP_DELETE") },
144 { WXK_NUMPAD_EQUAL
, wxTRANSLATE("KP_EQUAL") },
145 { WXK_NUMPAD_MULTIPLY
, wxTRANSLATE("KP_MULTIPLY") },
146 { WXK_NUMPAD_ADD
, wxTRANSLATE("KP_ADD") },
147 { WXK_NUMPAD_SEPARATOR
, wxTRANSLATE("KP_SEPARATOR") },
148 { WXK_NUMPAD_SUBTRACT
, wxTRANSLATE("KP_SUBTRACT") },
149 { WXK_NUMPAD_DECIMAL
, wxTRANSLATE("KP_DECIMAL") },
150 { WXK_NUMPAD_DIVIDE
, wxTRANSLATE("KP_DIVIDE") },
151 { WXK_WINDOWS_LEFT
, wxTRANSLATE("WINDOWS_LEFT") },
152 { WXK_WINDOWS_RIGHT
, wxTRANSLATE("WINDOWS_RIGHT") },
153 { WXK_WINDOWS_MENU
, wxTRANSLATE("WINDOWS_MENU") },
154 { WXK_COMMAND
, wxTRANSLATE("COMMAND") },
157 // return true if the 2 strings refer to the same accel
159 // as accels can be either translated or not, check for both possibilities and
160 // also compare case-insensitively as the key names case doesn't count
161 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
163 return str
.CmpNoCase(accel
) == 0
165 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
170 // return prefixCode+number if the string is of the form "<prefix><number>" and
173 // first and last parameter specify the valid domain for "number" part
175 IsNumberedAccelKey(const wxString
& str
,
176 const wxChar
*prefix
,
177 wxKeyCode prefixCode
,
181 const size_t lenPrefix
= wxStrlen(prefix
);
182 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
186 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
189 if ( num
< first
|| num
> last
)
191 // this must be a mistake, chances that this is a valid name of another
192 // key are vanishingly small
193 wxLogDebug(_T("Invalid key string \"%s\""), str
.c_str());
197 return prefixCode
+ num
- first
;
200 // return wxAcceleratorEntry for the given menu string or NULL if none
201 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
203 // check for accelerators: they are given after '\t'
204 int posTab
= label
.Find(wxT('\t'));
205 if ( posTab
== wxNOT_FOUND
)
208 // parse the accelerator string
209 int accelFlags
= wxACCEL_NORMAL
;
211 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.length(); n
++ )
213 if ( (label
[n
] == '+') || (label
[n
] == '-') )
215 if ( CompareAccelString(current
, wxTRANSLATE("ctrl")) )
216 accelFlags
|= wxACCEL_CTRL
;
217 else if ( CompareAccelString(current
, wxTRANSLATE("alt")) )
218 accelFlags
|= wxACCEL_ALT
;
219 else if ( CompareAccelString(current
, wxTRANSLATE("shift")) )
220 accelFlags
|= wxACCEL_SHIFT
;
221 else // not a recognized modifier name
223 // we may have "Ctrl-+", for example, but we still want to
224 // catch typos like "Crtl-A" so only give the warning if we
225 // have something before the current '+' or '-', else take
226 // it as a literal symbol
227 if ( current
.empty() )
231 // skip clearing it below
236 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
243 else // not special character
245 current
+= (wxChar
) wxTolower(label
[n
]);
250 const size_t len
= current
.length();
254 wxLogDebug(wxT("No accel key found, accel string ignored."));
258 // it's just a letter
259 keyCode
= current
[0U];
261 // if the key is used with any modifiers, make it an uppercase one
262 // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's
263 // used alone as 'a' and 'A' are different
264 if ( accelFlags
!= wxACCEL_NORMAL
)
265 keyCode
= wxToupper(keyCode
);
269 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
273 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
275 const wxKeyName
& kn
= wxKeyNames
[n
];
276 if ( CompareAccelString(current
, kn
.name
) )
285 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
288 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
289 WXK_SPECIAL1
, 1, 20);
293 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
300 wxASSERT_MSG( keyCode
, _T("logic error: should have key code here") );
302 return new wxAcceleratorEntry(accelFlags
, keyCode
);
305 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
307 return wxGetAccelFromString(GetText());
310 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
312 wxString text
= m_text
.BeforeFirst(wxT('\t'));
317 int flags
= accel
->GetFlags();
318 if ( flags
& wxACCEL_ALT
)
320 if ( flags
& wxACCEL_CTRL
)
322 if ( flags
& wxACCEL_SHIFT
)
325 const int code
= accel
->GetKeyCode();
327 if ( wxIsalnum(code
) )
328 text
<< (wxChar
)code
;
329 else if ( code
>= WXK_F1
&& code
<= WXK_F12
)
330 text
<< _("F") << code
- WXK_F1
+ 1;
331 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
332 text
<< _("KP_") << code
- WXK_NUMPAD0
;
333 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
334 text
<< _("SPECIAL") << code
- WXK_SPECIAL1
+ 1;
335 else // check the named keys
338 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
340 const wxKeyName
& kn
= wxKeyNames
[n
];
341 if ( code
== kn
.code
)
343 text
<< wxGetTranslation(kn
.name
);
348 wxASSERT_MSG( n
!= WXSIZEOF(wxKeyNames
),
349 wxT("unknown keyboard accelerator code") );
356 #endif // wxUSE_ACCEL
358 bool wxMenuBase::ms_locked
= true;
360 // ----------------------------------------------------------------------------
361 // wxMenu ctor and dtor
362 // ----------------------------------------------------------------------------
364 void wxMenuBase::Init(long style
)
366 m_menuBar
= (wxMenuBar
*)NULL
;
367 m_menuParent
= (wxMenu
*)NULL
;
369 m_invokingWindow
= (wxWindow
*)NULL
;
371 m_clientData
= (void *)NULL
;
372 m_eventHandler
= this;
375 wxMenuBase::~wxMenuBase()
377 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
379 // Actually, in GTK, the submenus have to get deleted first.
382 // ----------------------------------------------------------------------------
383 // wxMenu item adding/removing
384 // ----------------------------------------------------------------------------
386 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
388 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
390 submenu
->SetParent((wxMenu
*)this);
393 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
395 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
397 m_items
.Append(item
);
398 item
->SetMenu((wxMenu
*)this);
399 if ( item
->IsSubMenu() )
401 AddSubMenu(item
->GetSubMenu());
407 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
409 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
411 if ( pos
== GetMenuItemCount() )
413 return DoAppend(item
);
417 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
418 wxT("invalid index in wxMenu::Insert") );
420 return DoInsert(pos
, item
);
424 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
426 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
428 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
429 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
431 m_items
.Insert(node
, item
);
432 item
->SetMenu((wxMenu
*)this);
433 if ( item
->IsSubMenu() )
435 AddSubMenu(item
->GetSubMenu());
441 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
443 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
445 return DoRemove(item
);
448 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
450 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
452 // if we get here, the item is valid or one of Remove() functions is broken
453 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
455 // we detach the item, but we do delete the list node (i.e. don't call
456 // DetachNode() here!)
459 // item isn't attached to anything any more
460 item
->SetMenu((wxMenu
*)NULL
);
461 wxMenu
*submenu
= item
->GetSubMenu();
464 submenu
->SetParent((wxMenu
*)NULL
);
465 if ( submenu
->IsAttached() )
472 bool wxMenuBase::Delete(wxMenuItem
*item
)
474 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
476 return DoDelete(item
);
479 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
481 wxMenuItem
*item2
= DoRemove(item
);
482 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
484 // don't delete the submenu
485 item2
->SetSubMenu((wxMenu
*)NULL
);
492 bool wxMenuBase::Destroy(wxMenuItem
*item
)
494 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
496 return DoDestroy(item
);
499 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
501 wxMenuItem
*item2
= DoRemove(item
);
502 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
509 // ----------------------------------------------------------------------------
510 // wxMenu searching for items
511 // ----------------------------------------------------------------------------
513 // Finds the item id matching the given string, wxNOT_FOUND if not found.
514 int wxMenuBase::FindItem(const wxString
& text
) const
516 wxString label
= wxMenuItem::GetLabelFromText(text
);
517 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
519 node
= node
->GetNext() )
521 wxMenuItem
*item
= node
->GetData();
522 if ( item
->IsSubMenu() )
524 int rc
= item
->GetSubMenu()->FindItem(label
);
525 if ( rc
!= wxNOT_FOUND
)
529 // we execute this code for submenus as well to alllow finding them by
530 // name just like the ordinary items
531 if ( !item
->IsSeparator() )
533 if ( item
->GetLabel() == label
)
534 return item
->GetId();
541 // recursive search for item by id
542 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
547 wxMenuItem
*item
= NULL
;
548 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
550 node
= node
->GetNext() )
552 item
= node
->GetData();
554 if ( item
->GetId() == itemId
)
557 *itemMenu
= (wxMenu
*)this;
559 else if ( item
->IsSubMenu() )
561 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
565 // don't exit the loop
573 // non recursive search
574 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
576 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
577 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
580 for ( pos
= 0; node
; pos
++ )
582 if ( node
->GetData()->GetId() == id
)
584 item
= node
->GetData();
589 node
= node
->GetNext();
594 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
601 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
603 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
604 _T("wxMenu::FindItemByPosition(): invalid menu index") );
606 return m_items
.Item( position
)->GetData();
609 // ----------------------------------------------------------------------------
610 // wxMenu helpers used by derived classes
611 // ----------------------------------------------------------------------------
613 // Update a menu and all submenus recursively. source is the object that has
614 // the update event handlers defined for it. If NULL, the menu or associated
615 // window will be used.
616 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
618 if (GetInvokingWindow())
620 // Don't update menus if the parent
621 // frame is about to get deleted
622 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
623 if (tlw
&& wxPendingDelete
.Member(tlw
))
627 if ( !source
&& GetInvokingWindow() )
628 source
= GetInvokingWindow()->GetEventHandler();
630 source
= GetEventHandler();
634 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
637 wxMenuItem
* item
= node
->GetData();
638 if ( !item
->IsSeparator() )
640 wxWindowID id
= item
->GetId();
641 wxUpdateUIEvent
event(id
);
642 event
.SetEventObject( source
);
644 if ( source
->ProcessEvent(event
) )
646 // if anything changed, update the changed attribute
647 if (event
.GetSetText())
648 SetLabel(id
, event
.GetText());
649 if (event
.GetSetChecked())
650 Check(id
, event
.GetChecked());
651 if (event
.GetSetEnabled())
652 Enable(id
, event
.GetEnabled());
655 // recurse to the submenus
656 if ( item
->GetSubMenu() )
657 item
->GetSubMenu()->UpdateUI(source
);
659 //else: item is a separator (which doesn't process update UI events)
661 node
= node
->GetNext();
665 bool wxMenuBase::SendEvent(int id
, int checked
)
667 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
668 event
.SetEventObject(this);
669 event
.SetInt(checked
);
671 bool processed
= false;
673 // Try the menu's event handler
676 wxEvtHandler
*handler
= GetEventHandler();
678 processed
= handler
->ProcessEvent(event
);
681 // Try the window the menu was popped up from (and up through the
685 const wxMenuBase
*menu
= this;
688 wxWindow
*win
= menu
->GetInvokingWindow();
691 processed
= win
->GetEventHandler()->ProcessEvent(event
);
695 menu
= menu
->GetParent();
702 // ----------------------------------------------------------------------------
703 // wxMenu attaching/detaching to/from menu bar
704 // ----------------------------------------------------------------------------
706 wxMenuBar
* wxMenuBase::GetMenuBar() const
709 return GetParent()->GetMenuBar();
713 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
715 // use Detach() instead!
716 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
718 // use IsAttached() to prevent this from happening
719 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
721 m_menuBar
= (wxMenuBar
*)menubar
;
724 void wxMenuBase::Detach()
726 // use IsAttached() to prevent this from happening
727 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
732 // ----------------------------------------------------------------------------
733 // wxMenu functions forwarded to wxMenuItem
734 // ----------------------------------------------------------------------------
736 void wxMenuBase::Enable( int id
, bool enable
)
738 wxMenuItem
*item
= FindItem(id
);
740 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
742 item
->Enable(enable
);
745 bool wxMenuBase::IsEnabled( int id
) const
747 wxMenuItem
*item
= FindItem(id
);
749 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
751 return item
->IsEnabled();
754 void wxMenuBase::Check( int id
, bool enable
)
756 wxMenuItem
*item
= FindItem(id
);
758 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
763 bool wxMenuBase::IsChecked( int id
) const
765 wxMenuItem
*item
= FindItem(id
);
767 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
769 return item
->IsChecked();
772 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
774 wxMenuItem
*item
= FindItem(id
);
776 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
778 item
->SetText(label
);
781 wxString
wxMenuBase::GetLabel( int id
) const
783 wxMenuItem
*item
= FindItem(id
);
785 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
787 return item
->GetText();
790 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
792 wxMenuItem
*item
= FindItem(id
);
794 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
796 item
->SetHelp( helpString
);
799 wxString
wxMenuBase::GetHelpString( int id
) const
801 wxMenuItem
*item
= FindItem(id
);
803 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
805 return item
->GetHelp();
808 // ----------------------------------------------------------------------------
809 // wxMenuBarBase ctor and dtor
810 // ----------------------------------------------------------------------------
812 wxMenuBarBase::wxMenuBarBase()
815 m_menuBarFrame
= NULL
;
818 wxMenuBarBase::~wxMenuBarBase()
820 WX_CLEAR_LIST(wxMenuList
, m_menus
);
823 // ----------------------------------------------------------------------------
824 // wxMenuBar item access: the base class versions manage m_menus list, the
825 // derived class should reflect the changes in the real menubar
826 // ----------------------------------------------------------------------------
828 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
830 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
831 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
833 return node
->GetData();
836 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
838 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
840 m_menus
.Append(menu
);
846 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
847 const wxString
& title
)
849 if ( pos
== m_menus
.GetCount() )
851 return wxMenuBarBase::Append(menu
, title
);
853 else // not at the end
855 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
857 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
858 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
860 m_menus
.Insert(node
, menu
);
867 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
868 const wxString
& WXUNUSED(title
))
870 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
872 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
873 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
875 wxMenu
*menuOld
= node
->GetData();
884 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
886 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
887 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
889 wxMenu
*menu
= node
->GetData();
896 int wxMenuBarBase::FindMenu(const wxString
& title
) const
898 wxString label
= wxMenuItem::GetLabelFromText(title
);
900 size_t count
= GetMenuCount();
901 for ( size_t i
= 0; i
< count
; i
++ )
903 wxString title2
= GetLabelTop(i
);
904 if ( (title2
== title
) ||
905 (wxMenuItem::GetLabelFromText(title2
) == label
) )
916 // ----------------------------------------------------------------------------
917 // wxMenuBar attaching/detaching to/from the frame
918 // ----------------------------------------------------------------------------
920 void wxMenuBarBase::Attach(wxFrame
*frame
)
922 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
924 m_menuBarFrame
= frame
;
927 void wxMenuBarBase::Detach()
929 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
931 m_menuBarFrame
= NULL
;
934 // ----------------------------------------------------------------------------
935 // wxMenuBar searching for items
936 // ----------------------------------------------------------------------------
938 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
943 wxMenuItem
*item
= NULL
;
944 size_t count
= GetMenuCount(), i
;
945 wxMenuList::const_iterator it
;
946 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
948 item
= (*it
)->FindItem(id
, menu
);
954 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
956 wxString label
= wxMenuItem::GetLabelFromText(menu
);
959 wxMenuList::compatibility_iterator node
;
960 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
962 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
963 return node
->GetData()->FindItem(item
);
969 // ---------------------------------------------------------------------------
970 // wxMenuBar functions forwarded to wxMenuItem
971 // ---------------------------------------------------------------------------
973 void wxMenuBarBase::Enable(int id
, bool enable
)
975 wxMenuItem
*item
= FindItem(id
);
977 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
979 item
->Enable(enable
);
982 void wxMenuBarBase::Check(int id
, bool check
)
984 wxMenuItem
*item
= FindItem(id
);
986 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
987 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
992 bool wxMenuBarBase::IsChecked(int id
) const
994 wxMenuItem
*item
= FindItem(id
);
996 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
998 return item
->IsChecked();
1001 bool wxMenuBarBase::IsEnabled(int id
) const
1003 wxMenuItem
*item
= FindItem(id
);
1005 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1007 return item
->IsEnabled();
1010 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1012 wxMenuItem
*item
= FindItem(id
);
1014 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1016 item
->SetText(label
);
1019 wxString
wxMenuBarBase::GetLabel(int id
) const
1021 wxMenuItem
*item
= FindItem(id
);
1023 wxCHECK_MSG( item
, wxEmptyString
,
1024 wxT("wxMenuBar::GetLabel(): no such item") );
1026 return item
->GetText();
1029 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1031 wxMenuItem
*item
= FindItem(id
);
1033 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1035 item
->SetHelp(helpString
);
1038 wxString
wxMenuBarBase::GetHelpString(int id
) const
1040 wxMenuItem
*item
= FindItem(id
);
1042 wxCHECK_MSG( item
, wxEmptyString
,
1043 wxT("wxMenuBar::GetHelpString(): no such item") );
1045 return item
->GetHelp();
1048 void wxMenuBarBase::UpdateMenus( void )
1050 wxEvtHandler
* source
;
1052 int nCount
= GetMenuCount();
1053 for (int n
= 0; n
< nCount
; n
++)
1055 menu
= GetMenu( n
);
1058 source
= menu
->GetEventHandler();
1060 menu
->UpdateUI( source
);
1065 #endif // wxUSE_MENUS