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
;
158 else if ( current
== wxT("DELETE") )
159 keyCode
= WXK_DELETE
;
160 else if ( current
== wxT("INS") )
161 keyCode
= WXK_INSERT
;
162 else if ( current
== wxT("INSERT") )
163 keyCode
= WXK_INSERT
;
164 else if ( current
== wxT("ENTER") || current
== wxT("RETURN") )
165 keyCode
= WXK_RETURN
;
166 else if ( current
== wxT("PGUP") )
168 else if ( current
== wxT("PGDN") )
170 else if ( current
== wxT("LEFT") )
172 else if ( current
== wxT("RIGHT") )
174 else if ( current
== wxT("UP") )
176 else if ( current
== wxT("DOWN") )
178 else if ( current
== wxT("HOME") )
180 else if ( current
== wxT("END") )
182 else if ( current
== wxT("SPACE") )
184 else if ( current
== wxT("TAB") )
186 else if ( current
== wxT("ESC") || current
== wxT("ESCAPE") )
187 keyCode
= WXK_ESCAPE
;
190 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
198 // we do have something
199 return new wxAcceleratorEntry(accelFlags
, keyCode
);
203 return (wxAcceleratorEntry
*)NULL
;
206 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
208 return wxGetAccelFromString(GetText());
211 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
213 wxString text
= m_text
.BeforeFirst(wxT('\t'));
218 int flags
= accel
->GetFlags();
219 if ( flags
& wxACCEL_ALT
)
221 if ( flags
& wxACCEL_CTRL
)
222 text
+= wxT("Ctrl-");
223 if ( flags
& wxACCEL_SHIFT
)
224 text
+= wxT("Shift-");
226 int code
= accel
->GetKeyCode();
241 text
<< wxT('F') << code
- WXK_F1
+ 1;
244 // if there are any other keys wxGetAccelFromString() may return,
245 // we should process them here
248 if ( wxIsalnum(code
) )
250 text
<< (wxChar
)code
;
255 wxFAIL_MSG( wxT("unknown keyboard accel") );
262 #endif // wxUSE_ACCEL
264 // ----------------------------------------------------------------------------
265 // wxMenu ctor and dtor
266 // ----------------------------------------------------------------------------
268 void wxMenuBase::Init(long style
)
270 m_items
.DeleteContents(TRUE
);
272 m_menuBar
= (wxMenuBar
*)NULL
;
273 m_menuParent
= (wxMenu
*)NULL
;
275 m_invokingWindow
= (wxWindow
*)NULL
;
277 m_clientData
= (void *)NULL
;
278 m_eventHandler
= this;
280 #if wxUSE_MENU_CALLBACK
281 m_callback
= (wxFunction
) NULL
;
282 #endif // wxUSE_MENU_CALLBACK
285 wxMenuBase::~wxMenuBase()
287 // nothing to do, wxMenuItemList dtor will delete the menu items.
289 // Actually, in GTK, the submenus have to get deleted first.
292 // ----------------------------------------------------------------------------
293 // wxMenu item adding/removing
294 // ----------------------------------------------------------------------------
296 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
298 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
302 submenu
->Attach(m_menuBar
);
305 submenu
->SetParent((wxMenu
*)this);
308 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
310 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
312 m_items
.Append(item
);
313 if ( item
->IsSubMenu() )
315 AddSubMenu(item
->GetSubMenu());
321 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
323 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
325 if ( pos
== GetMenuItemCount() )
327 return DoAppend(item
);
331 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
332 wxT("invalid index in wxMenu::Insert") );
334 return DoInsert(pos
, item
);
338 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
340 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
342 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
343 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
345 m_items
.Insert(node
, item
);
346 if ( item
->IsSubMenu() )
348 AddSubMenu(item
->GetSubMenu());
354 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
356 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
358 return DoRemove(item
);
361 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
363 wxMenuItemList::Node
*node
= m_items
.Find(item
);
365 // if we get here, the item is valid or one of Remove() functions is broken
366 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
368 // we detach the item, but we do delete the list node (i.e. don't call
369 // DetachNode() here!)
370 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
371 m_items
.DeleteNode(node
);
373 // item isn't attached to anything any more
374 wxMenu
*submenu
= item
->GetSubMenu();
377 submenu
->SetParent((wxMenu
*)NULL
);
383 bool wxMenuBase::Delete(wxMenuItem
*item
)
385 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
387 return DoDelete(item
);
390 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
392 wxMenuItem
*item2
= DoRemove(item
);
393 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
395 // don't delete the submenu
396 item2
->SetSubMenu((wxMenu
*)NULL
);
403 bool wxMenuBase::Destroy(wxMenuItem
*item
)
405 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
407 return DoDestroy(item
);
410 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
412 wxMenuItem
*item2
= DoRemove(item
);
413 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
420 // ----------------------------------------------------------------------------
421 // wxMenu searching for items
422 // ----------------------------------------------------------------------------
424 // Finds the item id matching the given string, -1 if not found.
425 int wxMenuBase::FindItem(const wxString
& text
) const
427 wxString label
= wxMenuItem::GetLabelFromText(text
);
428 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
430 node
= node
->GetNext() )
432 wxMenuItem
*item
= node
->GetData();
433 if ( item
->IsSubMenu() )
435 int rc
= item
->GetSubMenu()->FindItem(label
);
436 if ( rc
!= wxNOT_FOUND
)
440 // we execute this code for submenus as well to alllow finding them by
441 // name just like the ordinary items
442 if ( !item
->IsSeparator() )
444 if ( item
->GetLabel() == label
)
445 return item
->GetId();
452 // recursive search for item by id
453 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
458 wxMenuItem
*item
= NULL
;
459 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
461 node
= node
->GetNext() )
463 item
= node
->GetData();
465 if ( item
->GetId() == itemId
)
468 *itemMenu
= (wxMenu
*)this;
470 else if ( item
->IsSubMenu() )
472 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
476 // don't exit the loop
484 // non recursive search
485 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
487 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
488 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
491 for ( pos
= 0; node
; pos
++ )
493 if ( node
->GetData()->GetId() == id
)
495 item
= node
->GetData();
500 node
= node
->GetNext();
505 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
511 // ----------------------------------------------------------------------------
512 // wxMenu helpers used by derived classes
513 // ----------------------------------------------------------------------------
515 // Update a menu and all submenus recursively. source is the object that has
516 // the update event handlers defined for it. If NULL, the menu or associated
517 // window will be used.
518 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
520 if ( !source
&& GetInvokingWindow() )
521 source
= GetInvokingWindow()->GetEventHandler();
523 source
= GetEventHandler();
527 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
530 wxMenuItem
* item
= node
->GetData();
531 if ( !item
->IsSeparator() )
533 wxWindowID id
= item
->GetId();
534 wxUpdateUIEvent
event(id
);
535 event
.SetEventObject( source
);
537 if ( source
->ProcessEvent(event
) )
539 // if anything changed, update the chanegd attribute
540 if (event
.GetSetText())
541 SetLabel(id
, event
.GetText());
542 if (event
.GetSetChecked())
543 Check(id
, event
.GetChecked());
544 if (event
.GetSetEnabled())
545 Enable(id
, event
.GetEnabled());
548 // recurse to the submenus
549 if ( item
->GetSubMenu() )
550 item
->GetSubMenu()->UpdateUI(source
);
552 //else: item is a separator (which don't process update UI events)
554 node
= node
->GetNext();
558 bool wxMenuBase::SendEvent(int id
, int checked
)
560 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
561 event
.SetEventObject(this);
562 event
.SetInt(checked
);
564 bool processed
= FALSE
;
566 #if wxUSE_MENU_CALLBACK
570 (void)(*(m_callback
))(*this, event
);
573 #endif // wxUSE_MENU_CALLBACK
575 // Try the menu's event handler
578 wxEvtHandler
*handler
= GetEventHandler();
580 processed
= handler
->ProcessEvent(event
);
583 // Try the window the menu was popped up from (and up through the
587 const wxMenuBase
*menu
= this;
590 wxWindow
*win
= menu
->GetInvokingWindow();
593 processed
= win
->GetEventHandler()->ProcessEvent(event
);
597 menu
= menu
->GetParent();
604 // ----------------------------------------------------------------------------
605 // wxMenu attaching/detaching to/from menu bar
606 // ----------------------------------------------------------------------------
608 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
610 // use Detach() instead!
611 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
613 // use IsAttached() to prevent this from happening
614 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
616 m_menuBar
= (wxMenuBar
*)menubar
;
619 void wxMenuBase::Detach()
621 // use IsAttached() to prevent this from happening
622 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
627 // ----------------------------------------------------------------------------
628 // wxMenu functions forwarded to wxMenuItem
629 // ----------------------------------------------------------------------------
631 void wxMenuBase::Enable( int id
, bool enable
)
633 wxMenuItem
*item
= FindItem(id
);
635 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
637 item
->Enable(enable
);
640 bool wxMenuBase::IsEnabled( int id
) const
642 wxMenuItem
*item
= FindItem(id
);
644 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
646 return item
->IsEnabled();
649 void wxMenuBase::Check( int id
, bool enable
)
651 wxMenuItem
*item
= FindItem(id
);
653 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
658 bool wxMenuBase::IsChecked( int id
) const
660 wxMenuItem
*item
= FindItem(id
);
662 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
664 return item
->IsChecked();
667 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
669 wxMenuItem
*item
= FindItem(id
);
671 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
673 item
->SetText(label
);
676 wxString
wxMenuBase::GetLabel( int id
) const
678 wxMenuItem
*item
= FindItem(id
);
680 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
682 return item
->GetText();
685 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
687 wxMenuItem
*item
= FindItem(id
);
689 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
691 item
->SetHelp( helpString
);
694 wxString
wxMenuBase::GetHelpString( int id
) const
696 wxMenuItem
*item
= FindItem(id
);
698 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
700 return item
->GetHelp();
703 // ----------------------------------------------------------------------------
704 // wxMenuBarBase ctor and dtor
705 // ----------------------------------------------------------------------------
707 wxMenuBarBase::wxMenuBarBase()
709 // we own the menus when we get them
710 m_menus
.DeleteContents(TRUE
);
713 m_menuBarFrame
= NULL
;
716 wxMenuBarBase::~wxMenuBarBase()
718 // nothing to do, the list will delete the menus because of the call to
719 // DeleteContents() above
722 // ----------------------------------------------------------------------------
723 // wxMenuBar item access: the base class versions manage m_menus list, the
724 // derived class should reflect the changes in the real menubar
725 // ----------------------------------------------------------------------------
727 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
729 wxMenuList::Node
*node
= m_menus
.Item(pos
);
730 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
732 return node
->GetData();
735 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
737 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
739 m_menus
.Append(menu
);
745 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
746 const wxString
& title
)
748 if ( pos
== m_menus
.GetCount() )
750 return wxMenuBarBase::Append(menu
, title
);
752 else // not at the end
754 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
756 wxMenuList::Node
*node
= m_menus
.Item(pos
);
757 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
759 m_menus
.Insert(node
, menu
);
766 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
767 const wxString
& WXUNUSED(title
))
769 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
771 wxMenuList::Node
*node
= m_menus
.Item(pos
);
772 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
774 wxMenu
*menuOld
= node
->GetData();
783 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
785 wxMenuList::Node
*node
= m_menus
.Item(pos
);
786 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
788 node
= m_menus
.DetachNode(node
);
789 wxCHECK( node
, NULL
); // unexpected
790 wxMenu
*menu
= node
->GetData();
798 int wxMenuBarBase::FindMenu(const wxString
& title
) const
800 wxString label
= wxMenuItem::GetLabelFromText(title
);
802 size_t count
= GetMenuCount();
803 for ( size_t i
= 0; i
< count
; i
++ )
805 wxString title2
= GetLabelTop(i
);
806 if ( (title2
== title
) ||
807 (wxMenuItem::GetLabelFromText(title2
) == label
) )
818 // ----------------------------------------------------------------------------
819 // wxMenuBar attaching/detaching to/from the frame
820 // ----------------------------------------------------------------------------
822 void wxMenuBarBase::Attach(wxFrame
*frame
)
824 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
826 m_menuBarFrame
= frame
;
829 void wxMenuBarBase::Detach()
831 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
833 m_menuBarFrame
= NULL
;
836 // ----------------------------------------------------------------------------
837 // wxMenuBar searching for items
838 // ----------------------------------------------------------------------------
840 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
845 wxMenuItem
*item
= NULL
;
846 size_t count
= GetMenuCount();
847 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
849 item
= m_menus
[i
]->FindItem(id
, menu
);
855 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
857 wxString label
= wxMenuItem::GetLabelFromText(menu
);
860 wxMenuList::Node
*node
;
861 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
863 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
864 return node
->GetData()->FindItem(item
);
870 // ---------------------------------------------------------------------------
871 // wxMenuBar functions forwarded to wxMenuItem
872 // ---------------------------------------------------------------------------
874 void wxMenuBarBase::Enable(int id
, bool enable
)
876 wxMenuItem
*item
= FindItem(id
);
878 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
880 item
->Enable(enable
);
883 void wxMenuBarBase::Check(int id
, bool check
)
885 wxMenuItem
*item
= FindItem(id
);
887 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
888 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
893 bool wxMenuBarBase::IsChecked(int id
) const
895 wxMenuItem
*item
= FindItem(id
);
897 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
899 return item
->IsChecked();
902 bool wxMenuBarBase::IsEnabled(int id
) const
904 wxMenuItem
*item
= FindItem(id
);
906 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
908 return item
->IsEnabled();
911 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
913 wxMenuItem
*item
= FindItem(id
);
915 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
917 item
->SetText(label
);
920 wxString
wxMenuBarBase::GetLabel(int id
) const
922 wxMenuItem
*item
= FindItem(id
);
924 wxCHECK_MSG( item
, wxEmptyString
,
925 wxT("wxMenuBar::GetLabel(): no such item") );
927 return item
->GetText();
930 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
932 wxMenuItem
*item
= FindItem(id
);
934 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
936 item
->SetHelp(helpString
);
939 wxString
wxMenuBarBase::GetHelpString(int id
) const
941 wxMenuItem
*item
= FindItem(id
);
943 wxCHECK_MSG( item
, wxEmptyString
,
944 wxT("wxMenuBar::GetHelpString(): no such item") );
946 return item
->GetHelp();
949 #endif // wxUSE_MENUS