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
,
70 wxListCollectionToAnyList
<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 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& )
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
,
127 wxListCollectionToAnyList
<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
)
139 const wxMenuInfoList
& wxMenuBarBase::GetMenuInfos() const
141 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* > (& m_menuInfos
);
142 WX_CLEAR_LIST( wxMenuInfoList
, *list
);
143 for (size_t i
= 0 ; i
< GetMenuCount(); ++i
)
145 wxMenuInfo
* info
= new wxMenuInfo();
146 info
->Create( GetMenu(i
), GetMenuLabel(i
));
153 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxMenu**>)
154 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxMenuItem**>)
155 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxMenuBar**>)
156 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxMenuInfo**>)
159 // IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
160 // IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
161 // IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject)
165 // ----------------------------------------------------------------------------
166 // XTI for wxMenuItem
167 // ----------------------------------------------------------------------------
169 #if wxUSE_EXTENDED_RTTI
171 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxObjectWriter
*,
172 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& )
174 const wxMenuItem
* mitem
= wx_dynamic_cast(const wxMenuItem
*, object
);
175 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
177 // we don't stream out the first two items for menus with a title,
178 // they will be reconstructed
179 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
||
180 mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
188 wxBEGIN_ENUM( wxItemKind
)
189 wxENUM_MEMBER( wxITEM_SEPARATOR
)
190 wxENUM_MEMBER( wxITEM_NORMAL
)
191 wxENUM_MEMBER( wxITEM_CHECK
)
192 wxENUM_MEMBER( wxITEM_RADIO
)
193 wxEND_ENUM( wxItemKind
)
195 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
, "wx/menuitem.h", \
196 wxMenuItemStreamingCallback
)
198 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
199 wxPROPERTY( Parent
, wxMenu
*, SetMenu
, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
200 0 /*flags*/, wxT("Helpstring"), wxT("group") )
201 wxPROPERTY( Id
, int, SetId
, GetId
, wxEMPTY_PARAMETER_VALUE
, \
202 0 /*flags*/, wxT("Helpstring"), wxT("group") )
203 wxPROPERTY( ItemLabel
, wxString
, SetItemLabel
, GetItemLabel
, wxString(), \
204 0 /*flags*/, wxT("Helpstring"), wxT("group") )
205 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), \
206 0 /*flags*/, wxT("Helpstring"), wxT("group") )
207 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, wxEMPTY_PARAMETER_VALUE
, \
208 0 /*flags*/, wxT("Helpstring"), wxT("group") )
209 wxPROPERTY( SubMenu
, wxMenu
*, SetSubMenu
, GetSubMenu
, wxEMPTY_PARAMETER_VALUE
, \
210 0 /*flags*/, wxT("Helpstring"), wxT("group") )
211 wxPROPERTY( Enabled
, bool, Enable
, IsEnabled
, wxAny((bool)true), \
212 0 /*flags*/, wxT("Helpstring"), wxT("group") )
213 wxPROPERTY( Checked
, bool, Check
, IsChecked
, wxAny((bool)false), \
214 0 /*flags*/, wxT("Helpstring"), wxT("group") )
215 wxPROPERTY( Checkable
, bool, SetCheckable
, IsCheckable
, wxAny((bool)false), \
216 0 /*flags*/, wxT("Helpstring"), wxT("group") )
217 wxEND_PROPERTIES_TABLE()
219 wxEMPTY_HANDLERS_TABLE(wxMenuItem
)
221 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
*, Parent
, int, Id
, wxString
, \
222 Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
*, SubMenu
)
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
230 const wxString
& text
,
231 const wxString
& help
,
238 m_id
= wxWindow::NewControlId();
242 m_id
= wxID_SEPARATOR
;
244 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
245 // and it makes sense to omit the following optional parameters,
246 // including the kind one which doesn't default to wxITEM_SEPARATOR,
247 // of course, so override it here
248 kind
= wxITEM_SEPARATOR
;
252 // (popup) menu titles in wxMSW use this ID to indicate that
253 // it's not a real menu item, so we don't want the check below to
259 // ids are limited to 16 bits under MSW so portable code shouldn't
260 // use ids outside of this range (negative ids generated by wx are
262 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
263 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
264 wxS("invalid id value") );
268 // notice that parentMenu can be NULL: the item can be attached to the menu
269 // later with SetMenu()
271 m_parentMenu
= parentMenu
;
281 wxMenuItemBase::~wxMenuItemBase()
288 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
290 return wxAcceleratorEntry::Create(GetItemLabel());
293 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
295 wxString text
= m_text
.BeforeFirst(wxT('\t'));
299 text
+= accel
->ToString();
305 #endif // wxUSE_ACCEL
307 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
311 if ( m_text
.empty() && !IsSeparator() )
313 wxASSERT_MSG( wxIsStockID(GetId()),
314 wxT("A non-stock menu item with an empty label?") );
315 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
316 wxSTOCK_WITH_MNEMONIC
);
320 void wxMenuItemBase::SetHelp(const wxString
& str
)
324 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
326 // get a stock help string
327 m_help
= wxGetStockHelpString(GetId());
332 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
334 return wxStripMenuCodes(text
);
338 #if WXWIN_COMPATIBILITY_2_8
339 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
341 return GetLabelText(text
);
345 bool wxMenuBase::ms_locked
= true;
347 // ----------------------------------------------------------------------------
348 // wxMenu ctor and dtor
349 // ----------------------------------------------------------------------------
351 void wxMenuBase::Init(long style
)
356 m_invokingWindow
= NULL
;
359 m_eventHandler
= this;
362 wxMenuBase::~wxMenuBase()
364 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
367 // ----------------------------------------------------------------------------
368 // wxMenu item adding/removing
369 // ----------------------------------------------------------------------------
371 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
373 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
375 submenu
->SetParent((wxMenu
*)this);
378 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
380 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
382 m_items
.Append(item
);
383 item
->SetMenu((wxMenu
*)this);
384 if ( item
->IsSubMenu() )
386 AddSubMenu(item
->GetSubMenu());
392 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
394 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
396 if ( pos
== GetMenuItemCount() )
398 return DoAppend(item
);
402 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
403 wxT("invalid index in wxMenu::Insert") );
405 return DoInsert(pos
, item
);
409 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
411 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
413 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
414 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
416 m_items
.Insert(node
, item
);
417 item
->SetMenu((wxMenu
*)this);
418 if ( item
->IsSubMenu() )
420 AddSubMenu(item
->GetSubMenu());
426 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
428 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
430 return DoRemove(item
);
433 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
435 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
437 // if we get here, the item is valid or one of Remove() functions is broken
438 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
440 // we detach the item, but we do delete the list node (i.e. don't call
441 // DetachNode() here!)
444 // item isn't attached to anything any more
446 wxMenu
*submenu
= item
->GetSubMenu();
449 submenu
->SetParent(NULL
);
450 if ( submenu
->IsAttached() )
457 bool wxMenuBase::Delete(wxMenuItem
*item
)
459 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
461 return DoDelete(item
);
464 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
466 wxMenuItem
*item2
= DoRemove(item
);
467 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
469 // don't delete the submenu
470 item2
->SetSubMenu(NULL
);
477 bool wxMenuBase::Destroy(wxMenuItem
*item
)
479 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
481 return DoDestroy(item
);
484 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
486 wxMenuItem
*item2
= DoRemove(item
);
487 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
494 // ----------------------------------------------------------------------------
495 // wxMenu searching for items
496 // ----------------------------------------------------------------------------
498 // Finds the item id matching the given string, wxNOT_FOUND if not found.
499 int wxMenuBase::FindItem(const wxString
& text
) const
501 wxString label
= wxMenuItem::GetLabelText(text
);
502 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
504 node
= node
->GetNext() )
506 wxMenuItem
*item
= node
->GetData();
507 if ( item
->IsSubMenu() )
509 int rc
= item
->GetSubMenu()->FindItem(label
);
510 if ( rc
!= wxNOT_FOUND
)
514 // we execute this code for submenus as well to alllow finding them by
515 // name just like the ordinary items
516 if ( !item
->IsSeparator() )
518 if ( item
->GetItemLabelText() == label
)
519 return item
->GetId();
526 // recursive search for item by id
527 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
532 wxMenuItem
*item
= NULL
;
533 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
535 node
= node
->GetNext() )
537 item
= node
->GetData();
539 if ( item
->GetId() == itemId
)
542 *itemMenu
= (wxMenu
*)this;
544 else if ( item
->IsSubMenu() )
546 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
550 // don't exit the loop
558 // non recursive search
559 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
561 wxMenuItem
*item
= NULL
;
562 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
565 for ( pos
= 0; node
; pos
++ )
567 if ( node
->GetData()->GetId() == id
)
569 item
= node
->GetData();
574 node
= node
->GetNext();
579 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
586 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
588 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
589 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
591 return m_items
.Item( position
)->GetData();
594 // ----------------------------------------------------------------------------
595 // wxMenu helpers used by derived classes
596 // ----------------------------------------------------------------------------
598 // Update a menu and all submenus recursively. source is the object that has
599 // the update event handlers defined for it. If NULL, the menu or associated
600 // window will be used.
601 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
603 if (GetInvokingWindow())
605 // Don't update menus if the parent
606 // frame is about to get deleted
607 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
608 if (tlw
&& wxPendingDelete
.Member(tlw
))
612 if ( !source
&& GetInvokingWindow() )
613 source
= GetInvokingWindow()->GetEventHandler();
615 source
= GetEventHandler();
619 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
622 wxMenuItem
* item
= node
->GetData();
623 if ( !item
->IsSeparator() )
625 wxWindowID id
= item
->GetId();
626 wxUpdateUIEvent
event(id
);
627 event
.SetEventObject( source
);
629 if ( source
->ProcessEvent(event
) )
631 // if anything changed, update the changed attribute
632 if (event
.GetSetText())
633 SetLabel(id
, event
.GetText());
634 if (event
.GetSetChecked())
635 Check(id
, event
.GetChecked());
636 if (event
.GetSetEnabled())
637 Enable(id
, event
.GetEnabled());
640 // recurse to the submenus
641 if ( item
->GetSubMenu() )
642 item
->GetSubMenu()->UpdateUI(source
);
644 //else: item is a separator (which doesn't process update UI events)
646 node
= node
->GetNext();
650 bool wxMenuBase::SendEvent(int id
, int checked
)
652 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
653 event
.SetEventObject(this);
654 event
.SetInt(checked
);
656 bool processed
= false;
658 // Try the menu's event handler first
659 wxEvtHandler
*handler
= GetEventHandler();
661 processed
= handler
->SafelyProcessEvent(event
);
663 // Try the window the menu was popped up from or its menu bar belongs to
666 wxWindow
* const win
= GetWindow();
668 processed
= win
->HandleWindowEvent(event
);
674 // ----------------------------------------------------------------------------
675 // wxMenu attaching/detaching to/from menu bar
676 // ----------------------------------------------------------------------------
678 wxMenuBar
* wxMenuBase::GetMenuBar() const
681 return GetParent()->GetMenuBar();
685 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
687 // use Detach() instead!
688 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
690 // use IsAttached() to prevent this from happening
691 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
693 m_menuBar
= (wxMenuBar
*)menubar
;
696 void wxMenuBase::Detach()
698 // use IsAttached() to prevent this from happening
699 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
704 // ----------------------------------------------------------------------------
705 // wxMenu invoking window handling
706 // ----------------------------------------------------------------------------
708 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
710 wxASSERT_MSG( !GetParent(),
711 "should only be called for top level popup menus" );
712 wxASSERT_MSG( !IsAttached(),
713 "menus attached to menu bar can't have invoking window" );
715 m_invokingWindow
= win
;
718 wxWindow
*wxMenuBase::GetWindow() const
720 // only the top level menus have non-NULL invoking window or a pointer to
721 // the menu bar so recurse upwards until we find it
722 const wxMenuBase
*menu
= this;
723 while ( menu
->GetParent() )
725 menu
= menu
->GetParent();
728 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
729 : menu
->GetInvokingWindow();
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
->SetItemLabel(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
->GetItemLabel();
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
& title
)
838 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
839 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
841 m_menus
.Append(menu
);
847 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
848 const wxString
& title
)
850 if ( pos
== m_menus
.GetCount() )
852 return wxMenuBarBase::Append(menu
, title
);
854 else // not at the end
856 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
858 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
859 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
861 m_menus
.Insert(node
, menu
);
868 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
869 const wxString
& WXUNUSED(title
))
871 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
873 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
874 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
876 wxMenu
*menuOld
= node
->GetData();
885 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
887 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
888 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
890 wxMenu
*menu
= node
->GetData();
897 int wxMenuBarBase::FindMenu(const wxString
& title
) const
899 wxString label
= wxMenuItem::GetLabelText(title
);
901 size_t count
= GetMenuCount();
902 for ( size_t i
= 0; i
< count
; i
++ )
904 wxString title2
= GetMenuLabel(i
);
905 if ( (title2
== title
) ||
906 (wxMenuItem::GetLabelText(title2
) == label
) )
917 // ----------------------------------------------------------------------------
918 // wxMenuBar attaching/detaching to/from the frame
919 // ----------------------------------------------------------------------------
921 void wxMenuBarBase::Attach(wxFrame
*frame
)
923 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
925 m_menuBarFrame
= frame
;
928 void wxMenuBarBase::Detach()
930 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
932 m_menuBarFrame
= NULL
;
935 // ----------------------------------------------------------------------------
936 // wxMenuBar searching for items
937 // ----------------------------------------------------------------------------
939 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
944 wxMenuItem
*item
= NULL
;
945 size_t count
= GetMenuCount(), i
;
946 wxMenuList::const_iterator it
;
947 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
949 item
= (*it
)->FindItem(id
, menu
);
955 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
957 wxString label
= wxMenuItem::GetLabelText(menu
);
960 wxMenuList::compatibility_iterator node
;
961 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
963 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
964 return node
->GetData()->FindItem(item
);
970 // ---------------------------------------------------------------------------
971 // wxMenuBar functions forwarded to wxMenuItem
972 // ---------------------------------------------------------------------------
974 void wxMenuBarBase::Enable(int id
, bool enable
)
976 wxMenuItem
*item
= FindItem(id
);
978 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
980 item
->Enable(enable
);
983 void wxMenuBarBase::Check(int id
, bool check
)
985 wxMenuItem
*item
= FindItem(id
);
987 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
988 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
993 bool wxMenuBarBase::IsChecked(int id
) const
995 wxMenuItem
*item
= FindItem(id
);
997 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
999 return item
->IsChecked();
1002 bool wxMenuBarBase::IsEnabled(int id
) const
1004 wxMenuItem
*item
= FindItem(id
);
1006 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1008 return item
->IsEnabled();
1011 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1013 wxMenuItem
*item
= FindItem(id
);
1015 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1017 item
->SetItemLabel(label
);
1020 wxString
wxMenuBarBase::GetLabel(int id
) const
1022 wxMenuItem
*item
= FindItem(id
);
1024 wxCHECK_MSG( item
, wxEmptyString
,
1025 wxT("wxMenuBar::GetLabel(): no such item") );
1027 return item
->GetItemLabel();
1030 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1032 wxMenuItem
*item
= FindItem(id
);
1034 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1036 item
->SetHelp(helpString
);
1039 wxString
wxMenuBarBase::GetHelpString(int id
) const
1041 wxMenuItem
*item
= FindItem(id
);
1043 wxCHECK_MSG( item
, wxEmptyString
,
1044 wxT("wxMenuBar::GetHelpString(): no such item") );
1046 return item
->GetHelp();
1049 void wxMenuBarBase::UpdateMenus()
1051 wxEvtHandler
* source
;
1053 int nCount
= GetMenuCount();
1054 for (int n
= 0; n
< nCount
; n
++)
1056 menu
= GetMenu( n
);
1059 source
= menu
->GetEventHandler();
1061 menu
->UpdateUI( source
);
1066 #if WXWIN_COMPATIBILITY_2_8
1067 // get or change the label of the menu at given position
1068 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
1070 SetMenuLabel(pos
, label
);
1073 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
1075 return GetMenuLabelText(pos
);
1079 #endif // wxUSE_MENUS