1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu and wxMenuBar classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_MENU_H_BASE_
13 #define _WX_MENU_H_BASE_
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 #include "wx/list.h" // for "template" list classes
24 #include "wx/window.h" // base class for wxMenuBar
26 // also include this one to ensure compatibility with old code which only
28 #include "wx/menuitem.h"
30 class WXDLLEXPORT wxMenu
;
31 class WXDLLEXPORT wxMenuBarBase
;
32 class WXDLLEXPORT wxMenuBar
;
33 class WXDLLEXPORT wxMenuItem
;
35 // pseudo template list classes
36 WX_DECLARE_EXPORTED_LIST(wxMenu
, wxMenuList
);
37 WX_DECLARE_EXPORTED_LIST(wxMenuItem
, wxMenuItemList
);
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 class WXDLLEXPORT wxMenuBase
: public wxEvtHandler
47 static wxMenu
*New(const wxString
& title
= wxEmptyString
, long style
= 0);
50 wxMenuBase(const wxString
& title
, long style
= 0) : m_title(title
)
52 wxMenuBase(long style
= 0)
55 // dtor deletes all the menu items we own
56 virtual ~wxMenuBase();
61 // append any kind of item (normal/check/radio/separator)
62 wxMenuItem
* Append(int itemid
,
64 const wxString
& help
= wxEmptyString
,
65 wxItemKind kind
= wxITEM_NORMAL
)
67 return DoAppend(wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
, kind
));
70 // append a separator to the menu
71 wxMenuItem
* AppendSeparator() { return Append(wxID_SEPARATOR
, wxEmptyString
); }
73 // append a check item
74 wxMenuItem
* AppendCheckItem(int itemid
,
76 const wxString
& help
= wxEmptyString
)
78 return Append(itemid
, text
, help
, wxITEM_CHECK
);
81 // append a radio item
82 wxMenuItem
* AppendRadioItem(int itemid
,
84 const wxString
& help
= wxEmptyString
)
86 return Append(itemid
, text
, help
, wxITEM_RADIO
);
90 wxMenuItem
* Append(int itemid
,
93 const wxString
& help
= wxEmptyString
)
95 return DoAppend(wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
,
96 wxITEM_NORMAL
, submenu
));
99 // the most generic form of Append() - append anything
100 wxMenuItem
* Append(wxMenuItem
*item
) { return DoAppend(item
); }
102 // insert a break in the menu (only works when appending the items, not
104 virtual void Break() { }
106 // insert an item before given position
107 wxMenuItem
* Insert(size_t pos
, wxMenuItem
*item
);
109 // insert an item before given position
110 wxMenuItem
* Insert(size_t pos
,
112 const wxString
& text
,
113 const wxString
& help
= wxEmptyString
,
114 wxItemKind kind
= wxITEM_NORMAL
)
116 return Insert(pos
, wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
, kind
));
119 // insert a separator
120 wxMenuItem
* InsertSeparator(size_t pos
)
122 return Insert(pos
, wxMenuItem::New((wxMenu
*)this, wxID_SEPARATOR
));
125 // insert a check item
126 wxMenuItem
* InsertCheckItem(size_t pos
,
128 const wxString
& text
,
129 const wxString
& help
= wxEmptyString
)
131 return Insert(pos
, itemid
, text
, help
, wxITEM_CHECK
);
134 // insert a radio item
135 wxMenuItem
* InsertRadioItem(size_t pos
,
137 const wxString
& text
,
138 const wxString
& help
= wxEmptyString
)
140 return Insert(pos
, itemid
, text
, help
, wxITEM_RADIO
);
144 wxMenuItem
* Insert(size_t pos
,
146 const wxString
& text
,
148 const wxString
& help
= wxEmptyString
)
150 return Insert(pos
, wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
,
151 wxITEM_NORMAL
, submenu
));
154 // prepend an item to the menu
155 wxMenuItem
* Prepend(wxMenuItem
*item
)
157 return Insert(0u, item
);
160 // prepend any item to the menu
161 wxMenuItem
* Prepend(int itemid
,
162 const wxString
& text
,
163 const wxString
& help
= wxEmptyString
,
164 wxItemKind kind
= wxITEM_NORMAL
)
166 return Insert(0u, itemid
, text
, help
, kind
);
169 // prepend a separator
170 wxMenuItem
* PrependSeparator()
172 return InsertSeparator(0u);
175 // prepend a check item
176 wxMenuItem
* PrependCheckItem(int itemid
,
177 const wxString
& text
,
178 const wxString
& help
= wxEmptyString
)
180 return InsertCheckItem(0u, itemid
, text
, help
);
183 // prepend a radio item
184 wxMenuItem
* PrependRadioItem(int itemid
,
185 const wxString
& text
,
186 const wxString
& help
= wxEmptyString
)
188 return InsertRadioItem(0u, itemid
, text
, help
);
192 wxMenuItem
* Prepend(int itemid
,
193 const wxString
& text
,
195 const wxString
& help
= wxEmptyString
)
197 return Insert(0u, itemid
, text
, submenu
, help
);
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
203 wxMenuItem
*Remove(int itemid
) { return Remove(FindChildItem(itemid
)); }
204 wxMenuItem
*Remove(wxMenuItem
*item
);
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
);
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
);
219 size_t GetMenuItemCount() const { return m_items
.GetCount(); }
221 const wxMenuItemList
& GetMenuItems() const { return m_items
; }
222 wxMenuItemList
& GetMenuItems() { return m_items
; }
225 virtual int FindItem(const wxString
& item
) const;
226 wxMenuItem
* FindItem(int itemid
, wxMenu
**menu
= NULL
) const;
229 wxMenuItem
* FindItemByPosition(size_t position
) const;
231 // get/set items attributes
232 void Enable(int itemid
, bool enable
);
233 bool IsEnabled(int itemid
) const;
235 void Check(int itemid
, bool check
);
236 bool IsChecked(int itemid
) const;
238 void SetLabel(int itemid
, const wxString
& label
);
239 wxString
GetLabel(int itemid
) const;
241 virtual void SetHelpString(int itemid
, const wxString
& helpString
);
242 virtual wxString
GetHelpString(int itemid
) const;
248 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
249 const wxString
GetTitle() const { return m_title
; }
252 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
253 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
256 void SetInvokingWindow(wxWindow
*win
) { m_invokingWindow
= win
; }
257 wxWindow
*GetInvokingWindow() const { return m_invokingWindow
; }
260 long GetStyle() const { return m_style
; }
262 // implementation helpers
263 // ----------------------
265 // Updates the UI for a menu and all submenus recursively. source is the
266 // object that has the update event handlers defined for it. If NULL, the
267 // menu or associated window will be used.
268 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
270 // get the menu bar this menu is attached to (may be NULL, always NULL for
271 // popup menus). Traverse up the menu hierarchy to find it.
272 wxMenuBar
*GetMenuBar() const;
274 // called when the menu is attached/detached to/from a menu bar
275 virtual void Attach(wxMenuBarBase
*menubar
);
276 virtual void Detach();
278 // is the menu attached to a menu bar (or is it a popup one)?
279 bool IsAttached() const { return GetMenuBar() != NULL
; }
281 // set/get the parent of this menu
282 void SetParent(wxMenu
*parent
) { m_menuParent
= parent
; }
283 wxMenu
*GetParent() const { return m_menuParent
; }
285 // implementation only from now on
286 // -------------------------------
288 // unlike FindItem(), this function doesn't recurse but only looks through
289 // our direct children and also may return the index of the found child if
291 wxMenuItem
*FindChildItem(int itemid
, size_t *pos
= NULL
) const;
293 // called to generate a wxCommandEvent, return true if it was processed,
296 // the checked parameter may have boolean value or -1 for uncheckable items
297 bool SendEvent(int itemid
, int checked
= -1);
299 // compatibility: these functions are deprecated, use the new ones instead
300 // -----------------------------------------------------------------------
302 // use the versions taking wxItem_XXX now instead, they're more readable
303 // and allow adding the radio items as well
304 void Append(int itemid
,
305 const wxString
& text
,
306 const wxString
& help
,
309 Append(itemid
, text
, help
, isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
);
312 void Insert(size_t pos
,
314 const wxString
& text
,
315 const wxString
& help
,
318 Insert(pos
, itemid
, text
, help
, isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
);
321 void Prepend(int itemid
,
322 const wxString
& text
,
323 const wxString
& help
,
326 Insert(0u, itemid
, text
, help
, isCheckable
);
329 static void LockAccels(bool locked
)
335 // virtuals to override in derived classes
336 // ---------------------------------------
338 virtual wxMenuItem
* DoAppend(wxMenuItem
*item
);
339 virtual wxMenuItem
* DoInsert(size_t pos
, wxMenuItem
*item
);
341 virtual wxMenuItem
*DoRemove(wxMenuItem
*item
);
342 virtual bool DoDelete(wxMenuItem
*item
);
343 virtual bool DoDestroy(wxMenuItem
*item
);
348 // common part of all ctors
349 void Init(long style
);
351 // associate the submenu with this menu
352 void AddSubMenu(wxMenu
*submenu
);
354 wxMenuBar
*m_menuBar
; // menubar we belong to or NULL
355 wxMenu
*m_menuParent
; // parent menu or NULL
357 wxString m_title
; // the menu title or label
358 wxMenuItemList m_items
; // the list of menu items
360 wxWindow
*m_invokingWindow
; // for popup menus
362 long m_style
; // combination of wxMENU_XXX flags
364 wxEvtHandler
*m_eventHandler
; // a pluggable in event handler
366 static bool ms_locked
;
368 DECLARE_NO_COPY_CLASS(wxMenuBase
)
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 class WXDLLEXPORT wxMenuBarBase
: public wxWindow
381 // dtor will delete all menus we own
382 virtual ~wxMenuBarBase();
384 // menu bar construction
385 // ---------------------
387 // append a menu to the end of menubar, return true if ok
388 virtual bool Append(wxMenu
*menu
, const wxString
& title
);
390 // insert a menu before the given position into the menubar, return true
392 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
394 // menu bar items access
395 // ---------------------
397 // get the number of menus in the menu bar
398 size_t GetMenuCount() const { return m_menus
.GetCount(); }
400 // get the menu at given position
401 wxMenu
*GetMenu(size_t pos
) const;
403 // replace the menu at given position with another one, returns the
404 // previous menu (which should be deleted by the caller)
405 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
407 // delete the menu at given position from the menu bar, return the pointer
408 // to the menu (which should be deleted by the caller)
409 virtual wxMenu
*Remove(size_t pos
);
411 // enable or disable a submenu
412 virtual void EnableTop(size_t pos
, bool enable
) = 0;
414 // is the menu enabled?
415 virtual bool IsEnabledTop(size_t WXUNUSED(pos
)) const { return true; }
417 // get or change the label of the menu at given position
418 virtual void SetLabelTop(size_t pos
, const wxString
& label
) = 0;
419 virtual wxString
GetLabelTop(size_t pos
) const = 0;
424 // by menu and item names, returns wxNOT_FOUND if not found or id of the
426 virtual int FindMenuItem(const wxString
& menu
, const wxString
& item
) const;
428 // find item by id (in any menu), returns NULL if not found
430 // if menu is !NULL, it will be filled with wxMenu this item belongs to
431 virtual wxMenuItem
* FindItem(int itemid
, wxMenu
**menu
= NULL
) const;
433 // find menu by its caption, return wxNOT_FOUND on failure
434 int FindMenu(const wxString
& title
) const;
439 // all these functions just use FindItem() and then call an appropriate
442 // NB: under MSW, these methods can only be used after the menubar had
443 // been attached to the frame
445 void Enable(int itemid
, bool enable
);
446 void Check(int itemid
, bool check
);
447 bool IsChecked(int itemid
) const;
448 bool IsEnabled(int itemid
) const;
449 virtual bool IsEnabled() const { return wxWindow::IsEnabled(); }
451 void SetLabel(int itemid
, const wxString
&label
);
452 wxString
GetLabel(int itemid
) const;
454 void SetHelpString(int itemid
, const wxString
& helpString
);
455 wxString
GetHelpString(int itemid
) const;
457 // implementation helpers
459 // get the frame we are attached to (may return NULL)
460 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
462 // returns true if we're attached to a frame
463 bool IsAttached() const { return GetFrame() != NULL
; }
465 // associate the menubar with the frame
466 virtual void Attach(wxFrame
*frame
);
468 // called before deleting the menubar normally
469 virtual void Detach();
471 // need to override these ones to avoid virtual function hiding
472 virtual bool Enable(bool enable
= true) { return wxWindow::Enable(enable
); }
473 virtual void SetLabel(const wxString
& s
) { wxWindow::SetLabel(s
); }
474 virtual wxString
GetLabel() const { return wxWindow::GetLabel(); }
476 // don't want menu bars to accept the focus by tabbing to them
477 virtual bool AcceptsFocusFromKeyboard() const { return false; }
480 // the list of all our menus
483 // the frame we are attached to (may be NULL)
484 wxFrame
*m_menuBarFrame
;
486 DECLARE_NO_COPY_CLASS(wxMenuBarBase
)
489 // ----------------------------------------------------------------------------
490 // include the real class declaration
491 // ----------------------------------------------------------------------------
493 #ifdef wxUSE_BASE_CLASSES_ONLY
494 #define wxMenuItem wxMenuItemBase
495 #else // !wxUSE_BASE_CLASSES_ONLY
496 #if defined(__WXUNIVERSAL__)
497 #include "wx/univ/menu.h"
498 #elif defined(__WXPALMOS__)
499 #include "wx/palmos/menu.h"
500 #elif defined(__WXMSW__)
501 #include "wx/msw/menu.h"
502 #elif defined(__WXMOTIF__)
503 #include "wx/motif/menu.h"
504 #elif defined(__WXGTK__)
505 #include "wx/gtk/menu.h"
506 #elif defined(__WXMAC__)
507 #include "wx/mac/menu.h"
508 #elif defined(__WXCOCOA__)
509 #include "wx/cocoa/menu.h"
510 #elif defined(__WXPM__)
511 #include "wx/os2/menu.h"
513 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
515 #endif // wxUSE_MENUS