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 // return wxAcceleratorEntry for the given menu string or NULL if none
86 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
88 // check for accelerators: they are given after '\t'
89 int posTab
= label
.Find(wxT('\t'));
90 if ( posTab
!= wxNOT_FOUND
) {
91 // parse the accelerator string
93 int accelFlags
= wxACCEL_NORMAL
;
95 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.Len(); n
++ ) {
96 if ( (label
[n
] == '+') || (label
[n
] == '-') ) {
97 if ( current
== _("ctrl") )
98 accelFlags
|= wxACCEL_CTRL
;
99 else if ( current
== _("alt") )
100 accelFlags
|= wxACCEL_ALT
;
101 else if ( current
== _("shift") )
102 accelFlags
|= wxACCEL_SHIFT
;
104 // we may have "Ctrl-+", for example, but we still want to
105 // catch typos like "Crtl-A" so only give the warning if we
106 // have something before the current '+' or '-', else take
107 // it as a literal symbol
108 if ( current
.empty() )
112 // skip clearing it below
117 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
125 current
+= wxTolower(label
[n
]);
129 if ( current
.IsEmpty() ) {
130 wxLogDebug(wxT("No accel key found, accel string ignored."));
133 if ( current
.Len() == 1 ) {
135 keyCode
= current
[0U];
137 // Only call wxToupper if control, alt, or shift is held down,
138 // otherwise lower case accelerators won't work.
139 if (accelFlags
!= wxACCEL_NORMAL
) {
140 keyCode
= wxToupper(keyCode
);
144 // is it a function key?
145 if ( current
[0U] == 'f' && isdigit(current
[1U]) &&
146 (current
.Len() == 2 ||
147 (current
.Len() == 3 && isdigit(current
[2U]))) ) {
149 wxSscanf(current
.c_str() + 1, wxT("%d"), &n
);
151 keyCode
= WXK_F1
+ n
- 1;
154 // several special cases
156 if ( current
== wxT("DEL") ) {
157 keyCode
= WXK_DELETE
;
159 else if ( current
== wxT("DELETE") ) {
160 keyCode
= WXK_DELETE
;
162 else if ( current
== wxT("INS") ) {
163 keyCode
= WXK_INSERT
;
165 else if ( current
== wxT("INSERT") ) {
166 keyCode
= WXK_INSERT
;
168 else if ( current
== wxT("ENTER") || current
== wxT("RETURN") ) {
169 keyCode
= WXK_RETURN
;
171 else if ( current
== wxT("PGUP") ) {
174 else if ( current
== wxT("PGDN") ) {
177 else if ( current
== wxT("LEFT") ) {
180 else if ( current
== wxT("RIGHT") ) {
183 else if ( current
== wxT("UP") ) {
186 else if ( current
== wxT("DOWN") ) {
189 else if ( current
== wxT("HOME") ) {
192 else if ( current
== wxT("END") ) {
195 else if ( current
== wxT("SPACE") ) {
198 else if ( current
== wxT("TAB") ) {
203 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
211 // we do have something
212 return new wxAcceleratorEntry(accelFlags
, keyCode
);
216 return (wxAcceleratorEntry
*)NULL
;
219 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
221 return wxGetAccelFromString(GetText());
224 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
226 wxString text
= m_text
.BeforeFirst(wxT('\t'));
231 int flags
= accel
->GetFlags();
232 if ( flags
& wxACCEL_ALT
)
234 if ( flags
& wxACCEL_CTRL
)
235 text
+= wxT("Ctrl-");
236 if ( flags
& wxACCEL_SHIFT
)
237 text
+= wxT("Shift-");
239 int code
= accel
->GetKeyCode();
254 text
<< wxT('F') << code
- WXK_F1
+ 1;
257 // if there are any other keys wxGetAccelFromString() may return,
258 // we should process them here
261 if ( wxIsalnum(code
) )
263 text
<< (wxChar
)code
;
268 wxFAIL_MSG( wxT("unknown keyboard accel") );
275 #endif // wxUSE_ACCEL
277 // ----------------------------------------------------------------------------
278 // wxMenu ctor and dtor
279 // ----------------------------------------------------------------------------
281 void wxMenuBase::Init(long style
)
283 m_items
.DeleteContents(TRUE
);
285 m_menuBar
= (wxMenuBar
*)NULL
;
286 m_menuParent
= (wxMenu
*)NULL
;
288 m_invokingWindow
= (wxWindow
*)NULL
;
290 m_clientData
= (void *)NULL
;
291 m_eventHandler
= this;
293 #if wxUSE_MENU_CALLBACK
294 m_callback
= (wxFunction
) NULL
;
295 #endif // wxUSE_MENU_CALLBACK
298 wxMenuBase::~wxMenuBase()
300 // nothing to do, wxMenuItemList dtor will delete the menu items.
302 // Actually, in GTK, the submenus have to get deleted first.
305 // ----------------------------------------------------------------------------
306 // wxMenu item adding/removing
307 // ----------------------------------------------------------------------------
309 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
311 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
315 submenu
->Attach(m_menuBar
);
318 submenu
->SetParent((wxMenu
*)this);
321 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
323 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
325 m_items
.Append(item
);
326 if ( item
->IsSubMenu() )
328 AddSubMenu(item
->GetSubMenu());
334 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
336 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
338 if ( pos
== GetMenuItemCount() )
340 return DoAppend(item
);
344 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
345 wxT("invalid index in wxMenu::Insert") );
347 return DoInsert(pos
, item
);
351 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
353 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
355 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
356 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
358 m_items
.Insert(node
, item
);
359 if ( item
->IsSubMenu() )
361 AddSubMenu(item
->GetSubMenu());
367 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
369 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
371 return DoRemove(item
);
374 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
376 wxMenuItemList::Node
*node
= m_items
.Find(item
);
378 // if we get here, the item is valid or one of Remove() functions is broken
379 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
381 // we detach the item, but we do delete the list node (i.e. don't call
382 // DetachNode() here!)
383 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
384 m_items
.DeleteNode(node
);
386 // item isn't attached to anything any more
387 wxMenu
*submenu
= item
->GetSubMenu();
390 submenu
->SetParent((wxMenu
*)NULL
);
396 bool wxMenuBase::Delete(wxMenuItem
*item
)
398 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
400 return DoDelete(item
);
403 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
405 wxMenuItem
*item2
= DoRemove(item
);
406 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
408 // don't delete the submenu
409 item2
->SetSubMenu((wxMenu
*)NULL
);
416 bool wxMenuBase::Destroy(wxMenuItem
*item
)
418 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
420 return DoDestroy(item
);
423 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
425 wxMenuItem
*item2
= DoRemove(item
);
426 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
433 // ----------------------------------------------------------------------------
434 // wxMenu searching for items
435 // ----------------------------------------------------------------------------
437 // Finds the item id matching the given string, -1 if not found.
438 int wxMenuBase::FindItem(const wxString
& text
) const
440 wxString label
= wxMenuItem::GetLabelFromText(text
);
441 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
443 node
= node
->GetNext() )
445 wxMenuItem
*item
= node
->GetData();
446 if ( item
->IsSubMenu() )
448 int rc
= item
->GetSubMenu()->FindItem(label
);
449 if ( rc
!= wxNOT_FOUND
)
453 // we execute this code for submenus as well to alllow finding them by
454 // name just like the ordinary items
455 if ( !item
->IsSeparator() )
457 if ( item
->GetLabel() == label
)
458 return item
->GetId();
465 // recursive search for item by id
466 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
471 wxMenuItem
*item
= NULL
;
472 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
474 node
= node
->GetNext() )
476 item
= node
->GetData();
478 if ( item
->GetId() == itemId
)
481 *itemMenu
= (wxMenu
*)this;
483 else if ( item
->IsSubMenu() )
485 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
489 // don't exit the loop
497 // non recursive search
498 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
500 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
501 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
504 for ( pos
= 0; node
; pos
++ )
506 if ( node
->GetData()->GetId() == id
)
508 item
= node
->GetData();
513 node
= node
->GetNext();
518 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
524 // ----------------------------------------------------------------------------
525 // wxMenu helpers used by derived classes
526 // ----------------------------------------------------------------------------
528 // Update a menu and all submenus recursively. source is the object that has
529 // the update event handlers defined for it. If NULL, the menu or associated
530 // window will be used.
531 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
533 if ( !source
&& GetInvokingWindow() )
534 source
= GetInvokingWindow()->GetEventHandler();
536 source
= GetEventHandler();
540 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
543 wxMenuItem
* item
= node
->GetData();
544 if ( !item
->IsSeparator() )
546 wxWindowID id
= item
->GetId();
547 wxUpdateUIEvent
event(id
);
548 event
.SetEventObject( source
);
550 if ( source
->ProcessEvent(event
) )
552 // if anything changed, update the chanegd attribute
553 if (event
.GetSetText())
554 SetLabel(id
, event
.GetText());
555 if (event
.GetSetChecked())
556 Check(id
, event
.GetChecked());
557 if (event
.GetSetEnabled())
558 Enable(id
, event
.GetEnabled());
561 // recurse to the submenus
562 if ( item
->GetSubMenu() )
563 item
->GetSubMenu()->UpdateUI(source
);
565 //else: item is a separator (which don't process update UI events)
567 node
= node
->GetNext();
571 bool wxMenuBase::SendEvent(int id
, int checked
)
573 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
574 event
.SetEventObject(this);
575 event
.SetInt(checked
);
577 bool processed
= FALSE
;
579 #if wxUSE_MENU_CALLBACK
583 (void)(*(m_callback
))(*this, event
);
586 #endif // wxUSE_MENU_CALLBACK
588 // Try the menu's event handler
591 wxEvtHandler
*handler
= GetEventHandler();
593 processed
= handler
->ProcessEvent(event
);
596 // Try the window the menu was popped up from (and up through the
600 const wxMenuBase
*menu
= this;
603 wxWindow
*win
= menu
->GetInvokingWindow();
606 processed
= win
->GetEventHandler()->ProcessEvent(event
);
610 menu
= menu
->GetParent();
617 // ----------------------------------------------------------------------------
618 // wxMenu attaching/detaching to/from menu bar
619 // ----------------------------------------------------------------------------
621 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
623 // use Detach() instead!
624 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
626 // use IsAttached() to prevent this from happening
627 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
629 m_menuBar
= (wxMenuBar
*)menubar
;
632 void wxMenuBase::Detach()
634 // use IsAttached() to prevent this from happening
635 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
640 // ----------------------------------------------------------------------------
641 // wxMenu functions forwarded to wxMenuItem
642 // ----------------------------------------------------------------------------
644 void wxMenuBase::Enable( int id
, bool enable
)
646 wxMenuItem
*item
= FindItem(id
);
648 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
650 item
->Enable(enable
);
653 bool wxMenuBase::IsEnabled( int id
) const
655 wxMenuItem
*item
= FindItem(id
);
657 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
659 return item
->IsEnabled();
662 void wxMenuBase::Check( int id
, bool enable
)
664 wxMenuItem
*item
= FindItem(id
);
666 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
671 bool wxMenuBase::IsChecked( int id
) const
673 wxMenuItem
*item
= FindItem(id
);
675 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
677 return item
->IsChecked();
680 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
682 wxMenuItem
*item
= FindItem(id
);
684 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
686 item
->SetText(label
);
689 wxString
wxMenuBase::GetLabel( int id
) const
691 wxMenuItem
*item
= FindItem(id
);
693 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
695 return item
->GetText();
698 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
700 wxMenuItem
*item
= FindItem(id
);
702 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
704 item
->SetHelp( helpString
);
707 wxString
wxMenuBase::GetHelpString( int id
) const
709 wxMenuItem
*item
= FindItem(id
);
711 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
713 return item
->GetHelp();
716 // ----------------------------------------------------------------------------
717 // wxMenuBarBase ctor and dtor
718 // ----------------------------------------------------------------------------
720 wxMenuBarBase::wxMenuBarBase()
722 // we own the menus when we get them
723 m_menus
.DeleteContents(TRUE
);
726 m_menuBarFrame
= NULL
;
729 wxMenuBarBase::~wxMenuBarBase()
731 // nothing to do, the list will delete the menus because of the call to
732 // DeleteContents() above
735 // ----------------------------------------------------------------------------
736 // wxMenuBar item access: the base class versions manage m_menus list, the
737 // derived class should reflect the changes in the real menubar
738 // ----------------------------------------------------------------------------
740 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
742 wxMenuList::Node
*node
= m_menus
.Item(pos
);
743 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
745 return node
->GetData();
748 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
750 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
752 m_menus
.Append(menu
);
758 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
759 const wxString
& title
)
761 if ( pos
== m_menus
.GetCount() )
763 return wxMenuBarBase::Append(menu
, title
);
765 else // not at the end
767 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
769 wxMenuList::Node
*node
= m_menus
.Item(pos
);
770 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
772 m_menus
.Insert(node
, menu
);
779 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
780 const wxString
& WXUNUSED(title
))
782 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
784 wxMenuList::Node
*node
= m_menus
.Item(pos
);
785 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
787 wxMenu
*menuOld
= node
->GetData();
796 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
798 wxMenuList::Node
*node
= m_menus
.Item(pos
);
799 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
801 node
= m_menus
.DetachNode(node
);
802 wxCHECK( node
, NULL
); // unexpected
803 wxMenu
*menu
= node
->GetData();
811 int wxMenuBarBase::FindMenu(const wxString
& title
) const
813 wxString label
= wxMenuItem::GetLabelFromText(title
);
815 size_t count
= GetMenuCount();
816 for ( size_t i
= 0; i
< count
; i
++ )
818 wxString title2
= GetLabelTop(i
);
819 if ( (title2
== title
) ||
820 (wxMenuItem::GetLabelFromText(title2
) == label
) )
831 // ----------------------------------------------------------------------------
832 // wxMenuBar attaching/detaching to/from the frame
833 // ----------------------------------------------------------------------------
835 void wxMenuBarBase::Attach(wxFrame
*frame
)
837 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
839 m_menuBarFrame
= frame
;
842 void wxMenuBarBase::Detach()
844 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
846 m_menuBarFrame
= NULL
;
849 // ----------------------------------------------------------------------------
850 // wxMenuBar searching for items
851 // ----------------------------------------------------------------------------
853 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
858 wxMenuItem
*item
= NULL
;
859 size_t count
= GetMenuCount();
860 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
862 item
= m_menus
[i
]->FindItem(id
, menu
);
868 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
870 wxString label
= wxMenuItem::GetLabelFromText(menu
);
873 wxMenuList::Node
*node
;
874 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
876 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
877 return node
->GetData()->FindItem(item
);
883 // ---------------------------------------------------------------------------
884 // wxMenuBar functions forwarded to wxMenuItem
885 // ---------------------------------------------------------------------------
887 void wxMenuBarBase::Enable(int id
, bool enable
)
889 wxMenuItem
*item
= FindItem(id
);
891 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
893 item
->Enable(enable
);
896 void wxMenuBarBase::Check(int id
, bool check
)
898 wxMenuItem
*item
= FindItem(id
);
900 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
901 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
906 bool wxMenuBarBase::IsChecked(int id
) const
908 wxMenuItem
*item
= FindItem(id
);
910 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
912 return item
->IsChecked();
915 bool wxMenuBarBase::IsEnabled(int id
) const
917 wxMenuItem
*item
= FindItem(id
);
919 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
921 return item
->IsEnabled();
924 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
926 wxMenuItem
*item
= FindItem(id
);
928 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
930 item
->SetText(label
);
933 wxString
wxMenuBarBase::GetLabel(int id
) const
935 wxMenuItem
*item
= FindItem(id
);
937 wxCHECK_MSG( item
, wxEmptyString
,
938 wxT("wxMenuBar::GetLabel(): no such item") );
940 return item
->GetText();
943 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
945 wxMenuItem
*item
= FindItem(id
);
947 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
949 item
->SetHelp(helpString
);
952 wxString
wxMenuBarBase::GetHelpString(int id
) const
954 wxMenuItem
*item
= FindItem(id
);
956 wxCHECK_MSG( item
, wxEmptyString
,
957 wxT("wxMenuBar::GetHelpString(): no such item") );
959 return item
->GetHelp();
962 #endif // wxUSE_MENUS