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 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
63 m_parentMenu
= parentMenu
;
70 m_id
= wxWindow::NewControlId();
71 if (m_id
== wxID_SEPARATOR
)
72 m_kind
= wxITEM_SEPARATOR
;
78 wxMenuItemBase::~wxMenuItemBase()
85 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
87 return wxAcceleratorEntry::Create(GetItemLabel());
90 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
92 wxString text
= m_text
.BeforeFirst(wxT('\t'));
96 text
+= accel
->ToString();
102 #endif // wxUSE_ACCEL
104 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
108 if ( m_text
.empty() && !IsSeparator() )
110 wxASSERT_MSG( wxIsStockID(GetId()),
111 wxT("A non-stock menu item with an empty label?") );
112 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
113 wxSTOCK_WITH_MNEMONIC
);
117 void wxMenuItemBase::SetHelp(const wxString
& str
)
121 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
123 // get a stock help string
124 m_help
= wxGetStockHelpString(GetId());
129 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
131 return wxStripMenuCodes(text
);
135 #if WXWIN_COMPATIBILITY_2_8
136 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
138 return GetLabelText(text
);
142 bool wxMenuBase::ms_locked
= true;
144 // ----------------------------------------------------------------------------
145 // wxMenu ctor and dtor
146 // ----------------------------------------------------------------------------
148 void wxMenuBase::Init(long style
)
150 m_menuBar
= (wxMenuBar
*)NULL
;
151 m_menuParent
= (wxMenu
*)NULL
;
153 m_invokingWindow
= (wxWindow
*)NULL
;
155 m_clientData
= (void *)NULL
;
156 m_eventHandler
= this;
159 wxMenuBase::~wxMenuBase()
161 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
164 // ----------------------------------------------------------------------------
165 // wxMenu item adding/removing
166 // ----------------------------------------------------------------------------
168 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
170 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
172 submenu
->SetParent((wxMenu
*)this);
175 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
177 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
179 m_items
.Append(item
);
180 item
->SetMenu((wxMenu
*)this);
181 if ( item
->IsSubMenu() )
183 AddSubMenu(item
->GetSubMenu());
189 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
191 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
193 if ( pos
== GetMenuItemCount() )
195 return DoAppend(item
);
199 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
200 wxT("invalid index in wxMenu::Insert") );
202 return DoInsert(pos
, item
);
206 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
208 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
210 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
211 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
213 m_items
.Insert(node
, item
);
214 item
->SetMenu((wxMenu
*)this);
215 if ( item
->IsSubMenu() )
217 AddSubMenu(item
->GetSubMenu());
223 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
225 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
227 return DoRemove(item
);
230 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
232 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
234 // if we get here, the item is valid or one of Remove() functions is broken
235 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
237 // we detach the item, but we do delete the list node (i.e. don't call
238 // DetachNode() here!)
241 // item isn't attached to anything any more
242 item
->SetMenu((wxMenu
*)NULL
);
243 wxMenu
*submenu
= item
->GetSubMenu();
246 submenu
->SetParent((wxMenu
*)NULL
);
247 if ( submenu
->IsAttached() )
254 bool wxMenuBase::Delete(wxMenuItem
*item
)
256 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
258 return DoDelete(item
);
261 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
263 wxMenuItem
*item2
= DoRemove(item
);
264 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
266 // don't delete the submenu
267 item2
->SetSubMenu((wxMenu
*)NULL
);
274 bool wxMenuBase::Destroy(wxMenuItem
*item
)
276 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
278 return DoDestroy(item
);
281 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
283 wxMenuItem
*item2
= DoRemove(item
);
284 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
291 // ----------------------------------------------------------------------------
292 // wxMenu searching for items
293 // ----------------------------------------------------------------------------
295 // Finds the item id matching the given string, wxNOT_FOUND if not found.
296 int wxMenuBase::FindItem(const wxString
& text
) const
298 wxString label
= wxMenuItem::GetLabelText(text
);
299 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
301 node
= node
->GetNext() )
303 wxMenuItem
*item
= node
->GetData();
304 if ( item
->IsSubMenu() )
306 int rc
= item
->GetSubMenu()->FindItem(label
);
307 if ( rc
!= wxNOT_FOUND
)
311 // we execute this code for submenus as well to alllow finding them by
312 // name just like the ordinary items
313 if ( !item
->IsSeparator() )
315 if ( item
->GetItemLabelText() == label
)
316 return item
->GetId();
323 // recursive search for item by id
324 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
329 wxMenuItem
*item
= NULL
;
330 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
332 node
= node
->GetNext() )
334 item
= node
->GetData();
336 if ( item
->GetId() == itemId
)
339 *itemMenu
= (wxMenu
*)this;
341 else if ( item
->IsSubMenu() )
343 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
347 // don't exit the loop
355 // non recursive search
356 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
358 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
359 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
362 for ( pos
= 0; node
; pos
++ )
364 if ( node
->GetData()->GetId() == id
)
366 item
= node
->GetData();
371 node
= node
->GetNext();
376 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
383 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
385 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
386 _T("wxMenu::FindItemByPosition(): invalid menu index") );
388 return m_items
.Item( position
)->GetData();
391 // ----------------------------------------------------------------------------
392 // wxMenu helpers used by derived classes
393 // ----------------------------------------------------------------------------
395 // Update a menu and all submenus recursively. source is the object that has
396 // the update event handlers defined for it. If NULL, the menu or associated
397 // window will be used.
398 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
400 if (GetInvokingWindow())
402 // Don't update menus if the parent
403 // frame is about to get deleted
404 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
405 if (tlw
&& wxPendingDelete
.Member(tlw
))
409 if ( !source
&& GetInvokingWindow() )
410 source
= GetInvokingWindow()->GetEventHandler();
412 source
= GetEventHandler();
416 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
419 wxMenuItem
* item
= node
->GetData();
420 if ( !item
->IsSeparator() )
422 wxWindowID id
= item
->GetId();
423 wxUpdateUIEvent
event(id
);
424 event
.SetEventObject( source
);
426 if ( source
->ProcessEvent(event
) )
428 // if anything changed, update the changed attribute
429 if (event
.GetSetText())
430 SetLabel(id
, event
.GetText());
431 if (event
.GetSetChecked())
432 Check(id
, event
.GetChecked());
433 if (event
.GetSetEnabled())
434 Enable(id
, event
.GetEnabled());
437 // recurse to the submenus
438 if ( item
->GetSubMenu() )
439 item
->GetSubMenu()->UpdateUI(source
);
441 //else: item is a separator (which doesn't process update UI events)
443 node
= node
->GetNext();
447 bool wxMenuBase::SendEvent(int id
, int checked
)
449 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
450 event
.SetEventObject(this);
451 event
.SetInt(checked
);
453 bool processed
= false;
455 // Try the menu's event handler
458 wxEvtHandler
*handler
= GetEventHandler();
460 processed
= handler
->SafelyProcessEvent(event
);
463 // Try the window the menu was popped up from (and up through the
467 const wxMenuBase
*menu
= this;
470 wxWindow
*win
= menu
->GetInvokingWindow();
473 processed
= win
->HandleWindowEvent(event
);
477 menu
= menu
->GetParent();
484 // ----------------------------------------------------------------------------
485 // wxMenu attaching/detaching to/from menu bar
486 // ----------------------------------------------------------------------------
488 wxMenuBar
* wxMenuBase::GetMenuBar() const
491 return GetParent()->GetMenuBar();
495 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
497 // use Detach() instead!
498 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
500 // use IsAttached() to prevent this from happening
501 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
503 m_menuBar
= (wxMenuBar
*)menubar
;
506 void wxMenuBase::Detach()
508 // use IsAttached() to prevent this from happening
509 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
514 // ----------------------------------------------------------------------------
515 // wxMenu functions forwarded to wxMenuItem
516 // ----------------------------------------------------------------------------
518 void wxMenuBase::Enable( int id
, bool enable
)
520 wxMenuItem
*item
= FindItem(id
);
522 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
524 item
->Enable(enable
);
527 bool wxMenuBase::IsEnabled( int id
) const
529 wxMenuItem
*item
= FindItem(id
);
531 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
533 return item
->IsEnabled();
536 void wxMenuBase::Check( int id
, bool enable
)
538 wxMenuItem
*item
= FindItem(id
);
540 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
545 bool wxMenuBase::IsChecked( int id
) const
547 wxMenuItem
*item
= FindItem(id
);
549 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
551 return item
->IsChecked();
554 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
556 wxMenuItem
*item
= FindItem(id
);
558 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
560 item
->SetItemLabel(label
);
563 wxString
wxMenuBase::GetLabel( int id
) const
565 wxMenuItem
*item
= FindItem(id
);
567 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
569 return item
->GetItemLabel();
572 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
574 wxMenuItem
*item
= FindItem(id
);
576 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
578 item
->SetHelp( helpString
);
581 wxString
wxMenuBase::GetHelpString( int id
) const
583 wxMenuItem
*item
= FindItem(id
);
585 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
587 return item
->GetHelp();
590 // ----------------------------------------------------------------------------
591 // wxMenuBarBase ctor and dtor
592 // ----------------------------------------------------------------------------
594 wxMenuBarBase::wxMenuBarBase()
597 m_menuBarFrame
= NULL
;
600 wxMenuBarBase::~wxMenuBarBase()
602 WX_CLEAR_LIST(wxMenuList
, m_menus
);
605 // ----------------------------------------------------------------------------
606 // wxMenuBar item access: the base class versions manage m_menus list, the
607 // derived class should reflect the changes in the real menubar
608 // ----------------------------------------------------------------------------
610 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
612 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
613 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
615 return node
->GetData();
618 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
620 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
622 m_menus
.Append(menu
);
628 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
629 const wxString
& title
)
631 if ( pos
== m_menus
.GetCount() )
633 return wxMenuBarBase::Append(menu
, title
);
635 else // not at the end
637 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
639 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
640 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
642 m_menus
.Insert(node
, menu
);
649 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
650 const wxString
& WXUNUSED(title
))
652 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
654 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
655 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
657 wxMenu
*menuOld
= node
->GetData();
666 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
668 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
669 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
671 wxMenu
*menu
= node
->GetData();
678 int wxMenuBarBase::FindMenu(const wxString
& title
) const
680 wxString label
= wxMenuItem::GetLabelText(title
);
682 size_t count
= GetMenuCount();
683 for ( size_t i
= 0; i
< count
; i
++ )
685 wxString title2
= GetMenuLabel(i
);
686 if ( (title2
== title
) ||
687 (wxMenuItem::GetLabelText(title2
) == label
) )
698 // ----------------------------------------------------------------------------
699 // wxMenuBar attaching/detaching to/from the frame
700 // ----------------------------------------------------------------------------
702 void wxMenuBarBase::Attach(wxFrame
*frame
)
704 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
706 m_menuBarFrame
= frame
;
709 void wxMenuBarBase::Detach()
711 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
713 m_menuBarFrame
= NULL
;
716 // ----------------------------------------------------------------------------
717 // wxMenuBar searching for items
718 // ----------------------------------------------------------------------------
720 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
725 wxMenuItem
*item
= NULL
;
726 size_t count
= GetMenuCount(), i
;
727 wxMenuList::const_iterator it
;
728 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
730 item
= (*it
)->FindItem(id
, menu
);
736 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
738 wxString label
= wxMenuItem::GetLabelText(menu
);
741 wxMenuList::compatibility_iterator node
;
742 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
744 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
745 return node
->GetData()->FindItem(item
);
751 // ---------------------------------------------------------------------------
752 // wxMenuBar functions forwarded to wxMenuItem
753 // ---------------------------------------------------------------------------
755 void wxMenuBarBase::Enable(int id
, bool enable
)
757 wxMenuItem
*item
= FindItem(id
);
759 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
761 item
->Enable(enable
);
764 void wxMenuBarBase::Check(int id
, bool check
)
766 wxMenuItem
*item
= FindItem(id
);
768 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
769 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
774 bool wxMenuBarBase::IsChecked(int id
) const
776 wxMenuItem
*item
= FindItem(id
);
778 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
780 return item
->IsChecked();
783 bool wxMenuBarBase::IsEnabled(int id
) const
785 wxMenuItem
*item
= FindItem(id
);
787 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
789 return item
->IsEnabled();
792 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
794 wxMenuItem
*item
= FindItem(id
);
796 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
798 item
->SetItemLabel(label
);
801 wxString
wxMenuBarBase::GetLabel(int id
) const
803 wxMenuItem
*item
= FindItem(id
);
805 wxCHECK_MSG( item
, wxEmptyString
,
806 wxT("wxMenuBar::GetLabel(): no such item") );
808 return item
->GetItemLabel();
811 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
813 wxMenuItem
*item
= FindItem(id
);
815 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
817 item
->SetHelp(helpString
);
820 wxString
wxMenuBarBase::GetHelpString(int id
) const
822 wxMenuItem
*item
= FindItem(id
);
824 wxCHECK_MSG( item
, wxEmptyString
,
825 wxT("wxMenuBar::GetHelpString(): no such item") );
827 return item
->GetHelp();
830 void wxMenuBarBase::UpdateMenus( void )
832 wxEvtHandler
* source
;
834 int nCount
= GetMenuCount();
835 for (int n
= 0; n
< nCount
; n
++)
840 source
= menu
->GetEventHandler();
842 menu
->UpdateUI( source
);
847 #if WXWIN_COMPATIBILITY_2_8
848 // get or change the label of the menu at given position
849 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
851 SetMenuLabel(pos
, label
);
854 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
856 return GetMenuLabelText(pos
);
860 #endif // wxUSE_MENUS