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
)
104 #if wxUSE_EXTENDED_RTTI
106 WX_DEFINE_LIST( wxMenuInfoList
)
107 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h")
109 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
110 wxREADONLY_PROPERTY( Menu
, wxMenu
*, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
111 0 /*flags*/, wxT("Helpstring"), wxT("group"))
113 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString(), \
114 0 /*flags*/, wxT("Helpstring"), wxT("group"))
115 wxEND_PROPERTIES_TABLE()
117 wxEMPTY_HANDLERS_TABLE(wxMenuInfo
)
119 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
*, Menu
, wxString
, Title
)
121 wxCOLLECTION_TYPE_INFO( wxMenuInfo
*, wxMenuInfoList
) ;
123 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
,
126 wxListCollectionToAnyList
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
129 const wxMenuInfoList
& wxMenuBarBase::GetMenuInfos() const
131 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* > (& m_menuInfos
);
132 WX_CLEAR_LIST( wxMenuInfoList
, *list
);
133 for (size_t i
= 0 ; i
< GetMenuCount(); ++i
)
135 wxMenuInfo
* info
= new wxMenuInfo();
136 info
->Create( GetMenu(i
), GetMenuLabel(i
));
144 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
145 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
*, AppendMenuInfo
, \
146 GetMenuInfos
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
147 wxEND_PROPERTIES_TABLE()
149 wxEMPTY_HANDLERS_TABLE(wxMenuBar
)
151 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
153 // ----------------------------------------------------------------------------
154 // XTI for wxMenuItem
155 // ----------------------------------------------------------------------------
157 #if wxUSE_EXTENDED_RTTI
159 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxObjectWriter
*,
160 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& )
162 const wxMenuItem
* mitem
= wx_dynamic_cast(const wxMenuItem
*, object
);
163 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
165 // we don't stream out the first two items for menus with a title,
166 // they will be reconstructed
167 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
||
168 mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
176 wxBEGIN_ENUM( wxItemKind
)
177 wxENUM_MEMBER( wxITEM_SEPARATOR
)
178 wxENUM_MEMBER( wxITEM_NORMAL
)
179 wxENUM_MEMBER( wxITEM_CHECK
)
180 wxENUM_MEMBER( wxITEM_RADIO
)
181 wxEND_ENUM( wxItemKind
)
183 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
, "wx/menuitem.h", \
184 wxMenuItemStreamingCallback
)
186 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
187 wxPROPERTY( Parent
, wxMenu
*, SetMenu
, GetMenu
, wxEMPTY_PARAMETER_VALUE
, \
188 0 /*flags*/, wxT("Helpstring"), wxT("group") )
189 wxPROPERTY( Id
, int, SetId
, GetId
, wxEMPTY_PARAMETER_VALUE
, \
190 0 /*flags*/, wxT("Helpstring"), wxT("group") )
191 wxPROPERTY( ItemLabel
, wxString
, SetItemLabel
, GetItemLabel
, wxString(), \
192 0 /*flags*/, wxT("Helpstring"), wxT("group") )
193 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), \
194 0 /*flags*/, wxT("Helpstring"), wxT("group") )
195 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, wxEMPTY_PARAMETER_VALUE
, \
196 0 /*flags*/, wxT("Helpstring"), wxT("group") )
197 wxPROPERTY( SubMenu
, wxMenu
*, SetSubMenu
, GetSubMenu
, wxEMPTY_PARAMETER_VALUE
, \
198 0 /*flags*/, wxT("Helpstring"), wxT("group") )
199 wxPROPERTY( Enabled
, bool, Enable
, IsEnabled
, wxAny((bool)true), \
200 0 /*flags*/, wxT("Helpstring"), wxT("group") )
201 wxPROPERTY( Checked
, bool, Check
, IsChecked
, wxAny((bool)false), \
202 0 /*flags*/, wxT("Helpstring"), wxT("group") )
203 wxPROPERTY( Checkable
, bool, SetCheckable
, IsCheckable
, wxAny((bool)false), \
204 0 /*flags*/, wxT("Helpstring"), wxT("group") )
205 wxEND_PROPERTIES_TABLE()
207 wxEMPTY_HANDLERS_TABLE(wxMenuItem
)
209 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
*, Parent
, int, Id
, wxString
, \
210 Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
*, SubMenu
)
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
218 const wxString
& text
,
219 const wxString
& help
,
226 m_id
= wxWindow::NewControlId();
230 m_id
= wxID_SEPARATOR
;
232 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
233 // and it makes sense to omit the following optional parameters,
234 // including the kind one which doesn't default to wxITEM_SEPARATOR,
235 // of course, so override it here
236 kind
= wxITEM_SEPARATOR
;
240 // (popup) menu titles in wxMSW use this ID to indicate that
241 // it's not a real menu item, so we don't want the check below to
247 // ids are limited to 16 bits under MSW so portable code shouldn't
248 // use ids outside of this range (negative ids generated by wx are
250 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
251 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
252 wxS("invalid id value") );
256 // notice that parentMenu can be NULL: the item can be attached to the menu
257 // later with SetMenu()
259 m_parentMenu
= parentMenu
;
269 wxMenuItemBase::~wxMenuItemBase()
276 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
278 return wxAcceleratorEntry::Create(GetItemLabel());
281 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
283 wxString text
= m_text
.BeforeFirst(wxT('\t'));
287 text
+= accel
->ToString();
293 #endif // wxUSE_ACCEL
295 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
299 if ( m_text
.empty() && !IsSeparator() )
301 wxASSERT_MSG( wxIsStockID(GetId()),
302 wxT("A non-stock menu item with an empty label?") );
303 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
304 wxSTOCK_WITH_MNEMONIC
);
308 void wxMenuItemBase::SetHelp(const wxString
& str
)
312 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
314 // get a stock help string
315 m_help
= wxGetStockHelpString(GetId());
320 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
322 return wxStripMenuCodes(text
);
326 #if WXWIN_COMPATIBILITY_2_8
327 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
329 return GetLabelText(text
);
333 bool wxMenuBase::ms_locked
= true;
335 // ----------------------------------------------------------------------------
336 // wxMenu ctor and dtor
337 // ----------------------------------------------------------------------------
339 void wxMenuBase::Init(long style
)
344 m_invokingWindow
= NULL
;
347 m_eventHandler
= this;
350 wxMenuBase::~wxMenuBase()
352 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
355 // ----------------------------------------------------------------------------
356 // wxMenu item adding/removing
357 // ----------------------------------------------------------------------------
359 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
361 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
363 submenu
->SetParent((wxMenu
*)this);
366 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
368 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
370 m_items
.Append(item
);
371 item
->SetMenu((wxMenu
*)this);
372 if ( item
->IsSubMenu() )
374 AddSubMenu(item
->GetSubMenu());
380 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
382 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
384 if ( pos
== GetMenuItemCount() )
386 return DoAppend(item
);
390 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
391 wxT("invalid index in wxMenu::Insert") );
393 return DoInsert(pos
, item
);
397 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
399 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
401 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
402 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
404 m_items
.Insert(node
, item
);
405 item
->SetMenu((wxMenu
*)this);
406 if ( item
->IsSubMenu() )
408 AddSubMenu(item
->GetSubMenu());
414 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
416 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
418 return DoRemove(item
);
421 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
423 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
425 // if we get here, the item is valid or one of Remove() functions is broken
426 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
428 // we detach the item, but we do delete the list node (i.e. don't call
429 // DetachNode() here!)
432 // item isn't attached to anything any more
434 wxMenu
*submenu
= item
->GetSubMenu();
437 submenu
->SetParent(NULL
);
438 if ( submenu
->IsAttached() )
445 bool wxMenuBase::Delete(wxMenuItem
*item
)
447 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
449 return DoDelete(item
);
452 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
454 wxMenuItem
*item2
= DoRemove(item
);
455 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
457 // don't delete the submenu
458 item2
->SetSubMenu(NULL
);
465 bool wxMenuBase::Destroy(wxMenuItem
*item
)
467 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
469 return DoDestroy(item
);
472 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
474 wxMenuItem
*item2
= DoRemove(item
);
475 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
482 // ----------------------------------------------------------------------------
483 // wxMenu searching for items
484 // ----------------------------------------------------------------------------
486 // Finds the item id matching the given string, wxNOT_FOUND if not found.
487 int wxMenuBase::FindItem(const wxString
& text
) const
489 wxString label
= wxMenuItem::GetLabelText(text
);
490 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
492 node
= node
->GetNext() )
494 wxMenuItem
*item
= node
->GetData();
495 if ( item
->IsSubMenu() )
497 int rc
= item
->GetSubMenu()->FindItem(label
);
498 if ( rc
!= wxNOT_FOUND
)
502 // we execute this code for submenus as well to alllow finding them by
503 // name just like the ordinary items
504 if ( !item
->IsSeparator() )
506 if ( item
->GetItemLabelText() == label
)
507 return item
->GetId();
514 // recursive search for item by id
515 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
520 wxMenuItem
*item
= NULL
;
521 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
523 node
= node
->GetNext() )
525 item
= node
->GetData();
527 if ( item
->GetId() == itemId
)
530 *itemMenu
= (wxMenu
*)this;
532 else if ( item
->IsSubMenu() )
534 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
538 // don't exit the loop
546 // non recursive search
547 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
549 wxMenuItem
*item
= NULL
;
550 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
553 for ( pos
= 0; node
; pos
++ )
555 if ( node
->GetData()->GetId() == id
)
557 item
= node
->GetData();
562 node
= node
->GetNext();
567 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
574 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
576 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
577 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
579 return m_items
.Item( position
)->GetData();
582 // ----------------------------------------------------------------------------
583 // wxMenu helpers used by derived classes
584 // ----------------------------------------------------------------------------
586 // Update a menu and all submenus recursively. source is the object that has
587 // the update event handlers defined for it. If NULL, the menu or associated
588 // window will be used.
589 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
591 if (GetInvokingWindow())
593 // Don't update menus if the parent
594 // frame is about to get deleted
595 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
596 if (tlw
&& wxPendingDelete
.Member(tlw
))
600 if ( !source
&& GetInvokingWindow() )
601 source
= GetInvokingWindow()->GetEventHandler();
603 source
= GetEventHandler();
607 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
610 wxMenuItem
* item
= node
->GetData();
611 if ( !item
->IsSeparator() )
613 wxWindowID id
= item
->GetId();
614 wxUpdateUIEvent
event(id
);
615 event
.SetEventObject( source
);
617 if ( source
->ProcessEvent(event
) )
619 // if anything changed, update the changed attribute
620 if (event
.GetSetText())
621 SetLabel(id
, event
.GetText());
622 if (event
.GetSetChecked())
623 Check(id
, event
.GetChecked());
624 if (event
.GetSetEnabled())
625 Enable(id
, event
.GetEnabled());
628 // recurse to the submenus
629 if ( item
->GetSubMenu() )
630 item
->GetSubMenu()->UpdateUI(source
);
632 //else: item is a separator (which doesn't process update UI events)
634 node
= node
->GetNext();
638 bool wxMenuBase::SendEvent(int id
, int checked
)
640 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
641 event
.SetEventObject(this);
642 event
.SetInt(checked
);
644 bool processed
= false;
646 // Try the menu's event handler first
647 wxEvtHandler
*handler
= GetEventHandler();
649 processed
= handler
->SafelyProcessEvent(event
);
651 // Try the window the menu was popped up from or its menu bar belongs to
654 wxWindow
* const win
= GetWindow();
656 processed
= win
->HandleWindowEvent(event
);
662 // ----------------------------------------------------------------------------
663 // wxMenu attaching/detaching to/from menu bar
664 // ----------------------------------------------------------------------------
666 wxMenuBar
* wxMenuBase::GetMenuBar() const
669 return GetParent()->GetMenuBar();
673 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
675 // use Detach() instead!
676 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
678 // use IsAttached() to prevent this from happening
679 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
681 m_menuBar
= (wxMenuBar
*)menubar
;
684 void wxMenuBase::Detach()
686 // use IsAttached() to prevent this from happening
687 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
692 // ----------------------------------------------------------------------------
693 // wxMenu invoking window handling
694 // ----------------------------------------------------------------------------
696 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
698 wxASSERT_MSG( !GetParent(),
699 "should only be called for top level popup menus" );
700 wxASSERT_MSG( !IsAttached(),
701 "menus attached to menu bar can't have invoking window" );
703 m_invokingWindow
= win
;
706 wxWindow
*wxMenuBase::GetWindow() const
708 // only the top level menus have non-NULL invoking window or a pointer to
709 // the menu bar so recurse upwards until we find it
710 const wxMenuBase
*menu
= this;
711 while ( menu
->GetParent() )
713 menu
= menu
->GetParent();
716 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
717 : menu
->GetInvokingWindow();
720 // ----------------------------------------------------------------------------
721 // wxMenu functions forwarded to wxMenuItem
722 // ----------------------------------------------------------------------------
724 void wxMenuBase::Enable( int id
, bool enable
)
726 wxMenuItem
*item
= FindItem(id
);
728 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
730 item
->Enable(enable
);
733 bool wxMenuBase::IsEnabled( int id
) const
735 wxMenuItem
*item
= FindItem(id
);
737 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
739 return item
->IsEnabled();
742 void wxMenuBase::Check( int id
, bool enable
)
744 wxMenuItem
*item
= FindItem(id
);
746 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
751 bool wxMenuBase::IsChecked( int id
) const
753 wxMenuItem
*item
= FindItem(id
);
755 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
757 return item
->IsChecked();
760 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
762 wxMenuItem
*item
= FindItem(id
);
764 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
766 item
->SetItemLabel(label
);
769 wxString
wxMenuBase::GetLabel( int id
) const
771 wxMenuItem
*item
= FindItem(id
);
773 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
775 return item
->GetItemLabel();
778 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
780 wxMenuItem
*item
= FindItem(id
);
782 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
784 item
->SetHelp( helpString
);
787 wxString
wxMenuBase::GetHelpString( int id
) const
789 wxMenuItem
*item
= FindItem(id
);
791 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
793 return item
->GetHelp();
796 // ----------------------------------------------------------------------------
797 // wxMenuBarBase ctor and dtor
798 // ----------------------------------------------------------------------------
800 wxMenuBarBase::wxMenuBarBase()
803 m_menuBarFrame
= NULL
;
806 wxMenuBarBase::~wxMenuBarBase()
808 WX_CLEAR_LIST(wxMenuList
, m_menus
);
811 // ----------------------------------------------------------------------------
812 // wxMenuBar item access: the base class versions manage m_menus list, the
813 // derived class should reflect the changes in the real menubar
814 // ----------------------------------------------------------------------------
816 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
818 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
819 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
821 return node
->GetData();
824 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
826 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
827 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
829 m_menus
.Append(menu
);
835 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
836 const wxString
& title
)
838 if ( pos
== m_menus
.GetCount() )
840 return wxMenuBarBase::Append(menu
, title
);
842 else // not at the end
844 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
846 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
847 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
849 m_menus
.Insert(node
, menu
);
856 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
857 const wxString
& WXUNUSED(title
))
859 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
861 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
862 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
864 wxMenu
*menuOld
= node
->GetData();
873 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
875 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
876 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
878 wxMenu
*menu
= node
->GetData();
885 int wxMenuBarBase::FindMenu(const wxString
& title
) const
887 wxString label
= wxMenuItem::GetLabelText(title
);
889 size_t count
= GetMenuCount();
890 for ( size_t i
= 0; i
< count
; i
++ )
892 wxString title2
= GetMenuLabel(i
);
893 if ( (title2
== title
) ||
894 (wxMenuItem::GetLabelText(title2
) == label
) )
905 // ----------------------------------------------------------------------------
906 // wxMenuBar attaching/detaching to/from the frame
907 // ----------------------------------------------------------------------------
909 void wxMenuBarBase::Attach(wxFrame
*frame
)
911 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
913 m_menuBarFrame
= frame
;
916 void wxMenuBarBase::Detach()
918 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
920 m_menuBarFrame
= NULL
;
923 // ----------------------------------------------------------------------------
924 // wxMenuBar searching for items
925 // ----------------------------------------------------------------------------
927 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
932 wxMenuItem
*item
= NULL
;
933 size_t count
= GetMenuCount(), i
;
934 wxMenuList::const_iterator it
;
935 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
937 item
= (*it
)->FindItem(id
, menu
);
943 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
945 wxString label
= wxMenuItem::GetLabelText(menu
);
948 wxMenuList::compatibility_iterator node
;
949 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
951 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
952 return node
->GetData()->FindItem(item
);
958 // ---------------------------------------------------------------------------
959 // wxMenuBar functions forwarded to wxMenuItem
960 // ---------------------------------------------------------------------------
962 void wxMenuBarBase::Enable(int id
, bool enable
)
964 wxMenuItem
*item
= FindItem(id
);
966 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
968 item
->Enable(enable
);
971 void wxMenuBarBase::Check(int id
, bool check
)
973 wxMenuItem
*item
= FindItem(id
);
975 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
976 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
981 bool wxMenuBarBase::IsChecked(int id
) const
983 wxMenuItem
*item
= FindItem(id
);
985 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
987 return item
->IsChecked();
990 bool wxMenuBarBase::IsEnabled(int id
) const
992 wxMenuItem
*item
= FindItem(id
);
994 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
996 return item
->IsEnabled();
999 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1001 wxMenuItem
*item
= FindItem(id
);
1003 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1005 item
->SetItemLabel(label
);
1008 wxString
wxMenuBarBase::GetLabel(int id
) const
1010 wxMenuItem
*item
= FindItem(id
);
1012 wxCHECK_MSG( item
, wxEmptyString
,
1013 wxT("wxMenuBar::GetLabel(): no such item") );
1015 return item
->GetItemLabel();
1018 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1020 wxMenuItem
*item
= FindItem(id
);
1022 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1024 item
->SetHelp(helpString
);
1027 wxString
wxMenuBarBase::GetHelpString(int id
) const
1029 wxMenuItem
*item
= FindItem(id
);
1031 wxCHECK_MSG( item
, wxEmptyString
,
1032 wxT("wxMenuBar::GetHelpString(): no such item") );
1034 return item
->GetHelp();
1037 void wxMenuBarBase::UpdateMenus()
1039 wxEvtHandler
* source
;
1041 int nCount
= GetMenuCount();
1042 for (int n
= 0; n
< nCount
; n
++)
1044 menu
= GetMenu( n
);
1047 source
= menu
->GetEventHandler();
1049 menu
->UpdateUI( source
);
1054 #if WXWIN_COMPATIBILITY_2_8
1055 // get or change the label of the menu at given position
1056 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
1058 SetMenuLabel(pos
, label
);
1061 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
1063 return GetMenuLabelText(pos
);
1067 #endif // wxUSE_MENUS