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 license
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
= wxToupper(current
[0U]);
138 // is it a function key?
139 if ( current
[0U] == 'f' && isdigit(current
[1U]) &&
140 (current
.Len() == 2 ||
141 (current
.Len() == 3 && isdigit(current
[2U]))) ) {
143 wxSscanf(current
.c_str() + 1, wxT("%d"), &n
);
145 keyCode
= WXK_F1
+ n
- 1;
148 // several special cases
150 if ( current
== wxT("DEL") ) {
151 keyCode
= WXK_DELETE
;
153 else if ( current
== wxT("DELETE") ) {
154 keyCode
= WXK_DELETE
;
156 else if ( current
== wxT("INS") ) {
157 keyCode
= WXK_INSERT
;
159 else if ( current
== wxT("INSERT") ) {
160 keyCode
= WXK_INSERT
;
162 else if ( current
== wxT("ENTER") || current
== wxT("RETURN") ) {
163 keyCode
= WXK_RETURN
;
165 else if ( current
== wxT("PGUP") ) {
168 else if ( current
== wxT("PGDN") ) {
171 else if ( current
== wxT("LEFT") ) {
174 else if ( current
== wxT("RIGHT") ) {
177 else if ( current
== wxT("UP") ) {
180 else if ( current
== wxT("DOWN") ) {
183 else if ( current
== wxT("HOME") ) {
186 else if ( current
== wxT("END") ) {
189 else if ( current
== wxT("SPACE") ) {
192 else if ( current
== wxT("TAB") ) {
197 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
205 // we do have something
206 return new wxAcceleratorEntry(accelFlags
, keyCode
);
210 return (wxAcceleratorEntry
*)NULL
;
213 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
215 return wxGetAccelFromString(GetText());
218 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
220 wxString text
= m_text
.BeforeFirst(wxT('\t'));
225 int flags
= accel
->GetFlags();
226 if ( flags
& wxACCEL_ALT
)
228 if ( flags
& wxACCEL_CTRL
)
229 text
+= wxT("Ctrl-");
230 if ( flags
& wxACCEL_SHIFT
)
231 text
+= wxT("Shift-");
233 int code
= accel
->GetKeyCode();
248 text
<< wxT('F') << code
- WXK_F1
+ 1;
251 // if there are any other keys wxGetAccelFromString() may return,
252 // we should process them here
255 if ( wxIsalnum(code
) )
257 text
<< (wxChar
)code
;
262 wxFAIL_MSG( wxT("unknown keyboard accel") );
269 #endif // wxUSE_ACCEL
271 // ----------------------------------------------------------------------------
272 // wxMenu ctor and dtor
273 // ----------------------------------------------------------------------------
275 void wxMenuBase::Init(long style
)
277 m_items
.DeleteContents(TRUE
);
279 m_menuBar
= (wxMenuBar
*)NULL
;
280 m_menuParent
= (wxMenu
*)NULL
;
282 m_invokingWindow
= (wxWindow
*)NULL
;
284 m_clientData
= (void *)NULL
;
285 m_eventHandler
= this;
287 #if wxUSE_MENU_CALLBACK
288 m_callback
= (wxFunction
) NULL
;
289 #endif // wxUSE_MENU_CALLBACK
292 wxMenuBase::~wxMenuBase()
294 // nothing to do, wxMenuItemList dtor will delete the menu items.
296 // Actually, in GTK, the submenus have to get deleted first.
299 // ----------------------------------------------------------------------------
300 // wxMenu item adding/removing
301 // ----------------------------------------------------------------------------
303 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
305 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
309 submenu
->Attach(m_menuBar
);
312 submenu
->SetParent((wxMenu
*)this);
315 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
317 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
319 m_items
.Append(item
);
320 if ( item
->IsSubMenu() )
322 AddSubMenu(item
->GetSubMenu());
328 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
330 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
332 if ( pos
== GetMenuItemCount() )
334 return DoAppend(item
);
338 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
339 wxT("invalid index in wxMenu::Insert") );
341 return DoInsert(pos
, item
);
345 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
347 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
349 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
350 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
352 m_items
.Insert(node
, item
);
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 wxMenu
*submenu
= item
->GetSubMenu();
384 submenu
->SetParent((wxMenu
*)NULL
);
390 bool wxMenuBase::Delete(wxMenuItem
*item
)
392 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
394 return DoDelete(item
);
397 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
399 wxMenuItem
*item2
= DoRemove(item
);
400 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
402 // don't delete the submenu
403 item2
->SetSubMenu((wxMenu
*)NULL
);
410 bool wxMenuBase::Destroy(wxMenuItem
*item
)
412 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
414 return DoDestroy(item
);
417 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
419 wxMenuItem
*item2
= DoRemove(item
);
420 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
427 // ----------------------------------------------------------------------------
428 // wxMenu searching for items
429 // ----------------------------------------------------------------------------
431 // Finds the item id matching the given string, -1 if not found.
432 int wxMenuBase::FindItem(const wxString
& text
) const
434 wxString label
= wxMenuItem::GetLabelFromText(text
);
435 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
437 node
= node
->GetNext() )
439 wxMenuItem
*item
= node
->GetData();
440 if ( item
->IsSubMenu() )
442 int rc
= item
->GetSubMenu()->FindItem(label
);
443 if ( rc
!= wxNOT_FOUND
)
447 // we execute this code for submenus as well to alllow finding them by
448 // name just like the ordinary items
449 if ( !item
->IsSeparator() )
451 if ( item
->GetLabel() == label
)
452 return item
->GetId();
459 // recursive search for item by id
460 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
465 wxMenuItem
*item
= NULL
;
466 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
468 node
= node
->GetNext() )
470 item
= node
->GetData();
472 if ( item
->GetId() == itemId
)
475 *itemMenu
= (wxMenu
*)this;
477 else if ( item
->IsSubMenu() )
479 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
483 // don't exit the loop
491 // non recursive search
492 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
494 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
495 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
498 for ( pos
= 0; node
; pos
++ )
500 if ( node
->GetData()->GetId() == id
)
502 item
= node
->GetData();
507 node
= node
->GetNext();
512 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
518 // ----------------------------------------------------------------------------
519 // wxMenu helpers used by derived classes
520 // ----------------------------------------------------------------------------
522 // Update a menu and all submenus recursively. source is the object that has
523 // the update event handlers defined for it. If NULL, the menu or associated
524 // window will be used.
525 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
527 if ( !source
&& GetInvokingWindow() )
528 source
= GetInvokingWindow()->GetEventHandler();
530 source
= GetEventHandler();
534 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
537 wxMenuItem
* item
= node
->GetData();
538 if ( !item
->IsSeparator() )
540 wxWindowID id
= item
->GetId();
541 wxUpdateUIEvent
event(id
);
542 event
.SetEventObject( source
);
544 if ( source
->ProcessEvent(event
) )
546 // if anything changed, update the chanegd attribute
547 if (event
.GetSetText())
548 SetLabel(id
, event
.GetText());
549 if (event
.GetSetChecked())
550 Check(id
, event
.GetChecked());
551 if (event
.GetSetEnabled())
552 Enable(id
, event
.GetEnabled());
555 // recurse to the submenus
556 if ( item
->GetSubMenu() )
557 item
->GetSubMenu()->UpdateUI(source
);
559 //else: item is a separator (which don't process update UI events)
561 node
= node
->GetNext();
565 bool wxMenuBase::SendEvent(int id
, int checked
)
567 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
568 event
.SetEventObject(this);
569 event
.SetInt(checked
);
571 bool processed
= FALSE
;
573 #if wxUSE_MENU_CALLBACK
577 (void)(*(m_callback
))(*this, event
);
580 #endif // wxUSE_MENU_CALLBACK
582 // Try the menu's event handler
585 wxEvtHandler
*handler
= GetEventHandler();
587 processed
= handler
->ProcessEvent(event
);
590 // Try the window the menu was popped up from (and up through the
594 const wxMenuBase
*menu
= this;
597 wxWindow
*win
= menu
->GetInvokingWindow();
600 processed
= win
->GetEventHandler()->ProcessEvent(event
);
604 menu
= menu
->GetParent();
611 // ----------------------------------------------------------------------------
612 // wxMenu attaching/detaching to/from menu bar
613 // ----------------------------------------------------------------------------
615 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
617 // use Detach() instead!
618 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
620 // use IsAttached() to prevent this from happening
621 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
623 m_menuBar
= (wxMenuBar
*)menubar
;
626 void wxMenuBase::Detach()
628 // use IsAttached() to prevent this from happening
629 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
634 // ----------------------------------------------------------------------------
635 // wxMenu functions forwarded to wxMenuItem
636 // ----------------------------------------------------------------------------
638 void wxMenuBase::Enable( int id
, bool enable
)
640 wxMenuItem
*item
= FindItem(id
);
642 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
644 item
->Enable(enable
);
647 bool wxMenuBase::IsEnabled( int id
) const
649 wxMenuItem
*item
= FindItem(id
);
651 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
653 return item
->IsEnabled();
656 void wxMenuBase::Check( int id
, bool enable
)
658 wxMenuItem
*item
= FindItem(id
);
660 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
665 bool wxMenuBase::IsChecked( int id
) const
667 wxMenuItem
*item
= FindItem(id
);
669 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
671 return item
->IsChecked();
674 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
676 wxMenuItem
*item
= FindItem(id
);
678 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
680 item
->SetText(label
);
683 wxString
wxMenuBase::GetLabel( int id
) const
685 wxMenuItem
*item
= FindItem(id
);
687 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
689 return item
->GetText();
692 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
694 wxMenuItem
*item
= FindItem(id
);
696 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
698 item
->SetHelp( helpString
);
701 wxString
wxMenuBase::GetHelpString( int id
) const
703 wxMenuItem
*item
= FindItem(id
);
705 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
707 return item
->GetHelp();
710 // ----------------------------------------------------------------------------
711 // wxMenuBarBase ctor and dtor
712 // ----------------------------------------------------------------------------
714 wxMenuBarBase::wxMenuBarBase()
716 // we own the menus when we get them
717 m_menus
.DeleteContents(TRUE
);
720 m_menuBarFrame
= NULL
;
723 wxMenuBarBase::~wxMenuBarBase()
725 // nothing to do, the list will delete the menus because of the call to
726 // DeleteContents() above
729 // ----------------------------------------------------------------------------
730 // wxMenuBar item access: the base class versions manage m_menus list, the
731 // derived class should reflect the changes in the real menubar
732 // ----------------------------------------------------------------------------
734 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
736 wxMenuList::Node
*node
= m_menus
.Item(pos
);
737 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
739 return node
->GetData();
742 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
744 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
746 m_menus
.Append(menu
);
752 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
753 const wxString
& title
)
755 if ( pos
== m_menus
.GetCount() )
757 return wxMenuBarBase::Append(menu
, title
);
759 else // not at the end
761 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
763 wxMenuList::Node
*node
= m_menus
.Item(pos
);
764 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
766 m_menus
.Insert(node
, menu
);
773 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
774 const wxString
& WXUNUSED(title
))
776 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
778 wxMenuList::Node
*node
= m_menus
.Item(pos
);
779 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
781 wxMenu
*menuOld
= node
->GetData();
790 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
792 wxMenuList::Node
*node
= m_menus
.Item(pos
);
793 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
795 node
= m_menus
.DetachNode(node
);
796 wxCHECK( node
, NULL
); // unexpected
797 wxMenu
*menu
= node
->GetData();
805 int wxMenuBarBase::FindMenu(const wxString
& title
) const
807 wxString label
= wxMenuItem::GetLabelFromText(title
);
809 size_t count
= GetMenuCount();
810 for ( size_t i
= 0; i
< count
; i
++ )
812 wxString title2
= GetLabelTop(i
);
813 if ( (title2
== title
) ||
814 (wxMenuItem::GetLabelFromText(title2
) == label
) )
825 // ----------------------------------------------------------------------------
826 // wxMenuBar attaching/detaching to/from the frame
827 // ----------------------------------------------------------------------------
829 void wxMenuBarBase::Attach(wxFrame
*frame
)
831 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
833 m_menuBarFrame
= frame
;
836 void wxMenuBarBase::Detach()
838 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
840 m_menuBarFrame
= NULL
;
843 // ----------------------------------------------------------------------------
844 // wxMenuBar searching for items
845 // ----------------------------------------------------------------------------
847 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
852 wxMenuItem
*item
= NULL
;
853 size_t count
= GetMenuCount();
854 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
856 item
= m_menus
[i
]->FindItem(id
, menu
);
862 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
864 wxString label
= wxMenuItem::GetLabelFromText(menu
);
867 wxMenuList::Node
*node
;
868 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
870 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
871 return node
->GetData()->FindItem(item
);
877 // ---------------------------------------------------------------------------
878 // wxMenuBar functions forwarded to wxMenuItem
879 // ---------------------------------------------------------------------------
881 void wxMenuBarBase::Enable(int id
, bool enable
)
883 wxMenuItem
*item
= FindItem(id
);
885 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
887 item
->Enable(enable
);
890 void wxMenuBarBase::Check(int id
, bool check
)
892 wxMenuItem
*item
= FindItem(id
);
894 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
895 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
900 bool wxMenuBarBase::IsChecked(int id
) const
902 wxMenuItem
*item
= FindItem(id
);
904 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
906 return item
->IsChecked();
909 bool wxMenuBarBase::IsEnabled(int id
) const
911 wxMenuItem
*item
= FindItem(id
);
913 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
915 return item
->IsEnabled();
918 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
920 wxMenuItem
*item
= FindItem(id
);
922 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
924 item
->SetText(label
);
927 wxString
wxMenuBarBase::GetLabel(int id
) const
929 wxMenuItem
*item
= FindItem(id
);
931 wxCHECK_MSG( item
, wxEmptyString
,
932 wxT("wxMenuBar::GetLabel(): no such item") );
934 return item
->GetText();
937 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
939 wxMenuItem
*item
= FindItem(id
);
941 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
943 item
->SetHelp(helpString
);
946 wxString
wxMenuBarBase::GetHelpString(int id
) const
948 wxMenuItem
*item
= FindItem(id
);
950 wxCHECK_MSG( item
, wxEmptyString
,
951 wxT("wxMenuBar::GetHelpString(): no such item") );
953 return item
->GetHelp();
956 #endif // wxUSE_MENUS