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 if ( item
->IsSubMenu() )
324 AddSubMenu(item
->GetSubMenu());
330 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
332 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
334 if ( pos
== GetMenuItemCount() )
336 return DoAppend(item
);
340 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
341 wxT("invalid index in wxMenu::Insert") );
343 return DoInsert(pos
, item
);
347 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
349 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
351 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
352 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
354 m_items
.Insert(node
, item
);
355 if ( item
->IsSubMenu() )
357 AddSubMenu(item
->GetSubMenu());
363 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
365 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
367 return DoRemove(item
);
370 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
372 wxMenuItemList::Node
*node
= m_items
.Find(item
);
374 // if we get here, the item is valid or one of Remove() functions is broken
375 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
377 // we detach the item, but we do delete the list node (i.e. don't call
378 // DetachNode() here!)
379 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
380 m_items
.DeleteNode(node
);
382 // item isn't attached to anything any more
383 wxMenu
*submenu
= item
->GetSubMenu();
386 submenu
->SetParent((wxMenu
*)NULL
);
392 bool wxMenuBase::Delete(wxMenuItem
*item
)
394 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
396 return DoDelete(item
);
399 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
401 wxMenuItem
*item2
= DoRemove(item
);
402 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
404 // don't delete the submenu
405 item2
->SetSubMenu((wxMenu
*)NULL
);
412 bool wxMenuBase::Destroy(wxMenuItem
*item
)
414 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
416 return DoDestroy(item
);
419 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
421 wxMenuItem
*item2
= DoRemove(item
);
422 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
429 // ----------------------------------------------------------------------------
430 // wxMenu searching for items
431 // ----------------------------------------------------------------------------
433 // Finds the item id matching the given string, -1 if not found.
434 int wxMenuBase::FindItem(const wxString
& text
) const
436 wxString label
= wxMenuItem::GetLabelFromText(text
);
437 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
439 node
= node
->GetNext() )
441 wxMenuItem
*item
= node
->GetData();
442 if ( item
->IsSubMenu() )
444 int rc
= item
->GetSubMenu()->FindItem(label
);
445 if ( rc
!= wxNOT_FOUND
)
449 // we execute this code for submenus as well to alllow finding them by
450 // name just like the ordinary items
451 if ( !item
->IsSeparator() )
453 if ( item
->GetLabel() == label
)
454 return item
->GetId();
461 // recursive search for item by id
462 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
467 wxMenuItem
*item
= NULL
;
468 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
470 node
= node
->GetNext() )
472 item
= node
->GetData();
474 if ( item
->GetId() == itemId
)
477 *itemMenu
= (wxMenu
*)this;
479 else if ( item
->IsSubMenu() )
481 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
485 // don't exit the loop
493 // non recursive search
494 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
496 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
497 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
500 for ( pos
= 0; node
; pos
++ )
502 if ( node
->GetData()->GetId() == id
)
504 item
= node
->GetData();
509 node
= node
->GetNext();
514 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
520 // ----------------------------------------------------------------------------
521 // wxMenu helpers used by derived classes
522 // ----------------------------------------------------------------------------
524 // Update a menu and all submenus recursively. source is the object that has
525 // the update event handlers defined for it. If NULL, the menu or associated
526 // window will be used.
527 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
529 if ( !source
&& GetInvokingWindow() )
530 source
= GetInvokingWindow()->GetEventHandler();
532 source
= GetEventHandler();
536 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
539 wxMenuItem
* item
= node
->GetData();
540 if ( !item
->IsSeparator() )
542 wxWindowID id
= item
->GetId();
543 wxUpdateUIEvent
event(id
);
544 event
.SetEventObject( source
);
546 if ( source
->ProcessEvent(event
) )
548 // if anything changed, update the chanegd attribute
549 if (event
.GetSetText())
550 SetLabel(id
, event
.GetText());
551 if (event
.GetSetChecked())
552 Check(id
, event
.GetChecked());
553 if (event
.GetSetEnabled())
554 Enable(id
, event
.GetEnabled());
557 // recurse to the submenus
558 if ( item
->GetSubMenu() )
559 item
->GetSubMenu()->UpdateUI(source
);
561 //else: item is a separator (which don't process update UI events)
563 node
= node
->GetNext();
567 bool wxMenuBase::SendEvent(int id
, int checked
)
569 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
570 event
.SetEventObject(this);
571 event
.SetInt(checked
);
573 bool processed
= FALSE
;
575 #if wxUSE_MENU_CALLBACK
579 (void)(*(m_callback
))(*this, event
);
582 #endif // wxUSE_MENU_CALLBACK
584 // Try the menu's event handler
587 wxEvtHandler
*handler
= GetEventHandler();
589 processed
= handler
->ProcessEvent(event
);
592 // Try the window the menu was popped up from (and up through the
596 const wxMenuBase
*menu
= this;
599 wxWindow
*win
= menu
->GetInvokingWindow();
602 processed
= win
->GetEventHandler()->ProcessEvent(event
);
606 menu
= menu
->GetParent();
613 // ----------------------------------------------------------------------------
614 // wxMenu attaching/detaching to/from menu bar
615 // ----------------------------------------------------------------------------
617 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
619 // use Detach() instead!
620 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
622 // use IsAttached() to prevent this from happening
623 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
625 m_menuBar
= (wxMenuBar
*)menubar
;
628 void wxMenuBase::Detach()
630 // use IsAttached() to prevent this from happening
631 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
636 // ----------------------------------------------------------------------------
637 // wxMenu functions forwarded to wxMenuItem
638 // ----------------------------------------------------------------------------
640 void wxMenuBase::Enable( int id
, bool enable
)
642 wxMenuItem
*item
= FindItem(id
);
644 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
646 item
->Enable(enable
);
649 bool wxMenuBase::IsEnabled( int id
) const
651 wxMenuItem
*item
= FindItem(id
);
653 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
655 return item
->IsEnabled();
658 void wxMenuBase::Check( int id
, bool enable
)
660 wxMenuItem
*item
= FindItem(id
);
662 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
667 bool wxMenuBase::IsChecked( int id
) const
669 wxMenuItem
*item
= FindItem(id
);
671 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
673 return item
->IsChecked();
676 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
678 wxMenuItem
*item
= FindItem(id
);
680 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
682 item
->SetText(label
);
685 wxString
wxMenuBase::GetLabel( int id
) const
687 wxMenuItem
*item
= FindItem(id
);
689 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
691 return item
->GetText();
694 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
696 wxMenuItem
*item
= FindItem(id
);
698 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
700 item
->SetHelp( helpString
);
703 wxString
wxMenuBase::GetHelpString( int id
) const
705 wxMenuItem
*item
= FindItem(id
);
707 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
709 return item
->GetHelp();
712 // ----------------------------------------------------------------------------
713 // wxMenuBarBase ctor and dtor
714 // ----------------------------------------------------------------------------
716 wxMenuBarBase::wxMenuBarBase()
718 // we own the menus when we get them
719 m_menus
.DeleteContents(TRUE
);
722 m_menuBarFrame
= NULL
;
725 wxMenuBarBase::~wxMenuBarBase()
727 // nothing to do, the list will delete the menus because of the call to
728 // DeleteContents() above
731 // ----------------------------------------------------------------------------
732 // wxMenuBar item access: the base class versions manage m_menus list, the
733 // derived class should reflect the changes in the real menubar
734 // ----------------------------------------------------------------------------
736 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
738 wxMenuList::Node
*node
= m_menus
.Item(pos
);
739 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
741 return node
->GetData();
744 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
746 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
748 m_menus
.Append(menu
);
754 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
755 const wxString
& title
)
757 if ( pos
== m_menus
.GetCount() )
759 return wxMenuBarBase::Append(menu
, title
);
761 else // not at the end
763 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
765 wxMenuList::Node
*node
= m_menus
.Item(pos
);
766 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
768 m_menus
.Insert(node
, menu
);
775 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
776 const wxString
& WXUNUSED(title
))
778 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
780 wxMenuList::Node
*node
= m_menus
.Item(pos
);
781 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
783 wxMenu
*menuOld
= node
->GetData();
792 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
794 wxMenuList::Node
*node
= m_menus
.Item(pos
);
795 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
797 node
= m_menus
.DetachNode(node
);
798 wxCHECK( node
, NULL
); // unexpected
799 wxMenu
*menu
= node
->GetData();
807 int wxMenuBarBase::FindMenu(const wxString
& title
) const
809 wxString label
= wxMenuItem::GetLabelFromText(title
);
811 size_t count
= GetMenuCount();
812 for ( size_t i
= 0; i
< count
; i
++ )
814 wxString title2
= GetLabelTop(i
);
815 if ( (title2
== title
) ||
816 (wxMenuItem::GetLabelFromText(title2
) == label
) )
827 // ----------------------------------------------------------------------------
828 // wxMenuBar attaching/detaching to/from the frame
829 // ----------------------------------------------------------------------------
831 void wxMenuBarBase::Attach(wxFrame
*frame
)
833 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
835 m_menuBarFrame
= frame
;
838 void wxMenuBarBase::Detach()
840 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
842 m_menuBarFrame
= NULL
;
845 // ----------------------------------------------------------------------------
846 // wxMenuBar searching for items
847 // ----------------------------------------------------------------------------
849 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
854 wxMenuItem
*item
= NULL
;
855 size_t count
= GetMenuCount();
856 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
858 item
= m_menus
[i
]->FindItem(id
, menu
);
864 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
866 wxString label
= wxMenuItem::GetLabelFromText(menu
);
869 wxMenuList::Node
*node
;
870 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
872 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
873 return node
->GetData()->FindItem(item
);
879 // ---------------------------------------------------------------------------
880 // wxMenuBar functions forwarded to wxMenuItem
881 // ---------------------------------------------------------------------------
883 void wxMenuBarBase::Enable(int id
, bool enable
)
885 wxMenuItem
*item
= FindItem(id
);
887 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
889 item
->Enable(enable
);
892 void wxMenuBarBase::Check(int id
, bool check
)
894 wxMenuItem
*item
= FindItem(id
);
896 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
897 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
902 bool wxMenuBarBase::IsChecked(int id
) const
904 wxMenuItem
*item
= FindItem(id
);
906 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
908 return item
->IsChecked();
911 bool wxMenuBarBase::IsEnabled(int id
) const
913 wxMenuItem
*item
= FindItem(id
);
915 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
917 return item
->IsEnabled();
920 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
922 wxMenuItem
*item
= FindItem(id
);
924 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
926 item
->SetText(label
);
929 wxString
wxMenuBarBase::GetLabel(int id
) const
931 wxMenuItem
*item
= FindItem(id
);
933 wxCHECK_MSG( item
, wxEmptyString
,
934 wxT("wxMenuBar::GetLabel(): no such item") );
936 return item
->GetText();
939 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
941 wxMenuItem
*item
= FindItem(id
);
943 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
945 item
->SetHelp(helpString
);
948 wxString
wxMenuBarBase::GetHelpString(int id
) const
950 wxMenuItem
*item
= FindItem(id
);
952 wxCHECK_MSG( item
, wxEmptyString
,
953 wxT("wxMenuBar::GetHelpString(): no such item") );
955 return item
->GetHelp();
958 #endif // wxUSE_MENUS