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 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
65 m_id
= wxWindow::NewControlId();
69 m_id
= wxID_SEPARATOR
;
71 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
72 // and it makes sense to omit the following optional parameters,
73 // including the kind one which doesn't default to wxITEM_SEPARATOR,
74 // of course, so override it here
75 kind
= wxITEM_SEPARATOR
;
79 // (popup) menu titles in wxMSW use this ID to indicate that
80 // it's not a real menu item, so we don't want the check below to
86 // ids are limited to 16 bits under MSW so portable code shouldn't
87 // use ids outside of this range (negative ids generated by wx are
89 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
90 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
91 wxS("invalid id value") );
95 // notice that parentMenu can be NULL: the item can be attached to the menu
96 // later with SetMenu()
98 m_parentMenu
= parentMenu
;
108 wxMenuItemBase::~wxMenuItemBase()
115 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
117 return wxAcceleratorEntry::Create(GetItemLabel());
120 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
122 wxString text
= m_text
.BeforeFirst(wxT('\t'));
126 text
+= accel
->ToString();
132 #endif // wxUSE_ACCEL
134 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
138 if ( m_text
.empty() && !IsSeparator() )
140 wxASSERT_MSG( wxIsStockID(GetId()),
141 wxT("A non-stock menu item with an empty label?") );
142 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
143 wxSTOCK_WITH_MNEMONIC
);
147 void wxMenuItemBase::SetHelp(const wxString
& str
)
151 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
153 // get a stock help string
154 m_help
= wxGetStockHelpString(GetId());
159 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
161 return wxStripMenuCodes(text
);
165 #if WXWIN_COMPATIBILITY_2_8
166 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
168 return GetLabelText(text
);
172 bool wxMenuBase::ms_locked
= true;
174 // ----------------------------------------------------------------------------
175 // wxMenu ctor and dtor
176 // ----------------------------------------------------------------------------
178 void wxMenuBase::Init(long style
)
183 m_invokingWindow
= NULL
;
186 m_eventHandler
= this;
189 wxMenuBase::~wxMenuBase()
191 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
194 // ----------------------------------------------------------------------------
195 // wxMenu item adding/removing
196 // ----------------------------------------------------------------------------
198 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
200 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
202 submenu
->SetParent((wxMenu
*)this);
205 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
207 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
209 m_items
.Append(item
);
210 item
->SetMenu((wxMenu
*)this);
211 if ( item
->IsSubMenu() )
213 AddSubMenu(item
->GetSubMenu());
219 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
221 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
223 if ( pos
== GetMenuItemCount() )
225 return DoAppend(item
);
229 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
230 wxT("invalid index in wxMenu::Insert") );
232 return DoInsert(pos
, item
);
236 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
238 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
240 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
241 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
243 m_items
.Insert(node
, item
);
244 item
->SetMenu((wxMenu
*)this);
245 if ( item
->IsSubMenu() )
247 AddSubMenu(item
->GetSubMenu());
253 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
255 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
257 return DoRemove(item
);
260 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
262 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
264 // if we get here, the item is valid or one of Remove() functions is broken
265 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
267 // we detach the item, but we do delete the list node (i.e. don't call
268 // DetachNode() here!)
271 // item isn't attached to anything any more
273 wxMenu
*submenu
= item
->GetSubMenu();
276 submenu
->SetParent(NULL
);
277 if ( submenu
->IsAttached() )
284 bool wxMenuBase::Delete(wxMenuItem
*item
)
286 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
288 return DoDelete(item
);
291 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
293 wxMenuItem
*item2
= DoRemove(item
);
294 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
296 // don't delete the submenu
297 item2
->SetSubMenu(NULL
);
304 bool wxMenuBase::Destroy(wxMenuItem
*item
)
306 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
308 return DoDestroy(item
);
311 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
313 wxMenuItem
*item2
= DoRemove(item
);
314 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
321 // ----------------------------------------------------------------------------
322 // wxMenu searching for items
323 // ----------------------------------------------------------------------------
325 // Finds the item id matching the given string, wxNOT_FOUND if not found.
326 int wxMenuBase::FindItem(const wxString
& text
) const
328 wxString label
= wxMenuItem::GetLabelText(text
);
329 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
331 node
= node
->GetNext() )
333 wxMenuItem
*item
= node
->GetData();
334 if ( item
->IsSubMenu() )
336 int rc
= item
->GetSubMenu()->FindItem(label
);
337 if ( rc
!= wxNOT_FOUND
)
341 // we execute this code for submenus as well to alllow finding them by
342 // name just like the ordinary items
343 if ( !item
->IsSeparator() )
345 if ( item
->GetItemLabelText() == label
)
346 return item
->GetId();
353 // recursive search for item by id
354 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
359 wxMenuItem
*item
= NULL
;
360 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
362 node
= node
->GetNext() )
364 item
= node
->GetData();
366 if ( item
->GetId() == itemId
)
369 *itemMenu
= (wxMenu
*)this;
371 else if ( item
->IsSubMenu() )
373 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
377 // don't exit the loop
385 // non recursive search
386 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
388 wxMenuItem
*item
= NULL
;
389 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
392 for ( pos
= 0; node
; pos
++ )
394 if ( node
->GetData()->GetId() == id
)
396 item
= node
->GetData();
401 node
= node
->GetNext();
406 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
413 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
415 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
416 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
418 return m_items
.Item( position
)->GetData();
421 // ----------------------------------------------------------------------------
422 // wxMenu helpers used by derived classes
423 // ----------------------------------------------------------------------------
425 // Update a menu and all submenus recursively. source is the object that has
426 // the update event handlers defined for it. If NULL, the menu or associated
427 // window will be used.
428 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
430 if (GetInvokingWindow())
432 // Don't update menus if the parent
433 // frame is about to get deleted
434 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
435 if (tlw
&& wxPendingDelete
.Member(tlw
))
439 if ( !source
&& GetInvokingWindow() )
440 source
= GetInvokingWindow()->GetEventHandler();
442 source
= GetEventHandler();
446 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
449 wxMenuItem
* item
= node
->GetData();
450 if ( !item
->IsSeparator() )
452 wxWindowID id
= item
->GetId();
453 wxUpdateUIEvent
event(id
);
454 event
.SetEventObject( source
);
456 if ( source
->ProcessEvent(event
) )
458 // if anything changed, update the changed attribute
459 if (event
.GetSetText())
460 SetLabel(id
, event
.GetText());
461 if (event
.GetSetChecked())
462 Check(id
, event
.GetChecked());
463 if (event
.GetSetEnabled())
464 Enable(id
, event
.GetEnabled());
467 // recurse to the submenus
468 if ( item
->GetSubMenu() )
469 item
->GetSubMenu()->UpdateUI(source
);
471 //else: item is a separator (which doesn't process update UI events)
473 node
= node
->GetNext();
477 bool wxMenuBase::SendEvent(int id
, int checked
)
479 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
480 event
.SetEventObject(this);
481 event
.SetInt(checked
);
483 bool processed
= false;
485 // Try the menu's event handler first
486 wxEvtHandler
*handler
= GetEventHandler();
488 processed
= handler
->SafelyProcessEvent(event
);
490 // Try the window the menu was popped up from or its menu bar belongs to
493 wxWindow
* const win
= GetWindow();
495 processed
= win
->HandleWindowEvent(event
);
501 // ----------------------------------------------------------------------------
502 // wxMenu attaching/detaching to/from menu bar
503 // ----------------------------------------------------------------------------
505 wxMenuBar
* wxMenuBase::GetMenuBar() const
508 return GetParent()->GetMenuBar();
512 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
514 // use Detach() instead!
515 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
517 // use IsAttached() to prevent this from happening
518 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
520 m_menuBar
= (wxMenuBar
*)menubar
;
523 void wxMenuBase::Detach()
525 // use IsAttached() to prevent this from happening
526 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
531 // ----------------------------------------------------------------------------
532 // wxMenu invoking window handling
533 // ----------------------------------------------------------------------------
535 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
537 wxASSERT_MSG( !GetParent(),
538 "should only be called for top level popup menus" );
539 wxASSERT_MSG( !IsAttached(),
540 "menus attached to menu bar can't have invoking window" );
542 m_invokingWindow
= win
;
545 wxWindow
*wxMenuBase::GetWindow() const
547 // only the top level menus have non-NULL invoking window or a pointer to
548 // the menu bar so recurse upwards until we find it
549 const wxMenuBase
*menu
= this;
550 while ( menu
->GetParent() )
552 menu
= menu
->GetParent();
555 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
556 : menu
->GetInvokingWindow();
559 // ----------------------------------------------------------------------------
560 // wxMenu functions forwarded to wxMenuItem
561 // ----------------------------------------------------------------------------
563 void wxMenuBase::Enable( int id
, bool enable
)
565 wxMenuItem
*item
= FindItem(id
);
567 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
569 item
->Enable(enable
);
572 bool wxMenuBase::IsEnabled( int id
) const
574 wxMenuItem
*item
= FindItem(id
);
576 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
578 return item
->IsEnabled();
581 void wxMenuBase::Check( int id
, bool enable
)
583 wxMenuItem
*item
= FindItem(id
);
585 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
590 bool wxMenuBase::IsChecked( int id
) const
592 wxMenuItem
*item
= FindItem(id
);
594 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
596 return item
->IsChecked();
599 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
601 wxMenuItem
*item
= FindItem(id
);
603 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
605 item
->SetItemLabel(label
);
608 wxString
wxMenuBase::GetLabel( int id
) const
610 wxMenuItem
*item
= FindItem(id
);
612 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
614 return item
->GetItemLabel();
617 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
619 wxMenuItem
*item
= FindItem(id
);
621 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
623 item
->SetHelp( helpString
);
626 wxString
wxMenuBase::GetHelpString( int id
) const
628 wxMenuItem
*item
= FindItem(id
);
630 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
632 return item
->GetHelp();
635 // ----------------------------------------------------------------------------
636 // wxMenuBarBase ctor and dtor
637 // ----------------------------------------------------------------------------
639 wxMenuBarBase::wxMenuBarBase()
642 m_menuBarFrame
= NULL
;
645 wxMenuBarBase::~wxMenuBarBase()
647 WX_CLEAR_LIST(wxMenuList
, m_menus
);
650 // ----------------------------------------------------------------------------
651 // wxMenuBar item access: the base class versions manage m_menus list, the
652 // derived class should reflect the changes in the real menubar
653 // ----------------------------------------------------------------------------
655 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
657 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
658 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
660 return node
->GetData();
663 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
665 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
666 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
668 m_menus
.Append(menu
);
674 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
675 const wxString
& title
)
677 if ( pos
== m_menus
.GetCount() )
679 return wxMenuBarBase::Append(menu
, title
);
681 else // not at the end
683 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
685 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
686 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
688 m_menus
.Insert(node
, menu
);
695 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
696 const wxString
& WXUNUSED(title
))
698 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
700 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
701 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
703 wxMenu
*menuOld
= node
->GetData();
712 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
714 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
715 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
717 wxMenu
*menu
= node
->GetData();
724 int wxMenuBarBase::FindMenu(const wxString
& title
) const
726 wxString label
= wxMenuItem::GetLabelText(title
);
728 size_t count
= GetMenuCount();
729 for ( size_t i
= 0; i
< count
; i
++ )
731 wxString title2
= GetMenuLabel(i
);
732 if ( (title2
== title
) ||
733 (wxMenuItem::GetLabelText(title2
) == label
) )
744 // ----------------------------------------------------------------------------
745 // wxMenuBar attaching/detaching to/from the frame
746 // ----------------------------------------------------------------------------
748 void wxMenuBarBase::Attach(wxFrame
*frame
)
750 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
752 m_menuBarFrame
= frame
;
755 void wxMenuBarBase::Detach()
757 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
759 m_menuBarFrame
= NULL
;
762 // ----------------------------------------------------------------------------
763 // wxMenuBar searching for items
764 // ----------------------------------------------------------------------------
766 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
771 wxMenuItem
*item
= NULL
;
772 size_t count
= GetMenuCount(), i
;
773 wxMenuList::const_iterator it
;
774 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
776 item
= (*it
)->FindItem(id
, menu
);
782 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
784 wxString label
= wxMenuItem::GetLabelText(menu
);
787 wxMenuList::compatibility_iterator node
;
788 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
790 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
791 return node
->GetData()->FindItem(item
);
797 // ---------------------------------------------------------------------------
798 // wxMenuBar functions forwarded to wxMenuItem
799 // ---------------------------------------------------------------------------
801 void wxMenuBarBase::Enable(int id
, bool enable
)
803 wxMenuItem
*item
= FindItem(id
);
805 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
807 item
->Enable(enable
);
810 void wxMenuBarBase::Check(int id
, bool check
)
812 wxMenuItem
*item
= FindItem(id
);
814 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
815 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
820 bool wxMenuBarBase::IsChecked(int id
) const
822 wxMenuItem
*item
= FindItem(id
);
824 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
826 return item
->IsChecked();
829 bool wxMenuBarBase::IsEnabled(int id
) const
831 wxMenuItem
*item
= FindItem(id
);
833 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
835 return item
->IsEnabled();
838 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
840 wxMenuItem
*item
= FindItem(id
);
842 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
844 item
->SetItemLabel(label
);
847 wxString
wxMenuBarBase::GetLabel(int id
) const
849 wxMenuItem
*item
= FindItem(id
);
851 wxCHECK_MSG( item
, wxEmptyString
,
852 wxT("wxMenuBar::GetLabel(): no such item") );
854 return item
->GetItemLabel();
857 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
859 wxMenuItem
*item
= FindItem(id
);
861 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
863 item
->SetHelp(helpString
);
866 wxString
wxMenuBarBase::GetHelpString(int id
) const
868 wxMenuItem
*item
= FindItem(id
);
870 wxCHECK_MSG( item
, wxEmptyString
,
871 wxT("wxMenuBar::GetHelpString(): no such item") );
873 return item
->GetHelp();
876 void wxMenuBarBase::UpdateMenus()
878 wxEvtHandler
* source
;
880 int nCount
= GetMenuCount();
881 for (int n
= 0; n
< nCount
; n
++)
886 source
= menu
->GetEventHandler();
888 menu
->UpdateUI( source
);
893 #if WXWIN_COMPATIBILITY_2_8
894 // get or change the label of the menu at given position
895 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
897 SetMenuLabel(pos
, label
);
900 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
902 return GetMenuLabelText(pos
);
906 #endif // wxUSE_MENUS