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 SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
212 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
215 void SetInvokingWindow(wxWindow
*win
) { m_invokingWindow
= win
; }
216 wxWindow
*GetInvokingWindow() const { return m_invokingWindow
; }
219 long GetStyle() const { return m_style
; }
221 // implementation helpers
222 // ----------------------
224 // Updates the UI for a menu and all submenus recursively. source is the
225 // object that has the update event handlers defined for it. If NULL, the
226 // menu or associated window will be used.
227 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
229 // get the menu bar this menu is attached to (may be NULL, always NULL for
231 wxMenuBar
*GetMenuBar() const { return m_menuBar
; }
233 // called when the menu is attached/detached to/from a menu bar
234 virtual void Attach(wxMenuBarBase
*menubar
);
235 virtual void Detach();
237 // is the menu attached to a menu bar (or is it a popup one)?
238 bool IsAttached() const { return m_menuBar
!= NULL
; }
240 // set/get the parent of this menu
241 void SetParent(wxMenu
*parent
) { m_menuParent
= parent
; }
242 wxMenu
*GetParent() const { return m_menuParent
; }
244 #if WXWIN_COMPATIBILITY
245 // compatibility: these functions are deprecated, use the new ones instead
246 bool Enabled(int id
) const { return IsEnabled(id
); }
247 bool Checked(int id
) const { return IsChecked(id
); }
249 wxMenuItem
* FindItemForId(int itemId
, wxMenu
**itemMenu
) const
250 { return FindItem(itemId
, itemMenu
); }
252 wxList
& GetItems() const { return (wxList
&)m_items
; }
253 #endif // WXWIN_COMPATIBILITY
255 #if wxUSE_MENU_CALLBACK || defined(__WXMOTIF__)
256 // wxWin 1.6x compatible menu event handling
257 wxFunction
GetCallback() const { return m_callback
; }
258 void Callback(const wxFunction func
) { m_callback
= func
; }
260 wxFunction m_callback
;
261 #endif // wxUSE_MENU_CALLBACK
263 // unlike FindItem(), this function doesn't recurse but only looks through
264 // our direct children and also may return the index of the found child if
266 wxMenuItem
*FindChildItem(int id
, size_t *pos
= NULL
) const;
268 // called to generate a wxCommandEvent, return TRUE if it was processed,
271 // the checked parameter may have boolean value or -1 for uncheckable items
272 bool SendEvent(int id
, int checked
= -1);
275 // virtuals to override in derived classes
276 // ---------------------------------------
278 virtual bool DoAppend(wxMenuItem
*item
);
279 virtual bool DoInsert(size_t pos
, wxMenuItem
*item
);
281 virtual wxMenuItem
*DoRemove(wxMenuItem
*item
);
282 virtual bool DoDelete(wxMenuItem
*item
);
283 virtual bool DoDestroy(wxMenuItem
*item
);
288 // common part of all ctors
289 void Init(long style
);
291 // associate the submenu with this menu
292 void AddSubMenu(wxMenu
*submenu
);
294 wxMenuBar
*m_menuBar
; // menubar we belong to or NULL
295 wxMenu
*m_menuParent
; // parent menu or NULL
297 wxString m_title
; // the menu title or label
298 wxMenuItemList m_items
; // the list of menu items
300 wxWindow
*m_invokingWindow
; // for popup menus
302 long m_style
; // combination of wxMENU_XXX flags
304 wxEvtHandler
*m_eventHandler
; // a pluggable in event handler
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 class WXDLLEXPORT wxMenuBarBase
: public wxWindow
317 // dtor will delete all menus we own
318 virtual ~wxMenuBarBase();
320 // menu bar construction
321 // ---------------------
323 // append a menu to the end of menubar, return TRUE if ok
324 virtual bool Append(wxMenu
*menu
, const wxString
& title
);
326 // insert a menu before the given position into the menubar, return TRUE
328 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
330 // menu bar items access
331 // ---------------------
333 // get the number of menus in the menu bar
334 size_t GetMenuCount() const { return m_menus
.GetCount(); }
336 // get the menu at given position
337 wxMenu
*GetMenu(size_t pos
) const;
339 // replace the menu at given position with another one, returns the
340 // previous menu (which should be deleted by the caller)
341 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
343 // delete the menu at given position from the menu bar, return the pointer
344 // to the menu (which should be deleted by the caller)
345 virtual wxMenu
*Remove(size_t pos
);
347 // enable or disable a submenu
348 virtual void EnableTop(size_t pos
, bool enable
) = 0;
350 // is the menu enabled?
351 virtual bool IsEnabledTop(size_t WXUNUSED(pos
)) const { return TRUE
; }
353 // get or change the label of the menu at given position
354 virtual void SetLabelTop(size_t pos
, const wxString
& label
) = 0;
355 virtual wxString
GetLabelTop(size_t pos
) const = 0;
360 // by menu and item names, returns wxNOT_FOUND if not found or id of the
362 virtual int FindMenuItem(const wxString
& menu
, const wxString
& item
) const;
364 // find item by id (in any menu), returns NULL if not found
366 // if menu is !NULL, it will be filled with wxMenu this item belongs to
367 virtual wxMenuItem
* FindItem(int id
, wxMenu
**menu
= NULL
) const;
369 // find menu by its caption, return wxNOT_FOUND on failure
370 int FindMenu(const wxString
& title
) const;
375 // all these functions just use FindItem() and then call an appropriate
378 // NB: under MSW, these methods can only be used after the menubar had
379 // been attached to the frame
381 void Enable(int id
, bool enable
);
382 void Check(int id
, bool check
);
383 bool IsChecked(int id
) const;
384 bool IsEnabled(int id
) const;
386 void SetLabel(int id
, const wxString
&label
);
387 wxString
GetLabel(int id
) const;
389 void SetHelpString(int id
, const wxString
& helpString
);
390 wxString
GetHelpString(int id
) const;
392 // implementation helpers
394 // get the frame we are attached to (may return NULL)
395 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
397 // returns TRUE if we're attached to a frame
398 bool IsAttached() const { return GetFrame() != NULL
; }
400 // associate the menubar with the frame
401 virtual void Attach(wxFrame
*frame
);
403 // called before deleting the menubar normally
404 virtual void Detach();
406 // need to override these ones to avoid virtual function hiding
407 virtual bool Enable(bool enable
= TRUE
) { return wxWindow::Enable(enable
); }
408 virtual void SetLabel(const wxString
& s
) { wxWindow::SetLabel(s
); }
409 virtual wxString
GetLabel() const { return wxWindow::GetLabel(); }
411 // don't want menu bars to accept the focus by tabbing to them
412 virtual bool AcceptsFocusFromKeyboard() const { return FALSE
; }
414 // compatibility only: these functions are deprecated, use the new ones
416 #if WXWIN_COMPATIBILITY
417 bool Enabled(int id
) const { return IsEnabled(id
); }
418 bool Checked(int id
) const { return IsChecked(id
); }
420 wxMenuItem
* FindMenuItemById(int id
) const
421 { return FindItem(id
); }
422 wxMenuItem
* FindItemForId(int id
, wxMenu
**menu
= NULL
) const
423 { return FindItem(id
, menu
); }
424 #endif // WXWIN_COMPATIBILITY
427 // the list of all our menus
430 // the frame we are attached to (may be NULL)
431 wxFrame
*m_menuBarFrame
;
434 // ----------------------------------------------------------------------------
435 // include the real class declaration
436 // ----------------------------------------------------------------------------
438 #ifdef wxUSE_BASE_CLASSES_ONLY
439 #define wxMenuItem wxMenuItemBase
440 #else // !wxUSE_BASE_CLASSES_ONLY
441 #if defined(__WXUNIVERSAL__)
442 #include "wx/univ/menu.h"
443 #elif defined(__WXMSW__)
444 #include "wx/msw/menu.h"
445 #elif defined(__WXMOTIF__)
446 #include "wx/motif/menu.h"
447 #elif defined(__WXGTK__)
448 #include "wx/gtk/menu.h"
449 #elif defined(__WXMAC__)
450 #include "wx/mac/menu.h"
451 #elif defined(__WXPM__)
452 #include "wx/os2/menu.h"
453 #elif defined(__WXSTUBS__)
454 #include "wx/stubs/menu.h"
456 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
458 #endif // wxUSE_MENUS