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.

1040 lines
47KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /** Creates and displays a popup-menu.
  17. To show a popup-menu, you create one of these, add some items to it, then
  18. call its show() method, which returns the id of the item the user selects.
  19. E.g. @code
  20. void MyWidget::mouseDown (const MouseEvent& e)
  21. {
  22. PopupMenu m;
  23. m.addItem (1, "item 1");
  24. m.addItem (2, "item 2");
  25. m.showMenuAsync (PopupMenu::Options(),
  26. [] (int result)
  27. {
  28. if (result == 0)
  29. {
  30. // user dismissed the menu without picking anything
  31. }
  32. else if (result == 1)
  33. {
  34. // user picked item 1
  35. }
  36. else if (result == 2)
  37. {
  38. // user picked item 2
  39. }
  40. });
  41. }
  42. @endcode
  43. Submenus are easy too: @code
  44. void MyWidget::mouseDown (const MouseEvent& e)
  45. {
  46. PopupMenu subMenu;
  47. subMenu.addItem (1, "item 1");
  48. subMenu.addItem (2, "item 2");
  49. PopupMenu mainMenu;
  50. mainMenu.addItem (3, "item 3");
  51. mainMenu.addSubMenu ("other choices", subMenu);
  52. m.showMenuAsync (...);
  53. }
  54. @endcode
  55. @tags{GUI}
  56. */
  57. class JUCE_API PopupMenu
  58. {
  59. public:
  60. //==============================================================================
  61. /** Creates an empty popup menu. */
  62. PopupMenu() = default;
  63. /** Creates a copy of another menu. */
  64. PopupMenu (const PopupMenu&);
  65. /** Destructor. */
  66. ~PopupMenu();
  67. /** Copies this menu from another one. */
  68. PopupMenu& operator= (const PopupMenu&);
  69. /** Move constructor */
  70. PopupMenu (PopupMenu&&) noexcept;
  71. /** Move assignment operator */
  72. PopupMenu& operator= (PopupMenu&&) noexcept;
  73. //==============================================================================
  74. class CustomComponent;
  75. class CustomCallback;
  76. //==============================================================================
  77. /** Resets the menu, removing all its items. */
  78. void clear();
  79. /** Describes a popup menu item. */
  80. struct JUCE_API Item
  81. {
  82. /** Creates a null item.
  83. You'll need to set some fields after creating an Item before you
  84. can add it to a PopupMenu
  85. */
  86. Item();
  87. /** Creates an item with the given text.
  88. This constructor also initialises the itemID to -1, which makes it suitable for
  89. creating lambda-based item actions.
  90. */
  91. Item (String text);
  92. Item (const Item&);
  93. Item& operator= (const Item&);
  94. Item (Item&&);
  95. Item& operator= (Item&&);
  96. /** The menu item's name. */
  97. String text;
  98. /** The menu item's ID.
  99. This must not be 0 if you want the item to be triggerable, but if you're attaching
  100. an action callback to the item, you can set the itemID to -1 to indicate that it
  101. isn't actively needed.
  102. */
  103. int itemID = 0;
  104. /** An optional function which should be invoked when this menu item is triggered. */
  105. std::function<void()> action;
  106. /** A sub-menu, or nullptr if there isn't one. */
  107. std::unique_ptr<PopupMenu> subMenu;
  108. /** A drawable to use as an icon, or nullptr if there isn't one. */
  109. std::unique_ptr<Drawable> image;
  110. /** A custom component for the item to display, or nullptr if there isn't one. */
  111. ReferenceCountedObjectPtr<CustomComponent> customComponent;
  112. /** A custom callback for the item to use, or nullptr if there isn't one. */
  113. ReferenceCountedObjectPtr<CustomCallback> customCallback;
  114. /** A command manager to use to automatically invoke the command, or nullptr if none is specified. */
  115. ApplicationCommandManager* commandManager = nullptr;
  116. /** An optional string describing the shortcut key for this item.
  117. This is only used for displaying at the right-hand edge of a menu item - the
  118. menu won't attempt to actually catch or process the key. If you supply a
  119. commandManager parameter then the menu will attempt to fill-in this field
  120. automatically.
  121. */
  122. String shortcutKeyDescription;
  123. /** A colour to use to draw the menu text.
  124. By default this is transparent black, which means that the LookAndFeel should choose the colour.
  125. */
  126. Colour colour;
  127. /** True if this menu item is enabled. */
  128. bool isEnabled = true;
  129. /** True if this menu item should have a tick mark next to it. */
  130. bool isTicked = false;
  131. /** True if this menu item is a separator line. */
  132. bool isSeparator = false;
  133. /** True if this menu item is a section header. */
  134. bool isSectionHeader = false;
  135. /** True if this is the final item in the current column. */
  136. bool shouldBreakAfter = false;
  137. /** Sets the isTicked flag (and returns a reference to this item to allow chaining). */
  138. Item& setTicked (bool shouldBeTicked = true) & noexcept;
  139. /** Sets the isEnabled flag (and returns a reference to this item to allow chaining). */
  140. Item& setEnabled (bool shouldBeEnabled) & noexcept;
  141. /** Sets the action property (and returns a reference to this item to allow chaining). */
  142. Item& setAction (std::function<void()> action) & noexcept;
  143. /** Sets the itemID property (and returns a reference to this item to allow chaining). */
  144. Item& setID (int newID) & noexcept;
  145. /** Sets the colour property (and returns a reference to this item to allow chaining). */
  146. Item& setColour (Colour) & noexcept;
  147. /** Sets the customComponent property (and returns a reference to this item to allow chaining). */
  148. Item& setCustomComponent (ReferenceCountedObjectPtr<CustomComponent> customComponent) & noexcept;
  149. /** Sets the image property (and returns a reference to this item to allow chaining). */
  150. Item& setImage (std::unique_ptr<Drawable>) & noexcept;
  151. /** Sets the isTicked flag (and returns a reference to this item to allow chaining). */
  152. Item&& setTicked (bool shouldBeTicked = true) && noexcept;
  153. /** Sets the isEnabled flag (and returns a reference to this item to allow chaining). */
  154. Item&& setEnabled (bool shouldBeEnabled) && noexcept;
  155. /** Sets the action property (and returns a reference to this item to allow chaining). */
  156. Item&& setAction (std::function<void()> action) && noexcept;
  157. /** Sets the itemID property (and returns a reference to this item to allow chaining). */
  158. Item&& setID (int newID) && noexcept;
  159. /** Sets the colour property (and returns a reference to this item to allow chaining). */
  160. Item&& setColour (Colour) && noexcept;
  161. /** Sets the customComponent property (and returns a reference to this item to allow chaining). */
  162. Item&& setCustomComponent (ReferenceCountedObjectPtr<CustomComponent> customComponent) && noexcept;
  163. /** Sets the image property (and returns a reference to this item to allow chaining). */
  164. Item&& setImage (std::unique_ptr<Drawable>) && noexcept;
  165. };
  166. /** Adds an item to the menu.
  167. You can call this method for full control over the item that is added, or use the other
  168. addItem helper methods if you want to pass arguments rather than creating an Item object.
  169. */
  170. void addItem (Item newItem);
  171. /** Adds an item to the menu with an action callback. */
  172. void addItem (String itemText,
  173. std::function<void()> action);
  174. /** Adds an item to the menu with an action callback. */
  175. void addItem (String itemText,
  176. bool isEnabled,
  177. bool isTicked,
  178. std::function<void()> action);
  179. /** Appends a new text item for this menu to show.
  180. @param itemResultID the number that will be returned from the show() method
  181. if the user picks this item. The value should never be
  182. zero, because that's used to indicate that the user didn't
  183. select anything.
  184. @param itemText the text to show.
  185. @param isEnabled if false, the item will be shown 'greyed-out' and can't be picked
  186. @param isTicked if true, the item will be shown with a tick next to it
  187. @see addSeparator, addColouredItem, addCustomItem, addSubMenu
  188. */
  189. void addItem (int itemResultID,
  190. String itemText,
  191. bool isEnabled = true,
  192. bool isTicked = false);
  193. /** Appends a new item with an icon.
  194. @param itemResultID the number that will be returned from the show() method
  195. if the user picks this item. The value should never be
  196. zero, because that's used to indicate that the user didn't
  197. select anything.
  198. @param itemText the text to show.
  199. @param isEnabled if false, the item will be shown 'greyed-out' and can't be picked
  200. @param isTicked if true, the item will be shown with a tick next to it
  201. @param iconToUse if this is a valid image, it will be displayed to the left of the item.
  202. @see addSeparator, addColouredItem, addCustomItem, addSubMenu
  203. */
  204. void addItem (int itemResultID,
  205. String itemText,
  206. bool isEnabled,
  207. bool isTicked,
  208. const Image& iconToUse);
  209. /** Appends a new item with an icon.
  210. @param itemResultID the number that will be returned from the show() method
  211. if the user picks this item. The value should never be
  212. zero, because that's used to indicate that the user didn't
  213. select anything.
  214. @param itemText the text to show.
  215. @param isEnabled if false, the item will be shown 'greyed-out' and can't be picked
  216. @param isTicked if true, the item will be shown with a tick next to it
  217. @param iconToUse a Drawable object to use as the icon to the left of the item.
  218. The menu will take ownership of this drawable object and will
  219. delete it later when no longer needed
  220. @see addSeparator, addColouredItem, addCustomItem, addSubMenu
  221. */
  222. void addItem (int itemResultID,
  223. String itemText,
  224. bool isEnabled,
  225. bool isTicked,
  226. std::unique_ptr<Drawable> iconToUse);
  227. /** Adds an item that represents one of the commands in a command manager object.
  228. @param commandManager the manager to use to trigger the command and get information
  229. about it
  230. @param commandID the ID of the command
  231. @param displayName if this is non-empty, then this string will be used instead of
  232. the command's registered name
  233. @param iconToUse an optional Drawable object to use as the icon to the left of the item.
  234. The menu will take ownership of this drawable object and will
  235. delete it later when no longer needed
  236. */
  237. void addCommandItem (ApplicationCommandManager* commandManager,
  238. CommandID commandID,
  239. String displayName = {},
  240. std::unique_ptr<Drawable> iconToUse = {});
  241. /** Appends a text item with a special colour.
  242. This is the same as addItem(), but specifies a colour to use for the
  243. text, which will override the default colours that are used by the
  244. current look-and-feel. See addItem() for a description of the parameters.
  245. */
  246. void addColouredItem (int itemResultID,
  247. String itemText,
  248. Colour itemTextColour,
  249. bool isEnabled = true,
  250. bool isTicked = false,
  251. const Image& iconToUse = {});
  252. /** Appends a text item with a special colour.
  253. This is the same as addItem(), but specifies a colour to use for the
  254. text, which will override the default colours that are used by the
  255. current look-and-feel. See addItem() for a description of the parameters.
  256. */
  257. void addColouredItem (int itemResultID,
  258. String itemText,
  259. Colour itemTextColour,
  260. bool isEnabled,
  261. bool isTicked,
  262. std::unique_ptr<Drawable> iconToUse);
  263. /** Appends a custom menu item.
  264. This will add a user-defined component to use as a menu item.
  265. Note that native macOS menus do not support custom components.
  266. itemTitle will be used as the fallback text for this item, and will
  267. be exposed to screen reader clients.
  268. @see CustomComponent
  269. */
  270. void addCustomItem (int itemResultID,
  271. std::unique_ptr<CustomComponent> customComponent,
  272. std::unique_ptr<const PopupMenu> optionalSubMenu = nullptr,
  273. const String& itemTitle = {});
  274. /** Appends a custom menu item that can't be used to trigger a result.
  275. This will add a user-defined component to use as a menu item.
  276. The caller must ensure that the passed-in component stays alive
  277. until after the menu has been hidden.
  278. If triggerMenuItemAutomaticallyWhenClicked is true, the menu itself will handle
  279. detection of a mouse-click on your component, and use that to trigger the
  280. menu ID specified in itemResultID. If this is false, the menu item can't
  281. be triggered, so itemResultID is not used.
  282. itemTitle will be used as the fallback text for this item, and will
  283. be exposed to screen reader clients.
  284. Note that native macOS menus do not support custom components.
  285. */
  286. void addCustomItem (int itemResultID,
  287. Component& customComponent,
  288. int idealWidth,
  289. int idealHeight,
  290. bool triggerMenuItemAutomaticallyWhenClicked,
  291. std::unique_ptr<const PopupMenu> optionalSubMenu = nullptr,
  292. const String& itemTitle = {});
  293. /** Appends a sub-menu.
  294. If the menu that's passed in is empty, it will appear as an inactive item.
  295. If the itemResultID argument is non-zero, then the sub-menu item itself can be
  296. clicked to trigger it as a command.
  297. */
  298. void addSubMenu (String subMenuName,
  299. PopupMenu subMenu,
  300. bool isEnabled = true);
  301. /** Appends a sub-menu with an icon.
  302. If the menu that's passed in is empty, it will appear as an inactive item.
  303. If the itemResultID argument is non-zero, then the sub-menu item itself can be
  304. clicked to trigger it as a command.
  305. */
  306. void addSubMenu (String subMenuName,
  307. PopupMenu subMenu,
  308. bool isEnabled,
  309. const Image& iconToUse,
  310. bool isTicked = false,
  311. int itemResultID = 0);
  312. /** Appends a sub-menu with an icon.
  313. If the menu that's passed in is empty, it will appear as an inactive item.
  314. If the itemResultID argument is non-zero, then the sub-menu item itself can be
  315. clicked to trigger it as a command.
  316. The iconToUse parameter is a Drawable object to use as the icon to the left of
  317. the item. The menu will take ownership of this drawable object and will delete it
  318. later when no longer needed
  319. */
  320. void addSubMenu (String subMenuName,
  321. PopupMenu subMenu,
  322. bool isEnabled,
  323. std::unique_ptr<Drawable> iconToUse,
  324. bool isTicked = false,
  325. int itemResultID = 0);
  326. /** Appends a separator to the menu, to help break it up into sections.
  327. The menu class is smart enough not to display separators at the top or bottom
  328. of the menu, and it will replace multiple adjacent separators with a single
  329. one, so your code can be quite free and easy about adding these, and it'll
  330. always look ok.
  331. */
  332. void addSeparator();
  333. /** Adds a non-clickable text item to the menu.
  334. This is a bold-font items which can be used as a header to separate the items
  335. into named groups.
  336. */
  337. void addSectionHeader (String title);
  338. /** Adds a column break to the menu, to help break it up into sections.
  339. Subsequent items will be placed in a new column, rather than being appended
  340. to the current column.
  341. If a menu contains explicit column breaks, the menu will never add additional
  342. breaks.
  343. */
  344. void addColumnBreak();
  345. /** Returns the number of items that the menu currently contains.
  346. (This doesn't count separators).
  347. */
  348. int getNumItems() const noexcept;
  349. /** Returns true if the menu contains a command item that triggers the given command. */
  350. bool containsCommandItem (int commandID) const;
  351. /** Returns true if the menu contains any items that can be used. */
  352. bool containsAnyActiveItems() const noexcept;
  353. //==============================================================================
  354. /** Class used to create a set of options to pass to the show() method.
  355. You can chain together a series of calls to this class's methods to create
  356. a set of whatever options you want to specify.
  357. E.g. @code
  358. PopupMenu menu;
  359. ...
  360. menu.showMenu (PopupMenu::Options().withMinimumWidth (100)
  361. .withMaximumNumColumns (3)
  362. .withTargetComponent (myComp));
  363. @endcode
  364. */
  365. class JUCE_API Options
  366. {
  367. public:
  368. /** By default, the target screen area will be the current mouse position. */
  369. Options();
  370. Options (const Options&) = default;
  371. Options& operator= (const Options&) = default;
  372. enum class PopupDirection
  373. {
  374. upwards,
  375. downwards
  376. };
  377. //==============================================================================
  378. /** Sets the target component to use when displaying the menu.
  379. This is normally the button or other control that triggered the menu.
  380. The target component is primarily used to control the scale of the menu, so
  381. it's important to supply a target component if you'll be using your program
  382. on hi-DPI displays.
  383. This function will also set the target screen area, so that the menu displays
  384. next to the target component. If you need to display the menu at a specific
  385. location, you should call withTargetScreenArea() after withTargetComponent.
  386. @see withTargetComponent, withTargetScreenArea
  387. */
  388. JUCE_NODISCARD Options withTargetComponent (Component* targetComponent) const;
  389. JUCE_NODISCARD Options withTargetComponent (Component& targetComponent) const;
  390. /** Sets the region of the screen next to which the menu should be displayed.
  391. To display the menu next to the mouse cursor use withMousePosition(),
  392. which is equivalent to passing the following to this function:
  393. @code
  394. Rectangle<int>{}.withPosition (Desktop::getMousePosition())
  395. @endcode
  396. withTargetComponent() will also set the target screen area. If you need
  397. a target component and a target screen area, make sure to call
  398. withTargetScreenArea() after withTargetComponent().
  399. @see withMousePosition
  400. */
  401. JUCE_NODISCARD Options withTargetScreenArea (Rectangle<int> targetArea) const;
  402. /** Sets the target screen area to match the current mouse position.
  403. Make sure to call this after withTargetComponent().
  404. @see withTargetScreenArea
  405. */
  406. JUCE_NODISCARD Options withMousePosition() const;
  407. /** If the passed component has been deleted when the popup menu exits,
  408. the selected item's action will not be called.
  409. This is useful for avoiding dangling references inside the action
  410. callback, in the case that the callback needs to access a component that
  411. may be deleted.
  412. */
  413. JUCE_NODISCARD Options withDeletionCheck (Component& componentToWatchForDeletion) const;
  414. /** Sets the minimum width of the popup window. */
  415. JUCE_NODISCARD Options withMinimumWidth (int minWidth) const;
  416. /** Sets the minimum number of columns in the popup window. */
  417. JUCE_NODISCARD Options withMinimumNumColumns (int minNumColumns) const;
  418. /** Sets the maximum number of columns in the popup window. */
  419. JUCE_NODISCARD Options withMaximumNumColumns (int maxNumColumns) const;
  420. /** Sets the default height of each item in the popup menu. */
  421. JUCE_NODISCARD Options withStandardItemHeight (int standardHeight) const;
  422. /** Sets an item which must be visible when the menu is initially drawn.
  423. This is useful to ensure that a particular item is shown when the menu
  424. contains too many items to display on a single screen.
  425. */
  426. JUCE_NODISCARD Options withItemThatMustBeVisible (int idOfItemToBeVisible) const;
  427. /** Sets a component that the popup menu will be drawn into.
  428. Some plugin formats, such as AUv3, dislike it when the plugin editor
  429. spawns additional windows. Some AUv3 hosts display pink backgrounds
  430. underneath transparent popup windows, which is confusing and can appear
  431. as though the plugin is malfunctioning. Setting a parent component will
  432. avoid this unwanted behaviour, but with the downside that the menu size
  433. will be constrained by the size of the parent component.
  434. */
  435. JUCE_NODISCARD Options withParentComponent (Component* parentComponent) const;
  436. /** Sets the direction of the popup menu relative to the target screen area. */
  437. JUCE_NODISCARD Options withPreferredPopupDirection (PopupDirection direction) const;
  438. /** Sets an item to select in the menu.
  439. This is useful for controls such as combo boxes, where opening the combo box
  440. with the keyboard should ideally highlight the currently-selected item, allowing
  441. the next/previous item to be selected by pressing up/down on the keyboard, rather
  442. than needing to move the highlighted row down from the top of the menu each time
  443. it is opened.
  444. */
  445. JUCE_NODISCARD Options withInitiallySelectedItem (int idOfItemToBeSelected) const;
  446. //==============================================================================
  447. /** Gets the parent component. This may be nullptr if the Component has been deleted.
  448. @see withParentComponent
  449. */
  450. Component* getParentComponent() const noexcept { return parentComponent; }
  451. /** Gets the target component. This may be nullptr if the Component has been deleted.
  452. @see withTargetComponent
  453. */
  454. Component* getTargetComponent() const noexcept { return targetComponent; }
  455. /** Returns true if the menu was watching a component, and that component has been deleted, and false otherwise.
  456. @see withDeletionCheck
  457. */
  458. bool hasWatchedComponentBeenDeleted() const noexcept { return isWatchingForDeletion && componentToWatchForDeletion == nullptr; }
  459. /** Gets the target screen area.
  460. @see withTargetScreenArea
  461. */
  462. Rectangle<int> getTargetScreenArea() const noexcept { return targetArea; }
  463. /** Gets the minimum width.
  464. @see withMinimumWidth
  465. */
  466. int getMinimumWidth() const noexcept { return minWidth; }
  467. /** Gets the maximum number of columns.
  468. @see withMaximumNumColumns
  469. */
  470. int getMaximumNumColumns() const noexcept { return maxColumns; }
  471. /** Gets the minimum number of columns.
  472. @see withMinimumNumColumns
  473. */
  474. int getMinimumNumColumns() const noexcept { return minColumns; }
  475. /** Gets the default height of items in the menu.
  476. @see withStandardItemHeight
  477. */
  478. int getStandardItemHeight() const noexcept { return standardHeight; }
  479. /** Gets the ID of the item that must be visible when the menu is initially shown.
  480. @see withItemThatMustBeVisible
  481. */
  482. int getItemThatMustBeVisible() const noexcept { return visibleItemID; }
  483. /** Gets the preferred popup menu direction.
  484. @see withPreferredPopupDirection
  485. */
  486. PopupDirection getPreferredPopupDirection() const noexcept { return preferredPopupDirection; }
  487. /** Gets the ID of the item that must be selected when the menu is initially shown.
  488. @see withItemThatMustBeVisible
  489. */
  490. int getInitiallySelectedItemId() const noexcept { return initiallySelectedItemId; }
  491. private:
  492. //==============================================================================
  493. Rectangle<int> targetArea;
  494. WeakReference<Component> targetComponent, parentComponent, componentToWatchForDeletion;
  495. int visibleItemID = 0, minWidth = 0, minColumns = 1, maxColumns = 0, standardHeight = 0, initiallySelectedItemId = 0;
  496. bool isWatchingForDeletion = false;
  497. PopupDirection preferredPopupDirection = PopupDirection::downwards;
  498. };
  499. //==============================================================================
  500. #if JUCE_MODAL_LOOPS_PERMITTED
  501. /** Displays the menu and waits for the user to pick something.
  502. This will display the menu modally, and return the ID of the item that the
  503. user picks. If they click somewhere off the menu to get rid of it without
  504. choosing anything, this will return 0.
  505. The current location of the mouse will be used as the position to show the
  506. menu - to explicitly set the menu's position, use showAt() instead. Depending
  507. on where this point is on the screen, the menu will appear above, below or
  508. to the side of the point.
  509. @param itemIDThatMustBeVisible if you set this to the ID of one of the menu items,
  510. then when the menu first appears, it will make sure
  511. that this item is visible. So if the menu has too many
  512. items to fit on the screen, it will be scrolled to a
  513. position where this item is visible.
  514. @param minimumWidth a minimum width for the menu, in pixels. It may be wider
  515. than this if some items are too long to fit.
  516. @param maximumNumColumns if there are too many items to fit on-screen in a single
  517. vertical column, the menu may be laid out as a series of
  518. columns - this is the maximum number allowed. To use the
  519. default value for this (probably about 7), you can pass
  520. in zero.
  521. @param standardItemHeight if this is non-zero, it will be used as the standard
  522. height for menu items (apart from custom items)
  523. @param callback if this is not a nullptr, the menu will be launched
  524. asynchronously, returning immediately, and the callback
  525. will receive a call when the menu is either dismissed or
  526. has an item selected. This object will be owned and
  527. deleted by the system, so make sure that it works safely
  528. and that any pointers that it uses are safely within scope.
  529. @see showAt
  530. */
  531. int show (int itemIDThatMustBeVisible = 0,
  532. int minimumWidth = 0,
  533. int maximumNumColumns = 0,
  534. int standardItemHeight = 0,
  535. ModalComponentManager::Callback* callback = nullptr);
  536. /** Displays the menu at a specific location.
  537. This is the same as show(), but uses a specific location (in global screen
  538. coordinates) rather than the current mouse position.
  539. The screenAreaToAttachTo parameter indicates a screen area to which the menu
  540. will be adjacent. Depending on where this is, the menu will decide which edge to
  541. attach itself to, in order to fit itself fully on-screen. If you just want to
  542. trigger a menu at a specific point, you can pass in a rectangle of size (0, 0)
  543. with the position that you want.
  544. @see show()
  545. */
  546. int showAt (Rectangle<int> screenAreaToAttachTo,
  547. int itemIDThatMustBeVisible = 0,
  548. int minimumWidth = 0,
  549. int maximumNumColumns = 0,
  550. int standardItemHeight = 0,
  551. ModalComponentManager::Callback* callback = nullptr);
  552. /** Displays the menu as if it's attached to a component such as a button.
  553. This is similar to showAt(), but will position it next to the given component, e.g.
  554. so that the menu's edge is aligned with that of the component. This is intended for
  555. things like buttons that trigger a pop-up menu.
  556. */
  557. int showAt (Component* componentToAttachTo,
  558. int itemIDThatMustBeVisible = 0,
  559. int minimumWidth = 0,
  560. int maximumNumColumns = 0,
  561. int standardItemHeight = 0,
  562. ModalComponentManager::Callback* callback = nullptr);
  563. /** Displays and runs the menu modally, with a set of options.
  564. */
  565. int showMenu (const Options& options);
  566. #endif
  567. /** Runs the menu asynchronously. */
  568. void showMenuAsync (const Options& options);
  569. /** Runs the menu asynchronously, with a user-provided callback that will receive the result. */
  570. void showMenuAsync (const Options& options,
  571. ModalComponentManager::Callback* callback);
  572. /** Runs the menu asynchronously, with a user-provided callback that will receive the result. */
  573. void showMenuAsync (const Options& options,
  574. std::function<void (int)> callback);
  575. //==============================================================================
  576. /** Closes any menus that are currently open.
  577. This might be useful if you have a situation where your window is being closed
  578. by some means other than a user action, and you'd like to make sure that menus
  579. aren't left hanging around.
  580. */
  581. static bool JUCE_CALLTYPE dismissAllActiveMenus();
  582. //==============================================================================
  583. /** Specifies a look-and-feel for the menu and any sub-menus that it has.
  584. This can be called before show() if you need a customised menu. Be careful
  585. not to delete the LookAndFeel object before the menu has been deleted.
  586. */
  587. void setLookAndFeel (LookAndFeel* newLookAndFeel);
  588. //==============================================================================
  589. /** A set of colour IDs to use to change the colour of various aspects of the menu.
  590. These constants can be used either via the LookAndFeel::setColour()
  591. method for the look and feel that is set for this menu with setLookAndFeel()
  592. @see setLookAndFeel, LookAndFeel::setColour, LookAndFeel::findColour
  593. */
  594. enum ColourIds
  595. {
  596. backgroundColourId = 0x1000700, /**< The colour to fill the menu's background with. */
  597. textColourId = 0x1000600, /**< The colour for normal menu item text, (unless the
  598. colour is specified when the item is added). */
  599. headerTextColourId = 0x1000601, /**< The colour for section header item text (see the
  600. addSectionHeader() method). */
  601. highlightedBackgroundColourId = 0x1000900, /**< The colour to fill the background of the currently
  602. highlighted menu item. */
  603. highlightedTextColourId = 0x1000800, /**< The colour to use for the text of the currently
  604. highlighted item. */
  605. };
  606. //==============================================================================
  607. /**
  608. Allows you to iterate through the items in a pop-up menu, and examine
  609. their properties.
  610. To use this, just create one and repeatedly call its next() method. When this
  611. returns true, all the member variables of the iterator are filled-out with
  612. information describing the menu item. When it returns false, the end of the
  613. list has been reached.
  614. */
  615. class JUCE_API MenuItemIterator
  616. {
  617. public:
  618. //==============================================================================
  619. /** Creates an iterator that will scan through the items in the specified
  620. menu.
  621. Be careful not to add any items to a menu while it is being iterated,
  622. or things could get out of step.
  623. @param menu the menu that needs to be scanned
  624. @param searchRecursively if true, all submenus will be recursed into to
  625. do an exhaustive search
  626. */
  627. MenuItemIterator (const PopupMenu& menu, bool searchRecursively = false);
  628. /** Destructor. */
  629. ~MenuItemIterator();
  630. /** Returns true if there is another item, and sets up all this object's
  631. member variables to reflect that item's properties.
  632. */
  633. bool next();
  634. /** Returns a reference to the description of the current item.
  635. It is only valid to call this after next() has returned true!
  636. */
  637. Item& getItem() const;
  638. private:
  639. //==============================================================================
  640. bool searchRecursively;
  641. Array<int> index;
  642. Array<const PopupMenu*> menus;
  643. PopupMenu::Item* currentItem = nullptr;
  644. MenuItemIterator& operator= (const MenuItemIterator&);
  645. JUCE_LEAK_DETECTOR (MenuItemIterator)
  646. };
  647. //==============================================================================
  648. /** A user-defined component that can be used as an item in a popup menu.
  649. @see PopupMenu::addCustomItem
  650. */
  651. class JUCE_API CustomComponent : public Component,
  652. public SingleThreadedReferenceCountedObject
  653. {
  654. public:
  655. /** Creates a custom item that is triggered automatically. */
  656. CustomComponent();
  657. /** Creates a custom item.
  658. If isTriggeredAutomatically is true, then the menu will automatically detect
  659. a mouse-click on this component and use that to invoke the menu item. If it's
  660. false, then it's up to your class to manually trigger the item when it wants to.
  661. If isTriggeredAutomatically is true, then an accessibility handler 'wrapper'
  662. will be created for the item that allows pressing, focusing, and toggling.
  663. If isTriggeredAutomatically is false, and the item has no submenu, then
  664. no accessibility wrapper will be created and your component must be
  665. independently accessible.
  666. */
  667. explicit CustomComponent (bool isTriggeredAutomatically);
  668. /** Returns a rectangle with the size that this component would like to have.
  669. Note that the size which this method returns isn't necessarily the one that
  670. the menu will give it, as the items will be stretched to have a uniform width.
  671. */
  672. virtual void getIdealSize (int& idealWidth, int& idealHeight) = 0;
  673. /** Dismisses the menu, indicating that this item has been chosen.
  674. This will cause the menu to exit from its modal state, returning
  675. this item's id as the result.
  676. */
  677. void triggerMenuItem();
  678. /** Returns true if this item should be highlighted because the mouse is over it.
  679. You can call this method in your paint() method to find out whether
  680. to draw a highlight.
  681. */
  682. bool isItemHighlighted() const noexcept { return isHighlighted; }
  683. /** Returns a pointer to the Item that holds this custom component, if this
  684. component is currently held by an Item.
  685. You can query the Item for information that you might want to use
  686. in your paint() method, such as the item's enabled and ticked states.
  687. */
  688. const PopupMenu::Item* getItem() const noexcept { return item; }
  689. /** @internal */
  690. bool isTriggeredAutomatically() const noexcept { return triggeredAutomatically; }
  691. /** @internal */
  692. void setHighlighted (bool shouldBeHighlighted);
  693. private:
  694. //==============================================================================
  695. bool isHighlighted = false, triggeredAutomatically;
  696. const PopupMenu::Item* item = nullptr;
  697. friend PopupMenu;
  698. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomComponent)
  699. };
  700. //==============================================================================
  701. /** A user-defined callback that can be used for specific items in a popup menu.
  702. @see PopupMenu::Item::customCallback
  703. */
  704. class JUCE_API CustomCallback : public SingleThreadedReferenceCountedObject
  705. {
  706. public:
  707. CustomCallback();
  708. ~CustomCallback() override;
  709. /** Callback to indicate this item has been triggered.
  710. @returns true if the itemID should be sent to the exitModalState method, or
  711. false if it should send 0, indicating no further action should be taken
  712. */
  713. virtual bool menuItemTriggered() = 0;
  714. private:
  715. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomCallback)
  716. };
  717. //==============================================================================
  718. /** This abstract base class is implemented by LookAndFeel classes to provide
  719. menu drawing functionality.
  720. */
  721. struct JUCE_API LookAndFeelMethods
  722. {
  723. virtual ~LookAndFeelMethods() = default;
  724. /** Fills the background of a popup menu component. */
  725. virtual void drawPopupMenuBackground (Graphics&, int width, int height);
  726. /** Fills the background of a popup menu component. */
  727. virtual void drawPopupMenuBackgroundWithOptions (Graphics&,
  728. int width,
  729. int height,
  730. const Options&) = 0;
  731. /** Draws one of the items in a popup menu. */
  732. virtual void drawPopupMenuItem (Graphics&, const Rectangle<int>& area,
  733. bool isSeparator, bool isActive, bool isHighlighted,
  734. bool isTicked, bool hasSubMenu,
  735. const String& text,
  736. const String& shortcutKeyText,
  737. const Drawable* icon,
  738. const Colour* textColour);
  739. /** Draws one of the items in a popup menu. */
  740. virtual void drawPopupMenuItemWithOptions (Graphics&, const Rectangle<int>& area,
  741. bool isHighlighted,
  742. const Item& item,
  743. const Options&) = 0;
  744. virtual void drawPopupMenuSectionHeader (Graphics&, const Rectangle<int>&,
  745. const String&);
  746. virtual void drawPopupMenuSectionHeaderWithOptions (Graphics&, const Rectangle<int>& area,
  747. const String& sectionName,
  748. const Options&) = 0;
  749. /** Returns the size and style of font to use in popup menus. */
  750. virtual Font getPopupMenuFont() = 0;
  751. virtual void drawPopupMenuUpDownArrow (Graphics&,
  752. int width, int height,
  753. bool isScrollUpArrow);
  754. virtual void drawPopupMenuUpDownArrowWithOptions (Graphics&,
  755. int width, int height,
  756. bool isScrollUpArrow,
  757. const Options&) = 0;
  758. /** Finds the best size for an item in a popup menu. */
  759. virtual void getIdealPopupMenuItemSize (const String& text,
  760. bool isSeparator,
  761. int standardMenuItemHeight,
  762. int& idealWidth,
  763. int& idealHeight);
  764. /** Finds the best size for an item in a popup menu. */
  765. virtual void getIdealPopupMenuItemSizeWithOptions (const String& text,
  766. bool isSeparator,
  767. int standardMenuItemHeight,
  768. int& idealWidth,
  769. int& idealHeight,
  770. const Options&) = 0;
  771. virtual int getMenuWindowFlags() = 0;
  772. virtual void drawMenuBarBackground (Graphics&, int width, int height,
  773. bool isMouseOverBar,
  774. MenuBarComponent&) = 0;
  775. virtual int getDefaultMenuBarHeight() = 0;
  776. virtual int getMenuBarItemWidth (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
  777. virtual Font getMenuBarFont (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
  778. virtual void drawMenuBarItem (Graphics&, int width, int height,
  779. int itemIndex,
  780. const String& itemText,
  781. bool isMouseOverItem,
  782. bool isMenuOpen,
  783. bool isMouseOverBar,
  784. MenuBarComponent&) = 0;
  785. virtual Component* getParentComponentForMenuOptions (const Options& options) = 0;
  786. virtual void preparePopupMenuWindow (Component& newWindow) = 0;
  787. /** Return true if you want your popup menus to scale with the target component's AffineTransform
  788. or scale factor
  789. */
  790. virtual bool shouldPopupMenuScaleWithTargetComponent (const Options& options) = 0;
  791. virtual int getPopupMenuBorderSize();
  792. virtual int getPopupMenuBorderSizeWithOptions (const Options&) = 0;
  793. /** Implement this to draw some custom decoration between the columns of the popup menu.
  794. `getPopupMenuColumnSeparatorWidthWithOptions` must return a positive value in order
  795. to display the separator.
  796. */
  797. virtual void drawPopupMenuColumnSeparatorWithOptions (Graphics& g,
  798. const Rectangle<int>& bounds,
  799. const Options&) = 0;
  800. /** Return the amount of space that should be left between popup menu columns. */
  801. virtual int getPopupMenuColumnSeparatorWidthWithOptions (const Options&) = 0;
  802. };
  803. //==============================================================================
  804. #ifndef DOXYGEN
  805. [[deprecated ("Use the new method.")]]
  806. int drawPopupMenuItem (Graphics&, int, int, bool, bool, bool, bool, bool, const String&, const String&, Image*, const Colour*) { return 0; }
  807. #endif
  808. private:
  809. //==============================================================================
  810. JUCE_PUBLIC_IN_DLL_BUILD (struct HelperClasses)
  811. class Window;
  812. friend struct HelperClasses;
  813. friend class MenuBarComponent;
  814. Array<Item> items;
  815. WeakReference<LookAndFeel> lookAndFeel;
  816. Component* createWindow (const Options&, ApplicationCommandManager**) const;
  817. int showWithOptionalCallback (const Options&, ModalComponentManager::Callback*, bool);
  818. static void setItem (CustomComponent&, const Item*);
  819. JUCE_LEAK_DETECTOR (PopupMenu)
  820. };
  821. } // namespace juce