1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu and wxMenuBar classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_MENU_H_BASE_
13 #define _WX_MENU_H_BASE_
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "menubase.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/list.h" // for "template" list classes
26 #include "wx/window.h" // base class for wxMenuBar
28 // also include this one to ensure compatibility with old code which only
30 #include "wx/menuitem.h"
32 class WXDLLEXPORT wxMenu
;
33 class WXDLLEXPORT wxMenuBarBase
;
34 class WXDLLEXPORT wxMenuBar
;
35 class WXDLLEXPORT wxMenuItem
;
37 // pseudo template list classes
38 WX_DECLARE_EXPORTED_LIST(wxMenu
, wxMenuList
);
39 WX_DECLARE_EXPORTED_LIST(wxMenuItem
, wxMenuItemList
);
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 class WXDLLEXPORT wxMenuBase
: public wxEvtHandler
49 static wxMenu
*New(const wxString
& title
= wxEmptyString
, long style
= 0);
52 wxMenuBase(const wxString
& title
, long style
= 0) : m_title(title
)
54 wxMenuBase(long style
= 0)
57 // dtor deletes all the menu items we own
58 virtual ~wxMenuBase();
63 // append any kind of item (normal/check/radio/separator)
64 void Append(int itemid
,
66 const wxString
& help
= wxEmptyString
,
67 wxItemKind kind
= wxITEM_NORMAL
)
69 DoAppend(wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
, kind
));
72 // append a separator to the menu
73 void AppendSeparator() { Append(wxID_SEPARATOR
, wxEmptyString
); }
75 // append a check item
76 void AppendCheckItem(int itemid
,
78 const wxString
& help
= wxEmptyString
)
80 Append(itemid
, text
, help
, wxITEM_CHECK
);
83 // append a radio item
84 void AppendRadioItem(int itemid
,
86 const wxString
& help
= wxEmptyString
)
88 Append(itemid
, text
, help
, wxITEM_RADIO
);
92 void Append(int itemid
,
95 const wxString
& help
= wxEmptyString
)
97 DoAppend(wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
,
98 wxITEM_NORMAL
, submenu
));
101 // the most generic form of Append() - append anything
102 void Append(wxMenuItem
*item
) { DoAppend(item
); }
104 // insert a break in the menu (only works when appending the items, not
106 virtual void Break() { }
108 // insert an item before given position
109 bool Insert(size_t pos
, wxMenuItem
*item
);
111 // insert an item before given position
112 void Insert(size_t pos
,
114 const wxString
& text
,
115 const wxString
& help
= wxEmptyString
,
116 wxItemKind kind
= wxITEM_NORMAL
)
118 Insert(pos
, wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
, kind
));
121 // insert a separator
122 void InsertSeparator(size_t pos
)
124 Insert(pos
, wxMenuItem::New((wxMenu
*)this));
127 // insert a check item
128 void InsertCheckItem(size_t pos
,
130 const wxString
& text
,
131 const wxString
& help
= wxEmptyString
)
133 Insert(pos
, itemid
, text
, help
, wxITEM_CHECK
);
136 // insert a radio item
137 void InsertRadioItem(size_t pos
,
139 const wxString
& text
,
140 const wxString
& help
= wxEmptyString
)
142 Insert(pos
, itemid
, text
, help
, wxITEM_RADIO
);
146 void Insert(size_t pos
,
148 const wxString
& text
,
150 const wxString
& help
= wxEmptyString
)
152 Insert(pos
, wxMenuItem::New((wxMenu
*)this, itemid
, text
, help
,
153 wxITEM_NORMAL
, submenu
));
156 // prepend an item to the menu
157 void Prepend(wxMenuItem
*item
)
162 // prepend any item to the menu
163 void Prepend(int itemid
,
164 const wxString
& text
,
165 const wxString
& help
= wxEmptyString
,
166 wxItemKind kind
= wxITEM_NORMAL
)
168 Insert(0u, itemid
, text
, help
, kind
);
171 // prepend a separator
172 void PrependSeparator()
177 // prepend a check item
178 void PrependCheckItem(int itemid
,
179 const wxString
& text
,
180 const wxString
& help
= wxEmptyString
)
182 InsertCheckItem(0u, itemid
, text
, help
);
185 // prepend a radio item
186 void PrependRadioItem(int itemid
,
187 const wxString
& text
,
188 const wxString
& help
= wxEmptyString
)
190 InsertRadioItem(0u, itemid
, text
, help
);
194 void Prepend(int itemid
,
195 const wxString
& text
,
197 const wxString
& help
= wxEmptyString
)
199 Insert(0u, itemid
, text
, submenu
, help
);
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
205 wxMenuItem
*Remove(int itemid
) { return Remove(FindChildItem(itemid
)); }
206 wxMenuItem
*Remove(wxMenuItem
*item
);
208 // delete an item from the menu (submenus are not destroyed by this
209 // function, see Destroy)
210 bool Delete(int itemid
) { return Delete(FindChildItem(itemid
)); }
211 bool Delete(wxMenuItem
*item
);
213 // delete the item from menu and destroy it (if it's a submenu)
214 bool Destroy(int itemid
) { return Destroy(FindChildItem(itemid
)); }
215 bool Destroy(wxMenuItem
*item
);
221 size_t GetMenuItemCount() const { return m_items
.GetCount(); }
223 const wxMenuItemList
& GetMenuItems() const { return m_items
; }
224 wxMenuItemList
& GetMenuItems() { return m_items
; }
227 virtual int FindItem(const wxString
& item
) const;
228 wxMenuItem
* FindItem(int itemid
, wxMenu
**menu
= NULL
) const;
231 wxMenuItem
* FindItemByPosition(size_t position
) const;
233 // get/set items attributes
234 void Enable(int itemid
, bool enable
);
235 bool IsEnabled(int itemid
) const;
237 void Check(int itemid
, bool check
);
238 bool IsChecked(int itemid
) const;
240 void SetLabel(int itemid
, const wxString
& label
);
241 wxString
GetLabel(int itemid
) const;
243 virtual void SetHelpString(int itemid
, const wxString
& helpString
);
244 virtual wxString
GetHelpString(int itemid
) const;
250 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
251 const wxString
GetTitle() const { return m_title
; }
254 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
255 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
258 void SetInvokingWindow(wxWindow
*win
) { m_invokingWindow
= win
; }
259 wxWindow
*GetInvokingWindow() const { return m_invokingWindow
; }
262 long GetStyle() const { return m_style
; }
264 // implementation helpers
265 // ----------------------
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
);
272 // get the menu bar this menu is attached to (may be NULL, always NULL for
274 wxMenuBar
*GetMenuBar() const { return m_menuBar
; }
276 // called when the menu is attached/detached to/from a menu bar
277 virtual void Attach(wxMenuBarBase
*menubar
);
278 virtual void Detach();
280 // is the menu attached to a menu bar (or is it a popup one)?
281 bool IsAttached() const { return m_menuBar
!= NULL
; }
283 // set/get the parent of this menu
284 void SetParent(wxMenu
*parent
) { m_menuParent
= parent
; }
285 wxMenu
*GetParent() const { return m_menuParent
; }
287 // implementation only from now on
288 // -------------------------------
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
293 wxMenuItem
*FindChildItem(int itemid
, size_t *pos
= NULL
) const;
295 // called to generate a wxCommandEvent, return TRUE if it was processed,
298 // the checked parameter may have boolean value or -1 for uncheckable items
299 bool SendEvent(int itemid
, int checked
= -1);
301 // compatibility: these functions are deprecated, use the new ones instead
302 // -----------------------------------------------------------------------
304 // use the versions taking wxItem_XXX now instead, they're more readable
305 // and allow adding the radio items as well
306 void Append(int itemid
,
307 const wxString
& text
,
308 const wxString
& help
,
311 Append(itemid
, text
, help
, isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
);
314 void Insert(size_t pos
,
316 const wxString
& text
,
317 const wxString
& help
,
320 Insert(pos
, itemid
, text
, help
, isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
);
323 void Prepend(int itemid
,
324 const wxString
& text
,
325 const wxString
& help
,
328 Insert(0u, itemid
, text
, help
, isCheckable
);
331 #if WXWIN_COMPATIBILITY
332 bool Enabled(int itemid
) const { return IsEnabled(itemid
); }
333 bool Checked(int itemid
) const { return IsChecked(itemid
); }
335 wxMenuItem
* FindItemForId(int itemId
, wxMenu
**itemMenu
) const
336 { return FindItem(itemId
, itemMenu
); }
338 wxList
& GetItems() const { return (wxList
&)m_items
; }
339 #endif // WXWIN_COMPATIBILITY
342 // virtuals to override in derived classes
343 // ---------------------------------------
345 virtual bool DoAppend(wxMenuItem
*item
);
346 virtual bool DoInsert(size_t pos
, wxMenuItem
*item
);
348 virtual wxMenuItem
*DoRemove(wxMenuItem
*item
);
349 virtual bool DoDelete(wxMenuItem
*item
);
350 virtual bool DoDestroy(wxMenuItem
*item
);
355 // common part of all ctors
356 void Init(long style
);
358 // associate the submenu with this menu
359 void AddSubMenu(wxMenu
*submenu
);
361 wxMenuBar
*m_menuBar
; // menubar we belong to or NULL
362 wxMenu
*m_menuParent
; // parent menu or NULL
364 wxString m_title
; // the menu title or label
365 wxMenuItemList m_items
; // the list of menu items
367 wxWindow
*m_invokingWindow
; // for popup menus
369 long m_style
; // combination of wxMENU_XXX flags
371 wxEvtHandler
*m_eventHandler
; // a pluggable in event handler
373 DECLARE_NO_COPY_CLASS(wxMenuBase
)
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 class WXDLLEXPORT wxMenuBarBase
: public wxWindow
386 // dtor will delete all menus we own
387 virtual ~wxMenuBarBase();
389 // menu bar construction
390 // ---------------------
392 // append a menu to the end of menubar, return TRUE if ok
393 virtual bool Append(wxMenu
*menu
, const wxString
& title
);
395 // insert a menu before the given position into the menubar, return TRUE
397 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
399 // menu bar items access
400 // ---------------------
402 // get the number of menus in the menu bar
403 size_t GetMenuCount() const { return m_menus
.GetCount(); }
405 // get the menu at given position
406 wxMenu
*GetMenu(size_t pos
) const;
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
);
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
);
416 // enable or disable a submenu
417 virtual void EnableTop(size_t pos
, bool enable
) = 0;
419 // is the menu enabled?
420 virtual bool IsEnabledTop(size_t WXUNUSED(pos
)) const { return TRUE
; }
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;
429 // by menu and item names, returns wxNOT_FOUND if not found or id of the
431 virtual int FindMenuItem(const wxString
& menu
, const wxString
& item
) const;
433 // find item by id (in any menu), returns NULL if not found
435 // if menu is !NULL, it will be filled with wxMenu this item belongs to
436 virtual wxMenuItem
* FindItem(int itemid
, wxMenu
**menu
= NULL
) const;
438 // find menu by its caption, return wxNOT_FOUND on failure
439 int FindMenu(const wxString
& title
) const;
444 // all these functions just use FindItem() and then call an appropriate
447 // NB: under MSW, these methods can only be used after the menubar had
448 // been attached to the frame
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;
455 void SetLabel(int itemid
, const wxString
&label
);
456 wxString
GetLabel(int itemid
) const;
458 void SetHelpString(int itemid
, const wxString
& helpString
);
459 wxString
GetHelpString(int itemid
) const;
461 // implementation helpers
463 // get the frame we are attached to (may return NULL)
464 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
466 // returns TRUE if we're attached to a frame
467 bool IsAttached() const { return GetFrame() != NULL
; }
469 // associate the menubar with the frame
470 virtual void Attach(wxFrame
*frame
);
472 // called before deleting the menubar normally
473 virtual void Detach();
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
); }
478 virtual wxString
GetLabel() const { return wxWindow::GetLabel(); }
480 // don't want menu bars to accept the focus by tabbing to them
481 virtual bool AcceptsFocusFromKeyboard() const { return FALSE
; }
483 // compatibility only: these functions are deprecated, use the new ones
485 #if WXWIN_COMPATIBILITY
486 bool Enabled(int itemid
) const { return IsEnabled(itemid
); }
487 bool Checked(int itemid
) const { return IsChecked(itemid
); }
489 wxMenuItem
* FindMenuItemById(int itemid
) const
490 { return FindItem(itemid
); }
491 wxMenuItem
* FindItemForId(int itemid
, wxMenu
**menu
= NULL
) const
492 { return FindItem(itemid
, menu
); }
493 #endif // WXWIN_COMPATIBILITY
496 // the list of all our menus
499 // the frame we are attached to (may be NULL)
500 wxFrame
*m_menuBarFrame
;
502 DECLARE_NO_COPY_CLASS(wxMenuBarBase
)
505 // ----------------------------------------------------------------------------
506 // include the real class declaration
507 // ----------------------------------------------------------------------------
509 #ifdef wxUSE_BASE_CLASSES_ONLY
510 #define wxMenuItem wxMenuItemBase
511 #else // !wxUSE_BASE_CLASSES_ONLY
512 #if defined(__WXUNIVERSAL__)
513 #include "wx/univ/menu.h"
514 #elif defined(__WXMSW__)
515 #include "wx/msw/menu.h"
516 #elif defined(__WXMOTIF__)
517 #include "wx/motif/menu.h"
518 #elif defined(__WXGTK__)
519 #include "wx/gtk/menu.h"
520 #elif defined(__WXMAC__)
521 #include "wx/mac/menu.h"
522 #elif defined(__WXCOCOA__)
523 #include "wx/cocoa/menu.h"
524 #elif defined(__WXPM__)
525 #include "wx/os2/menu.h"
527 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
529 #endif // wxUSE_MENUS