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"
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 wxMenuBar
;
32 class WXDLLEXPORT wxMenuItem
;
34 // pseudo template list classes
35 WX_DECLARE_EXPORTED_LIST(wxMenu
, wxMenuList
);
36 WX_DECLARE_EXPORTED_LIST(wxMenuItem
, wxMenuItemList
);
38 // ----------------------------------------------------------------------------
39 // conditional compilation
40 // ----------------------------------------------------------------------------
42 // having callbacks in menus is a wxWin 1.6x feature which should be replaced
43 // with event tables in wxWin 2.xx code - however provide it because many
44 // people like it a lot by default
45 #ifndef wxUSE_MENU_CALLBACK
46 #if WXWIN_COMPATIBILITY_2
47 #define wxUSE_MENU_CALLBACK 1
49 #define wxUSE_MENU_CALLBACK 0
50 #endif // WXWIN_COMPATIBILITY_2
51 #endif // !defined(wxUSE_MENU_CALLBACK)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxMenuBase
: public wxEvtHandler
61 static wxMenu
*New(const wxString
& title
= wxEmptyString
, long style
= 0);
64 wxMenuBase(const wxString
& title
, long style
= 0) : m_title(title
)
66 wxMenuBase(long style
= 0)
69 // dtor deletes all the menu items we own
70 virtual ~wxMenuBase();
75 // append a normal item to the menu
78 const wxString
& help
= wxEmptyString
,
79 bool isCheckable
= FALSE
)
81 DoAppend(wxMenuItem::New((wxMenu
*)this, id
, text
, help
, isCheckable
));
84 // append a separator to the menu
85 void AppendSeparator() { Append(wxID_SEPARATOR
, wxEmptyString
); }
91 const wxString
& help
= wxEmptyString
)
93 DoAppend(wxMenuItem::New((wxMenu
*)this, id
, text
, help
, FALSE
, submenu
));
96 // the most generic form of Append() - append anything
97 void Append(wxMenuItem
*item
) { DoAppend(item
); }
99 // insert a break in the menu (only works when appending the items, not
101 virtual void Break() { }
103 // insert an item before given position
104 bool Insert(size_t pos
, wxMenuItem
*item
);
105 void Insert(size_t pos
,
107 const wxString
& text
,
108 const wxString
& help
= wxEmptyString
,
109 bool isCheckable
= FALSE
)
111 Insert(pos
, wxMenuItem::New((wxMenu
*)this, id
, text
, help
, isCheckable
));
114 // insert a separator
115 void InsertSeparator(size_t pos
)
117 Insert(pos
, wxMenuItem::New((wxMenu
*)this));
121 void Insert(size_t pos
,
123 const wxString
& text
,
125 const wxString
& help
= wxEmptyString
)
127 Insert(pos
, wxMenuItem::New((wxMenu
*)this, id
, text
, help
, FALSE
, submenu
));
130 // prepend an item to the menu
131 void Prepend(wxMenuItem
*item
)
137 const wxString
& text
,
138 const wxString
& help
= wxEmptyString
,
139 bool isCheckable
= FALSE
)
141 Insert(0u, id
, text
, help
, isCheckable
);
144 // insert a separator
145 void PrependSeparator()
152 const wxString
& text
,
154 const wxString
& help
= wxEmptyString
)
156 Insert(0u, id
, text
, submenu
, help
);
159 // detach an item from the menu, but don't delete it so that it can be
160 // added back later (but if it's not, the caller is responsible for
162 wxMenuItem
*Remove(int id
) { return Remove(FindChildItem(id
)); }
163 wxMenuItem
*Remove(wxMenuItem
*item
);
165 // delete an item from the menu (submenus are not destroyed by this
166 // function, see Destroy)
167 bool Delete(int id
) { return Delete(FindChildItem(id
)); }
168 bool Delete(wxMenuItem
*item
);
170 // delete the item from menu and destroy it (if it's a submenu)
171 bool Destroy(int id
) { return Destroy(FindChildItem(id
)); }
172 bool Destroy(wxMenuItem
*item
);
178 size_t GetMenuItemCount() const { return m_items
.GetCount(); }
180 const wxMenuItemList
& GetMenuItems() const { return m_items
; }
181 wxMenuItemList
& GetMenuItems() { return m_items
; }
184 virtual int FindItem(const wxString
& itemString
) const;
185 wxMenuItem
* FindItem(int id
, wxMenu
**menu
= NULL
) const;
187 // get/set items attributes
188 void Enable(int id
, bool enable
);
189 bool IsEnabled(int id
) const;
191 void Check(int id
, bool check
);
192 bool IsChecked(int id
) const;
194 void SetLabel(int id
, const wxString
& label
);
195 wxString
GetLabel(int id
) const;
197 virtual void SetHelpString(int id
, const wxString
& helpString
);
198 virtual wxString
GetHelpString(int id
) const;
204 virtual void SetTitle(const wxString
& title
) { m_title
= title
; }
205 const wxString
GetTitle() const { return m_title
; }
208 void SetClientData(void* clientData
) { m_clientData
= clientData
; }
209 void* GetClientData() const { return m_clientData
; }
212 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
213 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
216 void SetInvokingWindow(wxWindow
*win
) { m_invokingWindow
= win
; }
217 wxWindow
*GetInvokingWindow() const { return m_invokingWindow
; }
220 long GetStyle() const { return m_style
; }
222 // implementation helpers
223 // ----------------------
225 // Updates the UI for a menu and all submenus recursively. source is the
226 // object that has the update event handlers defined for it. If NULL, the
227 // menu or associated window will be used.
228 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
230 // is the menu attached to a menu bar (or is it a popup one)?
231 bool IsAttached() const { return m_menuBar
!= NULL
; }
233 // set/get the parent of this menu
234 void SetParent(wxMenu
*parent
) { m_menuParent
= parent
; }
235 wxMenu
*GetParent() const { return m_menuParent
; }
237 #if WXWIN_COMPATIBILITY
238 // compatibility: these functions are deprecated, use the new ones instead
239 bool Enabled(int id
) const { return IsEnabled(id
); }
240 bool Checked(int id
) const { return IsChecked(id
); }
242 wxMenuItem
* FindItemForId(int itemId
, wxMenu
**itemMenu
) const
243 { return FindItem(itemId
, itemMenu
); }
245 wxList
& GetItems() const { return (wxList
&)m_items
; }
246 #endif // WXWIN_COMPATIBILITY
248 #if wxUSE_MENU_CALLBACK
249 // wxWin 1.6x compatible menu event handling
250 wxFunction
GetCallback() const { return m_callback
; }
251 void Callback(const wxFunction func
) { m_callback
= func
; }
253 wxFunction m_callback
;
254 #endif // wxUSE_MENU_CALLBACK
256 // unlike FindItem(), this function doesn't recurse but only looks through
257 // our direct children and also may return the index of the found child if
259 wxMenuItem
*FindChildItem(int id
, size_t *pos
= NULL
) const;
262 // virtuals to override in derived classes
263 // ---------------------------------------
265 virtual bool DoAppend(wxMenuItem
*item
);
266 virtual bool DoInsert(size_t pos
, wxMenuItem
*item
);
268 virtual wxMenuItem
*DoRemove(wxMenuItem
*item
);
269 virtual bool DoDelete(wxMenuItem
*item
);
270 virtual bool DoDestroy(wxMenuItem
*item
);
275 // common part of all ctors
276 void Init(long style
);
279 wxMenuBar
*m_menuBar
; // menubar we belong to or NULL
280 wxMenu
*m_menuParent
; // parent menu or NULL
282 wxString m_title
; // the menu title or label
283 wxMenuItemList m_items
; // the list of menu items
285 wxWindow
*m_invokingWindow
; // for popup menus
286 void *m_clientData
; // associated with the menu
288 long m_style
; // combination of wxMENU_XXX flags
290 wxEvtHandler
*m_eventHandler
; // a pluggable in event handler
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 class WXDLLEXPORT wxMenuBarBase
: public wxWindow
303 // dtor will delete all menus we own
304 virtual ~wxMenuBarBase();
306 // menu bar construction
307 // ---------------------
309 // append a menu to the end of menubar, return TRUE if ok
310 virtual bool Append(wxMenu
*menu
, const wxString
& title
);
312 // insert a menu before the given position into the menubar, return TRUE
314 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
316 // menu bar items access
317 // ---------------------
319 // get the number of menus in the menu bar
320 size_t GetMenuCount() const { return m_menus
.GetCount(); }
322 // get the menu at given position
323 wxMenu
*GetMenu(size_t pos
) const;
325 // replace the menu at given position with another one, returns the
326 // previous menu (which should be deleted by the caller)
327 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
329 // delete the menu at given position from the menu bar, return the pointer
330 // to the menu (which should be deleted by the caller)
331 virtual wxMenu
*Remove(size_t pos
);
333 // enable or disable a submenu
334 virtual void EnableTop(size_t pos
, bool enable
) = 0;
336 // get or change the label of the menu at given position
337 virtual void SetLabelTop(size_t pos
, const wxString
& label
) = 0;
338 virtual wxString
GetLabelTop(size_t pos
) const = 0;
343 // by menu and item names, returns wxNOT_FOUND if not found or id of the
345 virtual int FindMenuItem(const wxString
& menuString
,
346 const wxString
& itemString
) const = 0;
348 // find item by id (in any menu), returns NULL if not found
350 // if menu is !NULL, it will be filled with wxMenu this item belongs to
351 virtual wxMenuItem
* FindItem(int id
, wxMenu
**menu
= NULL
) const = 0;
353 // find menu by its caption, return wxNOT_FOUND on failure
354 int FindMenu(const wxString
& title
) const;
359 // all these functions just use FindItem() and then call an appropriate
362 // NB: under MSW, these methods can only be used after the menubar had
363 // been attached to the frame
365 void Enable(int id
, bool enable
);
366 void Check(int id
, bool check
);
367 bool IsChecked(int id
) const;
368 bool IsEnabled(int id
) const;
370 void SetLabel(int id
, const wxString
&label
);
371 wxString
GetLabel(int id
) const;
373 void SetHelpString(int id
, const wxString
& helpString
);
374 wxString
GetHelpString(int id
) const;
376 // need to override these ones to avoid virtual function hiding
377 virtual bool Enable(bool enable
= TRUE
) { return wxWindow::Enable(enable
); }
378 virtual void SetLabel(const wxString
& s
) { wxWindow::SetLabel(s
); }
379 virtual wxString
GetLabel() const { return wxWindow::GetLabel(); }
381 // compatibility only: these functions are deprecated, use the new ones
383 #if WXWIN_COMPATIBILITY
384 bool Enabled(int id
) const { return IsEnabled(id
); }
385 bool Checked(int id
) const { return IsChecked(id
); }
387 wxMenuItem
* FindMenuItemById(int id
) const
388 { return FindItem(id
); }
389 wxMenuItem
* FindItemForId(int id
, wxMenu
**menu
= NULL
) const
390 { return FindItem(id
, menu
); }
391 #endif // WXWIN_COMPATIBILITY
394 // the list of all our menus
398 // ----------------------------------------------------------------------------
399 // include the real class declaration
400 // ----------------------------------------------------------------------------
402 #ifdef wxUSE_BASE_CLASSES_ONLY
403 #define wxMenuItem wxMenuItemBase
404 #else // !wxUSE_BASE_CLASSES_ONLY
405 #if defined(__WXMSW__)
406 #include "wx/msw/menu.h"
407 #elif defined(__WXMOTIF__)
408 #include "wx/motif/menu.h"
409 #elif defined(__WXGTK__)
410 #include "wx/gtk/menu.h"
411 #elif defined(__WXQT__)
412 #include "wx/qt/menu.h"
413 #elif defined(__WXMAC__)
414 #include "wx/mac/menu.h"
415 #elif defined(__WXPM__)
416 #include "wx/os2/menu.h"
417 #elif defined(__WXSTUBS__)
418 #include "wx/stubs/menu.h"
420 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY