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
;
163 else if ( current
== wxT("PGUP") ) {
166 else if ( current
== wxT("PGDN") ) {
172 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
180 // we do have something
181 return new wxAcceleratorEntry(accelFlags
, keyCode
);
185 return (wxAcceleratorEntry
*)NULL
;
188 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
190 return wxGetAccelFromString(GetText());
193 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
195 wxString text
= m_text
.BeforeFirst(wxT('\t'));
200 int flags
= accel
->GetFlags();
201 if ( flags
& wxACCEL_ALT
)
203 if ( flags
& wxACCEL_CTRL
)
204 text
+= wxT("Ctrl-");
205 if ( flags
& wxACCEL_SHIFT
)
206 text
+= wxT("Shift-");
208 int code
= accel
->GetKeyCode();
223 text
<< wxT('F') << code
- WXK_F1
+ 1;
226 // if there are any other keys wxGetAccelFromString() may return,
227 // we should process them here
230 if ( wxIsalnum(code
) )
232 text
<< (wxChar
)code
;
237 wxFAIL_MSG( wxT("unknown keyboard accel") );
244 #endif // wxUSE_ACCEL
246 // ----------------------------------------------------------------------------
247 // wxMenu ctor and dtor
248 // ----------------------------------------------------------------------------
250 void wxMenuBase::Init(long style
)
252 m_items
.DeleteContents(TRUE
);
254 m_menuBar
= (wxMenuBar
*)NULL
;
255 m_menuParent
= (wxMenu
*)NULL
;
257 m_invokingWindow
= (wxWindow
*)NULL
;
259 m_clientData
= (void *)NULL
;
260 m_eventHandler
= this;
262 #if wxUSE_MENU_CALLBACK
263 m_callback
= (wxFunction
) NULL
;
264 #endif // wxUSE_MENU_CALLBACK
267 wxMenuBase::~wxMenuBase()
269 // nothing to do, wxMenuItemList dtor will delete the menu items.
271 // Actually, in GTK, the submenus have to get deleted first.
274 // ----------------------------------------------------------------------------
275 // wxMenu item adding/removing
276 // ----------------------------------------------------------------------------
278 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
280 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
284 submenu
->Attach(m_menuBar
);
287 submenu
->SetParent((wxMenu
*)this);
290 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
292 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
294 m_items
.Append(item
);
295 if ( item
->IsSubMenu() )
297 AddSubMenu(item
->GetSubMenu());
303 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
305 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
307 if ( pos
== GetMenuItemCount() )
309 return DoAppend(item
);
313 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
314 wxT("invalid index in wxMenu::Insert") );
316 return DoInsert(pos
, item
);
320 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
322 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
324 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
325 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
327 m_items
.Insert(node
, item
);
328 if ( item
->IsSubMenu() )
330 AddSubMenu(item
->GetSubMenu());
336 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
338 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
340 return DoRemove(item
);
343 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
345 wxMenuItemList::Node
*node
= m_items
.Find(item
);
347 // if we get here, the item is valid or one of Remove() functions is broken
348 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
350 // we detach the item, but we do delete the list node (i.e. don't call
351 // DetachNode() here!)
352 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
353 m_items
.DeleteNode(node
);
355 // item isn't attached to anything any more
356 wxMenu
*submenu
= item
->GetSubMenu();
359 submenu
->SetParent((wxMenu
*)NULL
);
365 bool wxMenuBase::Delete(wxMenuItem
*item
)
367 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
369 return DoDelete(item
);
372 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
374 wxMenuItem
*item2
= DoRemove(item
);
375 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
377 // don't delete the submenu
378 item2
->SetSubMenu((wxMenu
*)NULL
);
385 bool wxMenuBase::Destroy(wxMenuItem
*item
)
387 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
389 return DoDestroy(item
);
392 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
394 wxMenuItem
*item2
= DoRemove(item
);
395 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
402 // ----------------------------------------------------------------------------
403 // wxMenu searching for items
404 // ----------------------------------------------------------------------------
406 // Finds the item id matching the given string, -1 if not found.
407 int wxMenuBase::FindItem(const wxString
& text
) const
409 wxString label
= wxMenuItem::GetLabelFromText(text
);
410 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
412 node
= node
->GetNext() )
414 wxMenuItem
*item
= node
->GetData();
415 if ( item
->IsSubMenu() )
417 int rc
= item
->GetSubMenu()->FindItem(label
);
418 if ( rc
!= wxNOT_FOUND
)
422 // we execute this code for submenus as well to alllow finding them by
423 // name just like the ordinary items
424 if ( !item
->IsSeparator() )
426 if ( item
->GetLabel() == label
)
427 return item
->GetId();
434 // recursive search for item by id
435 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
440 wxMenuItem
*item
= NULL
;
441 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
443 node
= node
->GetNext() )
445 item
= node
->GetData();
447 if ( item
->GetId() == itemId
)
450 *itemMenu
= (wxMenu
*)this;
452 else if ( item
->IsSubMenu() )
454 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
458 // don't exit the loop
466 // non recursive search
467 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
469 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
470 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
473 for ( pos
= 0; node
; pos
++ )
475 if ( node
->GetData()->GetId() == id
)
477 item
= node
->GetData();
482 node
= node
->GetNext();
487 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
493 // ----------------------------------------------------------------------------
494 // wxMenu helpers used by derived classes
495 // ----------------------------------------------------------------------------
497 // Update a menu and all submenus recursively. source is the object that has
498 // the update event handlers defined for it. If NULL, the menu or associated
499 // window will be used.
500 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
502 if ( !source
&& GetInvokingWindow() )
503 source
= GetInvokingWindow()->GetEventHandler();
505 source
= GetEventHandler();
509 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
512 wxMenuItem
* item
= node
->GetData();
513 if ( !item
->IsSeparator() )
515 wxWindowID id
= item
->GetId();
516 wxUpdateUIEvent
event(id
);
517 event
.SetEventObject( source
);
519 if ( source
->ProcessEvent(event
) )
521 // if anything changed, update the chanegd attribute
522 if (event
.GetSetText())
523 SetLabel(id
, event
.GetText());
524 if (event
.GetSetChecked())
525 Check(id
, event
.GetChecked());
526 if (event
.GetSetEnabled())
527 Enable(id
, event
.GetEnabled());
530 // recurse to the submenus
531 if ( item
->GetSubMenu() )
532 item
->GetSubMenu()->UpdateUI(source
);
534 //else: item is a separator (which don't process update UI events)
536 node
= node
->GetNext();
540 bool wxMenuBase::SendEvent(int id
, int checked
)
542 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
543 event
.SetEventObject(this);
544 event
.SetInt(checked
);
546 bool processed
= FALSE
;
548 #if wxUSE_MENU_CALLBACK
552 (void)(*(m_callback
))(*this, event
);
555 #endif // wxUSE_MENU_CALLBACK
557 // Try the menu's event handler
560 wxEvtHandler
*handler
= GetEventHandler();
562 processed
= handler
->ProcessEvent(event
);
565 // Try the window the menu was popped up from (and up through the
569 const wxMenuBase
*menu
= this;
572 wxWindow
*win
= menu
->GetInvokingWindow();
575 processed
= win
->GetEventHandler()->ProcessEvent(event
);
579 menu
= menu
->GetParent();
586 // ----------------------------------------------------------------------------
587 // wxMenu attaching/detaching to/from menu bar
588 // ----------------------------------------------------------------------------
590 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
592 // use Detach() instead!
593 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
595 // use IsAttached() to prevent this from happening
596 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
598 m_menuBar
= (wxMenuBar
*)menubar
;
601 void wxMenuBase::Detach()
603 // use IsAttached() to prevent this from happening
604 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
609 // ----------------------------------------------------------------------------
610 // wxMenu functions forwarded to wxMenuItem
611 // ----------------------------------------------------------------------------
613 void wxMenuBase::Enable( int id
, bool enable
)
615 wxMenuItem
*item
= FindItem(id
);
617 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
619 item
->Enable(enable
);
622 bool wxMenuBase::IsEnabled( int id
) const
624 wxMenuItem
*item
= FindItem(id
);
626 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
628 return item
->IsEnabled();
631 void wxMenuBase::Check( int id
, bool enable
)
633 wxMenuItem
*item
= FindItem(id
);
635 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
640 bool wxMenuBase::IsChecked( int id
) const
642 wxMenuItem
*item
= FindItem(id
);
644 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
646 return item
->IsChecked();
649 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
651 wxMenuItem
*item
= FindItem(id
);
653 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
655 item
->SetText(label
);
658 wxString
wxMenuBase::GetLabel( int id
) const
660 wxMenuItem
*item
= FindItem(id
);
662 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
664 return item
->GetText();
667 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
669 wxMenuItem
*item
= FindItem(id
);
671 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
673 item
->SetHelp( helpString
);
676 wxString
wxMenuBase::GetHelpString( int id
) const
678 wxMenuItem
*item
= FindItem(id
);
680 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
682 return item
->GetHelp();
685 // ----------------------------------------------------------------------------
686 // wxMenuBarBase ctor and dtor
687 // ----------------------------------------------------------------------------
689 wxMenuBarBase::wxMenuBarBase()
691 // we own the menus when we get them
692 m_menus
.DeleteContents(TRUE
);
695 m_menuBarFrame
= NULL
;
698 wxMenuBarBase::~wxMenuBarBase()
700 // nothing to do, the list will delete the menus because of the call to
701 // DeleteContents() above
704 // ----------------------------------------------------------------------------
705 // wxMenuBar item access: the base class versions manage m_menus list, the
706 // derived class should reflect the changes in the real menubar
707 // ----------------------------------------------------------------------------
709 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
711 wxMenuList::Node
*node
= m_menus
.Item(pos
);
712 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
714 return node
->GetData();
717 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
719 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
721 m_menus
.Append(menu
);
727 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
728 const wxString
& title
)
730 if ( pos
== m_menus
.GetCount() )
732 return wxMenuBarBase::Append(menu
, title
);
734 else // not at the end
736 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
738 wxMenuList::Node
*node
= m_menus
.Item(pos
);
739 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
741 m_menus
.Insert(node
, menu
);
748 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
749 const wxString
& WXUNUSED(title
))
751 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
753 wxMenuList::Node
*node
= m_menus
.Item(pos
);
754 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
756 wxMenu
*menuOld
= node
->GetData();
765 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
767 wxMenuList::Node
*node
= m_menus
.Item(pos
);
768 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
770 node
= m_menus
.DetachNode(node
);
771 wxCHECK( node
, NULL
); // unexpected
772 wxMenu
*menu
= node
->GetData();
780 int wxMenuBarBase::FindMenu(const wxString
& title
) const
782 wxString label
= wxMenuItem::GetLabelFromText(title
);
784 size_t count
= GetMenuCount();
785 for ( size_t i
= 0; i
< count
; i
++ )
787 wxString title2
= GetLabelTop(i
);
788 if ( (title2
== title
) ||
789 (wxMenuItem::GetLabelFromText(title2
) == label
) )
800 // ----------------------------------------------------------------------------
801 // wxMenuBar attaching/detaching to/from the frame
802 // ----------------------------------------------------------------------------
804 void wxMenuBarBase::Attach(wxFrame
*frame
)
806 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
808 m_menuBarFrame
= frame
;
811 void wxMenuBarBase::Detach()
813 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
815 m_menuBarFrame
= NULL
;
818 // ----------------------------------------------------------------------------
819 // wxMenuBar searching for items
820 // ----------------------------------------------------------------------------
822 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
827 wxMenuItem
*item
= NULL
;
828 size_t count
= GetMenuCount();
829 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
831 item
= m_menus
[i
]->FindItem(id
, menu
);
837 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
839 wxString label
= wxMenuItem::GetLabelFromText(menu
);
842 wxMenuList::Node
*node
;
843 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
845 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
846 return node
->GetData()->FindItem(item
);
852 // ---------------------------------------------------------------------------
853 // wxMenuBar functions forwarded to wxMenuItem
854 // ---------------------------------------------------------------------------
856 void wxMenuBarBase::Enable(int id
, bool enable
)
858 wxMenuItem
*item
= FindItem(id
);
860 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
862 item
->Enable(enable
);
865 void wxMenuBarBase::Check(int id
, bool check
)
867 wxMenuItem
*item
= FindItem(id
);
869 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
870 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
875 bool wxMenuBarBase::IsChecked(int id
) const
877 wxMenuItem
*item
= FindItem(id
);
879 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
881 return item
->IsChecked();
884 bool wxMenuBarBase::IsEnabled(int id
) const
886 wxMenuItem
*item
= FindItem(id
);
888 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
890 return item
->IsEnabled();
893 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
895 wxMenuItem
*item
= FindItem(id
);
897 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
899 item
->SetText(label
);
902 wxString
wxMenuBarBase::GetLabel(int id
) const
904 wxMenuItem
*item
= FindItem(id
);
906 wxCHECK_MSG( item
, wxEmptyString
,
907 wxT("wxMenuBar::GetLabel(): no such item") );
909 return item
->GetText();
912 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
914 wxMenuItem
*item
= FindItem(id
);
916 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
918 item
->SetHelp(helpString
);
921 wxString
wxMenuBarBase::GetHelpString(int id
) const
923 wxMenuItem
*item
= FindItem(id
);
925 wxCHECK_MSG( item
, wxEmptyString
,
926 wxT("wxMenuBar::GetHelpString(): no such item") );
928 return item
->GetHelp();
931 #endif // wxUSE_MENUS