Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_PopupMenu.h 28KB

9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
10 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_POPUPMENU_H_INCLUDED
  18. #define JUCE_POPUPMENU_H_INCLUDED
  19. //==============================================================================
  20. /** Creates and displays a popup-menu.
  21. To show a popup-menu, you create one of these, add some items to it, then
  22. call its show() method, which returns the id of the item the user selects.
  23. E.g. @code
  24. void MyWidget::mouseDown (const MouseEvent& e)
  25. {
  26. PopupMenu m;
  27. m.addItem (1, "item 1");
  28. m.addItem (2, "item 2");
  29. const int result = m.show();
  30. if (result == 0)
  31. {
  32. // user dismissed the menu without picking anything
  33. }
  34. else if (result == 1)
  35. {
  36. // user picked item 1
  37. }
  38. else if (result == 2)
  39. {
  40. // user picked item 2
  41. }
  42. }
  43. @endcode
  44. Submenus are easy too: @code
  45. void MyWidget::mouseDown (const MouseEvent& e)
  46. {
  47. PopupMenu subMenu;
  48. subMenu.addItem (1, "item 1");
  49. subMenu.addItem (2, "item 2");
  50. PopupMenu mainMenu;
  51. mainMenu.addItem (3, "item 3");
  52. mainMenu.addSubMenu ("other choices", subMenu);
  53. const int result = m.show();
  54. ...etc
  55. }
  56. @endcode
  57. */
  58. class JUCE_API PopupMenu
  59. {
  60. private:
  61. class Window;
  62. public:
  63. class CustomComponent;
  64. //==============================================================================
  65. /** Creates an empty popup menu. */
  66. PopupMenu();
  67. /** Creates a copy of another menu. */
  68. PopupMenu (const PopupMenu& other);
  69. /** Destructor. */
  70. ~PopupMenu();
  71. /** Copies this menu from another one. */
  72. PopupMenu& operator= (const PopupMenu& other);
  73. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  74. PopupMenu (PopupMenu&& other) noexcept;
  75. PopupMenu& operator= (PopupMenu&& other) noexcept;
  76. #endif
  77. //==============================================================================
  78. /** Resets the menu, removing all its items. */
  79. void clear();
  80. /** Appends a new text item for this menu to show.
  81. @param itemResultID the number that will be returned from the show() method
  82. if the user picks this item. The value should never be
  83. zero, because that's used to indicate that the user didn't
  84. select anything.
  85. @param itemText the text to show.
  86. @param isEnabled if false, the item will be shown 'greyed-out' and can't be picked
  87. @param isTicked if true, the item will be shown with a tick next to it
  88. @see addSeparator, addColouredItem, addCustomItem, addSubMenu
  89. */
  90. void addItem (int itemResultID,
  91. const String& itemText,
  92. bool isEnabled = true,
  93. bool isTicked = false);
  94. /** Appends a new item with an icon.
  95. @param itemResultID the number that will be returned from the show() method
  96. if the user picks this item. The value should never be
  97. zero, because that's used to indicate that the user didn't
  98. select anything.
  99. @param itemText the text to show.
  100. @param isEnabled if false, the item will be shown 'greyed-out' and can't be picked
  101. @param isTicked if true, the item will be shown with a tick next to it
  102. @param iconToUse if this is a valid image, it will be displayed to the left of the item.
  103. @see addSeparator, addColouredItem, addCustomItem, addSubMenu
  104. */
  105. void addItem (int itemResultID,
  106. const String& itemText,
  107. bool isEnabled,
  108. bool isTicked,
  109. const Image& iconToUse);
  110. /** Appends a new item with an icon.
  111. @param itemResultID the number that will be returned from the show() method
  112. if the user picks this item. The value should never be
  113. zero, because that's used to indicate that the user didn't
  114. select anything.
  115. @param itemText the text to show.
  116. @param isEnabled if false, the item will be shown 'greyed-out' and can't be picked
  117. @param isTicked if true, the item will be shown with a tick next to it
  118. @param iconToUse a Drawable object to use as the icon to the left of the item.
  119. The menu will take ownership of this drawable object and will
  120. delete it later when no longer needed
  121. @see addSeparator, addColouredItem, addCustomItem, addSubMenu
  122. */
  123. void addItem (int itemResultID,
  124. const String& itemText,
  125. bool isEnabled,
  126. bool isTicked,
  127. Drawable* iconToUse);
  128. /** Adds an item that represents one of the commands in a command manager object.
  129. @param commandManager the manager to use to trigger the command and get information
  130. about it
  131. @param commandID the ID of the command
  132. @param displayName if this is non-empty, then this string will be used instead of
  133. the command's registered name
  134. @param iconToUse an optional Drawable object to use as the icon to the left of the item.
  135. The menu will take ownership of this drawable object and will
  136. delete it later when no longer needed
  137. */
  138. void addCommandItem (ApplicationCommandManager* commandManager,
  139. CommandID commandID,
  140. const String& displayName = String::empty,
  141. Drawable* iconToUse = nullptr);
  142. /** Appends a text item with a special colour.
  143. This is the same as addItem(), but specifies a colour to use for the
  144. text, which will override the default colours that are used by the
  145. current look-and-feel. See addItem() for a description of the parameters.
  146. */
  147. void addColouredItem (int itemResultID,
  148. const String& itemText,
  149. Colour itemTextColour,
  150. bool isEnabled = true,
  151. bool isTicked = false,
  152. const Image& iconToUse = Image());
  153. /** Appends a text item with a special colour.
  154. This is the same as addItem(), but specifies a colour to use for the
  155. text, which will override the default colours that are used by the
  156. current look-and-feel. See addItem() for a description of the parameters.
  157. */
  158. void addColouredItem (int itemResultID,
  159. const String& itemText,
  160. Colour itemTextColour,
  161. bool isEnabled,
  162. bool isTicked,
  163. Drawable* iconToUse);
  164. /** Appends a custom menu item.
  165. This will add a user-defined component to use as a menu item. The component
  166. passed in will be deleted by this menu when it's no longer needed.
  167. @see CustomComponent
  168. */
  169. void addCustomItem (int itemResultID,
  170. CustomComponent* customComponent,
  171. const PopupMenu* optionalSubMenu = nullptr);
  172. /** Appends a custom menu item that can't be used to trigger a result.
  173. This will add a user-defined component to use as a menu item.
  174. It's the caller's responsibility to delete the component that is passed-in
  175. when it's no longer needed after the menu has been hidden.
  176. If triggerMenuItemAutomaticallyWhenClicked is true, the menu itself will handle
  177. detection of a mouse-click on your component, and use that to trigger the
  178. menu ID specified in itemResultID. If this is false, the menu item can't
  179. be triggered, so itemResultID is not used.
  180. */
  181. void addCustomItem (int itemResultID,
  182. Component* customComponent,
  183. int idealWidth,
  184. int idealHeight,
  185. bool triggerMenuItemAutomaticallyWhenClicked,
  186. const PopupMenu* optionalSubMenu = nullptr);
  187. /** Appends a sub-menu.
  188. If the menu that's passed in is empty, it will appear as an inactive item.
  189. If the itemResultID argument is non-zero, then the sub-menu item itself can be
  190. clicked to trigger it as a command.
  191. */
  192. void addSubMenu (const String& subMenuName,
  193. const PopupMenu& subMenu,
  194. bool isEnabled = true);
  195. /** Appends a sub-menu with an icon.
  196. If the menu that's passed in is empty, it will appear as an inactive item.
  197. If the itemResultID argument is non-zero, then the sub-menu item itself can be
  198. clicked to trigger it as a command.
  199. */
  200. void addSubMenu (const String& subMenuName,
  201. const PopupMenu& subMenu,
  202. bool isEnabled,
  203. const Image& iconToUse,
  204. bool isTicked = false,
  205. int itemResultID = 0);
  206. /** Appends a sub-menu with an icon.
  207. If the menu that's passed in is empty, it will appear as an inactive item.
  208. If the itemResultID argument is non-zero, then the sub-menu item itself can be
  209. clicked to trigger it as a command.
  210. The iconToUse parameter is a Drawable object to use as the icon to the left of
  211. the item. The menu will take ownership of this drawable object and will delete it
  212. later when no longer needed
  213. */
  214. void addSubMenu (const String& subMenuName,
  215. const PopupMenu& subMenu,
  216. bool isEnabled,
  217. Drawable* iconToUse,
  218. bool isTicked = false,
  219. int itemResultID = 0);
  220. /** Appends a separator to the menu, to help break it up into sections.
  221. The menu class is smart enough not to display separators at the top or bottom
  222. of the menu, and it will replace mutliple adjacent separators with a single
  223. one, so your code can be quite free and easy about adding these, and it'll
  224. always look ok.
  225. */
  226. void addSeparator();
  227. /** Adds a non-clickable text item to the menu.
  228. This is a bold-font items which can be used as a header to separate the items
  229. into named groups.
  230. */
  231. void addSectionHeader (const String& title);
  232. /** Returns the number of items that the menu currently contains.
  233. (This doesn't count separators).
  234. */
  235. int getNumItems() const noexcept;
  236. /** Returns true if the menu contains a command item that triggers the given command. */
  237. bool containsCommandItem (int commandID) const;
  238. /** Returns true if the menu contains any items that can be used. */
  239. bool containsAnyActiveItems() const noexcept;
  240. //==============================================================================
  241. /** Class used to create a set of options to pass to the show() method.
  242. You can chain together a series of calls to this class's methods to create
  243. a set of whatever options you want to specify.
  244. E.g. @code
  245. PopupMenu menu;
  246. ...
  247. menu.showMenu (PopupMenu::Options().withMinimumWidth (100)
  248. .withMaximumNumColumns (3)
  249. .withTargetComponent (myComp));
  250. @endcode
  251. */
  252. class JUCE_API Options
  253. {
  254. public:
  255. Options();
  256. Options withTargetComponent (Component* targetComponent) const noexcept;
  257. Options withTargetScreenArea (const Rectangle<int>& targetArea) const noexcept;
  258. Options withMinimumWidth (int minWidth) const noexcept;
  259. Options withMaximumNumColumns (int maxNumColumns) const noexcept;
  260. Options withStandardItemHeight (int standardHeight) const noexcept;
  261. Options withItemThatMustBeVisible (int idOfItemToBeVisible) const noexcept;
  262. private:
  263. friend class PopupMenu;
  264. friend class PopupMenu::Window;
  265. Rectangle<int> targetArea;
  266. Component* targetComponent;
  267. int visibleItemID, minWidth, maxColumns, standardHeight;
  268. };
  269. //==============================================================================
  270. #if JUCE_MODAL_LOOPS_PERMITTED
  271. /** Displays the menu and waits for the user to pick something.
  272. This will display the menu modally, and return the ID of the item that the
  273. user picks. If they click somewhere off the menu to get rid of it without
  274. choosing anything, this will return 0.
  275. The current location of the mouse will be used as the position to show the
  276. menu - to explicitly set the menu's position, use showAt() instead. Depending
  277. on where this point is on the screen, the menu will appear above, below or
  278. to the side of the point.
  279. @param itemIDThatMustBeVisible if you set this to the ID of one of the menu items,
  280. then when the menu first appears, it will make sure
  281. that this item is visible. So if the menu has too many
  282. items to fit on the screen, it will be scrolled to a
  283. position where this item is visible.
  284. @param minimumWidth a minimum width for the menu, in pixels. It may be wider
  285. than this if some items are too long to fit.
  286. @param maximumNumColumns if there are too many items to fit on-screen in a single
  287. vertical column, the menu may be laid out as a series of
  288. columns - this is the maximum number allowed. To use the
  289. default value for this (probably about 7), you can pass
  290. in zero.
  291. @param standardItemHeight if this is non-zero, it will be used as the standard
  292. height for menu items (apart from custom items)
  293. @param callback if this is non-zero, the menu will be launched asynchronously,
  294. returning immediately, and the callback will receive a
  295. call when the menu is either dismissed or has an item
  296. selected. This object will be owned and deleted by the
  297. system, so make sure that it works safely and that any
  298. pointers that it uses are safely within scope.
  299. @see showAt
  300. */
  301. int show (int itemIDThatMustBeVisible = 0,
  302. int minimumWidth = 0,
  303. int maximumNumColumns = 0,
  304. int standardItemHeight = 0,
  305. ModalComponentManager::Callback* callback = nullptr);
  306. /** Displays the menu at a specific location.
  307. This is the same as show(), but uses a specific location (in global screen
  308. coordinates) rather than the current mouse position.
  309. The screenAreaToAttachTo parameter indicates a screen area to which the menu
  310. will be adjacent. Depending on where this is, the menu will decide which edge to
  311. attach itself to, in order to fit itself fully on-screen. If you just want to
  312. trigger a menu at a specific point, you can pass in a rectangle of size (0, 0)
  313. with the position that you want.
  314. @see show()
  315. */
  316. int showAt (const Rectangle<int>& screenAreaToAttachTo,
  317. int itemIDThatMustBeVisible = 0,
  318. int minimumWidth = 0,
  319. int maximumNumColumns = 0,
  320. int standardItemHeight = 0,
  321. ModalComponentManager::Callback* callback = nullptr);
  322. /** Displays the menu as if it's attached to a component such as a button.
  323. This is similar to showAt(), but will position it next to the given component, e.g.
  324. so that the menu's edge is aligned with that of the component. This is intended for
  325. things like buttons that trigger a pop-up menu.
  326. */
  327. int showAt (Component* componentToAttachTo,
  328. int itemIDThatMustBeVisible = 0,
  329. int minimumWidth = 0,
  330. int maximumNumColumns = 0,
  331. int standardItemHeight = 0,
  332. ModalComponentManager::Callback* callback = nullptr);
  333. /** Displays and runs the menu modally, with a set of options.
  334. */
  335. int showMenu (const Options& options);
  336. #endif
  337. /** Runs the menu asynchronously, with a user-provided callback that will receive the result. */
  338. void showMenuAsync (const Options& options,
  339. ModalComponentManager::Callback* callback);
  340. //==============================================================================
  341. /** Closes any menus that are currently open.
  342. This might be useful if you have a situation where your window is being closed
  343. by some means other than a user action, and you'd like to make sure that menus
  344. aren't left hanging around.
  345. */
  346. static bool JUCE_CALLTYPE dismissAllActiveMenus();
  347. //==============================================================================
  348. /** Specifies a look-and-feel for the menu and any sub-menus that it has.
  349. This can be called before show() if you need a customised menu. Be careful
  350. not to delete the LookAndFeel object before the menu has been deleted.
  351. */
  352. void setLookAndFeel (LookAndFeel* newLookAndFeel);
  353. //==============================================================================
  354. /** A set of colour IDs to use to change the colour of various aspects of the menu.
  355. These constants can be used either via the LookAndFeel::setColour()
  356. method for the look and feel that is set for this menu with setLookAndFeel()
  357. @see setLookAndFeel, LookAndFeel::setColour, LookAndFeel::findColour
  358. */
  359. enum ColourIds
  360. {
  361. backgroundColourId = 0x1000700, /**< The colour to fill the menu's background with. */
  362. textColourId = 0x1000600, /**< The colour for normal menu item text, (unless the
  363. colour is specified when the item is added). */
  364. headerTextColourId = 0x1000601, /**< The colour for section header item text (see the
  365. addSectionHeader() method). */
  366. highlightedBackgroundColourId = 0x1000900, /**< The colour to fill the background of the currently
  367. highlighted menu item. */
  368. highlightedTextColourId = 0x1000800, /**< The colour to use for the text of the currently
  369. highlighted item. */
  370. };
  371. //==============================================================================
  372. /**
  373. Allows you to iterate through the items in a pop-up menu, and examine
  374. their properties.
  375. To use this, just create one and repeatedly call its next() method. When this
  376. returns true, all the member variables of the iterator are filled-out with
  377. information describing the menu item. When it returns false, the end of the
  378. list has been reached.
  379. */
  380. class JUCE_API MenuItemIterator
  381. {
  382. public:
  383. //==============================================================================
  384. /** Creates an iterator that will scan through the items in the specified
  385. menu.
  386. Be careful not to add any items to a menu while it is being iterated,
  387. or things could get out of step.
  388. */
  389. MenuItemIterator (const PopupMenu& menu);
  390. /** Destructor. */
  391. ~MenuItemIterator();
  392. /** Returns true if there is another item, and sets up all this object's
  393. member variables to reflect that item's properties.
  394. */
  395. bool next();
  396. /** Adds an item to the target menu which has all the properties of this item. */
  397. void addItemTo (PopupMenu& targetMenu);
  398. //==============================================================================
  399. String itemName;
  400. const PopupMenu* subMenu;
  401. int itemId;
  402. bool isSeparator;
  403. bool isTicked;
  404. bool isEnabled;
  405. bool isCustomComponent;
  406. bool isSectionHeader;
  407. const Colour* customColour;
  408. const Drawable* icon;
  409. ApplicationCommandManager* commandManager;
  410. private:
  411. //==============================================================================
  412. const PopupMenu& menu;
  413. int index;
  414. MenuItemIterator& operator= (const MenuItemIterator&);
  415. JUCE_LEAK_DETECTOR (MenuItemIterator)
  416. };
  417. //==============================================================================
  418. /** A user-defined component that can be used as an item in a popup menu.
  419. @see PopupMenu::addCustomItem
  420. */
  421. class JUCE_API CustomComponent : public Component,
  422. public SingleThreadedReferenceCountedObject
  423. {
  424. public:
  425. /** Creates a custom item.
  426. If isTriggeredAutomatically is true, then the menu will automatically detect
  427. a mouse-click on this component and use that to invoke the menu item. If it's
  428. false, then it's up to your class to manually trigger the item when it wants to.
  429. */
  430. CustomComponent (bool isTriggeredAutomatically = true);
  431. /** Destructor. */
  432. ~CustomComponent();
  433. /** Returns a rectangle with the size that this component would like to have.
  434. Note that the size which this method returns isn't necessarily the one that
  435. the menu will give it, as the items will be stretched to have a uniform width.
  436. */
  437. virtual void getIdealSize (int& idealWidth, int& idealHeight) = 0;
  438. /** Dismisses the menu, indicating that this item has been chosen.
  439. This will cause the menu to exit from its modal state, returning
  440. this item's id as the result.
  441. */
  442. void triggerMenuItem();
  443. /** Returns true if this item should be highlighted because the mouse is over it.
  444. You can call this method in your paint() method to find out whether
  445. to draw a highlight.
  446. */
  447. bool isItemHighlighted() const noexcept { return isHighlighted; }
  448. /** @internal */
  449. bool isTriggeredAutomatically() const noexcept { return triggeredAutomatically; }
  450. /** @internal */
  451. void setHighlighted (bool shouldBeHighlighted);
  452. private:
  453. //==============================================================================
  454. bool isHighlighted, triggeredAutomatically;
  455. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomComponent)
  456. };
  457. //==============================================================================
  458. /** This abstract base class is implemented by LookAndFeel classes to provide
  459. menu drawing functionality.
  460. */
  461. struct JUCE_API LookAndFeelMethods
  462. {
  463. virtual ~LookAndFeelMethods() {}
  464. /** Fills the background of a popup menu component. */
  465. virtual void drawPopupMenuBackground (Graphics&, int width, int height) = 0;
  466. /** Draws one of the items in a popup menu. */
  467. virtual void drawPopupMenuItem (Graphics&, const Rectangle<int>& area,
  468. bool isSeparator, bool isActive, bool isHighlighted,
  469. bool isTicked, bool hasSubMenu,
  470. const String& text,
  471. const String& shortcutKeyText,
  472. const Drawable* icon,
  473. const Colour* textColour) = 0;
  474. virtual void drawPopupMenuSectionHeader (Graphics&, const Rectangle<int>& area,
  475. const String& sectionName) = 0;
  476. /** Returns the size and style of font to use in popup menus. */
  477. virtual Font getPopupMenuFont() = 0;
  478. virtual void drawPopupMenuUpDownArrow (Graphics&,
  479. int width, int height,
  480. bool isScrollUpArrow) = 0;
  481. /** Finds the best size for an item in a popup menu. */
  482. virtual void getIdealPopupMenuItemSize (const String& text,
  483. bool isSeparator,
  484. int standardMenuItemHeight,
  485. int& idealWidth,
  486. int& idealHeight) = 0;
  487. virtual int getMenuWindowFlags() = 0;
  488. virtual void drawMenuBarBackground (Graphics&, int width, int height,
  489. bool isMouseOverBar,
  490. MenuBarComponent&) = 0;
  491. virtual int getDefaultMenuBarHeight() = 0;
  492. virtual int getMenuBarItemWidth (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
  493. virtual Font getMenuBarFont (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
  494. virtual void drawMenuBarItem (Graphics&, int width, int height,
  495. int itemIndex,
  496. const String& itemText,
  497. bool isMouseOverItem,
  498. bool isMenuOpen,
  499. bool isMouseOverBar,
  500. MenuBarComponent&) = 0;
  501. };
  502. private:
  503. //==============================================================================
  504. JUCE_PUBLIC_IN_DLL_BUILD (class Item)
  505. JUCE_PUBLIC_IN_DLL_BUILD (struct HelperClasses)
  506. friend struct HelperClasses;
  507. friend class MenuBarComponent;
  508. OwnedArray<Item> items;
  509. WeakReference<LookAndFeel> lookAndFeel;
  510. Component* createWindow (const Options&, ApplicationCommandManager**) const;
  511. int showWithOptionalCallback (const Options&, ModalComponentManager::Callback*, bool);
  512. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  513. // These methods have new implementations now - see its new definition
  514. int drawPopupMenuItem (Graphics&, int, int, bool, bool, bool, bool, bool, const String&, const String&, Image*, const Colour*) { return 0; }
  515. #endif
  516. JUCE_LEAK_DETECTOR (PopupMenu)
  517. };
  518. #endif // JUCE_POPUPMENU_H_INCLUDED