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 #include "wx/stockitem.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxMenuList
)
46 WX_DEFINE_LIST(wxMenuItemList
)
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
59 static const struct wxKeyName
65 { WXK_DELETE
, wxTRANSLATE("DEL") },
66 { WXK_DELETE
, wxTRANSLATE("DELETE") },
67 { WXK_BACK
, wxTRANSLATE("BACK") },
68 { WXK_INSERT
, wxTRANSLATE("INS") },
69 { WXK_INSERT
, wxTRANSLATE("INSERT") },
70 { WXK_RETURN
, wxTRANSLATE("ENTER") },
71 { WXK_RETURN
, wxTRANSLATE("RETURN") },
72 { WXK_PAGEUP
, wxTRANSLATE("PGUP") },
73 { WXK_PAGEDOWN
, wxTRANSLATE("PGDN") },
74 { WXK_LEFT
, wxTRANSLATE("LEFT") },
75 { WXK_RIGHT
, wxTRANSLATE("RIGHT") },
76 { WXK_UP
, wxTRANSLATE("UP") },
77 { WXK_DOWN
, wxTRANSLATE("DOWN") },
78 { WXK_HOME
, wxTRANSLATE("HOME") },
79 { WXK_END
, wxTRANSLATE("END") },
80 { WXK_SPACE
, wxTRANSLATE("SPACE") },
81 { WXK_TAB
, wxTRANSLATE("TAB") },
82 { WXK_ESCAPE
, wxTRANSLATE("ESC") },
83 { WXK_ESCAPE
, wxTRANSLATE("ESCAPE") },
84 { WXK_CANCEL
, wxTRANSLATE("CANCEL") },
85 { WXK_CLEAR
, wxTRANSLATE("CLEAR") },
86 { WXK_MENU
, wxTRANSLATE("MENU") },
87 { WXK_PAUSE
, wxTRANSLATE("PAUSE") },
88 { WXK_CAPITAL
, wxTRANSLATE("CAPITAL") },
89 { WXK_SELECT
, wxTRANSLATE("SELECT") },
90 { WXK_PRINT
, wxTRANSLATE("PRINT") },
91 { WXK_EXECUTE
, wxTRANSLATE("EXECUTE") },
92 { WXK_SNAPSHOT
, wxTRANSLATE("SNAPSHOT") },
93 { WXK_HELP
, wxTRANSLATE("HELP") },
94 { WXK_ADD
, wxTRANSLATE("ADD") },
95 { WXK_SEPARATOR
, wxTRANSLATE("SEPARATOR") },
96 { WXK_SUBTRACT
, wxTRANSLATE("SUBTRACT") },
97 { WXK_DECIMAL
, wxTRANSLATE("DECIMAL") },
98 { WXK_DIVIDE
, wxTRANSLATE("DIVIDE") },
99 { WXK_NUMLOCK
, wxTRANSLATE("NUM_LOCK") },
100 { WXK_SCROLL
, wxTRANSLATE("SCROLL_LOCK") },
101 { WXK_PAGEUP
, wxTRANSLATE("PAGEUP") },
102 { WXK_PAGEDOWN
, wxTRANSLATE("PAGEDOWN") },
103 { WXK_NUMPAD_SPACE
, wxTRANSLATE("KP_SPACE") },
104 { WXK_NUMPAD_TAB
, wxTRANSLATE("KP_TAB") },
105 { WXK_NUMPAD_ENTER
, wxTRANSLATE("KP_ENTER") },
106 { WXK_NUMPAD_HOME
, wxTRANSLATE("KP_HOME") },
107 { WXK_NUMPAD_LEFT
, wxTRANSLATE("KP_LEFT") },
108 { WXK_NUMPAD_UP
, wxTRANSLATE("KP_UP") },
109 { WXK_NUMPAD_RIGHT
, wxTRANSLATE("KP_RIGHT") },
110 { WXK_NUMPAD_DOWN
, wxTRANSLATE("KP_DOWN") },
111 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PRIOR") },
112 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PAGEUP") },
113 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_NEXT") },
114 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_PAGEDOWN") },
115 { WXK_NUMPAD_END
, wxTRANSLATE("KP_END") },
116 { WXK_NUMPAD_BEGIN
, wxTRANSLATE("KP_BEGIN") },
117 { WXK_NUMPAD_INSERT
, wxTRANSLATE("KP_INSERT") },
118 { WXK_NUMPAD_DELETE
, wxTRANSLATE("KP_DELETE") },
119 { WXK_NUMPAD_EQUAL
, wxTRANSLATE("KP_EQUAL") },
120 { WXK_NUMPAD_MULTIPLY
, wxTRANSLATE("KP_MULTIPLY") },
121 { WXK_NUMPAD_ADD
, wxTRANSLATE("KP_ADD") },
122 { WXK_NUMPAD_SEPARATOR
, wxTRANSLATE("KP_SEPARATOR") },
123 { WXK_NUMPAD_SUBTRACT
, wxTRANSLATE("KP_SUBTRACT") },
124 { WXK_NUMPAD_DECIMAL
, wxTRANSLATE("KP_DECIMAL") },
125 { WXK_NUMPAD_DIVIDE
, wxTRANSLATE("KP_DIVIDE") },
126 { WXK_WINDOWS_LEFT
, wxTRANSLATE("WINDOWS_LEFT") },
127 { WXK_WINDOWS_RIGHT
, wxTRANSLATE("WINDOWS_RIGHT") },
128 { WXK_WINDOWS_MENU
, wxTRANSLATE("WINDOWS_MENU") },
129 { WXK_COMMAND
, wxTRANSLATE("COMMAND") },
132 // return true if the 2 strings refer to the same accel
134 // as accels can be either translated or not, check for both possibilities and
135 // also compare case-insensitively as the key names case doesn't count
136 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
138 return str
.CmpNoCase(accel
) == 0
140 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
145 // return prefixCode+number if the string is of the form "<prefix><number>" and
148 // first and last parameter specify the valid domain for "number" part
150 IsNumberedAccelKey(const wxString
& str
,
151 const wxChar
*prefix
,
152 wxKeyCode prefixCode
,
156 const size_t lenPrefix
= wxStrlen(prefix
);
157 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
161 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
164 if ( num
< first
|| num
> last
)
166 // this must be a mistake, chances that this is a valid name of another
167 // key are vanishingly small
168 wxLogDebug(_T("Invalid key string \"%s\""), str
.c_str());
172 return prefixCode
+ num
- first
;
177 wxAcceleratorEntry::ParseAccel(const wxString
& text
, int *flagsOut
, int *keyOut
)
179 // the parser won't like trailing spaces
180 wxString label
= text
;
181 label
.Trim(true); // the initial \t must be preserved so don't strip leading whitespaces
183 // check for accelerators: they are given after '\t'
184 int posTab
= label
.Find(wxT('\t'));
185 if ( posTab
== wxNOT_FOUND
)
190 // parse the accelerator string
191 int accelFlags
= wxACCEL_NORMAL
;
193 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.length(); n
++ )
195 if ( (label
[n
] == '+') || (label
[n
] == '-') )
197 if ( CompareAccelString(current
, wxTRANSLATE("ctrl")) )
198 accelFlags
|= wxACCEL_CTRL
;
199 else if ( CompareAccelString(current
, wxTRANSLATE("alt")) )
200 accelFlags
|= wxACCEL_ALT
;
201 else if ( CompareAccelString(current
, wxTRANSLATE("shift")) )
202 accelFlags
|= wxACCEL_SHIFT
;
203 else // not a recognized modifier name
205 // we may have "Ctrl-+", for example, but we still want to
206 // catch typos like "Crtl-A" so only give the warning if we
207 // have something before the current '+' or '-', else take
208 // it as a literal symbol
209 if ( current
.empty() )
213 // skip clearing it below
218 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
225 else // not special character
227 current
+= (wxChar
) wxTolower(label
[n
]);
232 const size_t len
= current
.length();
236 wxLogDebug(wxT("No accel key found, accel string ignored."));
240 // it's just a letter
241 keyCode
= current
[0U];
243 // if the key is used with any modifiers, make it an uppercase one
244 // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's
245 // used alone as 'a' and 'A' are different
246 if ( accelFlags
!= wxACCEL_NORMAL
)
247 keyCode
= wxToupper(keyCode
);
251 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
255 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
257 const wxKeyName
& kn
= wxKeyNames
[n
];
258 if ( CompareAccelString(current
, kn
.name
) )
267 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
270 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
271 WXK_SPECIAL1
, 1, 20);
275 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
282 wxASSERT_MSG( keyCode
, _T("logic error: should have key code here") );
285 *flagsOut
= accelFlags
;
293 wxAcceleratorEntry
*wxAcceleratorEntry::Create(const wxString
& str
)
297 if ( !ParseAccel(str
, &flags
, &keyCode
) )
300 return new wxAcceleratorEntry(flags
, keyCode
);
303 bool wxAcceleratorEntry::FromString(const wxString
& str
)
305 return ParseAccel(str
, &m_flags
, &m_keyCode
);
308 wxString
wxAcceleratorEntry::ToString() const
312 int flags
= GetFlags();
313 if ( flags
& wxACCEL_ALT
)
315 if ( flags
& wxACCEL_CTRL
)
317 if ( flags
& wxACCEL_SHIFT
)
320 const int code
= GetKeyCode();
322 if ( code
>= WXK_F1
&& code
<= WXK_F12
)
323 text
<< _("F") << code
- WXK_F1
+ 1;
324 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
325 text
<< _("KP_") << code
- WXK_NUMPAD0
;
326 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
327 text
<< _("SPECIAL") << code
- WXK_SPECIAL1
+ 1;
328 else // check the named keys
331 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
333 const wxKeyName
& kn
= wxKeyNames
[n
];
334 if ( code
== kn
.code
)
336 text
<< wxGetTranslation(kn
.name
);
341 if ( n
== WXSIZEOF(wxKeyNames
) )
343 // must be a simple key
350 text
<< (wxChar
)code
;
354 wxFAIL_MSG( wxT("unknown keyboard accelerator code") );
362 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
364 return wxAcceleratorEntry::Create(label
);
367 #endif // wxUSE_ACCEL
370 // ----------------------------------------------------------------------------
372 // ----------------------------------------------------------------------------
374 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
376 const wxString
& text
,
377 const wxString
& help
,
381 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
383 m_parentMenu
= parentMenu
;
389 if (m_id
== wxID_ANY
)
391 if (m_id
== wxID_SEPARATOR
)
392 m_kind
= wxITEM_SEPARATOR
;
398 wxMenuItemBase::~wxMenuItemBase()
405 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
407 return wxAcceleratorEntry::Create(GetText());
410 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
412 wxString text
= m_text
.BeforeFirst(wxT('\t'));
416 text
+= accel
->ToString();
422 #endif // wxUSE_ACCEL
424 void wxMenuItemBase::SetText(const wxString
& str
)
428 if ( m_text
.empty() && !IsSeparator() )
430 wxASSERT_MSG( wxIsStockID(GetId()),
431 wxT("A non-stock menu item with an empty label?") );
432 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
433 wxSTOCK_WITH_MNEMONIC
);
437 void wxMenuItemBase::SetHelp(const wxString
& str
)
441 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
443 // get a stock help string
444 m_help
= wxGetStockHelpString(GetId());
448 bool wxMenuBase::ms_locked
= true;
450 // ----------------------------------------------------------------------------
451 // wxMenu ctor and dtor
452 // ----------------------------------------------------------------------------
454 void wxMenuBase::Init(long style
)
456 m_menuBar
= (wxMenuBar
*)NULL
;
457 m_menuParent
= (wxMenu
*)NULL
;
459 m_invokingWindow
= (wxWindow
*)NULL
;
461 m_clientData
= (void *)NULL
;
462 m_eventHandler
= this;
465 wxMenuBase::~wxMenuBase()
467 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
469 // Actually, in GTK, the submenus have to get deleted first.
472 // ----------------------------------------------------------------------------
473 // wxMenu item adding/removing
474 // ----------------------------------------------------------------------------
476 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
478 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
480 submenu
->SetParent((wxMenu
*)this);
483 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
485 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
487 m_items
.Append(item
);
488 item
->SetMenu((wxMenu
*)this);
489 if ( item
->IsSubMenu() )
491 AddSubMenu(item
->GetSubMenu());
497 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
499 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
501 if ( pos
== GetMenuItemCount() )
503 return DoAppend(item
);
507 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
508 wxT("invalid index in wxMenu::Insert") );
510 return DoInsert(pos
, item
);
514 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
516 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
518 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
519 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
521 m_items
.Insert(node
, item
);
522 item
->SetMenu((wxMenu
*)this);
523 if ( item
->IsSubMenu() )
525 AddSubMenu(item
->GetSubMenu());
531 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
533 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
535 return DoRemove(item
);
538 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
540 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
542 // if we get here, the item is valid or one of Remove() functions is broken
543 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
545 // we detach the item, but we do delete the list node (i.e. don't call
546 // DetachNode() here!)
549 // item isn't attached to anything any more
550 item
->SetMenu((wxMenu
*)NULL
);
551 wxMenu
*submenu
= item
->GetSubMenu();
554 submenu
->SetParent((wxMenu
*)NULL
);
555 if ( submenu
->IsAttached() )
562 bool wxMenuBase::Delete(wxMenuItem
*item
)
564 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
566 return DoDelete(item
);
569 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
571 wxMenuItem
*item2
= DoRemove(item
);
572 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
574 // don't delete the submenu
575 item2
->SetSubMenu((wxMenu
*)NULL
);
582 bool wxMenuBase::Destroy(wxMenuItem
*item
)
584 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
586 return DoDestroy(item
);
589 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
591 wxMenuItem
*item2
= DoRemove(item
);
592 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
599 // ----------------------------------------------------------------------------
600 // wxMenu searching for items
601 // ----------------------------------------------------------------------------
603 // Finds the item id matching the given string, wxNOT_FOUND if not found.
604 int wxMenuBase::FindItem(const wxString
& text
) const
606 wxString label
= wxMenuItem::GetLabelFromText(text
);
607 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
609 node
= node
->GetNext() )
611 wxMenuItem
*item
= node
->GetData();
612 if ( item
->IsSubMenu() )
614 int rc
= item
->GetSubMenu()->FindItem(label
);
615 if ( rc
!= wxNOT_FOUND
)
619 // we execute this code for submenus as well to alllow finding them by
620 // name just like the ordinary items
621 if ( !item
->IsSeparator() )
623 if ( item
->GetLabel() == label
)
624 return item
->GetId();
631 // recursive search for item by id
632 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
637 wxMenuItem
*item
= NULL
;
638 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
640 node
= node
->GetNext() )
642 item
= node
->GetData();
644 if ( item
->GetId() == itemId
)
647 *itemMenu
= (wxMenu
*)this;
649 else if ( item
->IsSubMenu() )
651 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
655 // don't exit the loop
663 // non recursive search
664 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
666 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
667 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
670 for ( pos
= 0; node
; pos
++ )
672 if ( node
->GetData()->GetId() == id
)
674 item
= node
->GetData();
679 node
= node
->GetNext();
684 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
691 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
693 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
694 _T("wxMenu::FindItemByPosition(): invalid menu index") );
696 return m_items
.Item( position
)->GetData();
699 // ----------------------------------------------------------------------------
700 // wxMenu helpers used by derived classes
701 // ----------------------------------------------------------------------------
703 // Update a menu and all submenus recursively. source is the object that has
704 // the update event handlers defined for it. If NULL, the menu or associated
705 // window will be used.
706 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
708 if (GetInvokingWindow())
710 // Don't update menus if the parent
711 // frame is about to get deleted
712 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
713 if (tlw
&& wxPendingDelete
.Member(tlw
))
717 if ( !source
&& GetInvokingWindow() )
718 source
= GetInvokingWindow()->GetEventHandler();
720 source
= GetEventHandler();
724 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
727 wxMenuItem
* item
= node
->GetData();
728 if ( !item
->IsSeparator() )
730 wxWindowID id
= item
->GetId();
731 wxUpdateUIEvent
event(id
);
732 event
.SetEventObject( source
);
734 if ( source
->ProcessEvent(event
) )
736 // if anything changed, update the changed attribute
737 if (event
.GetSetText())
738 SetLabel(id
, event
.GetText());
739 if (event
.GetSetChecked())
740 Check(id
, event
.GetChecked());
741 if (event
.GetSetEnabled())
742 Enable(id
, event
.GetEnabled());
745 // recurse to the submenus
746 if ( item
->GetSubMenu() )
747 item
->GetSubMenu()->UpdateUI(source
);
749 //else: item is a separator (which doesn't process update UI events)
751 node
= node
->GetNext();
755 bool wxMenuBase::SendEvent(int id
, int checked
)
757 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
758 event
.SetEventObject(this);
759 event
.SetInt(checked
);
761 bool processed
= false;
763 // Try the menu's event handler
766 wxEvtHandler
*handler
= GetEventHandler();
768 processed
= handler
->ProcessEvent(event
);
771 // Try the window the menu was popped up from (and up through the
775 const wxMenuBase
*menu
= this;
778 wxWindow
*win
= menu
->GetInvokingWindow();
781 processed
= win
->GetEventHandler()->ProcessEvent(event
);
785 menu
= menu
->GetParent();
792 // ----------------------------------------------------------------------------
793 // wxMenu attaching/detaching to/from menu bar
794 // ----------------------------------------------------------------------------
796 wxMenuBar
* wxMenuBase::GetMenuBar() const
799 return GetParent()->GetMenuBar();
803 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
805 // use Detach() instead!
806 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
808 // use IsAttached() to prevent this from happening
809 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
811 m_menuBar
= (wxMenuBar
*)menubar
;
814 void wxMenuBase::Detach()
816 // use IsAttached() to prevent this from happening
817 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
822 // ----------------------------------------------------------------------------
823 // wxMenu functions forwarded to wxMenuItem
824 // ----------------------------------------------------------------------------
826 void wxMenuBase::Enable( int id
, bool enable
)
828 wxMenuItem
*item
= FindItem(id
);
830 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
832 item
->Enable(enable
);
835 bool wxMenuBase::IsEnabled( int id
) const
837 wxMenuItem
*item
= FindItem(id
);
839 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
841 return item
->IsEnabled();
844 void wxMenuBase::Check( int id
, bool enable
)
846 wxMenuItem
*item
= FindItem(id
);
848 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
853 bool wxMenuBase::IsChecked( int id
) const
855 wxMenuItem
*item
= FindItem(id
);
857 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
859 return item
->IsChecked();
862 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
864 wxMenuItem
*item
= FindItem(id
);
866 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
868 item
->SetText(label
);
871 wxString
wxMenuBase::GetLabel( int id
) const
873 wxMenuItem
*item
= FindItem(id
);
875 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
877 return item
->GetText();
880 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
882 wxMenuItem
*item
= FindItem(id
);
884 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
886 item
->SetHelp( helpString
);
889 wxString
wxMenuBase::GetHelpString( int id
) const
891 wxMenuItem
*item
= FindItem(id
);
893 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
895 return item
->GetHelp();
898 // ----------------------------------------------------------------------------
899 // wxMenuBarBase ctor and dtor
900 // ----------------------------------------------------------------------------
902 wxMenuBarBase::wxMenuBarBase()
905 m_menuBarFrame
= NULL
;
908 wxMenuBarBase::~wxMenuBarBase()
910 WX_CLEAR_LIST(wxMenuList
, m_menus
);
913 // ----------------------------------------------------------------------------
914 // wxMenuBar item access: the base class versions manage m_menus list, the
915 // derived class should reflect the changes in the real menubar
916 // ----------------------------------------------------------------------------
918 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
920 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
921 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
923 return node
->GetData();
926 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
928 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
930 m_menus
.Append(menu
);
936 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
937 const wxString
& title
)
939 if ( pos
== m_menus
.GetCount() )
941 return wxMenuBarBase::Append(menu
, title
);
943 else // not at the end
945 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
947 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
948 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
950 m_menus
.Insert(node
, menu
);
957 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
958 const wxString
& WXUNUSED(title
))
960 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
962 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
963 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
965 wxMenu
*menuOld
= node
->GetData();
974 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
976 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
977 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
979 wxMenu
*menu
= node
->GetData();
986 int wxMenuBarBase::FindMenu(const wxString
& title
) const
988 wxString label
= wxMenuItem::GetLabelFromText(title
);
990 size_t count
= GetMenuCount();
991 for ( size_t i
= 0; i
< count
; i
++ )
993 wxString title2
= GetLabelTop(i
);
994 if ( (title2
== title
) ||
995 (wxMenuItem::GetLabelFromText(title2
) == label
) )
1006 // ----------------------------------------------------------------------------
1007 // wxMenuBar attaching/detaching to/from the frame
1008 // ----------------------------------------------------------------------------
1010 void wxMenuBarBase::Attach(wxFrame
*frame
)
1012 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
1014 m_menuBarFrame
= frame
;
1017 void wxMenuBarBase::Detach()
1019 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
1021 m_menuBarFrame
= NULL
;
1024 // ----------------------------------------------------------------------------
1025 // wxMenuBar searching for items
1026 // ----------------------------------------------------------------------------
1028 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
1033 wxMenuItem
*item
= NULL
;
1034 size_t count
= GetMenuCount(), i
;
1035 wxMenuList::const_iterator it
;
1036 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
1038 item
= (*it
)->FindItem(id
, menu
);
1044 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
1046 wxString label
= wxMenuItem::GetLabelFromText(menu
);
1049 wxMenuList::compatibility_iterator node
;
1050 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
1052 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
1053 return node
->GetData()->FindItem(item
);
1059 // ---------------------------------------------------------------------------
1060 // wxMenuBar functions forwarded to wxMenuItem
1061 // ---------------------------------------------------------------------------
1063 void wxMenuBarBase::Enable(int id
, bool enable
)
1065 wxMenuItem
*item
= FindItem(id
);
1067 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
1069 item
->Enable(enable
);
1072 void wxMenuBarBase::Check(int id
, bool check
)
1074 wxMenuItem
*item
= FindItem(id
);
1076 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
1077 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
1082 bool wxMenuBarBase::IsChecked(int id
) const
1084 wxMenuItem
*item
= FindItem(id
);
1086 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
1088 return item
->IsChecked();
1091 bool wxMenuBarBase::IsEnabled(int id
) const
1093 wxMenuItem
*item
= FindItem(id
);
1095 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1097 return item
->IsEnabled();
1100 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1102 wxMenuItem
*item
= FindItem(id
);
1104 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1106 item
->SetText(label
);
1109 wxString
wxMenuBarBase::GetLabel(int id
) const
1111 wxMenuItem
*item
= FindItem(id
);
1113 wxCHECK_MSG( item
, wxEmptyString
,
1114 wxT("wxMenuBar::GetLabel(): no such item") );
1116 return item
->GetText();
1119 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1121 wxMenuItem
*item
= FindItem(id
);
1123 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1125 item
->SetHelp(helpString
);
1128 wxString
wxMenuBarBase::GetHelpString(int id
) const
1130 wxMenuItem
*item
= FindItem(id
);
1132 wxCHECK_MSG( item
, wxEmptyString
,
1133 wxT("wxMenuBar::GetHelpString(): no such item") );
1135 return item
->GetHelp();
1138 void wxMenuBarBase::UpdateMenus( void )
1140 wxEvtHandler
* source
;
1142 int nCount
= GetMenuCount();
1143 for (int n
= 0; n
< nCount
; n
++)
1145 menu
= GetMenu( n
);
1148 source
= menu
->GetEventHandler();
1150 menu
->UpdateUI( source
);
1155 #endif // wxUSE_MENUS