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
;
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
);
156 // Actually, in GTK, the submenus have to get deleted first.
159 // ----------------------------------------------------------------------------
160 // wxMenu item adding/removing
161 // ----------------------------------------------------------------------------
163 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
165 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
167 submenu
->SetParent((wxMenu
*)this);
170 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
172 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
174 m_items
.Append(item
);
175 item
->SetMenu((wxMenu
*)this);
176 if ( item
->IsSubMenu() )
178 AddSubMenu(item
->GetSubMenu());
184 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
186 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
188 if ( pos
== GetMenuItemCount() )
190 return DoAppend(item
);
194 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
195 wxT("invalid index in wxMenu::Insert") );
197 return DoInsert(pos
, item
);
201 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
203 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
205 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
206 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
208 m_items
.Insert(node
, item
);
209 item
->SetMenu((wxMenu
*)this);
210 if ( item
->IsSubMenu() )
212 AddSubMenu(item
->GetSubMenu());
218 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
220 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
222 return DoRemove(item
);
225 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
227 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
229 // if we get here, the item is valid or one of Remove() functions is broken
230 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
232 // we detach the item, but we do delete the list node (i.e. don't call
233 // DetachNode() here!)
236 // item isn't attached to anything any more
237 item
->SetMenu((wxMenu
*)NULL
);
238 wxMenu
*submenu
= item
->GetSubMenu();
241 submenu
->SetParent((wxMenu
*)NULL
);
242 if ( submenu
->IsAttached() )
249 bool wxMenuBase::Delete(wxMenuItem
*item
)
251 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
253 return DoDelete(item
);
256 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
258 wxMenuItem
*item2
= DoRemove(item
);
259 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
261 // don't delete the submenu
262 item2
->SetSubMenu((wxMenu
*)NULL
);
269 bool wxMenuBase::Destroy(wxMenuItem
*item
)
271 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
273 return DoDestroy(item
);
276 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
278 wxMenuItem
*item2
= DoRemove(item
);
279 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
286 // ----------------------------------------------------------------------------
287 // wxMenu searching for items
288 // ----------------------------------------------------------------------------
290 // Finds the item id matching the given string, wxNOT_FOUND if not found.
291 int wxMenuBase::FindItem(const wxString
& text
) const
293 wxString label
= wxMenuItem::GetLabelText(text
);
294 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
296 node
= node
->GetNext() )
298 wxMenuItem
*item
= node
->GetData();
299 if ( item
->IsSubMenu() )
301 int rc
= item
->GetSubMenu()->FindItem(label
);
302 if ( rc
!= wxNOT_FOUND
)
306 // we execute this code for submenus as well to alllow finding them by
307 // name just like the ordinary items
308 if ( !item
->IsSeparator() )
310 if ( item
->GetItemLabelText() == label
)
311 return item
->GetId();
318 // recursive search for item by id
319 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
324 wxMenuItem
*item
= NULL
;
325 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
327 node
= node
->GetNext() )
329 item
= node
->GetData();
331 if ( item
->GetId() == itemId
)
334 *itemMenu
= (wxMenu
*)this;
336 else if ( item
->IsSubMenu() )
338 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
342 // don't exit the loop
350 // non recursive search
351 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
353 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
354 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
357 for ( pos
= 0; node
; pos
++ )
359 if ( node
->GetData()->GetId() == id
)
361 item
= node
->GetData();
366 node
= node
->GetNext();
371 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
378 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
380 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
381 _T("wxMenu::FindItemByPosition(): invalid menu index") );
383 return m_items
.Item( position
)->GetData();
386 // ----------------------------------------------------------------------------
387 // wxMenu helpers used by derived classes
388 // ----------------------------------------------------------------------------
390 // Update a menu and all submenus recursively. source is the object that has
391 // the update event handlers defined for it. If NULL, the menu or associated
392 // window will be used.
393 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
395 if (GetInvokingWindow())
397 // Don't update menus if the parent
398 // frame is about to get deleted
399 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
400 if (tlw
&& wxPendingDelete
.Member(tlw
))
404 if ( !source
&& GetInvokingWindow() )
405 source
= GetInvokingWindow()->GetEventHandler();
407 source
= GetEventHandler();
411 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
414 wxMenuItem
* item
= node
->GetData();
415 if ( !item
->IsSeparator() )
417 wxWindowID id
= item
->GetId();
418 wxUpdateUIEvent
event(id
);
419 event
.SetEventObject( source
);
421 if ( source
->ProcessEvent(event
) )
423 // if anything changed, update the changed attribute
424 if (event
.GetSetText())
425 SetLabel(id
, event
.GetText());
426 if (event
.GetSetChecked())
427 Check(id
, event
.GetChecked());
428 if (event
.GetSetEnabled())
429 Enable(id
, event
.GetEnabled());
432 // recurse to the submenus
433 if ( item
->GetSubMenu() )
434 item
->GetSubMenu()->UpdateUI(source
);
436 //else: item is a separator (which doesn't process update UI events)
438 node
= node
->GetNext();
442 bool wxMenuBase::SendEvent(int id
, int checked
)
444 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
445 event
.SetEventObject(this);
446 event
.SetInt(checked
);
448 bool processed
= false;
450 // Try the menu's event handler
453 wxEvtHandler
*handler
= GetEventHandler();
455 processed
= handler
->ProcessEvent(event
);
458 // Try the window the menu was popped up from (and up through the
462 const wxMenuBase
*menu
= this;
465 wxWindow
*win
= menu
->GetInvokingWindow();
468 processed
= win
->GetEventHandler()->ProcessEvent(event
);
472 menu
= menu
->GetParent();
479 // ----------------------------------------------------------------------------
480 // wxMenu attaching/detaching to/from menu bar
481 // ----------------------------------------------------------------------------
483 wxMenuBar
* wxMenuBase::GetMenuBar() const
486 return GetParent()->GetMenuBar();
490 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
492 // use Detach() instead!
493 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
495 // use IsAttached() to prevent this from happening
496 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
498 m_menuBar
= (wxMenuBar
*)menubar
;
501 void wxMenuBase::Detach()
503 // use IsAttached() to prevent this from happening
504 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
509 // ----------------------------------------------------------------------------
510 // wxMenu functions forwarded to wxMenuItem
511 // ----------------------------------------------------------------------------
513 void wxMenuBase::Enable( int id
, bool enable
)
515 wxMenuItem
*item
= FindItem(id
);
517 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
519 item
->Enable(enable
);
522 bool wxMenuBase::IsEnabled( int id
) const
524 wxMenuItem
*item
= FindItem(id
);
526 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
528 return item
->IsEnabled();
531 void wxMenuBase::Check( int id
, bool enable
)
533 wxMenuItem
*item
= FindItem(id
);
535 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
540 bool wxMenuBase::IsChecked( int id
) const
542 wxMenuItem
*item
= FindItem(id
);
544 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
546 return item
->IsChecked();
549 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
551 wxMenuItem
*item
= FindItem(id
);
553 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
555 item
->SetItemLabel(label
);
558 wxString
wxMenuBase::GetLabel( int id
) const
560 wxMenuItem
*item
= FindItem(id
);
562 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
564 return item
->GetItemLabel();
567 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
569 wxMenuItem
*item
= FindItem(id
);
571 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
573 item
->SetHelp( helpString
);
576 wxString
wxMenuBase::GetHelpString( int id
) const
578 wxMenuItem
*item
= FindItem(id
);
580 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
582 return item
->GetHelp();
585 // ----------------------------------------------------------------------------
586 // wxMenuBarBase ctor and dtor
587 // ----------------------------------------------------------------------------
589 wxMenuBarBase::wxMenuBarBase()
592 m_menuBarFrame
= NULL
;
595 wxMenuBarBase::~wxMenuBarBase()
597 WX_CLEAR_LIST(wxMenuList
, m_menus
);
600 // ----------------------------------------------------------------------------
601 // wxMenuBar item access: the base class versions manage m_menus list, the
602 // derived class should reflect the changes in the real menubar
603 // ----------------------------------------------------------------------------
605 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
607 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
608 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
610 return node
->GetData();
613 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
615 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
617 m_menus
.Append(menu
);
623 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
624 const wxString
& title
)
626 if ( pos
== m_menus
.GetCount() )
628 return wxMenuBarBase::Append(menu
, title
);
630 else // not at the end
632 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
634 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
635 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
637 m_menus
.Insert(node
, menu
);
644 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
645 const wxString
& WXUNUSED(title
))
647 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
649 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
650 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
652 wxMenu
*menuOld
= node
->GetData();
661 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
663 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
664 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
666 wxMenu
*menu
= node
->GetData();
673 int wxMenuBarBase::FindMenu(const wxString
& title
) const
675 wxString label
= wxMenuItem::GetLabelText(title
);
677 size_t count
= GetMenuCount();
678 for ( size_t i
= 0; i
< count
; i
++ )
680 wxString title2
= GetMenuLabel(i
);
681 if ( (title2
== title
) ||
682 (wxMenuItem::GetLabelText(title2
) == label
) )
693 // ----------------------------------------------------------------------------
694 // wxMenuBar attaching/detaching to/from the frame
695 // ----------------------------------------------------------------------------
697 void wxMenuBarBase::Attach(wxFrame
*frame
)
699 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
701 m_menuBarFrame
= frame
;
704 void wxMenuBarBase::Detach()
706 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
708 m_menuBarFrame
= NULL
;
711 // ----------------------------------------------------------------------------
712 // wxMenuBar searching for items
713 // ----------------------------------------------------------------------------
715 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
720 wxMenuItem
*item
= NULL
;
721 size_t count
= GetMenuCount(), i
;
722 wxMenuList::const_iterator it
;
723 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
725 item
= (*it
)->FindItem(id
, menu
);
731 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
733 wxString label
= wxMenuItem::GetLabelText(menu
);
736 wxMenuList::compatibility_iterator node
;
737 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
739 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
740 return node
->GetData()->FindItem(item
);
746 // ---------------------------------------------------------------------------
747 // wxMenuBar functions forwarded to wxMenuItem
748 // ---------------------------------------------------------------------------
750 void wxMenuBarBase::Enable(int id
, bool enable
)
752 wxMenuItem
*item
= FindItem(id
);
754 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
756 item
->Enable(enable
);
759 void wxMenuBarBase::Check(int id
, bool check
)
761 wxMenuItem
*item
= FindItem(id
);
763 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
764 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
769 bool wxMenuBarBase::IsChecked(int id
) const
771 wxMenuItem
*item
= FindItem(id
);
773 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
775 return item
->IsChecked();
778 bool wxMenuBarBase::IsEnabled(int id
) const
780 wxMenuItem
*item
= FindItem(id
);
782 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
784 return item
->IsEnabled();
787 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
789 wxMenuItem
*item
= FindItem(id
);
791 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
793 item
->SetItemLabel(label
);
796 wxString
wxMenuBarBase::GetLabel(int id
) const
798 wxMenuItem
*item
= FindItem(id
);
800 wxCHECK_MSG( item
, wxEmptyString
,
801 wxT("wxMenuBar::GetLabel(): no such item") );
803 return item
->GetItemLabel();
806 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
808 wxMenuItem
*item
= FindItem(id
);
810 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
812 item
->SetHelp(helpString
);
815 wxString
wxMenuBarBase::GetHelpString(int id
) const
817 wxMenuItem
*item
= FindItem(id
);
819 wxCHECK_MSG( item
, wxEmptyString
,
820 wxT("wxMenuBar::GetHelpString(): no such item") );
822 return item
->GetHelp();
825 void wxMenuBarBase::UpdateMenus( void )
827 wxEvtHandler
* source
;
829 int nCount
= GetMenuCount();
830 for (int n
= 0; n
< nCount
; n
++)
835 source
= menu
->GetEventHandler();
837 menu
->UpdateUI( source
);
842 #if WXWIN_COMPATIBILITY_2_8
843 // get or change the label of the menu at given position
844 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
846 SetMenuLabel(pos
, label
);
849 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
851 return GetMenuLabelText(pos
);
856 #endif // wxUSE_MENUS