]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/menu.h
Added reparenting helper classes to help apps to grab the windows
[wxWidgets.git] / include / wx / menu.h
... / ...
CommitLineData
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#if wxUSE_MENUS
20
21// ----------------------------------------------------------------------------
22// headers
23// ----------------------------------------------------------------------------
24
25#include "wx/list.h" // for "template" list classes
26#include "wx/window.h" // base class for wxMenuBar
27
28// also include this one to ensure compatibility with old code which only
29// included wx/menu.h
30#include "wx/menuitem.h"
31
32class WXDLLEXPORT wxMenu;
33class WXDLLEXPORT wxMenuBarBase;
34class WXDLLEXPORT wxMenuBar;
35class WXDLLEXPORT wxMenuItem;
36
37// pseudo template list classes
38WX_DECLARE_EXPORTED_LIST(wxMenu, wxMenuList);
39WX_DECLARE_EXPORTED_LIST(wxMenuItem, wxMenuItemList);
40
41// ----------------------------------------------------------------------------
42// conditional compilation
43// ----------------------------------------------------------------------------
44
45// having callbacks in menus is a wxWin 1.6x feature which should be replaced
46// with event tables in wxWin 2.xx code - however provide it because many
47// people like it a lot by default
48#ifndef wxUSE_MENU_CALLBACK
49 #if WXWIN_COMPATIBILITY_2
50 #define wxUSE_MENU_CALLBACK 1
51 #else
52 #define wxUSE_MENU_CALLBACK 0
53 #endif // WXWIN_COMPATIBILITY_2
54#endif // !defined(wxUSE_MENU_CALLBACK)
55
56// ----------------------------------------------------------------------------
57// wxMenu
58// ----------------------------------------------------------------------------
59
60class WXDLLEXPORT wxMenuBase : public wxEvtHandler
61{
62public:
63 // create a menu
64 static wxMenu *New(const wxString& title = wxEmptyString, long style = 0);
65
66 // ctors
67 wxMenuBase(const wxString& title, long style = 0) : m_title(title)
68 { Init(style); }
69 wxMenuBase(long style = 0)
70 { Init(style); }
71
72 // dtor deletes all the menu items we own
73 virtual ~wxMenuBase();
74
75 // menu construction
76 // -----------------
77
78 // append a normal item to the menu
79 void Append(int id,
80 const wxString& text,
81 const wxString& help = wxEmptyString,
82 bool isCheckable = FALSE)
83 {
84 DoAppend(wxMenuItem::New((wxMenu *)this, id, text, help, isCheckable));
85 }
86
87 // append a separator to the menu
88 void AppendSeparator() { Append(wxID_SEPARATOR, wxEmptyString); }
89
90 // append a submenu
91 void Append(int id,
92 const wxString& text,
93 wxMenu *submenu,
94 const wxString& help = wxEmptyString)
95 {
96 DoAppend(wxMenuItem::New((wxMenu *)this, id, text, help, FALSE, submenu));
97 }
98
99 // the most generic form of Append() - append anything
100 void Append(wxMenuItem *item) { DoAppend(item); }
101
102 // insert a break in the menu (only works when appending the items, not
103 // inserting them)
104 virtual void Break() { }
105
106 // insert an item before given position
107 bool Insert(size_t pos, wxMenuItem *item);
108 void Insert(size_t pos,
109 int id,
110 const wxString& text,
111 const wxString& help = wxEmptyString,
112 bool isCheckable = FALSE)
113 {
114 Insert(pos, wxMenuItem::New((wxMenu *)this, id, text, help, isCheckable));
115 }
116
117 // insert a separator
118 void InsertSeparator(size_t pos)
119 {
120 Insert(pos, wxMenuItem::New((wxMenu *)this));
121 }
122
123 // insert a submenu
124 void Insert(size_t pos,
125 int id,
126 const wxString& text,
127 wxMenu *submenu,
128 const wxString& help = wxEmptyString)
129 {
130 Insert(pos, wxMenuItem::New((wxMenu *)this, id, text, help, FALSE, submenu));
131 }
132
133 // prepend an item to the menu
134 void Prepend(wxMenuItem *item)
135 {
136 Insert(0u, item);
137 }
138
139 void Prepend(int id,
140 const wxString& text,
141 const wxString& help = wxEmptyString,
142 bool isCheckable = FALSE)
143 {
144 Insert(0u, id, text, help, isCheckable);
145 }
146
147 // insert a separator
148 void PrependSeparator()
149 {
150 InsertSeparator(0u);
151 }
152
153 // insert a submenu
154 void Prepend(int id,
155 const wxString& text,
156 wxMenu *submenu,
157 const wxString& help = wxEmptyString)
158 {
159 Insert(0u, id, text, submenu, help);
160 }
161
162 // detach an item from the menu, but don't delete it so that it can be
163 // added back later (but if it's not, the caller is responsible for
164 // deleting it!)
165 wxMenuItem *Remove(int id) { return Remove(FindChildItem(id)); }
166 wxMenuItem *Remove(wxMenuItem *item);
167
168 // delete an item from the menu (submenus are not destroyed by this
169 // function, see Destroy)
170 bool Delete(int id) { return Delete(FindChildItem(id)); }
171 bool Delete(wxMenuItem *item);
172
173 // delete the item from menu and destroy it (if it's a submenu)
174 bool Destroy(int id) { return Destroy(FindChildItem(id)); }
175 bool Destroy(wxMenuItem *item);
176
177 // menu items access
178 // -----------------
179
180 // get the items
181 size_t GetMenuItemCount() const { return m_items.GetCount(); }
182
183 const wxMenuItemList& GetMenuItems() const { return m_items; }
184 wxMenuItemList& GetMenuItems() { return m_items; }
185
186 // search
187 virtual int FindItem(const wxString& item) const;
188 wxMenuItem* FindItem(int id, wxMenu **menu = NULL) const;
189
190 // get/set items attributes
191 void Enable(int id, bool enable);
192 bool IsEnabled(int id) const;
193
194 void Check(int id, bool check);
195 bool IsChecked(int id) const;
196
197 void SetLabel(int id, const wxString& label);
198 wxString GetLabel(int id) const;
199
200 virtual void SetHelpString(int id, const wxString& helpString);
201 virtual wxString GetHelpString(int id) const;
202
203 // misc accessors
204 // --------------
205
206 // the title
207 virtual void SetTitle(const wxString& title) { m_title = title; }
208 const wxString GetTitle() const { return m_title; }
209
210 // event handler
211 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
212 wxEvtHandler *GetEventHandler() const { return m_eventHandler; }
213
214 // invoking window
215 void SetInvokingWindow(wxWindow *win) { m_invokingWindow = win; }
216 wxWindow *GetInvokingWindow() const { return m_invokingWindow; }
217
218 // style
219 long GetStyle() const { return m_style; }
220
221 // implementation helpers
222 // ----------------------
223
224 // Updates the UI for a menu and all submenus recursively. source is the
225 // object that has the update event handlers defined for it. If NULL, the
226 // menu or associated window will be used.
227 void UpdateUI(wxEvtHandler* source = (wxEvtHandler*)NULL);
228
229 // get the menu bar this menu is attached to (may be NULL, always NULL for
230 // popup menus)
231 wxMenuBar *GetMenuBar() const { return m_menuBar; }
232
233 // called when the menu is attached/detached to/from a menu bar
234 virtual void Attach(wxMenuBarBase *menubar);
235 virtual void Detach();
236
237 // is the menu attached to a menu bar (or is it a popup one)?
238 bool IsAttached() const { return m_menuBar != NULL; }
239
240 // set/get the parent of this menu
241 void SetParent(wxMenu *parent) { m_menuParent = parent; }
242 wxMenu *GetParent() const { return m_menuParent; }
243
244#if WXWIN_COMPATIBILITY
245 // compatibility: these functions are deprecated, use the new ones instead
246 bool Enabled(int id) const { return IsEnabled(id); }
247 bool Checked(int id) const { return IsChecked(id); }
248
249 wxMenuItem* FindItemForId(int itemId, wxMenu **itemMenu) const
250 { return FindItem(itemId, itemMenu); }
251
252 wxList& GetItems() const { return (wxList &)m_items; }
253#endif // WXWIN_COMPATIBILITY
254
255#if wxUSE_MENU_CALLBACK || defined(__WXMOTIF__)
256 // wxWin 1.6x compatible menu event handling
257 wxFunction GetCallback() const { return m_callback; }
258 void Callback(const wxFunction func) { m_callback = func; }
259
260 wxFunction m_callback;
261#endif // wxUSE_MENU_CALLBACK
262
263 // unlike FindItem(), this function doesn't recurse but only looks through
264 // our direct children and also may return the index of the found child if
265 // pos != NULL
266 wxMenuItem *FindChildItem(int id, size_t *pos = NULL) const;
267
268 // called to generate a wxCommandEvent, return TRUE if it was processed,
269 // FALSE otherwise
270 //
271 // the checked parameter may have boolean value or -1 for uncheckable items
272 bool SendEvent(int id, int checked = -1);
273
274protected:
275 // virtuals to override in derived classes
276 // ---------------------------------------
277
278 virtual bool DoAppend(wxMenuItem *item);
279 virtual bool DoInsert(size_t pos, wxMenuItem *item);
280
281 virtual wxMenuItem *DoRemove(wxMenuItem *item);
282 virtual bool DoDelete(wxMenuItem *item);
283 virtual bool DoDestroy(wxMenuItem *item);
284
285 // helpers
286 // -------
287
288 // common part of all ctors
289 void Init(long style);
290
291 // associate the submenu with this menu
292 void AddSubMenu(wxMenu *submenu);
293
294 wxMenuBar *m_menuBar; // menubar we belong to or NULL
295 wxMenu *m_menuParent; // parent menu or NULL
296
297 wxString m_title; // the menu title or label
298 wxMenuItemList m_items; // the list of menu items
299
300 wxWindow *m_invokingWindow; // for popup menus
301
302 long m_style; // combination of wxMENU_XXX flags
303
304 wxEvtHandler *m_eventHandler; // a pluggable in event handler
305};
306
307// ----------------------------------------------------------------------------
308// wxMenuBar
309// ----------------------------------------------------------------------------
310
311class WXDLLEXPORT wxMenuBarBase : public wxWindow
312{
313public:
314 // default ctor
315 wxMenuBarBase();
316
317 // dtor will delete all menus we own
318 virtual ~wxMenuBarBase();
319
320 // menu bar construction
321 // ---------------------
322
323 // append a menu to the end of menubar, return TRUE if ok
324 virtual bool Append(wxMenu *menu, const wxString& title);
325
326 // insert a menu before the given position into the menubar, return TRUE
327 // if inserted ok
328 virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
329
330 // menu bar items access
331 // ---------------------
332
333 // get the number of menus in the menu bar
334 size_t GetMenuCount() const { return m_menus.GetCount(); }
335
336 // get the menu at given position
337 wxMenu *GetMenu(size_t pos) const;
338
339 // replace the menu at given position with another one, returns the
340 // previous menu (which should be deleted by the caller)
341 virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
342
343 // delete the menu at given position from the menu bar, return the pointer
344 // to the menu (which should be deleted by the caller)
345 virtual wxMenu *Remove(size_t pos);
346
347 // enable or disable a submenu
348 virtual void EnableTop(size_t pos, bool enable) = 0;
349
350 // is the menu enabled?
351 virtual bool IsEnabledTop(size_t WXUNUSED(pos)) const { return TRUE; }
352
353 // get or change the label of the menu at given position
354 virtual void SetLabelTop(size_t pos, const wxString& label) = 0;
355 virtual wxString GetLabelTop(size_t pos) const = 0;
356
357 // item search
358 // -----------
359
360 // by menu and item names, returns wxNOT_FOUND if not found or id of the
361 // found item
362 virtual int FindMenuItem(const wxString& menu, const wxString& item) const;
363
364 // find item by id (in any menu), returns NULL if not found
365 //
366 // if menu is !NULL, it will be filled with wxMenu this item belongs to
367 virtual wxMenuItem* FindItem(int id, wxMenu **menu = NULL) const;
368
369 // find menu by its caption, return wxNOT_FOUND on failure
370 int FindMenu(const wxString& title) const;
371
372 // item access
373 // -----------
374
375 // all these functions just use FindItem() and then call an appropriate
376 // method on it
377 //
378 // NB: under MSW, these methods can only be used after the menubar had
379 // been attached to the frame
380
381 void Enable(int id, bool enable);
382 void Check(int id, bool check);
383 bool IsChecked(int id) const;
384 bool IsEnabled(int id) const;
385
386 void SetLabel(int id, const wxString &label);
387 wxString GetLabel(int id) const;
388
389 void SetHelpString(int id, const wxString& helpString);
390 wxString GetHelpString(int id) const;
391
392 // implementation helpers
393
394 // get the frame we are attached to (may return NULL)
395 wxFrame *GetFrame() const { return m_menuBarFrame; }
396
397 // returns TRUE if we're attached to a frame
398 bool IsAttached() const { return GetFrame() != NULL; }
399
400 // associate the menubar with the frame
401 virtual void Attach(wxFrame *frame);
402
403 // called before deleting the menubar normally
404 virtual void Detach();
405
406 // need to override these ones to avoid virtual function hiding
407 virtual bool Enable(bool enable = TRUE) { return wxWindow::Enable(enable); }
408 virtual void SetLabel(const wxString& s) { wxWindow::SetLabel(s); }
409 virtual wxString GetLabel() const { return wxWindow::GetLabel(); }
410
411 // don't want menu bars to accept the focus by tabbing to them
412 virtual bool AcceptsFocusFromKeyboard() const { return FALSE; }
413
414 // compatibility only: these functions are deprecated, use the new ones
415 // instead
416#if WXWIN_COMPATIBILITY
417 bool Enabled(int id) const { return IsEnabled(id); }
418 bool Checked(int id) const { return IsChecked(id); }
419
420 wxMenuItem* FindMenuItemById(int id) const
421 { return FindItem(id); }
422 wxMenuItem* FindItemForId(int id, wxMenu **menu = NULL) const
423 { return FindItem(id, menu); }
424#endif // WXWIN_COMPATIBILITY
425
426protected:
427 // the list of all our menus
428 wxMenuList m_menus;
429
430 // the frame we are attached to (may be NULL)
431 wxFrame *m_menuBarFrame;
432};
433
434// ----------------------------------------------------------------------------
435// include the real class declaration
436// ----------------------------------------------------------------------------
437
438#ifdef wxUSE_BASE_CLASSES_ONLY
439 #define wxMenuItem wxMenuItemBase
440#else // !wxUSE_BASE_CLASSES_ONLY
441#if defined(__WXUNIVERSAL__)
442 #include "wx/univ/menu.h"
443#elif defined(__WXMSW__)
444 #include "wx/msw/menu.h"
445#elif defined(__WXMOTIF__)
446 #include "wx/motif/menu.h"
447#elif defined(__WXGTK__)
448 #include "wx/gtk/menu.h"
449#elif defined(__WXMAC__)
450 #include "wx/mac/menu.h"
451#elif defined(__WXPM__)
452 #include "wx/os2/menu.h"
453#elif defined(__WXSTUBS__)
454 #include "wx/stubs/menu.h"
455#endif
456#endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
457
458#endif // wxUSE_MENUS
459
460#endif
461 // _WX_MENU_H_BASE_