1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/menucmn.cpp
3 // Purpose: wxMenu and wxMenuBar methods common to all ports
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxMenuList
)
44 WX_DEFINE_LIST(wxMenuItemList
)
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 static const struct wxKeyName
63 { WXK_DELETE
, wxTRANSLATE("DEL") },
64 { WXK_DELETE
, wxTRANSLATE("DELETE") },
65 { WXK_BACK
, wxTRANSLATE("BACK") },
66 { WXK_INSERT
, wxTRANSLATE("INS") },
67 { WXK_INSERT
, wxTRANSLATE("INSERT") },
68 { WXK_RETURN
, wxTRANSLATE("ENTER") },
69 { WXK_RETURN
, wxTRANSLATE("RETURN") },
70 { WXK_PAGEUP
, wxTRANSLATE("PGUP") },
71 { WXK_PAGEDOWN
, wxTRANSLATE("PGDN") },
72 { WXK_LEFT
, wxTRANSLATE("LEFT") },
73 { WXK_RIGHT
, wxTRANSLATE("RIGHT") },
74 { WXK_UP
, wxTRANSLATE("UP") },
75 { WXK_DOWN
, wxTRANSLATE("DOWN") },
76 { WXK_HOME
, wxTRANSLATE("HOME") },
77 { WXK_END
, wxTRANSLATE("END") },
78 { WXK_SPACE
, wxTRANSLATE("SPACE") },
79 { WXK_TAB
, wxTRANSLATE("TAB") },
80 { WXK_ESCAPE
, wxTRANSLATE("ESC") },
81 { WXK_ESCAPE
, wxTRANSLATE("ESCAPE") },
82 { WXK_CANCEL
, wxTRANSLATE("CANCEL") },
83 { WXK_CLEAR
, wxTRANSLATE("CLEAR") },
84 { WXK_MENU
, wxTRANSLATE("MENU") },
85 { WXK_PAUSE
, wxTRANSLATE("PAUSE") },
86 { WXK_CAPITAL
, wxTRANSLATE("CAPITAL") },
87 { WXK_SELECT
, wxTRANSLATE("SELECT") },
88 { WXK_PRINT
, wxTRANSLATE("PRINT") },
89 { WXK_EXECUTE
, wxTRANSLATE("EXECUTE") },
90 { WXK_SNAPSHOT
, wxTRANSLATE("SNAPSHOT") },
91 { WXK_HELP
, wxTRANSLATE("HELP") },
92 { WXK_ADD
, wxTRANSLATE("ADD") },
93 { WXK_SEPARATOR
, wxTRANSLATE("SEPARATOR") },
94 { WXK_SUBTRACT
, wxTRANSLATE("SUBTRACT") },
95 { WXK_DECIMAL
, wxTRANSLATE("DECIMAL") },
96 { WXK_DIVIDE
, wxTRANSLATE("DIVIDE") },
97 { WXK_NUMLOCK
, wxTRANSLATE("NUM_LOCK") },
98 { WXK_SCROLL
, wxTRANSLATE("SCROLL_LOCK") },
99 { WXK_PAGEUP
, wxTRANSLATE("PAGEUP") },
100 { WXK_PAGEDOWN
, wxTRANSLATE("PAGEDOWN") },
101 { WXK_NUMPAD_SPACE
, wxTRANSLATE("KP_SPACE") },
102 { WXK_NUMPAD_TAB
, wxTRANSLATE("KP_TAB") },
103 { WXK_NUMPAD_ENTER
, wxTRANSLATE("KP_ENTER") },
104 { WXK_NUMPAD_HOME
, wxTRANSLATE("KP_HOME") },
105 { WXK_NUMPAD_LEFT
, wxTRANSLATE("KP_LEFT") },
106 { WXK_NUMPAD_UP
, wxTRANSLATE("KP_UP") },
107 { WXK_NUMPAD_RIGHT
, wxTRANSLATE("KP_RIGHT") },
108 { WXK_NUMPAD_DOWN
, wxTRANSLATE("KP_DOWN") },
109 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PRIOR") },
110 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PAGEUP") },
111 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_NEXT") },
112 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_PAGEDOWN") },
113 { WXK_NUMPAD_END
, wxTRANSLATE("KP_END") },
114 { WXK_NUMPAD_BEGIN
, wxTRANSLATE("KP_BEGIN") },
115 { WXK_NUMPAD_INSERT
, wxTRANSLATE("KP_INSERT") },
116 { WXK_NUMPAD_DELETE
, wxTRANSLATE("KP_DELETE") },
117 { WXK_NUMPAD_EQUAL
, wxTRANSLATE("KP_EQUAL") },
118 { WXK_NUMPAD_MULTIPLY
, wxTRANSLATE("KP_MULTIPLY") },
119 { WXK_NUMPAD_ADD
, wxTRANSLATE("KP_ADD") },
120 { WXK_NUMPAD_SEPARATOR
, wxTRANSLATE("KP_SEPARATOR") },
121 { WXK_NUMPAD_SUBTRACT
, wxTRANSLATE("KP_SUBTRACT") },
122 { WXK_NUMPAD_DECIMAL
, wxTRANSLATE("KP_DECIMAL") },
123 { WXK_NUMPAD_DIVIDE
, wxTRANSLATE("KP_DIVIDE") },
124 { WXK_WINDOWS_LEFT
, wxTRANSLATE("WINDOWS_LEFT") },
125 { WXK_WINDOWS_RIGHT
, wxTRANSLATE("WINDOWS_RIGHT") },
126 { WXK_WINDOWS_MENU
, wxTRANSLATE("WINDOWS_MENU") },
127 { WXK_COMMAND
, wxTRANSLATE("COMMAND") },
130 // return true if the 2 strings refer to the same accel
132 // as accels can be either translated or not, check for both possibilities and
133 // also compare case-insensitively as the key names case doesn't count
134 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
136 return str
.CmpNoCase(accel
) == 0
138 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
143 // return prefixCode+number if the string is of the form "<prefix><number>" and
146 // first and last parameter specify the valid domain for "number" part
148 IsNumberedAccelKey(const wxString
& str
,
149 const wxChar
*prefix
,
150 wxKeyCode prefixCode
,
154 const size_t lenPrefix
= wxStrlen(prefix
);
155 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
159 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
162 if ( num
< first
|| num
> last
)
164 // this must be a mistake, chances that this is a valid name of another
165 // key are vanishingly small
166 wxLogDebug(_T("Invalid key string \"%s\""), str
.c_str());
170 return prefixCode
+ num
- first
;
175 wxAcceleratorEntry::ParseAccel(const wxString
& text
, int *flagsOut
, int *keyOut
)
177 // the parser won't like leading/trailing spaces
178 wxString label
= text
.Strip(wxString::both
);
180 // check for accelerators: they are given after '\t'
181 int posTab
= label
.Find(wxT('\t'));
182 if ( posTab
== wxNOT_FOUND
)
185 // parse the accelerator string
186 int accelFlags
= wxACCEL_NORMAL
;
188 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.length(); n
++ )
190 if ( (label
[n
] == '+') || (label
[n
] == '-') )
192 if ( CompareAccelString(current
, wxTRANSLATE("ctrl")) )
193 accelFlags
|= wxACCEL_CTRL
;
194 else if ( CompareAccelString(current
, wxTRANSLATE("alt")) )
195 accelFlags
|= wxACCEL_ALT
;
196 else if ( CompareAccelString(current
, wxTRANSLATE("shift")) )
197 accelFlags
|= wxACCEL_SHIFT
;
198 else // not a recognized modifier name
200 // we may have "Ctrl-+", for example, but we still want to
201 // catch typos like "Crtl-A" so only give the warning if we
202 // have something before the current '+' or '-', else take
203 // it as a literal symbol
204 if ( current
.empty() )
208 // skip clearing it below
213 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
220 else // not special character
222 current
+= (wxChar
) wxTolower(label
[n
]);
227 const size_t len
= current
.length();
231 wxLogDebug(wxT("No accel key found, accel string ignored."));
235 // it's just a letter
236 keyCode
= current
[0U];
238 // if the key is used with any modifiers, make it an uppercase one
239 // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's
240 // used alone as 'a' and 'A' are different
241 if ( accelFlags
!= wxACCEL_NORMAL
)
242 keyCode
= wxToupper(keyCode
);
246 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
250 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
252 const wxKeyName
& kn
= wxKeyNames
[n
];
253 if ( CompareAccelString(current
, kn
.name
) )
262 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
265 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
266 WXK_SPECIAL1
, 1, 20);
270 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
277 wxASSERT_MSG( keyCode
, _T("logic error: should have key code here") );
280 *flagsOut
= accelFlags
;
288 wxAcceleratorEntry
*wxAcceleratorEntry::Create(const wxString
& str
)
292 if ( !ParseAccel(str
, &flags
, &keyCode
) )
295 return new wxAcceleratorEntry(flags
, keyCode
);
298 bool wxAcceleratorEntry::FromString(const wxString
& str
)
300 return ParseAccel(str
, &m_flags
, &m_keyCode
);
303 wxString
wxAcceleratorEntry::ToString() const
307 int flags
= GetFlags();
308 if ( flags
& wxACCEL_ALT
)
310 if ( flags
& wxACCEL_CTRL
)
312 if ( flags
& wxACCEL_SHIFT
)
315 const int code
= GetKeyCode();
317 if ( wxIsalnum(code
) )
318 text
<< (wxChar
)code
;
319 else if ( code
>= WXK_F1
&& code
<= WXK_F12
)
320 text
<< _("F") << code
- WXK_F1
+ 1;
321 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
322 text
<< _("KP_") << code
- WXK_NUMPAD0
;
323 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
324 text
<< _("SPECIAL") << code
- WXK_SPECIAL1
+ 1;
325 else // check the named keys
328 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
330 const wxKeyName
& kn
= wxKeyNames
[n
];
331 if ( code
== kn
.code
)
333 text
<< wxGetTranslation(kn
.name
);
338 wxASSERT_MSG( n
!= WXSIZEOF(wxKeyNames
),
339 wxT("unknown keyboard accelerator code") );
345 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
347 return wxAcceleratorEntry::Create(label
);
350 #endif // wxUSE_ACCEL
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
359 const wxString
& text
,
360 const wxString
& help
,
366 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
368 m_parentMenu
= parentMenu
;
374 if (m_id
== wxID_ANY
)
376 if (m_id
== wxID_SEPARATOR
)
377 m_kind
= wxITEM_SEPARATOR
;
380 wxMenuItemBase::~wxMenuItemBase()
387 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
389 return wxAcceleratorEntry::Create(GetText());
392 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
394 wxString text
= m_text
.BeforeFirst(wxT('\t'));
398 text
+= accel
->ToString();
404 #endif // wxUSE_ACCEL
406 bool wxMenuBase::ms_locked
= true;
408 // ----------------------------------------------------------------------------
409 // wxMenu ctor and dtor
410 // ----------------------------------------------------------------------------
412 void wxMenuBase::Init(long style
)
414 m_menuBar
= (wxMenuBar
*)NULL
;
415 m_menuParent
= (wxMenu
*)NULL
;
417 m_invokingWindow
= (wxWindow
*)NULL
;
419 m_clientData
= (void *)NULL
;
420 m_eventHandler
= this;
423 wxMenuBase::~wxMenuBase()
425 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
427 // Actually, in GTK, the submenus have to get deleted first.
430 // ----------------------------------------------------------------------------
431 // wxMenu item adding/removing
432 // ----------------------------------------------------------------------------
434 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
436 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
438 submenu
->SetParent((wxMenu
*)this);
441 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
443 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
445 m_items
.Append(item
);
446 item
->SetMenu((wxMenu
*)this);
447 if ( item
->IsSubMenu() )
449 AddSubMenu(item
->GetSubMenu());
455 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
457 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
459 if ( pos
== GetMenuItemCount() )
461 return DoAppend(item
);
465 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
466 wxT("invalid index in wxMenu::Insert") );
468 return DoInsert(pos
, item
);
472 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
474 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
476 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
477 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
479 m_items
.Insert(node
, item
);
480 item
->SetMenu((wxMenu
*)this);
481 if ( item
->IsSubMenu() )
483 AddSubMenu(item
->GetSubMenu());
489 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
491 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
493 return DoRemove(item
);
496 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
498 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
500 // if we get here, the item is valid or one of Remove() functions is broken
501 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
503 // we detach the item, but we do delete the list node (i.e. don't call
504 // DetachNode() here!)
507 // item isn't attached to anything any more
508 item
->SetMenu((wxMenu
*)NULL
);
509 wxMenu
*submenu
= item
->GetSubMenu();
512 submenu
->SetParent((wxMenu
*)NULL
);
513 if ( submenu
->IsAttached() )
520 bool wxMenuBase::Delete(wxMenuItem
*item
)
522 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
524 return DoDelete(item
);
527 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
529 wxMenuItem
*item2
= DoRemove(item
);
530 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
532 // don't delete the submenu
533 item2
->SetSubMenu((wxMenu
*)NULL
);
540 bool wxMenuBase::Destroy(wxMenuItem
*item
)
542 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
544 return DoDestroy(item
);
547 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
549 wxMenuItem
*item2
= DoRemove(item
);
550 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
557 // ----------------------------------------------------------------------------
558 // wxMenu searching for items
559 // ----------------------------------------------------------------------------
561 // Finds the item id matching the given string, wxNOT_FOUND if not found.
562 int wxMenuBase::FindItem(const wxString
& text
) const
564 wxString label
= wxMenuItem::GetLabelFromText(text
);
565 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
567 node
= node
->GetNext() )
569 wxMenuItem
*item
= node
->GetData();
570 if ( item
->IsSubMenu() )
572 int rc
= item
->GetSubMenu()->FindItem(label
);
573 if ( rc
!= wxNOT_FOUND
)
577 // we execute this code for submenus as well to alllow finding them by
578 // name just like the ordinary items
579 if ( !item
->IsSeparator() )
581 if ( item
->GetLabel() == label
)
582 return item
->GetId();
589 // recursive search for item by id
590 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
595 wxMenuItem
*item
= NULL
;
596 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
598 node
= node
->GetNext() )
600 item
= node
->GetData();
602 if ( item
->GetId() == itemId
)
605 *itemMenu
= (wxMenu
*)this;
607 else if ( item
->IsSubMenu() )
609 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
613 // don't exit the loop
621 // non recursive search
622 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
624 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
625 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
628 for ( pos
= 0; node
; pos
++ )
630 if ( node
->GetData()->GetId() == id
)
632 item
= node
->GetData();
637 node
= node
->GetNext();
642 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
649 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
651 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
652 _T("wxMenu::FindItemByPosition(): invalid menu index") );
654 return m_items
.Item( position
)->GetData();
657 // ----------------------------------------------------------------------------
658 // wxMenu helpers used by derived classes
659 // ----------------------------------------------------------------------------
661 // Update a menu and all submenus recursively. source is the object that has
662 // the update event handlers defined for it. If NULL, the menu or associated
663 // window will be used.
664 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
666 if (GetInvokingWindow())
668 // Don't update menus if the parent
669 // frame is about to get deleted
670 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
671 if (tlw
&& wxPendingDelete
.Member(tlw
))
675 if ( !source
&& GetInvokingWindow() )
676 source
= GetInvokingWindow()->GetEventHandler();
678 source
= GetEventHandler();
682 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
685 wxMenuItem
* item
= node
->GetData();
686 if ( !item
->IsSeparator() )
688 wxWindowID id
= item
->GetId();
689 wxUpdateUIEvent
event(id
);
690 event
.SetEventObject( source
);
692 if ( source
->ProcessEvent(event
) )
694 // if anything changed, update the changed attribute
695 if (event
.GetSetText())
696 SetLabel(id
, event
.GetText());
697 if (event
.GetSetChecked())
698 Check(id
, event
.GetChecked());
699 if (event
.GetSetEnabled())
700 Enable(id
, event
.GetEnabled());
703 // recurse to the submenus
704 if ( item
->GetSubMenu() )
705 item
->GetSubMenu()->UpdateUI(source
);
707 //else: item is a separator (which doesn't process update UI events)
709 node
= node
->GetNext();
713 bool wxMenuBase::SendEvent(int id
, int checked
)
715 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
716 event
.SetEventObject(this);
717 event
.SetInt(checked
);
719 bool processed
= false;
721 // Try the menu's event handler
724 wxEvtHandler
*handler
= GetEventHandler();
726 processed
= handler
->ProcessEvent(event
);
729 // Try the window the menu was popped up from (and up through the
733 const wxMenuBase
*menu
= this;
736 wxWindow
*win
= menu
->GetInvokingWindow();
739 processed
= win
->GetEventHandler()->ProcessEvent(event
);
743 menu
= menu
->GetParent();
750 // ----------------------------------------------------------------------------
751 // wxMenu attaching/detaching to/from menu bar
752 // ----------------------------------------------------------------------------
754 wxMenuBar
* wxMenuBase::GetMenuBar() const
757 return GetParent()->GetMenuBar();
761 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
763 // use Detach() instead!
764 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
766 // use IsAttached() to prevent this from happening
767 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
769 m_menuBar
= (wxMenuBar
*)menubar
;
772 void wxMenuBase::Detach()
774 // use IsAttached() to prevent this from happening
775 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
780 // ----------------------------------------------------------------------------
781 // wxMenu functions forwarded to wxMenuItem
782 // ----------------------------------------------------------------------------
784 void wxMenuBase::Enable( int id
, bool enable
)
786 wxMenuItem
*item
= FindItem(id
);
788 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
790 item
->Enable(enable
);
793 bool wxMenuBase::IsEnabled( int id
) const
795 wxMenuItem
*item
= FindItem(id
);
797 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
799 return item
->IsEnabled();
802 void wxMenuBase::Check( int id
, bool enable
)
804 wxMenuItem
*item
= FindItem(id
);
806 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
811 bool wxMenuBase::IsChecked( int id
) const
813 wxMenuItem
*item
= FindItem(id
);
815 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
817 return item
->IsChecked();
820 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
822 wxMenuItem
*item
= FindItem(id
);
824 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
826 item
->SetText(label
);
829 wxString
wxMenuBase::GetLabel( int id
) const
831 wxMenuItem
*item
= FindItem(id
);
833 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
835 return item
->GetText();
838 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
840 wxMenuItem
*item
= FindItem(id
);
842 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
844 item
->SetHelp( helpString
);
847 wxString
wxMenuBase::GetHelpString( int id
) const
849 wxMenuItem
*item
= FindItem(id
);
851 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
853 return item
->GetHelp();
856 // ----------------------------------------------------------------------------
857 // wxMenuBarBase ctor and dtor
858 // ----------------------------------------------------------------------------
860 wxMenuBarBase::wxMenuBarBase()
863 m_menuBarFrame
= NULL
;
866 wxMenuBarBase::~wxMenuBarBase()
868 WX_CLEAR_LIST(wxMenuList
, m_menus
);
871 // ----------------------------------------------------------------------------
872 // wxMenuBar item access: the base class versions manage m_menus list, the
873 // derived class should reflect the changes in the real menubar
874 // ----------------------------------------------------------------------------
876 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
878 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
879 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
881 return node
->GetData();
884 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
886 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
888 m_menus
.Append(menu
);
894 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
895 const wxString
& title
)
897 if ( pos
== m_menus
.GetCount() )
899 return wxMenuBarBase::Append(menu
, title
);
901 else // not at the end
903 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
905 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
906 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
908 m_menus
.Insert(node
, menu
);
915 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
916 const wxString
& WXUNUSED(title
))
918 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
920 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
921 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
923 wxMenu
*menuOld
= node
->GetData();
932 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
934 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
935 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
937 wxMenu
*menu
= node
->GetData();
944 int wxMenuBarBase::FindMenu(const wxString
& title
) const
946 wxString label
= wxMenuItem::GetLabelFromText(title
);
948 size_t count
= GetMenuCount();
949 for ( size_t i
= 0; i
< count
; i
++ )
951 wxString title2
= GetLabelTop(i
);
952 if ( (title2
== title
) ||
953 (wxMenuItem::GetLabelFromText(title2
) == label
) )
964 // ----------------------------------------------------------------------------
965 // wxMenuBar attaching/detaching to/from the frame
966 // ----------------------------------------------------------------------------
968 void wxMenuBarBase::Attach(wxFrame
*frame
)
970 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
972 m_menuBarFrame
= frame
;
975 void wxMenuBarBase::Detach()
977 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
979 m_menuBarFrame
= NULL
;
982 // ----------------------------------------------------------------------------
983 // wxMenuBar searching for items
984 // ----------------------------------------------------------------------------
986 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
991 wxMenuItem
*item
= NULL
;
992 size_t count
= GetMenuCount(), i
;
993 wxMenuList::const_iterator it
;
994 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
996 item
= (*it
)->FindItem(id
, menu
);
1002 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
1004 wxString label
= wxMenuItem::GetLabelFromText(menu
);
1007 wxMenuList::compatibility_iterator node
;
1008 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
1010 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
1011 return node
->GetData()->FindItem(item
);
1017 // ---------------------------------------------------------------------------
1018 // wxMenuBar functions forwarded to wxMenuItem
1019 // ---------------------------------------------------------------------------
1021 void wxMenuBarBase::Enable(int id
, bool enable
)
1023 wxMenuItem
*item
= FindItem(id
);
1025 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
1027 item
->Enable(enable
);
1030 void wxMenuBarBase::Check(int id
, bool check
)
1032 wxMenuItem
*item
= FindItem(id
);
1034 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
1035 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
1040 bool wxMenuBarBase::IsChecked(int id
) const
1042 wxMenuItem
*item
= FindItem(id
);
1044 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
1046 return item
->IsChecked();
1049 bool wxMenuBarBase::IsEnabled(int id
) const
1051 wxMenuItem
*item
= FindItem(id
);
1053 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1055 return item
->IsEnabled();
1058 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1060 wxMenuItem
*item
= FindItem(id
);
1062 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1064 item
->SetText(label
);
1067 wxString
wxMenuBarBase::GetLabel(int id
) const
1069 wxMenuItem
*item
= FindItem(id
);
1071 wxCHECK_MSG( item
, wxEmptyString
,
1072 wxT("wxMenuBar::GetLabel(): no such item") );
1074 return item
->GetText();
1077 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1079 wxMenuItem
*item
= FindItem(id
);
1081 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1083 item
->SetHelp(helpString
);
1086 wxString
wxMenuBarBase::GetHelpString(int id
) const
1088 wxMenuItem
*item
= FindItem(id
);
1090 wxCHECK_MSG( item
, wxEmptyString
,
1091 wxT("wxMenuBar::GetHelpString(): no such item") );
1093 return item
->GetHelp();
1096 void wxMenuBarBase::UpdateMenus( void )
1098 wxEvtHandler
* source
;
1100 int nCount
= GetMenuCount();
1101 for (int n
= 0; n
< nCount
; n
++)
1103 menu
= GetMenu( n
);
1106 source
= menu
->GetEventHandler();
1108 menu
->UpdateUI( source
);
1113 #endif // wxUSE_MENUS