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.

622 lines
27KB

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