]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.h | |
3 | // Purpose: wxMenu, wxMenuBar classes | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin (wxMenuItem is now in separate file) | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
a3622daa | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
bbcdf8bc JS |
12 | #ifndef _WX_MENU_H_ |
13 | #define _WX_MENU_H_ | |
2bda0e17 KB |
14 | |
15 | #ifdef __GNUG__ | |
c626a8b7 | 16 | #pragma interface "menu.h" |
2bda0e17 KB |
17 | #endif |
18 | ||
19 | #include "wx/defs.h" | |
20 | #include "wx/event.h" | |
546db2a8 | 21 | #include "wx/dynarray.h" |
2bda0e17 | 22 | |
bbcdf8bc JS |
23 | class WXDLLEXPORT wxMenuItem; |
24 | class WXDLLEXPORT wxMenuBar; | |
25 | class WXDLLEXPORT wxMenu; | |
c626a8b7 | 26 | class WXDLLEXPORT wxFrame; |
2bda0e17 | 27 | |
39ca6d79 | 28 | WXDLLEXPORT_DATA(extern const wxChar*) wxEmptyString; |
2bda0e17 KB |
29 | |
30 | // ---------------------------------------------------------------------------- | |
31 | // Menu | |
32 | // ---------------------------------------------------------------------------- | |
c626a8b7 VZ |
33 | |
34 | class WXDLLEXPORT wxMenu : public wxEvtHandler | |
2bda0e17 | 35 | { |
c626a8b7 | 36 | DECLARE_DYNAMIC_CLASS(wxMenu) |
2bda0e17 KB |
37 | |
38 | public: | |
b908d224 | 39 | // ctors & dtor |
b908d224 VZ |
40 | wxMenu(const wxString& title, |
41 | const wxFunction func) | |
42 | { | |
43 | Init(title, func); | |
44 | } | |
33961d59 RR |
45 | |
46 | wxMenu( long WXUNUSED(style) ) | |
47 | { | |
48 | Init( wxEmptyString ); | |
49 | } | |
b908d224 VZ |
50 | |
51 | wxMenu(const wxString& title = wxEmptyString, long WXUNUSED(style) = 0) | |
52 | { | |
53 | Init(title); | |
54 | } | |
55 | ||
c626a8b7 VZ |
56 | virtual ~wxMenu(); |
57 | ||
58 | // construct menu | |
59 | // append a separator to the menu | |
60 | void AppendSeparator(); | |
61 | // append a normal item to the menu | |
62 | void Append(int id, const wxString& label, | |
63 | const wxString& helpString = wxEmptyString, | |
64 | bool checkable = FALSE); | |
65 | // append a submenu | |
66 | void Append(int id, const wxString& label, | |
67 | wxMenu *submenu, | |
68 | const wxString& helpString = wxEmptyString); | |
69 | // append anything (create wxMenuItem first) | |
70 | void Append(wxMenuItem *pItem); | |
71 | ||
72 | // insert a break in the menu | |
73 | void Break(); | |
74 | ||
75 | // delete an item | |
76 | // If it's a submenu, menu is not destroyed. | |
77 | // VZ: why? shouldn't it return "wxMenu *" then? | |
78 | void Delete(int id); | |
79 | ||
80 | // client data | |
81 | void SetClientData(void* clientData) { m_clientData = clientData; } | |
82 | void* GetClientData() const { return m_clientData; } | |
83 | ||
84 | // menu item control | |
85 | // enable/disable item | |
86 | void Enable(int id, bool enable); | |
87 | // TRUE if enabled | |
88 | bool IsEnabled(int id) const; | |
89 | ||
90 | // check/uncheck item - only for checkable items, of course | |
91 | void Check(int id, bool check); | |
92 | // TRUE if checked | |
93 | bool IsChecked(int id) const; | |
94 | ||
95 | // other properties | |
96 | // the menu title | |
97 | void SetTitle(const wxString& label); | |
98 | const wxString GetTitle() const; | |
99 | // the item label | |
100 | void SetLabel(int id, const wxString& label); | |
101 | wxString GetLabel(int id) const; | |
102 | // help string | |
103 | virtual void SetHelpString(int id, const wxString& helpString); | |
104 | virtual wxString GetHelpString(int id) const; | |
105 | ||
106 | // get the list of items | |
107 | wxList& GetItems() const { return (wxList &)m_menuItems; } | |
108 | ||
109 | // find item | |
110 | // returns id of the item matching the given string or wxNOT_FOUND | |
111 | virtual int FindItem(const wxString& itemString) const; | |
112 | // returns NULL if not found | |
113 | wxMenuItem* FindItem(int id) const { return FindItemForId(id); } | |
114 | // find wxMenuItem by ID, and item's menu too if itemMenu is !NULL | |
115 | wxMenuItem *FindItemForId(int itemId, wxMenu **itemMenu = NULL) const; | |
116 | ||
117 | // Updates the UI for a menu and all submenus recursively. source is the | |
118 | // object that has the update event handlers defined for it. If NULL, the | |
119 | // menu or associated window will be used. | |
120 | void UpdateUI(wxEvtHandler* source = (wxEvtHandler*)NULL); | |
121 | ||
42e69d6b | 122 | bool ProcessCommand(wxCommandEvent& event); |
c626a8b7 VZ |
123 | |
124 | virtual void SetParent(wxEvtHandler *parent) { m_parent = parent; } | |
125 | void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; } | |
126 | wxEvtHandler *GetEventHandler() const { return m_eventHandler; } | |
127 | ||
c626a8b7 VZ |
128 | // IMPLEMENTATION |
129 | bool MSWCommand(WXUINT param, WXWORD id); | |
130 | ||
131 | void SetInvokingWindow(wxWindow *pWin) { m_pInvokingWindow = pWin; } | |
132 | wxWindow *GetInvokingWindow() const { return m_pInvokingWindow; } | |
133 | ||
134 | // semi-private accessors | |
135 | // get the window which contains this menu | |
136 | wxWindow *GetWindow() const; | |
137 | // get the menu handle | |
138 | WXHMENU GetHMenu() const; | |
2bda0e17 | 139 | |
c2dcfdef VZ |
140 | // only for wxMenuBar |
141 | void Attach(wxMenuBar *menubar); | |
142 | void Detach(); | |
143 | ||
d427503c | 144 | #if wxUSE_ACCEL |
42e69d6b VZ |
145 | size_t GetAccelCount() const { return m_accelKeyCodes.GetCount(); } |
146 | size_t CopyAccels(wxAcceleratorEntry *accels) const; | |
d427503c | 147 | #endif // wxUSE_ACCEL |
42e69d6b | 148 | |
33961d59 | 149 | wxFunction GetCallback() const { return m_callback; } |
42e69d6b | 150 | void Callback(const wxFunction func) { m_callback = func; } |
33961d59 | 151 | wxFunction m_callback; |
42e69d6b | 152 | |
33961d59 | 153 | #ifdef WXWIN_COMPATIBILITY |
42e69d6b VZ |
154 | // compatibility: these functions are deprecated |
155 | bool Enabled(int id) const { return IsEnabled(id); } | |
156 | bool Checked(int id) const { return IsChecked(id); } | |
157 | ||
42e69d6b VZ |
158 | #endif // WXWIN_COMPATIBILITY |
159 | ||
2bda0e17 | 160 | private: |
b908d224 | 161 | // common part of all ctors |
33961d59 | 162 | void Init(const wxString& title, const wxFunction func = NULL ); |
b908d224 | 163 | |
c2dcfdef | 164 | bool m_doBreak; |
2bda0e17 | 165 | |
c626a8b7 VZ |
166 | // This is used when m_hMenu is NULL because we don't want to |
167 | // delete it in ~wxMenu (it's been added to a parent menu). | |
168 | // But we'll still need the handle for other purposes. | |
169 | // Might be better to have a flag saying whether it's deleteable or not. | |
170 | WXHMENU m_savehMenu ; // Used for Enable() on popup | |
171 | WXHMENU m_hMenu; | |
c626a8b7 VZ |
172 | |
173 | int m_noItems; | |
174 | wxString m_title; | |
175 | wxMenu * m_topLevelMenu; | |
176 | wxMenuBar * m_menuBar; | |
177 | wxList m_menuItems; | |
178 | wxEvtHandler * m_parent; | |
179 | wxEvtHandler * m_eventHandler; | |
180 | wxWindow *m_pInvokingWindow; | |
181 | void* m_clientData; | |
42e69d6b | 182 | |
d427503c | 183 | #if wxUSE_ACCEL |
42e69d6b VZ |
184 | // the accelerators data |
185 | wxArrayInt m_accelKeyCodes, m_accelFlags, m_accelIds; | |
d427503c | 186 | #endif // wxUSE_ACCEL |
2bda0e17 KB |
187 | }; |
188 | ||
189 | // ---------------------------------------------------------------------------- | |
190 | // Menu Bar (a la Windows) | |
191 | // ---------------------------------------------------------------------------- | |
c626a8b7 VZ |
192 | |
193 | class WXDLLEXPORT wxMenuBar : public wxEvtHandler | |
2bda0e17 | 194 | { |
c626a8b7 VZ |
195 | DECLARE_DYNAMIC_CLASS(wxMenuBar) |
196 | ||
197 | public: | |
198 | // ctors & dtor | |
c2dcfdef | 199 | // default constructor |
c626a8b7 | 200 | wxMenuBar(); |
c2dcfdef | 201 | // unused under MSW |
c626a8b7 | 202 | wxMenuBar(long style); |
c2dcfdef | 203 | // menubar takes ownership of the menus arrays but copies the titles |
c626a8b7 VZ |
204 | wxMenuBar(int n, wxMenu *menus[], const wxString titles[]); |
205 | virtual ~wxMenuBar(); | |
206 | ||
207 | // menubar construction | |
c2dcfdef | 208 | WXHMENU Create(); |
c626a8b7 VZ |
209 | void Append(wxMenu *menu, const wxString& title); |
210 | virtual void Delete(wxMenu *menu, int index = 0); /* Menu not destroyed */ | |
211 | ||
212 | // state control | |
213 | // NB: must only be used AFTER menu has been attached to frame, | |
214 | // otherwise use individual menus to enable/disable items | |
215 | // enable the item | |
216 | void Enable(int id, bool enable); | |
217 | // TRUE if item enabled | |
218 | bool IsEnabled(int id) const; | |
219 | // | |
220 | void EnableTop(int pos, bool enable); | |
221 | ||
222 | // works only with checkable items | |
223 | void Check(int id, bool check); | |
224 | // TRUE if checked | |
225 | bool IsChecked(int id) const; | |
226 | ||
227 | void SetLabel(int id, const wxString& label) ; | |
228 | wxString GetLabel(int id) const ; | |
229 | ||
230 | virtual void SetHelpString(int id, const wxString& helpString); | |
231 | virtual wxString GetHelpString(int id) const ; | |
232 | ||
233 | void SetLabelTop(int pos, const wxString& label) ; | |
234 | wxString GetLabelTop(int pos) const ; | |
235 | ||
c2dcfdef VZ |
236 | // notifications: return FALSE to prevent the menu from being |
237 | // appended/deleted | |
9df8c2df | 238 | virtual bool OnAppend(wxMenu *menu, const wxChar *title); |
c626a8b7 VZ |
239 | virtual bool OnDelete(wxMenu *menu, int index); |
240 | ||
241 | // item search | |
242 | // by menu and item names, returns wxNOT_FOUND if not found | |
243 | virtual int FindMenuItem(const wxString& menuString, | |
244 | const wxString& itemString) const; | |
245 | // returns NULL if not found | |
246 | wxMenuItem* FindItem(int id) const { return FindItemForId(id); } | |
247 | // returns NULL if not found, fills menuForItem if !NULL | |
248 | wxMenuItem *FindItemForId(int itemId, wxMenu **menuForItem = NULL) const; | |
249 | ||
250 | // submenus access | |
251 | int GetMenuCount() const { return m_menuCount; } | |
252 | wxMenu *GetMenu(int i) const { return m_menus[i]; } | |
253 | ||
254 | void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; } | |
255 | wxEvtHandler *GetEventHandler() { return m_eventHandler; } | |
256 | ||
257 | #ifdef WXWIN_COMPATIBILITY | |
258 | // compatibility: these functions are deprecated | |
259 | bool Enabled(int id) const { return IsEnabled(id); } | |
260 | bool Checked(int id) const { return IsChecked(id); } | |
261 | #endif // WXWIN_COMPATIBILITY | |
2bda0e17 | 262 | |
c2dcfdef VZ |
263 | // IMPLEMENTATION |
264 | // returns TRUE if we're attached to a frame | |
265 | bool IsAttached() const { return m_menuBarFrame != NULL; } | |
266 | // get the frame we live in | |
267 | wxFrame *GetFrame() const { return m_menuBarFrame; } | |
268 | // attach to a frame | |
42e69d6b | 269 | void Attach(wxFrame *frame); |
c2dcfdef | 270 | |
d427503c | 271 | #if wxUSE_ACCEL |
42e69d6b VZ |
272 | // get the accel table for the menus |
273 | const wxAcceleratorTable& GetAccelTable() const { return m_accelTable; } | |
d427503c VZ |
274 | #endif // wxUSE_ACCEL |
275 | ||
c2dcfdef VZ |
276 | // get the menu handle |
277 | WXHMENU GetHMenu() const { return m_hMenu; } | |
278 | ||
279 | protected: | |
280 | // common part of all ctors | |
281 | void Init(); | |
282 | ||
283 | // if the menubar is modified, the display is not updated automatically, | |
284 | // call this function to update it (m_menuBarFrame should be !NULL) | |
285 | void Refresh(); | |
286 | ||
287 | wxEvtHandler *m_eventHandler; | |
288 | int m_menuCount; | |
289 | wxMenu **m_menus; | |
290 | wxString *m_titles; | |
291 | wxFrame *m_menuBarFrame; | |
292 | WXHMENU m_hMenu; | |
42e69d6b | 293 | |
d427503c | 294 | #if wxUSE_ACCEL |
42e69d6b VZ |
295 | // the accelerator table for all accelerators in all our menus |
296 | wxAcceleratorTable m_accelTable; | |
d427503c | 297 | #endif // wxUSE_ACCEL |
2bda0e17 KB |
298 | }; |
299 | ||
bbcdf8bc | 300 | #endif // _WX_MENU_H_ |