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 wxDEFINE_FLAGS( wxMenuStyle
)
56 wxBEGIN_FLAGS( wxMenuStyle
)
57 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
58 wxEND_FLAGS( wxMenuStyle
)
60 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
, "wx/menu.h")
61 wxCOLLECTION_TYPE_INFO( wxMenuItem
*, wxMenuItemList
) ;
63 #if wxUSE_EXTENDED_RTTI
64 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
,
67 wxListCollectionToAnyList
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
71 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
72 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
74 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), \
75 0 /*flags*/, wxT("Helpstring"), wxT("group") )
77 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long, GetStyle
, \
78 wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, wxT("Helpstring"), \
79 wxT("group")) // style
81 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
*, Append
, \
82 GetMenuItems
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
83 wxEND_PROPERTIES_TABLE()
85 wxEMPTY_HANDLERS_TABLE(wxMenu
)
87 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long, MenuStyle
)
89 wxDEFINE_FLAGS( wxMenuBarStyle
)
91 wxBEGIN_FLAGS( wxMenuBarStyle
)
92 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
93 wxEND_FLAGS( wxMenuBarStyle
)
95 #if wxUSE_EXTENDED_RTTI
96 // the negative id would lead the window (its superclass !) to
97 // vetoe streaming out otherwise
98 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxObjectWriter
*,
99 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& )
105 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
, "wx/menu.h", \
106 wxMenuBarStreamingCallback
)
109 #if wxUSE_EXTENDED_RTTI
110 WX_DEFINE_LIST( wxMenuInfoHelperList
)
112 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfoHelper
, wxObject
, "wx/menu.h")
114 wxBEGIN_PROPERTIES_TABLE(wxMenuInfoHelper
)
115 wxREADONLY_PROPERTY( Menu
, wxMenu
*, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
116 0 /*flags*/, wxT("Helpstring"), wxT("group"))
118 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString(), \
119 0 /*flags*/, wxT("Helpstring"), wxT("group"))
120 wxEND_PROPERTIES_TABLE()
122 wxEMPTY_HANDLERS_TABLE(wxMenuInfoHelper
)
124 wxCONSTRUCTOR_2( wxMenuInfoHelper
, wxMenu
*, Menu
, wxString
, Title
)
126 wxCOLLECTION_TYPE_INFO( wxMenuInfoHelper
*, wxMenuInfoHelperList
) ;
128 template<> void wxCollectionToVariantArray( wxMenuInfoHelperList
const &theList
,
131 wxListCollectionToAnyList
<wxMenuInfoHelperList::compatibility_iterator
>( theList
, value
) ;
136 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
137 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoHelperList
, wxMenuInfoHelper
*, AppendMenuInfo
, \
138 GetMenuInfos
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
139 wxEND_PROPERTIES_TABLE()
141 wxEMPTY_HANDLERS_TABLE(wxMenuBar
)
143 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
145 #if wxUSE_EXTENDED_RTTI
147 const wxMenuInfoHelperList
& wxMenuBarBase::GetMenuInfos() const
149 wxMenuInfoHelperList
* list
= const_cast< wxMenuInfoHelperList
* > (& m_menuInfos
);
150 WX_CLEAR_LIST( wxMenuInfoHelperList
, *list
);
151 for (size_t i
= 0 ; i
< GetMenuCount(); ++i
)
153 wxMenuInfoHelper
* info
= new wxMenuInfoHelper();
154 info
->Create( GetMenu(i
), GetMenuLabel(i
));
162 // ----------------------------------------------------------------------------
163 // XTI for wxMenuItem
164 // ----------------------------------------------------------------------------
166 #if wxUSE_EXTENDED_RTTI
168 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxObjectWriter
*,
169 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& )
171 const wxMenuItem
* mitem
= wx_dynamic_cast(const wxMenuItem
*, object
);
172 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
174 // we don't stream out the first two items for menus with a title,
175 // they will be reconstructed
176 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
||
177 mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
185 wxBEGIN_ENUM( wxItemKind
)
186 wxENUM_MEMBER( wxITEM_SEPARATOR
)
187 wxENUM_MEMBER( wxITEM_NORMAL
)
188 wxENUM_MEMBER( wxITEM_CHECK
)
189 wxENUM_MEMBER( wxITEM_RADIO
)
190 wxEND_ENUM( wxItemKind
)
192 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
, "wx/menuitem.h", \
193 wxMenuItemStreamingCallback
)
195 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
196 wxPROPERTY( Parent
, wxMenu
*, SetMenu
, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
197 0 /*flags*/, wxT("Helpstring"), wxT("group") )
198 wxPROPERTY( Id
, int, SetId
, GetId
, wxEMPTY_PARAMETER_VALUE
, \
199 0 /*flags*/, wxT("Helpstring"), wxT("group") )
200 wxPROPERTY( ItemLabel
, wxString
, SetItemLabel
, GetItemLabel
, wxString(), \
201 0 /*flags*/, wxT("Helpstring"), wxT("group") )
202 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), \
203 0 /*flags*/, wxT("Helpstring"), wxT("group") )
204 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, wxEMPTY_PARAMETER_VALUE
, \
205 0 /*flags*/, wxT("Helpstring"), wxT("group") )
206 wxPROPERTY( SubMenu
, wxMenu
*, SetSubMenu
, GetSubMenu
, wxEMPTY_PARAMETER_VALUE
, \
207 0 /*flags*/, wxT("Helpstring"), wxT("group") )
208 wxPROPERTY( Enabled
, bool, Enable
, IsEnabled
, wxAny((bool)true), \
209 0 /*flags*/, wxT("Helpstring"), wxT("group") )
210 wxPROPERTY( Checked
, bool, Check
, IsChecked
, wxAny((bool)false), \
211 0 /*flags*/, wxT("Helpstring"), wxT("group") )
212 wxPROPERTY( Checkable
, bool, SetCheckable
, IsCheckable
, wxAny((bool)false), \
213 0 /*flags*/, wxT("Helpstring"), wxT("group") )
214 wxEND_PROPERTIES_TABLE()
216 wxEMPTY_HANDLERS_TABLE(wxMenuItem
)
218 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
*, Parent
, int, Id
, wxString
, \
219 Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
*, SubMenu
)
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
227 const wxString
& text
,
228 const wxString
& help
,
235 m_id
= wxWindow::NewControlId();
239 m_id
= wxID_SEPARATOR
;
241 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
242 // and it makes sense to omit the following optional parameters,
243 // including the kind one which doesn't default to wxITEM_SEPARATOR,
244 // of course, so override it here
245 kind
= wxITEM_SEPARATOR
;
249 // (popup) menu titles in wxMSW use this ID to indicate that
250 // it's not a real menu item, so we don't want the check below to
256 // ids are limited to 16 bits under MSW so portable code shouldn't
257 // use ids outside of this range (negative ids generated by wx are
259 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
260 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
261 wxS("invalid id value") );
265 // notice that parentMenu can be NULL: the item can be attached to the menu
266 // later with SetMenu()
268 m_parentMenu
= parentMenu
;
278 wxMenuItemBase::~wxMenuItemBase()
285 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
287 return wxAcceleratorEntry::Create(GetItemLabel());
290 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
292 wxString text
= m_text
.BeforeFirst(wxT('\t'));
296 text
+= accel
->ToString();
302 #endif // wxUSE_ACCEL
304 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
308 if ( m_text
.empty() && !IsSeparator() )
310 wxASSERT_MSG( wxIsStockID(GetId()),
311 wxT("A non-stock menu item with an empty label?") );
312 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
313 wxSTOCK_WITH_MNEMONIC
);
317 void wxMenuItemBase::SetHelp(const wxString
& str
)
321 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
323 // get a stock help string
324 m_help
= wxGetStockHelpString(GetId());
329 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
331 return wxStripMenuCodes(text
);
335 #if WXWIN_COMPATIBILITY_2_8
336 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
338 return GetLabelText(text
);
342 bool wxMenuBase::ms_locked
= true;
344 // ----------------------------------------------------------------------------
345 // wxMenu ctor and dtor
346 // ----------------------------------------------------------------------------
348 void wxMenuBase::Init(long style
)
353 m_invokingWindow
= NULL
;
356 m_eventHandler
= this;
359 wxMenuBase::~wxMenuBase()
361 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
364 // ----------------------------------------------------------------------------
365 // wxMenu item adding/removing
366 // ----------------------------------------------------------------------------
368 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
370 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
372 submenu
->SetParent((wxMenu
*)this);
375 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
377 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
379 m_items
.Append(item
);
380 item
->SetMenu((wxMenu
*)this);
381 if ( item
->IsSubMenu() )
383 AddSubMenu(item
->GetSubMenu());
389 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
391 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
393 if ( pos
== GetMenuItemCount() )
395 return DoAppend(item
);
399 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
400 wxT("invalid index in wxMenu::Insert") );
402 return DoInsert(pos
, item
);
406 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
408 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
410 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
411 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
413 m_items
.Insert(node
, item
);
414 item
->SetMenu((wxMenu
*)this);
415 if ( item
->IsSubMenu() )
417 AddSubMenu(item
->GetSubMenu());
423 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
425 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
427 return DoRemove(item
);
430 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
432 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
434 // if we get here, the item is valid or one of Remove() functions is broken
435 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
437 // we detach the item, but we do delete the list node (i.e. don't call
438 // DetachNode() here!)
441 // item isn't attached to anything any more
443 wxMenu
*submenu
= item
->GetSubMenu();
446 submenu
->SetParent(NULL
);
447 if ( submenu
->IsAttached() )
454 bool wxMenuBase::Delete(wxMenuItem
*item
)
456 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
458 return DoDelete(item
);
461 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
463 wxMenuItem
*item2
= DoRemove(item
);
464 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
466 // don't delete the submenu
467 item2
->SetSubMenu(NULL
);
474 bool wxMenuBase::Destroy(wxMenuItem
*item
)
476 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
478 return DoDestroy(item
);
481 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
483 wxMenuItem
*item2
= DoRemove(item
);
484 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
491 // ----------------------------------------------------------------------------
492 // wxMenu searching for items
493 // ----------------------------------------------------------------------------
495 // Finds the item id matching the given string, wxNOT_FOUND if not found.
496 int wxMenuBase::FindItem(const wxString
& text
) const
498 wxString label
= wxMenuItem::GetLabelText(text
);
499 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
501 node
= node
->GetNext() )
503 wxMenuItem
*item
= node
->GetData();
504 if ( item
->IsSubMenu() )
506 int rc
= item
->GetSubMenu()->FindItem(label
);
507 if ( rc
!= wxNOT_FOUND
)
511 // we execute this code for submenus as well to alllow finding them by
512 // name just like the ordinary items
513 if ( !item
->IsSeparator() )
515 if ( item
->GetItemLabelText() == label
)
516 return item
->GetId();
523 // recursive search for item by id
524 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
529 wxMenuItem
*item
= NULL
;
530 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
532 node
= node
->GetNext() )
534 item
= node
->GetData();
536 if ( item
->GetId() == itemId
)
539 *itemMenu
= (wxMenu
*)this;
541 else if ( item
->IsSubMenu() )
543 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
547 // don't exit the loop
555 // non recursive search
556 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
558 wxMenuItem
*item
= NULL
;
559 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
562 for ( pos
= 0; node
; pos
++ )
564 if ( node
->GetData()->GetId() == id
)
566 item
= node
->GetData();
571 node
= node
->GetNext();
576 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
583 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
585 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
586 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
588 return m_items
.Item( position
)->GetData();
591 // ----------------------------------------------------------------------------
592 // wxMenu helpers used by derived classes
593 // ----------------------------------------------------------------------------
595 // Update a menu and all submenus recursively. source is the object that has
596 // the update event handlers defined for it. If NULL, the menu or associated
597 // window will be used.
598 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
600 if (GetInvokingWindow())
602 // Don't update menus if the parent
603 // frame is about to get deleted
604 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
605 if (tlw
&& wxPendingDelete
.Member(tlw
))
609 if ( !source
&& GetInvokingWindow() )
610 source
= GetInvokingWindow()->GetEventHandler();
612 source
= GetEventHandler();
616 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
619 wxMenuItem
* item
= node
->GetData();
620 if ( !item
->IsSeparator() )
622 wxWindowID id
= item
->GetId();
623 wxUpdateUIEvent
event(id
);
624 event
.SetEventObject( source
);
626 if ( source
->ProcessEvent(event
) )
628 // if anything changed, update the changed attribute
629 if (event
.GetSetText())
630 SetLabel(id
, event
.GetText());
631 if (event
.GetSetChecked())
632 Check(id
, event
.GetChecked());
633 if (event
.GetSetEnabled())
634 Enable(id
, event
.GetEnabled());
637 // recurse to the submenus
638 if ( item
->GetSubMenu() )
639 item
->GetSubMenu()->UpdateUI(source
);
641 //else: item is a separator (which doesn't process update UI events)
643 node
= node
->GetNext();
647 bool wxMenuBase::SendEvent(int id
, int checked
)
649 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
650 event
.SetEventObject(this);
651 event
.SetInt(checked
);
653 bool processed
= false;
655 // Try the menu's event handler first
656 wxEvtHandler
*handler
= GetEventHandler();
658 processed
= handler
->SafelyProcessEvent(event
);
660 // Try the window the menu was popped up from or its menu bar belongs to
663 wxWindow
* const win
= GetWindow();
665 processed
= win
->HandleWindowEvent(event
);
671 // ----------------------------------------------------------------------------
672 // wxMenu attaching/detaching to/from menu bar
673 // ----------------------------------------------------------------------------
675 wxMenuBar
* wxMenuBase::GetMenuBar() const
678 return GetParent()->GetMenuBar();
682 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
684 // use Detach() instead!
685 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
687 // use IsAttached() to prevent this from happening
688 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
690 m_menuBar
= (wxMenuBar
*)menubar
;
693 void wxMenuBase::Detach()
695 // use IsAttached() to prevent this from happening
696 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
701 // ----------------------------------------------------------------------------
702 // wxMenu invoking window handling
703 // ----------------------------------------------------------------------------
705 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
707 wxASSERT_MSG( !GetParent(),
708 "should only be called for top level popup menus" );
709 wxASSERT_MSG( !IsAttached(),
710 "menus attached to menu bar can't have invoking window" );
712 m_invokingWindow
= win
;
715 wxWindow
*wxMenuBase::GetWindow() const
717 // only the top level menus have non-NULL invoking window or a pointer to
718 // the menu bar so recurse upwards until we find it
719 const wxMenuBase
*menu
= this;
720 while ( menu
->GetParent() )
722 menu
= menu
->GetParent();
725 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
726 : menu
->GetInvokingWindow();
729 // ----------------------------------------------------------------------------
730 // wxMenu functions forwarded to wxMenuItem
731 // ----------------------------------------------------------------------------
733 void wxMenuBase::Enable( int id
, bool enable
)
735 wxMenuItem
*item
= FindItem(id
);
737 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
739 item
->Enable(enable
);
742 bool wxMenuBase::IsEnabled( int id
) const
744 wxMenuItem
*item
= FindItem(id
);
746 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
748 return item
->IsEnabled();
751 void wxMenuBase::Check( int id
, bool enable
)
753 wxMenuItem
*item
= FindItem(id
);
755 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
760 bool wxMenuBase::IsChecked( int id
) const
762 wxMenuItem
*item
= FindItem(id
);
764 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
766 return item
->IsChecked();
769 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
771 wxMenuItem
*item
= FindItem(id
);
773 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
775 item
->SetItemLabel(label
);
778 wxString
wxMenuBase::GetLabel( int id
) const
780 wxMenuItem
*item
= FindItem(id
);
782 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
784 return item
->GetItemLabel();
787 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
789 wxMenuItem
*item
= FindItem(id
);
791 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
793 item
->SetHelp( helpString
);
796 wxString
wxMenuBase::GetHelpString( int id
) const
798 wxMenuItem
*item
= FindItem(id
);
800 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
802 return item
->GetHelp();
805 // ----------------------------------------------------------------------------
806 // wxMenuBarBase ctor and dtor
807 // ----------------------------------------------------------------------------
809 wxMenuBarBase::wxMenuBarBase()
812 m_menuBarFrame
= NULL
;
815 wxMenuBarBase::~wxMenuBarBase()
817 WX_CLEAR_LIST(wxMenuList
, m_menus
);
820 // ----------------------------------------------------------------------------
821 // wxMenuBar item access: the base class versions manage m_menus list, the
822 // derived class should reflect the changes in the real menubar
823 // ----------------------------------------------------------------------------
825 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
827 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
828 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
830 return node
->GetData();
833 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
835 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
836 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
838 m_menus
.Append(menu
);
844 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
845 const wxString
& title
)
847 if ( pos
== m_menus
.GetCount() )
849 return wxMenuBarBase::Append(menu
, title
);
851 else // not at the end
853 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
855 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
856 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
858 m_menus
.Insert(node
, menu
);
865 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
866 const wxString
& WXUNUSED(title
))
868 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
870 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
871 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
873 wxMenu
*menuOld
= node
->GetData();
882 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
884 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
885 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
887 wxMenu
*menu
= node
->GetData();
894 int wxMenuBarBase::FindMenu(const wxString
& title
) const
896 wxString label
= wxMenuItem::GetLabelText(title
);
898 size_t count
= GetMenuCount();
899 for ( size_t i
= 0; i
< count
; i
++ )
901 wxString title2
= GetMenuLabel(i
);
902 if ( (title2
== title
) ||
903 (wxMenuItem::GetLabelText(title2
) == label
) )
914 // ----------------------------------------------------------------------------
915 // wxMenuBar attaching/detaching to/from the frame
916 // ----------------------------------------------------------------------------
918 void wxMenuBarBase::Attach(wxFrame
*frame
)
920 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
922 m_menuBarFrame
= frame
;
925 void wxMenuBarBase::Detach()
927 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
929 m_menuBarFrame
= NULL
;
932 // ----------------------------------------------------------------------------
933 // wxMenuBar searching for items
934 // ----------------------------------------------------------------------------
936 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
941 wxMenuItem
*item
= NULL
;
942 size_t count
= GetMenuCount(), i
;
943 wxMenuList::const_iterator it
;
944 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
946 item
= (*it
)->FindItem(id
, menu
);
952 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
954 wxString label
= wxMenuItem::GetLabelText(menu
);
957 wxMenuList::compatibility_iterator node
;
958 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
960 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
961 return node
->GetData()->FindItem(item
);
967 // ---------------------------------------------------------------------------
968 // wxMenuBar functions forwarded to wxMenuItem
969 // ---------------------------------------------------------------------------
971 void wxMenuBarBase::Enable(int id
, bool enable
)
973 wxMenuItem
*item
= FindItem(id
);
975 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
977 item
->Enable(enable
);
980 void wxMenuBarBase::Check(int id
, bool check
)
982 wxMenuItem
*item
= FindItem(id
);
984 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
985 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
990 bool wxMenuBarBase::IsChecked(int id
) const
992 wxMenuItem
*item
= FindItem(id
);
994 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
996 return item
->IsChecked();
999 bool wxMenuBarBase::IsEnabled(int id
) const
1001 wxMenuItem
*item
= FindItem(id
);
1003 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1005 return item
->IsEnabled();
1008 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1010 wxMenuItem
*item
= FindItem(id
);
1012 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1014 item
->SetItemLabel(label
);
1017 wxString
wxMenuBarBase::GetLabel(int id
) const
1019 wxMenuItem
*item
= FindItem(id
);
1021 wxCHECK_MSG( item
, wxEmptyString
,
1022 wxT("wxMenuBar::GetLabel(): no such item") );
1024 return item
->GetItemLabel();
1027 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1029 wxMenuItem
*item
= FindItem(id
);
1031 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1033 item
->SetHelp(helpString
);
1036 wxString
wxMenuBarBase::GetHelpString(int id
) const
1038 wxMenuItem
*item
= FindItem(id
);
1040 wxCHECK_MSG( item
, wxEmptyString
,
1041 wxT("wxMenuBar::GetHelpString(): no such item") );
1043 return item
->GetHelp();
1046 void wxMenuBarBase::UpdateMenus()
1048 wxEvtHandler
* source
;
1050 int nCount
= GetMenuCount();
1051 for (int n
= 0; n
< nCount
; n
++)
1053 menu
= GetMenu( n
);
1056 source
= menu
->GetEventHandler();
1058 menu
->UpdateUI( source
);
1063 #if WXWIN_COMPATIBILITY_2_8
1064 // get or change the label of the menu at given position
1065 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
1067 SetMenuLabel(pos
, label
);
1070 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
1072 return GetMenuLabelText(pos
);
1076 #endif // wxUSE_MENUS