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