1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu and wxMenuBar classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_MENU_H_BASE_
13 #define _WX_MENU_H_BASE_
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 // ----------------------------------------------------------------------------
42 // conditional compilation
43 // ----------------------------------------------------------------------------
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
52 #define wxUSE_MENU_CALLBACK 0
53 #endif // WXWIN_COMPATIBILITY_2
54 #endif // !defined(wxUSE_MENU_CALLBACK)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 class WXDLLEXPORT wxMenuBase
: public wxEvtHandler
64 static wxMenu
*New(const wxString
& title
= wxEmptyString
, long style
= 0);
67 wxMenuBase(const wxString
& title
, long style
= 0) : m_title(title
)
69 wxMenuBase(long style
= 0)
72 // dtor deletes all the menu items we own
73 virtual ~wxMenuBase();
78 // append a normal item to the menu
81 const wxString
& help
= wxEmptyString
,
82 bool isCheckable
= FALSE
)
84 DoAppend(wxMenuItem::New((wxMenu
*)this, id
, text
, help
, isCheckable
));
87 // append a separator to the menu
88 void AppendSeparator() { Append(wxID_SEPARATOR
, wxEmptyString
); }
94 const wxString
& help
= wxEmptyString
)
96 DoAppend(wxMenuItem::New((wxMenu
*)this, id
, text
, help
, FALSE
, submenu
));
99 // the most generic form of Append() - append anything
100 void Append(wxMenuItem
*item
) { 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 bool Insert(size_t pos
, wxMenuItem
*item
);
108 void Insert(size_t pos
,
110 const wxString
& text
,
111 const wxString
& help
= wxEmptyString
,
112 bool isCheckable
= FALSE
)
114 Insert(pos
, wxMenuItem::New((wxMenu
*)this, id
, text
, help
, isCheckable
));
117 // insert a separator
118 void InsertSeparator(size_t pos
)
120 Insert(pos
, wxMenuItem::New((wxMenu
*)this));
124 void Insert(size_t pos
,
126 const wxString
& text
,
128 const wxString
& help
= wxEmptyString
)
130 Insert(pos
, wxMenuItem::New((wxMenu
*)this, id
, text
, help
, FALSE
, submenu
));
133 // prepend an item to the menu
134 void Prepend(wxMenuItem
*item
)
140 const wxString
& text
,
141 const wxString
& help
= wxEmptyString
,
142 bool isCheckable
= FALSE
)
144 Insert(0u, id
, text
, help
, isCheckable
);
147 // insert a separator
148 void PrependSeparator()
155 const wxString
& text
,
157 const wxString
& help
= wxEmptyString
)
159 Insert(0u, id
, text
, submenu
, help
);
162 // detach an item from the menu, but don't delete it so that it can be
163 // added back later (but if it's not, the caller is responsible for
165 wxMenuItem
*Remove(int id
) { return Remove(FindChildItem(id
)); }
166 wxMenuItem
*Remove(wxMenuItem
*item
);
168 // delete an item from the menu (submenus are not destroyed by this
169 // function, see Destroy)
170 bool Delete(int id
) { return Delete(FindChildItem(id
)); }
171 bool Delete(wxMenuItem
*item
);
173 // delete the item from menu and destroy it (if it's a submenu)
174 bool Destroy(int id
) { return Destroy(FindChildItem(id
)); }
175 bool Destroy(wxMenuItem
*item
);
181 size_t GetMenuItemCount() const { return m_items
.GetCount(); }
183 const wxMenuItemList
& GetMenuItems() const { return m_items
; }
184 wxMenuItemList
& GetMenuItems() { return m_items
; }
187 virtual int FindItem(const wxString
& item
) const;
188 wxMenuItem
* FindItem(int id
, wxMenu
**menu
= NULL
) const;
190 // get/set items attributes
191 void Enable(int id
, bool enable
);
192 bool IsEnabled(int id
) const;
194 void Check(int id
, bool check
);
195 bool IsChecked(int id
) const;
197 void SetLabel(int id
, const wxString
& label
);
198 wxString
GetLabel(int id
) const;
200 virtual void SetHelpString(int id
, const wxString
& helpString
);
201 virtual wxString
GetHelpString(int id
) const;
207 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
208 const wxString
GetTitle() const { return m_title
; }
211 void SetClientData(void* clientData
) { m_clientData
= clientData
; }
212 void* GetClientData() const { return m_clientData
; }
215 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
216 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
219 void SetInvokingWindow(wxWindow
*win
) { m_invokingWindow
= win
; }
220 wxWindow
*GetInvokingWindow() const { return m_invokingWindow
; }
223 long GetStyle() const { return m_style
; }
225 // implementation helpers
226 // ----------------------
228 // Updates the UI for a menu and all submenus recursively. source is the
229 // object that has the update event handlers defined for it. If NULL, the
230 // menu or associated window will be used.
231 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
233 // get the menu bar this menu is attached to (may be NULL, always NULL for
235 wxMenuBar
*GetMenuBar() const { return m_menuBar
; }
237 // called when the menu is attached/detached to/from a menu bar
238 virtual void Attach(wxMenuBarBase
*menubar
);
239 virtual void Detach();
241 // is the menu attached to a menu bar (or is it a popup one)?
242 bool IsAttached() const { return m_menuBar
!= NULL
; }
244 // set/get the parent of this menu
245 void SetParent(wxMenu
*parent
) { m_menuParent
= parent
; }
246 wxMenu
*GetParent() const { return m_menuParent
; }
248 #if WXWIN_COMPATIBILITY
249 // compatibility: these functions are deprecated, use the new ones instead
250 bool Enabled(int id
) const { return IsEnabled(id
); }
251 bool Checked(int id
) const { return IsChecked(id
); }
253 wxMenuItem
* FindItemForId(int itemId
, wxMenu
**itemMenu
) const
254 { return FindItem(itemId
, itemMenu
); }
256 wxList
& GetItems() const { return (wxList
&)m_items
; }
257 #endif // WXWIN_COMPATIBILITY
259 #if wxUSE_MENU_CALLBACK || defined(__WXMOTIF__)
260 // wxWin 1.6x compatible menu event handling
261 wxFunction
GetCallback() const { return m_callback
; }
262 void Callback(const wxFunction func
) { m_callback
= func
; }
264 wxFunction m_callback
;
265 #endif // wxUSE_MENU_CALLBACK
267 // unlike FindItem(), this function doesn't recurse but only looks through
268 // our direct children and also may return the index of the found child if
270 wxMenuItem
*FindChildItem(int id
, size_t *pos
= NULL
) const;
272 // called to generate a wxCommandEvent, return TRUE if it was processed,
275 // the checked parameter may have boolean value or -1 for uncheckable items
276 bool SendEvent(int id
, int checked
= -1);
279 // virtuals to override in derived classes
280 // ---------------------------------------
282 virtual bool DoAppend(wxMenuItem
*item
);
283 virtual bool DoInsert(size_t pos
, wxMenuItem
*item
);
285 virtual wxMenuItem
*DoRemove(wxMenuItem
*item
);
286 virtual bool DoDelete(wxMenuItem
*item
);
287 virtual bool DoDestroy(wxMenuItem
*item
);
292 // common part of all ctors
293 void Init(long style
);
295 // associate the submenu with this menu
296 void AddSubMenu(wxMenu
*submenu
);
298 wxMenuBar
*m_menuBar
; // menubar we belong to or NULL
299 wxMenu
*m_menuParent
; // parent menu or NULL
301 wxString m_title
; // the menu title or label
302 wxMenuItemList m_items
; // the list of menu items
304 wxWindow
*m_invokingWindow
; // for popup menus
305 void *m_clientData
; // associated with the menu
307 long m_style
; // combination of wxMENU_XXX flags
309 wxEvtHandler
*m_eventHandler
; // a pluggable in event handler
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 class WXDLLEXPORT wxMenuBarBase
: public wxWindow
322 // dtor will delete all menus we own
323 virtual ~wxMenuBarBase();
325 // menu bar construction
326 // ---------------------
328 // append a menu to the end of menubar, return TRUE if ok
329 virtual bool Append(wxMenu
*menu
, const wxString
& title
);
331 // insert a menu before the given position into the menubar, return TRUE
333 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
335 // menu bar items access
336 // ---------------------
338 // get the number of menus in the menu bar
339 size_t GetMenuCount() const { return m_menus
.GetCount(); }
341 // get the menu at given position
342 wxMenu
*GetMenu(size_t pos
) const;
344 // replace the menu at given position with another one, returns the
345 // previous menu (which should be deleted by the caller)
346 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
348 // delete the menu at given position from the menu bar, return the pointer
349 // to the menu (which should be deleted by the caller)
350 virtual wxMenu
*Remove(size_t pos
);
352 // enable or disable a submenu
353 virtual void EnableTop(size_t pos
, bool enable
) = 0;
355 // is the menu enabled?
356 virtual bool IsEnabledTop(size_t pos
) const { return TRUE
; }
358 // get or change the label of the menu at given position
359 virtual void SetLabelTop(size_t pos
, const wxString
& label
) = 0;
360 virtual wxString
GetLabelTop(size_t pos
) const = 0;
365 // by menu and item names, returns wxNOT_FOUND if not found or id of the
367 virtual int FindMenuItem(const wxString
& menu
, const wxString
& item
) const;
369 // find item by id (in any menu), returns NULL if not found
371 // if menu is !NULL, it will be filled with wxMenu this item belongs to
372 virtual wxMenuItem
* FindItem(int id
, wxMenu
**menu
= NULL
) const;
374 // find menu by its caption, return wxNOT_FOUND on failure
375 int FindMenu(const wxString
& title
) const;
380 // all these functions just use FindItem() and then call an appropriate
383 // NB: under MSW, these methods can only be used after the menubar had
384 // been attached to the frame
386 void Enable(int id
, bool enable
);
387 void Check(int id
, bool check
);
388 bool IsChecked(int id
) const;
389 bool IsEnabled(int id
) const;
391 void SetLabel(int id
, const wxString
&label
);
392 wxString
GetLabel(int id
) const;
394 void SetHelpString(int id
, const wxString
& helpString
);
395 wxString
GetHelpString(int id
) const;
397 // implementation helpers
399 // get the frame we are attached to (may return NULL)
400 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
402 // returns TRUE if we're attached to a frame
403 bool IsAttached() const { return GetFrame() != NULL
; }
405 // associate the menubar with the frame
406 virtual void Attach(wxFrame
*frame
);
408 // called before deleting the menubar normally
409 virtual void Detach();
411 // need to override these ones to avoid virtual function hiding
412 virtual bool Enable(bool enable
= TRUE
) { return wxWindow::Enable(enable
); }
413 virtual void SetLabel(const wxString
& s
) { wxWindow::SetLabel(s
); }
414 virtual wxString
GetLabel() const { return wxWindow::GetLabel(); }
416 // don't want menu bars to accept the focus by tabbing to them
417 virtual bool AcceptsFocusFromKeyboard() const { return FALSE
; }
419 // compatibility only: these functions are deprecated, use the new ones
421 #if WXWIN_COMPATIBILITY
422 bool Enabled(int id
) const { return IsEnabled(id
); }
423 bool Checked(int id
) const { return IsChecked(id
); }
425 wxMenuItem
* FindMenuItemById(int id
) const
426 { return FindItem(id
); }
427 wxMenuItem
* FindItemForId(int id
, wxMenu
**menu
= NULL
) const
428 { return FindItem(id
, menu
); }
429 #endif // WXWIN_COMPATIBILITY
432 // the list of all our menus
435 // the frame we are attached to (may be NULL)
436 wxFrame
*m_menuBarFrame
;
439 // ----------------------------------------------------------------------------
440 // include the real class declaration
441 // ----------------------------------------------------------------------------
443 #ifdef wxUSE_BASE_CLASSES_ONLY
444 #define wxMenuItem wxMenuItemBase
445 #else // !wxUSE_BASE_CLASSES_ONLY
446 #if defined(__WXUNIVERSAL__)
447 #include "wx/univ/menu.h"
448 #elif defined(__WXMSW__)
449 #include "wx/msw/menu.h"
450 #elif defined(__WXMOTIF__)
451 #include "wx/motif/menu.h"
452 #elif defined(__WXGTK__)
453 #include "wx/gtk/menu.h"
454 #elif defined(__WXQT__)
455 #include "wx/qt/menu.h"
456 #elif defined(__WXMAC__)
457 #include "wx/mac/menu.h"
458 #elif defined(__WXPM__)
459 #include "wx/os2/menu.h"
460 #elif defined(__WXSTUBS__)
461 #include "wx/stubs/menu.h"
463 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
465 #endif // wxUSE_MENUS