]> git.saurik.com Git - wxWidgets.git/blame - include/wx/menu.h
only define globals when wxUSE_BASE to avoid duplicate definitions in wxBase and...
[wxWidgets.git] / include / wx / menu.h
CommitLineData
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$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
3dfac970
VZ
10///////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_MENU_H_BASE_
13#define _WX_MENU_H_BASE_
c801d85f 14
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3dfac970
VZ
16 #pragma interface "menubase.h"
17#endif
18
997176a3
VZ
19#include "wx/defs.h"
20
1e6feb95
VZ
21#if wxUSE_MENUS
22
3dfac970
VZ
23// ----------------------------------------------------------------------------
24// headers
25// ----------------------------------------------------------------------------
26
717a57c2 27#include "wx/list.h" // for "template" list classes
3dfac970
VZ
28#include "wx/window.h" // base class for wxMenuBar
29
717a57c2
VZ
30// also include this one to ensure compatibility with old code which only
31// included wx/menu.h
32#include "wx/menuitem.h"
33
3dfac970 34class WXDLLEXPORT wxMenu;
1e6feb95 35class WXDLLEXPORT wxMenuBarBase;
3dfac970
VZ
36class WXDLLEXPORT wxMenuBar;
37class WXDLLEXPORT wxMenuItem;
38
717a57c2 39// pseudo template list classes
f6bcfd97
BP
40WX_DECLARE_EXPORTED_LIST(wxMenu, wxMenuList);
41WX_DECLARE_EXPORTED_LIST(wxMenuItem, wxMenuItemList);
717a57c2 42
3dfac970
VZ
43// ----------------------------------------------------------------------------
44// wxMenu
45// ----------------------------------------------------------------------------
46
717a57c2
VZ
47class WXDLLEXPORT wxMenuBase : public wxEvtHandler
48{
49public:
50 // create a menu
51 static wxMenu *New(const wxString& title = wxEmptyString, long style = 0);
52
53 // ctors
54 wxMenuBase(const wxString& title, long style = 0) : m_title(title)
55 { Init(style); }
56 wxMenuBase(long style = 0)
57 { Init(style); }
58
59 // dtor deletes all the menu items we own
60 virtual ~wxMenuBase();
61
62 // menu construction
63 // -----------------
64
d65c269b 65 // append any kind of item (normal/check/radio/separator)
9add9367
RD
66 wxMenuItem* Append(int itemid,
67 const wxString& text,
68 const wxString& help = wxEmptyString,
69 wxItemKind kind = wxITEM_NORMAL)
717a57c2 70 {
9add9367 71 return DoAppend(wxMenuItem::New((wxMenu *)this, itemid, text, help, kind));
717a57c2
VZ
72 }
73
0e528b99 74 // append a separator to the menu
9add9367 75 wxMenuItem* AppendSeparator() { return Append(wxID_SEPARATOR, wxEmptyString); }
0e528b99 76
d65c269b 77 // append a check item
9add9367
RD
78 wxMenuItem* AppendCheckItem(int itemid,
79 const wxString& text,
80 const wxString& help = wxEmptyString)
d65c269b 81 {
9add9367 82 return Append(itemid, text, help, wxITEM_CHECK);
d65c269b
VZ
83 }
84
85 // append a radio item
9add9367
RD
86 wxMenuItem* AppendRadioItem(int itemid,
87 const wxString& text,
88 const wxString& help = wxEmptyString)
d65c269b 89 {
9add9367 90 return Append(itemid, text, help, wxITEM_RADIO);
d65c269b
VZ
91 }
92
717a57c2 93 // append a submenu
9add9367
RD
94 wxMenuItem* Append(int itemid,
95 const wxString& text,
96 wxMenu *submenu,
97 const wxString& help = wxEmptyString)
717a57c2 98 {
9add9367
RD
99 return DoAppend(wxMenuItem::New((wxMenu *)this, itemid, text, help,
100 wxITEM_NORMAL, submenu));
717a57c2
VZ
101 }
102
103 // the most generic form of Append() - append anything
9add9367 104 wxMenuItem* Append(wxMenuItem *item) { return DoAppend(item); }
717a57c2
VZ
105
106 // insert a break in the menu (only works when appending the items, not
107 // inserting them)
108 virtual void Break() { }
109
110 // insert an item before given position
9add9367 111 wxMenuItem* Insert(size_t pos, wxMenuItem *item);
d65c269b
VZ
112
113 // insert an item before given position
9add9367
RD
114 wxMenuItem* Insert(size_t pos,
115 int itemid,
116 const wxString& text,
117 const wxString& help = wxEmptyString,
118 wxItemKind kind = wxITEM_NORMAL)
f6bcfd97 119 {
9add9367 120 return Insert(pos, wxMenuItem::New((wxMenu *)this, itemid, text, help, kind));
f6bcfd97
BP
121 }
122
123 // insert a separator
9add9367 124 wxMenuItem* InsertSeparator(size_t pos)
f6bcfd97 125 {
9add9367 126 return Insert(pos, wxMenuItem::New((wxMenu *)this, wxID_SEPARATOR));
f6bcfd97
BP
127 }
128
d65c269b 129 // insert a check item
9add9367
RD
130 wxMenuItem* InsertCheckItem(size_t pos,
131 int itemid,
132 const wxString& text,
133 const wxString& help = wxEmptyString)
d65c269b 134 {
9add9367 135 return Insert(pos, itemid, text, help, wxITEM_CHECK);
d65c269b
VZ
136 }
137
138 // insert a radio item
9add9367
RD
139 wxMenuItem* InsertRadioItem(size_t pos,
140 int itemid,
141 const wxString& text,
142 const wxString& help = wxEmptyString)
d65c269b 143 {
9add9367 144 return Insert(pos, itemid, text, help, wxITEM_RADIO);
d65c269b
VZ
145 }
146
f6bcfd97 147 // insert a submenu
9add9367
RD
148 wxMenuItem* Insert(size_t pos,
149 int itemid,
150 const wxString& text,
151 wxMenu *submenu,
152 const wxString& help = wxEmptyString)
f6bcfd97 153 {
9add9367
RD
154 return Insert(pos, wxMenuItem::New((wxMenu *)this, itemid, text, help,
155 wxITEM_NORMAL, submenu));
f6bcfd97
BP
156 }
157
158 // prepend an item to the menu
9add9367 159 wxMenuItem* Prepend(wxMenuItem *item)
f6bcfd97 160 {
9add9367 161 return Insert(0u, item);
f6bcfd97
BP
162 }
163
d65c269b 164 // prepend any item to the menu
9add9367
RD
165 wxMenuItem* Prepend(int itemid,
166 const wxString& text,
167 const wxString& help = wxEmptyString,
168 wxItemKind kind = wxITEM_NORMAL)
f6bcfd97 169 {
9add9367 170 return Insert(0u, itemid, text, help, kind);
f6bcfd97
BP
171 }
172
d65c269b 173 // prepend a separator
9add9367 174 wxMenuItem* PrependSeparator()
f6bcfd97 175 {
9add9367 176 return InsertSeparator(0u);
f6bcfd97
BP
177 }
178
d65c269b 179 // prepend a check item
9add9367
RD
180 wxMenuItem* PrependCheckItem(int itemid,
181 const wxString& text,
182 const wxString& help = wxEmptyString)
d65c269b 183 {
9add9367 184 return InsertCheckItem(0u, itemid, text, help);
d65c269b
VZ
185 }
186
187 // prepend a radio item
9add9367
RD
188 wxMenuItem* PrependRadioItem(int itemid,
189 const wxString& text,
190 const wxString& help = wxEmptyString)
d65c269b 191 {
9add9367 192 return InsertRadioItem(0u, itemid, text, help);
d65c269b
VZ
193 }
194
195 // prepend a submenu
9add9367
RD
196 wxMenuItem* Prepend(int itemid,
197 const wxString& text,
198 wxMenu *submenu,
199 const wxString& help = wxEmptyString)
f6bcfd97 200 {
9add9367 201 return Insert(0u, itemid, text, submenu, help);
f6bcfd97 202 }
717a57c2
VZ
203
204 // detach an item from the menu, but don't delete it so that it can be
205 // added back later (but if it's not, the caller is responsible for
206 // deleting it!)
d9e2e4c2 207 wxMenuItem *Remove(int itemid) { return Remove(FindChildItem(itemid)); }
717a57c2
VZ
208 wxMenuItem *Remove(wxMenuItem *item);
209
210 // delete an item from the menu (submenus are not destroyed by this
211 // function, see Destroy)
d9e2e4c2 212 bool Delete(int itemid) { return Delete(FindChildItem(itemid)); }
717a57c2
VZ
213 bool Delete(wxMenuItem *item);
214
215 // delete the item from menu and destroy it (if it's a submenu)
d9e2e4c2 216 bool Destroy(int itemid) { return Destroy(FindChildItem(itemid)); }
717a57c2
VZ
217 bool Destroy(wxMenuItem *item);
218
219 // menu items access
220 // -----------------
221
222 // get the items
223 size_t GetMenuItemCount() const { return m_items.GetCount(); }
224
225 const wxMenuItemList& GetMenuItems() const { return m_items; }
226 wxMenuItemList& GetMenuItems() { return m_items; }
227
228 // search
1e6feb95 229 virtual int FindItem(const wxString& item) const;
d9e2e4c2 230 wxMenuItem* FindItem(int itemid, wxMenu **menu = NULL) const;
717a57c2 231
bd582574
JS
232 // find by position
233 wxMenuItem* FindItemByPosition(size_t position) const;
234
717a57c2 235 // get/set items attributes
d9e2e4c2
DE
236 void Enable(int itemid, bool enable);
237 bool IsEnabled(int itemid) const;
3dfac970 238
d9e2e4c2
DE
239 void Check(int itemid, bool check);
240 bool IsChecked(int itemid) const;
717a57c2 241
d9e2e4c2
DE
242 void SetLabel(int itemid, const wxString& label);
243 wxString GetLabel(int itemid) const;
717a57c2 244
d9e2e4c2
DE
245 virtual void SetHelpString(int itemid, const wxString& helpString);
246 virtual wxString GetHelpString(int itemid) const;
717a57c2
VZ
247
248 // misc accessors
249 // --------------
250
251 // the title
252 virtual void SetTitle(const wxString& title) { m_title = title; }
253 const wxString GetTitle() const { return m_title; }
254
717a57c2
VZ
255 // event handler
256 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
257 wxEvtHandler *GetEventHandler() const { return m_eventHandler; }
258
259 // invoking window
260 void SetInvokingWindow(wxWindow *win) { m_invokingWindow = win; }
261 wxWindow *GetInvokingWindow() const { return m_invokingWindow; }
262
263 // style
264 long GetStyle() const { return m_style; }
265
266 // implementation helpers
267 // ----------------------
268
269 // Updates the UI for a menu and all submenus recursively. source is the
270 // object that has the update event handlers defined for it. If NULL, the
271 // menu or associated window will be used.
272 void UpdateUI(wxEvtHandler* source = (wxEvtHandler*)NULL);
273
1e6feb95 274 // get the menu bar this menu is attached to (may be NULL, always NULL for
dbdf9a17
DE
275 // popup menus). Traverse up the menu hierarchy to find it.
276 wxMenuBar *GetMenuBar() const;
1e6feb95
VZ
277
278 // called when the menu is attached/detached to/from a menu bar
279 virtual void Attach(wxMenuBarBase *menubar);
280 virtual void Detach();
281
717a57c2 282 // is the menu attached to a menu bar (or is it a popup one)?
dbdf9a17 283 bool IsAttached() const { return GetMenuBar() != NULL; }
717a57c2
VZ
284
285 // set/get the parent of this menu
286 void SetParent(wxMenu *parent) { m_menuParent = parent; }
287 wxMenu *GetParent() const { return m_menuParent; }
288
d65c269b
VZ
289 // implementation only from now on
290 // -------------------------------
291
292 // unlike FindItem(), this function doesn't recurse but only looks through
293 // our direct children and also may return the index of the found child if
294 // pos != NULL
d9e2e4c2 295 wxMenuItem *FindChildItem(int itemid, size_t *pos = NULL) const;
d65c269b 296
4e32eea1
WS
297 // called to generate a wxCommandEvent, return true if it was processed,
298 // false otherwise
d65c269b
VZ
299 //
300 // the checked parameter may have boolean value or -1 for uncheckable items
d9e2e4c2 301 bool SendEvent(int itemid, int checked = -1);
d65c269b 302
717a57c2 303 // compatibility: these functions are deprecated, use the new ones instead
d65c269b
VZ
304 // -----------------------------------------------------------------------
305
306 // use the versions taking wxItem_XXX now instead, they're more readable
307 // and allow adding the radio items as well
d9e2e4c2 308 void Append(int itemid,
d65c269b
VZ
309 const wxString& text,
310 const wxString& help,
311 bool isCheckable)
312 {
d9e2e4c2 313 Append(itemid, text, help, isCheckable ? wxITEM_CHECK : wxITEM_NORMAL);
d65c269b
VZ
314 }
315
316 void Insert(size_t pos,
d9e2e4c2 317 int itemid,
d65c269b
VZ
318 const wxString& text,
319 const wxString& help,
320 bool isCheckable)
321 {
d9e2e4c2 322 Insert(pos, itemid, text, help, isCheckable ? wxITEM_CHECK : wxITEM_NORMAL);
d65c269b
VZ
323 }
324
d9e2e4c2 325 void Prepend(int itemid,
d65c269b
VZ
326 const wxString& text,
327 const wxString& help,
328 bool isCheckable)
329 {
d9e2e4c2 330 Insert(0u, itemid, text, help, isCheckable);
d65c269b
VZ
331 }
332
6d971354
RR
333 static void LockAccels(bool locked)
334 {
335 ms_locked = locked;
336 }
4e32eea1 337
717a57c2
VZ
338protected:
339 // virtuals to override in derived classes
340 // ---------------------------------------
341
9add9367
RD
342 virtual wxMenuItem* DoAppend(wxMenuItem *item);
343 virtual wxMenuItem* DoInsert(size_t pos, wxMenuItem *item);
717a57c2
VZ
344
345 virtual wxMenuItem *DoRemove(wxMenuItem *item);
346 virtual bool DoDelete(wxMenuItem *item);
347 virtual bool DoDestroy(wxMenuItem *item);
348
349 // helpers
350 // -------
351
352 // common part of all ctors
353 void Init(long style);
354
1e6feb95
VZ
355 // associate the submenu with this menu
356 void AddSubMenu(wxMenu *submenu);
357
717a57c2
VZ
358 wxMenuBar *m_menuBar; // menubar we belong to or NULL
359 wxMenu *m_menuParent; // parent menu or NULL
360
361 wxString m_title; // the menu title or label
362 wxMenuItemList m_items; // the list of menu items
363
364 wxWindow *m_invokingWindow; // for popup menus
717a57c2
VZ
365
366 long m_style; // combination of wxMENU_XXX flags
367
368 wxEvtHandler *m_eventHandler; // a pluggable in event handler
22f3361e 369
6d971354 370 static bool ms_locked;
4e32eea1 371
22f3361e 372 DECLARE_NO_COPY_CLASS(wxMenuBase)
717a57c2 373};
3dfac970
VZ
374
375// ----------------------------------------------------------------------------
376// wxMenuBar
377// ----------------------------------------------------------------------------
378
379class WXDLLEXPORT wxMenuBarBase : public wxWindow
380{
381public:
382 // default ctor
383 wxMenuBarBase();
384
385 // dtor will delete all menus we own
386 virtual ~wxMenuBarBase();
387
388 // menu bar construction
389 // ---------------------
390
4e32eea1 391 // append a menu to the end of menubar, return true if ok
3dfac970
VZ
392 virtual bool Append(wxMenu *menu, const wxString& title);
393
4e32eea1 394 // insert a menu before the given position into the menubar, return true
3dfac970
VZ
395 // if inserted ok
396 virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
397
398 // menu bar items access
399 // ---------------------
400
401 // get the number of menus in the menu bar
402 size_t GetMenuCount() const { return m_menus.GetCount(); }
403
404 // get the menu at given position
405 wxMenu *GetMenu(size_t pos) const;
406
407 // replace the menu at given position with another one, returns the
408 // previous menu (which should be deleted by the caller)
409 virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
410
411 // delete the menu at given position from the menu bar, return the pointer
412 // to the menu (which should be deleted by the caller)
413 virtual wxMenu *Remove(size_t pos);
414
415 // enable or disable a submenu
416 virtual void EnableTop(size_t pos, bool enable) = 0;
417
1e6feb95 418 // is the menu enabled?
4e32eea1 419 virtual bool IsEnabledTop(size_t WXUNUSED(pos)) const { return true; }
1e6feb95 420
3dfac970
VZ
421 // get or change the label of the menu at given position
422 virtual void SetLabelTop(size_t pos, const wxString& label) = 0;
423 virtual wxString GetLabelTop(size_t pos) const = 0;
424
425 // item search
426 // -----------
427
428 // by menu and item names, returns wxNOT_FOUND if not found or id of the
429 // found item
1e6feb95 430 virtual int FindMenuItem(const wxString& menu, const wxString& item) const;
3dfac970
VZ
431
432 // find item by id (in any menu), returns NULL if not found
433 //
434 // if menu is !NULL, it will be filled with wxMenu this item belongs to
d9e2e4c2 435 virtual wxMenuItem* FindItem(int itemid, wxMenu **menu = NULL) const;
3dfac970 436
52130557 437 // find menu by its caption, return wxNOT_FOUND on failure
270e8b6a 438 int FindMenu(const wxString& title) const;
52130557 439
3dfac970
VZ
440 // item access
441 // -----------
442
443 // all these functions just use FindItem() and then call an appropriate
444 // method on it
445 //
446 // NB: under MSW, these methods can only be used after the menubar had
447 // been attached to the frame
448
d9e2e4c2
DE
449 void Enable(int itemid, bool enable);
450 void Check(int itemid, bool check);
451 bool IsChecked(int itemid) const;
452 bool IsEnabled(int itemid) const;
3dfac970 453
d9e2e4c2
DE
454 void SetLabel(int itemid, const wxString &label);
455 wxString GetLabel(int itemid) const;
3dfac970 456
d9e2e4c2
DE
457 void SetHelpString(int itemid, const wxString& helpString);
458 wxString GetHelpString(int itemid) const;
3dfac970 459
1e6feb95
VZ
460 // implementation helpers
461
462 // get the frame we are attached to (may return NULL)
463 wxFrame *GetFrame() const { return m_menuBarFrame; }
464
4e32eea1 465 // returns true if we're attached to a frame
1e6feb95
VZ
466 bool IsAttached() const { return GetFrame() != NULL; }
467
468 // associate the menubar with the frame
469 virtual void Attach(wxFrame *frame);
470
471 // called before deleting the menubar normally
472 virtual void Detach();
473
9874b4ee 474 // need to override these ones to avoid virtual function hiding
4e32eea1 475 virtual bool Enable(bool enable = true) { return wxWindow::Enable(enable); }
9874b4ee 476 virtual void SetLabel(const wxString& s) { wxWindow::SetLabel(s); }
3dfac970
VZ
477 virtual wxString GetLabel() const { return wxWindow::GetLabel(); }
478
1e6feb95 479 // don't want menu bars to accept the focus by tabbing to them
4e32eea1 480 virtual bool AcceptsFocusFromKeyboard() const { return false; }
1e6feb95 481
3dfac970
VZ
482protected:
483 // the list of all our menus
484 wxMenuList m_menus;
1e6feb95
VZ
485
486 // the frame we are attached to (may be NULL)
487 wxFrame *m_menuBarFrame;
22f3361e
VZ
488
489 DECLARE_NO_COPY_CLASS(wxMenuBarBase)
3dfac970
VZ
490};
491
492// ----------------------------------------------------------------------------
493// include the real class declaration
494// ----------------------------------------------------------------------------
495
496#ifdef wxUSE_BASE_CLASSES_ONLY
497 #define wxMenuItem wxMenuItemBase
498#else // !wxUSE_BASE_CLASSES_ONLY
1e6feb95
VZ
499#if defined(__WXUNIVERSAL__)
500 #include "wx/univ/menu.h"
4055ed82 501#elif defined(__WXPALMOS__)
ffecfa5a 502 #include "wx/palmos/menu.h"
1e6feb95 503#elif defined(__WXMSW__)
3dfac970 504 #include "wx/msw/menu.h"
2049ba38 505#elif defined(__WXMOTIF__)
3dfac970 506 #include "wx/motif/menu.h"
2049ba38 507#elif defined(__WXGTK__)
3dfac970 508 #include "wx/gtk/menu.h"
34138703 509#elif defined(__WXMAC__)
3dfac970 510 #include "wx/mac/menu.h"
e64df9bc
DE
511#elif defined(__WXCOCOA__)
512 #include "wx/cocoa/menu.h"
1777b9bb 513#elif defined(__WXPM__)
3dfac970 514 #include "wx/os2/menu.h"
c801d85f 515#endif
3dfac970
VZ
516#endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
517
1e6feb95
VZ
518#endif // wxUSE_MENUS
519
c801d85f 520#endif
34138703 521 // _WX_MENU_H_BASE_