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 ( wxIsalnum(code
) )
323 text
<< (wxChar
)code
;
324 else if ( code
>= WXK_F1
&& code
<= WXK_F12
)
325 text
<< _("F") << code
- WXK_F1
+ 1;
326 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
327 text
<< _("KP_") << code
- WXK_NUMPAD0
;
328 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
329 text
<< _("SPECIAL") << code
- WXK_SPECIAL1
+ 1;
330 else // check the named keys
333 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
335 const wxKeyName
& kn
= wxKeyNames
[n
];
336 if ( code
== kn
.code
)
338 text
<< wxGetTranslation(kn
.name
);
343 wxASSERT_MSG( n
!= WXSIZEOF(wxKeyNames
),
344 wxT("unknown keyboard accelerator code") );
350 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
352 return wxAcceleratorEntry::Create(label
);
355 #endif // wxUSE_ACCEL
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 wxMenuItemBase::wxMenuItemBase(wxMenu
*parentMenu
,
364 const wxString
& text
,
365 const wxString
& help
,
369 wxASSERT_MSG( parentMenu
!= NULL
, wxT("menuitem should have a menu") );
371 m_parentMenu
= parentMenu
;
377 if (m_id
== wxID_ANY
)
378 m_id
= wxWindow::NewControlId();
379 if (m_id
== wxID_SEPARATOR
)
380 m_kind
= wxITEM_SEPARATOR
;
386 wxMenuItemBase::~wxMenuItemBase()
393 wxAcceleratorEntry
*wxMenuItemBase::GetAccel() const
395 return wxAcceleratorEntry::Create(GetText());
398 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
400 wxString text
= m_text
.BeforeFirst(wxT('\t'));
404 text
+= accel
->ToString();
410 #endif // wxUSE_ACCEL
412 void wxMenuItemBase::SetText(const wxString
& str
)
416 if ( m_text
.empty() && !IsSeparator() )
418 wxASSERT_MSG( wxIsStockID(GetId()),
419 wxT("A non-stock menu item with an empty label?") );
420 m_text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
421 wxSTOCK_WITH_MNEMONIC
);
425 void wxMenuItemBase::SetHelp(const wxString
& str
)
429 if ( m_help
.empty() && !IsSeparator() && wxIsStockID(GetId()) )
431 // get a stock help string
432 m_help
= wxGetStockHelpString(GetId());
436 bool wxMenuBase::ms_locked
= true;
438 // ----------------------------------------------------------------------------
439 // wxMenu ctor and dtor
440 // ----------------------------------------------------------------------------
442 void wxMenuBase::Init(long style
)
444 m_menuBar
= (wxMenuBar
*)NULL
;
445 m_menuParent
= (wxMenu
*)NULL
;
447 m_invokingWindow
= (wxWindow
*)NULL
;
449 m_clientData
= (void *)NULL
;
450 m_eventHandler
= this;
453 wxMenuBase::~wxMenuBase()
455 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
457 // Actually, in GTK, the submenus have to get deleted first.
460 // ----------------------------------------------------------------------------
461 // wxMenu item adding/removing
462 // ----------------------------------------------------------------------------
464 void wxMenuBase::AddSubMenu(wxMenu
*submenu
)
466 wxCHECK_RET( submenu
, _T("can't add a NULL submenu") );
468 submenu
->SetParent((wxMenu
*)this);
471 wxMenuItem
* wxMenuBase::DoAppend(wxMenuItem
*item
)
473 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Append()") );
475 m_items
.Append(item
);
476 item
->SetMenu((wxMenu
*)this);
477 if ( item
->IsSubMenu() )
479 AddSubMenu(item
->GetSubMenu());
485 wxMenuItem
* wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
487 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert") );
489 if ( pos
== GetMenuItemCount() )
491 return DoAppend(item
);
495 wxCHECK_MSG( pos
< GetMenuItemCount(), NULL
,
496 wxT("invalid index in wxMenu::Insert") );
498 return DoInsert(pos
, item
);
502 wxMenuItem
* wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
504 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Insert()") );
506 wxMenuItemList::compatibility_iterator node
= m_items
.Item(pos
);
507 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxMenu::Insert()") );
509 m_items
.Insert(node
, item
);
510 item
->SetMenu((wxMenu
*)this);
511 if ( item
->IsSubMenu() )
513 AddSubMenu(item
->GetSubMenu());
519 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
521 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
523 return DoRemove(item
);
526 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
528 wxMenuItemList::compatibility_iterator node
= m_items
.Find(item
);
530 // if we get here, the item is valid or one of Remove() functions is broken
531 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
533 // we detach the item, but we do delete the list node (i.e. don't call
534 // DetachNode() here!)
537 // item isn't attached to anything any more
538 item
->SetMenu((wxMenu
*)NULL
);
539 wxMenu
*submenu
= item
->GetSubMenu();
542 submenu
->SetParent((wxMenu
*)NULL
);
543 if ( submenu
->IsAttached() )
550 bool wxMenuBase::Delete(wxMenuItem
*item
)
552 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Delete") );
554 return DoDelete(item
);
557 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
559 wxMenuItem
*item2
= DoRemove(item
);
560 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
562 // don't delete the submenu
563 item2
->SetSubMenu((wxMenu
*)NULL
);
570 bool wxMenuBase::Destroy(wxMenuItem
*item
)
572 wxCHECK_MSG( item
, false, wxT("invalid item in wxMenu::Destroy") );
574 return DoDestroy(item
);
577 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
579 wxMenuItem
*item2
= DoRemove(item
);
580 wxCHECK_MSG( item2
, false, wxT("failed to delete menu item") );
587 // ----------------------------------------------------------------------------
588 // wxMenu searching for items
589 // ----------------------------------------------------------------------------
591 // Finds the item id matching the given string, wxNOT_FOUND if not found.
592 int wxMenuBase::FindItem(const wxString
& text
) const
594 wxString label
= wxMenuItem::GetLabelFromText(text
);
595 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
597 node
= node
->GetNext() )
599 wxMenuItem
*item
= node
->GetData();
600 if ( item
->IsSubMenu() )
602 int rc
= item
->GetSubMenu()->FindItem(label
);
603 if ( rc
!= wxNOT_FOUND
)
607 // we execute this code for submenus as well to alllow finding them by
608 // name just like the ordinary items
609 if ( !item
->IsSeparator() )
611 if ( item
->GetLabel() == label
)
612 return item
->GetId();
619 // recursive search for item by id
620 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
625 wxMenuItem
*item
= NULL
;
626 for ( wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
628 node
= node
->GetNext() )
630 item
= node
->GetData();
632 if ( item
->GetId() == itemId
)
635 *itemMenu
= (wxMenu
*)this;
637 else if ( item
->IsSubMenu() )
639 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
643 // don't exit the loop
651 // non recursive search
652 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
654 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
655 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
658 for ( pos
= 0; node
; pos
++ )
660 if ( node
->GetData()->GetId() == id
)
662 item
= node
->GetData();
667 node
= node
->GetNext();
672 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
679 wxMenuItem
* wxMenuBase::FindItemByPosition(size_t position
) const
681 wxCHECK_MSG( position
< m_items
.GetCount(), NULL
,
682 _T("wxMenu::FindItemByPosition(): invalid menu index") );
684 return m_items
.Item( position
)->GetData();
687 // ----------------------------------------------------------------------------
688 // wxMenu helpers used by derived classes
689 // ----------------------------------------------------------------------------
691 // Update a menu and all submenus recursively. source is the object that has
692 // the update event handlers defined for it. If NULL, the menu or associated
693 // window will be used.
694 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
696 if (GetInvokingWindow())
698 // Don't update menus if the parent
699 // frame is about to get deleted
700 wxWindow
*tlw
= wxGetTopLevelParent( GetInvokingWindow() );
701 if (tlw
&& wxPendingDelete
.Member(tlw
))
705 if ( !source
&& GetInvokingWindow() )
706 source
= GetInvokingWindow()->GetEventHandler();
708 source
= GetEventHandler();
712 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
715 wxMenuItem
* item
= node
->GetData();
716 if ( !item
->IsSeparator() )
718 wxWindowID id
= item
->GetId();
719 wxUpdateUIEvent
event(id
);
720 event
.SetEventObject( source
);
722 if ( source
->ProcessEvent(event
) )
724 // if anything changed, update the changed attribute
725 if (event
.GetSetText())
726 SetLabel(id
, event
.GetText());
727 if (event
.GetSetChecked())
728 Check(id
, event
.GetChecked());
729 if (event
.GetSetEnabled())
730 Enable(id
, event
.GetEnabled());
733 // recurse to the submenus
734 if ( item
->GetSubMenu() )
735 item
->GetSubMenu()->UpdateUI(source
);
737 //else: item is a separator (which doesn't process update UI events)
739 node
= node
->GetNext();
743 bool wxMenuBase::SendEvent(int id
, int checked
)
745 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
746 event
.SetEventObject(this);
747 event
.SetInt(checked
);
749 bool processed
= false;
751 // Try the menu's event handler
754 wxEvtHandler
*handler
= GetEventHandler();
756 processed
= handler
->ProcessEvent(event
);
759 // Try the window the menu was popped up from (and up through the
763 const wxMenuBase
*menu
= this;
766 wxWindow
*win
= menu
->GetInvokingWindow();
769 processed
= win
->GetEventHandler()->ProcessEvent(event
);
773 menu
= menu
->GetParent();
780 // ----------------------------------------------------------------------------
781 // wxMenu attaching/detaching to/from menu bar
782 // ----------------------------------------------------------------------------
784 wxMenuBar
* wxMenuBase::GetMenuBar() const
787 return GetParent()->GetMenuBar();
791 void wxMenuBase::Attach(wxMenuBarBase
*menubar
)
793 // use Detach() instead!
794 wxASSERT_MSG( menubar
, _T("menu can't be attached to NULL menubar") );
796 // use IsAttached() to prevent this from happening
797 wxASSERT_MSG( !m_menuBar
, _T("attaching menu twice?") );
799 m_menuBar
= (wxMenuBar
*)menubar
;
802 void wxMenuBase::Detach()
804 // use IsAttached() to prevent this from happening
805 wxASSERT_MSG( m_menuBar
, _T("detaching unattached menu?") );
810 // ----------------------------------------------------------------------------
811 // wxMenu functions forwarded to wxMenuItem
812 // ----------------------------------------------------------------------------
814 void wxMenuBase::Enable( int id
, bool enable
)
816 wxMenuItem
*item
= FindItem(id
);
818 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
820 item
->Enable(enable
);
823 bool wxMenuBase::IsEnabled( int id
) const
825 wxMenuItem
*item
= FindItem(id
);
827 wxCHECK_MSG( item
, false, wxT("wxMenu::IsEnabled: no such item") );
829 return item
->IsEnabled();
832 void wxMenuBase::Check( int id
, bool enable
)
834 wxMenuItem
*item
= FindItem(id
);
836 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
841 bool wxMenuBase::IsChecked( int id
) const
843 wxMenuItem
*item
= FindItem(id
);
845 wxCHECK_MSG( item
, false, wxT("wxMenu::IsChecked: no such item") );
847 return item
->IsChecked();
850 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
852 wxMenuItem
*item
= FindItem(id
);
854 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
856 item
->SetText(label
);
859 wxString
wxMenuBase::GetLabel( int id
) const
861 wxMenuItem
*item
= FindItem(id
);
863 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetLabel: no such item") );
865 return item
->GetText();
868 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
870 wxMenuItem
*item
= FindItem(id
);
872 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
874 item
->SetHelp( helpString
);
877 wxString
wxMenuBase::GetHelpString( int id
) const
879 wxMenuItem
*item
= FindItem(id
);
881 wxCHECK_MSG( item
, wxEmptyString
, wxT("wxMenu::GetHelpString: no such item") );
883 return item
->GetHelp();
886 // ----------------------------------------------------------------------------
887 // wxMenuBarBase ctor and dtor
888 // ----------------------------------------------------------------------------
890 wxMenuBarBase::wxMenuBarBase()
893 m_menuBarFrame
= NULL
;
896 wxMenuBarBase::~wxMenuBarBase()
898 WX_CLEAR_LIST(wxMenuList
, m_menus
);
901 // ----------------------------------------------------------------------------
902 // wxMenuBar item access: the base class versions manage m_menus list, the
903 // derived class should reflect the changes in the real menubar
904 // ----------------------------------------------------------------------------
906 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
908 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
909 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
911 return node
->GetData();
914 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
916 wxCHECK_MSG( menu
, false, wxT("can't append NULL menu") );
918 m_menus
.Append(menu
);
924 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
925 const wxString
& title
)
927 if ( pos
== m_menus
.GetCount() )
929 return wxMenuBarBase::Append(menu
, title
);
931 else // not at the end
933 wxCHECK_MSG( menu
, false, wxT("can't insert NULL menu") );
935 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
936 wxCHECK_MSG( node
, false, wxT("bad index in wxMenuBar::Insert()") );
938 m_menus
.Insert(node
, menu
);
945 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
946 const wxString
& WXUNUSED(title
))
948 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
950 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
951 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
953 wxMenu
*menuOld
= node
->GetData();
962 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
964 wxMenuList::compatibility_iterator node
= m_menus
.Item(pos
);
965 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
967 wxMenu
*menu
= node
->GetData();
974 int wxMenuBarBase::FindMenu(const wxString
& title
) const
976 wxString label
= wxMenuItem::GetLabelFromText(title
);
978 size_t count
= GetMenuCount();
979 for ( size_t i
= 0; i
< count
; i
++ )
981 wxString title2
= GetLabelTop(i
);
982 if ( (title2
== title
) ||
983 (wxMenuItem::GetLabelFromText(title2
) == label
) )
994 // ----------------------------------------------------------------------------
995 // wxMenuBar attaching/detaching to/from the frame
996 // ----------------------------------------------------------------------------
998 void wxMenuBarBase::Attach(wxFrame
*frame
)
1000 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
1002 m_menuBarFrame
= frame
;
1005 void wxMenuBarBase::Detach()
1007 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
1009 m_menuBarFrame
= NULL
;
1012 // ----------------------------------------------------------------------------
1013 // wxMenuBar searching for items
1014 // ----------------------------------------------------------------------------
1016 wxMenuItem
*wxMenuBarBase::FindItem(int id
, wxMenu
**menu
) const
1021 wxMenuItem
*item
= NULL
;
1022 size_t count
= GetMenuCount(), i
;
1023 wxMenuList::const_iterator it
;
1024 for ( i
= 0, it
= m_menus
.begin(); !item
&& (i
< count
); i
++, it
++ )
1026 item
= (*it
)->FindItem(id
, menu
);
1032 int wxMenuBarBase::FindMenuItem(const wxString
& menu
, const wxString
& item
) const
1034 wxString label
= wxMenuItem::GetLabelFromText(menu
);
1037 wxMenuList::compatibility_iterator node
;
1038 for ( node
= m_menus
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
1040 if ( label
== wxMenuItem::GetLabelFromText(GetLabelTop(i
)) )
1041 return node
->GetData()->FindItem(item
);
1047 // ---------------------------------------------------------------------------
1048 // wxMenuBar functions forwarded to wxMenuItem
1049 // ---------------------------------------------------------------------------
1051 void wxMenuBarBase::Enable(int id
, bool enable
)
1053 wxMenuItem
*item
= FindItem(id
);
1055 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
1057 item
->Enable(enable
);
1060 void wxMenuBarBase::Check(int id
, bool check
)
1062 wxMenuItem
*item
= FindItem(id
);
1064 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
1065 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
1070 bool wxMenuBarBase::IsChecked(int id
) const
1072 wxMenuItem
*item
= FindItem(id
);
1074 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsChecked(): no such item") );
1076 return item
->IsChecked();
1079 bool wxMenuBarBase::IsEnabled(int id
) const
1081 wxMenuItem
*item
= FindItem(id
);
1083 wxCHECK_MSG( item
, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1085 return item
->IsEnabled();
1088 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
1090 wxMenuItem
*item
= FindItem(id
);
1092 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
1094 item
->SetText(label
);
1097 wxString
wxMenuBarBase::GetLabel(int id
) const
1099 wxMenuItem
*item
= FindItem(id
);
1101 wxCHECK_MSG( item
, wxEmptyString
,
1102 wxT("wxMenuBar::GetLabel(): no such item") );
1104 return item
->GetText();
1107 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
1109 wxMenuItem
*item
= FindItem(id
);
1111 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
1113 item
->SetHelp(helpString
);
1116 wxString
wxMenuBarBase::GetHelpString(int id
) const
1118 wxMenuItem
*item
= FindItem(id
);
1120 wxCHECK_MSG( item
, wxEmptyString
,
1121 wxT("wxMenuBar::GetHelpString(): no such item") );
1123 return item
->GetHelp();
1126 void wxMenuBarBase::UpdateMenus( void )
1128 wxEvtHandler
* source
;
1130 int nCount
= GetMenuCount();
1131 for (int n
= 0; n
< nCount
; n
++)
1133 menu
= GetMenu( n
);
1136 source
= menu
->GetEventHandler();
1138 menu
->UpdateUI( source
);
1143 #endif // wxUSE_MENUS