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