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
,
62 // notice that parentMenu can be NULL: the item can be attached to the menu
63 // later with SetMenu()
65 m_parentMenu
= parentMenu
;
72 m_id
= wxWindow::NewControlId();
73 if (m_id
== wxID_SEPARATOR
)
74 m_kind
= wxITEM_SEPARATOR
;
80 wxMenuItemBase::~wxMenuItemBase()
87 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
89 return wxAcceleratorEntry::Create(GetItemLabel());
92 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
94 wxString text
= m_text
.BeforeFirst(wxT('\t'));
98 text
+= accel
->ToString();
104 #endif // wxUSE_ACCEL
106 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
110 if ( m_text
.empty() && !IsSeparator() )
112 wxASSERT_MSG( wxIsStockID(GetId()),
113 wxT("A non-stock menu item with an empty label?") );
114 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
115 wxSTOCK_WITH_MNEMONIC
);
119 void wxMenuItemBase::SetHelp(const wxString
& str
)
123 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
125 // get a stock help string
126 m_help
= wxGetStockHelpString(GetId());
131 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
133 return wxStripMenuCodes(text
);
137 #if WXWIN_COMPATIBILITY_2_8
138 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
140 return GetLabelText(text
);
144 bool wxMenuBase::ms_locked
= true;
146 // ----------------------------------------------------------------------------
147 // wxMenu ctor and dtor
148 // ----------------------------------------------------------------------------
150 void wxMenuBase::Init(long style
)
155 m_invokingWindow
= NULL
;
158 m_eventHandler
= this;
161 wxMenuBase::~wxMenuBase()
163 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
166 // ----------------------------------------------------------------------------
167 // wxMenu item adding/removing
168 // ----------------------------------------------------------------------------
170 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
172 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
174 submenu
->SetParent((wxMenu
*)this);
177 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
179 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
181 m_items
.Append(item
);
182 item
->SetMenu((wxMenu
*)this);
183 if ( item
->IsSubMenu() )
185 AddSubMenu(item
->GetSubMenu());
191 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
193 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
195 if ( pos
== GetMenuItemCount() )
197 return DoAppend(item
);
201 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
202 wxT("invalid index in wxMenu::Insert") );
204 return DoInsert(pos
, item
);
208 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
210 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
212 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
213 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
215 m_items
.Insert(node
, item
);
216 item
->SetMenu((wxMenu
*)this);
217 if ( item
->IsSubMenu() )
219 AddSubMenu(item
->GetSubMenu());
225 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
227 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
229 return DoRemove(item
);
232 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
234 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
236 // if we get here, the item is valid or one of Remove() functions is broken
237 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
239 // we detach the item, but we do delete the list node (i.e. don't call
240 // DetachNode() here!)
243 // item isn't attached to anything any more
245 wxMenu
*submenu
= item
->GetSubMenu();
248 submenu
->SetParent(NULL
);
249 if ( submenu
->IsAttached() )
256 bool wxMenuBase::Delete(wxMenuItem
*item
)
258 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
260 return DoDelete(item
);
263 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
265 wxMenuItem
*item2
= DoRemove(item
);
266 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
268 // don't delete the submenu
269 item2
->SetSubMenu(NULL
);
276 bool wxMenuBase::Destroy(wxMenuItem
*item
)
278 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
280 return DoDestroy(item
);
283 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
285 wxMenuItem
*item2
= DoRemove(item
);
286 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
293 // ----------------------------------------------------------------------------
294 // wxMenu searching for items
295 // ----------------------------------------------------------------------------
297 // Finds the item id matching the given string, wxNOT_FOUND if not found.
298 int wxMenuBase::FindItem(const wxString
& text
) const
300 wxString label
= wxMenuItem::GetLabelText(text
);
301 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
303 node
= node
->GetNext() )
305 wxMenuItem
*item
= node
->GetData();
306 if ( item
->IsSubMenu() )
308 int rc
= item
->GetSubMenu()->FindItem(label
);
309 if ( rc
!= wxNOT_FOUND
)
313 // we execute this code for submenus as well to alllow finding them by
314 // name just like the ordinary items
315 if ( !item
->IsSeparator() )
317 if ( item
->GetItemLabelText() == label
)
318 return item
->GetId();
325 // recursive search for item by id
326 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
331 wxMenuItem
*item
= NULL
;
332 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
334 node
= node
->GetNext() )
336 item
= node
->GetData();
338 if ( item
->GetId() == itemId
)
341 *itemMenu
= (wxMenu
*)this;
343 else if ( item
->IsSubMenu() )
345 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
349 // don't exit the loop
357 // non recursive search
358 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
360 wxMenuItem
*item
= NULL
;
361 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
364 for ( pos
= 0; node
; pos
++ )
366 if ( node
->GetData()->GetId() == id
)
368 item
= node
->GetData();
373 node
= node
->GetNext();
378 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
385 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
387 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
388 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
390 return m_items
.Item( position
)->GetData();
393 // ----------------------------------------------------------------------------
394 // wxMenu helpers used by derived classes
395 // ----------------------------------------------------------------------------
397 // Update a menu and all submenus recursively. source is the object that has
398 // the update event handlers defined for it. If NULL, the menu or associated
399 // window will be used.
400 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
402 if (GetInvokingWindow())
404 // Don't update menus if the parent
405 // frame is about to get deleted
406 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
407 if (tlw
&& wxPendingDelete
.Member(tlw
))
411 if ( !source
&& GetInvokingWindow() )
412 source
= GetInvokingWindow()->GetEventHandler();
414 source
= GetEventHandler();
418 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
421 wxMenuItem
* item
= node
->GetData();
422 if ( !item
->IsSeparator() )
424 wxWindowID id
= item
->GetId();
425 wxUpdateUIEvent
event(id
);
426 event
.SetEventObject( source
);
428 if ( source
->ProcessEvent(event
) )
430 // if anything changed, update the changed attribute
431 if (event
.GetSetText())
432 SetLabel(id
, event
.GetText());
433 if (event
.GetSetChecked())
434 Check(id
, event
.GetChecked());
435 if (event
.GetSetEnabled())
436 Enable(id
, event
.GetEnabled());
439 // recurse to the submenus
440 if ( item
->GetSubMenu() )
441 item
->GetSubMenu()->UpdateUI(source
);
443 //else: item is a separator (which doesn't process update UI events)
445 node
= node
->GetNext();
449 bool wxMenuBase::SendEvent(int id
, int checked
)
451 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
452 event
.SetEventObject(this);
453 event
.SetInt(checked
);
455 bool processed
= false;
457 // Try the menu's event handler first
458 wxEvtHandler
*handler
= GetEventHandler();
460 processed
= handler
->SafelyProcessEvent(event
);
462 // Try the window the menu was popped up from or its menu bar belongs to
465 wxWindow
* const win
= GetWindow();
467 processed
= win
->HandleWindowEvent(event
);
473 // ----------------------------------------------------------------------------
474 // wxMenu attaching/detaching to/from menu bar
475 // ----------------------------------------------------------------------------
477 wxMenuBar
* wxMenuBase::GetMenuBar() const
480 return GetParent()->GetMenuBar();
484 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
486 // use Detach() instead!
487 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
489 // use IsAttached() to prevent this from happening
490 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
492 m_menuBar
= (wxMenuBar
*)menubar
;
495 void wxMenuBase::Detach()
497 // use IsAttached() to prevent this from happening
498 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
503 // ----------------------------------------------------------------------------
504 // wxMenu invoking window handling
505 // ----------------------------------------------------------------------------
507 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
509 wxASSERT_MSG( !GetParent(),
510 "should only be called for top level popup menus" );
511 wxASSERT_MSG( !IsAttached(),
512 "menus attached to menu bar can't have invoking window" );
514 m_invokingWindow
= win
;
517 wxWindow
*wxMenuBase::GetWindow() const
519 // only the top level menus have non-NULL invoking window or a pointer to
520 // the menu bar so recurse upwards until we find it
521 const wxMenuBase
*menu
= this;
522 while ( menu
->GetParent() )
524 menu
= menu
->GetParent();
527 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
528 : menu
->GetInvokingWindow();
531 // ----------------------------------------------------------------------------
532 // wxMenu functions forwarded to wxMenuItem
533 // ----------------------------------------------------------------------------
535 void wxMenuBase::Enable( int id
, bool enable
)
537 wxMenuItem
*item
= FindItem(id
);
539 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
541 item
->Enable(enable
);
544 bool wxMenuBase::IsEnabled( int id
) const
546 wxMenuItem
*item
= FindItem(id
);
548 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
550 return item
->IsEnabled();
553 void wxMenuBase::Check( int id
, bool enable
)
555 wxMenuItem
*item
= FindItem(id
);
557 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
562 bool wxMenuBase::IsChecked( int id
) const
564 wxMenuItem
*item
= FindItem(id
);
566 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
568 return item
->IsChecked();
571 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
573 wxMenuItem
*item
= FindItem(id
);
575 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
577 item
->SetItemLabel(label
);
580 wxString
wxMenuBase::GetLabel( int id
) const
582 wxMenuItem
*item
= FindItem(id
);
584 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
586 return item
->GetItemLabel();
589 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
591 wxMenuItem
*item
= FindItem(id
);
593 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
595 item
->SetHelp( helpString
);
598 wxString
wxMenuBase::GetHelpString( int id
) const
600 wxMenuItem
*item
= FindItem(id
);
602 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
604 return item
->GetHelp();
607 // ----------------------------------------------------------------------------
608 // wxMenuBarBase ctor and dtor
609 // ----------------------------------------------------------------------------
611 wxMenuBarBase::wxMenuBarBase()
614 m_menuBarFrame
= NULL
;
617 wxMenuBarBase::~wxMenuBarBase()
619 WX_CLEAR_LIST(wxMenuList
, m_menus
);
622 // ----------------------------------------------------------------------------
623 // wxMenuBar item access: the base class versions manage m_menus list, the
624 // derived class should reflect the changes in the real menubar
625 // ----------------------------------------------------------------------------
627 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
629 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
630 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
632 return node
->GetData();
635 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
637 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
638 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
640 m_menus
.Append(menu
);
646 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
647 const wxString
& title
)
649 if ( pos
== m_menus
.GetCount() )
651 return wxMenuBarBase::Append(menu
, title
);
653 else // not at the end
655 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
657 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
658 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
660 m_menus
.Insert(node
, menu
);
667 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
668 const wxString
& WXUNUSED(title
))
670 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
672 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
673 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
675 wxMenu
*menuOld
= node
->GetData();
684 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
686 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
687 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
689 wxMenu
*menu
= node
->GetData();
696 int wxMenuBarBase::FindMenu(const wxString
& title
) const
698 wxString label
= wxMenuItem::GetLabelText(title
);
700 size_t count
= GetMenuCount();
701 for ( size_t i
= 0; i
< count
; i
++ )
703 wxString title2
= GetMenuLabel(i
);
704 if ( (title2
== title
) ||
705 (wxMenuItem::GetLabelText(title2
) == label
) )
716 // ----------------------------------------------------------------------------
717 // wxMenuBar attaching/detaching to/from the frame
718 // ----------------------------------------------------------------------------
720 void wxMenuBarBase::Attach(wxFrame
*frame
)
722 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
724 m_menuBarFrame
= frame
;
727 void wxMenuBarBase::Detach()
729 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
731 m_menuBarFrame
= NULL
;
734 // ----------------------------------------------------------------------------
735 // wxMenuBar searching for items
736 // ----------------------------------------------------------------------------
738 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
743 wxMenuItem
*item
= NULL
;
744 size_t count
= GetMenuCount(), i
;
745 wxMenuList::const_iterator it
;
746 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
748 item
= (*it
)->FindItem(id
, menu
);
754 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
756 wxString label
= wxMenuItem::GetLabelText(menu
);
759 wxMenuList::compatibility_iterator node
;
760 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
762 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
763 return node
->GetData()->FindItem(item
);
769 // ---------------------------------------------------------------------------
770 // wxMenuBar functions forwarded to wxMenuItem
771 // ---------------------------------------------------------------------------
773 void wxMenuBarBase::Enable(int id
, bool enable
)
775 wxMenuItem
*item
= FindItem(id
);
777 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
779 item
->Enable(enable
);
782 void wxMenuBarBase::Check(int id
, bool check
)
784 wxMenuItem
*item
= FindItem(id
);
786 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
787 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
792 bool wxMenuBarBase::IsChecked(int id
) const
794 wxMenuItem
*item
= FindItem(id
);
796 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
798 return item
->IsChecked();
801 bool wxMenuBarBase::IsEnabled(int id
) const
803 wxMenuItem
*item
= FindItem(id
);
805 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
807 return item
->IsEnabled();
810 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
812 wxMenuItem
*item
= FindItem(id
);
814 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
816 item
->SetItemLabel(label
);
819 wxString
wxMenuBarBase::GetLabel(int id
) const
821 wxMenuItem
*item
= FindItem(id
);
823 wxCHECK_MSG( item
, wxEmptyString
,
824 wxT("wxMenuBar::GetLabel(): no such item") );
826 return item
->GetItemLabel();
829 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
831 wxMenuItem
*item
= FindItem(id
);
833 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
835 item
->SetHelp(helpString
);
838 wxString
wxMenuBarBase::GetHelpString(int id
) const
840 wxMenuItem
*item
= FindItem(id
);
842 wxCHECK_MSG( item
, wxEmptyString
,
843 wxT("wxMenuBar::GetHelpString(): no such item") );
845 return item
->GetHelp();
848 void wxMenuBarBase::UpdateMenus()
850 wxEvtHandler
* source
;
852 int nCount
= GetMenuCount();
853 for (int n
= 0; n
< nCount
; n
++)
858 source
= menu
->GetEventHandler();
860 menu
->UpdateUI( source
);
865 #if WXWIN_COMPATIBILITY_2_8
866 // get or change the label of the menu at given position
867 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
869 SetMenuLabel(pos
, label
);
872 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
874 return GetMenuLabelText(pos
);
878 #endif // wxUSE_MENUS