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 const wxString accel
= GetItemLabel().AfterFirst(wxT('\t'));
119 return accel
.empty() ? NULL
: wxAcceleratorEntry::Create(accel
);
122 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
124 wxString text
= m_text
.BeforeFirst(wxT('\t'));
128 text
+= accel
->ToString();
134 #endif // wxUSE_ACCEL
136 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
140 if ( m_text
.empty() && !IsSeparator() )
142 wxASSERT_MSG( wxIsStockID(GetId()),
143 wxT("A non-stock menu item with an empty label?") );
144 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
145 wxSTOCK_WITH_MNEMONIC
);
149 void wxMenuItemBase::SetHelp(const wxString
& str
)
153 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
155 // get a stock help string
156 m_help
= wxGetStockHelpString(GetId());
161 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
163 return wxStripMenuCodes(text
);
167 #if WXWIN_COMPATIBILITY_2_8
168 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
170 return GetLabelText(text
);
174 bool wxMenuBase::ms_locked
= true;
176 // ----------------------------------------------------------------------------
177 // wxMenu ctor and dtor
178 // ----------------------------------------------------------------------------
180 void wxMenuBase::Init(long style
)
185 m_invokingWindow
= NULL
;
188 m_eventHandler
= this;
191 wxMenuBase::~wxMenuBase()
193 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
196 // ----------------------------------------------------------------------------
197 // wxMenu item adding/removing
198 // ----------------------------------------------------------------------------
200 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
202 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
204 submenu
->SetParent((wxMenu
*)this);
207 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
209 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
211 m_items
.Append(item
);
212 item
->SetMenu((wxMenu
*)this);
213 if ( item
->IsSubMenu() )
215 AddSubMenu(item
->GetSubMenu());
221 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
223 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
225 if ( pos
== GetMenuItemCount() )
227 return DoAppend(item
);
231 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
232 wxT("invalid index in wxMenu::Insert") );
234 return DoInsert(pos
, item
);
238 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
240 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
242 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
243 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
245 m_items
.Insert(node
, item
);
246 item
->SetMenu((wxMenu
*)this);
247 if ( item
->IsSubMenu() )
249 AddSubMenu(item
->GetSubMenu());
255 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
257 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
259 return DoRemove(item
);
262 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
264 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
266 // if we get here, the item is valid or one of Remove() functions is broken
267 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
269 // we detach the item, but we do delete the list node (i.e. don't call
270 // DetachNode() here!)
273 // item isn't attached to anything any more
275 wxMenu
*submenu
= item
->GetSubMenu();
278 submenu
->SetParent(NULL
);
279 if ( submenu
->IsAttached() )
286 bool wxMenuBase::Delete(wxMenuItem
*item
)
288 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
290 return DoDelete(item
);
293 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
295 wxMenuItem
*item2
= DoRemove(item
);
296 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
298 // don't delete the submenu
299 item2
->SetSubMenu(NULL
);
306 bool wxMenuBase::Destroy(wxMenuItem
*item
)
308 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
310 return DoDestroy(item
);
313 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
315 wxMenuItem
*item2
= DoRemove(item
);
316 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
323 // ----------------------------------------------------------------------------
324 // wxMenu searching for items
325 // ----------------------------------------------------------------------------
327 // Finds the item id matching the given string, wxNOT_FOUND if not found.
328 int wxMenuBase::FindItem(const wxString
& text
) const
330 wxString label
= wxMenuItem::GetLabelText(text
);
331 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
333 node
= node
->GetNext() )
335 wxMenuItem
*item
= node
->GetData();
336 if ( item
->IsSubMenu() )
338 int rc
= item
->GetSubMenu()->FindItem(label
);
339 if ( rc
!= wxNOT_FOUND
)
343 // we execute this code for submenus as well to alllow finding them by
344 // name just like the ordinary items
345 if ( !item
->IsSeparator() )
347 if ( item
->GetItemLabelText() == label
)
348 return item
->GetId();
355 // recursive search for item by id
356 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
361 wxMenuItem
*item
= NULL
;
362 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
364 node
= node
->GetNext() )
366 item
= node
->GetData();
368 if ( item
->GetId() == itemId
)
371 *itemMenu
= (wxMenu
*)this;
373 else if ( item
->IsSubMenu() )
375 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
379 // don't exit the loop
387 // non recursive search
388 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
390 wxMenuItem
*item
= NULL
;
391 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
394 for ( pos
= 0; node
; pos
++ )
396 if ( node
->GetData()->GetId() == id
)
398 item
= node
->GetData();
403 node
= node
->GetNext();
408 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
415 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
417 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
418 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
420 return m_items
.Item( position
)->GetData();
423 // ----------------------------------------------------------------------------
424 // wxMenu helpers used by derived classes
425 // ----------------------------------------------------------------------------
427 // Update a menu and all submenus recursively. source is the object that has
428 // the update event handlers defined for it. If NULL, the menu or associated
429 // window will be used.
430 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
432 if (GetInvokingWindow())
434 // Don't update menus if the parent
435 // frame is about to get deleted
436 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
437 if (tlw
&& wxPendingDelete
.Member(tlw
))
441 if ( !source
&& GetInvokingWindow() )
442 source
= GetInvokingWindow()->GetEventHandler();
444 source
= GetEventHandler();
448 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
451 wxMenuItem
* item
= node
->GetData();
452 if ( !item
->IsSeparator() )
454 wxWindowID id
= item
->GetId();
455 wxUpdateUIEvent
event(id
);
456 event
.SetEventObject( source
);
458 if ( source
->ProcessEvent(event
) )
460 // if anything changed, update the changed attribute
461 if (event
.GetSetText())
462 SetLabel(id
, event
.GetText());
463 if (event
.GetSetChecked())
464 Check(id
, event
.GetChecked());
465 if (event
.GetSetEnabled())
466 Enable(id
, event
.GetEnabled());
469 // recurse to the submenus
470 if ( item
->GetSubMenu() )
471 item
->GetSubMenu()->UpdateUI(source
);
473 //else: item is a separator (which doesn't process update UI events)
475 node
= node
->GetNext();
479 bool wxMenuBase::SendEvent(int id
, int checked
)
481 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
482 event
.SetEventObject(this);
483 event
.SetInt(checked
);
485 bool processed
= false;
487 // Try the menu's event handler first
488 wxEvtHandler
*handler
= GetEventHandler();
490 processed
= handler
->SafelyProcessEvent(event
);
492 // Try the window the menu was popped up from or its menu bar belongs to
495 wxWindow
* const win
= GetWindow();
497 processed
= win
->HandleWindowEvent(event
);
503 // ----------------------------------------------------------------------------
504 // wxMenu attaching/detaching to/from menu bar
505 // ----------------------------------------------------------------------------
507 wxMenuBar
* wxMenuBase::GetMenuBar() const
510 return GetParent()->GetMenuBar();
514 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
516 // use Detach() instead!
517 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
519 // use IsAttached() to prevent this from happening
520 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
522 m_menuBar
= (wxMenuBar
*)menubar
;
525 void wxMenuBase::Detach()
527 // use IsAttached() to prevent this from happening
528 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
533 // ----------------------------------------------------------------------------
534 // wxMenu invoking window handling
535 // ----------------------------------------------------------------------------
537 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
539 wxASSERT_MSG( !GetParent(),
540 "should only be called for top level popup menus" );
541 wxASSERT_MSG( !IsAttached(),
542 "menus attached to menu bar can't have invoking window" );
544 m_invokingWindow
= win
;
547 wxWindow
*wxMenuBase::GetWindow() const
549 // only the top level menus have non-NULL invoking window or a pointer to
550 // the menu bar so recurse upwards until we find it
551 const wxMenuBase
*menu
= this;
552 while ( menu
->GetParent() )
554 menu
= menu
->GetParent();
557 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
558 : menu
->GetInvokingWindow();
561 // ----------------------------------------------------------------------------
562 // wxMenu functions forwarded to wxMenuItem
563 // ----------------------------------------------------------------------------
565 void wxMenuBase::Enable( int id
, bool enable
)
567 wxMenuItem
*item
= FindItem(id
);
569 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
571 item
->Enable(enable
);
574 bool wxMenuBase::IsEnabled( int id
) const
576 wxMenuItem
*item
= FindItem(id
);
578 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
580 return item
->IsEnabled();
583 void wxMenuBase::Check( int id
, bool enable
)
585 wxMenuItem
*item
= FindItem(id
);
587 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
592 bool wxMenuBase::IsChecked( int id
) const
594 wxMenuItem
*item
= FindItem(id
);
596 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
598 return item
->IsChecked();
601 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
603 wxMenuItem
*item
= FindItem(id
);
605 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
607 item
->SetItemLabel(label
);
610 wxString
wxMenuBase::GetLabel( int id
) const
612 wxMenuItem
*item
= FindItem(id
);
614 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
616 return item
->GetItemLabel();
619 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
621 wxMenuItem
*item
= FindItem(id
);
623 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
625 item
->SetHelp( helpString
);
628 wxString
wxMenuBase::GetHelpString( int id
) const
630 wxMenuItem
*item
= FindItem(id
);
632 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
634 return item
->GetHelp();
637 // ----------------------------------------------------------------------------
638 // wxMenuBarBase ctor and dtor
639 // ----------------------------------------------------------------------------
641 wxMenuBarBase::wxMenuBarBase()
644 m_menuBarFrame
= NULL
;
647 wxMenuBarBase::~wxMenuBarBase()
649 WX_CLEAR_LIST(wxMenuList
, m_menus
);
652 // ----------------------------------------------------------------------------
653 // wxMenuBar item access: the base class versions manage m_menus list, the
654 // derived class should reflect the changes in the real menubar
655 // ----------------------------------------------------------------------------
657 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
659 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
660 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
662 return node
->GetData();
665 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
667 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
668 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
670 m_menus
.Append(menu
);
676 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
677 const wxString
& title
)
679 if ( pos
== m_menus
.GetCount() )
681 return wxMenuBarBase::Append(menu
, title
);
683 else // not at the end
685 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
687 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
688 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
690 m_menus
.Insert(node
, menu
);
697 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
698 const wxString
& WXUNUSED(title
))
700 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
702 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
703 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
705 wxMenu
*menuOld
= node
->GetData();
714 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
716 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
717 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
719 wxMenu
*menu
= node
->GetData();
726 int wxMenuBarBase::FindMenu(const wxString
& title
) const
728 wxString label
= wxMenuItem::GetLabelText(title
);
730 size_t count
= GetMenuCount();
731 for ( size_t i
= 0; i
< count
; i
++ )
733 wxString title2
= GetMenuLabel(i
);
734 if ( (title2
== title
) ||
735 (wxMenuItem::GetLabelText(title2
) == label
) )
746 // ----------------------------------------------------------------------------
747 // wxMenuBar attaching/detaching to/from the frame
748 // ----------------------------------------------------------------------------
750 void wxMenuBarBase::Attach(wxFrame
*frame
)
752 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
754 m_menuBarFrame
= frame
;
757 void wxMenuBarBase::Detach()
759 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
761 m_menuBarFrame
= NULL
;
764 // ----------------------------------------------------------------------------
765 // wxMenuBar searching for items
766 // ----------------------------------------------------------------------------
768 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
773 wxMenuItem
*item
= NULL
;
774 size_t count
= GetMenuCount(), i
;
775 wxMenuList::const_iterator it
;
776 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
778 item
= (*it
)->FindItem(id
, menu
);
784 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
786 wxString label
= wxMenuItem::GetLabelText(menu
);
789 wxMenuList::compatibility_iterator node
;
790 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
792 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
793 return node
->GetData()->FindItem(item
);
799 // ---------------------------------------------------------------------------
800 // wxMenuBar functions forwarded to wxMenuItem
801 // ---------------------------------------------------------------------------
803 void wxMenuBarBase::Enable(int id
, bool enable
)
805 wxMenuItem
*item
= FindItem(id
);
807 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
809 item
->Enable(enable
);
812 void wxMenuBarBase::Check(int id
, bool check
)
814 wxMenuItem
*item
= FindItem(id
);
816 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
817 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
822 bool wxMenuBarBase::IsChecked(int id
) const
824 wxMenuItem
*item
= FindItem(id
);
826 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
828 return item
->IsChecked();
831 bool wxMenuBarBase::IsEnabled(int id
) const
833 wxMenuItem
*item
= FindItem(id
);
835 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
837 return item
->IsEnabled();
840 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
842 wxMenuItem
*item
= FindItem(id
);
844 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
846 item
->SetItemLabel(label
);
849 wxString
wxMenuBarBase::GetLabel(int id
) const
851 wxMenuItem
*item
= FindItem(id
);
853 wxCHECK_MSG( item
, wxEmptyString
,
854 wxT("wxMenuBar::GetLabel(): no such item") );
856 return item
->GetItemLabel();
859 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
861 wxMenuItem
*item
= FindItem(id
);
863 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
865 item
->SetHelp(helpString
);
868 wxString
wxMenuBarBase::GetHelpString(int id
) const
870 wxMenuItem
*item
= FindItem(id
);
872 wxCHECK_MSG( item
, wxEmptyString
,
873 wxT("wxMenuBar::GetHelpString(): no such item") );
875 return item
->GetHelp();
878 void wxMenuBarBase::UpdateMenus()
880 wxEvtHandler
* source
;
882 int nCount
= GetMenuCount();
883 for (int n
= 0; n
< nCount
; n
++)
888 source
= menu
->GetEventHandler();
890 menu
->UpdateUI( source
);
895 #if WXWIN_COMPATIBILITY_2_8
896 // get or change the label of the menu at given position
897 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
899 SetMenuLabel(pos
, label
);
902 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
904 return GetMenuLabelText(pos
);
908 #endif // wxUSE_MENUS