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