Ensure that a wxMenuItem's parent menu is updated if it is removed
[wxWidgets.git] / include / wx / menuitem.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/menuitem.h
3 // Purpose: wxMenuItem class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 25.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_MENUITEM_H_BASE_
13 #define _WX_MENUITEM_H_BASE_
14
15 #if wxUSE_MENUS
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/object.h" // base class
22
23 // ----------------------------------------------------------------------------
24 // forward declarations
25 // ----------------------------------------------------------------------------
26
27 class WXDLLEXPORT wxAcceleratorEntry;
28 class WXDLLEXPORT wxMenuItem;
29 class WXDLLEXPORT wxMenu;
30
31 // ----------------------------------------------------------------------------
32 // wxMenuItem is an item in the menu which may be either a normal item, a sub
33 // menu or a separator
34 // ----------------------------------------------------------------------------
35
36 class WXDLLEXPORT wxMenuItemBase : public wxObject
37 {
38 public:
39 // creation
40 static wxMenuItem *New(wxMenu *parentMenu = (wxMenu *)NULL,
41 int itemid = wxID_SEPARATOR,
42 const wxString& text = wxEmptyString,
43 const wxString& help = wxEmptyString,
44 wxItemKind kind = wxITEM_NORMAL,
45 wxMenu *subMenu = (wxMenu *)NULL);
46
47 // destruction: wxMenuItem will delete its submenu
48 virtual ~wxMenuItemBase();
49
50 // the menu we're in
51 wxMenu *GetMenu() const { return m_parentMenu; }
52 void SetMenu(wxMenu* menu) { m_parentMenu = menu; }
53
54 // get/set id
55 void SetId(int itemid) { m_id = itemid; }
56 int GetId() const { return m_id; }
57 bool IsSeparator() const { return m_id == wxID_SEPARATOR; }
58
59 // the item's text (or name)
60 //
61 // NB: the item's text 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 label which only contains the text shown
64 // in the menu
65 virtual void SetText(const wxString& str) { m_text = str; }
66 wxString GetLabel() const { return GetLabelFromText(m_text); }
67 const wxString& GetText() const { return m_text; }
68
69 // get the label from text (implemented in platform-specific code)
70 static wxString GetLabelFromText(const wxString& text);
71
72 // what kind of menu item we are
73 wxItemKind GetKind() const { return m_kind; }
74
75 virtual void SetCheckable(bool checkable) { m_kind = checkable ? wxITEM_CHECK : wxITEM_NORMAL; }
76 bool IsCheckable() const
77 { return m_kind == wxITEM_CHECK || m_kind == wxITEM_RADIO; }
78
79 bool IsSubMenu() const { return m_subMenu != NULL; }
80 void SetSubMenu(wxMenu *menu) { m_subMenu = menu; }
81 wxMenu *GetSubMenu() const { return m_subMenu; }
82
83 // state
84 virtual void Enable(bool enable = TRUE) { m_isEnabled = enable; }
85 virtual bool IsEnabled() const { return m_isEnabled; }
86
87 virtual void Check(bool check = TRUE) { m_isChecked = check; }
88 virtual bool IsChecked() const { return m_isChecked; }
89 void Toggle() { Check(!m_isChecked); }
90
91 // help string (displayed in the status bar by default)
92 void SetHelp(const wxString& str) { m_help = str; }
93 const wxString& GetHelp() const { return m_help; }
94
95 #if wxUSE_ACCEL
96 // extract the accelerator from the given menu string, return NULL if none
97 // found
98 static wxAcceleratorEntry *GetAccelFromString(const wxString& label);
99
100 // get our accelerator or NULL (caller must delete the pointer)
101 virtual wxAcceleratorEntry *GetAccel() const;
102
103 // set the accel for this item - this may also be done indirectly with
104 // SetText()
105 virtual void SetAccel(wxAcceleratorEntry *accel);
106 #endif // wxUSE_ACCEL
107
108 // compatibility only, use new functions in the new code
109 void SetName(const wxString& str) { SetText(str); }
110 const wxString& GetName() const { return GetText(); }
111
112 static wxMenuItem *New(wxMenu *parentMenu,
113 int itemid,
114 const wxString& text,
115 const wxString& help,
116 bool isCheckable,
117 wxMenu *subMenu = (wxMenu *)NULL)
118 {
119 return New(parentMenu, itemid, text, help,
120 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu);
121 }
122
123 protected:
124 int m_id; // numeric id of the item >= 0 or -1
125 wxMenu *m_parentMenu, // the menu we belong to
126 *m_subMenu; // our sub menu or NULL
127 wxString m_text, // label of the item
128 m_help; // the help string for the item
129 wxItemKind m_kind; // seperator/normal/check/radio item?
130 bool m_isChecked; // is checked?
131 bool m_isEnabled; // is enabled?
132
133 // this ctor is for the derived classes only, we're never created directly
134 wxMenuItemBase(wxMenu *parentMenu = (wxMenu *)NULL,
135 int itemid = wxID_SEPARATOR,
136 const wxString& text = wxEmptyString,
137 const wxString& help = wxEmptyString,
138 wxItemKind kind = wxITEM_NORMAL,
139 wxMenu *subMenu = (wxMenu *)NULL);
140
141 private:
142 // and, if we have one ctor, compiler won't generate a default copy one, so
143 // declare them ourselves - but don't implement as they shouldn't be used
144 wxMenuItemBase(const wxMenuItemBase& item);
145 wxMenuItemBase& operator=(const wxMenuItemBase& item);
146 };
147
148 // ----------------------------------------------------------------------------
149 // include the real class declaration
150 // ----------------------------------------------------------------------------
151
152 #ifdef wxUSE_BASE_CLASSES_ONLY
153 #define wxMenuItem wxMenuItemBase
154 #else // !wxUSE_BASE_CLASSES_ONLY
155 #if defined(__WXUNIVERSAL__)
156 #include "wx/univ/menuitem.h"
157 #elif defined(__WXMSW__)
158 #include "wx/msw/menuitem.h"
159 #elif defined(__WXMOTIF__)
160 #include "wx/motif/menuitem.h"
161 #elif defined(__WXGTK__)
162 #include "wx/gtk/menuitem.h"
163 #elif defined(__WXMAC__)
164 #include "wx/mac/menuitem.h"
165 #elif defined(__WXCOCOA__)
166 #include "wx/cocoa/menuitem.h"
167 #elif defined(__WXPM__)
168 #include "wx/os2/menuitem.h"
169 #endif
170 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
171
172 #endif // wxUSE_MENUS
173
174 #endif
175 // _WX_MENUITEM_H_BASE_