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
::GetInvokingWindow() const
519 // only the popup menu itself has a non-NULL invoking window so recurse
520 // upwards until we find it
521 const wxMenuBase
*menu
= this;
522 while ( menu
->GetParent() )
524 menu
= menu
->GetParent();
527 // menu is a top level menu here
528 return menu
->m_invokingWindow
;
531 wxWindow
*wxMenuBase
::GetWindow() const
533 return GetMenuBar() ?
GetMenuBar()->GetFrame() : GetInvokingWindow();
536 // ----------------------------------------------------------------------------
537 // wxMenu functions forwarded to wxMenuItem
538 // ----------------------------------------------------------------------------
540 void wxMenuBase
::Enable( int id
, bool enable
)
542 wxMenuItem
*item
= FindItem(id
);
544 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
546 item
->Enable(enable
);
549 bool wxMenuBase
::IsEnabled( int id
) const
551 wxMenuItem
*item
= FindItem(id
);
553 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
555 return item
->IsEnabled();
558 void wxMenuBase
::Check( int id
, bool enable
)
560 wxMenuItem
*item
= FindItem(id
);
562 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
567 bool wxMenuBase
::IsChecked( int id
) const
569 wxMenuItem
*item
= FindItem(id
);
571 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
573 return item
->IsChecked();
576 void wxMenuBase
::SetLabel( int id
, const wxString
&label
)
578 wxMenuItem
*item
= FindItem(id
);
580 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
582 item
->SetItemLabel(label
);
585 wxString wxMenuBase
::GetLabel( int id
) const
587 wxMenuItem
*item
= FindItem(id
);
589 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
591 return item
->GetItemLabel();
594 void wxMenuBase
::SetHelpString( int id
, const wxString
& helpString
)
596 wxMenuItem
*item
= FindItem(id
);
598 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
600 item
->SetHelp( helpString
);
603 wxString wxMenuBase
::GetHelpString( int id
) const
605 wxMenuItem
*item
= FindItem(id
);
607 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
609 return item
->GetHelp();
612 // ----------------------------------------------------------------------------
613 // wxMenuBarBase ctor and dtor
614 // ----------------------------------------------------------------------------
616 wxMenuBarBase
::wxMenuBarBase()
619 m_menuBarFrame
= NULL
;
622 wxMenuBarBase
::~wxMenuBarBase()
624 WX_CLEAR_LIST(wxMenuList
, m_menus
);
627 // ----------------------------------------------------------------------------
628 // wxMenuBar item access: the base class versions manage m_menus list, the
629 // derived class should reflect the changes in the real menubar
630 // ----------------------------------------------------------------------------
632 wxMenu
*wxMenuBarBase
::GetMenu(size_t pos
) const
634 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
635 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
637 return node
->GetData();
640 bool wxMenuBarBase
::Append(wxMenu
*menu
, const wxString
& title
)
642 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
643 wxCHECK_MSG( !title
.empty(), false, wxT("can't append menu with empty title") );
645 m_menus
.Append(menu
);
651 bool wxMenuBarBase
::Insert(size_t pos
, wxMenu
*menu
,
652 const wxString
& title
)
654 if ( pos
== m_menus
.GetCount() )
656 return wxMenuBarBase
::Append(menu
, title
);
658 else // not at the end
660 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
662 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
663 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
665 m_menus
.Insert(node
, menu
);
672 wxMenu
*wxMenuBarBase
::Replace(size_t pos
, wxMenu
*menu
,
673 const wxString
& WXUNUSED(title
))
675 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
677 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
678 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
680 wxMenu
*menuOld
= node
->GetData();
689 wxMenu
*wxMenuBarBase
::Remove(size_t pos
)
691 wxMenuList
::compatibility_iterator node
= m_menus
.Item(pos
);
692 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
694 wxMenu
*menu
= node
->GetData();
701 int wxMenuBarBase
::FindMenu(const wxString
& title
) const
703 wxString label
= wxMenuItem
::GetLabelText(title
);
705 size_t count
= GetMenuCount();
706 for ( size_t i
= 0; i
< count
; i
++ )
708 wxString title2
= GetMenuLabel(i
);
709 if ( (title2
== title
) ||
710 (wxMenuItem
::GetLabelText(title2
) == label
) )
721 // ----------------------------------------------------------------------------
722 // wxMenuBar attaching/detaching to/from the frame
723 // ----------------------------------------------------------------------------
725 void wxMenuBarBase
::Attach(wxFrame
*frame
)
727 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
729 m_menuBarFrame
= frame
;
732 void wxMenuBarBase
::Detach()
734 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
736 m_menuBarFrame
= NULL
;
739 // ----------------------------------------------------------------------------
740 // wxMenuBar searching for items
741 // ----------------------------------------------------------------------------
743 wxMenuItem
*wxMenuBarBase
::FindItem(int id
, wxMenu
**menu
) const
748 wxMenuItem
*item
= NULL
;
749 size_t count
= GetMenuCount(), i
;
750 wxMenuList
::const_iterator it
;
751 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
753 item
= (*it
)->FindItem(id
, menu
);
759 int wxMenuBarBase
::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
761 wxString label
= wxMenuItem
::GetLabelText(menu
);
764 wxMenuList
::compatibility_iterator node
;
765 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
767 if ( label
== wxMenuItem
::GetLabelText(GetMenuLabel(i
)) )
768 return node
->GetData()->FindItem(item
);
774 // ---------------------------------------------------------------------------
775 // wxMenuBar functions forwarded to wxMenuItem
776 // ---------------------------------------------------------------------------
778 void wxMenuBarBase
::Enable(int id
, bool enable
)
780 wxMenuItem
*item
= FindItem(id
);
782 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
784 item
->Enable(enable
);
787 void wxMenuBarBase
::Check(int id
, bool check
)
789 wxMenuItem
*item
= FindItem(id
);
791 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
792 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
797 bool wxMenuBarBase
::IsChecked(int id
) const
799 wxMenuItem
*item
= FindItem(id
);
801 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
803 return item
->IsChecked();
806 bool wxMenuBarBase
::IsEnabled(int id
) const
808 wxMenuItem
*item
= FindItem(id
);
810 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
812 return item
->IsEnabled();
815 void wxMenuBarBase
::SetLabel(int id
, const wxString
& label
)
817 wxMenuItem
*item
= FindItem(id
);
819 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
821 item
->SetItemLabel(label
);
824 wxString wxMenuBarBase
::GetLabel(int id
) const
826 wxMenuItem
*item
= FindItem(id
);
828 wxCHECK_MSG( item
, wxEmptyString
,
829 wxT("wxMenuBar::GetLabel(): no such item") );
831 return item
->GetItemLabel();
834 void wxMenuBarBase
::SetHelpString(int id
, const wxString
& helpString
)
836 wxMenuItem
*item
= FindItem(id
);
838 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
840 item
->SetHelp(helpString
);
843 wxString wxMenuBarBase
::GetHelpString(int id
) const
845 wxMenuItem
*item
= FindItem(id
);
847 wxCHECK_MSG( item
, wxEmptyString
,
848 wxT("wxMenuBar::GetHelpString(): no such item") );
850 return item
->GetHelp();
853 void wxMenuBarBase
::UpdateMenus()
855 wxEvtHandler
* source
;
857 int nCount
= GetMenuCount();
858 for (int n
= 0; n
< nCount
; n
++)
863 source
= menu
->GetEventHandler();
865 menu
->UpdateUI( source
);
870 #if WXWIN_COMPATIBILITY_2_8
871 // get or change the label of the menu at given position
872 void wxMenuBarBase
::SetLabelTop(size_t pos
, const wxString
& label
)
874 SetMenuLabel(pos
, label
);
877 wxString wxMenuBarBase
::GetLabelTop(size_t pos
) const
879 return GetMenuLabelText(pos
);
883 #endif // wxUSE_MENUS