menu.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MENU_H_BASE_
  11. #define _WX_MENU_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_MENUS
  14. // ----------------------------------------------------------------------------
  15. // headers
  16. // ----------------------------------------------------------------------------
  17. #include "wx/list.h" // for "template" list classes
  18. #include "wx/window.h" // base class for wxMenuBar
  19. // also include this one to ensure compatibility with old code which only
  20. // included wx/menu.h
  21. #include "wx/menuitem.h"
  22. class WXDLLIMPEXP_FWD_CORE wxFrame;
  23. class WXDLLIMPEXP_FWD_CORE wxMenu;
  24. class WXDLLIMPEXP_FWD_CORE wxMenuBarBase;
  25. class WXDLLIMPEXP_FWD_CORE wxMenuBar;
  26. class WXDLLIMPEXP_FWD_CORE wxMenuItem;
  27. // pseudo template list classes
  28. WX_DECLARE_EXPORTED_LIST(wxMenu, wxMenuList);
  29. WX_DECLARE_EXPORTED_LIST(wxMenuItem, wxMenuItemList);
  30. // ----------------------------------------------------------------------------
  31. // wxMenu
  32. // ----------------------------------------------------------------------------
  33. class WXDLLIMPEXP_CORE wxMenuBase : public wxEvtHandler
  34. {
  35. public:
  36. // create a menu
  37. static wxMenu *New(const wxString& title = wxEmptyString, long style = 0);
  38. // ctors
  39. wxMenuBase(const wxString& title, long style = 0) : m_title(title)
  40. { Init(style); }
  41. wxMenuBase(long style = 0)
  42. { Init(style); }
  43. // dtor deletes all the menu items we own
  44. virtual ~wxMenuBase();
  45. // menu construction
  46. // -----------------
  47. // append any kind of item (normal/check/radio/separator)
  48. wxMenuItem* Append(int itemid,
  49. const wxString& text = wxEmptyString,
  50. const wxString& help = wxEmptyString,
  51. wxItemKind kind = wxITEM_NORMAL)
  52. {
  53. return DoAppend(wxMenuItem::New((wxMenu *)this, itemid, text, help, kind));
  54. }
  55. // append a separator to the menu
  56. wxMenuItem* AppendSeparator() { return Append(wxID_SEPARATOR); }
  57. // append a check item
  58. wxMenuItem* AppendCheckItem(int itemid,
  59. const wxString& text,
  60. const wxString& help = wxEmptyString)
  61. {
  62. return Append(itemid, text, help, wxITEM_CHECK);
  63. }
  64. // append a radio item
  65. wxMenuItem* AppendRadioItem(int itemid,
  66. const wxString& text,
  67. const wxString& help = wxEmptyString)
  68. {
  69. return Append(itemid, text, help, wxITEM_RADIO);
  70. }
  71. // append a submenu
  72. wxMenuItem* AppendSubMenu(wxMenu *submenu,
  73. const wxString& text,
  74. const wxString& help = wxEmptyString)
  75. {
  76. return DoAppend(wxMenuItem::New((wxMenu *)this, wxID_ANY, text, help,
  77. wxITEM_NORMAL, submenu));
  78. }
  79. // the most generic form of Append() - append anything
  80. wxMenuItem* Append(wxMenuItem *item) { return DoAppend(item); }
  81. // insert a break in the menu (only works when appending the items, not
  82. // inserting them)
  83. virtual void Break() { }
  84. // insert an item before given position
  85. wxMenuItem* Insert(size_t pos, wxMenuItem *item);
  86. // insert an item before given position
  87. wxMenuItem* Insert(size_t pos,
  88. int itemid,
  89. const wxString& text = wxEmptyString,
  90. const wxString& help = wxEmptyString,
  91. wxItemKind kind = wxITEM_NORMAL)
  92. {
  93. return Insert(pos, wxMenuItem::New((wxMenu *)this, itemid, text, help, kind));
  94. }
  95. // insert a separator
  96. wxMenuItem* InsertSeparator(size_t pos)
  97. {
  98. return Insert(pos, wxMenuItem::New((wxMenu *)this, wxID_SEPARATOR));
  99. }
  100. // insert a check item
  101. wxMenuItem* InsertCheckItem(size_t pos,
  102. int itemid,
  103. const wxString& text,
  104. const wxString& help = wxEmptyString)
  105. {
  106. return Insert(pos, itemid, text, help, wxITEM_CHECK);
  107. }
  108. // insert a radio item
  109. wxMenuItem* InsertRadioItem(size_t pos,
  110. int itemid,
  111. const wxString& text,
  112. const wxString& help = wxEmptyString)
  113. {
  114. return Insert(pos, itemid, text, help, wxITEM_RADIO);
  115. }
  116. // insert a submenu
  117. wxMenuItem* Insert(size_t pos,
  118. int itemid,
  119. const wxString& text,
  120. wxMenu *submenu,
  121. const wxString& help = wxEmptyString)
  122. {
  123. return Insert(pos, wxMenuItem::New((wxMenu *)this, itemid, text, help,
  124. wxITEM_NORMAL, submenu));
  125. }
  126. // prepend an item to the menu
  127. wxMenuItem* Prepend(wxMenuItem *item)
  128. {
  129. return Insert(0u, item);
  130. }
  131. // prepend any item to the menu
  132. wxMenuItem* Prepend(int itemid,
  133. const wxString& text = wxEmptyString,
  134. const wxString& help = wxEmptyString,
  135. wxItemKind kind = wxITEM_NORMAL)
  136. {
  137. return Insert(0u, itemid, text, help, kind);
  138. }
  139. // prepend a separator
  140. wxMenuItem* PrependSeparator()
  141. {
  142. return InsertSeparator(0u);
  143. }
  144. // prepend a check item
  145. wxMenuItem* PrependCheckItem(int itemid,
  146. const wxString& text,
  147. const wxString& help = wxEmptyString)
  148. {
  149. return InsertCheckItem(0u, itemid, text, help);
  150. }
  151. // prepend a radio item
  152. wxMenuItem* PrependRadioItem(int itemid,
  153. const wxString& text,
  154. const wxString& help = wxEmptyString)
  155. {
  156. return InsertRadioItem(0u, itemid, text, help);
  157. }
  158. // prepend a submenu
  159. wxMenuItem* Prepend(int itemid,
  160. const wxString& text,
  161. wxMenu *submenu,
  162. const wxString& help = wxEmptyString)
  163. {
  164. return Insert(0u, itemid, text, submenu, help);
  165. }
  166. // detach an item from the menu, but don't delete it so that it can be
  167. // added back later (but if it's not, the caller is responsible for
  168. // deleting it!)
  169. wxMenuItem *Remove(int itemid) { return Remove(FindChildItem(itemid)); }
  170. wxMenuItem *Remove(wxMenuItem *item);
  171. // delete an item from the menu (submenus are not destroyed by this
  172. // function, see Destroy)
  173. bool Delete(int itemid) { return Delete(FindChildItem(itemid)); }
  174. bool Delete(wxMenuItem *item);
  175. // delete the item from menu and destroy it (if it's a submenu)
  176. bool Destroy(int itemid) { return Destroy(FindChildItem(itemid)); }
  177. bool Destroy(wxMenuItem *item);
  178. // menu items access
  179. // -----------------
  180. // get the items
  181. size_t GetMenuItemCount() const { return m_items.GetCount(); }
  182. const wxMenuItemList& GetMenuItems() const { return m_items; }
  183. wxMenuItemList& GetMenuItems() { return m_items; }
  184. // search
  185. virtual int FindItem(const wxString& item) const;
  186. wxMenuItem* FindItem(int itemid, wxMenu **menu = NULL) const;
  187. // find by position
  188. wxMenuItem* FindItemByPosition(size_t position) const;
  189. // get/set items attributes
  190. void Enable(int itemid, bool enable);
  191. bool IsEnabled(int itemid) const;
  192. void Check(int itemid, bool check);
  193. bool IsChecked(int itemid) const;
  194. void SetLabel(int itemid, const wxString& label);
  195. wxString GetLabel(int itemid) const;
  196. // Returns the stripped label
  197. wxString GetLabelText(int itemid) const { return wxMenuItem::GetLabelText(GetLabel(itemid)); }
  198. virtual void SetHelpString(int itemid, const wxString& helpString);
  199. virtual wxString GetHelpString(int itemid) const;
  200. // misc accessors
  201. // --------------
  202. // the title
  203. virtual void SetTitle(const wxString& title) { m_title = title; }
  204. const wxString& GetTitle() const { return m_title; }
  205. // event handler
  206. void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
  207. wxEvtHandler *GetEventHandler() const { return m_eventHandler; }
  208. // Invoking window: this is set by wxWindow::PopupMenu() before showing a
  209. // popup menu and reset after it's hidden. Notice that you probably want to
  210. // use GetWindow() below instead of GetInvokingWindow() as the latter only
  211. // returns non-NULL for the top level menus
  212. //
  213. // NB: avoid calling SetInvokingWindow() directly if possible, use
  214. // wxMenuInvokingWindowSetter class below instead
  215. void SetInvokingWindow(wxWindow *win);
  216. wxWindow *GetInvokingWindow() const { return m_invokingWindow; }
  217. // the window associated with this menu: this is the invoking window for
  218. // popup menus or the top level window to which the menu bar is attached
  219. // for menus which are part of a menu bar
  220. wxWindow *GetWindow() const;
  221. // style
  222. long GetStyle() const { return m_style; }
  223. // implementation helpers
  224. // ----------------------
  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 = NULL);
  229. // get the menu bar this menu is attached to (may be NULL, always NULL for
  230. // popup menus). Traverse up the menu hierarchy to find it.
  231. wxMenuBar *GetMenuBar() const;
  232. // called when the menu is attached/detached to/from a menu bar
  233. virtual void Attach(wxMenuBarBase *menubar);
  234. virtual void Detach();
  235. // is the menu attached to a menu bar (or is it a popup one)?
  236. bool IsAttached() const { return GetMenuBar() != NULL; }
  237. // set/get the parent of this menu
  238. void SetParent(wxMenu *parent) { m_menuParent = parent; }
  239. wxMenu *GetParent() const { return m_menuParent; }
  240. // implementation only from now on
  241. // -------------------------------
  242. // unlike FindItem(), this function doesn't recurse but only looks through
  243. // our direct children and also may return the index of the found child if
  244. // pos != NULL
  245. wxMenuItem *FindChildItem(int itemid, size_t *pos = NULL) const;
  246. // called to generate a wxCommandEvent, return true if it was processed,
  247. // false otherwise
  248. //
  249. // the checked parameter may have boolean value or -1 for uncheckable items
  250. bool SendEvent(int itemid, int checked = -1);
  251. // compatibility: these functions are deprecated, use the new ones instead
  252. // -----------------------------------------------------------------------
  253. // use the versions taking wxItem_XXX now instead, they're more readable
  254. // and allow adding the radio items as well
  255. void Append(int itemid,
  256. const wxString& text,
  257. const wxString& help,
  258. bool isCheckable)
  259. {
  260. Append(itemid, text, help, isCheckable ? wxITEM_CHECK : wxITEM_NORMAL);
  261. }
  262. // use more readable and not requiring unused itemid AppendSubMenu() instead
  263. wxMenuItem* Append(int itemid,
  264. const wxString& text,
  265. wxMenu *submenu,
  266. const wxString& help = wxEmptyString)
  267. {
  268. return DoAppend(wxMenuItem::New((wxMenu *)this, itemid, text, help,
  269. wxITEM_NORMAL, submenu));
  270. }
  271. void Insert(size_t pos,
  272. int itemid,
  273. const wxString& text,
  274. const wxString& help,
  275. bool isCheckable)
  276. {
  277. Insert(pos, itemid, text, help, isCheckable ? wxITEM_CHECK : wxITEM_NORMAL);
  278. }
  279. void Prepend(int itemid,
  280. const wxString& text,
  281. const wxString& help,
  282. bool isCheckable)
  283. {
  284. Insert(0u, itemid, text, help, isCheckable);
  285. }
  286. static void LockAccels(bool locked)
  287. {
  288. ms_locked = locked;
  289. }
  290. protected:
  291. // virtuals to override in derived classes
  292. // ---------------------------------------
  293. virtual wxMenuItem* DoAppend(wxMenuItem *item);
  294. virtual wxMenuItem* DoInsert(size_t pos, wxMenuItem *item);
  295. virtual wxMenuItem *DoRemove(wxMenuItem *item);
  296. virtual bool DoDelete(wxMenuItem *item);
  297. virtual bool DoDestroy(wxMenuItem *item);
  298. // helpers
  299. // -------
  300. // common part of all ctors
  301. void Init(long style);
  302. // associate the submenu with this menu
  303. void AddSubMenu(wxMenu *submenu);
  304. wxMenuBar *m_menuBar; // menubar we belong to or NULL
  305. wxMenu *m_menuParent; // parent menu or NULL
  306. wxString m_title; // the menu title or label
  307. wxMenuItemList m_items; // the list of menu items
  308. wxWindow *m_invokingWindow; // for popup menus
  309. long m_style; // combination of wxMENU_XXX flags
  310. wxEvtHandler *m_eventHandler; // a pluggable in event handler
  311. static bool ms_locked;
  312. wxDECLARE_NO_COPY_CLASS(wxMenuBase);
  313. };
  314. #if wxUSE_EXTENDED_RTTI
  315. // ----------------------------------------------------------------------------
  316. // XTI accessor
  317. // ----------------------------------------------------------------------------
  318. class WXDLLEXPORT wxMenuInfoHelper : public wxObject
  319. {
  320. public:
  321. wxMenuInfoHelper() { m_menu = NULL; }
  322. virtual ~wxMenuInfoHelper() { }
  323. bool Create( wxMenu *menu, const wxString &title )
  324. {
  325. m_menu = menu;
  326. m_title = title;
  327. return true;
  328. }
  329. wxMenu* GetMenu() const { return m_menu; }
  330. wxString GetTitle() const { return m_title; }
  331. private:
  332. wxMenu *m_menu;
  333. wxString m_title;
  334. DECLARE_DYNAMIC_CLASS(wxMenuInfoHelper)
  335. };
  336. WX_DECLARE_EXPORTED_LIST(wxMenuInfoHelper, wxMenuInfoHelperList );
  337. #endif
  338. // ----------------------------------------------------------------------------
  339. // wxMenuBar
  340. // ----------------------------------------------------------------------------
  341. class WXDLLIMPEXP_CORE wxMenuBarBase : public wxWindow
  342. {
  343. public:
  344. // default ctor
  345. wxMenuBarBase();
  346. // dtor will delete all menus we own
  347. virtual ~wxMenuBarBase();
  348. // menu bar construction
  349. // ---------------------
  350. // append a menu to the end of menubar, return true if ok
  351. virtual bool Append(wxMenu *menu, const wxString& title);
  352. // insert a menu before the given position into the menubar, return true
  353. // if inserted ok
  354. virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
  355. // menu bar items access
  356. // ---------------------
  357. // get the number of menus in the menu bar
  358. size_t GetMenuCount() const { return m_menus.GetCount(); }
  359. // get the menu at given position
  360. wxMenu *GetMenu(size_t pos) const;
  361. // replace the menu at given position with another one, returns the
  362. // previous menu (which should be deleted by the caller)
  363. virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
  364. // delete the menu at given position from the menu bar, return the pointer
  365. // to the menu (which should be deleted by the caller)
  366. virtual wxMenu *Remove(size_t pos);
  367. // enable or disable a submenu
  368. virtual void EnableTop(size_t pos, bool enable) = 0;
  369. // is the menu enabled?
  370. virtual bool IsEnabledTop(size_t WXUNUSED(pos)) const { return true; }
  371. // get or change the label of the menu at given position
  372. virtual void SetMenuLabel(size_t pos, const wxString& label) = 0;
  373. virtual wxString GetMenuLabel(size_t pos) const = 0;
  374. // get the stripped label of the menu at given position
  375. virtual wxString GetMenuLabelText(size_t pos) const { return wxMenuItem::GetLabelText(GetMenuLabel(pos)); }
  376. // item search
  377. // -----------
  378. // by menu and item names, returns wxNOT_FOUND if not found or id of the
  379. // found item
  380. virtual int FindMenuItem(const wxString& menu, const wxString& item) const;
  381. // find item by id (in any menu), returns NULL if not found
  382. //
  383. // if menu is !NULL, it will be filled with wxMenu this item belongs to
  384. virtual wxMenuItem* FindItem(int itemid, wxMenu **menu = NULL) const;
  385. // find menu by its caption, return wxNOT_FOUND on failure
  386. int FindMenu(const wxString& title) const;
  387. // item access
  388. // -----------
  389. // all these functions just use FindItem() and then call an appropriate
  390. // method on it
  391. //
  392. // NB: under MSW, these methods can only be used after the menubar had
  393. // been attached to the frame
  394. void Enable(int itemid, bool enable);
  395. void Check(int itemid, bool check);
  396. bool IsChecked(int itemid) const;
  397. bool IsEnabled(int itemid) const;
  398. virtual bool IsEnabled() const { return wxWindow::IsEnabled(); }
  399. void SetLabel(int itemid, const wxString &label);
  400. wxString GetLabel(int itemid) const;
  401. void SetHelpString(int itemid, const wxString& helpString);
  402. wxString GetHelpString(int itemid) const;
  403. // implementation helpers
  404. // get the frame we are attached to (may return NULL)
  405. wxFrame *GetFrame() const { return m_menuBarFrame; }
  406. // returns true if we're attached to a frame
  407. bool IsAttached() const { return GetFrame() != NULL; }
  408. // associate the menubar with the frame
  409. virtual void Attach(wxFrame *frame);
  410. // called before deleting the menubar normally
  411. virtual void Detach();
  412. // need to override these ones to avoid virtual function hiding
  413. virtual bool Enable(bool enable = true) { return wxWindow::Enable(enable); }
  414. virtual void SetLabel(const wxString& s) { wxWindow::SetLabel(s); }
  415. 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; }
  418. // update all menu item states in all menus
  419. virtual void UpdateMenus();
  420. virtual bool CanBeOutsideClientArea() const { return true; }
  421. #if wxUSE_EXTENDED_RTTI
  422. // XTI helpers:
  423. bool AppendMenuInfo( const wxMenuInfoHelper *info )
  424. { return Append( info->GetMenu(), info->GetTitle() ); }
  425. const wxMenuInfoHelperList& GetMenuInfos() const;
  426. #endif
  427. #if WXWIN_COMPATIBILITY_2_8
  428. // get or change the label of the menu at given position
  429. // Deprecated in favour of SetMenuLabel
  430. wxDEPRECATED( void SetLabelTop(size_t pos, const wxString& label) );
  431. // Deprecated in favour of GetMenuLabelText
  432. wxDEPRECATED( wxString GetLabelTop(size_t pos) const );
  433. #endif
  434. protected:
  435. // the list of all our menus
  436. wxMenuList m_menus;
  437. #if wxUSE_EXTENDED_RTTI
  438. // used by XTI
  439. wxMenuInfoHelperList m_menuInfos;
  440. #endif
  441. // the frame we are attached to (may be NULL)
  442. wxFrame *m_menuBarFrame;
  443. wxDECLARE_NO_COPY_CLASS(wxMenuBarBase);
  444. };
  445. // ----------------------------------------------------------------------------
  446. // include the real class declaration
  447. // ----------------------------------------------------------------------------
  448. #ifdef wxUSE_BASE_CLASSES_ONLY
  449. #define wxMenuItem wxMenuItemBase
  450. #else // !wxUSE_BASE_CLASSES_ONLY
  451. #if defined(__WXUNIVERSAL__)
  452. #include "wx/univ/menu.h"
  453. #elif defined(__WXMSW__)
  454. #include "wx/msw/menu.h"
  455. #elif defined(__WXMOTIF__)
  456. #include "wx/motif/menu.h"
  457. #elif defined(__WXGTK20__)
  458. #include "wx/gtk/menu.h"
  459. #elif defined(__WXGTK__)
  460. #include "wx/gtk1/menu.h"
  461. #elif defined(__WXMAC__)
  462. #include "wx/osx/menu.h"
  463. #elif defined(__WXCOCOA__)
  464. #include "wx/cocoa/menu.h"
  465. #elif defined(__WXPM__)
  466. #include "wx/os2/menu.h"
  467. #endif
  468. #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
  469. // ----------------------------------------------------------------------------
  470. // Helper class used in the implementation only: sets the invoking window of
  471. // the given menu in its ctor and resets it in dtor.
  472. // ----------------------------------------------------------------------------
  473. class wxMenuInvokingWindowSetter
  474. {
  475. public:
  476. // Ctor sets the invoking window for the given menu.
  477. //
  478. // The menu lifetime must be greater than that of this class.
  479. wxMenuInvokingWindowSetter(wxMenu& menu, wxWindow *win)
  480. : m_menu(menu)
  481. {
  482. menu.SetInvokingWindow(win);
  483. }
  484. // Dtor resets the invoking window.
  485. ~wxMenuInvokingWindowSetter()
  486. {
  487. m_menu.SetInvokingWindow(NULL);
  488. }
  489. private:
  490. wxMenu& m_menu;
  491. wxDECLARE_NO_COPY_CLASS(wxMenuInvokingWindowSetter);
  492. };
  493. #endif // wxUSE_MENUS
  494. #endif // _WX_MENU_H_BASE_