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"
36 #include "wx/stockitem.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 #include "wx/listimpl.cpp"
44 WX_DEFINE_LIST(wxMenuList
)
45 WX_DEFINE_LIST(wxMenuItemList
)
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
52 // XTI for wxMenu(Bar)
53 // ----------------------------------------------------------------------------
55 #if wxUSE_EXTENDED_RTTI
57 WX_DEFINE_LIST( wxMenuInfoList
)
59 wxDEFINE_FLAGS( wxMenuStyle
)
60 wxBEGIN_FLAGS( wxMenuStyle
)
61 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
62 wxEND_FLAGS( wxMenuStyle
)
64 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
, "wx/menu.h")
65 wxCOLLECTION_TYPE_INFO( wxMenuItem
*, wxMenuItemList
) ;
67 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
,
68 wxVariantBaseArray
&value
)
70 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
73 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
74 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
76 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), \
77 0 /*flags*/, wxT("Helpstring"), wxT("group") )
79 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long, GetStyle
, \
80 wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, wxT("Helpstring"), \
81 wxT("group")) // style
83 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
*, Append
, \
84 GetMenuItems
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
85 wxEND_PROPERTIES_TABLE()
87 wxEMPTY_HANDLERS_TABLE(wxMenu
)
89 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long, MenuStyle
)
91 wxDEFINE_FLAGS( wxMenuBarStyle
)
93 wxBEGIN_FLAGS( wxMenuBarStyle
)
94 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
95 wxEND_FLAGS( wxMenuBarStyle
)
97 // the negative id would lead the window (its superclass !) to
98 // vetoe streaming out otherwise
99 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxObjectWriter
*,
100 wxObjectReaderCallback
*, wxVariantBaseArray
& )
105 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
, "wx/menu.h", \
106 wxMenuBarStreamingCallback
)
108 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h")
110 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
111 wxREADONLY_PROPERTY( Menu
, wxMenu
*, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
112 0 /*flags*/, wxT("Helpstring"), wxT("group"))
114 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString(), \
115 0 /*flags*/, wxT("Helpstring"), wxT("group"))
116 wxEND_PROPERTIES_TABLE()
118 wxEMPTY_HANDLERS_TABLE(wxMenuInfo
)
120 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
*, Menu
, wxString
, Title
)
122 wxCOLLECTION_TYPE_INFO( wxMenuInfo
*, wxMenuInfoList
) ;
124 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
,
125 wxVariantBaseArray
&value
)
127 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
130 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
131 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
*, AppendMenuInfo
, \
132 GetMenuInfos
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
133 wxEND_PROPERTIES_TABLE()
135 wxEMPTY_HANDLERS_TABLE(wxMenuBar
)
137 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
140 // IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
141 // IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
142 // IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject)
146 // ----------------------------------------------------------------------------
147 // XTI for wxMenuItem
148 // ----------------------------------------------------------------------------
150 #if wxUSE_EXTENDED_RTTI
152 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxObjectWriter
*,
153 wxObjectReaderCallback
*, wxVariantBaseArray
& )
155 const wxMenuItem
* mitem
= wx_dynamic_cast(const wxMenuItem
*, object
);
156 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
158 // we don't stream out the first two items for menus with a title,
159 // they will be reconstructed
160 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
||
161 mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
167 wxBEGIN_ENUM( wxItemKind
)
168 wxENUM_MEMBER( wxITEM_SEPARATOR
)
169 wxENUM_MEMBER( wxITEM_NORMAL
)
170 wxENUM_MEMBER( wxITEM_CHECK
)
171 wxENUM_MEMBER( wxITEM_RADIO
)
172 wxEND_ENUM( wxItemKind
)
174 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
, "wx/menuitem.h", \
175 wxMenuItemStreamingCallback
)
177 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
178 wxPROPERTY( Parent
, wxMenu
*, SetMenu
, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
179 0 /*flags*/, wxT("Helpstring"), wxT("group") )
180 wxPROPERTY( Id
, int, SetId
, GetId
, wxEMPTY_PARAMETER_VALUE
, \
181 0 /*flags*/, wxT("Helpstring"), wxT("group") )
182 wxPROPERTY( Text
, wxString
, SetText
, GetText
, wxString(), \
183 0 /*flags*/, wxT("Helpstring"), wxT("group") )
184 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), \
185 0 /*flags*/, wxT("Helpstring"), wxT("group") )
186 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, wxEMPTY_PARAMETER_VALUE
, \
187 0 /*flags*/, wxT("Helpstring"), wxT("group") )
188 wxPROPERTY( SubMenu
, wxMenu
*, SetSubMenu
, GetSubMenu
, wxEMPTY_PARAMETER_VALUE
, \
189 0 /*flags*/, wxT("Helpstring"), wxT("group") )
190 wxPROPERTY( Enabled
, bool, Enable
, IsEnabled
, wxVariantBase((bool)true), \
191 0 /*flags*/, wxT("Helpstring"), wxT("group") )
192 wxPROPERTY( Checked
, bool, Check
, IsChecked
, wxVariantBase((bool)false), \
193 0 /*flags*/, wxT("Helpstring"), wxT("group") )
194 wxPROPERTY( Checkable
, bool, SetCheckable
, IsCheckable
, wxVariantBase((bool)false), \
195 0 /*flags*/, wxT("Helpstring"), wxT("group") )
196 wxEND_PROPERTIES_TABLE()
198 wxEMPTY_HANDLERS_TABLE(wxMenuItem
)
200 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
*, Parent
, int, Id
, wxString
, \
201 Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
*, SubMenu
)
203 //IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
212 const wxString
& text
,
213 const wxString
& help
,
220 m_id
= wxWindow::NewControlId();
224 m_id
= wxID_SEPARATOR
;
226 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
227 // and it makes sense to omit the following optional parameters,
228 // including the kind one which doesn't default to wxITEM_SEPARATOR,
229 // of course, so override it here
230 kind
= wxITEM_SEPARATOR
;
234 // (popup) menu titles in wxMSW use this ID to indicate that
235 // it's not a real menu item, so we don't want the check below to
241 // ids are limited to 16 bits under MSW so portable code shouldn't
242 // use ids outside of this range (negative ids generated by wx are
244 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
245 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
246 wxS("invalid id value") );
250 // notice that parentMenu can be NULL: the item can be attached to the menu
251 // later with SetMenu()
253 m_parentMenu
= parentMenu
;
263 wxMenuItemBase::~wxMenuItemBase()
270 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
272 return wxAcceleratorEntry::Create(GetItemLabel());
275 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
277 wxString text
= m_text
.BeforeFirst(wxT('\t'));
281 text
+= accel
->ToString();
287 #endif // wxUSE_ACCEL
289 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
293 if ( m_text
.empty() && !IsSeparator() )
295 wxASSERT_MSG( wxIsStockID(GetId()),
296 wxT("A non-stock menu item with an empty label?") );
297 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
298 wxSTOCK_WITH_MNEMONIC
);
302 void wxMenuItemBase::SetHelp(const wxString
& str
)
306 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
308 // get a stock help string
309 m_help
= wxGetStockHelpString(GetId());
314 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
316 return wxStripMenuCodes(text
);
320 #if WXWIN_COMPATIBILITY_2_8
321 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
323 return GetLabelText(text
);
327 bool wxMenuBase::ms_locked
= true;
329 // ----------------------------------------------------------------------------
330 // wxMenu ctor and dtor
331 // ----------------------------------------------------------------------------
333 void wxMenuBase::Init(long style
)
338 m_invokingWindow
= NULL
;
341 m_eventHandler
= this;
344 wxMenuBase::~wxMenuBase()
346 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
349 // ----------------------------------------------------------------------------
350 // wxMenu item adding/removing
351 // ----------------------------------------------------------------------------
353 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
355 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
357 submenu
->SetParent((wxMenu
*)this);
360 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
362 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
364 m_items
.Append(item
);
365 item
->SetMenu((wxMenu
*)this);
366 if ( item
->IsSubMenu() )
368 AddSubMenu(item
->GetSubMenu());
374 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
376 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
378 if ( pos
== GetMenuItemCount() )
380 return DoAppend(item
);
384 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
385 wxT("invalid index in wxMenu::Insert") );
387 return DoInsert(pos
, item
);
391 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
393 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
395 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
396 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
398 m_items
.Insert(node
, item
);
399 item
->SetMenu((wxMenu
*)this);
400 if ( item
->IsSubMenu() )
402 AddSubMenu(item
->GetSubMenu());
408 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
410 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
412 return DoRemove(item
);
415 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
417 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
419 // if we get here, the item is valid or one of Remove() functions is broken
420 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
422 // we detach the item, but we do delete the list node (i.e. don't call
423 // DetachNode() here!)
426 // item isn't attached to anything any more
428 wxMenu
*submenu
= item
->GetSubMenu();
431 submenu
->SetParent(NULL
);
432 if ( submenu
->IsAttached() )
439 bool wxMenuBase::Delete(wxMenuItem
*item
)
441 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
443 return DoDelete(item
);
446 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
448 wxMenuItem
*item2
= DoRemove(item
);
449 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
451 // don't delete the submenu
452 item2
->SetSubMenu(NULL
);
459 bool wxMenuBase::Destroy(wxMenuItem
*item
)
461 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
463 return DoDestroy(item
);
466 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
468 wxMenuItem
*item2
= DoRemove(item
);
469 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
476 // ----------------------------------------------------------------------------
477 // wxMenu searching for items
478 // ----------------------------------------------------------------------------
480 // Finds the item id matching the given string, wxNOT_FOUND if not found.
481 int wxMenuBase::FindItem(const wxString
& text
) const
483 wxString label
= wxMenuItem::GetLabelText(text
);
484 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
486 node
= node
->GetNext() )
488 wxMenuItem
*item
= node
->GetData();
489 if ( item
->IsSubMenu() )
491 int rc
= item
->GetSubMenu()->FindItem(label
);
492 if ( rc
!= wxNOT_FOUND
)
496 // we execute this code for submenus as well to alllow finding them by
497 // name just like the ordinary items
498 if ( !item
->IsSeparator() )
500 if ( item
->GetItemLabelText() == label
)
501 return item
->GetId();
508 // recursive search for item by id
509 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
514 wxMenuItem
*item
= NULL
;
515 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
517 node
= node
->GetNext() )
519 item
= node
->GetData();
521 if ( item
->GetId() == itemId
)
524 *itemMenu
= (wxMenu
*)this;
526 else if ( item
->IsSubMenu() )
528 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
532 // don't exit the loop
540 // non recursive search
541 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
543 wxMenuItem
*item
= NULL
;
544 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
547 for ( pos
= 0; node
; pos
++ )
549 if ( node
->GetData()->GetId() == id
)
551 item
= node
->GetData();
556 node
= node
->GetNext();
561 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
568 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
570 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
571 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
573 return m_items
.Item( position
)->GetData();
576 // ----------------------------------------------------------------------------
577 // wxMenu helpers used by derived classes
578 // ----------------------------------------------------------------------------
580 // Update a menu and all submenus recursively. source is the object that has
581 // the update event handlers defined for it. If NULL, the menu or associated
582 // window will be used.
583 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
585 if (GetInvokingWindow())
587 // Don't update menus if the parent
588 // frame is about to get deleted
589 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
590 if (tlw
&& wxPendingDelete
.Member(tlw
))
594 if ( !source
&& GetInvokingWindow() )
595 source
= GetInvokingWindow()->GetEventHandler();
597 source
= GetEventHandler();
601 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
604 wxMenuItem
* item
= node
->GetData();
605 if ( !item
->IsSeparator() )
607 wxWindowID id
= item
->GetId();
608 wxUpdateUIEvent
event(id
);
609 event
.SetEventObject( source
);
611 if ( source
->ProcessEvent(event
) )
613 // if anything changed, update the changed attribute
614 if (event
.GetSetText())
615 SetLabel(id
, event
.GetText());
616 if (event
.GetSetChecked())
617 Check(id
, event
.GetChecked());
618 if (event
.GetSetEnabled())
619 Enable(id
, event
.GetEnabled());
622 // recurse to the submenus
623 if ( item
->GetSubMenu() )
624 item
->GetSubMenu()->UpdateUI(source
);
626 //else: item is a separator (which doesn't process update UI events)
628 node
= node
->GetNext();
632 bool wxMenuBase::SendEvent(int id
, int checked
)
634 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
635 event
.SetEventObject(this);
636 event
.SetInt(checked
);
638 bool processed
= false;
640 // Try the menu's event handler first
641 wxEvtHandler
*handler
= GetEventHandler();
643 processed
= handler
->SafelyProcessEvent(event
);
645 // Try the window the menu was popped up from or its menu bar belongs to
648 wxWindow
* const win
= GetWindow();
650 processed
= win
->HandleWindowEvent(event
);
656 // ----------------------------------------------------------------------------
657 // wxMenu attaching/detaching to/from menu bar
658 // ----------------------------------------------------------------------------
660 wxMenuBar
* wxMenuBase::GetMenuBar() const
663 return GetParent()->GetMenuBar();
667 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
669 // use Detach() instead!
670 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
672 // use IsAttached() to prevent this from happening
673 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
675 m_menuBar
= (wxMenuBar
*)menubar
;
678 void wxMenuBase::Detach()
680 // use IsAttached() to prevent this from happening
681 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
686 // ----------------------------------------------------------------------------
687 // wxMenu invoking window handling
688 // ----------------------------------------------------------------------------
690 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
692 wxASSERT_MSG( !GetParent(),
693 "should only be called for top level popup menus" );
694 wxASSERT_MSG( !IsAttached(),
695 "menus attached to menu bar can't have invoking window" );
697 m_invokingWindow
= win
;
700 wxWindow
*wxMenuBase::GetWindow() const
702 // only the top level menus have non-NULL invoking window or a pointer to
703 // the menu bar so recurse upwards until we find it
704 const wxMenuBase
*menu
= this;
705 while ( menu
->GetParent() )
707 menu
= menu
->GetParent();
710 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
711 : menu
->GetInvokingWindow();
714 // ----------------------------------------------------------------------------
715 // wxMenu functions forwarded to wxMenuItem
716 // ----------------------------------------------------------------------------
718 void wxMenuBase::Enable( int id
, bool enable
)
720 wxMenuItem
*item
= FindItem(id
);
722 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
724 item
->Enable(enable
);
727 bool wxMenuBase::IsEnabled( int id
) const
729 wxMenuItem
*item
= FindItem(id
);
731 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
733 return item
->IsEnabled();
736 void wxMenuBase::Check( int id
, bool enable
)
738 wxMenuItem
*item
= FindItem(id
);
740 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
745 bool wxMenuBase::IsChecked( int id
) const
747 wxMenuItem
*item
= FindItem(id
);
749 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
751 return item
->IsChecked();
754 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
756 wxMenuItem
*item
= FindItem(id
);
758 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
760 item
->SetItemLabel(label
);
763 wxString
wxMenuBase::GetLabel( int id
) const
765 wxMenuItem
*item
= FindItem(id
);
767 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
769 return item
->GetItemLabel();
772 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
774 wxMenuItem
*item
= FindItem(id
);
776 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
778 item
->SetHelp( helpString
);
781 wxString
wxMenuBase::GetHelpString( int id
) const
783 wxMenuItem
*item
= FindItem(id
);
785 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
787 return item
->GetHelp();
790 // ----------------------------------------------------------------------------
791 // wxMenuBarBase ctor and dtor
792 // ----------------------------------------------------------------------------
794 wxMenuBarBase::wxMenuBarBase()
797 m_menuBarFrame
= NULL
;
800 wxMenuBarBase::~wxMenuBarBase()
802 WX_CLEAR_LIST(wxMenuList
, m_menus
);
805 // ----------------------------------------------------------------------------
806 // wxMenuBar item access: the base class versions manage m_menus list, the
807 // derived class should reflect the changes in the real menubar
808 // ----------------------------------------------------------------------------
810 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
812 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
813 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
815 return node
->GetData();
818 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
820 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
821 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
823 m_menus
.Append(menu
);
829 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
830 const wxString
& title
)
832 if ( pos
== m_menus
.GetCount() )
834 return wxMenuBarBase::Append(menu
, title
);
836 else // not at the end
838 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
840 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
841 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
843 m_menus
.Insert(node
, menu
);
850 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
851 const wxString
& WXUNUSED(title
))
853 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
855 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
856 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
858 wxMenu
*menuOld
= node
->GetData();
867 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
869 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
870 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
872 wxMenu
*menu
= node
->GetData();
879 int wxMenuBarBase::FindMenu(const wxString
& title
) const
881 wxString label
= wxMenuItem::GetLabelText(title
);
883 size_t count
= GetMenuCount();
884 for ( size_t i
= 0; i
< count
; i
++ )
886 wxString title2
= GetMenuLabel(i
);
887 if ( (title2
== title
) ||
888 (wxMenuItem::GetLabelText(title2
) == label
) )
899 // ----------------------------------------------------------------------------
900 // wxMenuBar attaching/detaching to/from the frame
901 // ----------------------------------------------------------------------------
903 void wxMenuBarBase::Attach(wxFrame
*frame
)
905 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
907 m_menuBarFrame
= frame
;
910 void wxMenuBarBase::Detach()
912 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
914 m_menuBarFrame
= NULL
;
917 // ----------------------------------------------------------------------------
918 // wxMenuBar searching for items
919 // ----------------------------------------------------------------------------
921 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
926 wxMenuItem
*item
= NULL
;
927 size_t count
= GetMenuCount(), i
;
928 wxMenuList::const_iterator it
;
929 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
931 item
= (*it
)->FindItem(id
, menu
);
937 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
939 wxString label
= wxMenuItem::GetLabelText(menu
);
942 wxMenuList::compatibility_iterator node
;
943 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
945 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
946 return node
->GetData()->FindItem(item
);
952 // ---------------------------------------------------------------------------
953 // wxMenuBar functions forwarded to wxMenuItem
954 // ---------------------------------------------------------------------------
956 void wxMenuBarBase::Enable(int id
, bool enable
)
958 wxMenuItem
*item
= FindItem(id
);
960 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
962 item
->Enable(enable
);
965 void wxMenuBarBase::Check(int id
, bool check
)
967 wxMenuItem
*item
= FindItem(id
);
969 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
970 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
975 bool wxMenuBarBase::IsChecked(int id
) const
977 wxMenuItem
*item
= FindItem(id
);
979 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
981 return item
->IsChecked();
984 bool wxMenuBarBase::IsEnabled(int id
) const
986 wxMenuItem
*item
= FindItem(id
);
988 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
990 return item
->IsEnabled();
993 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
995 wxMenuItem
*item
= FindItem(id
);
997 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
999 item
->SetItemLabel(label
);
1002 wxString
wxMenuBarBase::GetLabel(int id
) const
1004 wxMenuItem
*item
= FindItem(id
);
1006 wxCHECK_MSG( item
, wxEmptyString
,
1007 wxT("wxMenuBar::GetLabel(): no such item") );
1009 return item
->GetItemLabel();
1012 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1014 wxMenuItem
*item
= FindItem(id
);
1016 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1018 item
->SetHelp(helpString
);
1021 wxString
wxMenuBarBase::GetHelpString(int id
) const
1023 wxMenuItem
*item
= FindItem(id
);
1025 wxCHECK_MSG( item
, wxEmptyString
,
1026 wxT("wxMenuBar::GetHelpString(): no such item") );
1028 return item
->GetHelp();
1031 void wxMenuBarBase::UpdateMenus()
1033 wxEvtHandler
* source
;
1035 int nCount
= GetMenuCount();
1036 for (int n
= 0; n
< nCount
; n
++)
1038 menu
= GetMenu( n
);
1041 source
= menu
->GetEventHandler();
1043 menu
->UpdateUI( source
);
1048 #if WXWIN_COMPATIBILITY_2_8
1049 // get or change the label of the menu at given position
1050 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
1052 SetMenuLabel(pos
, label
);
1055 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
1057 return GetMenuLabelText(pos
);
1061 #endif // wxUSE_MENUS