]> git.saurik.com Git - wxWidgets.git/blame - include/wx/menu.h
Unicode compilation fixes.
[wxWidgets.git] / include / wx / menu.h
CommitLineData
3dfac970
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/menu.h
3// Purpose: wxMenu and wxMenuBar classes
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 26.10.99
7// RCS-ID: $Id$
8// Copyright: (c) wxWindows team
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_MENU_H_BASE_
13#define _WX_MENU_H_BASE_
c801d85f 14
3dfac970
VZ
15#ifdef __GNUG__
16 #pragma interface "menubase.h"
17#endif
18
1e6feb95
VZ
19#if wxUSE_MENUS
20
3dfac970
VZ
21// ----------------------------------------------------------------------------
22// headers
23// ----------------------------------------------------------------------------
24
717a57c2 25#include "wx/list.h" // for "template" list classes
3dfac970
VZ
26#include "wx/window.h" // base class for wxMenuBar
27
717a57c2
VZ
28// also include this one to ensure compatibility with old code which only
29// included wx/menu.h
30#include "wx/menuitem.h"
31
3dfac970 32class WXDLLEXPORT wxMenu;
1e6feb95 33class WXDLLEXPORT wxMenuBarBase;
3dfac970
VZ
34class WXDLLEXPORT wxMenuBar;
35class WXDLLEXPORT wxMenuItem;
36
717a57c2 37// pseudo template list classes
f6bcfd97
BP
38WX_DECLARE_EXPORTED_LIST(wxMenu, wxMenuList);
39WX_DECLARE_EXPORTED_LIST(wxMenuItem, wxMenuItemList);
717a57c2 40
9739d9ee
VZ
41// ----------------------------------------------------------------------------
42// conditional compilation
43// ----------------------------------------------------------------------------
44
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
51 #else
52 #define wxUSE_MENU_CALLBACK 0
53 #endif // WXWIN_COMPATIBILITY_2
54#endif // !defined(wxUSE_MENU_CALLBACK)
55
3dfac970
VZ
56// ----------------------------------------------------------------------------
57// wxMenu
58// ----------------------------------------------------------------------------
59
717a57c2
VZ
60class WXDLLEXPORT wxMenuBase : public wxEvtHandler
61{
62public:
63 // create a menu
64 static wxMenu *New(const wxString& title = wxEmptyString, long style = 0);
65
66 // ctors
67 wxMenuBase(const wxString& title, long style = 0) : m_title(title)
68 { Init(style); }
69 wxMenuBase(long style = 0)
70 { Init(style); }
71
72 // dtor deletes all the menu items we own
73 virtual ~wxMenuBase();
74
75 // menu construction
76 // -----------------
77
717a57c2
VZ
78 // append a normal item to the menu
79 void Append(int id,
80 const wxString& text,
81 const wxString& help = wxEmptyString,
82 bool isCheckable = FALSE)
83 {
84 DoAppend(wxMenuItem::New((wxMenu *)this, id, text, help, isCheckable));
85 }
86
0e528b99
JS
87 // append a separator to the menu
88 void AppendSeparator() { Append(wxID_SEPARATOR, wxEmptyString); }
89
717a57c2
VZ
90 // append a submenu
91 void Append(int id,
92 const wxString& text,
93 wxMenu *submenu,
94 const wxString& help = wxEmptyString)
95 {
96 DoAppend(wxMenuItem::New((wxMenu *)this, id, text, help, FALSE, submenu));
97 }
98
99 // the most generic form of Append() - append anything
100 void Append(wxMenuItem *item) { DoAppend(item); }
101
102 // insert a break in the menu (only works when appending the items, not
103 // inserting them)
104 virtual void Break() { }
105
106 // insert an item before given position
107 bool Insert(size_t pos, wxMenuItem *item);
f6bcfd97
BP
108 void Insert(size_t pos,
109 int id,
110 const wxString& text,
111 const wxString& help = wxEmptyString,
112 bool isCheckable = FALSE)
113 {
114 Insert(pos, wxMenuItem::New((wxMenu *)this, id, text, help, isCheckable));
115 }
116
117 // insert a separator
118 void InsertSeparator(size_t pos)
119 {
120 Insert(pos, wxMenuItem::New((wxMenu *)this));
121 }
122
123 // insert a submenu
124 void Insert(size_t pos,
125 int id,
126 const wxString& text,
127 wxMenu *submenu,
128 const wxString& help = wxEmptyString)
129 {
130 Insert(pos, wxMenuItem::New((wxMenu *)this, id, text, help, FALSE, submenu));
131 }
132
133 // prepend an item to the menu
134 void Prepend(wxMenuItem *item)
135 {
136 Insert(0u, item);
137 }
138
139 void Prepend(int id,
140 const wxString& text,
141 const wxString& help = wxEmptyString,
142 bool isCheckable = FALSE)
143 {
144 Insert(0u, id, text, help, isCheckable);
145 }
146
147 // insert a separator
148 void PrependSeparator()
149 {
150 InsertSeparator(0u);
151 }
152
153 // insert a submenu
154 void Prepend(int id,
155 const wxString& text,
156 wxMenu *submenu,
157 const wxString& help = wxEmptyString)
158 {
159 Insert(0u, id, text, submenu, help);
160 }
717a57c2
VZ
161
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
164 // deleting it!)
165 wxMenuItem *Remove(int id) { return Remove(FindChildItem(id)); }
166 wxMenuItem *Remove(wxMenuItem *item);
167
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);
172
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);
176
177 // menu items access
178 // -----------------
179
180 // get the items
181 size_t GetMenuItemCount() const { return m_items.GetCount(); }
182
183 const wxMenuItemList& GetMenuItems() const { return m_items; }
184 wxMenuItemList& GetMenuItems() { return m_items; }
185
186 // search
1e6feb95 187 virtual int FindItem(const wxString& item) const;
717a57c2
VZ
188 wxMenuItem* FindItem(int id, wxMenu **menu = NULL) const;
189
190 // get/set items attributes
191 void Enable(int id, bool enable);
192 bool IsEnabled(int id) const;
3dfac970 193
717a57c2
VZ
194 void Check(int id, bool check);
195 bool IsChecked(int id) const;
196
197 void SetLabel(int id, const wxString& label);
198 wxString GetLabel(int id) const;
199
200 virtual void SetHelpString(int id, const wxString& helpString);
201 virtual wxString GetHelpString(int id) const;
202
203 // misc accessors
204 // --------------
205
206 // the title
207 virtual void SetTitle(const wxString& title) { m_title = title; }
208 const wxString GetTitle() const { return m_title; }
209
210 // client data
211 void SetClientData(void* clientData) { m_clientData = clientData; }
212 void* GetClientData() const { return m_clientData; }
213
214 // event handler
215 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
216 wxEvtHandler *GetEventHandler() const { return m_eventHandler; }
217
218 // invoking window
219 void SetInvokingWindow(wxWindow *win) { m_invokingWindow = win; }
220 wxWindow *GetInvokingWindow() const { return m_invokingWindow; }
221
222 // style
223 long GetStyle() const { return m_style; }
224
225 // implementation helpers
226 // ----------------------
227
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);
232
1e6feb95
VZ
233 // get the menu bar this menu is attached to (may be NULL, always NULL for
234 // popup menus)
235 wxMenuBar *GetMenuBar() const { return m_menuBar; }
236
237 // called when the menu is attached/detached to/from a menu bar
238 virtual void Attach(wxMenuBarBase *menubar);
239 virtual void Detach();
240
717a57c2
VZ
241 // is the menu attached to a menu bar (or is it a popup one)?
242 bool IsAttached() const { return m_menuBar != NULL; }
243
244 // set/get the parent of this menu
245 void SetParent(wxMenu *parent) { m_menuParent = parent; }
246 wxMenu *GetParent() const { return m_menuParent; }
247
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); }
252
253 wxMenuItem* FindItemForId(int itemId, wxMenu **itemMenu) const
254 { return FindItem(itemId, itemMenu); }
255
256 wxList& GetItems() const { return (wxList &)m_items; }
c0ab6adf 257#endif // WXWIN_COMPATIBILITY
717a57c2 258
45f22d48 259#if wxUSE_MENU_CALLBACK || defined(__WXMOTIF__)
717a57c2
VZ
260 // wxWin 1.6x compatible menu event handling
261 wxFunction GetCallback() const { return m_callback; }
262 void Callback(const wxFunction func) { m_callback = func; }
9739d9ee 263
717a57c2 264 wxFunction m_callback;
9739d9ee 265#endif // wxUSE_MENU_CALLBACK
717a57c2 266
1987af7e
VZ
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
269 // pos != NULL
270 wxMenuItem *FindChildItem(int id, size_t *pos = NULL) const;
271
1e6feb95
VZ
272 // called to generate a wxCommandEvent, return TRUE if it was processed,
273 // FALSE otherwise
274 //
275 // the checked parameter may have boolean value or -1 for uncheckable items
276 bool SendEvent(int id, int checked = -1);
277
717a57c2
VZ
278protected:
279 // virtuals to override in derived classes
280 // ---------------------------------------
281
282 virtual bool DoAppend(wxMenuItem *item);
283 virtual bool DoInsert(size_t pos, wxMenuItem *item);
284
285 virtual wxMenuItem *DoRemove(wxMenuItem *item);
286 virtual bool DoDelete(wxMenuItem *item);
287 virtual bool DoDestroy(wxMenuItem *item);
288
289 // helpers
290 // -------
291
292 // common part of all ctors
293 void Init(long style);
294
1e6feb95
VZ
295 // associate the submenu with this menu
296 void AddSubMenu(wxMenu *submenu);
297
717a57c2
VZ
298 wxMenuBar *m_menuBar; // menubar we belong to or NULL
299 wxMenu *m_menuParent; // parent menu or NULL
300
301 wxString m_title; // the menu title or label
302 wxMenuItemList m_items; // the list of menu items
303
304 wxWindow *m_invokingWindow; // for popup menus
305 void *m_clientData; // associated with the menu
306
307 long m_style; // combination of wxMENU_XXX flags
308
309 wxEvtHandler *m_eventHandler; // a pluggable in event handler
310};
3dfac970
VZ
311
312// ----------------------------------------------------------------------------
313// wxMenuBar
314// ----------------------------------------------------------------------------
315
316class WXDLLEXPORT wxMenuBarBase : public wxWindow
317{
318public:
319 // default ctor
320 wxMenuBarBase();
321
322 // dtor will delete all menus we own
323 virtual ~wxMenuBarBase();
324
325 // menu bar construction
326 // ---------------------
327
328 // append a menu to the end of menubar, return TRUE if ok
329 virtual bool Append(wxMenu *menu, const wxString& title);
330
331 // insert a menu before the given position into the menubar, return TRUE
332 // if inserted ok
333 virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
334
335 // menu bar items access
336 // ---------------------
337
338 // get the number of menus in the menu bar
339 size_t GetMenuCount() const { return m_menus.GetCount(); }
340
341 // get the menu at given position
342 wxMenu *GetMenu(size_t pos) const;
343
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);
347
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);
351
352 // enable or disable a submenu
353 virtual void EnableTop(size_t pos, bool enable) = 0;
354
1e6feb95 355 // is the menu enabled?
d699f48b 356 virtual bool IsEnabledTop(size_t WXUNUSED(pos)) const { return TRUE; }
1e6feb95 357
3dfac970
VZ
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;
361
362 // item search
363 // -----------
364
365 // by menu and item names, returns wxNOT_FOUND if not found or id of the
366 // found item
1e6feb95 367 virtual int FindMenuItem(const wxString& menu, const wxString& item) const;
3dfac970
VZ
368
369 // find item by id (in any menu), returns NULL if not found
370 //
371 // if menu is !NULL, it will be filled with wxMenu this item belongs to
1e6feb95 372 virtual wxMenuItem* FindItem(int id, wxMenu **menu = NULL) const;
3dfac970 373
52130557 374 // find menu by its caption, return wxNOT_FOUND on failure
270e8b6a 375 int FindMenu(const wxString& title) const;
52130557 376
3dfac970
VZ
377 // item access
378 // -----------
379
380 // all these functions just use FindItem() and then call an appropriate
381 // method on it
382 //
383 // NB: under MSW, these methods can only be used after the menubar had
384 // been attached to the frame
385
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;
390
391 void SetLabel(int id, const wxString &label);
392 wxString GetLabel(int id) const;
393
394 void SetHelpString(int id, const wxString& helpString);
395 wxString GetHelpString(int id) const;
396
1e6feb95
VZ
397 // implementation helpers
398
399 // get the frame we are attached to (may return NULL)
400 wxFrame *GetFrame() const { return m_menuBarFrame; }
401
402 // returns TRUE if we're attached to a frame
403 bool IsAttached() const { return GetFrame() != NULL; }
404
405 // associate the menubar with the frame
406 virtual void Attach(wxFrame *frame);
407
408 // called before deleting the menubar normally
409 virtual void Detach();
410
9874b4ee
VZ
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); }
3dfac970
VZ
414 virtual wxString GetLabel() const { return wxWindow::GetLabel(); }
415
1e6feb95
VZ
416 // don't want menu bars to accept the focus by tabbing to them
417 virtual bool AcceptsFocusFromKeyboard() const { return FALSE; }
418
3dfac970
VZ
419 // compatibility only: these functions are deprecated, use the new ones
420 // instead
717a57c2 421#if WXWIN_COMPATIBILITY
3dfac970
VZ
422 bool Enabled(int id) const { return IsEnabled(id); }
423 bool Checked(int id) const { return IsChecked(id); }
424
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
430
431protected:
432 // the list of all our menus
433 wxMenuList m_menus;
1e6feb95
VZ
434
435 // the frame we are attached to (may be NULL)
436 wxFrame *m_menuBarFrame;
3dfac970
VZ
437};
438
439// ----------------------------------------------------------------------------
440// include the real class declaration
441// ----------------------------------------------------------------------------
442
443#ifdef wxUSE_BASE_CLASSES_ONLY
444 #define wxMenuItem wxMenuItemBase
445#else // !wxUSE_BASE_CLASSES_ONLY
1e6feb95
VZ
446#if defined(__WXUNIVERSAL__)
447 #include "wx/univ/menu.h"
448#elif defined(__WXMSW__)
3dfac970 449 #include "wx/msw/menu.h"
2049ba38 450#elif defined(__WXMOTIF__)
3dfac970 451 #include "wx/motif/menu.h"
2049ba38 452#elif defined(__WXGTK__)
3dfac970 453 #include "wx/gtk/menu.h"
34138703 454#elif defined(__WXMAC__)
3dfac970 455 #include "wx/mac/menu.h"
1777b9bb 456#elif defined(__WXPM__)
3dfac970 457 #include "wx/os2/menu.h"
34138703 458#elif defined(__WXSTUBS__)
3dfac970 459 #include "wx/stubs/menu.h"
c801d85f 460#endif
3dfac970
VZ
461#endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
462
1e6feb95
VZ
463#endif // wxUSE_MENUS
464
c801d85f 465#endif
34138703 466 // _WX_MENU_H_BASE_