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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
+= (wxChar
) 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' && wxIsdigit(current
[1U]) &&
155 (current
.Len() == 2 ||
156 (current
.Len() == 3 && wxIsdigit(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("BACK") )
171 else if ( current
== wxT("INS") )
172 keyCode
= WXK_INSERT
;
173 else if ( current
== wxT("INSERT") )
174 keyCode
= WXK_INSERT
;
175 else if ( current
== wxT("ENTER") || current
== wxT("RETURN") )
176 keyCode
= WXK_RETURN
;
177 else if ( current
== wxT("PGUP") )
179 else if ( current
== wxT("PGDN") )
181 else if ( current
== wxT("LEFT") )
183 else if ( current
== wxT("RIGHT") )
185 else if ( current
== wxT("UP") )
187 else if ( current
== wxT("DOWN") )
189 else if ( current
== wxT("HOME") )
191 else if ( current
== wxT("END") )
193 else if ( current
== wxT("SPACE") )
195 else if ( current
== wxT("TAB") )
197 else if ( current
== wxT("ESC") || current
== wxT("ESCAPE") )
198 keyCode
= WXK_ESCAPE
;
201 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
209 // we do have something
210 return new wxAcceleratorEntry(accelFlags
, keyCode
);
214 return (wxAcceleratorEntry
*)NULL
;
217 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
219 return wxGetAccelFromString(GetText());
222 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
224 wxString text
= m_text
.BeforeFirst(wxT('\t'));
229 int flags
= accel
->GetFlags();
230 if ( flags
& wxACCEL_ALT
)
232 if ( flags
& wxACCEL_CTRL
)
233 text
+= wxT("Ctrl-");
234 if ( flags
& wxACCEL_SHIFT
)
235 text
+= wxT("Shift-");
237 int code
= accel
->GetKeyCode();
252 text
<< wxT('F') << code
- WXK_F1
+ 1;
255 // if there are any other keys wxGetAccelFromString() may return,
256 // we should process them here
259 if ( wxIsalnum(code
) )
261 text
<< (wxChar
)code
;
266 wxFAIL_MSG( wxT("unknown keyboard accel") );
273 #endif // wxUSE_ACCEL
275 // ----------------------------------------------------------------------------
276 // wxMenu ctor and dtor
277 // ----------------------------------------------------------------------------
279 void wxMenuBase::Init(long style
)
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 WX_CLEAR_LIST(wxMenuItemList
, m_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::compatibility_iterator 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::compatibility_iterator 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!)
379 // item isn't attached to anything any more
380 item
->SetMenu((wxMenu
*)NULL
);
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::compatibility_iterator 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::compatibility_iterator 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::compatibility_iterator 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
;
519 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
521 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
522 _T("wxMenu::FindItemByPosition(): invalid menu index") );
524 return m_items
.Item( position
)->GetData();
527 // ----------------------------------------------------------------------------
528 // wxMenu helpers used by derived classes
529 // ----------------------------------------------------------------------------
531 // Update a menu and all submenus recursively. source is the object that has
532 // the update event handlers defined for it. If NULL, the menu or associated
533 // window will be used.
534 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
536 if ( !source
&& GetInvokingWindow() )
537 source
= GetInvokingWindow()->GetEventHandler();
539 source
= GetEventHandler();
543 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
546 wxMenuItem
* item
= node
->GetData();
547 if ( !item
->IsSeparator() )
549 wxWindowID id
= item
->GetId();
550 wxUpdateUIEvent
event(id
);
551 event
.SetEventObject( source
);
553 if ( source
->ProcessEvent(event
) )
555 // if anything changed, update the changed attribute
556 if (event
.GetSetText())
557 SetLabel(id
, event
.GetText());
558 if (event
.GetSetChecked())
559 Check(id
, event
.GetChecked());
560 if (event
.GetSetEnabled())
561 Enable(id
, event
.GetEnabled());
564 // recurse to the submenus
565 if ( item
->GetSubMenu() )
566 item
->GetSubMenu()->UpdateUI(source
);
568 //else: item is a separator (which doesn't process update UI events)
570 node
= node
->GetNext();
574 bool wxMenuBase::SendEvent(int id
, int checked
)
576 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
577 event
.SetEventObject(this);
578 event
.SetInt(checked
);
580 bool processed
= FALSE
;
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()
717 m_menuBarFrame
= NULL
;
720 wxMenuBarBase::~wxMenuBarBase()
722 WX_CLEAR_LIST(wxMenuList
, m_menus
);
725 // ----------------------------------------------------------------------------
726 // wxMenuBar item access: the base class versions manage m_menus list, the
727 // derived class should reflect the changes in the real menubar
728 // ----------------------------------------------------------------------------
730 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
732 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
733 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
735 return node
->GetData();
738 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
740 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
742 m_menus
.Append(menu
);
748 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
749 const wxString
& title
)
751 if ( pos
== m_menus
.GetCount() )
753 return wxMenuBarBase::Append(menu
, title
);
755 else // not at the end
757 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
759 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
760 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
762 m_menus
.Insert(node
, menu
);
769 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
770 const wxString
& WXUNUSED(title
))
772 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
774 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
775 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
777 wxMenu
*menuOld
= node
->GetData();
786 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
788 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
789 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
791 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(), i
;
847 wxMenuList::const_iterator it
;
848 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
850 item
= (*it
)->FindItem(id
, menu
);
856 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
858 wxString label
= wxMenuItem::GetLabelFromText(menu
);
861 wxMenuList::compatibility_iterator node
;
862 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
864 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
865 return node
->GetData()->FindItem(item
);
871 // ---------------------------------------------------------------------------
872 // wxMenuBar functions forwarded to wxMenuItem
873 // ---------------------------------------------------------------------------
875 void wxMenuBarBase::Enable(int id
, bool enable
)
877 wxMenuItem
*item
= FindItem(id
);
879 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
881 item
->Enable(enable
);
884 void wxMenuBarBase::Check(int id
, bool check
)
886 wxMenuItem
*item
= FindItem(id
);
888 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
889 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
894 bool wxMenuBarBase::IsChecked(int id
) const
896 wxMenuItem
*item
= FindItem(id
);
898 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
900 return item
->IsChecked();
903 bool wxMenuBarBase::IsEnabled(int id
) const
905 wxMenuItem
*item
= FindItem(id
);
907 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
909 return item
->IsEnabled();
912 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
914 wxMenuItem
*item
= FindItem(id
);
916 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
918 item
->SetText(label
);
921 wxString
wxMenuBarBase::GetLabel(int id
) const
923 wxMenuItem
*item
= FindItem(id
);
925 wxCHECK_MSG( item
, wxEmptyString
,
926 wxT("wxMenuBar::GetLabel(): no such item") );
928 return item
->GetText();
931 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
933 wxMenuItem
*item
= FindItem(id
);
935 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
937 item
->SetHelp(helpString
);
940 wxString
wxMenuBarBase::GetHelpString(int id
) const
942 wxMenuItem
*item
= FindItem(id
);
944 wxCHECK_MSG( item
, wxEmptyString
,
945 wxT("wxMenuBar::GetHelpString(): no such item") );
947 return item
->GetHelp();
950 #endif // wxUSE_MENUS