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