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 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
,
66 wxListCollectionToAnyList
<wxMenuItemList
::compatibility_iterator
>( theList
, value
) ;
69 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
70 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
72 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), \
73 0 /*flags*/, wxT("Helpstring"), wxT("group") )
75 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long, GetStyle
, \
76 wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, wxT("Helpstring"), \
77 wxT("group")) // style
79 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
*, Append
, \
80 GetMenuItems
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
81 wxEND_PROPERTIES_TABLE()
83 wxEMPTY_HANDLERS_TABLE(wxMenu
)
85 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long, MenuStyle
)
87 wxDEFINE_FLAGS( wxMenuBarStyle
)
89 wxBEGIN_FLAGS( wxMenuBarStyle
)
90 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
91 wxEND_FLAGS( wxMenuBarStyle
)
93 // the negative id would lead the window (its superclass !) to
94 // vetoe streaming out otherwise
95 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxObjectWriter
*,
96 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& )
101 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
, "wx/menu.h", \
102 wxMenuBarStreamingCallback
)
105 #if wxUSE_EXTENDED_RTTI
106 WX_DEFINE_LIST( wxMenuInfoHelperList
)
108 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfoHelper
, wxObject
, "wx/menu.h")
110 wxBEGIN_PROPERTIES_TABLE(wxMenuInfoHelper
)
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(wxMenuInfoHelper
)
120 wxCONSTRUCTOR_2( wxMenuInfoHelper
, wxMenu
*, Menu
, wxString
, Title
)
122 wxCOLLECTION_TYPE_INFO( wxMenuInfoHelper
*, wxMenuInfoHelperList
) ;
124 template<> void wxCollectionToVariantArray( wxMenuInfoHelperList
const &theList
,
127 wxListCollectionToAnyList
<wxMenuInfoHelperList
::compatibility_iterator
>( theList
, value
) ;
132 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
133 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoHelperList
, wxMenuInfoHelper
*, AppendMenuInfo
, \
134 GetMenuInfos
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
135 wxEND_PROPERTIES_TABLE()
137 wxEMPTY_HANDLERS_TABLE(wxMenuBar
)
139 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
141 #if wxUSE_EXTENDED_RTTI
143 const wxMenuInfoHelperList
& wxMenuBarBase
::GetMenuInfos() const
145 wxMenuInfoHelperList
* list
= const_cast< wxMenuInfoHelperList
* > (& m_menuInfos
);
146 WX_CLEAR_LIST( wxMenuInfoHelperList
, *list
);
147 for (size_t i
= 0 ; i
< GetMenuCount(); ++i
)
149 wxMenuInfoHelper
* info
= new wxMenuInfoHelper();
150 info
->Create( GetMenu(i
), GetMenuLabel(i
));
158 // ----------------------------------------------------------------------------
159 // XTI for wxMenuItem
160 // ----------------------------------------------------------------------------
162 #if wxUSE_EXTENDED_RTTI
164 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxObjectWriter
*,
165 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& )
167 const wxMenuItem
* mitem
= wx_dynamic_cast(const wxMenuItem
*, object
);
168 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
170 // we don't stream out the first two items for menus with a title,
171 // they will be reconstructed
172 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
||
173 mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
181 wxBEGIN_ENUM( wxItemKind
)
182 wxENUM_MEMBER( wxITEM_SEPARATOR
)
183 wxENUM_MEMBER( wxITEM_NORMAL
)
184 wxENUM_MEMBER( wxITEM_CHECK
)
185 wxENUM_MEMBER( wxITEM_RADIO
)
186 wxEND_ENUM( wxItemKind
)
188 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
, "wx/menuitem.h", \
189 wxMenuItemStreamingCallback
)
191 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
192 wxPROPERTY( Parent
, wxMenu
*, SetMenu
, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
193 0 /*flags*/, wxT("Helpstring"), wxT("group") )
194 wxPROPERTY( Id
, int, SetId
, GetId
, wxEMPTY_PARAMETER_VALUE
, \
195 0 /*flags*/, wxT("Helpstring"), wxT("group") )
196 wxPROPERTY( ItemLabel
, wxString
, SetItemLabel
, GetItemLabel
, wxString(), \
197 0 /*flags*/, wxT("Helpstring"), wxT("group") )
198 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), \
199 0 /*flags*/, wxT("Helpstring"), wxT("group") )
200 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, wxEMPTY_PARAMETER_VALUE
, \
201 0 /*flags*/, wxT("Helpstring"), wxT("group") )
202 wxPROPERTY( SubMenu
, wxMenu
*, SetSubMenu
, GetSubMenu
, wxEMPTY_PARAMETER_VALUE
, \
203 0 /*flags*/, wxT("Helpstring"), wxT("group") )
204 wxPROPERTY( Enabled
, bool, Enable
, IsEnabled
, wxAny((bool)true), \
205 0 /*flags*/, wxT("Helpstring"), wxT("group") )
206 wxPROPERTY( Checked
, bool, Check
, IsChecked
, wxAny((bool)false), \
207 0 /*flags*/, wxT("Helpstring"), wxT("group") )
208 wxPROPERTY( Checkable
, bool, SetCheckable
, IsCheckable
, wxAny((bool)false), \
209 0 /*flags*/, wxT("Helpstring"), wxT("group") )
210 wxEND_PROPERTIES_TABLE()
212 wxEMPTY_HANDLERS_TABLE(wxMenuItem
)
214 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
*, Parent
, int, Id
, wxString
, \
215 Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
*, SubMenu
)
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 wxMenuItemBase
::wxMenuItemBase(wxMenu
*parentMenu
,
223 const wxString
& text
,
224 const wxString
& help
,
231 m_id
= wxWindow
::NewControlId();
235 m_id
= wxID_SEPARATOR
;
237 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
238 // and it makes sense to omit the following optional parameters,
239 // including the kind one which doesn't default to wxITEM_SEPARATOR,
240 // of course, so override it here
241 kind
= wxITEM_SEPARATOR
;
245 // (popup) menu titles in wxMSW use this ID to indicate that
246 // it's not a real menu item, so we don't want the check below to
252 // ids are limited to 16 bits under MSW so portable code shouldn't
253 // use ids outside of this range (negative ids generated by wx are
255 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
256 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
257 wxS("invalid id value") );
261 // notice that parentMenu can be NULL: the item can be attached to the menu
262 // later with SetMenu()
264 m_parentMenu
= parentMenu
;
274 wxMenuItemBase
::~wxMenuItemBase()
281 wxAcceleratorEntry
*wxMenuItemBase
::GetAccel() const
283 return wxAcceleratorEntry
::Create(GetItemLabel());
286 void wxMenuItemBase
::SetAccel(wxAcceleratorEntry
*accel
)
288 wxString text
= m_text
.BeforeFirst(wxT('\t'));
292 text
+= accel
->ToString();
298 #endif // wxUSE_ACCEL
300 void wxMenuItemBase
::SetItemLabel(const wxString
& str
)
304 if ( m_text
.empty() && !IsSeparator() )
306 wxASSERT_MSG( wxIsStockID(GetId()),
307 wxT("A non-stock menu item with an empty label?") );
308 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
309 wxSTOCK_WITH_MNEMONIC
);
313 void wxMenuItemBase
::SetHelp(const wxString
& str
)
317 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
319 // get a stock help string
320 m_help
= wxGetStockHelpString(GetId());
325 wxString wxMenuItemBase
::GetLabelText(const wxString
& text
)
327 return wxStripMenuCodes(text
);
331 #if WXWIN_COMPATIBILITY_2_8
332 wxString wxMenuItemBase
::GetLabelFromText(const wxString
& text
)
334 return GetLabelText(text
);
338 bool wxMenuBase
::ms_locked
= true;
340 // ----------------------------------------------------------------------------
341 // wxMenu ctor and dtor
342 // ----------------------------------------------------------------------------
344 void wxMenuBase
::Init(long style
)
349 m_invokingWindow
= NULL
;
352 m_eventHandler
= this;
355 wxMenuBase
::~wxMenuBase()
357 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
360 // ----------------------------------------------------------------------------
361 // wxMenu item adding/removing
362 // ----------------------------------------------------------------------------
364 void wxMenuBase
::AddSubMenu(wxMenu
*submenu
)
366 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
368 submenu
->SetParent((wxMenu
*)this);
371 wxMenuItem
* wxMenuBase
::DoAppend(wxMenuItem
*item
)
373 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
375 m_items
.Append(item
);
376 item
->SetMenu((wxMenu
*)this);
377 if ( item
->IsSubMenu() )
379 AddSubMenu(item
->GetSubMenu());
385 wxMenuItem
* wxMenuBase
::Insert(size_t pos
, wxMenuItem
*item
)
387 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
389 if ( pos
== GetMenuItemCount() )
391 return DoAppend(item
);
395 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
396 wxT("invalid index in wxMenu::Insert") );
398 return DoInsert(pos
, item
);
402 wxMenuItem
* wxMenuBase
::DoInsert(size_t pos
, wxMenuItem
*item
)
404 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
406 wxMenuItemList
::compatibility_iterator node
= m_items
.Item(pos
);
407 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
409 m_items
.Insert(node
, item
);
410 item
->SetMenu((wxMenu
*)this);
411 if ( item
->IsSubMenu() )
413 AddSubMenu(item
->GetSubMenu());
419 wxMenuItem
*wxMenuBase
::Remove(wxMenuItem
*item
)
421 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
423 return DoRemove(item
);
426 wxMenuItem
*wxMenuBase
::DoRemove(wxMenuItem
*item
)
428 wxMenuItemList
::compatibility_iterator node
= m_items
.Find(item
);
430 // if we get here, the item is valid or one of Remove() functions is broken
431 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
433 // we detach the item, but we do delete the list node (i.e. don't call
434 // DetachNode() here!)
437 // item isn't attached to anything any more
439 wxMenu
*submenu
= item
->GetSubMenu();
442 submenu
->SetParent(NULL
);
443 if ( submenu
->IsAttached() )
450 bool wxMenuBase
::Delete(wxMenuItem
*item
)
452 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
454 return DoDelete(item
);
457 bool wxMenuBase
::DoDelete(wxMenuItem
*item
)
459 wxMenuItem
*item2
= DoRemove(item
);
460 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
462 // don't delete the submenu
463 item2
->SetSubMenu(NULL
);
470 bool wxMenuBase
::Destroy(wxMenuItem
*item
)
472 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
474 return DoDestroy(item
);
477 bool wxMenuBase
::DoDestroy(wxMenuItem
*item
)
479 wxMenuItem
*item2
= DoRemove(item
);
480 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
487 // ----------------------------------------------------------------------------
488 // wxMenu searching for items
489 // ----------------------------------------------------------------------------
491 // Finds the item id matching the given string, wxNOT_FOUND if not found.
492 int wxMenuBase
::FindItem(const wxString
& text
) const
494 wxString label
= wxMenuItem
::GetLabelText(text
);
495 for ( wxMenuItemList
::compatibility_iterator node
= m_items
.GetFirst();
497 node
= node
->GetNext() )
499 wxMenuItem
*item
= node
->GetData();
500 if ( item
->IsSubMenu() )
502 int rc
= item
->GetSubMenu()->FindItem(label
);
503 if ( rc
!= wxNOT_FOUND
)
507 // we execute this code for submenus as well to alllow finding them by
508 // name just like the ordinary items
509 if ( !item
->IsSeparator() )
511 if ( item
->GetItemLabelText() == label
)
512 return item
->GetId();
519 // recursive search for item by id
520 wxMenuItem
*wxMenuBase
::FindItem(int itemId
, wxMenu
**itemMenu
) const
525 wxMenuItem
*item
= NULL
;
526 for ( wxMenuItemList
::compatibility_iterator node
= m_items
.GetFirst();
528 node
= node
->GetNext() )
530 item
= node
->GetData();
532 if ( item
->GetId() == itemId
)
535 *itemMenu
= (wxMenu
*)this;
537 else if ( item
->IsSubMenu() )
539 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
543 // don't exit the loop
551 // non recursive search
552 wxMenuItem
*wxMenuBase
::FindChildItem(int id
, size_t *ppos
) const
554 wxMenuItem
*item
= NULL
;
555 wxMenuItemList
::compatibility_iterator node
= GetMenuItems().GetFirst();
558 for ( pos
= 0; node
; pos
++ )
560 if ( node
->GetData()->GetId() == id
)
562 item
= node
->GetData();
567 node
= node
->GetNext();
572 *ppos
= item ? pos
: (size_t)wxNOT_FOUND
;
579 wxMenuItem
* wxMenuBase
::FindItemByPosition(size_t position
) const
581 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
582 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
584 return m_items
.Item( position
)->GetData();
587 // ----------------------------------------------------------------------------
588 // wxMenu helpers used by derived classes
589 // ----------------------------------------------------------------------------
591 // Update a menu and all submenus recursively. source is the object that has
592 // the update event handlers defined for it. If NULL, the menu or associated
593 // window will be used.
594 void wxMenuBase
::UpdateUI(wxEvtHandler
* source
)
596 if (GetInvokingWindow())
598 // Don't update menus if the parent
599 // frame is about to get deleted
600 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
601 if (tlw
&& wxPendingDelete
.Member(tlw
))
605 if ( !source
&& GetInvokingWindow() )
606 source
= GetInvokingWindow()->GetEventHandler();
608 source
= GetEventHandler();
612 wxMenuItemList
::compatibility_iterator node
= GetMenuItems().GetFirst();
615 wxMenuItem
* item
= node
->GetData();
616 if ( !item
->IsSeparator() )
618 wxWindowID id
= item
->GetId();
619 wxUpdateUIEvent
event(id
);
620 event
.SetEventObject( source
);
622 if ( source
->ProcessEvent(event
) )
624 // if anything changed, update the changed attribute
625 if (event
.GetSetText())
626 SetLabel(id
, event
.GetText());
627 if (event
.GetSetChecked())
628 Check(id
, event
.GetChecked());
629 if (event
.GetSetEnabled())
630 Enable(id
, event
.GetEnabled());
633 // recurse to the submenus
634 if ( item
->GetSubMenu() )
635 item
->GetSubMenu()->UpdateUI(source
);
637 //else: item is a separator (which doesn't process update UI events)
639 node
= node
->GetNext();
643 bool wxMenuBase
::SendEvent(int id
, int checked
)
645 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
646 event
.SetEventObject(this);
647 event
.SetInt(checked
);
649 bool processed
= false;
651 // Try the menu's event handler first
652 wxEvtHandler
*handler
= GetEventHandler();
654 processed
= handler
->SafelyProcessEvent(event
);
656 // Try the window the menu was popped up from or its menu bar belongs to
659 wxWindow
* const win
= GetWindow();
661 processed
= win
->HandleWindowEvent(event
);
667 // ----------------------------------------------------------------------------
668 // wxMenu attaching/detaching to/from menu bar
669 // ----------------------------------------------------------------------------
671 wxMenuBar
* wxMenuBase
::GetMenuBar() const
674 return GetParent()->GetMenuBar();
678 void wxMenuBase
::Attach(wxMenuBarBase
*menubar
)
680 // use Detach() instead!
681 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
683 // use IsAttached() to prevent this from happening
684 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
686 m_menuBar
= (wxMenuBar
*)menubar
;
689 void wxMenuBase
::Detach()
691 // use IsAttached() to prevent this from happening
692 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
697 // ----------------------------------------------------------------------------
698 // wxMenu invoking window handling
699 // ----------------------------------------------------------------------------
701 void wxMenuBase
::SetInvokingWindow(wxWindow
*win
)
703 wxASSERT_MSG( !GetParent(),
704 "should only be called for top level popup menus" );
705 wxASSERT_MSG( !IsAttached(),
706 "menus attached to menu bar can't have invoking window" );
708 m_invokingWindow
= win
;
711 wxWindow
*wxMenuBase
::GetWindow() const
713 // only the top level menus have non-NULL invoking window or a pointer to
714 // the menu bar so recurse upwards until we find it
715 const wxMenuBase
*menu
= this;
716 while ( menu
->GetParent() )
718 menu
= menu
->GetParent();
721 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
722 : menu
->GetInvokingWindow();
725 // ----------------------------------------------------------------------------
726 // wxMenu functions forwarded to wxMenuItem
727 // ----------------------------------------------------------------------------
729 void wxMenuBase
::Enable( int id
, bool enable
)
731 wxMenuItem
*item
= FindItem(id
);
733 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
735 item
->Enable(enable
);
738 bool wxMenuBase
::IsEnabled( int id
) const
740 wxMenuItem
*item
= FindItem(id
);
742 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
744 return item
->IsEnabled();
747 void wxMenuBase
::Check( int id
, bool enable
)
749 wxMenuItem
*item
= FindItem(id
);
751 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
756 bool wxMenuBase
::IsChecked( int id
) const
758 wxMenuItem
*item
= FindItem(id
);
760 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
762 return item
->IsChecked();
765 void wxMenuBase
::SetLabel( int id
, const wxString
&label
)
767 wxMenuItem
*item
= FindItem(id
);
769 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
771 item
->SetItemLabel(label
);
774 wxString wxMenuBase
::GetLabel( int id
) const
776 wxMenuItem
*item
= FindItem(id
);
778 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
780 return item
->GetItemLabel();
783 void wxMenuBase
::SetHelpString( int id
, const wxString
& helpString
)
785 wxMenuItem
*item
= FindItem(id
);
787 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
789 item
->SetHelp( helpString
);
792 wxString wxMenuBase
::GetHelpString( int id
) const
794 wxMenuItem
*item
= FindItem(id
);
796 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
798 return item
->GetHelp();
801 // ----------------------------------------------------------------------------
802 // wxMenuBarBase ctor and dtor
803 // ----------------------------------------------------------------------------
805 wxMenuBarBase
::wxMenuBarBase()
808 m_menuBarFrame
= NULL
;
811 wxMenuBarBase
::~wxMenuBarBase()
813 WX_CLEAR_LIST(wxMenuList
, m_menus
);
816 // ----------------------------------------------------------------------------
817 // wxMenuBar item access: the base class versions manage m_menus list, the
818 // derived class should reflect the changes in the real menubar
819 // ----------------------------------------------------------------------------
821 wxMenu
*wxMenuBarBase
::GetMenu(size_t pos
) const
823 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
824 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
826 return node
->GetData();
829 bool wxMenuBarBase
::Append(wxMenu
*menu
, const wxString
& title
)
831 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
832 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
834 m_menus
.Append(menu
);
840 bool wxMenuBarBase
::Insert(size_t pos
, wxMenu
*menu
,
841 const wxString
& title
)
843 if ( pos
== m_menus
.GetCount() )
845 return wxMenuBarBase
::Append(menu
, title
);
847 else // not at the end
849 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
851 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
852 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
854 m_menus
.Insert(node
, menu
);
861 wxMenu
*wxMenuBarBase
::Replace(size_t pos
, wxMenu
*menu
,
862 const wxString
& WXUNUSED(title
))
864 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
866 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
867 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
869 wxMenu
*menuOld
= node
->GetData();
878 wxMenu
*wxMenuBarBase
::Remove(size_t pos
)
880 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
881 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
883 wxMenu
*menu
= node
->GetData();
890 int wxMenuBarBase
::FindMenu(const wxString
& title
) const
892 wxString label
= wxMenuItem
::GetLabelText(title
);
894 size_t count
= GetMenuCount();
895 for ( size_t i
= 0; i
< count
; i
++ )
897 wxString title2
= GetMenuLabel(i
);
898 if ( (title2
== title
) ||
899 (wxMenuItem
::GetLabelText(title2
) == label
) )
910 // ----------------------------------------------------------------------------
911 // wxMenuBar attaching/detaching to/from the frame
912 // ----------------------------------------------------------------------------
914 void wxMenuBarBase
::Attach(wxFrame
*frame
)
916 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
918 m_menuBarFrame
= frame
;
921 void wxMenuBarBase
::Detach()
923 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
925 m_menuBarFrame
= NULL
;
928 // ----------------------------------------------------------------------------
929 // wxMenuBar searching for items
930 // ----------------------------------------------------------------------------
932 wxMenuItem
*wxMenuBarBase
::FindItem(int id
, wxMenu
**menu
) const
937 wxMenuItem
*item
= NULL
;
938 size_t count
= GetMenuCount(), i
;
939 wxMenuList
::const_iterator it
;
940 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
942 item
= (*it
)->FindItem(id
, menu
);
948 int wxMenuBarBase
::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
950 wxString label
= wxMenuItem
::GetLabelText(menu
);
953 wxMenuList
::compatibility_iterator node
;
954 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
956 if ( label
== wxMenuItem
::GetLabelText(GetMenuLabel(i
)) )
957 return node
->GetData()->FindItem(item
);
963 // ---------------------------------------------------------------------------
964 // wxMenuBar functions forwarded to wxMenuItem
965 // ---------------------------------------------------------------------------
967 void wxMenuBarBase
::Enable(int id
, bool enable
)
969 wxMenuItem
*item
= FindItem(id
);
971 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
973 item
->Enable(enable
);
976 void wxMenuBarBase
::Check(int id
, bool check
)
978 wxMenuItem
*item
= FindItem(id
);
980 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
981 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
986 bool wxMenuBarBase
::IsChecked(int id
) const
988 wxMenuItem
*item
= FindItem(id
);
990 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
992 return item
->IsChecked();
995 bool wxMenuBarBase
::IsEnabled(int id
) const
997 wxMenuItem
*item
= FindItem(id
);
999 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1001 return item
->IsEnabled();
1004 void wxMenuBarBase
::SetLabel(int id
, const wxString
& label
)
1006 wxMenuItem
*item
= FindItem(id
);
1008 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1010 item
->SetItemLabel(label
);
1013 wxString wxMenuBarBase
::GetLabel(int id
) const
1015 wxMenuItem
*item
= FindItem(id
);
1017 wxCHECK_MSG( item
, wxEmptyString
,
1018 wxT("wxMenuBar::GetLabel(): no such item") );
1020 return item
->GetItemLabel();
1023 void wxMenuBarBase
::SetHelpString(int id
, const wxString
& helpString
)
1025 wxMenuItem
*item
= FindItem(id
);
1027 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1029 item
->SetHelp(helpString
);
1032 wxString wxMenuBarBase
::GetHelpString(int id
) const
1034 wxMenuItem
*item
= FindItem(id
);
1036 wxCHECK_MSG( item
, wxEmptyString
,
1037 wxT("wxMenuBar::GetHelpString(): no such item") );
1039 return item
->GetHelp();
1042 void wxMenuBarBase
::UpdateMenus()
1044 wxEvtHandler
* source
;
1046 int nCount
= GetMenuCount();
1047 for (int n
= 0; n
< nCount
; n
++)
1049 menu
= GetMenu( n
);
1052 source
= menu
->GetEventHandler();
1054 menu
->UpdateUI( source
);
1059 #if WXWIN_COMPATIBILITY_2_8
1060 // get or change the label of the menu at given position
1061 void wxMenuBarBase
::SetLabelTop(size_t pos
, const wxString
& label
)
1063 SetMenuLabel(pos
, label
);
1066 wxString wxMenuBarBase
::GetLabelTop(size_t pos
) const
1068 return GetMenuLabelText(pos
);
1072 #endif // wxUSE_MENUS