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
;
70 m_kind
= wxITEM_SEPARATOR
;
74 // ids are limited to 16 bits under MSW so portable code shouldn't
75 // use ids outside of this range (negative ids generated by wx are
77 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
78 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
79 wxS("invalid id value") );
83 // notice that parentMenu can be NULL: the item can be attached to the menu
84 // later with SetMenu()
86 m_parentMenu
= parentMenu
;
97 wxMenuItemBase::~wxMenuItemBase()
104 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
106 return wxAcceleratorEntry::Create(GetItemLabel());
109 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
111 wxString text
= m_text
.BeforeFirst(wxT('\t'));
115 text
+= accel
->ToString();
121 #endif // wxUSE_ACCEL
123 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
127 if ( m_text
.empty() && !IsSeparator() )
129 wxASSERT_MSG( wxIsStockID(GetId()),
130 wxT("A non-stock menu item with an empty label?") );
131 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
132 wxSTOCK_WITH_MNEMONIC
);
136 void wxMenuItemBase::SetHelp(const wxString
& str
)
140 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
142 // get a stock help string
143 m_help
= wxGetStockHelpString(GetId());
148 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
150 return wxStripMenuCodes(text
);
154 #if WXWIN_COMPATIBILITY_2_8
155 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
157 return GetLabelText(text
);
161 bool wxMenuBase::ms_locked
= true;
163 // ----------------------------------------------------------------------------
164 // wxMenu ctor and dtor
165 // ----------------------------------------------------------------------------
167 void wxMenuBase::Init(long style
)
172 m_invokingWindow
= NULL
;
175 m_eventHandler
= this;
178 wxMenuBase::~wxMenuBase()
180 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
183 // ----------------------------------------------------------------------------
184 // wxMenu item adding/removing
185 // ----------------------------------------------------------------------------
187 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
189 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
191 submenu
->SetParent((wxMenu
*)this);
194 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
196 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
198 m_items
.Append(item
);
199 item
->SetMenu((wxMenu
*)this);
200 if ( item
->IsSubMenu() )
202 AddSubMenu(item
->GetSubMenu());
208 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
210 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
212 if ( pos
== GetMenuItemCount() )
214 return DoAppend(item
);
218 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
219 wxT("invalid index in wxMenu::Insert") );
221 return DoInsert(pos
, item
);
225 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
227 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
229 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
230 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
232 m_items
.Insert(node
, item
);
233 item
->SetMenu((wxMenu
*)this);
234 if ( item
->IsSubMenu() )
236 AddSubMenu(item
->GetSubMenu());
242 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
244 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
246 return DoRemove(item
);
249 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
251 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
253 // if we get here, the item is valid or one of Remove() functions is broken
254 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
256 // we detach the item, but we do delete the list node (i.e. don't call
257 // DetachNode() here!)
260 // item isn't attached to anything any more
262 wxMenu
*submenu
= item
->GetSubMenu();
265 submenu
->SetParent(NULL
);
266 if ( submenu
->IsAttached() )
273 bool wxMenuBase::Delete(wxMenuItem
*item
)
275 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
277 return DoDelete(item
);
280 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
282 wxMenuItem
*item2
= DoRemove(item
);
283 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
285 // don't delete the submenu
286 item2
->SetSubMenu(NULL
);
293 bool wxMenuBase::Destroy(wxMenuItem
*item
)
295 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
297 return DoDestroy(item
);
300 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
302 wxMenuItem
*item2
= DoRemove(item
);
303 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
310 // ----------------------------------------------------------------------------
311 // wxMenu searching for items
312 // ----------------------------------------------------------------------------
314 // Finds the item id matching the given string, wxNOT_FOUND if not found.
315 int wxMenuBase::FindItem(const wxString
& text
) const
317 wxString label
= wxMenuItem::GetLabelText(text
);
318 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
320 node
= node
->GetNext() )
322 wxMenuItem
*item
= node
->GetData();
323 if ( item
->IsSubMenu() )
325 int rc
= item
->GetSubMenu()->FindItem(label
);
326 if ( rc
!= wxNOT_FOUND
)
330 // we execute this code for submenus as well to alllow finding them by
331 // name just like the ordinary items
332 if ( !item
->IsSeparator() )
334 if ( item
->GetItemLabelText() == label
)
335 return item
->GetId();
342 // recursive search for item by id
343 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
348 wxMenuItem
*item
= NULL
;
349 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
351 node
= node
->GetNext() )
353 item
= node
->GetData();
355 if ( item
->GetId() == itemId
)
358 *itemMenu
= (wxMenu
*)this;
360 else if ( item
->IsSubMenu() )
362 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
366 // don't exit the loop
374 // non recursive search
375 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
377 wxMenuItem
*item
= NULL
;
378 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
381 for ( pos
= 0; node
; pos
++ )
383 if ( node
->GetData()->GetId() == id
)
385 item
= node
->GetData();
390 node
= node
->GetNext();
395 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
402 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
404 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
405 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
407 return m_items
.Item( position
)->GetData();
410 // ----------------------------------------------------------------------------
411 // wxMenu helpers used by derived classes
412 // ----------------------------------------------------------------------------
414 // Update a menu and all submenus recursively. source is the object that has
415 // the update event handlers defined for it. If NULL, the menu or associated
416 // window will be used.
417 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
419 if (GetInvokingWindow())
421 // Don't update menus if the parent
422 // frame is about to get deleted
423 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
424 if (tlw
&& wxPendingDelete
.Member(tlw
))
428 if ( !source
&& GetInvokingWindow() )
429 source
= GetInvokingWindow()->GetEventHandler();
431 source
= GetEventHandler();
435 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
438 wxMenuItem
* item
= node
->GetData();
439 if ( !item
->IsSeparator() )
441 wxWindowID id
= item
->GetId();
442 wxUpdateUIEvent
event(id
);
443 event
.SetEventObject( source
);
445 if ( source
->ProcessEvent(event
) )
447 // if anything changed, update the changed attribute
448 if (event
.GetSetText())
449 SetLabel(id
, event
.GetText());
450 if (event
.GetSetChecked())
451 Check(id
, event
.GetChecked());
452 if (event
.GetSetEnabled())
453 Enable(id
, event
.GetEnabled());
456 // recurse to the submenus
457 if ( item
->GetSubMenu() )
458 item
->GetSubMenu()->UpdateUI(source
);
460 //else: item is a separator (which doesn't process update UI events)
462 node
= node
->GetNext();
466 bool wxMenuBase::SendEvent(int id
, int checked
)
468 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
469 event
.SetEventObject(this);
470 event
.SetInt(checked
);
472 bool processed
= false;
474 // Try the menu's event handler first
475 wxEvtHandler
*handler
= GetEventHandler();
477 processed
= handler
->SafelyProcessEvent(event
);
479 // Try the window the menu was popped up from or its menu bar belongs to
482 wxWindow
* const win
= GetWindow();
484 processed
= win
->HandleWindowEvent(event
);
490 // ----------------------------------------------------------------------------
491 // wxMenu attaching/detaching to/from menu bar
492 // ----------------------------------------------------------------------------
494 wxMenuBar
* wxMenuBase::GetMenuBar() const
497 return GetParent()->GetMenuBar();
501 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
503 // use Detach() instead!
504 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
506 // use IsAttached() to prevent this from happening
507 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
509 m_menuBar
= (wxMenuBar
*)menubar
;
512 void wxMenuBase::Detach()
514 // use IsAttached() to prevent this from happening
515 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
520 // ----------------------------------------------------------------------------
521 // wxMenu invoking window handling
522 // ----------------------------------------------------------------------------
524 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
526 wxASSERT_MSG( !GetParent(),
527 "should only be called for top level popup menus" );
528 wxASSERT_MSG( !IsAttached(),
529 "menus attached to menu bar can't have invoking window" );
531 m_invokingWindow
= win
;
534 wxWindow
*wxMenuBase::GetWindow() const
536 // only the top level menus have non-NULL invoking window or a pointer to
537 // the menu bar so recurse upwards until we find it
538 const wxMenuBase
*menu
= this;
539 while ( menu
->GetParent() )
541 menu
= menu
->GetParent();
544 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
545 : menu
->GetInvokingWindow();
548 // ----------------------------------------------------------------------------
549 // wxMenu functions forwarded to wxMenuItem
550 // ----------------------------------------------------------------------------
552 void wxMenuBase::Enable( int id
, bool enable
)
554 wxMenuItem
*item
= FindItem(id
);
556 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
558 item
->Enable(enable
);
561 bool wxMenuBase::IsEnabled( int id
) const
563 wxMenuItem
*item
= FindItem(id
);
565 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
567 return item
->IsEnabled();
570 void wxMenuBase::Check( int id
, bool enable
)
572 wxMenuItem
*item
= FindItem(id
);
574 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
579 bool wxMenuBase::IsChecked( int id
) const
581 wxMenuItem
*item
= FindItem(id
);
583 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
585 return item
->IsChecked();
588 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
590 wxMenuItem
*item
= FindItem(id
);
592 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
594 item
->SetItemLabel(label
);
597 wxString
wxMenuBase::GetLabel( int id
) const
599 wxMenuItem
*item
= FindItem(id
);
601 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
603 return item
->GetItemLabel();
606 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
608 wxMenuItem
*item
= FindItem(id
);
610 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
612 item
->SetHelp( helpString
);
615 wxString
wxMenuBase::GetHelpString( int id
) const
617 wxMenuItem
*item
= FindItem(id
);
619 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
621 return item
->GetHelp();
624 // ----------------------------------------------------------------------------
625 // wxMenuBarBase ctor and dtor
626 // ----------------------------------------------------------------------------
628 wxMenuBarBase::wxMenuBarBase()
631 m_menuBarFrame
= NULL
;
634 wxMenuBarBase::~wxMenuBarBase()
636 WX_CLEAR_LIST(wxMenuList
, m_menus
);
639 // ----------------------------------------------------------------------------
640 // wxMenuBar item access: the base class versions manage m_menus list, the
641 // derived class should reflect the changes in the real menubar
642 // ----------------------------------------------------------------------------
644 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
646 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
647 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
649 return node
->GetData();
652 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
654 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
655 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
657 m_menus
.Append(menu
);
663 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
664 const wxString
& title
)
666 if ( pos
== m_menus
.GetCount() )
668 return wxMenuBarBase::Append(menu
, title
);
670 else // not at the end
672 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
674 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
675 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
677 m_menus
.Insert(node
, menu
);
684 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
685 const wxString
& WXUNUSED(title
))
687 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
689 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
690 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
692 wxMenu
*menuOld
= node
->GetData();
701 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
703 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
704 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
706 wxMenu
*menu
= node
->GetData();
713 int wxMenuBarBase::FindMenu(const wxString
& title
) const
715 wxString label
= wxMenuItem::GetLabelText(title
);
717 size_t count
= GetMenuCount();
718 for ( size_t i
= 0; i
< count
; i
++ )
720 wxString title2
= GetMenuLabel(i
);
721 if ( (title2
== title
) ||
722 (wxMenuItem::GetLabelText(title2
) == label
) )
733 // ----------------------------------------------------------------------------
734 // wxMenuBar attaching/detaching to/from the frame
735 // ----------------------------------------------------------------------------
737 void wxMenuBarBase::Attach(wxFrame
*frame
)
739 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
741 m_menuBarFrame
= frame
;
744 void wxMenuBarBase::Detach()
746 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
748 m_menuBarFrame
= NULL
;
751 // ----------------------------------------------------------------------------
752 // wxMenuBar searching for items
753 // ----------------------------------------------------------------------------
755 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
760 wxMenuItem
*item
= NULL
;
761 size_t count
= GetMenuCount(), i
;
762 wxMenuList::const_iterator it
;
763 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
765 item
= (*it
)->FindItem(id
, menu
);
771 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
773 wxString label
= wxMenuItem::GetLabelText(menu
);
776 wxMenuList::compatibility_iterator node
;
777 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
779 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
780 return node
->GetData()->FindItem(item
);
786 // ---------------------------------------------------------------------------
787 // wxMenuBar functions forwarded to wxMenuItem
788 // ---------------------------------------------------------------------------
790 void wxMenuBarBase::Enable(int id
, bool enable
)
792 wxMenuItem
*item
= FindItem(id
);
794 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
796 item
->Enable(enable
);
799 void wxMenuBarBase::Check(int id
, bool check
)
801 wxMenuItem
*item
= FindItem(id
);
803 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
804 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
809 bool wxMenuBarBase::IsChecked(int id
) const
811 wxMenuItem
*item
= FindItem(id
);
813 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
815 return item
->IsChecked();
818 bool wxMenuBarBase::IsEnabled(int id
) const
820 wxMenuItem
*item
= FindItem(id
);
822 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
824 return item
->IsEnabled();
827 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
829 wxMenuItem
*item
= FindItem(id
);
831 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
833 item
->SetItemLabel(label
);
836 wxString
wxMenuBarBase::GetLabel(int id
) const
838 wxMenuItem
*item
= FindItem(id
);
840 wxCHECK_MSG( item
, wxEmptyString
,
841 wxT("wxMenuBar::GetLabel(): no such item") );
843 return item
->GetItemLabel();
846 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
848 wxMenuItem
*item
= FindItem(id
);
850 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
852 item
->SetHelp(helpString
);
855 wxString
wxMenuBarBase::GetHelpString(int id
) const
857 wxMenuItem
*item
= FindItem(id
);
859 wxCHECK_MSG( item
, wxEmptyString
,
860 wxT("wxMenuBar::GetHelpString(): no such item") );
862 return item
->GetHelp();
865 void wxMenuBarBase::UpdateMenus()
867 wxEvtHandler
* source
;
869 int nCount
= GetMenuCount();
870 for (int n
= 0; n
< nCount
; n
++)
875 source
= menu
->GetEventHandler();
877 menu
->UpdateUI( source
);
882 #if WXWIN_COMPATIBILITY_2_8
883 // get or change the label of the menu at given position
884 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
886 SetMenuLabel(pos
, label
);
889 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
891 return GetMenuLabelText(pos
);
895 #endif // wxUSE_MENUS