1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_MENUITEM_H_BASE_
12 #define _WX_MENUITEM_H_BASE_
18 // ----------------------------------------------------------------------------
20 // ----------------------------------------------------------------------------
22 #include "wx/object.h" // base class
24 // ----------------------------------------------------------------------------
25 // forward declarations
26 // ----------------------------------------------------------------------------
28 class WXDLLIMPEXP_FWD_CORE wxAcceleratorEntry
;
29 class WXDLLIMPEXP_FWD_CORE wxMenuItem
;
30 class WXDLLIMPEXP_FWD_CORE wxMenu
;
32 // ----------------------------------------------------------------------------
33 // wxMenuItem is an item in the menu which may be either a normal item, a sub
34 // menu or a separator
35 // ----------------------------------------------------------------------------
37 class WXDLLIMPEXP_CORE wxMenuItemBase
: public wxObject
41 static wxMenuItem
*New(wxMenu
*parentMenu
= NULL
,
42 int itemid
= wxID_SEPARATOR
,
43 const wxString
& text
= wxEmptyString
,
44 const wxString
& help
= wxEmptyString
,
45 wxItemKind kind
= wxITEM_NORMAL
,
46 wxMenu
*subMenu
= NULL
);
48 // destruction: wxMenuItem will delete its submenu
49 virtual ~wxMenuItemBase();
52 wxMenu
*GetMenu() const { return m_parentMenu
; }
53 void SetMenu(wxMenu
* menu
) { m_parentMenu
= menu
; }
56 void SetId(int itemid
) { m_id
= itemid
; }
57 int GetId() const { return m_id
; }
59 // the item's text (or name)
61 // NB: the item's label includes the accelerators and mnemonics info (if
62 // any), i.e. it may contain '&' or '_' or "\t..." and thus is
63 // different from the item's text which only contains the text shown
64 // in the menu. This used to be called SetText.
65 virtual void SetItemLabel(const wxString
& str
);
67 // return the item label including any mnemonics and accelerators.
68 // This used to be called GetText.
69 virtual wxString
GetItemLabel() const { return m_text
; }
71 // return just the text of the item label, without any mnemonics
72 // This used to be called GetLabel.
73 virtual wxString
GetItemLabelText() const { return GetLabelText(m_text
); }
75 // return just the text part of the given label (implemented in platform-specific code)
76 // This used to be called GetLabelFromText.
77 static wxString
GetLabelText(const wxString
& label
);
79 // what kind of menu item we are
80 wxItemKind
GetKind() const { return m_kind
; }
81 void SetKind(wxItemKind kind
) { m_kind
= kind
; }
82 bool IsSeparator() const { return m_kind
== wxITEM_SEPARATOR
; }
84 bool IsCheck() const { return m_kind
== wxITEM_CHECK
; }
85 bool IsRadio() const { return m_kind
== wxITEM_RADIO
; }
87 virtual void SetCheckable(bool checkable
)
88 { m_kind
= checkable
? wxITEM_CHECK
: wxITEM_NORMAL
; }
90 // Notice that this doesn't quite match SetCheckable().
91 bool IsCheckable() const
92 { return m_kind
== wxITEM_CHECK
|| m_kind
== wxITEM_RADIO
; }
94 bool IsSubMenu() const { return m_subMenu
!= NULL
; }
95 void SetSubMenu(wxMenu
*menu
) { m_subMenu
= menu
; }
96 wxMenu
*GetSubMenu() const { return m_subMenu
; }
99 virtual void Enable(bool enable
= true) { m_isEnabled
= enable
; }
100 virtual bool IsEnabled() const { return m_isEnabled
; }
102 virtual void Check(bool check
= true) { m_isChecked
= check
; }
103 virtual bool IsChecked() const { return m_isChecked
; }
104 void Toggle() { Check(!m_isChecked
); }
106 // help string (displayed in the status bar by default)
107 void SetHelp(const wxString
& str
);
108 const wxString
& GetHelp() const { return m_help
; }
111 // extract the accelerator from the given menu string, return NULL if none
113 static wxAcceleratorEntry
*GetAccelFromString(const wxString
& label
);
115 // get our accelerator or NULL (caller must delete the pointer)
116 virtual wxAcceleratorEntry
*GetAccel() const;
118 // set the accel for this item - this may also be done indirectly with
120 virtual void SetAccel(wxAcceleratorEntry
*accel
);
121 #endif // wxUSE_ACCEL
123 #if WXWIN_COMPATIBILITY_2_8
124 // compatibility only, use new functions in the new code
125 wxDEPRECATED( void SetName(const wxString
& str
) );
126 wxDEPRECATED( wxString
GetName() const );
128 // Now use GetItemLabelText
129 wxDEPRECATED( wxString
GetLabel() const ) ;
131 // Now use GetItemLabel
132 wxDEPRECATED( const wxString
& GetText() const );
134 // Now use GetLabelText to strip the accelerators
135 static wxDEPRECATED( wxString
GetLabelFromText(const wxString
& text
) );
137 // Now use SetItemLabel
138 wxDEPRECATED( virtual void SetText(const wxString
& str
) );
139 #endif // WXWIN_COMPATIBILITY_2_8
141 static wxMenuItem
*New(wxMenu
*parentMenu
,
143 const wxString
& text
,
144 const wxString
& help
,
146 wxMenu
*subMenu
= NULL
)
148 return New(parentMenu
, itemid
, text
, help
,
149 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
);
153 wxWindowIDRef m_id
; // numeric id of the item >= 0 or wxID_ANY or wxID_SEPARATOR
154 wxMenu
*m_parentMenu
, // the menu we belong to
155 *m_subMenu
; // our sub menu or NULL
156 wxString m_text
, // label of the item
157 m_help
; // the help string for the item
158 wxItemKind m_kind
; // separator/normal/check/radio item?
159 bool m_isChecked
; // is checked?
160 bool m_isEnabled
; // is enabled?
162 // this ctor is for the derived classes only, we're never created directly
163 wxMenuItemBase(wxMenu
*parentMenu
= NULL
,
164 int itemid
= wxID_SEPARATOR
,
165 const wxString
& text
= wxEmptyString
,
166 const wxString
& help
= wxEmptyString
,
167 wxItemKind kind
= wxITEM_NORMAL
,
168 wxMenu
*subMenu
= NULL
);
171 // and, if we have one ctor, compiler won't generate a default copy one, so
172 // declare them ourselves - but don't implement as they shouldn't be used
173 wxMenuItemBase(const wxMenuItemBase
& item
);
174 wxMenuItemBase
& operator=(const wxMenuItemBase
& item
);
177 #if WXWIN_COMPATIBILITY_2_8
178 inline void wxMenuItemBase::SetName(const wxString
&str
)
179 { SetItemLabel(str
); }
180 inline wxString
wxMenuItemBase::GetName() const
181 { return GetItemLabel(); }
182 inline wxString
wxMenuItemBase::GetLabel() const
183 { return GetLabelText(m_text
); }
184 inline const wxString
& wxMenuItemBase::GetText() const { return m_text
; }
185 inline void wxMenuItemBase::SetText(const wxString
& text
) { SetItemLabel(text
); }
186 #endif // WXWIN_COMPATIBILITY_2_8
188 // ----------------------------------------------------------------------------
189 // include the real class declaration
190 // ----------------------------------------------------------------------------
192 #ifdef wxUSE_BASE_CLASSES_ONLY
193 #define wxMenuItem wxMenuItemBase
194 #else // !wxUSE_BASE_CLASSES_ONLY
195 #if defined(__WXUNIVERSAL__)
196 #include "wx/univ/menuitem.h"
197 #elif defined(__WXMSW__)
198 #include "wx/msw/menuitem.h"
199 #elif defined(__WXMOTIF__)
200 #include "wx/motif/menuitem.h"
201 #elif defined(__WXGTK20__)
202 #include "wx/gtk/menuitem.h"
203 #elif defined(__WXGTK__)
204 #include "wx/gtk1/menuitem.h"
205 #elif defined(__WXMAC__)
206 #include "wx/osx/menuitem.h"
207 #elif defined(__WXCOCOA__)
208 #include "wx/cocoa/menuitem.h"
209 #elif defined(__WXPM__)
210 #include "wx/os2/menuitem.h"
212 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
214 #endif // wxUSE_MENUS
217 // _WX_MENUITEM_H_BASE_