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