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 functions forwarded to wxMenuItem
517 // ----------------------------------------------------------------------------
519 void wxMenuBase::Enable( int id
, bool enable
)
521 wxMenuItem
*item
= FindItem(id
);
523 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
525 item
->Enable(enable
);
528 bool wxMenuBase::IsEnabled( int id
) const
530 wxMenuItem
*item
= FindItem(id
);
532 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
534 return item
->IsEnabled();
537 void wxMenuBase::Check( int id
, bool enable
)
539 wxMenuItem
*item
= FindItem(id
);
541 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
546 bool wxMenuBase::IsChecked( int id
) const
548 wxMenuItem
*item
= FindItem(id
);
550 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
552 return item
->IsChecked();
555 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
557 wxMenuItem
*item
= FindItem(id
);
559 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
561 item
->SetItemLabel(label
);
564 wxString
wxMenuBase::GetLabel( int id
) const
566 wxMenuItem
*item
= FindItem(id
);
568 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
570 return item
->GetItemLabel();
573 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
575 wxMenuItem
*item
= FindItem(id
);
577 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
579 item
->SetHelp( helpString
);
582 wxString
wxMenuBase::GetHelpString( int id
) const
584 wxMenuItem
*item
= FindItem(id
);
586 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
588 return item
->GetHelp();
591 // ----------------------------------------------------------------------------
592 // wxMenuBarBase ctor and dtor
593 // ----------------------------------------------------------------------------
595 wxMenuBarBase::wxMenuBarBase()
598 m_menuBarFrame
= NULL
;
601 wxMenuBarBase::~wxMenuBarBase()
603 WX_CLEAR_LIST(wxMenuList
, m_menus
);
606 // ----------------------------------------------------------------------------
607 // wxMenuBar item access: the base class versions manage m_menus list, the
608 // derived class should reflect the changes in the real menubar
609 // ----------------------------------------------------------------------------
611 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
613 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
614 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
616 return node
->GetData();
619 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
621 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
623 m_menus
.Append(menu
);
629 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
630 const wxString
& title
)
632 if ( pos
== m_menus
.GetCount() )
634 return wxMenuBarBase::Append(menu
, title
);
636 else // not at the end
638 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
640 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
641 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
643 m_menus
.Insert(node
, menu
);
650 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
651 const wxString
& WXUNUSED(title
))
653 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
655 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
656 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
658 wxMenu
*menuOld
= node
->GetData();
667 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
669 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
670 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
672 wxMenu
*menu
= node
->GetData();
679 int wxMenuBarBase::FindMenu(const wxString
& title
) const
681 wxString label
= wxMenuItem::GetLabelText(title
);
683 size_t count
= GetMenuCount();
684 for ( size_t i
= 0; i
< count
; i
++ )
686 wxString title2
= GetMenuLabel(i
);
687 if ( (title2
== title
) ||
688 (wxMenuItem::GetLabelText(title2
) == label
) )
699 // ----------------------------------------------------------------------------
700 // wxMenuBar attaching/detaching to/from the frame
701 // ----------------------------------------------------------------------------
703 void wxMenuBarBase::Attach(wxFrame
*frame
)
705 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
707 m_menuBarFrame
= frame
;
710 void wxMenuBarBase::Detach()
712 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
714 m_menuBarFrame
= NULL
;
717 // ----------------------------------------------------------------------------
718 // wxMenuBar searching for items
719 // ----------------------------------------------------------------------------
721 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
726 wxMenuItem
*item
= NULL
;
727 size_t count
= GetMenuCount(), i
;
728 wxMenuList::const_iterator it
;
729 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
731 item
= (*it
)->FindItem(id
, menu
);
737 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
739 wxString label
= wxMenuItem::GetLabelText(menu
);
742 wxMenuList::compatibility_iterator node
;
743 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
745 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
746 return node
->GetData()->FindItem(item
);
752 // ---------------------------------------------------------------------------
753 // wxMenuBar functions forwarded to wxMenuItem
754 // ---------------------------------------------------------------------------
756 void wxMenuBarBase::Enable(int id
, bool enable
)
758 wxMenuItem
*item
= FindItem(id
);
760 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
762 item
->Enable(enable
);
765 void wxMenuBarBase::Check(int id
, bool check
)
767 wxMenuItem
*item
= FindItem(id
);
769 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
770 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
775 bool wxMenuBarBase::IsChecked(int id
) const
777 wxMenuItem
*item
= FindItem(id
);
779 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
781 return item
->IsChecked();
784 bool wxMenuBarBase::IsEnabled(int id
) const
786 wxMenuItem
*item
= FindItem(id
);
788 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
790 return item
->IsEnabled();
793 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
795 wxMenuItem
*item
= FindItem(id
);
797 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
799 item
->SetItemLabel(label
);
802 wxString
wxMenuBarBase::GetLabel(int id
) const
804 wxMenuItem
*item
= FindItem(id
);
806 wxCHECK_MSG( item
, wxEmptyString
,
807 wxT("wxMenuBar::GetLabel(): no such item") );
809 return item
->GetItemLabel();
812 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
814 wxMenuItem
*item
= FindItem(id
);
816 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
818 item
->SetHelp(helpString
);
821 wxString
wxMenuBarBase::GetHelpString(int id
) const
823 wxMenuItem
*item
= FindItem(id
);
825 wxCHECK_MSG( item
, wxEmptyString
,
826 wxT("wxMenuBar::GetHelpString(): no such item") );
828 return item
->GetHelp();
831 void wxMenuBarBase::UpdateMenus()
833 wxEvtHandler
* source
;
835 int nCount
= GetMenuCount();
836 for (int n
= 0; n
< nCount
; n
++)
841 source
= menu
->GetEventHandler();
843 menu
->UpdateUI( source
);
848 #if WXWIN_COMPATIBILITY_2_8
849 // get or change the label of the menu at given position
850 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
852 SetMenuLabel(pos
, label
);
855 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
857 return GetMenuLabelText(pos
);
861 #endif // wxUSE_MENUS