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 // notice that parentMenu can be NULL: the item can be attached to the menu
62 // later with SetMenu()
64 m_parentMenu
= parentMenu
;
71 m_id
= wxWindow::NewControlId();
72 if (m_id
== wxID_SEPARATOR
)
73 m_kind
= wxITEM_SEPARATOR
;
79 wxMenuItemBase::~wxMenuItemBase()
86 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
88 return wxAcceleratorEntry::Create(GetItemLabel());
91 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
93 wxString text
= m_text
.BeforeFirst(wxT('\t'));
97 text
+= accel
->ToString();
103 #endif // wxUSE_ACCEL
105 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
109 if ( m_text
.empty() && !IsSeparator() )
111 wxASSERT_MSG( wxIsStockID(GetId()),
112 wxT("A non-stock menu item with an empty label?") );
113 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
114 wxSTOCK_WITH_MNEMONIC
);
118 void wxMenuItemBase::SetHelp(const wxString
& str
)
122 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
124 // get a stock help string
125 m_help
= wxGetStockHelpString(GetId());
130 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
132 return wxStripMenuCodes(text
);
136 #if WXWIN_COMPATIBILITY_2_8
137 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
139 return GetLabelText(text
);
143 bool wxMenuBase::ms_locked
= true;
145 // ----------------------------------------------------------------------------
146 // wxMenu ctor and dtor
147 // ----------------------------------------------------------------------------
149 void wxMenuBase::Init(long style
)
154 m_invokingWindow
= NULL
;
157 m_eventHandler
= this;
160 wxMenuBase::~wxMenuBase()
162 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
165 // ----------------------------------------------------------------------------
166 // wxMenu item adding/removing
167 // ----------------------------------------------------------------------------
169 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
171 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
173 submenu
->SetParent((wxMenu
*)this);
176 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
178 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
180 m_items
.Append(item
);
181 item
->SetMenu((wxMenu
*)this);
182 if ( item
->IsSubMenu() )
184 AddSubMenu(item
->GetSubMenu());
190 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
192 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
194 if ( pos
== GetMenuItemCount() )
196 return DoAppend(item
);
200 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
201 wxT("invalid index in wxMenu::Insert") );
203 return DoInsert(pos
, item
);
207 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
209 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
211 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
212 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
214 m_items
.Insert(node
, item
);
215 item
->SetMenu((wxMenu
*)this);
216 if ( item
->IsSubMenu() )
218 AddSubMenu(item
->GetSubMenu());
224 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
226 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
228 return DoRemove(item
);
231 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
233 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
235 // if we get here, the item is valid or one of Remove() functions is broken
236 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
238 // we detach the item, but we do delete the list node (i.e. don't call
239 // DetachNode() here!)
242 // item isn't attached to anything any more
244 wxMenu
*submenu
= item
->GetSubMenu();
247 submenu
->SetParent(NULL
);
248 if ( submenu
->IsAttached() )
255 bool wxMenuBase::Delete(wxMenuItem
*item
)
257 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
259 return DoDelete(item
);
262 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
264 wxMenuItem
*item2
= DoRemove(item
);
265 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
267 // don't delete the submenu
268 item2
->SetSubMenu(NULL
);
275 bool wxMenuBase::Destroy(wxMenuItem
*item
)
277 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
279 return DoDestroy(item
);
282 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
284 wxMenuItem
*item2
= DoRemove(item
);
285 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
292 // ----------------------------------------------------------------------------
293 // wxMenu searching for items
294 // ----------------------------------------------------------------------------
296 // Finds the item id matching the given string, wxNOT_FOUND if not found.
297 int wxMenuBase::FindItem(const wxString
& text
) const
299 wxString label
= wxMenuItem::GetLabelText(text
);
300 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
302 node
= node
->GetNext() )
304 wxMenuItem
*item
= node
->GetData();
305 if ( item
->IsSubMenu() )
307 int rc
= item
->GetSubMenu()->FindItem(label
);
308 if ( rc
!= wxNOT_FOUND
)
312 // we execute this code for submenus as well to alllow finding them by
313 // name just like the ordinary items
314 if ( !item
->IsSeparator() )
316 if ( item
->GetItemLabelText() == label
)
317 return item
->GetId();
324 // recursive search for item by id
325 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
330 wxMenuItem
*item
= NULL
;
331 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
333 node
= node
->GetNext() )
335 item
= node
->GetData();
337 if ( item
->GetId() == itemId
)
340 *itemMenu
= (wxMenu
*)this;
342 else if ( item
->IsSubMenu() )
344 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
348 // don't exit the loop
356 // non recursive search
357 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
359 wxMenuItem
*item
= NULL
;
360 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
363 for ( pos
= 0; node
; pos
++ )
365 if ( node
->GetData()->GetId() == id
)
367 item
= node
->GetData();
372 node
= node
->GetNext();
377 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
384 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
386 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
387 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
389 return m_items
.Item( position
)->GetData();
392 // ----------------------------------------------------------------------------
393 // wxMenu helpers used by derived classes
394 // ----------------------------------------------------------------------------
396 // Update a menu and all submenus recursively. source is the object that has
397 // the update event handlers defined for it. If NULL, the menu or associated
398 // window will be used.
399 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
401 if (GetInvokingWindow())
403 // Don't update menus if the parent
404 // frame is about to get deleted
405 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
406 if (tlw
&& wxPendingDelete
.Member(tlw
))
410 if ( !source
&& GetInvokingWindow() )
411 source
= GetInvokingWindow()->GetEventHandler();
413 source
= GetEventHandler();
417 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
420 wxMenuItem
* item
= node
->GetData();
421 if ( !item
->IsSeparator() )
423 wxWindowID id
= item
->GetId();
424 wxUpdateUIEvent
event(id
);
425 event
.SetEventObject( source
);
427 if ( source
->ProcessEvent(event
) )
429 // if anything changed, update the changed attribute
430 if (event
.GetSetText())
431 SetLabel(id
, event
.GetText());
432 if (event
.GetSetChecked())
433 Check(id
, event
.GetChecked());
434 if (event
.GetSetEnabled())
435 Enable(id
, event
.GetEnabled());
438 // recurse to the submenus
439 if ( item
->GetSubMenu() )
440 item
->GetSubMenu()->UpdateUI(source
);
442 //else: item is a separator (which doesn't process update UI events)
444 node
= node
->GetNext();
448 bool wxMenuBase::SendEvent(int id
, int checked
)
450 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
451 event
.SetEventObject(this);
452 event
.SetInt(checked
);
454 bool processed
= false;
456 // Try the menu's event handler
459 wxEvtHandler
*handler
= GetEventHandler();
461 processed
= handler
->SafelyProcessEvent(event
);
464 // Try the window the menu was popped up from (and up through the
468 const wxMenuBase
*menu
= this;
471 wxWindow
*win
= menu
->GetInvokingWindow();
474 processed
= win
->HandleWindowEvent(event
);
478 menu
= menu
->GetParent();
485 // ----------------------------------------------------------------------------
486 // wxMenu attaching/detaching to/from menu bar
487 // ----------------------------------------------------------------------------
489 wxMenuBar
* wxMenuBase::GetMenuBar() const
492 return GetParent()->GetMenuBar();
496 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
498 // use Detach() instead!
499 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
501 // use IsAttached() to prevent this from happening
502 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
504 m_menuBar
= (wxMenuBar
*)menubar
;
507 void wxMenuBase::Detach()
509 // use IsAttached() to prevent this from happening
510 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
515 // ----------------------------------------------------------------------------
516 // wxMenu functions forwarded to wxMenuItem
517 // ----------------------------------------------------------------------------
519 void wxMenuBase::Enable( int id
, bool enable
)
521 wxMenuItem
*item
= FindItem(id
);
523 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
525 item
->Enable(enable
);
528 bool wxMenuBase::IsEnabled( int id
) const
530 wxMenuItem
*item
= FindItem(id
);
532 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
534 return item
->IsEnabled();
537 void wxMenuBase::Check( int id
, bool enable
)
539 wxMenuItem
*item
= FindItem(id
);
541 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
546 bool wxMenuBase::IsChecked( int id
) const
548 wxMenuItem
*item
= FindItem(id
);
550 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
552 return item
->IsChecked();
555 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
557 wxMenuItem
*item
= FindItem(id
);
559 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
561 item
->SetItemLabel(label
);
564 wxString
wxMenuBase::GetLabel( int id
) const
566 wxMenuItem
*item
= FindItem(id
);
568 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
570 return item
->GetItemLabel();
573 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
575 wxMenuItem
*item
= FindItem(id
);
577 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
579 item
->SetHelp( helpString
);
582 wxString
wxMenuBase::GetHelpString( int id
) const
584 wxMenuItem
*item
= FindItem(id
);
586 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
588 return item
->GetHelp();
591 // ----------------------------------------------------------------------------
592 // wxMenuBarBase ctor and dtor
593 // ----------------------------------------------------------------------------
595 wxMenuBarBase::wxMenuBarBase()
598 m_menuBarFrame
= NULL
;
601 wxMenuBarBase::~wxMenuBarBase()
603 WX_CLEAR_LIST(wxMenuList
, m_menus
);
606 // ----------------------------------------------------------------------------
607 // wxMenuBar item access: the base class versions manage m_menus list, the
608 // derived class should reflect the changes in the real menubar
609 // ----------------------------------------------------------------------------
611 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
613 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
614 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
616 return node
->GetData();
619 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
621 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
622 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
624 m_menus
.Append(menu
);
630 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
631 const wxString
& title
)
633 if ( pos
== m_menus
.GetCount() )
635 return wxMenuBarBase::Append(menu
, title
);
637 else // not at the end
639 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
641 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
642 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
644 m_menus
.Insert(node
, menu
);
651 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
652 const wxString
& WXUNUSED(title
))
654 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
656 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
657 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
659 wxMenu
*menuOld
= node
->GetData();
668 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
670 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
671 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
673 wxMenu
*menu
= node
->GetData();
680 int wxMenuBarBase::FindMenu(const wxString
& title
) const
682 wxString label
= wxMenuItem::GetLabelText(title
);
684 size_t count
= GetMenuCount();
685 for ( size_t i
= 0; i
< count
; i
++ )
687 wxString title2
= GetMenuLabel(i
);
688 if ( (title2
== title
) ||
689 (wxMenuItem::GetLabelText(title2
) == label
) )
700 // ----------------------------------------------------------------------------
701 // wxMenuBar attaching/detaching to/from the frame
702 // ----------------------------------------------------------------------------
704 void wxMenuBarBase::Attach(wxFrame
*frame
)
706 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
708 m_menuBarFrame
= frame
;
711 void wxMenuBarBase::Detach()
713 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
715 m_menuBarFrame
= NULL
;
718 // ----------------------------------------------------------------------------
719 // wxMenuBar searching for items
720 // ----------------------------------------------------------------------------
722 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
727 wxMenuItem
*item
= NULL
;
728 size_t count
= GetMenuCount(), i
;
729 wxMenuList::const_iterator it
;
730 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
732 item
= (*it
)->FindItem(id
, menu
);
738 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
740 wxString label
= wxMenuItem::GetLabelText(menu
);
743 wxMenuList::compatibility_iterator node
;
744 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
746 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
747 return node
->GetData()->FindItem(item
);
753 // ---------------------------------------------------------------------------
754 // wxMenuBar functions forwarded to wxMenuItem
755 // ---------------------------------------------------------------------------
757 void wxMenuBarBase::Enable(int id
, bool enable
)
759 wxMenuItem
*item
= FindItem(id
);
761 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
763 item
->Enable(enable
);
766 void wxMenuBarBase::Check(int id
, bool check
)
768 wxMenuItem
*item
= FindItem(id
);
770 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
771 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
776 bool wxMenuBarBase::IsChecked(int id
) const
778 wxMenuItem
*item
= FindItem(id
);
780 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
782 return item
->IsChecked();
785 bool wxMenuBarBase::IsEnabled(int id
) const
787 wxMenuItem
*item
= FindItem(id
);
789 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
791 return item
->IsEnabled();
794 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
796 wxMenuItem
*item
= FindItem(id
);
798 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
800 item
->SetItemLabel(label
);
803 wxString
wxMenuBarBase::GetLabel(int id
) const
805 wxMenuItem
*item
= FindItem(id
);
807 wxCHECK_MSG( item
, wxEmptyString
,
808 wxT("wxMenuBar::GetLabel(): no such item") );
810 return item
->GetItemLabel();
813 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
815 wxMenuItem
*item
= FindItem(id
);
817 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
819 item
->SetHelp(helpString
);
822 wxString
wxMenuBarBase::GetHelpString(int id
) const
824 wxMenuItem
*item
= FindItem(id
);
826 wxCHECK_MSG( item
, wxEmptyString
,
827 wxT("wxMenuBar::GetHelpString(): no such item") );
829 return item
->GetHelp();
832 void wxMenuBarBase::UpdateMenus()
834 wxEvtHandler
* source
;
836 int nCount
= GetMenuCount();
837 for (int n
= 0; n
< nCount
; n
++)
842 source
= menu
->GetEventHandler();
844 menu
->UpdateUI( source
);
849 #if WXWIN_COMPATIBILITY_2_8
850 // get or change the label of the menu at given position
851 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
853 SetMenuLabel(pos
, label
);
856 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
858 return GetMenuLabelText(pos
);
862 #endif // wxUSE_MENUS