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 // ids are limited to 16 bits under MSW so portable code shouldn't
80 // use ids outside of this range (negative ids generated by wx are
82 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
83 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
84 wxS("invalid id value") );
88 // notice that parentMenu can be NULL: the item can be attached to the menu
89 // later with SetMenu()
91 m_parentMenu
= parentMenu
;
102 wxMenuItemBase
::~wxMenuItemBase()
109 wxAcceleratorEntry
*wxMenuItemBase
::GetAccel() const
111 return wxAcceleratorEntry
::Create(GetItemLabel());
114 void wxMenuItemBase
::SetAccel(wxAcceleratorEntry
*accel
)
116 wxString text
= m_text
.BeforeFirst(wxT('\t'));
120 text
+= accel
->ToString();
126 #endif // wxUSE_ACCEL
128 void wxMenuItemBase
::SetItemLabel(const wxString
& str
)
132 if ( m_text
.empty() && !IsSeparator() )
134 wxASSERT_MSG( wxIsStockID(GetId()),
135 wxT("A non-stock menu item with an empty label?") );
136 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
137 wxSTOCK_WITH_MNEMONIC
);
141 void wxMenuItemBase
::SetHelp(const wxString
& str
)
145 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
147 // get a stock help string
148 m_help
= wxGetStockHelpString(GetId());
153 wxString wxMenuItemBase
::GetLabelText(const wxString
& text
)
155 return wxStripMenuCodes(text
);
159 #if WXWIN_COMPATIBILITY_2_8
160 wxString wxMenuItemBase
::GetLabelFromText(const wxString
& text
)
162 return GetLabelText(text
);
166 bool wxMenuBase
::ms_locked
= true;
168 // ----------------------------------------------------------------------------
169 // wxMenu ctor and dtor
170 // ----------------------------------------------------------------------------
172 void wxMenuBase
::Init(long style
)
177 m_invokingWindow
= NULL
;
180 m_eventHandler
= this;
183 wxMenuBase
::~wxMenuBase()
185 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
188 // ----------------------------------------------------------------------------
189 // wxMenu item adding/removing
190 // ----------------------------------------------------------------------------
192 void wxMenuBase
::AddSubMenu(wxMenu
*submenu
)
194 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
196 submenu
->SetParent((wxMenu
*)this);
199 wxMenuItem
* wxMenuBase
::DoAppend(wxMenuItem
*item
)
201 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
203 m_items
.Append(item
);
204 item
->SetMenu((wxMenu
*)this);
205 if ( item
->IsSubMenu() )
207 AddSubMenu(item
->GetSubMenu());
213 wxMenuItem
* wxMenuBase
::Insert(size_t pos
, wxMenuItem
*item
)
215 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
217 if ( pos
== GetMenuItemCount() )
219 return DoAppend(item
);
223 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
224 wxT("invalid index in wxMenu::Insert") );
226 return DoInsert(pos
, item
);
230 wxMenuItem
* wxMenuBase
::DoInsert(size_t pos
, wxMenuItem
*item
)
232 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
234 wxMenuItemList
::compatibility_iterator node
= m_items
.Item(pos
);
235 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
237 m_items
.Insert(node
, item
);
238 item
->SetMenu((wxMenu
*)this);
239 if ( item
->IsSubMenu() )
241 AddSubMenu(item
->GetSubMenu());
247 wxMenuItem
*wxMenuBase
::Remove(wxMenuItem
*item
)
249 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
251 return DoRemove(item
);
254 wxMenuItem
*wxMenuBase
::DoRemove(wxMenuItem
*item
)
256 wxMenuItemList
::compatibility_iterator node
= m_items
.Find(item
);
258 // if we get here, the item is valid or one of Remove() functions is broken
259 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
261 // we detach the item, but we do delete the list node (i.e. don't call
262 // DetachNode() here!)
265 // item isn't attached to anything any more
267 wxMenu
*submenu
= item
->GetSubMenu();
270 submenu
->SetParent(NULL
);
271 if ( submenu
->IsAttached() )
278 bool wxMenuBase
::Delete(wxMenuItem
*item
)
280 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
282 return DoDelete(item
);
285 bool wxMenuBase
::DoDelete(wxMenuItem
*item
)
287 wxMenuItem
*item2
= DoRemove(item
);
288 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
290 // don't delete the submenu
291 item2
->SetSubMenu(NULL
);
298 bool wxMenuBase
::Destroy(wxMenuItem
*item
)
300 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
302 return DoDestroy(item
);
305 bool wxMenuBase
::DoDestroy(wxMenuItem
*item
)
307 wxMenuItem
*item2
= DoRemove(item
);
308 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
315 // ----------------------------------------------------------------------------
316 // wxMenu searching for items
317 // ----------------------------------------------------------------------------
319 // Finds the item id matching the given string, wxNOT_FOUND if not found.
320 int wxMenuBase
::FindItem(const wxString
& text
) const
322 wxString label
= wxMenuItem
::GetLabelText(text
);
323 for ( wxMenuItemList
::compatibility_iterator node
= m_items
.GetFirst();
325 node
= node
->GetNext() )
327 wxMenuItem
*item
= node
->GetData();
328 if ( item
->IsSubMenu() )
330 int rc
= item
->GetSubMenu()->FindItem(label
);
331 if ( rc
!= wxNOT_FOUND
)
335 // we execute this code for submenus as well to alllow finding them by
336 // name just like the ordinary items
337 if ( !item
->IsSeparator() )
339 if ( item
->GetItemLabelText() == label
)
340 return item
->GetId();
347 // recursive search for item by id
348 wxMenuItem
*wxMenuBase
::FindItem(int itemId
, wxMenu
**itemMenu
) const
353 wxMenuItem
*item
= NULL
;
354 for ( wxMenuItemList
::compatibility_iterator node
= m_items
.GetFirst();
356 node
= node
->GetNext() )
358 item
= node
->GetData();
360 if ( item
->GetId() == itemId
)
363 *itemMenu
= (wxMenu
*)this;
365 else if ( item
->IsSubMenu() )
367 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
371 // don't exit the loop
379 // non recursive search
380 wxMenuItem
*wxMenuBase
::FindChildItem(int id
, size_t *ppos
) const
382 wxMenuItem
*item
= NULL
;
383 wxMenuItemList
::compatibility_iterator node
= GetMenuItems().GetFirst();
386 for ( pos
= 0; node
; pos
++ )
388 if ( node
->GetData()->GetId() == id
)
390 item
= node
->GetData();
395 node
= node
->GetNext();
400 *ppos
= item ? pos
: (size_t)wxNOT_FOUND
;
407 wxMenuItem
* wxMenuBase
::FindItemByPosition(size_t position
) const
409 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
410 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
412 return m_items
.Item( position
)->GetData();
415 // ----------------------------------------------------------------------------
416 // wxMenu helpers used by derived classes
417 // ----------------------------------------------------------------------------
419 // Update a menu and all submenus recursively. source is the object that has
420 // the update event handlers defined for it. If NULL, the menu or associated
421 // window will be used.
422 void wxMenuBase
::UpdateUI(wxEvtHandler
* source
)
424 if (GetInvokingWindow())
426 // Don't update menus if the parent
427 // frame is about to get deleted
428 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
429 if (tlw
&& wxPendingDelete
.Member(tlw
))
433 if ( !source
&& GetInvokingWindow() )
434 source
= GetInvokingWindow()->GetEventHandler();
436 source
= GetEventHandler();
440 wxMenuItemList
::compatibility_iterator node
= GetMenuItems().GetFirst();
443 wxMenuItem
* item
= node
->GetData();
444 if ( !item
->IsSeparator() )
446 wxWindowID id
= item
->GetId();
447 wxUpdateUIEvent
event(id
);
448 event
.SetEventObject( source
);
450 if ( source
->ProcessEvent(event
) )
452 // if anything changed, update the changed attribute
453 if (event
.GetSetText())
454 SetLabel(id
, event
.GetText());
455 if (event
.GetSetChecked())
456 Check(id
, event
.GetChecked());
457 if (event
.GetSetEnabled())
458 Enable(id
, event
.GetEnabled());
461 // recurse to the submenus
462 if ( item
->GetSubMenu() )
463 item
->GetSubMenu()->UpdateUI(source
);
465 //else: item is a separator (which doesn't process update UI events)
467 node
= node
->GetNext();
471 bool wxMenuBase
::SendEvent(int id
, int checked
)
473 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
474 event
.SetEventObject(this);
475 event
.SetInt(checked
);
477 bool processed
= false;
479 // Try the menu's event handler first
480 wxEvtHandler
*handler
= GetEventHandler();
482 processed
= handler
->SafelyProcessEvent(event
);
484 // Try the window the menu was popped up from or its menu bar belongs to
487 wxWindow
* const win
= GetWindow();
489 processed
= win
->HandleWindowEvent(event
);
495 // ----------------------------------------------------------------------------
496 // wxMenu attaching/detaching to/from menu bar
497 // ----------------------------------------------------------------------------
499 wxMenuBar
* wxMenuBase
::GetMenuBar() const
502 return GetParent()->GetMenuBar();
506 void wxMenuBase
::Attach(wxMenuBarBase
*menubar
)
508 // use Detach() instead!
509 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
511 // use IsAttached() to prevent this from happening
512 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
514 m_menuBar
= (wxMenuBar
*)menubar
;
517 void wxMenuBase
::Detach()
519 // use IsAttached() to prevent this from happening
520 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
525 // ----------------------------------------------------------------------------
526 // wxMenu invoking window handling
527 // ----------------------------------------------------------------------------
529 void wxMenuBase
::SetInvokingWindow(wxWindow
*win
)
531 wxASSERT_MSG( !GetParent(),
532 "should only be called for top level popup menus" );
533 wxASSERT_MSG( !IsAttached(),
534 "menus attached to menu bar can't have invoking window" );
536 m_invokingWindow
= win
;
539 wxWindow
*wxMenuBase
::GetWindow() const
541 // only the top level menus have non-NULL invoking window or a pointer to
542 // the menu bar so recurse upwards until we find it
543 const wxMenuBase
*menu
= this;
544 while ( menu
->GetParent() )
546 menu
= menu
->GetParent();
549 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
550 : menu
->GetInvokingWindow();
553 // ----------------------------------------------------------------------------
554 // wxMenu functions forwarded to wxMenuItem
555 // ----------------------------------------------------------------------------
557 void wxMenuBase
::Enable( int id
, bool enable
)
559 wxMenuItem
*item
= FindItem(id
);
561 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
563 item
->Enable(enable
);
566 bool wxMenuBase
::IsEnabled( int id
) const
568 wxMenuItem
*item
= FindItem(id
);
570 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
572 return item
->IsEnabled();
575 void wxMenuBase
::Check( int id
, bool enable
)
577 wxMenuItem
*item
= FindItem(id
);
579 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
584 bool wxMenuBase
::IsChecked( int id
) const
586 wxMenuItem
*item
= FindItem(id
);
588 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
590 return item
->IsChecked();
593 void wxMenuBase
::SetLabel( int id
, const wxString
&label
)
595 wxMenuItem
*item
= FindItem(id
);
597 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
599 item
->SetItemLabel(label
);
602 wxString wxMenuBase
::GetLabel( int id
) const
604 wxMenuItem
*item
= FindItem(id
);
606 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
608 return item
->GetItemLabel();
611 void wxMenuBase
::SetHelpString( int id
, const wxString
& helpString
)
613 wxMenuItem
*item
= FindItem(id
);
615 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
617 item
->SetHelp( helpString
);
620 wxString wxMenuBase
::GetHelpString( int id
) const
622 wxMenuItem
*item
= FindItem(id
);
624 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
626 return item
->GetHelp();
629 // ----------------------------------------------------------------------------
630 // wxMenuBarBase ctor and dtor
631 // ----------------------------------------------------------------------------
633 wxMenuBarBase
::wxMenuBarBase()
636 m_menuBarFrame
= NULL
;
639 wxMenuBarBase
::~wxMenuBarBase()
641 WX_CLEAR_LIST(wxMenuList
, m_menus
);
644 // ----------------------------------------------------------------------------
645 // wxMenuBar item access: the base class versions manage m_menus list, the
646 // derived class should reflect the changes in the real menubar
647 // ----------------------------------------------------------------------------
649 wxMenu
*wxMenuBarBase
::GetMenu(size_t pos
) const
651 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
652 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
654 return node
->GetData();
657 bool wxMenuBarBase
::Append(wxMenu
*menu
, const wxString
& title
)
659 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
660 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
662 m_menus
.Append(menu
);
668 bool wxMenuBarBase
::Insert(size_t pos
, wxMenu
*menu
,
669 const wxString
& title
)
671 if ( pos
== m_menus
.GetCount() )
673 return wxMenuBarBase
::Append(menu
, title
);
675 else // not at the end
677 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
679 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
680 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
682 m_menus
.Insert(node
, menu
);
689 wxMenu
*wxMenuBarBase
::Replace(size_t pos
, wxMenu
*menu
,
690 const wxString
& WXUNUSED(title
))
692 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
694 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
695 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
697 wxMenu
*menuOld
= node
->GetData();
706 wxMenu
*wxMenuBarBase
::Remove(size_t pos
)
708 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
709 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
711 wxMenu
*menu
= node
->GetData();
718 int wxMenuBarBase
::FindMenu(const wxString
& title
) const
720 wxString label
= wxMenuItem
::GetLabelText(title
);
722 size_t count
= GetMenuCount();
723 for ( size_t i
= 0; i
< count
; i
++ )
725 wxString title2
= GetMenuLabel(i
);
726 if ( (title2
== title
) ||
727 (wxMenuItem
::GetLabelText(title2
) == label
) )
738 // ----------------------------------------------------------------------------
739 // wxMenuBar attaching/detaching to/from the frame
740 // ----------------------------------------------------------------------------
742 void wxMenuBarBase
::Attach(wxFrame
*frame
)
744 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
746 m_menuBarFrame
= frame
;
749 void wxMenuBarBase
::Detach()
751 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
753 m_menuBarFrame
= NULL
;
756 // ----------------------------------------------------------------------------
757 // wxMenuBar searching for items
758 // ----------------------------------------------------------------------------
760 wxMenuItem
*wxMenuBarBase
::FindItem(int id
, wxMenu
**menu
) const
765 wxMenuItem
*item
= NULL
;
766 size_t count
= GetMenuCount(), i
;
767 wxMenuList
::const_iterator it
;
768 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
770 item
= (*it
)->FindItem(id
, menu
);
776 int wxMenuBarBase
::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
778 wxString label
= wxMenuItem
::GetLabelText(menu
);
781 wxMenuList
::compatibility_iterator node
;
782 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
784 if ( label
== wxMenuItem
::GetLabelText(GetMenuLabel(i
)) )
785 return node
->GetData()->FindItem(item
);
791 // ---------------------------------------------------------------------------
792 // wxMenuBar functions forwarded to wxMenuItem
793 // ---------------------------------------------------------------------------
795 void wxMenuBarBase
::Enable(int id
, bool enable
)
797 wxMenuItem
*item
= FindItem(id
);
799 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
801 item
->Enable(enable
);
804 void wxMenuBarBase
::Check(int id
, bool check
)
806 wxMenuItem
*item
= FindItem(id
);
808 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
809 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
814 bool wxMenuBarBase
::IsChecked(int id
) const
816 wxMenuItem
*item
= FindItem(id
);
818 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
820 return item
->IsChecked();
823 bool wxMenuBarBase
::IsEnabled(int id
) const
825 wxMenuItem
*item
= FindItem(id
);
827 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
829 return item
->IsEnabled();
832 void wxMenuBarBase
::SetLabel(int id
, const wxString
& label
)
834 wxMenuItem
*item
= FindItem(id
);
836 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
838 item
->SetItemLabel(label
);
841 wxString wxMenuBarBase
::GetLabel(int id
) const
843 wxMenuItem
*item
= FindItem(id
);
845 wxCHECK_MSG( item
, wxEmptyString
,
846 wxT("wxMenuBar::GetLabel(): no such item") );
848 return item
->GetItemLabel();
851 void wxMenuBarBase
::SetHelpString(int id
, const wxString
& helpString
)
853 wxMenuItem
*item
= FindItem(id
);
855 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
857 item
->SetHelp(helpString
);
860 wxString wxMenuBarBase
::GetHelpString(int id
) const
862 wxMenuItem
*item
= FindItem(id
);
864 wxCHECK_MSG( item
, wxEmptyString
,
865 wxT("wxMenuBar::GetHelpString(): no such item") );
867 return item
->GetHelp();
870 void wxMenuBarBase
::UpdateMenus()
872 wxEvtHandler
* source
;
874 int nCount
= GetMenuCount();
875 for (int n
= 0; n
< nCount
; n
++)
880 source
= menu
->GetEventHandler();
882 menu
->UpdateUI( source
);
887 #if WXWIN_COMPATIBILITY_2_8
888 // get or change the label of the menu at given position
889 void wxMenuBarBase
::SetLabelTop(size_t pos
, const wxString
& label
)
891 SetMenuLabel(pos
, label
);
894 wxString wxMenuBarBase
::GetLabelTop(size_t pos
) const
896 return GetMenuLabelText(pos
);
900 #endif // wxUSE_MENUS