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