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 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
63 m_parentMenu
= parentMenu
;
70 m_id
= wxWindow::NewControlId();
71 if (m_id
== wxID_SEPARATOR
)
72 m_kind
= wxITEM_SEPARATOR
;
78 wxMenuItemBase::~wxMenuItemBase()
85 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
87 return wxAcceleratorEntry::Create(GetItemLabel());
90 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
92 wxString text
= m_text
.BeforeFirst(wxT('\t'));
96 text
+= accel
->ToString();
102 #endif // wxUSE_ACCEL
104 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
108 if ( m_text
.empty() && !IsSeparator() )
110 wxASSERT_MSG( wxIsStockID(GetId()),
111 wxT("A non-stock menu item with an empty label?") );
112 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
113 wxSTOCK_WITH_MNEMONIC
);
117 void wxMenuItemBase::SetHelp(const wxString
& str
)
121 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
123 // get a stock help string
124 m_help
= wxGetStockHelpString(GetId());
128 #if WXWIN_COMPATIBILITY_2_8
129 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
131 return GetLabelText(text
);
135 bool wxMenuBase::ms_locked
= true;
137 // ----------------------------------------------------------------------------
138 // wxMenu ctor and dtor
139 // ----------------------------------------------------------------------------
141 void wxMenuBase::Init(long style
)
143 m_menuBar
= (wxMenuBar
*)NULL
;
144 m_menuParent
= (wxMenu
*)NULL
;
146 m_invokingWindow
= (wxWindow
*)NULL
;
148 m_clientData
= (void *)NULL
;
149 m_eventHandler
= this;
152 wxMenuBase::~wxMenuBase()
154 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
157 // ----------------------------------------------------------------------------
158 // wxMenu item adding/removing
159 // ----------------------------------------------------------------------------
161 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
163 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
165 submenu
->SetParent((wxMenu
*)this);
168 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
170 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
172 m_items
.Append(item
);
173 item
->SetMenu((wxMenu
*)this);
174 if ( item
->IsSubMenu() )
176 AddSubMenu(item
->GetSubMenu());
182 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
184 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
186 if ( pos
== GetMenuItemCount() )
188 return DoAppend(item
);
192 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
193 wxT("invalid index in wxMenu::Insert") );
195 return DoInsert(pos
, item
);
199 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
201 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
203 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
204 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
206 m_items
.Insert(node
, item
);
207 item
->SetMenu((wxMenu
*)this);
208 if ( item
->IsSubMenu() )
210 AddSubMenu(item
->GetSubMenu());
216 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
218 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
220 return DoRemove(item
);
223 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
225 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
227 // if we get here, the item is valid or one of Remove() functions is broken
228 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
230 // we detach the item, but we do delete the list node (i.e. don't call
231 // DetachNode() here!)
234 // item isn't attached to anything any more
235 item
->SetMenu((wxMenu
*)NULL
);
236 wxMenu
*submenu
= item
->GetSubMenu();
239 submenu
->SetParent((wxMenu
*)NULL
);
240 if ( submenu
->IsAttached() )
247 bool wxMenuBase::Delete(wxMenuItem
*item
)
249 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
251 return DoDelete(item
);
254 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
256 wxMenuItem
*item2
= DoRemove(item
);
257 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
259 // don't delete the submenu
260 item2
->SetSubMenu((wxMenu
*)NULL
);
267 bool wxMenuBase::Destroy(wxMenuItem
*item
)
269 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
271 return DoDestroy(item
);
274 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
276 wxMenuItem
*item2
= DoRemove(item
);
277 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
284 // ----------------------------------------------------------------------------
285 // wxMenu searching for items
286 // ----------------------------------------------------------------------------
288 // Finds the item id matching the given string, wxNOT_FOUND if not found.
289 int wxMenuBase::FindItem(const wxString
& text
) const
291 wxString label
= wxMenuItem::GetLabelText(text
);
292 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
294 node
= node
->GetNext() )
296 wxMenuItem
*item
= node
->GetData();
297 if ( item
->IsSubMenu() )
299 int rc
= item
->GetSubMenu()->FindItem(label
);
300 if ( rc
!= wxNOT_FOUND
)
304 // we execute this code for submenus as well to alllow finding them by
305 // name just like the ordinary items
306 if ( !item
->IsSeparator() )
308 if ( item
->GetItemLabelText() == label
)
309 return item
->GetId();
316 // recursive search for item by id
317 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
322 wxMenuItem
*item
= NULL
;
323 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
325 node
= node
->GetNext() )
327 item
= node
->GetData();
329 if ( item
->GetId() == itemId
)
332 *itemMenu
= (wxMenu
*)this;
334 else if ( item
->IsSubMenu() )
336 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
340 // don't exit the loop
348 // non recursive search
349 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
351 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
352 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
355 for ( pos
= 0; node
; pos
++ )
357 if ( node
->GetData()->GetId() == id
)
359 item
= node
->GetData();
364 node
= node
->GetNext();
369 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
376 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
378 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
379 _T("wxMenu::FindItemByPosition(): invalid menu index") );
381 return m_items
.Item( position
)->GetData();
384 // ----------------------------------------------------------------------------
385 // wxMenu helpers used by derived classes
386 // ----------------------------------------------------------------------------
388 // Update a menu and all submenus recursively. source is the object that has
389 // the update event handlers defined for it. If NULL, the menu or associated
390 // window will be used.
391 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
393 if (GetInvokingWindow())
395 // Don't update menus if the parent
396 // frame is about to get deleted
397 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
398 if (tlw
&& wxPendingDelete
.Member(tlw
))
402 if ( !source
&& GetInvokingWindow() )
403 source
= GetInvokingWindow()->GetEventHandler();
405 source
= GetEventHandler();
409 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
412 wxMenuItem
* item
= node
->GetData();
413 if ( !item
->IsSeparator() )
415 wxWindowID id
= item
->GetId();
416 wxUpdateUIEvent
event(id
);
417 event
.SetEventObject( source
);
419 if ( source
->ProcessEvent(event
) )
421 // if anything changed, update the changed attribute
422 if (event
.GetSetText())
423 SetLabel(id
, event
.GetText());
424 if (event
.GetSetChecked())
425 Check(id
, event
.GetChecked());
426 if (event
.GetSetEnabled())
427 Enable(id
, event
.GetEnabled());
430 // recurse to the submenus
431 if ( item
->GetSubMenu() )
432 item
->GetSubMenu()->UpdateUI(source
);
434 //else: item is a separator (which doesn't process update UI events)
436 node
= node
->GetNext();
440 bool wxMenuBase::SendEvent(int id
, int checked
)
442 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
443 event
.SetEventObject(this);
444 event
.SetInt(checked
);
446 bool processed
= false;
448 // Try the menu's event handler
451 wxEvtHandler
*handler
= GetEventHandler();
453 processed
= handler
->SafelyProcessEvent(event
);
456 // Try the window the menu was popped up from (and up through the
460 const wxMenuBase
*menu
= this;
463 wxWindow
*win
= menu
->GetInvokingWindow();
466 processed
= win
->HandleWindowEvent(event
);
470 menu
= menu
->GetParent();
477 // ----------------------------------------------------------------------------
478 // wxMenu attaching/detaching to/from menu bar
479 // ----------------------------------------------------------------------------
481 wxMenuBar
* wxMenuBase::GetMenuBar() const
484 return GetParent()->GetMenuBar();
488 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
490 // use Detach() instead!
491 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
493 // use IsAttached() to prevent this from happening
494 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
496 m_menuBar
= (wxMenuBar
*)menubar
;
499 void wxMenuBase::Detach()
501 // use IsAttached() to prevent this from happening
502 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
507 // ----------------------------------------------------------------------------
508 // wxMenu functions forwarded to wxMenuItem
509 // ----------------------------------------------------------------------------
511 void wxMenuBase::Enable( int id
, bool enable
)
513 wxMenuItem
*item
= FindItem(id
);
515 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
517 item
->Enable(enable
);
520 bool wxMenuBase::IsEnabled( int id
) const
522 wxMenuItem
*item
= FindItem(id
);
524 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
526 return item
->IsEnabled();
529 void wxMenuBase::Check( int id
, bool enable
)
531 wxMenuItem
*item
= FindItem(id
);
533 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
538 bool wxMenuBase::IsChecked( int id
) const
540 wxMenuItem
*item
= FindItem(id
);
542 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
544 return item
->IsChecked();
547 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
549 wxMenuItem
*item
= FindItem(id
);
551 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
553 item
->SetItemLabel(label
);
556 wxString
wxMenuBase::GetLabel( int id
) const
558 wxMenuItem
*item
= FindItem(id
);
560 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
562 return item
->GetItemLabel();
565 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
567 wxMenuItem
*item
= FindItem(id
);
569 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
571 item
->SetHelp( helpString
);
574 wxString
wxMenuBase::GetHelpString( int id
) const
576 wxMenuItem
*item
= FindItem(id
);
578 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
580 return item
->GetHelp();
583 // ----------------------------------------------------------------------------
584 // wxMenuBarBase ctor and dtor
585 // ----------------------------------------------------------------------------
587 wxMenuBarBase::wxMenuBarBase()
590 m_menuBarFrame
= NULL
;
593 wxMenuBarBase::~wxMenuBarBase()
595 WX_CLEAR_LIST(wxMenuList
, m_menus
);
598 // ----------------------------------------------------------------------------
599 // wxMenuBar item access: the base class versions manage m_menus list, the
600 // derived class should reflect the changes in the real menubar
601 // ----------------------------------------------------------------------------
603 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
605 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
606 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
608 return node
->GetData();
611 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
613 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
615 m_menus
.Append(menu
);
621 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
622 const wxString
& title
)
624 if ( pos
== m_menus
.GetCount() )
626 return wxMenuBarBase::Append(menu
, title
);
628 else // not at the end
630 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
632 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
633 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
635 m_menus
.Insert(node
, menu
);
642 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
643 const wxString
& WXUNUSED(title
))
645 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
647 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
648 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
650 wxMenu
*menuOld
= node
->GetData();
659 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
661 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
662 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
664 wxMenu
*menu
= node
->GetData();
671 int wxMenuBarBase::FindMenu(const wxString
& title
) const
673 wxString label
= wxMenuItem::GetLabelText(title
);
675 size_t count
= GetMenuCount();
676 for ( size_t i
= 0; i
< count
; i
++ )
678 wxString title2
= GetMenuLabel(i
);
679 if ( (title2
== title
) ||
680 (wxMenuItem::GetLabelText(title2
) == label
) )
691 // ----------------------------------------------------------------------------
692 // wxMenuBar attaching/detaching to/from the frame
693 // ----------------------------------------------------------------------------
695 void wxMenuBarBase::Attach(wxFrame
*frame
)
697 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
699 m_menuBarFrame
= frame
;
702 void wxMenuBarBase::Detach()
704 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
706 m_menuBarFrame
= NULL
;
709 // ----------------------------------------------------------------------------
710 // wxMenuBar searching for items
711 // ----------------------------------------------------------------------------
713 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
718 wxMenuItem
*item
= NULL
;
719 size_t count
= GetMenuCount(), i
;
720 wxMenuList::const_iterator it
;
721 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
723 item
= (*it
)->FindItem(id
, menu
);
729 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
731 wxString label
= wxMenuItem::GetLabelText(menu
);
734 wxMenuList::compatibility_iterator node
;
735 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
737 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
738 return node
->GetData()->FindItem(item
);
744 // ---------------------------------------------------------------------------
745 // wxMenuBar functions forwarded to wxMenuItem
746 // ---------------------------------------------------------------------------
748 void wxMenuBarBase::Enable(int id
, bool enable
)
750 wxMenuItem
*item
= FindItem(id
);
752 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
754 item
->Enable(enable
);
757 void wxMenuBarBase::Check(int id
, bool check
)
759 wxMenuItem
*item
= FindItem(id
);
761 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
762 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
767 bool wxMenuBarBase::IsChecked(int id
) const
769 wxMenuItem
*item
= FindItem(id
);
771 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
773 return item
->IsChecked();
776 bool wxMenuBarBase::IsEnabled(int id
) const
778 wxMenuItem
*item
= FindItem(id
);
780 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
782 return item
->IsEnabled();
785 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
787 wxMenuItem
*item
= FindItem(id
);
789 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
791 item
->SetItemLabel(label
);
794 wxString
wxMenuBarBase::GetLabel(int id
) const
796 wxMenuItem
*item
= FindItem(id
);
798 wxCHECK_MSG( item
, wxEmptyString
,
799 wxT("wxMenuBar::GetLabel(): no such item") );
801 return item
->GetItemLabel();
804 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
806 wxMenuItem
*item
= FindItem(id
);
808 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
810 item
->SetHelp(helpString
);
813 wxString
wxMenuBarBase::GetHelpString(int id
) const
815 wxMenuItem
*item
= FindItem(id
);
817 wxCHECK_MSG( item
, wxEmptyString
,
818 wxT("wxMenuBar::GetHelpString(): no such item") );
820 return item
->GetHelp();
823 void wxMenuBarBase::UpdateMenus( void )
825 wxEvtHandler
* source
;
827 int nCount
= GetMenuCount();
828 for (int n
= 0; n
< nCount
; n
++)
833 source
= menu
->GetEventHandler();
835 menu
->UpdateUI( source
);
840 #if WXWIN_COMPATIBILITY_2_8
841 // get or change the label of the menu at given position
842 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
844 SetMenuLabel(pos
, label
);
847 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
849 return GetMenuLabelText(pos
);
853 #endif // wxUSE_MENUS