menu.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: menu.h
  3. // Purpose: interface of wxMenuBar
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxMenuBar
  9. A menu bar is a series of menus accessible from the top of a frame.
  10. @remarks
  11. To respond to a menu selection, provide a handler for EVT_MENU, in the frame
  12. that contains the menu bar.
  13. If you have a toolbar which uses the same identifiers as your EVT_MENU entries,
  14. events from the toolbar will also be processed by your EVT_MENU event handlers.
  15. Tip: under Windows, if you discover that menu shortcuts (for example, Alt-F
  16. to show the file menu) are not working, check any EVT_CHAR events you are
  17. handling in child windows.
  18. If you are not calling event.Skip() for events that you don't process in
  19. these event handlers, menu shortcuts may cease to work.
  20. @library{wxcore}
  21. @category{menus}
  22. @see wxMenu, @ref overview_events
  23. */
  24. class wxMenuBar : public wxWindow
  25. {
  26. public:
  27. /**
  28. Construct an empty menu bar.
  29. @param style
  30. If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
  31. */
  32. wxMenuBar(long style = 0);
  33. /**
  34. Construct a menu bar from arrays of menus and titles.
  35. @param n
  36. The number of menus.
  37. @param menus
  38. An array of menus. Do not use this array again - it now belongs to
  39. the menu bar.
  40. @param titles
  41. An array of title strings. Deallocate this array after creating
  42. the menu bar.
  43. @param style
  44. If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
  45. @beginWxPerlOnly
  46. Not supported by wxPerl.
  47. @endWxPerlOnly
  48. */
  49. wxMenuBar(size_t n, wxMenu* menus[], const wxString titles[],
  50. long style = 0);
  51. /**
  52. Destructor, destroying the menu bar and removing it from the parent
  53. frame (if any).
  54. */
  55. virtual ~wxMenuBar();
  56. /**
  57. Adds the item to the end of the menu bar.
  58. @param menu
  59. The menu to add. Do not deallocate this menu after calling Append().
  60. @param title
  61. The title of the menu, must be non-empty.
  62. @return @true on success, @false if an error occurred.
  63. @see Insert()
  64. */
  65. virtual bool Append(wxMenu* menu, const wxString& title);
  66. /**
  67. Checks or unchecks a menu item.
  68. @param id
  69. The menu item identifier.
  70. @param check
  71. If @true, checks the menu item, otherwise the item is unchecked.
  72. @remarks Only use this when the menu bar has been associated with a
  73. frame; otherwise, use the wxMenu equivalent call.
  74. */
  75. void Check(int id, bool check);
  76. /**
  77. Enables or disables (greys out) a menu item.
  78. @param id
  79. The menu item identifier.
  80. @param enable
  81. @true to enable the item, @false to disable it.
  82. @remarks Only use this when the menu bar has been associated with a
  83. frame; otherwise, use the wxMenu equivalent call.
  84. */
  85. void Enable(int id, bool enable);
  86. /**
  87. Returns true if the menu with the given index is enabled.
  88. @param pos
  89. The menu position (0-based)
  90. @since 2.9.4
  91. */
  92. bool IsEnabledTop(size_t pos) const;
  93. /**
  94. Enables or disables a whole menu.
  95. @param pos
  96. The position of the menu, starting from zero.
  97. @param enable
  98. @true to enable the menu, @false to disable it.
  99. @remarks Only use this when the menu bar has been associated with a frame.
  100. */
  101. virtual void EnableTop(size_t pos, bool enable);
  102. /**
  103. Finds the menu item object associated with the given menu item identifier.
  104. @param id
  105. Menu item identifier.
  106. @param menu
  107. If not @NULL, menu will get set to the associated menu.
  108. @return The found menu item object, or @NULL if one was not found.
  109. @beginWxPerlOnly
  110. In wxPerl this method takes just the @a id parameter;
  111. in scalar context it returns the associated @c Wx::MenuItem, in list
  112. context it returns a 2-element list (item, submenu).
  113. @endWxPerlOnly
  114. */
  115. virtual wxMenuItem* FindItem(int id, wxMenu** menu = NULL) const;
  116. /**
  117. Returns the index of the menu with the given @a title or @c wxNOT_FOUND if no
  118. such menu exists in this menubar.
  119. The @a title parameter may specify either the menu title
  120. (with accelerator characters, i.e. @c "&File") or just the
  121. menu label (@c "File") indifferently.
  122. */
  123. int FindMenu(const wxString& title) const;
  124. /**
  125. Finds the menu item id for a menu name/menu item string pair.
  126. @param menuString
  127. Menu title to find.
  128. @param itemString
  129. Item to find.
  130. @return The menu item identifier, or wxNOT_FOUND if none was found.
  131. @remarks Any special menu codes are stripped out of source and target
  132. strings before matching.
  133. */
  134. virtual int FindMenuItem(const wxString& menuString,
  135. const wxString& itemString) const;
  136. /**
  137. Gets the help string associated with the menu item identifier.
  138. @param id
  139. The menu item identifier.
  140. @return The help string, or the empty string if there was no help string
  141. or the menu item was not found.
  142. @see SetHelpString()
  143. */
  144. wxString GetHelpString(int id) const;
  145. /**
  146. Gets the label associated with a menu item.
  147. @param id
  148. The menu item identifier.
  149. @return The menu item label, or the empty string if the item was not
  150. found.
  151. @remarks Use only after the menubar has been associated with a frame.
  152. */
  153. wxString GetLabel(int id) const;
  154. /**
  155. Returns the label of a top-level menu. Note that the returned string does not
  156. include the accelerator characters which could have been specified in the menu
  157. title string during its construction.
  158. @param pos
  159. Position of the menu on the menu bar, starting from zero.
  160. @return The menu label, or the empty string if the menu was not found.
  161. @remarks Use only after the menubar has been associated with a frame.
  162. @deprecated
  163. This function is deprecated in favour of GetMenuLabel() and GetMenuLabelText().
  164. @see SetLabelTop()
  165. */
  166. wxString GetLabelTop(size_t pos) const;
  167. /**
  168. Returns the menu at @a menuIndex (zero-based).
  169. */
  170. wxMenu* GetMenu(size_t menuIndex) const;
  171. /**
  172. Returns the number of menus in this menubar.
  173. */
  174. size_t GetMenuCount() const;
  175. /**
  176. Returns the label of a top-level menu. Note that the returned string
  177. includes the accelerator characters that have been specified in the menu
  178. title string during its construction.
  179. @param pos
  180. Position of the menu on the menu bar, starting from zero.
  181. @return The menu label, or the empty string if the menu was not found.
  182. @remarks Use only after the menubar has been associated with a frame.
  183. @see GetMenuLabelText(), SetMenuLabel()
  184. */
  185. virtual wxString GetMenuLabel(size_t pos) const;
  186. /**
  187. Returns the label of a top-level menu. Note that the returned string does not
  188. include any accelerator characters that may have been specified in the menu
  189. title string during its construction.
  190. @param pos
  191. Position of the menu on the menu bar, starting from zero.
  192. @return The menu label, or the empty string if the menu was not found.
  193. @remarks Use only after the menubar has been associated with a frame.
  194. @see GetMenuLabel(), SetMenuLabel()
  195. */
  196. virtual wxString GetMenuLabelText(size_t pos) const;
  197. /**
  198. Inserts the menu at the given position into the menu bar. Inserting menu at
  199. position 0 will insert it in the very beginning of it, inserting at position
  200. GetMenuCount() is the same as calling Append().
  201. @param pos
  202. The position of the new menu in the menu bar
  203. @param menu
  204. The menu to add. wxMenuBar owns the menu and will free it.
  205. @param title
  206. The title of the menu.
  207. @return @true on success, @false if an error occurred.
  208. @see Append()
  209. */
  210. virtual bool Insert(size_t pos, wxMenu* menu, const wxString& title);
  211. /**
  212. Determines whether an item is checked.
  213. @param id
  214. The menu item identifier.
  215. @return @true if the item was found and is checked, @false otherwise.
  216. */
  217. bool IsChecked(int id) const;
  218. /**
  219. Determines whether an item is enabled.
  220. @param id
  221. The menu item identifier.
  222. @return @true if the item was found and is enabled, @false otherwise.
  223. */
  224. bool IsEnabled(int id) const;
  225. /**
  226. Redraw the menu bar
  227. */
  228. virtual void Refresh(bool eraseBackground = true, const wxRect* rect = NULL);
  229. /**
  230. Removes the menu from the menu bar and returns the menu object - the caller
  231. is responsible for deleting it. This function may be used together with
  232. Insert() to change the menubar dynamically.
  233. @see Replace()
  234. */
  235. virtual wxMenu* Remove(size_t pos);
  236. /**
  237. Replaces the menu at the given position with another one.
  238. @param pos
  239. The position of the new menu in the menu bar
  240. @param menu
  241. The menu to add.
  242. @param title
  243. The title of the menu.
  244. @return The menu which was previously at position pos.
  245. The caller is responsible for deleting it.
  246. @see Insert(), Remove()
  247. */
  248. virtual wxMenu* Replace(size_t pos, wxMenu* menu, const wxString& title);
  249. /**
  250. Sets the help string associated with a menu item.
  251. @param id
  252. Menu item identifier.
  253. @param helpString
  254. Help string to associate with the menu item.
  255. @see GetHelpString()
  256. */
  257. void SetHelpString(int id, const wxString& helpString);
  258. /**
  259. Sets the label of a menu item.
  260. @param id
  261. Menu item identifier.
  262. @param label
  263. Menu item label.
  264. @remarks Use only after the menubar has been associated with a frame.
  265. @see GetLabel()
  266. */
  267. void SetLabel(int id, const wxString& label);
  268. /**
  269. Sets the label of a top-level menu.
  270. @param pos
  271. The position of a menu on the menu bar, starting from zero.
  272. @param label
  273. The menu label.
  274. @remarks Use only after the menubar has been associated with a frame.
  275. @deprecated
  276. This function has been deprecated in favour of SetMenuLabel().
  277. @see GetLabelTop()
  278. */
  279. void SetLabelTop(size_t pos, const wxString& label);
  280. /**
  281. Sets the label of a top-level menu.
  282. @param pos
  283. The position of a menu on the menu bar, starting from zero.
  284. @param label
  285. The menu label.
  286. @remarks Use only after the menubar has been associated with a frame.
  287. */
  288. virtual void SetMenuLabel(size_t pos, const wxString& label);
  289. /**
  290. Enables you to set the global menubar on Mac, that is, the menubar displayed
  291. when the app is running without any frames open.
  292. @param menubar
  293. The menubar to set.
  294. @remarks Only exists on Mac, other platforms do not have this method.
  295. @onlyfor{wxosx}
  296. */
  297. static void MacSetCommonMenuBar(wxMenuBar* menubar);
  298. /**
  299. Enables you to get the global menubar on Mac, that is, the menubar displayed
  300. when the app is running without any frames open.
  301. @return The global menubar.
  302. @remarks Only exists on Mac, other platforms do not have this method.
  303. @onlyfor{wxosx}
  304. */
  305. static wxMenuBar* MacGetCommonMenuBar();
  306. /**
  307. Returns the Apple menu.
  308. This is the leftmost menu with application's name as its title. You
  309. shouldn't remove any items from it, but it is safe to insert extra menu
  310. items or submenus into it.
  311. @onlyfor{wxosx}
  312. @since 3.0.1
  313. */
  314. wxMenu *OSXGetAppleMenu() const;
  315. wxFrame *GetFrame() const;
  316. bool IsAttached() const;
  317. virtual void Attach(wxFrame *frame);
  318. virtual void Detach();
  319. };
  320. /**
  321. @class wxMenu
  322. A menu is a popup (or pull down) list of items, one of which may be
  323. selected before the menu goes away (clicking elsewhere dismisses the
  324. menu). Menus may be used to construct either menu bars or popup menus.
  325. A menu item has an integer ID associated with it which can be used to
  326. identify the selection, or to change the menu item in some way. A menu item
  327. with a special identifier @e wxID_SEPARATOR is a separator item and doesn't
  328. have an associated command but just makes a separator line appear in the
  329. menu.
  330. @note
  331. Please note that @e wxID_ABOUT and @e wxID_EXIT are predefined by wxWidgets
  332. and have a special meaning since entries using these IDs will be taken out
  333. of the normal menus under OS X and will be inserted into the system menu
  334. (following the appropriate OS X interface guideline).
  335. Menu items may be either @e normal items, @e check items or @e radio items.
  336. Normal items don't have any special properties while the check items have a
  337. boolean flag associated to them and they show a checkmark in the menu when
  338. the flag is set.
  339. wxWidgets automatically toggles the flag value when the item is clicked and its
  340. value may be retrieved using either wxMenu::IsChecked method of wxMenu or
  341. wxMenuBar itself or by using wxEvent::IsChecked when you get the menu
  342. notification for the item in question.
  343. The radio items are similar to the check items except that all the other items
  344. in the same radio group are unchecked when a radio item is checked. The radio
  345. group is formed by a contiguous range of radio items, i.e. it starts at the
  346. first item of this kind and ends with the first item of a different kind (or
  347. the end of the menu). Notice that because the radio groups are defined in terms
  348. of the item positions inserting or removing the items in the menu containing
  349. the radio items risks to not work correctly.
  350. @section menu_allocation Allocation strategy
  351. All menus must be created on the @b heap because all menus attached to a
  352. menubar or to another menu will be deleted by their parent when it is
  353. deleted. The only exception to this rule are the popup menus (i.e. menus
  354. used with wxWindow::PopupMenu()) as wxWidgets does not destroy them to
  355. allow reusing the same menu more than once. But the exception applies only
  356. to the menus themselves and not to any submenus of popup menus which are
  357. still destroyed by wxWidgets as usual and so must be heap-allocated.
  358. As the frame menubar is deleted by the frame itself, it means that normally
  359. all menus used are deleted automatically.
  360. @section menu_eventhandling Event handling
  361. If the menu is part of a menubar, then wxMenuBar event processing is used.
  362. With a popup menu (see wxWindow::PopupMenu), there is a variety of ways to
  363. handle a menu selection event (@c wxEVT_MENU):
  364. - Provide @c EVT_MENU handlers in the window which pops up the menu, or in an
  365. ancestor of that window (the simplest method);
  366. - Derive a new class from wxMenu and define event table entries using the @c EVT_MENU macro;
  367. - Set a new event handler for wxMenu, through wxEvtHandler::SetNextHandler,
  368. specifying an object whose class has @c EVT_MENU entries;
  369. Note that instead of static @c EVT_MENU macros you can also use dynamic
  370. connection; see @ref overview_events_bind.
  371. @library{wxcore}
  372. @category{menus}
  373. @see wxMenuBar, wxWindow::PopupMenu, @ref overview_events,
  374. @ref wxFileHistory "wxFileHistory (most recently used files menu)"
  375. */
  376. class wxMenu : public wxEvtHandler
  377. {
  378. public:
  379. /**
  380. Constructs a wxMenu object.
  381. */
  382. wxMenu();
  383. /**
  384. Constructs a wxMenu object.
  385. @param style
  386. If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
  387. */
  388. wxMenu(long style);
  389. /**
  390. Constructs a wxMenu object with a title.
  391. @param title
  392. Title at the top of the menu (not always supported).
  393. @param style
  394. If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
  395. */
  396. wxMenu(const wxString& title, long style = 0);
  397. /**
  398. Destructor, destroying the menu.
  399. @note
  400. Under Motif, a popup menu must have a valid parent (the window
  401. it was last popped up on) when being destroyed. Therefore, make sure
  402. you delete or re-use the popup menu @e before destroying the parent
  403. window. Re-use in this context means popping up the menu on a different
  404. window from last time, which causes an implicit destruction and
  405. recreation of internal data structures.
  406. */
  407. virtual ~wxMenu();
  408. /**
  409. Adds a menu item.
  410. @param id
  411. The menu command identifier.
  412. @param item
  413. The string to appear on the menu item.
  414. See wxMenuItem::SetItemLabel() for more details.
  415. @param helpString
  416. An optional help string associated with the item.
  417. By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays
  418. this string in the status line.
  419. @param kind
  420. May be @c wxITEM_SEPARATOR, @c wxITEM_NORMAL, @c wxITEM_CHECK or @c wxITEM_RADIO.
  421. Example:
  422. @code
  423. m_pFileMenu->Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a new XYZ document");
  424. @endcode
  425. or even better for stock menu items (see wxMenuItem::wxMenuItem):
  426. @code
  427. m_pFileMenu->Append(wxID_NEW, "", "Creates a new XYZ document");
  428. @endcode
  429. @remarks
  430. This command can be used after the menu has been shown, as well as on
  431. initial creation of a menu or menubar.
  432. @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
  433. AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
  434. SetHelpString(), wxMenuItem
  435. */
  436. wxMenuItem* Append(int id, const wxString& item = wxEmptyString,
  437. const wxString& helpString = wxEmptyString,
  438. wxItemKind kind = wxITEM_NORMAL);
  439. /**
  440. Adds a submenu.
  441. @deprecated This function is deprecated, use AppendSubMenu() instead.
  442. @param id
  443. The menu command identifier.
  444. @param item
  445. The string to appear on the menu item.
  446. @param subMenu
  447. Pull-right submenu.
  448. @param helpString
  449. An optional help string associated with the item.
  450. By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays
  451. this string in the status line.
  452. @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
  453. AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
  454. SetHelpString(), wxMenuItem
  455. */
  456. wxMenuItem* Append(int id, const wxString& item, wxMenu* subMenu,
  457. const wxString& helpString = wxEmptyString);
  458. /**
  459. Adds a menu item object.
  460. This is the most generic variant of Append() method because it may be
  461. used for both items (including separators) and submenus and because
  462. you can also specify various extra properties of a menu item this way,
  463. such as bitmaps and fonts.
  464. @param menuItem
  465. A menuitem object. It will be owned by the wxMenu object after this
  466. function is called, so do not delete it yourself.
  467. @remarks
  468. See the remarks for the other Append() overloads.
  469. @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
  470. AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
  471. SetHelpString(), wxMenuItem
  472. */
  473. wxMenuItem* Append(wxMenuItem* menuItem);
  474. /**
  475. Adds a checkable item to the end of the menu.
  476. @see Append(), InsertCheckItem()
  477. */
  478. wxMenuItem* AppendCheckItem(int id, const wxString& item,
  479. const wxString& help = wxEmptyString);
  480. /**
  481. Adds a radio item to the end of the menu.
  482. All consequent radio items form a group and when an item in the group is
  483. checked, all the others are automatically unchecked.
  484. @note Radio items are not supported under wxMotif.
  485. @see Append(), InsertRadioItem()
  486. */
  487. wxMenuItem* AppendRadioItem(int id, const wxString& item,
  488. const wxString& help = wxEmptyString);
  489. /**
  490. Adds a separator to the end of the menu.
  491. @see Append(), InsertSeparator()
  492. */
  493. wxMenuItem* AppendSeparator();
  494. /**
  495. Adds the given @a submenu to this menu. @a text is the text shown in the
  496. menu for it and @a help is the help string shown in the status bar when the
  497. submenu item is selected.
  498. @see Insert(), Prepend()
  499. */
  500. wxMenuItem* AppendSubMenu(wxMenu* submenu, const wxString& text,
  501. const wxString& help = wxEmptyString);
  502. /**
  503. Inserts a break in a menu, causing the next appended item to appear in
  504. a new column.
  505. */
  506. virtual void Break();
  507. /**
  508. Checks or unchecks the menu item.
  509. @param id
  510. The menu item identifier.
  511. @param check
  512. If @true, the item will be checked, otherwise it will be unchecked.
  513. @see IsChecked()
  514. */
  515. void Check(int id, bool check);
  516. /**
  517. Deletes the menu item from the menu. If the item is a submenu, it will
  518. @b not be deleted. Use Destroy() if you want to delete a submenu.
  519. @param id
  520. Id of the menu item to be deleted.
  521. @see FindItem(), Destroy(), Remove()
  522. */
  523. bool Delete(int id);
  524. /**
  525. Deletes the menu item from the menu. If the item is a submenu, it will
  526. @b not be deleted. Use Destroy() if you want to delete a submenu.
  527. @param item
  528. Menu item to be deleted.
  529. @see FindItem(), Destroy(), Remove()
  530. */
  531. bool Delete(wxMenuItem* item);
  532. /**
  533. Deletes the menu item from the menu. If the item is a submenu, it will
  534. be deleted. Use Remove() if you want to keep the submenu (for example,
  535. to reuse it later).
  536. @param id
  537. Id of the menu item to be deleted.
  538. @see FindItem(), Delete(), Remove()
  539. */
  540. bool Destroy(int id);
  541. /**
  542. Deletes the menu item from the menu. If the item is a submenu, it will
  543. be deleted. Use Remove() if you want to keep the submenu (for example,
  544. to reuse it later).
  545. @param item
  546. Menu item to be deleted.
  547. @see FindItem(), Delete(), Remove()
  548. */
  549. bool Destroy(wxMenuItem* item);
  550. /**
  551. Enables or disables (greys out) a menu item.
  552. @param id
  553. The menu item identifier.
  554. @param enable
  555. @true to enable the menu item, @false to disable it.
  556. @see IsEnabled()
  557. */
  558. void Enable(int id, bool enable);
  559. /**
  560. Finds the menu item object associated with the given menu item identifier
  561. and, optionally, the position of the item in the menu.
  562. Unlike FindItem(), this function doesn't recurse but only looks at the
  563. direct children of this menu.
  564. @param id
  565. The identifier of the menu item to find.
  566. @param pos
  567. If the pointer is not @NULL, it is filled with the item's position if
  568. it was found or @c (size_t)wxNOT_FOUND otherwise.
  569. @return
  570. Menu item object or @NULL if not found.
  571. */
  572. wxMenuItem *FindChildItem(int id, size_t *pos = NULL) const;
  573. /**
  574. Finds the menu id for a menu item string.
  575. @param itemString
  576. Menu item string to find.
  577. @return Menu item identifier, or wxNOT_FOUND if none is found.
  578. @remarks Any special menu codes are stripped out of source and target
  579. strings before matching.
  580. */
  581. virtual int FindItem(const wxString& itemString) const;
  582. /**
  583. Finds the menu item object associated with the given menu item identifier and,
  584. optionally, the (sub)menu it belongs to.
  585. @param id
  586. Menu item identifier.
  587. @param menu
  588. If the pointer is not @NULL, it will be filled with the item's
  589. parent menu (if the item was found)
  590. @return Menu item object or NULL if none is found.
  591. */
  592. wxMenuItem* FindItem(int id, wxMenu** menu = NULL) const;
  593. /**
  594. Returns the wxMenuItem given a position in the menu.
  595. */
  596. wxMenuItem* FindItemByPosition(size_t position) const;
  597. /**
  598. Returns the help string associated with a menu item.
  599. @param id
  600. The menu item identifier.
  601. @return The help string, or the empty string if there is no help string
  602. or the item was not found.
  603. @see SetHelpString(), Append()
  604. */
  605. virtual wxString GetHelpString(int id) const;
  606. /**
  607. Returns a menu item label.
  608. @param id
  609. The menu item identifier.
  610. @return The item label, or the empty string if the item was not found.
  611. @see GetLabelText(), SetLabel()
  612. */
  613. wxString GetLabel(int id) const;
  614. /**
  615. Returns a menu item label, without any of the original mnemonics and
  616. accelerators.
  617. @param id
  618. The menu item identifier.
  619. @return The item label, or the empty string if the item was not found.
  620. @see GetLabel(), SetLabel()
  621. */
  622. wxString GetLabelText(int id) const;
  623. /**
  624. Returns the number of items in the menu.
  625. */
  626. size_t GetMenuItemCount() const;
  627. //@{
  628. /**
  629. Returns the list of items in the menu.
  630. wxMenuItemList is a pseudo-template list class containing wxMenuItem
  631. pointers, see wxList.
  632. */
  633. wxMenuItemList& GetMenuItems();
  634. const wxMenuItemList& GetMenuItems() const;
  635. //@}
  636. /**
  637. Returns the title of the menu.
  638. @see SetTitle()
  639. */
  640. const wxString& GetTitle() const;
  641. /**
  642. Inserts the given @a item before the position @a pos.
  643. Inserting the item at position GetMenuItemCount() is the same
  644. as appending it.
  645. @see Append(), Prepend()
  646. */
  647. wxMenuItem* Insert(size_t pos, wxMenuItem* menuItem);
  648. /**
  649. Inserts the given @a item before the position @a pos.
  650. Inserting the item at position GetMenuItemCount() is the same
  651. as appending it.
  652. @see Append(), Prepend()
  653. */
  654. wxMenuItem* Insert(size_t pos, int id,
  655. const wxString& item = wxEmptyString,
  656. const wxString& helpString = wxEmptyString,
  657. wxItemKind kind = wxITEM_NORMAL);
  658. /**
  659. Inserts the given @a submenu before the position @a pos.
  660. @a text is the text shown in the menu for it and @a help is the
  661. help string shown in the status bar when the submenu item is selected.
  662. @see AppendSubMenu(), Prepend()
  663. */
  664. wxMenuItem* Insert(size_t pos, int id, const wxString& text,
  665. wxMenu* submenu, const wxString& help = wxEmptyString);
  666. /**
  667. Inserts a checkable item at the given position.
  668. @see Insert(), AppendCheckItem()
  669. */
  670. wxMenuItem* InsertCheckItem(size_t pos, int id, const wxString& item,
  671. const wxString& helpString = wxEmptyString);
  672. /**
  673. Inserts a radio item at the given position.
  674. @see Insert(), AppendRadioItem()
  675. */
  676. wxMenuItem* InsertRadioItem(size_t pos, int id, const wxString& item,
  677. const wxString& helpString = wxEmptyString);
  678. /**
  679. Inserts a separator at the given position.
  680. @see Insert(), AppendSeparator()
  681. */
  682. wxMenuItem* InsertSeparator(size_t pos);
  683. /**
  684. Determines whether a menu item is checked.
  685. @param id
  686. The menu item identifier.
  687. @return @true if the menu item is checked, @false otherwise.
  688. @see Check()
  689. */
  690. bool IsChecked(int id) const;
  691. /**
  692. Determines whether a menu item is enabled.
  693. @param id
  694. The menu item identifier.
  695. @return @true if the menu item is enabled, @false otherwise.
  696. @see Enable()
  697. */
  698. bool IsEnabled(int id) const;
  699. /**
  700. Inserts the given @a item at position 0, i.e.\ before all the other
  701. existing items.
  702. @see Append(), Insert()
  703. */
  704. wxMenuItem* Prepend(wxMenuItem* item);
  705. /**
  706. Inserts the given @a item at position 0, i.e.\ before all the other
  707. existing items.
  708. @see Append(), Insert()
  709. */
  710. wxMenuItem* Prepend(int id, const wxString& item = wxEmptyString,
  711. const wxString& helpString = wxEmptyString,
  712. wxItemKind kind = wxITEM_NORMAL);
  713. /**
  714. Inserts the given @a submenu at position 0.
  715. @see AppendSubMenu(), Insert()
  716. */
  717. wxMenuItem* Prepend(int id, const wxString& text, wxMenu* submenu,
  718. const wxString& help = wxEmptyString);
  719. /**
  720. Inserts a checkable item at position 0.
  721. @see Prepend(), AppendCheckItem()
  722. */
  723. wxMenuItem* PrependCheckItem(int id, const wxString& item,
  724. const wxString& helpString = wxEmptyString);
  725. /**
  726. Inserts a radio item at position 0.
  727. @see Prepend(), AppendRadioItem()
  728. */
  729. wxMenuItem* PrependRadioItem(int id, const wxString& item,
  730. const wxString& helpString = wxEmptyString);
  731. /**
  732. Inserts a separator at position 0.
  733. @see Prepend(), AppendSeparator()
  734. */
  735. wxMenuItem* PrependSeparator();
  736. /**
  737. Removes the menu item from the menu but doesn't delete the associated C++
  738. object. This allows you to reuse the same item later by adding it back to
  739. the menu (especially useful with submenus).
  740. @param id
  741. The identifier of the menu item to remove.
  742. @return A pointer to the item which was detached from the menu.
  743. */
  744. wxMenuItem* Remove(int id);
  745. /**
  746. Removes the menu item from the menu but doesn't delete the associated C++
  747. object. This allows you to reuse the same item later by adding it back to
  748. the menu (especially useful with submenus).
  749. @param item
  750. The menu item to remove.
  751. @return A pointer to the item which was detached from the menu.
  752. */
  753. wxMenuItem* Remove(wxMenuItem* item);
  754. /**
  755. Sets an item's help string.
  756. @param id
  757. The menu item identifier.
  758. @param helpString
  759. The help string to set.
  760. @see GetHelpString()
  761. */
  762. virtual void SetHelpString(int id, const wxString& helpString);
  763. /**
  764. Sets the label of a menu item.
  765. @param id
  766. The menu item identifier.
  767. @param label
  768. The menu item label to set.
  769. @see Append(), GetLabel()
  770. */
  771. void SetLabel(int id, const wxString& label);
  772. /**
  773. Sets the title of the menu.
  774. @param title
  775. The title to set.
  776. @remarks Notice that you can only call this method directly for the
  777. popup menus, to change the title of a menu that is part of a menu
  778. bar you need to use wxMenuBar::SetLabelTop().
  779. @see GetTitle()
  780. */
  781. virtual void SetTitle(const wxString& title);
  782. /**
  783. Sends events to @a source (or owning window if @NULL) to update the
  784. menu UI.
  785. This is called just before the menu is popped up with wxWindow::PopupMenu,
  786. but the application may call it at other times if required.
  787. */
  788. void UpdateUI(wxEvtHandler* source = NULL);
  789. void SetInvokingWindow(wxWindow *win);
  790. wxWindow *GetInvokingWindow() const;
  791. wxWindow *GetWindow() const;
  792. long GetStyle() const;
  793. void SetParent(wxMenu *parent);
  794. wxMenu *GetParent() const;
  795. virtual void Attach(wxMenuBar *menubar);
  796. virtual void Detach();
  797. bool IsAttached() const;
  798. };