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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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' && wxIsdigit(current
[1U]) &&
155 (current
.Len() == 2 ||
156 (current
.Len() == 3 && wxIsdigit(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_menuBar
= (wxMenuBar
*)NULL
;
280 m_menuParent
= (wxMenu
*)NULL
;
282 m_invokingWindow
= (wxWindow
*)NULL
;
284 m_clientData
= (void *)NULL
;
285 m_eventHandler
= this;
288 wxMenuBase::~wxMenuBase()
290 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
292 // Actually, in GTK, the submenus have to get deleted first.
295 // ----------------------------------------------------------------------------
296 // wxMenu item adding/removing
297 // ----------------------------------------------------------------------------
299 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
301 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
305 submenu
->Attach(m_menuBar
);
308 submenu
->SetParent((wxMenu
*)this);
311 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
313 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
315 m_items
.Append(item
);
316 item
->SetMenu((wxMenu
*)this);
317 if ( item
->IsSubMenu() )
319 AddSubMenu(item
->GetSubMenu());
325 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
327 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
329 if ( pos
== GetMenuItemCount() )
331 return DoAppend(item
);
335 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
336 wxT("invalid index in wxMenu::Insert") );
338 return DoInsert(pos
, item
);
342 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
344 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
346 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
347 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
349 m_items
.Insert(node
, item
);
350 item
->SetMenu((wxMenu
*)this);
351 if ( item
->IsSubMenu() )
353 AddSubMenu(item
->GetSubMenu());
359 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
361 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
363 return DoRemove(item
);
366 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
368 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
370 // if we get here, the item is valid or one of Remove() functions is broken
371 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
373 // we detach the item, but we do delete the list node (i.e. don't call
374 // DetachNode() here!)
377 // item isn't attached to anything any more
378 item
->SetMenu((wxMenu
*)NULL
);
379 wxMenu
*submenu
= item
->GetSubMenu();
382 submenu
->SetParent((wxMenu
*)NULL
);
388 bool wxMenuBase::Delete(wxMenuItem
*item
)
390 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
392 return DoDelete(item
);
395 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
397 wxMenuItem
*item2
= DoRemove(item
);
398 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
400 // don't delete the submenu
401 item2
->SetSubMenu((wxMenu
*)NULL
);
408 bool wxMenuBase::Destroy(wxMenuItem
*item
)
410 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
412 return DoDestroy(item
);
415 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
417 wxMenuItem
*item2
= DoRemove(item
);
418 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
425 // ----------------------------------------------------------------------------
426 // wxMenu searching for items
427 // ----------------------------------------------------------------------------
429 // Finds the item id matching the given string, -1 if not found.
430 int wxMenuBase::FindItem(const wxString
& text
) const
432 wxString label
= wxMenuItem::GetLabelFromText(text
);
433 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
435 node
= node
->GetNext() )
437 wxMenuItem
*item
= node
->GetData();
438 if ( item
->IsSubMenu() )
440 int rc
= item
->GetSubMenu()->FindItem(label
);
441 if ( rc
!= wxNOT_FOUND
)
445 // we execute this code for submenus as well to alllow finding them by
446 // name just like the ordinary items
447 if ( !item
->IsSeparator() )
449 if ( item
->GetLabel() == label
)
450 return item
->GetId();
457 // recursive search for item by id
458 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
463 wxMenuItem
*item
= NULL
;
464 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
466 node
= node
->GetNext() )
468 item
= node
->GetData();
470 if ( item
->GetId() == itemId
)
473 *itemMenu
= (wxMenu
*)this;
475 else if ( item
->IsSubMenu() )
477 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
481 // don't exit the loop
489 // non recursive search
490 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
492 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
493 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
496 for ( pos
= 0; node
; pos
++ )
498 if ( node
->GetData()->GetId() == id
)
500 item
= node
->GetData();
505 node
= node
->GetNext();
510 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
517 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
519 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
520 _T("wxMenu::FindItemByPosition(): invalid menu index") );
522 return m_items
.Item( position
)->GetData();
525 // ----------------------------------------------------------------------------
526 // wxMenu helpers used by derived classes
527 // ----------------------------------------------------------------------------
529 // Update a menu and all submenus recursively. source is the object that has
530 // the update event handlers defined for it. If NULL, the menu or associated
531 // window will be used.
532 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
534 if ( !source
&& GetInvokingWindow() )
535 source
= GetInvokingWindow()->GetEventHandler();
537 source
= GetEventHandler();
541 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
544 wxMenuItem
* item
= node
->GetData();
545 if ( !item
->IsSeparator() )
547 wxWindowID id
= item
->GetId();
548 wxUpdateUIEvent
event(id
);
549 event
.SetEventObject( source
);
551 if ( source
->ProcessEvent(event
) )
553 // if anything changed, update the changed attribute
554 if (event
.GetSetText())
555 SetLabel(id
, event
.GetText());
556 if (event
.GetSetChecked())
557 Check(id
, event
.GetChecked());
558 if (event
.GetSetEnabled())
559 Enable(id
, event
.GetEnabled());
562 // recurse to the submenus
563 if ( item
->GetSubMenu() )
564 item
->GetSubMenu()->UpdateUI(source
);
566 //else: item is a separator (which doesn't process update UI events)
568 node
= node
->GetNext();
572 bool wxMenuBase::SendEvent(int id
, int checked
)
574 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
575 event
.SetEventObject(this);
576 event
.SetInt(checked
);
578 bool processed
= FALSE
;
580 // Try the menu's event handler
583 wxEvtHandler
*handler
= GetEventHandler();
585 processed
= handler
->ProcessEvent(event
);
588 // Try the window the menu was popped up from (and up through the
592 const wxMenuBase
*menu
= this;
595 wxWindow
*win
= menu
->GetInvokingWindow();
598 processed
= win
->GetEventHandler()->ProcessEvent(event
);
602 menu
= menu
->GetParent();
609 // ----------------------------------------------------------------------------
610 // wxMenu attaching/detaching to/from menu bar
611 // ----------------------------------------------------------------------------
613 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
615 // use Detach() instead!
616 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
618 // use IsAttached() to prevent this from happening
619 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
621 m_menuBar
= (wxMenuBar
*)menubar
;
624 void wxMenuBase::Detach()
626 // use IsAttached() to prevent this from happening
627 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
632 // ----------------------------------------------------------------------------
633 // wxMenu functions forwarded to wxMenuItem
634 // ----------------------------------------------------------------------------
636 void wxMenuBase::Enable( int id
, bool enable
)
638 wxMenuItem
*item
= FindItem(id
);
640 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
642 item
->Enable(enable
);
645 bool wxMenuBase::IsEnabled( int id
) const
647 wxMenuItem
*item
= FindItem(id
);
649 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
651 return item
->IsEnabled();
654 void wxMenuBase::Check( int id
, bool enable
)
656 wxMenuItem
*item
= FindItem(id
);
658 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
663 bool wxMenuBase::IsChecked( int id
) const
665 wxMenuItem
*item
= FindItem(id
);
667 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
669 return item
->IsChecked();
672 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
674 wxMenuItem
*item
= FindItem(id
);
676 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
678 item
->SetText(label
);
681 wxString
wxMenuBase::GetLabel( int id
) const
683 wxMenuItem
*item
= FindItem(id
);
685 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
687 return item
->GetText();
690 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
692 wxMenuItem
*item
= FindItem(id
);
694 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
696 item
->SetHelp( helpString
);
699 wxString
wxMenuBase::GetHelpString( int id
) const
701 wxMenuItem
*item
= FindItem(id
);
703 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
705 return item
->GetHelp();
708 // ----------------------------------------------------------------------------
709 // wxMenuBarBase ctor and dtor
710 // ----------------------------------------------------------------------------
712 wxMenuBarBase::wxMenuBarBase()
715 m_menuBarFrame
= NULL
;
718 wxMenuBarBase::~wxMenuBarBase()
720 WX_CLEAR_LIST(wxMenuList
, m_menus
);
723 // ----------------------------------------------------------------------------
724 // wxMenuBar item access: the base class versions manage m_menus list, the
725 // derived class should reflect the changes in the real menubar
726 // ----------------------------------------------------------------------------
728 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
730 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
731 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
733 return node
->GetData();
736 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
738 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
740 m_menus
.Append(menu
);
746 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
747 const wxString
& title
)
749 if ( pos
== m_menus
.GetCount() )
751 return wxMenuBarBase::Append(menu
, title
);
753 else // not at the end
755 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
757 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
758 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
760 m_menus
.Insert(node
, menu
);
767 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
768 const wxString
& WXUNUSED(title
))
770 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
772 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
773 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
775 wxMenu
*menuOld
= node
->GetData();
784 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
786 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
787 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
789 wxMenu
*menu
= node
->GetData();
796 int wxMenuBarBase::FindMenu(const wxString
& title
) const
798 wxString label
= wxMenuItem::GetLabelFromText(title
);
800 size_t count
= GetMenuCount();
801 for ( size_t i
= 0; i
< count
; i
++ )
803 wxString title2
= GetLabelTop(i
);
804 if ( (title2
== title
) ||
805 (wxMenuItem::GetLabelFromText(title2
) == label
) )
816 // ----------------------------------------------------------------------------
817 // wxMenuBar attaching/detaching to/from the frame
818 // ----------------------------------------------------------------------------
820 void wxMenuBarBase::Attach(wxFrame
*frame
)
822 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
824 m_menuBarFrame
= frame
;
827 void wxMenuBarBase::Detach()
829 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
831 m_menuBarFrame
= NULL
;
834 // ----------------------------------------------------------------------------
835 // wxMenuBar searching for items
836 // ----------------------------------------------------------------------------
838 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
843 wxMenuItem
*item
= NULL
;
844 size_t count
= GetMenuCount(), i
;
845 wxMenuList::const_iterator it
;
846 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
848 item
= (*it
)->FindItem(id
, menu
);
854 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
856 wxString label
= wxMenuItem::GetLabelFromText(menu
);
859 wxMenuList::compatibility_iterator node
;
860 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
862 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
863 return node
->GetData()->FindItem(item
);
869 // ---------------------------------------------------------------------------
870 // wxMenuBar functions forwarded to wxMenuItem
871 // ---------------------------------------------------------------------------
873 void wxMenuBarBase::Enable(int id
, bool enable
)
875 wxMenuItem
*item
= FindItem(id
);
877 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
879 item
->Enable(enable
);
882 void wxMenuBarBase::Check(int id
, bool check
)
884 wxMenuItem
*item
= FindItem(id
);
886 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
887 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
892 bool wxMenuBarBase::IsChecked(int id
) const
894 wxMenuItem
*item
= FindItem(id
);
896 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
898 return item
->IsChecked();
901 bool wxMenuBarBase::IsEnabled(int id
) const
903 wxMenuItem
*item
= FindItem(id
);
905 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
907 return item
->IsEnabled();
910 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
912 wxMenuItem
*item
= FindItem(id
);
914 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
916 item
->SetText(label
);
919 wxString
wxMenuBarBase::GetLabel(int id
) const
921 wxMenuItem
*item
= FindItem(id
);
923 wxCHECK_MSG( item
, wxEmptyString
,
924 wxT("wxMenuBar::GetLabel(): no such item") );
926 return item
->GetText();
929 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
931 wxMenuItem
*item
= FindItem(id
);
933 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
935 item
->SetHelp(helpString
);
938 wxString
wxMenuBarBase::GetHelpString(int id
) const
940 wxMenuItem
*item
= FindItem(id
);
942 wxCHECK_MSG( item
, wxEmptyString
,
943 wxT("wxMenuBar::GetHelpString(): no such item") );
945 return item
->GetHelp();
948 #endif // wxUSE_MENUS