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