]>
Commit | Line | Data |
---|---|---|
3dfac970 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/menu.h | |
3 | // Purpose: wxMenu and wxMenuBar classes | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 26.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWindows team | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
34138703 JS |
12 | #ifndef _WX_MENU_H_BASE_ |
13 | #define _WX_MENU_H_BASE_ | |
c801d85f | 14 | |
3dfac970 VZ |
15 | #ifdef __GNUG__ |
16 | #pragma interface "menubase.h" | |
17 | #endif | |
18 | ||
19 | // ---------------------------------------------------------------------------- | |
20 | // headers | |
21 | // ---------------------------------------------------------------------------- | |
22 | ||
717a57c2 | 23 | #include "wx/list.h" // for "template" list classes |
3dfac970 VZ |
24 | #include "wx/window.h" // base class for wxMenuBar |
25 | ||
717a57c2 VZ |
26 | // also include this one to ensure compatibility with old code which only |
27 | // included wx/menu.h | |
28 | #include "wx/menuitem.h" | |
29 | ||
3dfac970 VZ |
30 | class WXDLLEXPORT wxMenu; |
31 | class WXDLLEXPORT wxMenuBar; | |
32 | class WXDLLEXPORT wxMenuItem; | |
33 | ||
717a57c2 VZ |
34 | // pseudo template list classes |
35 | WX_DECLARE_LIST(wxMenu, wxMenuList); | |
36 | WX_DECLARE_LIST(wxMenuItem, wxMenuItemList); | |
37 | ||
9739d9ee VZ |
38 | // ---------------------------------------------------------------------------- |
39 | // conditional compilation | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // having callbacks in menus is a wxWin 1.6x feature which should be replaced | |
43 | // with event tables in wxWin 2.xx code - however provide it because many | |
44 | // people like it a lot by default | |
45 | #ifndef wxUSE_MENU_CALLBACK | |
46 | #if WXWIN_COMPATIBILITY_2 | |
47 | #define wxUSE_MENU_CALLBACK 1 | |
48 | #else | |
49 | #define wxUSE_MENU_CALLBACK 0 | |
50 | #endif // WXWIN_COMPATIBILITY_2 | |
51 | #endif // !defined(wxUSE_MENU_CALLBACK) | |
52 | ||
3dfac970 VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // wxMenu | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
717a57c2 VZ |
57 | class WXDLLEXPORT wxMenuBase : public wxEvtHandler |
58 | { | |
59 | public: | |
60 | // create a menu | |
61 | static wxMenu *New(const wxString& title = wxEmptyString, long style = 0); | |
62 | ||
63 | // ctors | |
64 | wxMenuBase(const wxString& title, long style = 0) : m_title(title) | |
65 | { Init(style); } | |
66 | wxMenuBase(long style = 0) | |
67 | { Init(style); } | |
68 | ||
69 | // dtor deletes all the menu items we own | |
70 | virtual ~wxMenuBase(); | |
71 | ||
72 | // menu construction | |
73 | // ----------------- | |
74 | ||
717a57c2 VZ |
75 | // append a normal item to the menu |
76 | void Append(int id, | |
77 | const wxString& text, | |
78 | const wxString& help = wxEmptyString, | |
79 | bool isCheckable = FALSE) | |
80 | { | |
81 | DoAppend(wxMenuItem::New((wxMenu *)this, id, text, help, isCheckable)); | |
82 | } | |
83 | ||
0e528b99 JS |
84 | // append a separator to the menu |
85 | void AppendSeparator() { Append(wxID_SEPARATOR, wxEmptyString); } | |
86 | ||
717a57c2 VZ |
87 | // append a submenu |
88 | void Append(int id, | |
89 | const wxString& text, | |
90 | wxMenu *submenu, | |
91 | const wxString& help = wxEmptyString) | |
92 | { | |
93 | DoAppend(wxMenuItem::New((wxMenu *)this, id, text, help, FALSE, submenu)); | |
94 | } | |
95 | ||
96 | // the most generic form of Append() - append anything | |
97 | void Append(wxMenuItem *item) { DoAppend(item); } | |
98 | ||
99 | // insert a break in the menu (only works when appending the items, not | |
100 | // inserting them) | |
101 | virtual void Break() { } | |
102 | ||
103 | // insert an item before given position | |
104 | bool Insert(size_t pos, wxMenuItem *item); | |
105 | ||
106 | // detach an item from the menu, but don't delete it so that it can be | |
107 | // added back later (but if it's not, the caller is responsible for | |
108 | // deleting it!) | |
109 | wxMenuItem *Remove(int id) { return Remove(FindChildItem(id)); } | |
110 | wxMenuItem *Remove(wxMenuItem *item); | |
111 | ||
112 | // delete an item from the menu (submenus are not destroyed by this | |
113 | // function, see Destroy) | |
114 | bool Delete(int id) { return Delete(FindChildItem(id)); } | |
115 | bool Delete(wxMenuItem *item); | |
116 | ||
117 | // delete the item from menu and destroy it (if it's a submenu) | |
118 | bool Destroy(int id) { return Destroy(FindChildItem(id)); } | |
119 | bool Destroy(wxMenuItem *item); | |
120 | ||
121 | // menu items access | |
122 | // ----------------- | |
123 | ||
124 | // get the items | |
125 | size_t GetMenuItemCount() const { return m_items.GetCount(); } | |
126 | ||
127 | const wxMenuItemList& GetMenuItems() const { return m_items; } | |
128 | wxMenuItemList& GetMenuItems() { return m_items; } | |
129 | ||
130 | // search | |
131 | virtual int FindItem(const wxString& itemString) const; | |
132 | wxMenuItem* FindItem(int id, wxMenu **menu = NULL) const; | |
133 | ||
134 | // get/set items attributes | |
135 | void Enable(int id, bool enable); | |
136 | bool IsEnabled(int id) const; | |
3dfac970 | 137 | |
717a57c2 VZ |
138 | void Check(int id, bool check); |
139 | bool IsChecked(int id) const; | |
140 | ||
141 | void SetLabel(int id, const wxString& label); | |
142 | wxString GetLabel(int id) const; | |
143 | ||
144 | virtual void SetHelpString(int id, const wxString& helpString); | |
145 | virtual wxString GetHelpString(int id) const; | |
146 | ||
147 | // misc accessors | |
148 | // -------------- | |
149 | ||
150 | // the title | |
151 | virtual void SetTitle(const wxString& title) { m_title = title; } | |
152 | const wxString GetTitle() const { return m_title; } | |
153 | ||
154 | // client data | |
155 | void SetClientData(void* clientData) { m_clientData = clientData; } | |
156 | void* GetClientData() const { return m_clientData; } | |
157 | ||
158 | // event handler | |
159 | void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; } | |
160 | wxEvtHandler *GetEventHandler() const { return m_eventHandler; } | |
161 | ||
162 | // invoking window | |
163 | void SetInvokingWindow(wxWindow *win) { m_invokingWindow = win; } | |
164 | wxWindow *GetInvokingWindow() const { return m_invokingWindow; } | |
165 | ||
166 | // style | |
167 | long GetStyle() const { return m_style; } | |
168 | ||
169 | // implementation helpers | |
170 | // ---------------------- | |
171 | ||
172 | // Updates the UI for a menu and all submenus recursively. source is the | |
173 | // object that has the update event handlers defined for it. If NULL, the | |
174 | // menu or associated window will be used. | |
175 | void UpdateUI(wxEvtHandler* source = (wxEvtHandler*)NULL); | |
176 | ||
177 | // is the menu attached to a menu bar (or is it a popup one)? | |
178 | bool IsAttached() const { return m_menuBar != NULL; } | |
179 | ||
180 | // set/get the parent of this menu | |
181 | void SetParent(wxMenu *parent) { m_menuParent = parent; } | |
182 | wxMenu *GetParent() const { return m_menuParent; } | |
183 | ||
184 | #if WXWIN_COMPATIBILITY | |
185 | // compatibility: these functions are deprecated, use the new ones instead | |
186 | bool Enabled(int id) const { return IsEnabled(id); } | |
187 | bool Checked(int id) const { return IsChecked(id); } | |
188 | ||
189 | wxMenuItem* FindItemForId(int itemId, wxMenu **itemMenu) const | |
190 | { return FindItem(itemId, itemMenu); } | |
191 | ||
192 | wxList& GetItems() const { return (wxList &)m_items; } | |
c0ab6adf | 193 | #endif // WXWIN_COMPATIBILITY |
717a57c2 | 194 | |
9739d9ee | 195 | #if wxUSE_MENU_CALLBACK |
717a57c2 VZ |
196 | // wxWin 1.6x compatible menu event handling |
197 | wxFunction GetCallback() const { return m_callback; } | |
198 | void Callback(const wxFunction func) { m_callback = func; } | |
9739d9ee | 199 | |
717a57c2 | 200 | wxFunction m_callback; |
9739d9ee | 201 | #endif // wxUSE_MENU_CALLBACK |
717a57c2 | 202 | |
1987af7e VZ |
203 | // unlike FindItem(), this function doesn't recurse but only looks through |
204 | // our direct children and also may return the index of the found child if | |
205 | // pos != NULL | |
206 | wxMenuItem *FindChildItem(int id, size_t *pos = NULL) const; | |
207 | ||
717a57c2 VZ |
208 | protected: |
209 | // virtuals to override in derived classes | |
210 | // --------------------------------------- | |
211 | ||
212 | virtual bool DoAppend(wxMenuItem *item); | |
213 | virtual bool DoInsert(size_t pos, wxMenuItem *item); | |
214 | ||
215 | virtual wxMenuItem *DoRemove(wxMenuItem *item); | |
216 | virtual bool DoDelete(wxMenuItem *item); | |
217 | virtual bool DoDestroy(wxMenuItem *item); | |
218 | ||
219 | // helpers | |
220 | // ------- | |
221 | ||
222 | // common part of all ctors | |
223 | void Init(long style); | |
224 | ||
717a57c2 VZ |
225 | protected: |
226 | wxMenuBar *m_menuBar; // menubar we belong to or NULL | |
227 | wxMenu *m_menuParent; // parent menu or NULL | |
228 | ||
229 | wxString m_title; // the menu title or label | |
230 | wxMenuItemList m_items; // the list of menu items | |
231 | ||
232 | wxWindow *m_invokingWindow; // for popup menus | |
233 | void *m_clientData; // associated with the menu | |
234 | ||
235 | long m_style; // combination of wxMENU_XXX flags | |
236 | ||
237 | wxEvtHandler *m_eventHandler; // a pluggable in event handler | |
238 | }; | |
3dfac970 VZ |
239 | |
240 | // ---------------------------------------------------------------------------- | |
241 | // wxMenuBar | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | class WXDLLEXPORT wxMenuBarBase : public wxWindow | |
245 | { | |
246 | public: | |
247 | // default ctor | |
248 | wxMenuBarBase(); | |
249 | ||
250 | // dtor will delete all menus we own | |
251 | virtual ~wxMenuBarBase(); | |
252 | ||
253 | // menu bar construction | |
254 | // --------------------- | |
255 | ||
256 | // append a menu to the end of menubar, return TRUE if ok | |
257 | virtual bool Append(wxMenu *menu, const wxString& title); | |
258 | ||
259 | // insert a menu before the given position into the menubar, return TRUE | |
260 | // if inserted ok | |
261 | virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title); | |
262 | ||
263 | // menu bar items access | |
264 | // --------------------- | |
265 | ||
266 | // get the number of menus in the menu bar | |
267 | size_t GetMenuCount() const { return m_menus.GetCount(); } | |
268 | ||
269 | // get the menu at given position | |
270 | wxMenu *GetMenu(size_t pos) const; | |
271 | ||
272 | // replace the menu at given position with another one, returns the | |
273 | // previous menu (which should be deleted by the caller) | |
274 | virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title); | |
275 | ||
276 | // delete the menu at given position from the menu bar, return the pointer | |
277 | // to the menu (which should be deleted by the caller) | |
278 | virtual wxMenu *Remove(size_t pos); | |
279 | ||
280 | // enable or disable a submenu | |
281 | virtual void EnableTop(size_t pos, bool enable) = 0; | |
282 | ||
283 | // get or change the label of the menu at given position | |
284 | virtual void SetLabelTop(size_t pos, const wxString& label) = 0; | |
285 | virtual wxString GetLabelTop(size_t pos) const = 0; | |
286 | ||
287 | // item search | |
288 | // ----------- | |
289 | ||
290 | // by menu and item names, returns wxNOT_FOUND if not found or id of the | |
291 | // found item | |
292 | virtual int FindMenuItem(const wxString& menuString, | |
293 | const wxString& itemString) const = 0; | |
294 | ||
295 | // find item by id (in any menu), returns NULL if not found | |
296 | // | |
297 | // if menu is !NULL, it will be filled with wxMenu this item belongs to | |
298 | virtual wxMenuItem* FindItem(int id, wxMenu **menu = NULL) const = 0; | |
299 | ||
52130557 VZ |
300 | // find menu by its caption, return wxNOT_FOUND on failure |
301 | int FindMenu(const wxString& title); | |
302 | ||
3dfac970 VZ |
303 | // item access |
304 | // ----------- | |
305 | ||
306 | // all these functions just use FindItem() and then call an appropriate | |
307 | // method on it | |
308 | // | |
309 | // NB: under MSW, these methods can only be used after the menubar had | |
310 | // been attached to the frame | |
311 | ||
312 | void Enable(int id, bool enable); | |
313 | void Check(int id, bool check); | |
314 | bool IsChecked(int id) const; | |
315 | bool IsEnabled(int id) const; | |
316 | ||
317 | void SetLabel(int id, const wxString &label); | |
318 | wxString GetLabel(int id) const; | |
319 | ||
320 | void SetHelpString(int id, const wxString& helpString); | |
321 | wxString GetHelpString(int id) const; | |
322 | ||
9874b4ee VZ |
323 | // need to override these ones to avoid virtual function hiding |
324 | virtual bool Enable(bool enable = TRUE) { return wxWindow::Enable(enable); } | |
325 | virtual void SetLabel(const wxString& s) { wxWindow::SetLabel(s); } | |
3dfac970 VZ |
326 | virtual wxString GetLabel() const { return wxWindow::GetLabel(); } |
327 | ||
328 | // compatibility only: these functions are deprecated, use the new ones | |
329 | // instead | |
717a57c2 | 330 | #if WXWIN_COMPATIBILITY |
3dfac970 VZ |
331 | bool Enabled(int id) const { return IsEnabled(id); } |
332 | bool Checked(int id) const { return IsChecked(id); } | |
333 | ||
334 | wxMenuItem* FindMenuItemById(int id) const | |
335 | { return FindItem(id); } | |
336 | wxMenuItem* FindItemForId(int id, wxMenu **menu = NULL) const | |
337 | { return FindItem(id, menu); } | |
338 | #endif // WXWIN_COMPATIBILITY | |
339 | ||
340 | protected: | |
341 | // the list of all our menus | |
342 | wxMenuList m_menus; | |
343 | }; | |
344 | ||
345 | // ---------------------------------------------------------------------------- | |
346 | // include the real class declaration | |
347 | // ---------------------------------------------------------------------------- | |
348 | ||
349 | #ifdef wxUSE_BASE_CLASSES_ONLY | |
350 | #define wxMenuItem wxMenuItemBase | |
351 | #else // !wxUSE_BASE_CLASSES_ONLY | |
2049ba38 | 352 | #if defined(__WXMSW__) |
3dfac970 | 353 | #include "wx/msw/menu.h" |
2049ba38 | 354 | #elif defined(__WXMOTIF__) |
3dfac970 | 355 | #include "wx/motif/menu.h" |
2049ba38 | 356 | #elif defined(__WXGTK__) |
3dfac970 | 357 | #include "wx/gtk/menu.h" |
b4e76e0d | 358 | #elif defined(__WXQT__) |
3dfac970 | 359 | #include "wx/qt/menu.h" |
34138703 | 360 | #elif defined(__WXMAC__) |
3dfac970 | 361 | #include "wx/mac/menu.h" |
1777b9bb | 362 | #elif defined(__WXPM__) |
3dfac970 | 363 | #include "wx/os2/menu.h" |
34138703 | 364 | #elif defined(__WXSTUBS__) |
3dfac970 | 365 | #include "wx/stubs/menu.h" |
c801d85f | 366 | #endif |
3dfac970 VZ |
367 | #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY |
368 | ||
c801d85f | 369 | #endif |
34138703 | 370 | // _WX_MENU_H_BASE_ |