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.

839 lines
38KB

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