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 // (popup) menu titles in wxMSW use this ID to indicate that
80 // it's not a real menu item, so we don't want the check below to
86 // ids are limited to 16 bits under MSW so portable code shouldn't
87 // use ids outside of this range (negative ids generated by wx are
89 wxASSERT_MSG( (id
>= 0 && id
< SHRT_MAX
) ||
90 (id
>= wxID_AUTO_LOWEST
&& id
<= wxID_AUTO_HIGHEST
),
91 wxS("invalid id value") );
95 // notice that parentMenu can be NULL: the item can be attached to the menu
96 // later with SetMenu()
98 m_parentMenu
= parentMenu
;
109 wxMenuItemBase::~wxMenuItemBase()
116 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
118 return wxAcceleratorEntry::Create(GetItemLabel());
121 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
123 wxString text
= m_text
.BeforeFirst(wxT('\t'));
127 text
+= accel
->ToString();
133 #endif // wxUSE_ACCEL
135 void wxMenuItemBase::SetItemLabel(const wxString
& str
)
139 if ( m_text
.empty() && !IsSeparator() )
141 wxASSERT_MSG( wxIsStockID(GetId()),
142 wxT("A non-stock menu item with an empty label?") );
143 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
144 wxSTOCK_WITH_MNEMONIC
);
148 void wxMenuItemBase::SetHelp(const wxString
& str
)
152 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
154 // get a stock help string
155 m_help
= wxGetStockHelpString(GetId());
160 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
162 return wxStripMenuCodes(text
);
166 #if WXWIN_COMPATIBILITY_2_8
167 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
169 return GetLabelText(text
);
173 bool wxMenuBase::ms_locked
= true;
175 // ----------------------------------------------------------------------------
176 // wxMenu ctor and dtor
177 // ----------------------------------------------------------------------------
179 void wxMenuBase::Init(long style
)
184 m_invokingWindow
= NULL
;
187 m_eventHandler
= this;
190 wxMenuBase::~wxMenuBase()
192 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
195 // ----------------------------------------------------------------------------
196 // wxMenu item adding/removing
197 // ----------------------------------------------------------------------------
199 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
201 wxCHECK_RET( submenu
, wxT("can't add a NULL submenu") );
203 submenu
->SetParent((wxMenu
*)this);
206 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
208 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
210 m_items
.Append(item
);
211 item
->SetMenu((wxMenu
*)this);
212 if ( item
->IsSubMenu() )
214 AddSubMenu(item
->GetSubMenu());
220 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
222 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
224 if ( pos
== GetMenuItemCount() )
226 return DoAppend(item
);
230 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
231 wxT("invalid index in wxMenu::Insert") );
233 return DoInsert(pos
, item
);
237 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
239 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
241 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
242 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
244 m_items
.Insert(node
, item
);
245 item
->SetMenu((wxMenu
*)this);
246 if ( item
->IsSubMenu() )
248 AddSubMenu(item
->GetSubMenu());
254 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
256 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
258 return DoRemove(item
);
261 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
263 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
265 // if we get here, the item is valid or one of Remove() functions is broken
266 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
268 // we detach the item, but we do delete the list node (i.e. don't call
269 // DetachNode() here!)
272 // item isn't attached to anything any more
274 wxMenu
*submenu
= item
->GetSubMenu();
277 submenu
->SetParent(NULL
);
278 if ( submenu
->IsAttached() )
285 bool wxMenuBase::Delete(wxMenuItem
*item
)
287 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
289 return DoDelete(item
);
292 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
294 wxMenuItem
*item2
= DoRemove(item
);
295 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
297 // don't delete the submenu
298 item2
->SetSubMenu(NULL
);
305 bool wxMenuBase::Destroy(wxMenuItem
*item
)
307 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
309 return DoDestroy(item
);
312 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
314 wxMenuItem
*item2
= DoRemove(item
);
315 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
322 // ----------------------------------------------------------------------------
323 // wxMenu searching for items
324 // ----------------------------------------------------------------------------
326 // Finds the item id matching the given string, wxNOT_FOUND if not found.
327 int wxMenuBase::FindItem(const wxString
& text
) const
329 wxString label
= wxMenuItem::GetLabelText(text
);
330 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
332 node
= node
->GetNext() )
334 wxMenuItem
*item
= node
->GetData();
335 if ( item
->IsSubMenu() )
337 int rc
= item
->GetSubMenu()->FindItem(label
);
338 if ( rc
!= wxNOT_FOUND
)
342 // we execute this code for submenus as well to alllow finding them by
343 // name just like the ordinary items
344 if ( !item
->IsSeparator() )
346 if ( item
->GetItemLabelText() == label
)
347 return item
->GetId();
354 // recursive search for item by id
355 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
360 wxMenuItem
*item
= NULL
;
361 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
363 node
= node
->GetNext() )
365 item
= node
->GetData();
367 if ( item
->GetId() == itemId
)
370 *itemMenu
= (wxMenu
*)this;
372 else if ( item
->IsSubMenu() )
374 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
378 // don't exit the loop
386 // non recursive search
387 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
389 wxMenuItem
*item
= NULL
;
390 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
393 for ( pos
= 0; node
; pos
++ )
395 if ( node
->GetData()->GetId() == id
)
397 item
= node
->GetData();
402 node
= node
->GetNext();
407 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
414 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
416 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
417 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
419 return m_items
.Item( position
)->GetData();
422 // ----------------------------------------------------------------------------
423 // wxMenu helpers used by derived classes
424 // ----------------------------------------------------------------------------
426 // Update a menu and all submenus recursively. source is the object that has
427 // the update event handlers defined for it. If NULL, the menu or associated
428 // window will be used.
429 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
431 if (GetInvokingWindow())
433 // Don't update menus if the parent
434 // frame is about to get deleted
435 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
436 if (tlw
&& wxPendingDelete
.Member(tlw
))
440 if ( !source
&& GetInvokingWindow() )
441 source
= GetInvokingWindow()->GetEventHandler();
443 source
= GetEventHandler();
447 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
450 wxMenuItem
* item
= node
->GetData();
451 if ( !item
->IsSeparator() )
453 wxWindowID id
= item
->GetId();
454 wxUpdateUIEvent
event(id
);
455 event
.SetEventObject( source
);
457 if ( source
->ProcessEvent(event
) )
459 // if anything changed, update the changed attribute
460 if (event
.GetSetText())
461 SetLabel(id
, event
.GetText());
462 if (event
.GetSetChecked())
463 Check(id
, event
.GetChecked());
464 if (event
.GetSetEnabled())
465 Enable(id
, event
.GetEnabled());
468 // recurse to the submenus
469 if ( item
->GetSubMenu() )
470 item
->GetSubMenu()->UpdateUI(source
);
472 //else: item is a separator (which doesn't process update UI events)
474 node
= node
->GetNext();
478 bool wxMenuBase::SendEvent(int id
, int checked
)
480 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
481 event
.SetEventObject(this);
482 event
.SetInt(checked
);
484 bool processed
= false;
486 // Try the menu's event handler first
487 wxEvtHandler
*handler
= GetEventHandler();
489 processed
= handler
->SafelyProcessEvent(event
);
491 // Try the window the menu was popped up from or its menu bar belongs to
494 wxWindow
* const win
= GetWindow();
496 processed
= win
->HandleWindowEvent(event
);
502 // ----------------------------------------------------------------------------
503 // wxMenu attaching/detaching to/from menu bar
504 // ----------------------------------------------------------------------------
506 wxMenuBar
* wxMenuBase::GetMenuBar() const
509 return GetParent()->GetMenuBar();
513 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
515 // use Detach() instead!
516 wxASSERT_MSG( menubar
, wxT("menu can't be attached to NULL menubar") );
518 // use IsAttached() to prevent this from happening
519 wxASSERT_MSG( !m_menuBar
, wxT("attaching menu twice?") );
521 m_menuBar
= (wxMenuBar
*)menubar
;
524 void wxMenuBase::Detach()
526 // use IsAttached() to prevent this from happening
527 wxASSERT_MSG( m_menuBar
, wxT("detaching unattached menu?") );
532 // ----------------------------------------------------------------------------
533 // wxMenu invoking window handling
534 // ----------------------------------------------------------------------------
536 void wxMenuBase::SetInvokingWindow(wxWindow
*win
)
538 wxASSERT_MSG( !GetParent(),
539 "should only be called for top level popup menus" );
540 wxASSERT_MSG( !IsAttached(),
541 "menus attached to menu bar can't have invoking window" );
543 m_invokingWindow
= win
;
546 wxWindow
*wxMenuBase::GetWindow() const
548 // only the top level menus have non-NULL invoking window or a pointer to
549 // the menu bar so recurse upwards until we find it
550 const wxMenuBase
*menu
= this;
551 while ( menu
->GetParent() )
553 menu
= menu
->GetParent();
556 return menu
->GetMenuBar() ? menu
->GetMenuBar()->GetFrame()
557 : menu
->GetInvokingWindow();
560 // ----------------------------------------------------------------------------
561 // wxMenu functions forwarded to wxMenuItem
562 // ----------------------------------------------------------------------------
564 void wxMenuBase::Enable( int id
, bool enable
)
566 wxMenuItem
*item
= FindItem(id
);
568 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
570 item
->Enable(enable
);
573 bool wxMenuBase::IsEnabled( int id
) const
575 wxMenuItem
*item
= FindItem(id
);
577 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
579 return item
->IsEnabled();
582 void wxMenuBase::Check( int id
, bool enable
)
584 wxMenuItem
*item
= FindItem(id
);
586 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
591 bool wxMenuBase::IsChecked( int id
) const
593 wxMenuItem
*item
= FindItem(id
);
595 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
597 return item
->IsChecked();
600 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
602 wxMenuItem
*item
= FindItem(id
);
604 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
606 item
->SetItemLabel(label
);
609 wxString
wxMenuBase::GetLabel( int id
) const
611 wxMenuItem
*item
= FindItem(id
);
613 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
615 return item
->GetItemLabel();
618 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
620 wxMenuItem
*item
= FindItem(id
);
622 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
624 item
->SetHelp( helpString
);
627 wxString
wxMenuBase::GetHelpString( int id
) const
629 wxMenuItem
*item
= FindItem(id
);
631 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
633 return item
->GetHelp();
636 // ----------------------------------------------------------------------------
637 // wxMenuBarBase ctor and dtor
638 // ----------------------------------------------------------------------------
640 wxMenuBarBase::wxMenuBarBase()
643 m_menuBarFrame
= NULL
;
646 wxMenuBarBase::~wxMenuBarBase()
648 WX_CLEAR_LIST(wxMenuList
, m_menus
);
651 // ----------------------------------------------------------------------------
652 // wxMenuBar item access: the base class versions manage m_menus list, the
653 // derived class should reflect the changes in the real menubar
654 // ----------------------------------------------------------------------------
656 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
658 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
659 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
661 return node
->GetData();
664 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& title
)
666 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
667 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
669 m_menus
.Append(menu
);
675 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
676 const wxString
& title
)
678 if ( pos
== m_menus
.GetCount() )
680 return wxMenuBarBase::Append(menu
, title
);
682 else // not at the end
684 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
686 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
687 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
689 m_menus
.Insert(node
, menu
);
696 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
697 const wxString
& WXUNUSED(title
))
699 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
701 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
702 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
704 wxMenu
*menuOld
= node
->GetData();
713 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
715 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
716 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
718 wxMenu
*menu
= node
->GetData();
725 int wxMenuBarBase::FindMenu(const wxString
& title
) const
727 wxString label
= wxMenuItem::GetLabelText(title
);
729 size_t count
= GetMenuCount();
730 for ( size_t i
= 0; i
< count
; i
++ )
732 wxString title2
= GetMenuLabel(i
);
733 if ( (title2
== title
) ||
734 (wxMenuItem::GetLabelText(title2
) == label
) )
745 // ----------------------------------------------------------------------------
746 // wxMenuBar attaching/detaching to/from the frame
747 // ----------------------------------------------------------------------------
749 void wxMenuBarBase::Attach(wxFrame
*frame
)
751 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
753 m_menuBarFrame
= frame
;
756 void wxMenuBarBase::Detach()
758 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
760 m_menuBarFrame
= NULL
;
763 // ----------------------------------------------------------------------------
764 // wxMenuBar searching for items
765 // ----------------------------------------------------------------------------
767 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
772 wxMenuItem
*item
= NULL
;
773 size_t count
= GetMenuCount(), i
;
774 wxMenuList::const_iterator it
;
775 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
777 item
= (*it
)->FindItem(id
, menu
);
783 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
785 wxString label
= wxMenuItem::GetLabelText(menu
);
788 wxMenuList::compatibility_iterator node
;
789 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
791 if ( label
== wxMenuItem::GetLabelText(GetMenuLabel(i
)) )
792 return node
->GetData()->FindItem(item
);
798 // ---------------------------------------------------------------------------
799 // wxMenuBar functions forwarded to wxMenuItem
800 // ---------------------------------------------------------------------------
802 void wxMenuBarBase::Enable(int id
, bool enable
)
804 wxMenuItem
*item
= FindItem(id
);
806 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
808 item
->Enable(enable
);
811 void wxMenuBarBase::Check(int id
, bool check
)
813 wxMenuItem
*item
= FindItem(id
);
815 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
816 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
821 bool wxMenuBarBase::IsChecked(int id
) const
823 wxMenuItem
*item
= FindItem(id
);
825 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
827 return item
->IsChecked();
830 bool wxMenuBarBase::IsEnabled(int id
) const
832 wxMenuItem
*item
= FindItem(id
);
834 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
836 return item
->IsEnabled();
839 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
841 wxMenuItem
*item
= FindItem(id
);
843 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
845 item
->SetItemLabel(label
);
848 wxString
wxMenuBarBase::GetLabel(int id
) const
850 wxMenuItem
*item
= FindItem(id
);
852 wxCHECK_MSG( item
, wxEmptyString
,
853 wxT("wxMenuBar::GetLabel(): no such item") );
855 return item
->GetItemLabel();
858 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
860 wxMenuItem
*item
= FindItem(id
);
862 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
864 item
->SetHelp(helpString
);
867 wxString
wxMenuBarBase::GetHelpString(int id
) const
869 wxMenuItem
*item
= FindItem(id
);
871 wxCHECK_MSG( item
, wxEmptyString
,
872 wxT("wxMenuBar::GetHelpString(): no such item") );
874 return item
->GetHelp();
877 void wxMenuBarBase::UpdateMenus()
879 wxEvtHandler
* source
;
881 int nCount
= GetMenuCount();
882 for (int n
= 0; n
< nCount
; n
++)
887 source
= menu
->GetEventHandler();
889 menu
->UpdateUI( source
);
894 #if WXWIN_COMPATIBILITY_2_8
895 // get or change the label of the menu at given position
896 void wxMenuBarBase::SetLabelTop(size_t pos
, const wxString
& label
)
898 SetMenuLabel(pos
, label
);
901 wxString
wxMenuBarBase::GetLabelTop(size_t pos
) const
903 return GetMenuLabelText(pos
);
907 #endif // wxUSE_MENUS