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()
65 // return wxAcceleratorEntry for the given menu string or NULL if none
67 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
69 // check for accelerators: they are given after '\t'
70 int posTab
= label
.Find(wxT('\t'));
71 if ( posTab
!= wxNOT_FOUND
) {
72 // parse the accelerator string
74 int accelFlags
= wxACCEL_NORMAL
;
76 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.Len(); n
++ ) {
77 if ( (label
[n
] == '+') || (label
[n
] == '-') ) {
78 if ( current
== _("ctrl") )
79 accelFlags
|= wxACCEL_CTRL
;
80 else if ( current
== _("alt") )
81 accelFlags
|= wxACCEL_ALT
;
82 else if ( current
== _("shift") )
83 accelFlags
|= wxACCEL_SHIFT
;
85 // we may have "Ctrl-+", for example, but we still want to
86 // catch typos like "Crtl-A" so only give the warning if we
87 // have something before the current '+' or '-', else take
88 // it as a literal symbol
89 if ( current
.empty() )
93 // skip clearing it below
98 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
106 current
+= wxTolower(label
[n
]);
110 if ( current
.IsEmpty() ) {
111 wxLogDebug(wxT("No accel key found, accel string ignored."));
114 if ( current
.Len() == 1 ) {
116 keyCode
= wxToupper(current
[0U]);
119 // is it a function key?
120 if ( current
[0U] == 'f' && isdigit(current
[1U]) &&
121 (current
.Len() == 2 ||
122 (current
.Len() == 3 && isdigit(current
[2U]))) ) {
124 wxSscanf(current
.c_str() + 1, wxT("%d"), &n
);
126 keyCode
= WXK_F1
+ n
- 1;
129 // several special cases
131 if ( current
== wxT("DEL") ) {
132 keyCode
= WXK_DELETE
;
134 else if ( current
== wxT("DELETE") ) {
135 keyCode
= WXK_DELETE
;
137 else if ( current
== wxT("INS") ) {
138 keyCode
= WXK_INSERT
;
140 else if ( current
== wxT("INSERT") ) {
141 keyCode
= WXK_INSERT
;
144 else if ( current
== wxT("PGUP") ) {
147 else if ( current
== wxT("PGDN") ) {
153 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
161 // we do have something
162 return new wxAcceleratorEntry(accelFlags
, keyCode
);
166 return (wxAcceleratorEntry
*)NULL
;
169 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
171 return wxGetAccelFromString(GetText());
174 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
176 wxString text
= m_text
.BeforeFirst(wxT('\t'));
181 int flags
= accel
->GetFlags();
182 if ( flags
& wxACCEL_ALT
)
184 if ( flags
& wxACCEL_CTRL
)
185 text
+= wxT("Ctrl-");
186 if ( flags
& wxACCEL_SHIFT
)
187 text
+= wxT("Shift-");
189 int code
= accel
->GetKeyCode();
204 text
<< wxT('F') << code
- WXK_F1
+ 1;
207 // if there are any other keys wxGetAccelFromString() may return,
208 // we should process them here
211 if ( wxIsalnum(code
) )
213 text
<< (wxChar
)code
;
218 wxFAIL_MSG( wxT("unknown keyboard accel") );
225 #endif // wxUSE_ACCEL
227 // ----------------------------------------------------------------------------
228 // wxMenu ctor and dtor
229 // ----------------------------------------------------------------------------
231 void wxMenuBase::Init(long style
)
233 m_items
.DeleteContents(TRUE
);
235 m_menuBar
= (wxMenuBar
*)NULL
;
236 m_menuParent
= (wxMenu
*)NULL
;
238 m_invokingWindow
= (wxWindow
*)NULL
;
240 m_clientData
= (void *)NULL
;
241 m_eventHandler
= this;
243 #if wxUSE_MENU_CALLBACK
244 m_callback
= (wxFunction
) NULL
;
245 #endif // wxUSE_MENU_CALLBACK
248 wxMenuBase::~wxMenuBase()
250 // nothing to do, wxMenuItemList dtor will delete the menu items.
252 // Actually, in GTK, the submenus have to get deleted first.
255 // ----------------------------------------------------------------------------
256 // wxMenu item adding/removing
257 // ----------------------------------------------------------------------------
259 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
261 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
265 submenu
->Attach(m_menuBar
);
268 submenu
->SetParent((wxMenu
*)this);
271 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
273 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
275 m_items
.Append(item
);
276 if ( item
->IsSubMenu() )
278 AddSubMenu(item
->GetSubMenu());
284 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
286 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
288 if ( pos
== GetMenuItemCount() )
290 return DoAppend(item
);
294 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
295 wxT("invalid index in wxMenu::Insert") );
297 return DoInsert(pos
, item
);
301 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
303 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
305 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
306 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
308 m_items
.Insert(node
, item
);
309 if ( item
->IsSubMenu() )
311 AddSubMenu(item
->GetSubMenu());
317 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
319 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
321 return DoRemove(item
);
324 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
326 wxMenuItemList::Node
*node
= m_items
.Find(item
);
328 // if we get here, the item is valid or one of Remove() functions is broken
329 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
331 // we detach the item, but we do delete the list node (i.e. don't call
332 // DetachNode() here!)
333 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
334 m_items
.DeleteNode(node
);
336 // item isn't attached to anything any more
337 wxMenu
*submenu
= item
->GetSubMenu();
340 submenu
->SetParent((wxMenu
*)NULL
);
346 bool wxMenuBase::Delete(wxMenuItem
*item
)
348 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
350 return DoDelete(item
);
353 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
355 wxMenuItem
*item2
= DoRemove(item
);
356 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
358 // don't delete the submenu
359 item2
->SetSubMenu((wxMenu
*)NULL
);
366 bool wxMenuBase::Destroy(wxMenuItem
*item
)
368 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
370 return DoDestroy(item
);
373 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
375 wxMenuItem
*item2
= DoRemove(item
);
376 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
383 // ----------------------------------------------------------------------------
384 // wxMenu searching for items
385 // ----------------------------------------------------------------------------
387 // Finds the item id matching the given string, -1 if not found.
388 int wxMenuBase::FindItem(const wxString
& text
) const
390 wxString label
= wxMenuItem::GetLabelFromText(text
);
391 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
393 node
= node
->GetNext() )
395 wxMenuItem
*item
= node
->GetData();
396 if ( item
->IsSubMenu() )
398 int rc
= item
->GetSubMenu()->FindItem(label
);
399 if ( rc
!= wxNOT_FOUND
)
403 // we execute this code for submenus as well to alllow finding them by
404 // name just like the ordinary items
405 if ( !item
->IsSeparator() )
407 if ( item
->GetLabel() == label
)
408 return item
->GetId();
415 // recursive search for item by id
416 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
421 wxMenuItem
*item
= NULL
;
422 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
424 node
= node
->GetNext() )
426 item
= node
->GetData();
428 if ( item
->GetId() == itemId
)
431 *itemMenu
= (wxMenu
*)this;
433 else if ( item
->IsSubMenu() )
435 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
439 // don't exit the loop
447 // non recursive search
448 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
450 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
451 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
454 for ( pos
= 0; node
; pos
++ )
456 if ( node
->GetData()->GetId() == id
)
458 item
= node
->GetData();
463 node
= node
->GetNext();
468 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
474 // ----------------------------------------------------------------------------
475 // wxMenu helpers used by derived classes
476 // ----------------------------------------------------------------------------
478 // Update a menu and all submenus recursively. source is the object that has
479 // the update event handlers defined for it. If NULL, the menu or associated
480 // window will be used.
481 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
483 if ( !source
&& GetInvokingWindow() )
484 source
= GetInvokingWindow()->GetEventHandler();
486 source
= GetEventHandler();
490 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
493 wxMenuItem
* item
= node
->GetData();
494 if ( !item
->IsSeparator() )
496 wxWindowID id
= item
->GetId();
497 wxUpdateUIEvent
event(id
);
498 event
.SetEventObject( source
);
500 if ( source
->ProcessEvent(event
) )
502 // if anything changed, update the chanegd attribute
503 if (event
.GetSetText())
504 SetLabel(id
, event
.GetText());
505 if (event
.GetSetChecked())
506 Check(id
, event
.GetChecked());
507 if (event
.GetSetEnabled())
508 Enable(id
, event
.GetEnabled());
511 // recurse to the submenus
512 if ( item
->GetSubMenu() )
513 item
->GetSubMenu()->UpdateUI(source
);
515 //else: item is a separator (which don't process update UI events)
517 node
= node
->GetNext();
521 bool wxMenuBase::SendEvent(int id
, int checked
)
523 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
524 event
.SetEventObject(this);
525 event
.SetInt(checked
);
527 bool processed
= FALSE
;
529 #if wxUSE_MENU_CALLBACK
533 (void)(*(m_callback
))(*this, event
);
536 #endif // wxUSE_MENU_CALLBACK
538 // Try the menu's event handler
541 wxEvtHandler
*handler
= GetEventHandler();
543 processed
= handler
->ProcessEvent(event
);
546 // Try the window the menu was popped up from (and up through the
550 const wxMenuBase
*menu
= this;
553 wxWindow
*win
= menu
->GetInvokingWindow();
556 processed
= win
->GetEventHandler()->ProcessEvent(event
);
560 menu
= menu
->GetParent();
567 // ----------------------------------------------------------------------------
568 // wxMenu attaching/detaching to/from menu bar
569 // ----------------------------------------------------------------------------
571 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
573 // use Detach() instead!
574 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
576 // use IsAttached() to prevent this from happening
577 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
579 m_menuBar
= (wxMenuBar
*)menubar
;
582 void wxMenuBase::Detach()
584 // use IsAttached() to prevent this from happening
585 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
590 // ----------------------------------------------------------------------------
591 // wxMenu functions forwarded to wxMenuItem
592 // ----------------------------------------------------------------------------
594 void wxMenuBase::Enable( int id
, bool enable
)
596 wxMenuItem
*item
= FindItem(id
);
598 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
600 item
->Enable(enable
);
603 bool wxMenuBase::IsEnabled( int id
) const
605 wxMenuItem
*item
= FindItem(id
);
607 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
609 return item
->IsEnabled();
612 void wxMenuBase::Check( int id
, bool enable
)
614 wxMenuItem
*item
= FindItem(id
);
616 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
621 bool wxMenuBase::IsChecked( int id
) const
623 wxMenuItem
*item
= FindItem(id
);
625 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
627 return item
->IsChecked();
630 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
632 wxMenuItem
*item
= FindItem(id
);
634 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
636 item
->SetText(label
);
639 wxString
wxMenuBase::GetLabel( int id
) const
641 wxMenuItem
*item
= FindItem(id
);
643 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
645 return item
->GetText();
648 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
650 wxMenuItem
*item
= FindItem(id
);
652 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
654 item
->SetHelp( helpString
);
657 wxString
wxMenuBase::GetHelpString( int id
) const
659 wxMenuItem
*item
= FindItem(id
);
661 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
663 return item
->GetHelp();
666 // ----------------------------------------------------------------------------
667 // wxMenuBarBase ctor and dtor
668 // ----------------------------------------------------------------------------
670 wxMenuBarBase::wxMenuBarBase()
672 // we own the menus when we get them
673 m_menus
.DeleteContents(TRUE
);
676 m_menuBarFrame
= NULL
;
679 wxMenuBarBase::~wxMenuBarBase()
681 // nothing to do, the list will delete the menus because of the call to
682 // DeleteContents() above
685 // ----------------------------------------------------------------------------
686 // wxMenuBar item access: the base class versions manage m_menus list, the
687 // derived class should reflect the changes in the real menubar
688 // ----------------------------------------------------------------------------
690 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
692 wxMenuList::Node
*node
= m_menus
.Item(pos
);
693 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
695 return node
->GetData();
698 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
700 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
702 m_menus
.Append(menu
);
708 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
709 const wxString
& title
)
711 if ( pos
== m_menus
.GetCount() )
713 return wxMenuBarBase::Append(menu
, title
);
715 else // not at the end
717 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
719 wxMenuList::Node
*node
= m_menus
.Item(pos
);
720 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
722 m_menus
.Insert(node
, menu
);
729 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
730 const wxString
& WXUNUSED(title
))
732 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
734 wxMenuList::Node
*node
= m_menus
.Item(pos
);
735 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
737 wxMenu
*menuOld
= node
->GetData();
746 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
748 wxMenuList::Node
*node
= m_menus
.Item(pos
);
749 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
751 node
= m_menus
.DetachNode(node
);
752 wxCHECK( node
, NULL
); // unexpected
753 wxMenu
*menu
= node
->GetData();
761 int wxMenuBarBase::FindMenu(const wxString
& title
) const
763 wxString label
= wxMenuItem::GetLabelFromText(title
);
765 size_t count
= GetMenuCount();
766 for ( size_t i
= 0; i
< count
; i
++ )
768 wxString title2
= GetLabelTop(i
);
769 if ( (title2
== title
) ||
770 (wxMenuItem::GetLabelFromText(title2
) == label
) )
781 // ----------------------------------------------------------------------------
782 // wxMenuBar attaching/detaching to/from the frame
783 // ----------------------------------------------------------------------------
785 void wxMenuBarBase::Attach(wxFrame
*frame
)
787 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
789 m_menuBarFrame
= frame
;
792 void wxMenuBarBase::Detach()
794 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
796 m_menuBarFrame
= NULL
;
799 // ----------------------------------------------------------------------------
800 // wxMenuBar searching for items
801 // ----------------------------------------------------------------------------
803 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
808 wxMenuItem
*item
= NULL
;
809 size_t count
= GetMenuCount();
810 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
812 item
= m_menus
[i
]->FindItem(id
, menu
);
818 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
820 wxString label
= wxMenuItem::GetLabelFromText(menu
);
823 wxMenuList::Node
*node
;
824 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
826 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
827 return node
->GetData()->FindItem(item
);
833 // ---------------------------------------------------------------------------
834 // wxMenuBar functions forwarded to wxMenuItem
835 // ---------------------------------------------------------------------------
837 void wxMenuBarBase::Enable(int id
, bool enable
)
839 wxMenuItem
*item
= FindItem(id
);
841 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
843 item
->Enable(enable
);
846 void wxMenuBarBase::Check(int id
, bool check
)
848 wxMenuItem
*item
= FindItem(id
);
850 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
851 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
856 bool wxMenuBarBase::IsChecked(int id
) const
858 wxMenuItem
*item
= FindItem(id
);
860 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
862 return item
->IsChecked();
865 bool wxMenuBarBase::IsEnabled(int id
) const
867 wxMenuItem
*item
= FindItem(id
);
869 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
871 return item
->IsEnabled();
874 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
876 wxMenuItem
*item
= FindItem(id
);
878 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
880 item
->SetText(label
);
883 wxString
wxMenuBarBase::GetLabel(int id
) const
885 wxMenuItem
*item
= FindItem(id
);
887 wxCHECK_MSG( item
, wxEmptyString
,
888 wxT("wxMenuBar::GetLabel(): no such item") );
890 return item
->GetText();
893 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
895 wxMenuItem
*item
= FindItem(id
);
897 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
899 item
->SetHelp(helpString
);
902 wxString
wxMenuBarBase::GetHelpString(int id
) const
904 wxMenuItem
*item
= FindItem(id
);
906 wxCHECK_MSG( item
, wxEmptyString
,
907 wxT("wxMenuBar::GetHelpString(): no such item") );
909 return item
->GetHelp();
912 #endif // wxUSE_MENUS