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"
35 #include "wx/stockitem.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxMenuList
)
44 WX_DEFINE_LIST(wxMenuItemList
)
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
61 // notice that parentMenu can be NULL: the item can be attached to the menu
62 // later with SetMenu()
64 m_parentMenu
= parentMenu
;
71 m_id
= wxWindow::NewControlId();
72 if (m_id
== wxID_SEPARATOR
)
73 m_kind
= wxITEM_SEPARATOR
;
79 wxMenuItemBase::~wxMenuItemBase()
86 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
88 return wxAcceleratorEntry::Create(GetItemLabel());
91 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
93 wxString text
= m_text
.BeforeFirst(wxT('\t'));
97 text
+= accel
->ToString();
103 #endif // wxUSE_ACCEL
105 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
109 if ( m_text
.empty() && !IsSeparator() )
111 wxASSERT_MSG( wxIsStockID(GetId()),
112 wxT("A non-stock menu item with an empty label?") );
113 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
114 wxSTOCK_WITH_MNEMONIC
);
118 void wxMenuItemBase::SetHelp(const wxString
& str
)
122 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
124 // get a stock help string
125 m_help
= wxGetStockHelpString(GetId());
130 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
132 return wxStripMenuCodes(text
);
136 #if WXWIN_COMPATIBILITY_2_8
137 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
139 return GetLabelText(text
);
143 bool wxMenuBase::ms_locked
= true;
145 // ----------------------------------------------------------------------------
146 // wxMenu ctor and dtor
147 // ----------------------------------------------------------------------------
149 void wxMenuBase::Init(long style
)
154 m_invokingWindow
= NULL
;
157 m_eventHandler
= this;
160 wxMenuBase::~wxMenuBase()
162 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
165 // ----------------------------------------------------------------------------
166 // wxMenu item adding/removing
167 // ----------------------------------------------------------------------------
169 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
171 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
173 submenu
->SetParent((wxMenu
*)this);
176 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
178 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
180 m_items
.Append(item
);
181 item
->SetMenu((wxMenu
*)this);
182 if ( item
->IsSubMenu() )
184 AddSubMenu(item
->GetSubMenu());
190 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
192 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
194 if ( pos
== GetMenuItemCount() )
196 return DoAppend(item
);
200 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
201 wxT("invalid index in wxMenu::Insert") );
203 return DoInsert(pos
, item
);
207 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
209 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
211 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
212 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
214 m_items
.Insert(node
, item
);
215 item
->SetMenu((wxMenu
*)this);
216 if ( item
->IsSubMenu() )
218 AddSubMenu(item
->GetSubMenu());
224 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
226 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
228 return DoRemove(item
);
231 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
233 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
235 // if we get here, the item is valid or one of Remove() functions is broken
236 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
238 // we detach the item, but we do delete the list node (i.e. don't call
239 // DetachNode() here!)
242 // item isn't attached to anything any more
244 wxMenu
*submenu
= item
->GetSubMenu();
247 submenu
->SetParent(NULL
);
248 if ( submenu
->IsAttached() )
255 bool wxMenuBase::Delete(wxMenuItem
*item
)
257 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
259 return DoDelete(item
);
262 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
264 wxMenuItem
*item2
= DoRemove(item
);
265 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
267 // don't delete the submenu
268 item2
->SetSubMenu(NULL
);
275 bool wxMenuBase::Destroy(wxMenuItem
*item
)
277 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
279 return DoDestroy(item
);
282 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
284 wxMenuItem
*item2
= DoRemove(item
);
285 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
292 // ----------------------------------------------------------------------------
293 // wxMenu searching for items
294 // ----------------------------------------------------------------------------
296 // Finds the item id matching the given string, wxNOT_FOUND if not found.
297 int wxMenuBase::FindItem(const wxString
& text
) const
299 wxString label
= wxMenuItem::GetLabelText(text
);
300 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
302 node
= node
->GetNext() )
304 wxMenuItem
*item
= node
->GetData();
305 if ( item
->IsSubMenu() )
307 int rc
= item
->GetSubMenu()->FindItem(label
);
308 if ( rc
!= wxNOT_FOUND
)
312 // we execute this code for submenus as well to alllow finding them by
313 // name just like the ordinary items
314 if ( !item
->IsSeparator() )
316 if ( item
->GetItemLabelText() == label
)
317 return item
->GetId();
324 // recursive search for item by id
325 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
330 wxMenuItem
*item
= NULL
;
331 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
333 node
= node
->GetNext() )
335 item
= node
->GetData();
337 if ( item
->GetId() == itemId
)
340 *itemMenu
= (wxMenu
*)this;
342 else if ( item
->IsSubMenu() )
344 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
348 // don't exit the loop
356 // non recursive search
357 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
359 wxMenuItem
*item
= NULL
;
360 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
363 for ( pos
= 0; node
; pos
++ )
365 if ( node
->GetData()->GetId() == id
)
367 item
= node
->GetData();
372 node
= node
->GetNext();
377 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
384 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
386 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
387 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
389 return m_items
.Item( position
)->GetData();
392 // ----------------------------------------------------------------------------
393 // wxMenu helpers used by derived classes
394 // ----------------------------------------------------------------------------
396 // Update a menu and all submenus recursively. source is the object that has
397 // the update event handlers defined for it. If NULL, the menu or associated
398 // window will be used.
399 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
401 if (GetInvokingWindow())
403 // Don't update menus if the parent
404 // frame is about to get deleted
405 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
406 if (tlw
&& wxPendingDelete
.Member(tlw
))
410 if ( !source
&& GetInvokingWindow() )
411 source
= GetInvokingWindow()->GetEventHandler();
413 source
= GetEventHandler();
417 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
420 wxMenuItem
* item
= node
->GetData();
421 if ( !item
->IsSeparator() )
423 wxWindowID id
= item
->GetId();
424 wxUpdateUIEvent
event(id
);
425 event
.SetEventObject( source
);
427 if ( source
->ProcessEvent(event
) )
429 // if anything changed, update the changed attribute
430 if (event
.GetSetText())
431 SetLabel(id
, event
.GetText());
432 if (event
.GetSetChecked())
433 Check(id
, event
.GetChecked());
434 if (event
.GetSetEnabled())
435 Enable(id
, event
.GetEnabled());
438 // recurse to the submenus
439 if ( item
->GetSubMenu() )
440 item
->GetSubMenu()->UpdateUI(source
);
442 //else: item is a separator (which doesn't process update UI events)
444 node
= node
->GetNext();
448 bool wxMenuBase::SendEvent(int id
, int checked
)
450 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
451 event
.SetEventObject(this);
452 event
.SetInt(checked
);
454 bool processed
= false;
456 // Try the menu's event handler
459 wxEvtHandler
*handler
= GetEventHandler();
461 processed
= handler
->SafelyProcessEvent(event
);
464 // Try the window the menu was popped up from (and up through the
468 const wxMenuBase
*menu
= this;
471 wxWindow
*win
= menu
->GetInvokingWindow();
474 processed
= win
->HandleWindowEvent(event
);
478 menu
= menu
->GetParent();
485 // ----------------------------------------------------------------------------
486 // wxMenu attaching/detaching to/from menu bar
487 // ----------------------------------------------------------------------------
489 wxMenuBar
* wxMenuBase::GetMenuBar() const
492 return GetParent()->GetMenuBar();
496 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
498 // use Detach() instead!
499 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
501 // use IsAttached() to prevent this from happening
502 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
504 m_menuBar
= (wxMenuBar
*)menubar
;
507 void wxMenuBase::Detach()
509 // use IsAttached() to prevent this from happening
510 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
515 // ----------------------------------------------------------------------------
516 // wxMenu invoking window handling
517 // ----------------------------------------------------------------------------
519 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
521 wxASSERT_MSG( !GetParent(),
522 "should only be called for top level popup menus" );
523 wxASSERT_MSG( !IsAttached(),
524 "menus attached to menu bar can't have invoking window" );
526 m_invokingWindow
= win
;
529 wxWindow
*wxMenuBase::GetInvokingWindow() const
531 // only the popup menu itself has a non-NULL invoking window so recurse
532 // upwards until we find it
533 const wxMenuBase
*menu
= this;
534 while ( menu
->GetParent() )
536 menu
= menu
->GetParent();
539 // menu is a top level menu here
540 return menu
->m_invokingWindow
;
543 // ----------------------------------------------------------------------------
544 // wxMenu functions forwarded to wxMenuItem
545 // ----------------------------------------------------------------------------
547 void wxMenuBase::Enable( int id
, bool enable
)
549 wxMenuItem
*item
= FindItem(id
);
551 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
553 item
->Enable(enable
);
556 bool wxMenuBase::IsEnabled( int id
) const
558 wxMenuItem
*item
= FindItem(id
);
560 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
562 return item
->IsEnabled();
565 void wxMenuBase::Check( int id
, bool enable
)
567 wxMenuItem
*item
= FindItem(id
);
569 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
574 bool wxMenuBase::IsChecked( int id
) const
576 wxMenuItem
*item
= FindItem(id
);
578 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
580 return item
->IsChecked();
583 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
585 wxMenuItem
*item
= FindItem(id
);
587 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
589 item
->SetItemLabel(label
);
592 wxString
wxMenuBase::GetLabel( int id
) const
594 wxMenuItem
*item
= FindItem(id
);
596 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
598 return item
->GetItemLabel();
601 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
603 wxMenuItem
*item
= FindItem(id
);
605 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
607 item
->SetHelp( helpString
);
610 wxString
wxMenuBase::GetHelpString( int id
) const
612 wxMenuItem
*item
= FindItem(id
);
614 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
616 return item
->GetHelp();
619 // ----------------------------------------------------------------------------
620 // wxMenuBarBase ctor and dtor
621 // ----------------------------------------------------------------------------
623 wxMenuBarBase::wxMenuBarBase()
626 m_menuBarFrame
= NULL
;
629 wxMenuBarBase::~wxMenuBarBase()
631 WX_CLEAR_LIST(wxMenuList
, m_menus
);
634 // ----------------------------------------------------------------------------
635 // wxMenuBar item access: the base class versions manage m_menus list, the
636 // derived class should reflect the changes in the real menubar
637 // ----------------------------------------------------------------------------
639 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
641 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
642 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
644 return node
->GetData();
647 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
649 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
650 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
652 m_menus
.Append(menu
);
658 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
659 const wxString
& title
)
661 if ( pos
== m_menus
.GetCount() )
663 return wxMenuBarBase::Append(menu
, title
);
665 else // not at the end
667 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
669 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
670 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
672 m_menus
.Insert(node
, menu
);
679 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
680 const wxString
& WXUNUSED(title
))
682 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
684 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
685 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
687 wxMenu
*menuOld
= node
->GetData();
696 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
698 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
699 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
701 wxMenu
*menu
= node
->GetData();
708 int wxMenuBarBase::FindMenu(const wxString
& title
) const
710 wxString label
= wxMenuItem::GetLabelText(title
);
712 size_t count
= GetMenuCount();
713 for ( size_t i
= 0; i
< count
; i
++ )
715 wxString title2
= GetMenuLabel(i
);
716 if ( (title2
== title
) ||
717 (wxMenuItem::GetLabelText(title2
) == label
) )
728 // ----------------------------------------------------------------------------
729 // wxMenuBar attaching/detaching to/from the frame
730 // ----------------------------------------------------------------------------
732 void wxMenuBarBase::Attach(wxFrame
*frame
)
734 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
736 m_menuBarFrame
= frame
;
739 void wxMenuBarBase::Detach()
741 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
743 m_menuBarFrame
= NULL
;
746 // ----------------------------------------------------------------------------
747 // wxMenuBar searching for items
748 // ----------------------------------------------------------------------------
750 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
755 wxMenuItem
*item
= NULL
;
756 size_t count
= GetMenuCount(), i
;
757 wxMenuList::const_iterator it
;
758 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
760 item
= (*it
)->FindItem(id
, menu
);
766 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
768 wxString label
= wxMenuItem::GetLabelText(menu
);
771 wxMenuList::compatibility_iterator node
;
772 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
774 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
775 return node
->GetData()->FindItem(item
);
781 // ---------------------------------------------------------------------------
782 // wxMenuBar functions forwarded to wxMenuItem
783 // ---------------------------------------------------------------------------
785 void wxMenuBarBase::Enable(int id
, bool enable
)
787 wxMenuItem
*item
= FindItem(id
);
789 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
791 item
->Enable(enable
);
794 void wxMenuBarBase::Check(int id
, bool check
)
796 wxMenuItem
*item
= FindItem(id
);
798 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
799 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
804 bool wxMenuBarBase::IsChecked(int id
) const
806 wxMenuItem
*item
= FindItem(id
);
808 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
810 return item
->IsChecked();
813 bool wxMenuBarBase::IsEnabled(int id
) const
815 wxMenuItem
*item
= FindItem(id
);
817 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
819 return item
->IsEnabled();
822 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
824 wxMenuItem
*item
= FindItem(id
);
826 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
828 item
->SetItemLabel(label
);
831 wxString
wxMenuBarBase::GetLabel(int id
) const
833 wxMenuItem
*item
= FindItem(id
);
835 wxCHECK_MSG( item
, wxEmptyString
,
836 wxT("wxMenuBar::GetLabel(): no such item") );
838 return item
->GetItemLabel();
841 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
843 wxMenuItem
*item
= FindItem(id
);
845 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
847 item
->SetHelp(helpString
);
850 wxString
wxMenuBarBase::GetHelpString(int id
) const
852 wxMenuItem
*item
= FindItem(id
);
854 wxCHECK_MSG( item
, wxEmptyString
,
855 wxT("wxMenuBar::GetHelpString(): no such item") );
857 return item
->GetHelp();
860 void wxMenuBarBase::UpdateMenus()
862 wxEvtHandler
* source
;
864 int nCount
= GetMenuCount();
865 for (int n
= 0; n
< nCount
; n
++)
870 source
= menu
->GetEventHandler();
872 menu
->UpdateUI( source
);
877 #if WXWIN_COMPATIBILITY_2_8
878 // get or change the label of the menu at given position
879 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
881 SetMenuLabel(pos
, label
);
884 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
886 return GetMenuLabelText(pos
);
890 #endif // wxUSE_MENUS