1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/menucmn.cpp
3 // Purpose: wxMenu and wxMenuBar methods common to all ports
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "menubase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxMenuList
);
48 WX_DEFINE_LIST(wxMenuItemList
);
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
67 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
69 m_parentMenu
= parentMenu
;
77 wxMenuItemBase::~wxMenuItemBase()
84 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
87 return str
== accel
|| str
== wxGetTranslation(accel
);
93 // return wxAcceleratorEntry for the given menu string or NULL if none
95 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
97 // check for accelerators: they are given after '\t'
98 int posTab
= label
.Find(wxT('\t'));
99 if ( posTab
!= wxNOT_FOUND
) {
100 // parse the accelerator string
102 int accelFlags
= wxACCEL_NORMAL
;
104 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.Len(); n
++ ) {
105 if ( (label
[n
] == '+') || (label
[n
] == '-') ) {
106 if ( CompareAccelString(current
, wxTRANSLATE("ctrl")) )
107 accelFlags
|= wxACCEL_CTRL
;
108 else if ( CompareAccelString(current
, wxTRANSLATE("alt")) )
109 accelFlags
|= wxACCEL_ALT
;
110 else if ( CompareAccelString(current
, wxTRANSLATE("shift")) )
111 accelFlags
|= wxACCEL_SHIFT
;
113 // we may have "Ctrl-+", for example, but we still want to
114 // catch typos like "Crtl-A" so only give the warning if we
115 // have something before the current '+' or '-', else take
116 // it as a literal symbol
117 if ( current
.empty() )
121 // skip clearing it below
126 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
134 current
+= wxTolower(label
[n
]);
138 if ( current
.IsEmpty() ) {
139 wxLogDebug(wxT("No accel key found, accel string ignored."));
142 if ( current
.Len() == 1 ) {
144 keyCode
= current
[0U];
146 // Only call wxToupper if control, alt, or shift is held down,
147 // otherwise lower case accelerators won't work.
148 if (accelFlags
!= wxACCEL_NORMAL
) {
149 keyCode
= wxToupper(keyCode
);
153 // is it a function key?
154 if ( current
[0U] == 'f' && isdigit(current
[1U]) &&
155 (current
.Len() == 2 ||
156 (current
.Len() == 3 && isdigit(current
[2U]))) ) {
158 wxSscanf(current
.c_str() + 1, wxT("%d"), &n
);
160 keyCode
= WXK_F1
+ n
- 1;
163 // several special cases
165 if ( current
== wxT("DEL") )
166 keyCode
= WXK_DELETE
;
167 else if ( current
== wxT("DELETE") )
168 keyCode
= WXK_DELETE
;
169 else if ( current
== wxT("INS") )
170 keyCode
= WXK_INSERT
;
171 else if ( current
== wxT("INSERT") )
172 keyCode
= WXK_INSERT
;
173 else if ( current
== wxT("ENTER") || current
== wxT("RETURN") )
174 keyCode
= WXK_RETURN
;
175 else if ( current
== wxT("PGUP") )
177 else if ( current
== wxT("PGDN") )
179 else if ( current
== wxT("LEFT") )
181 else if ( current
== wxT("RIGHT") )
183 else if ( current
== wxT("UP") )
185 else if ( current
== wxT("DOWN") )
187 else if ( current
== wxT("HOME") )
189 else if ( current
== wxT("END") )
191 else if ( current
== wxT("SPACE") )
193 else if ( current
== wxT("TAB") )
195 else if ( current
== wxT("ESC") || current
== wxT("ESCAPE") )
196 keyCode
= WXK_ESCAPE
;
199 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
207 // we do have something
208 return new wxAcceleratorEntry(accelFlags
, keyCode
);
212 return (wxAcceleratorEntry
*)NULL
;
215 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
217 return wxGetAccelFromString(GetText());
220 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
222 wxString text
= m_text
.BeforeFirst(wxT('\t'));
227 int flags
= accel
->GetFlags();
228 if ( flags
& wxACCEL_ALT
)
230 if ( flags
& wxACCEL_CTRL
)
231 text
+= wxT("Ctrl-");
232 if ( flags
& wxACCEL_SHIFT
)
233 text
+= wxT("Shift-");
235 int code
= accel
->GetKeyCode();
250 text
<< wxT('F') << code
- WXK_F1
+ 1;
253 // if there are any other keys wxGetAccelFromString() may return,
254 // we should process them here
257 if ( wxIsalnum(code
) )
259 text
<< (wxChar
)code
;
264 wxFAIL_MSG( wxT("unknown keyboard accel") );
271 #endif // wxUSE_ACCEL
273 // ----------------------------------------------------------------------------
274 // wxMenu ctor and dtor
275 // ----------------------------------------------------------------------------
277 void wxMenuBase::Init(long style
)
279 m_items
.DeleteContents(TRUE
);
281 m_menuBar
= (wxMenuBar
*)NULL
;
282 m_menuParent
= (wxMenu
*)NULL
;
284 m_invokingWindow
= (wxWindow
*)NULL
;
286 m_clientData
= (void *)NULL
;
287 m_eventHandler
= this;
289 #if wxUSE_MENU_CALLBACK
290 m_callback
= (wxFunction
) NULL
;
291 #endif // wxUSE_MENU_CALLBACK
294 wxMenuBase::~wxMenuBase()
296 // nothing to do, wxMenuItemList dtor will delete the menu items.
298 // Actually, in GTK, the submenus have to get deleted first.
301 // ----------------------------------------------------------------------------
302 // wxMenu item adding/removing
303 // ----------------------------------------------------------------------------
305 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
307 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
311 submenu
->Attach(m_menuBar
);
314 submenu
->SetParent((wxMenu
*)this);
317 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
319 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
321 m_items
.Append(item
);
322 item
->SetMenu((wxMenu
*)this);
323 if ( item
->IsSubMenu() )
325 AddSubMenu(item
->GetSubMenu());
331 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
333 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
335 if ( pos
== GetMenuItemCount() )
337 return DoAppend(item
);
341 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
342 wxT("invalid index in wxMenu::Insert") );
344 return DoInsert(pos
, item
);
348 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
350 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
352 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
353 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
355 m_items
.Insert(node
, item
);
356 item
->SetMenu((wxMenu
*)this);
357 if ( item
->IsSubMenu() )
359 AddSubMenu(item
->GetSubMenu());
365 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
367 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
369 return DoRemove(item
);
372 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
374 wxMenuItemList::Node
*node
= m_items
.Find(item
);
376 // if we get here, the item is valid or one of Remove() functions is broken
377 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
379 // we detach the item, but we do delete the list node (i.e. don't call
380 // DetachNode() here!)
381 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
382 m_items
.DeleteNode(node
);
384 // item isn't attached to anything any more
385 item
->SetMenu((wxMenu
*)NULL
);
386 wxMenu
*submenu
= item
->GetSubMenu();
389 submenu
->SetParent((wxMenu
*)NULL
);
395 bool wxMenuBase::Delete(wxMenuItem
*item
)
397 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
399 return DoDelete(item
);
402 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
404 wxMenuItem
*item2
= DoRemove(item
);
405 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
407 // don't delete the submenu
408 item2
->SetSubMenu((wxMenu
*)NULL
);
415 bool wxMenuBase::Destroy(wxMenuItem
*item
)
417 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
419 return DoDestroy(item
);
422 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
424 wxMenuItem
*item2
= DoRemove(item
);
425 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
432 // ----------------------------------------------------------------------------
433 // wxMenu searching for items
434 // ----------------------------------------------------------------------------
436 // Finds the item id matching the given string, -1 if not found.
437 int wxMenuBase::FindItem(const wxString
& text
) const
439 wxString label
= wxMenuItem::GetLabelFromText(text
);
440 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
442 node
= node
->GetNext() )
444 wxMenuItem
*item
= node
->GetData();
445 if ( item
->IsSubMenu() )
447 int rc
= item
->GetSubMenu()->FindItem(label
);
448 if ( rc
!= wxNOT_FOUND
)
452 // we execute this code for submenus as well to alllow finding them by
453 // name just like the ordinary items
454 if ( !item
->IsSeparator() )
456 if ( item
->GetLabel() == label
)
457 return item
->GetId();
464 // recursive search for item by id
465 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
470 wxMenuItem
*item
= NULL
;
471 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
473 node
= node
->GetNext() )
475 item
= node
->GetData();
477 if ( item
->GetId() == itemId
)
480 *itemMenu
= (wxMenu
*)this;
482 else if ( item
->IsSubMenu() )
484 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
488 // don't exit the loop
496 // non recursive search
497 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
499 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
500 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
503 for ( pos
= 0; node
; pos
++ )
505 if ( node
->GetData()->GetId() == id
)
507 item
= node
->GetData();
512 node
= node
->GetNext();
517 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
523 // ----------------------------------------------------------------------------
524 // wxMenu helpers used by derived classes
525 // ----------------------------------------------------------------------------
527 // Update a menu and all submenus recursively. source is the object that has
528 // the update event handlers defined for it. If NULL, the menu or associated
529 // window will be used.
530 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
532 if ( !source
&& GetInvokingWindow() )
533 source
= GetInvokingWindow()->GetEventHandler();
535 source
= GetEventHandler();
539 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
542 wxMenuItem
* item
= node
->GetData();
543 if ( !item
->IsSeparator() )
545 wxWindowID id
= item
->GetId();
546 wxUpdateUIEvent
event(id
);
547 event
.SetEventObject( source
);
549 if ( source
->ProcessEvent(event
) )
551 // if anything changed, update the chanegd attribute
552 if (event
.GetSetText())
553 SetLabel(id
, event
.GetText());
554 if (event
.GetSetChecked())
555 Check(id
, event
.GetChecked());
556 if (event
.GetSetEnabled())
557 Enable(id
, event
.GetEnabled());
560 // recurse to the submenus
561 if ( item
->GetSubMenu() )
562 item
->GetSubMenu()->UpdateUI(source
);
564 //else: item is a separator (which don't process update UI events)
566 node
= node
->GetNext();
570 bool wxMenuBase::SendEvent(int id
, int checked
)
572 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
573 event
.SetEventObject(this);
574 event
.SetInt(checked
);
576 bool processed
= FALSE
;
578 #if wxUSE_MENU_CALLBACK
582 (void)(*(m_callback
))(*this, event
);
585 #endif // wxUSE_MENU_CALLBACK
587 // Try the menu's event handler
590 wxEvtHandler
*handler
= GetEventHandler();
592 processed
= handler
->ProcessEvent(event
);
595 // Try the window the menu was popped up from (and up through the
599 const wxMenuBase
*menu
= this;
602 wxWindow
*win
= menu
->GetInvokingWindow();
605 processed
= win
->GetEventHandler()->ProcessEvent(event
);
609 menu
= menu
->GetParent();
616 // ----------------------------------------------------------------------------
617 // wxMenu attaching/detaching to/from menu bar
618 // ----------------------------------------------------------------------------
620 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
622 // use Detach() instead!
623 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
625 // use IsAttached() to prevent this from happening
626 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
628 m_menuBar
= (wxMenuBar
*)menubar
;
631 void wxMenuBase::Detach()
633 // use IsAttached() to prevent this from happening
634 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
639 // ----------------------------------------------------------------------------
640 // wxMenu functions forwarded to wxMenuItem
641 // ----------------------------------------------------------------------------
643 void wxMenuBase::Enable( int id
, bool enable
)
645 wxMenuItem
*item
= FindItem(id
);
647 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
649 item
->Enable(enable
);
652 bool wxMenuBase::IsEnabled( int id
) const
654 wxMenuItem
*item
= FindItem(id
);
656 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
658 return item
->IsEnabled();
661 void wxMenuBase::Check( int id
, bool enable
)
663 wxMenuItem
*item
= FindItem(id
);
665 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
670 bool wxMenuBase::IsChecked( int id
) const
672 wxMenuItem
*item
= FindItem(id
);
674 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
676 return item
->IsChecked();
679 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
681 wxMenuItem
*item
= FindItem(id
);
683 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
685 item
->SetText(label
);
688 wxString
wxMenuBase::GetLabel( int id
) const
690 wxMenuItem
*item
= FindItem(id
);
692 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
694 return item
->GetText();
697 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
699 wxMenuItem
*item
= FindItem(id
);
701 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
703 item
->SetHelp( helpString
);
706 wxString
wxMenuBase::GetHelpString( int id
) const
708 wxMenuItem
*item
= FindItem(id
);
710 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
712 return item
->GetHelp();
715 // ----------------------------------------------------------------------------
716 // wxMenuBarBase ctor and dtor
717 // ----------------------------------------------------------------------------
719 wxMenuBarBase::wxMenuBarBase()
721 // we own the menus when we get them
722 m_menus
.DeleteContents(TRUE
);
725 m_menuBarFrame
= NULL
;
728 wxMenuBarBase::~wxMenuBarBase()
730 // nothing to do, the list will delete the menus because of the call to
731 // DeleteContents() above
734 // ----------------------------------------------------------------------------
735 // wxMenuBar item access: the base class versions manage m_menus list, the
736 // derived class should reflect the changes in the real menubar
737 // ----------------------------------------------------------------------------
739 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
741 wxMenuList::Node
*node
= m_menus
.Item(pos
);
742 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
744 return node
->GetData();
747 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
749 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
751 m_menus
.Append(menu
);
757 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
758 const wxString
& title
)
760 if ( pos
== m_menus
.GetCount() )
762 return wxMenuBarBase::Append(menu
, title
);
764 else // not at the end
766 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
768 wxMenuList::Node
*node
= m_menus
.Item(pos
);
769 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
771 m_menus
.Insert(node
, menu
);
778 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
779 const wxString
& WXUNUSED(title
))
781 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
783 wxMenuList::Node
*node
= m_menus
.Item(pos
);
784 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
786 wxMenu
*menuOld
= node
->GetData();
795 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
797 wxMenuList::Node
*node
= m_menus
.Item(pos
);
798 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
800 node
= m_menus
.DetachNode(node
);
801 wxCHECK( node
, NULL
); // unexpected
802 wxMenu
*menu
= node
->GetData();
810 int wxMenuBarBase::FindMenu(const wxString
& title
) const
812 wxString label
= wxMenuItem::GetLabelFromText(title
);
814 size_t count
= GetMenuCount();
815 for ( size_t i
= 0; i
< count
; i
++ )
817 wxString title2
= GetLabelTop(i
);
818 if ( (title2
== title
) ||
819 (wxMenuItem::GetLabelFromText(title2
) == label
) )
830 // ----------------------------------------------------------------------------
831 // wxMenuBar attaching/detaching to/from the frame
832 // ----------------------------------------------------------------------------
834 void wxMenuBarBase::Attach(wxFrame
*frame
)
836 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
838 m_menuBarFrame
= frame
;
841 void wxMenuBarBase::Detach()
843 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
845 m_menuBarFrame
= NULL
;
848 // ----------------------------------------------------------------------------
849 // wxMenuBar searching for items
850 // ----------------------------------------------------------------------------
852 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
857 wxMenuItem
*item
= NULL
;
858 size_t count
= GetMenuCount();
859 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
861 item
= m_menus
[i
]->FindItem(id
, menu
);
867 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
869 wxString label
= wxMenuItem::GetLabelFromText(menu
);
872 wxMenuList::Node
*node
;
873 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
875 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
876 return node
->GetData()->FindItem(item
);
882 // ---------------------------------------------------------------------------
883 // wxMenuBar functions forwarded to wxMenuItem
884 // ---------------------------------------------------------------------------
886 void wxMenuBarBase::Enable(int id
, bool enable
)
888 wxMenuItem
*item
= FindItem(id
);
890 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
892 item
->Enable(enable
);
895 void wxMenuBarBase::Check(int id
, bool check
)
897 wxMenuItem
*item
= FindItem(id
);
899 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
900 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
905 bool wxMenuBarBase::IsChecked(int id
) const
907 wxMenuItem
*item
= FindItem(id
);
909 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
911 return item
->IsChecked();
914 bool wxMenuBarBase::IsEnabled(int id
) const
916 wxMenuItem
*item
= FindItem(id
);
918 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
920 return item
->IsEnabled();
923 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
925 wxMenuItem
*item
= FindItem(id
);
927 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
929 item
->SetText(label
);
932 wxString
wxMenuBarBase::GetLabel(int id
) const
934 wxMenuItem
*item
= FindItem(id
);
936 wxCHECK_MSG( item
, wxEmptyString
,
937 wxT("wxMenuBar::GetLabel(): no such item") );
939 return item
->GetText();
942 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
944 wxMenuItem
*item
= FindItem(id
);
946 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
948 item
->SetHelp(helpString
);
951 wxString
wxMenuBarBase::GetHelpString(int id
) const
953 wxMenuItem
*item
= FindItem(id
);
955 wxCHECK_MSG( item
, wxEmptyString
,
956 wxT("wxMenuBar::GetHelpString(): no such item") );
958 return item
->GetHelp();
961 #endif // wxUSE_MENUS