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;
290 wxMenuBase::~wxMenuBase()
292 // nothing to do, wxMenuItemList dtor will delete the menu items.
294 // Actually, in GTK, the submenus have to get deleted first.
297 // ----------------------------------------------------------------------------
298 // wxMenu item adding/removing
299 // ----------------------------------------------------------------------------
301 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
303 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
307 submenu
->Attach(m_menuBar
);
310 submenu
->SetParent((wxMenu
*)this);
313 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
315 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
317 m_items
.Append(item
);
318 item
->SetMenu((wxMenu
*)this);
319 if ( item
->IsSubMenu() )
321 AddSubMenu(item
->GetSubMenu());
327 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
329 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
331 if ( pos
== GetMenuItemCount() )
333 return DoAppend(item
);
337 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
338 wxT("invalid index in wxMenu::Insert") );
340 return DoInsert(pos
, item
);
344 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
346 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
348 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
349 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
351 m_items
.Insert(node
, item
);
352 item
->SetMenu((wxMenu
*)this);
353 if ( item
->IsSubMenu() )
355 AddSubMenu(item
->GetSubMenu());
361 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
363 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
365 return DoRemove(item
);
368 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
370 wxMenuItemList::Node
*node
= m_items
.Find(item
);
372 // if we get here, the item is valid or one of Remove() functions is broken
373 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
375 // we detach the item, but we do delete the list node (i.e. don't call
376 // DetachNode() here!)
377 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
378 m_items
.DeleteNode(node
);
380 // item isn't attached to anything any more
381 item
->SetMenu((wxMenu
*)NULL
);
382 wxMenu
*submenu
= item
->GetSubMenu();
385 submenu
->SetParent((wxMenu
*)NULL
);
391 bool wxMenuBase::Delete(wxMenuItem
*item
)
393 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
395 return DoDelete(item
);
398 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
400 wxMenuItem
*item2
= DoRemove(item
);
401 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
403 // don't delete the submenu
404 item2
->SetSubMenu((wxMenu
*)NULL
);
411 bool wxMenuBase::Destroy(wxMenuItem
*item
)
413 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
415 return DoDestroy(item
);
418 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
420 wxMenuItem
*item2
= DoRemove(item
);
421 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
428 // ----------------------------------------------------------------------------
429 // wxMenu searching for items
430 // ----------------------------------------------------------------------------
432 // Finds the item id matching the given string, -1 if not found.
433 int wxMenuBase::FindItem(const wxString
& text
) const
435 wxString label
= wxMenuItem::GetLabelFromText(text
);
436 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
438 node
= node
->GetNext() )
440 wxMenuItem
*item
= node
->GetData();
441 if ( item
->IsSubMenu() )
443 int rc
= item
->GetSubMenu()->FindItem(label
);
444 if ( rc
!= wxNOT_FOUND
)
448 // we execute this code for submenus as well to alllow finding them by
449 // name just like the ordinary items
450 if ( !item
->IsSeparator() )
452 if ( item
->GetLabel() == label
)
453 return item
->GetId();
460 // recursive search for item by id
461 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
466 wxMenuItem
*item
= NULL
;
467 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
469 node
= node
->GetNext() )
471 item
= node
->GetData();
473 if ( item
->GetId() == itemId
)
476 *itemMenu
= (wxMenu
*)this;
478 else if ( item
->IsSubMenu() )
480 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
484 // don't exit the loop
492 // non recursive search
493 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
495 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
496 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
499 for ( pos
= 0; node
; pos
++ )
501 if ( node
->GetData()->GetId() == id
)
503 item
= node
->GetData();
508 node
= node
->GetNext();
513 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
520 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
522 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
523 _T("wxMenu::FindItemByPosition(): invalid menu index") );
525 return m_items
.Item( position
)->GetData();
528 // ----------------------------------------------------------------------------
529 // wxMenu helpers used by derived classes
530 // ----------------------------------------------------------------------------
532 // Update a menu and all submenus recursively. source is the object that has
533 // the update event handlers defined for it. If NULL, the menu or associated
534 // window will be used.
535 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
537 if ( !source
&& GetInvokingWindow() )
538 source
= GetInvokingWindow()->GetEventHandler();
540 source
= GetEventHandler();
544 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
547 wxMenuItem
* item
= node
->GetData();
548 if ( !item
->IsSeparator() )
550 wxWindowID id
= item
->GetId();
551 wxUpdateUIEvent
event(id
);
552 event
.SetEventObject( source
);
554 if ( source
->ProcessEvent(event
) )
556 // if anything changed, update the changed attribute
557 if (event
.GetSetText())
558 SetLabel(id
, event
.GetText());
559 if (event
.GetSetChecked())
560 Check(id
, event
.GetChecked());
561 if (event
.GetSetEnabled())
562 Enable(id
, event
.GetEnabled());
565 // recurse to the submenus
566 if ( item
->GetSubMenu() )
567 item
->GetSubMenu()->UpdateUI(source
);
569 //else: item is a separator (which doesn't process update UI events)
571 node
= node
->GetNext();
575 bool wxMenuBase::SendEvent(int id
, int checked
)
577 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
578 event
.SetEventObject(this);
579 event
.SetInt(checked
);
581 bool processed
= FALSE
;
583 // Try the menu's event handler
586 wxEvtHandler
*handler
= GetEventHandler();
588 processed
= handler
->ProcessEvent(event
);
591 // Try the window the menu was popped up from (and up through the
595 const wxMenuBase
*menu
= this;
598 wxWindow
*win
= menu
->GetInvokingWindow();
601 processed
= win
->GetEventHandler()->ProcessEvent(event
);
605 menu
= menu
->GetParent();
612 // ----------------------------------------------------------------------------
613 // wxMenu attaching/detaching to/from menu bar
614 // ----------------------------------------------------------------------------
616 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
618 // use Detach() instead!
619 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
621 // use IsAttached() to prevent this from happening
622 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
624 m_menuBar
= (wxMenuBar
*)menubar
;
627 void wxMenuBase::Detach()
629 // use IsAttached() to prevent this from happening
630 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
635 // ----------------------------------------------------------------------------
636 // wxMenu functions forwarded to wxMenuItem
637 // ----------------------------------------------------------------------------
639 void wxMenuBase::Enable( int id
, bool enable
)
641 wxMenuItem
*item
= FindItem(id
);
643 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
645 item
->Enable(enable
);
648 bool wxMenuBase::IsEnabled( int id
) const
650 wxMenuItem
*item
= FindItem(id
);
652 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
654 return item
->IsEnabled();
657 void wxMenuBase::Check( int id
, bool enable
)
659 wxMenuItem
*item
= FindItem(id
);
661 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
666 bool wxMenuBase::IsChecked( int id
) const
668 wxMenuItem
*item
= FindItem(id
);
670 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
672 return item
->IsChecked();
675 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
677 wxMenuItem
*item
= FindItem(id
);
679 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
681 item
->SetText(label
);
684 wxString
wxMenuBase::GetLabel( int id
) const
686 wxMenuItem
*item
= FindItem(id
);
688 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
690 return item
->GetText();
693 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
695 wxMenuItem
*item
= FindItem(id
);
697 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
699 item
->SetHelp( helpString
);
702 wxString
wxMenuBase::GetHelpString( int id
) const
704 wxMenuItem
*item
= FindItem(id
);
706 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
708 return item
->GetHelp();
711 // ----------------------------------------------------------------------------
712 // wxMenuBarBase ctor and dtor
713 // ----------------------------------------------------------------------------
715 wxMenuBarBase::wxMenuBarBase()
717 // we own the menus when we get them
718 m_menus
.DeleteContents(TRUE
);
721 m_menuBarFrame
= NULL
;
724 wxMenuBarBase::~wxMenuBarBase()
726 // nothing to do, the list will delete the menus because of the call to
727 // DeleteContents() above
730 // ----------------------------------------------------------------------------
731 // wxMenuBar item access: the base class versions manage m_menus list, the
732 // derived class should reflect the changes in the real menubar
733 // ----------------------------------------------------------------------------
735 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
737 wxMenuList::Node
*node
= m_menus
.Item(pos
);
738 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
740 return node
->GetData();
743 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
745 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
747 m_menus
.Append(menu
);
753 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
754 const wxString
& title
)
756 if ( pos
== m_menus
.GetCount() )
758 return wxMenuBarBase::Append(menu
, title
);
760 else // not at the end
762 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
764 wxMenuList::Node
*node
= m_menus
.Item(pos
);
765 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
767 m_menus
.Insert(node
, menu
);
774 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
775 const wxString
& WXUNUSED(title
))
777 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
779 wxMenuList::Node
*node
= m_menus
.Item(pos
);
780 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
782 wxMenu
*menuOld
= node
->GetData();
791 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
793 wxMenuList::Node
*node
= m_menus
.Item(pos
);
794 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
796 node
= m_menus
.DetachNode(node
);
797 wxCHECK( node
, NULL
); // unexpected
798 wxMenu
*menu
= node
->GetData();
806 int wxMenuBarBase::FindMenu(const wxString
& title
) const
808 wxString label
= wxMenuItem::GetLabelFromText(title
);
810 size_t count
= GetMenuCount();
811 for ( size_t i
= 0; i
< count
; i
++ )
813 wxString title2
= GetLabelTop(i
);
814 if ( (title2
== title
) ||
815 (wxMenuItem::GetLabelFromText(title2
) == label
) )
826 // ----------------------------------------------------------------------------
827 // wxMenuBar attaching/detaching to/from the frame
828 // ----------------------------------------------------------------------------
830 void wxMenuBarBase::Attach(wxFrame
*frame
)
832 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
834 m_menuBarFrame
= frame
;
837 void wxMenuBarBase::Detach()
839 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
841 m_menuBarFrame
= NULL
;
844 // ----------------------------------------------------------------------------
845 // wxMenuBar searching for items
846 // ----------------------------------------------------------------------------
848 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
853 wxMenuItem
*item
= NULL
;
854 size_t count
= GetMenuCount();
855 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
857 item
= m_menus
[i
]->FindItem(id
, menu
);
863 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
865 wxString label
= wxMenuItem::GetLabelFromText(menu
);
868 wxMenuList::Node
*node
;
869 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
871 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
872 return node
->GetData()->FindItem(item
);
878 // ---------------------------------------------------------------------------
879 // wxMenuBar functions forwarded to wxMenuItem
880 // ---------------------------------------------------------------------------
882 void wxMenuBarBase::Enable(int id
, bool enable
)
884 wxMenuItem
*item
= FindItem(id
);
886 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
888 item
->Enable(enable
);
891 void wxMenuBarBase::Check(int id
, bool check
)
893 wxMenuItem
*item
= FindItem(id
);
895 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
896 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
901 bool wxMenuBarBase::IsChecked(int id
) const
903 wxMenuItem
*item
= FindItem(id
);
905 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
907 return item
->IsChecked();
910 bool wxMenuBarBase::IsEnabled(int id
) const
912 wxMenuItem
*item
= FindItem(id
);
914 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
916 return item
->IsEnabled();
919 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
921 wxMenuItem
*item
= FindItem(id
);
923 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
925 item
->SetText(label
);
928 wxString
wxMenuBarBase::GetLabel(int id
) const
930 wxMenuItem
*item
= FindItem(id
);
932 wxCHECK_MSG( item
, wxEmptyString
,
933 wxT("wxMenuBar::GetLabel(): no such item") );
935 return item
->GetText();
938 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
940 wxMenuItem
*item
= FindItem(id
);
942 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
944 item
->SetHelp(helpString
);
947 wxString
wxMenuBarBase::GetHelpString(int id
) const
949 wxMenuItem
*item
= FindItem(id
);
951 wxCHECK_MSG( item
, wxEmptyString
,
952 wxT("wxMenuBar::GetHelpString(): no such item") );
954 return item
->GetHelp();
957 #endif // wxUSE_MENUS