]> git.saurik.com Git - wxWidgets.git/blob - include/wx/menu.h
compilation fix
[wxWidgets.git] / include / wx / menu.h
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 licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_MENU_H_BASE_
13 #define _WX_MENU_H_BASE_
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "menubase.h"
17 #endif
18
19 #if wxUSE_MENUS
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 #include "wx/list.h" // for "template" list classes
26 #include "wx/window.h" // base class for wxMenuBar
27
28 // also include this one to ensure compatibility with old code which only
29 // included wx/menu.h
30 #include "wx/menuitem.h"
31
32 class WXDLLEXPORT wxMenu;
33 class WXDLLEXPORT wxMenuBarBase;
34 class WXDLLEXPORT wxMenuBar;
35 class WXDLLEXPORT wxMenuItem;
36
37 // pseudo template list classes
38 WX_DECLARE_EXPORTED_LIST(wxMenu, wxMenuList);
39 WX_DECLARE_EXPORTED_LIST(wxMenuItem, wxMenuItemList);
40
41 // ----------------------------------------------------------------------------
42 // wxMenu
43 // ----------------------------------------------------------------------------
44
45 class WXDLLEXPORT wxMenuBase : public wxEvtHandler
46 {
47 public:
48 // create a menu
49 static wxMenu *New(const wxString& title = wxEmptyString, long style = 0);
50
51 // ctors
52 wxMenuBase(const wxString& title, long style = 0) : m_title(title)
53 { Init(style); }
54 wxMenuBase(long style = 0)
55 { Init(style); }
56
57 // dtor deletes all the menu items we own
58 virtual ~wxMenuBase();
59
60 // menu construction
61 // -----------------
62
63 // append any kind of item (normal/check/radio/separator)
64 void Append(int itemid,
65 const wxString& text,
66 const wxString& help = wxEmptyString,
67 wxItemKind kind = wxITEM_NORMAL)
68 {
69 DoAppend(wxMenuItem::New((wxMenu *)this, itemid, text, help, kind));
70 }
71
72 // append a separator to the menu
73 void AppendSeparator() { Append(wxID_SEPARATOR, wxEmptyString); }
74
75 // append a check item
76 void AppendCheckItem(int itemid,
77 const wxString& text,
78 const wxString& help = wxEmptyString)
79 {
80 Append(itemid, text, help, wxITEM_CHECK);
81 }
82
83 // append a radio item
84 void AppendRadioItem(int itemid,
85 const wxString& text,
86 const wxString& help = wxEmptyString)
87 {
88 Append(itemid, text, help, wxITEM_RADIO);
89 }
90
91 // append a submenu
92 void Append(int itemid,
93 const wxString& text,
94 wxMenu *submenu,
95 const wxString& help = wxEmptyString)
96 {
97 DoAppend(wxMenuItem::New((wxMenu *)this, itemid, text, help,
98 wxITEM_NORMAL, submenu));
99 }
100
101 // the most generic form of Append() - append anything
102 void Append(wxMenuItem *item) { DoAppend(item); }
103
104 // insert a break in the menu (only works when appending the items, not
105 // inserting them)
106 virtual void Break() { }
107
108 // insert an item before given position
109 bool Insert(size_t pos, wxMenuItem *item);
110
111 // insert an item before given position
112 void Insert(size_t pos,
113 int itemid,
114 const wxString& text,
115 const wxString& help = wxEmptyString,
116 wxItemKind kind = wxITEM_NORMAL)
117 {
118 Insert(pos, wxMenuItem::New((wxMenu *)this, itemid, text, help, kind));
119 }
120
121 // insert a separator
122 void InsertSeparator(size_t pos)
123 {
124 Insert(pos, wxMenuItem::New((wxMenu *)this));
125 }
126
127 // insert a check item
128 void InsertCheckItem(size_t pos,
129 int itemid,
130 const wxString& text,
131 const wxString& help = wxEmptyString)
132 {
133 Insert(pos, itemid, text, help, wxITEM_CHECK);
134 }
135
136 // insert a radio item
137 void InsertRadioItem(size_t pos,
138 int itemid,
139 const wxString& text,
140 const wxString& help = wxEmptyString)
141 {
142 Insert(pos, itemid, text, help, wxITEM_RADIO);
143 }
144
145 // insert a submenu
146 void Insert(size_t pos,
147 int itemid,
148 const wxString& text,
149 wxMenu *submenu,
150 const wxString& help = wxEmptyString)
151 {
152 Insert(pos, wxMenuItem::New((wxMenu *)this, itemid, text, help,
153 wxITEM_NORMAL, submenu));
154 }
155
156 // prepend an item to the menu
157 void Prepend(wxMenuItem *item)
158 {
159 Insert(0u, item);
160 }
161
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)
167 {
168 Insert(0u, itemid, text, help, kind);
169 }
170
171 // prepend a separator
172 void PrependSeparator()
173 {
174 InsertSeparator(0u);
175 }
176
177 // prepend a check item
178 void PrependCheckItem(int itemid,
179 const wxString& text,
180 const wxString& help = wxEmptyString)
181 {
182 InsertCheckItem(0u, itemid, text, help);
183 }
184
185 // prepend a radio item
186 void PrependRadioItem(int itemid,
187 const wxString& text,
188 const wxString& help = wxEmptyString)
189 {
190 InsertRadioItem(0u, itemid, text, help);
191 }
192
193 // prepend a submenu
194 void Prepend(int itemid,
195 const wxString& text,
196 wxMenu *submenu,
197 const wxString& help = wxEmptyString)
198 {
199 Insert(0u, itemid, text, submenu, help);
200 }
201
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
204 // deleting it!)
205 wxMenuItem *Remove(int itemid) { return Remove(FindChildItem(itemid)); }
206 wxMenuItem *Remove(wxMenuItem *item);
207
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);
212
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);
216
217 // menu items access
218 // -----------------
219
220 // get the items
221 size_t GetMenuItemCount() const { return m_items.GetCount(); }
222
223 const wxMenuItemList& GetMenuItems() const { return m_items; }
224 wxMenuItemList& GetMenuItems() { return m_items; }
225
226 // search
227 virtual int FindItem(const wxString& item) const;
228 wxMenuItem* FindItem(int itemid, wxMenu **menu = NULL) const;
229
230 // find by position
231 wxMenuItem* FindItemByPosition(size_t position) const;
232
233 // get/set items attributes
234 void Enable(int itemid, bool enable);
235 bool IsEnabled(int itemid) const;
236
237 void Check(int itemid, bool check);
238 bool IsChecked(int itemid) const;
239
240 void SetLabel(int itemid, const wxString& label);
241 wxString GetLabel(int itemid) const;
242
243 virtual void SetHelpString(int itemid, const wxString& helpString);
244 virtual wxString GetHelpString(int itemid) const;
245
246 // misc accessors
247 // --------------
248
249 // the title
250 virtual void SetTitle(const wxString& title) { m_title = title; }
251 const wxString GetTitle() const { return m_title; }
252
253 // event handler
254 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
255 wxEvtHandler *GetEventHandler() const { return m_eventHandler; }
256
257 // invoking window
258 void SetInvokingWindow(wxWindow *win) { m_invokingWindow = win; }
259 wxWindow *GetInvokingWindow() const { return m_invokingWindow; }
260
261 // style
262 long GetStyle() const { return m_style; }
263
264 // implementation helpers
265 // ----------------------
266
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);
271
272 // get the menu bar this menu is attached to (may be NULL, always NULL for
273 // popup menus)
274 wxMenuBar *GetMenuBar() const { return m_menuBar; }
275
276 // called when the menu is attached/detached to/from a menu bar
277 virtual void Attach(wxMenuBarBase *menubar);
278 virtual void Detach();
279
280 // is the menu attached to a menu bar (or is it a popup one)?
281 bool IsAttached() const { return m_menuBar != NULL; }
282
283 // set/get the parent of this menu
284 void SetParent(wxMenu *parent) { m_menuParent = parent; }
285 wxMenu *GetParent() const { return m_menuParent; }
286
287 // implementation only from now on
288 // -------------------------------
289
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
292 // pos != NULL
293 wxMenuItem *FindChildItem(int itemid, size_t *pos = NULL) const;
294
295 // called to generate a wxCommandEvent, return TRUE if it was processed,
296 // FALSE otherwise
297 //
298 // the checked parameter may have boolean value or -1 for uncheckable items
299 bool SendEvent(int itemid, int checked = -1);
300
301 // compatibility: these functions are deprecated, use the new ones instead
302 // -----------------------------------------------------------------------
303
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,
309 bool isCheckable)
310 {
311 Append(itemid, text, help, isCheckable ? wxITEM_CHECK : wxITEM_NORMAL);
312 }
313
314 void Insert(size_t pos,
315 int itemid,
316 const wxString& text,
317 const wxString& help,
318 bool isCheckable)
319 {
320 Insert(pos, itemid, text, help, isCheckable ? wxITEM_CHECK : wxITEM_NORMAL);
321 }
322
323 void Prepend(int itemid,
324 const wxString& text,
325 const wxString& help,
326 bool isCheckable)
327 {
328 Insert(0u, itemid, text, help, isCheckable);
329 }
330
331 #if WXWIN_COMPATIBILITY
332 bool Enabled(int itemid) const { return IsEnabled(itemid); }
333 bool Checked(int itemid) const { return IsChecked(itemid); }
334
335 wxMenuItem* FindItemForId(int itemId, wxMenu **itemMenu) const
336 { return FindItem(itemId, itemMenu); }
337
338 wxList& GetItems() const { return (wxList &)m_items; }
339 #endif // WXWIN_COMPATIBILITY
340
341 protected:
342 // virtuals to override in derived classes
343 // ---------------------------------------
344
345 virtual bool DoAppend(wxMenuItem *item);
346 virtual bool DoInsert(size_t pos, wxMenuItem *item);
347
348 virtual wxMenuItem *DoRemove(wxMenuItem *item);
349 virtual bool DoDelete(wxMenuItem *item);
350 virtual bool DoDestroy(wxMenuItem *item);
351
352 // helpers
353 // -------
354
355 // common part of all ctors
356 void Init(long style);
357
358 // associate the submenu with this menu
359 void AddSubMenu(wxMenu *submenu);
360
361 wxMenuBar *m_menuBar; // menubar we belong to or NULL
362 wxMenu *m_menuParent; // parent menu or NULL
363
364 wxString m_title; // the menu title or label
365 wxMenuItemList m_items; // the list of menu items
366
367 wxWindow *m_invokingWindow; // for popup menus
368
369 long m_style; // combination of wxMENU_XXX flags
370
371 wxEvtHandler *m_eventHandler; // a pluggable in event handler
372
373 DECLARE_NO_COPY_CLASS(wxMenuBase)
374 };
375
376 // ----------------------------------------------------------------------------
377 // wxMenuBar
378 // ----------------------------------------------------------------------------
379
380 class WXDLLEXPORT wxMenuBarBase : public wxWindow
381 {
382 public:
383 // default ctor
384 wxMenuBarBase();
385
386 // dtor will delete all menus we own
387 virtual ~wxMenuBarBase();
388
389 // menu bar construction
390 // ---------------------
391
392 // append a menu to the end of menubar, return TRUE if ok
393 virtual bool Append(wxMenu *menu, const wxString& title);
394
395 // insert a menu before the given position into the menubar, return TRUE
396 // if inserted ok
397 virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
398
399 // menu bar items access
400 // ---------------------
401
402 // get the number of menus in the menu bar
403 size_t GetMenuCount() const { return m_menus.GetCount(); }
404
405 // get the menu at given position
406 wxMenu *GetMenu(size_t pos) const;
407
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);
411
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);
415
416 // enable or disable a submenu
417 virtual void EnableTop(size_t pos, bool enable) = 0;
418
419 // is the menu enabled?
420 virtual bool IsEnabledTop(size_t WXUNUSED(pos)) const { return TRUE; }
421
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;
425
426 // item search
427 // -----------
428
429 // by menu and item names, returns wxNOT_FOUND if not found or id of the
430 // found item
431 virtual int FindMenuItem(const wxString& menu, const wxString& item) const;
432
433 // find item by id (in any menu), returns NULL if not found
434 //
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;
437
438 // find menu by its caption, return wxNOT_FOUND on failure
439 int FindMenu(const wxString& title) const;
440
441 // item access
442 // -----------
443
444 // all these functions just use FindItem() and then call an appropriate
445 // method on it
446 //
447 // NB: under MSW, these methods can only be used after the menubar had
448 // been attached to the frame
449
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;
454
455 void SetLabel(int itemid, const wxString &label);
456 wxString GetLabel(int itemid) const;
457
458 void SetHelpString(int itemid, const wxString& helpString);
459 wxString GetHelpString(int itemid) const;
460
461 // implementation helpers
462
463 // get the frame we are attached to (may return NULL)
464 wxFrame *GetFrame() const { return m_menuBarFrame; }
465
466 // returns TRUE if we're attached to a frame
467 bool IsAttached() const { return GetFrame() != NULL; }
468
469 // associate the menubar with the frame
470 virtual void Attach(wxFrame *frame);
471
472 // called before deleting the menubar normally
473 virtual void Detach();
474
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(); }
479
480 // don't want menu bars to accept the focus by tabbing to them
481 virtual bool AcceptsFocusFromKeyboard() const { return FALSE; }
482
483 // compatibility only: these functions are deprecated, use the new ones
484 // instead
485 #if WXWIN_COMPATIBILITY
486 bool Enabled(int itemid) const { return IsEnabled(itemid); }
487 bool Checked(int itemid) const { return IsChecked(itemid); }
488
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
494
495 protected:
496 // the list of all our menus
497 wxMenuList m_menus;
498
499 // the frame we are attached to (may be NULL)
500 wxFrame *m_menuBarFrame;
501
502 DECLARE_NO_COPY_CLASS(wxMenuBarBase)
503 };
504
505 // ----------------------------------------------------------------------------
506 // include the real class declaration
507 // ----------------------------------------------------------------------------
508
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"
526 #endif
527 #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
528
529 #endif // wxUSE_MENUS
530
531 #endif
532 // _WX_MENU_H_BASE_