The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

823 lines
37KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /** Creates and displays a popup-menu.
  23. To show a popup-menu, you create one of these, add some items to it, then
  24. call its show() method, which returns the id of the item the user selects.
  25. E.g. @code
  26. void MyWidget::mouseDown (const MouseEvent& e)
  27. {
  28. PopupMenu m;
  29. m.addItem (1, "item 1");
  30. m.addItem (2, "item 2");
  31. const int result = m.show();
  32. if (result == 0)
  33. {
  34. // user dismissed the menu without picking anything
  35. }
  36. else if (result == 1)
  37. {
  38. // user picked item 1
  39. }
  40. else if (result == 2)
  41. {
  42. // user picked item 2
  43. }
  44. }
  45. @endcode
  46. Submenus are easy too: @code
  47. void MyWidget::mouseDown (const MouseEvent& e)
  48. {
  49. PopupMenu subMenu;
  50. subMenu.addItem (1, "item 1");
  51. subMenu.addItem (2, "item 2");
  52. PopupMenu mainMenu;
  53. mainMenu.addItem (3, "item 3");
  54. mainMenu.addSubMenu ("other choices", subMenu);
  55. const int result = m.show();
  56. ...etc
  57. }
  58. @endcode
  59. @tags{GUI}
  60. */
  61. class JUCE_API PopupMenu
  62. {
  63. public:
  64. //==============================================================================
  65. /** Creates an empty popup menu. */
  66. PopupMenu();
  67. /** Creates a copy of another menu. */
  68. PopupMenu (const PopupMenu&);
  69. /** Destructor. */
  70. ~PopupMenu();
  71. /** Copies this menu from another one. */
  72. PopupMenu& operator= (const PopupMenu&);
  73. /** Move constructor */
  74. PopupMenu (PopupMenu&&) noexcept;
  75. /** Move assignment operator */
  76. PopupMenu& operator= (PopupMenu&&) noexcept;
  77. //==============================================================================
  78. class CustomComponent;
  79. class CustomCallback;
  80. //==============================================================================
  81. /** Resets the menu, removing all its items. */
  82. void clear();
  83. /** Describes a popup menu item. */
  84. struct JUCE_API Item
  85. {
  86. /** Creates a null item.
  87. You'll need to set some fields after creating an Item before you
  88. can add it to a PopupMenu
  89. */
  90. Item();
  91. /** Creates an item with the given text.
  92. This constructor also initialises the itemID to -1, which makes it suitable for
  93. creating lambda-based item actions.
  94. */
  95. Item (String text);
  96. Item (const Item&);
  97. Item& operator= (const Item&);
  98. Item (Item&&);
  99. Item& operator= (Item&&);
  100. /** The menu item's name. */
  101. String text;
  102. /** The menu item's ID.
  103. This must not be 0 if you want the item to be triggerable, but if you're attaching
  104. an action callback to the item, you can set the itemID to -1 to indicate that it
  105. isn't actively needed.
  106. */
  107. int itemID = 0;
  108. /** An optional function which should be invoked when this menu item is triggered. */
  109. std::function<void()> action;
  110. /** A sub-menu, or nullptr if there isn't one. */
  111. std::unique_ptr<PopupMenu> subMenu;
  112. /** A drawable to use as an icon, or nullptr if there isn't one. */
  113. std::unique_ptr<Drawable> image;
  114. /** A custom component for the item to display, or nullptr if there isn't one. */
  115. ReferenceCountedObjectPtr<CustomComponent> customComponent;
  116. /** A custom callback for the item to use, or nullptr if there isn't one. */
  117. ReferenceCountedObjectPtr<CustomCallback> customCallback;
  118. /** A command manager to use to automatically invoke the command, or nullptr if none is specified. */
  119. ApplicationCommandManager* commandManager = nullptr;
  120. /** An optional string describing the shortcut key for this item.
  121. This is only used for displaying at the right-hand edge of a menu item - the
  122. menu won't attempt to actually catch or process the key. If you supply a
  123. commandManager parameter then the menu will attempt to fill-in this field
  124. automatically.
  125. */
  126. String shortcutKeyDescription;
  127. /** A colour to use to draw the menu text.
  128. By default this is transparent black, which means that the LookAndFeel should choose the colour.
  129. */
  130. Colour colour;
  131. /** True if this menu item is enabled. */
  132. bool isEnabled = true;
  133. /** True if this menu item should have a tick mark next to it. */
  134. bool isTicked = false;
  135. /** True if this menu item is a separator line. */
  136. bool isSeparator = false;
  137. /** True if this menu item is a section header. */
  138. bool isSectionHeader = false;
  139. /** Sets the isTicked flag (and returns a reference to this item to allow chaining). */
  140. Item& setTicked (bool shouldBeTicked = true) JUCE_REF_QUALIFIER noexcept;
  141. /** Sets the isEnabled flag (and returns a reference to this item to allow chaining). */
  142. Item& setEnabled (bool shouldBeEnabled) JUCE_REF_QUALIFIER noexcept;
  143. /** Sets the action property (and returns a reference to this item to allow chaining). */
  144. Item& setAction (std::function<void()> action) JUCE_REF_QUALIFIER noexcept;
  145. /** Sets the itemID property (and returns a reference to this item to allow chaining). */
  146. Item& setID (int newID) JUCE_REF_QUALIFIER noexcept;
  147. /** Sets the colour property (and returns a reference to this item to allow chaining). */
  148. Item& setColour (Colour) JUCE_REF_QUALIFIER noexcept;
  149. /** Sets the customComponent property (and returns a reference to this item to allow chaining). */
  150. Item& setCustomComponent (ReferenceCountedObjectPtr<CustomComponent> customComponent) JUCE_REF_QUALIFIER noexcept;
  151. #if ! (JUCE_MSVC && _MSC_VER < 1900) // Gah.. no ref-qualifiers in VC2013...
  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. #endif
  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. The component
  265. passed in will be deleted by this menu when it's no longer needed.
  266. Note that native macOS menus do not support custom components.
  267. @see CustomComponent
  268. */
  269. void addCustomItem (int itemResultID,
  270. CustomComponent* customComponent,
  271. 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. It's the caller's responsibility to delete the component that is passed-in
  275. when it's no longer needed 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. 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 mutliple 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 noexcept;
  366. Options withTargetComponent (Component& targetComponent) const noexcept;
  367. Options withTargetScreenArea (Rectangle<int> targetArea) const noexcept;
  368. Options withMinimumWidth (int minWidth) const noexcept;
  369. Options withMinimumNumColumns (int minNumColumns) const noexcept;
  370. Options withMaximumNumColumns (int maxNumColumns) const noexcept;
  371. Options withStandardItemHeight (int standardHeight) const noexcept;
  372. Options withItemThatMustBeVisible (int idOfItemToBeVisible) const noexcept;
  373. Options withParentComponent (Component* parentComponent) const noexcept;
  374. Options withPreferredPopupDirection (PopupDirection direction) const noexcept;
  375. //==============================================================================
  376. Component* getParentComponent() const noexcept { return parentComponent; }
  377. Component* getTargetComponent() const noexcept { return targetComponent; }
  378. Rectangle<int> getTargetScreenArea() const noexcept { return targetArea; }
  379. int getMinimumWidth() const noexcept { return minWidth; }
  380. int getMaximumNumColumns() const noexcept { return maxColumns; }
  381. int getMinimumNumColumns() const noexcept { return minColumns; }
  382. int getStandardItemHeight() const noexcept { return standardHeight; }
  383. int getItemThatMustBeVisible() const noexcept { return visibleItemID; }
  384. PopupDirection getPreferredPopupDirection() const noexcept { return preferredPopupDirection; }
  385. private:
  386. //==============================================================================
  387. Rectangle<int> targetArea;
  388. Component* targetComponent = nullptr;
  389. Component* parentComponent = nullptr;
  390. int visibleItemID = 0, minWidth = 0, minColumns = 1, maxColumns = 0, standardHeight = 0;
  391. PopupDirection preferredPopupDirection = PopupDirection::downwards;
  392. };
  393. //==============================================================================
  394. #if JUCE_MODAL_LOOPS_PERMITTED
  395. /** Displays the menu and waits for the user to pick something.
  396. This will display the menu modally, and return the ID of the item that the
  397. user picks. If they click somewhere off the menu to get rid of it without
  398. choosing anything, this will return 0.
  399. The current location of the mouse will be used as the position to show the
  400. menu - to explicitly set the menu's position, use showAt() instead. Depending
  401. on where this point is on the screen, the menu will appear above, below or
  402. to the side of the point.
  403. @param itemIDThatMustBeVisible if you set this to the ID of one of the menu items,
  404. then when the menu first appears, it will make sure
  405. that this item is visible. So if the menu has too many
  406. items to fit on the screen, it will be scrolled to a
  407. position where this item is visible.
  408. @param minimumWidth a minimum width for the menu, in pixels. It may be wider
  409. than this if some items are too long to fit.
  410. @param maximumNumColumns if there are too many items to fit on-screen in a single
  411. vertical column, the menu may be laid out as a series of
  412. columns - this is the maximum number allowed. To use the
  413. default value for this (probably about 7), you can pass
  414. in zero.
  415. @param standardItemHeight if this is non-zero, it will be used as the standard
  416. height for menu items (apart from custom items)
  417. @param callback if this is not a nullptr, the menu will be launched
  418. asynchronously, returning immediately, and the callback
  419. will receive a call when the menu is either dismissed or
  420. has an item selected. This object will be owned and
  421. deleted by the system, so make sure that it works safely
  422. and that any pointers that it uses are safely within scope.
  423. @see showAt
  424. */
  425. int show (int itemIDThatMustBeVisible = 0,
  426. int minimumWidth = 0,
  427. int maximumNumColumns = 0,
  428. int standardItemHeight = 0,
  429. ModalComponentManager::Callback* callback = nullptr);
  430. /** Displays the menu at a specific location.
  431. This is the same as show(), but uses a specific location (in global screen
  432. coordinates) rather than the current mouse position.
  433. The screenAreaToAttachTo parameter indicates a screen area to which the menu
  434. will be adjacent. Depending on where this is, the menu will decide which edge to
  435. attach itself to, in order to fit itself fully on-screen. If you just want to
  436. trigger a menu at a specific point, you can pass in a rectangle of size (0, 0)
  437. with the position that you want.
  438. @see show()
  439. */
  440. int showAt (Rectangle<int> screenAreaToAttachTo,
  441. int itemIDThatMustBeVisible = 0,
  442. int minimumWidth = 0,
  443. int maximumNumColumns = 0,
  444. int standardItemHeight = 0,
  445. ModalComponentManager::Callback* callback = nullptr);
  446. /** Displays the menu as if it's attached to a component such as a button.
  447. This is similar to showAt(), but will position it next to the given component, e.g.
  448. so that the menu's edge is aligned with that of the component. This is intended for
  449. things like buttons that trigger a pop-up menu.
  450. */
  451. int showAt (Component* componentToAttachTo,
  452. int itemIDThatMustBeVisible = 0,
  453. int minimumWidth = 0,
  454. int maximumNumColumns = 0,
  455. int standardItemHeight = 0,
  456. ModalComponentManager::Callback* callback = nullptr);
  457. /** Displays and runs the menu modally, with a set of options.
  458. */
  459. int showMenu (const Options& options);
  460. #endif
  461. /** Runs the menu asynchronously. */
  462. void showMenuAsync (const Options& options);
  463. /** Runs the menu asynchronously, with a user-provided callback that will receive the result. */
  464. void showMenuAsync (const Options& options,
  465. ModalComponentManager::Callback* callback);
  466. /** Runs the menu asynchronously, with a user-provided callback that will receive the result. */
  467. void showMenuAsync (const Options& options,
  468. std::function<void(int)> callback);
  469. //==============================================================================
  470. /** Closes any menus that are currently open.
  471. This might be useful if you have a situation where your window is being closed
  472. by some means other than a user action, and you'd like to make sure that menus
  473. aren't left hanging around.
  474. */
  475. static bool JUCE_CALLTYPE dismissAllActiveMenus();
  476. //==============================================================================
  477. /** Specifies a look-and-feel for the menu and any sub-menus that it has.
  478. This can be called before show() if you need a customised menu. Be careful
  479. not to delete the LookAndFeel object before the menu has been deleted.
  480. */
  481. void setLookAndFeel (LookAndFeel* newLookAndFeel);
  482. //==============================================================================
  483. /** A set of colour IDs to use to change the colour of various aspects of the menu.
  484. These constants can be used either via the LookAndFeel::setColour()
  485. method for the look and feel that is set for this menu with setLookAndFeel()
  486. @see setLookAndFeel, LookAndFeel::setColour, LookAndFeel::findColour
  487. */
  488. enum ColourIds
  489. {
  490. backgroundColourId = 0x1000700, /**< The colour to fill the menu's background with. */
  491. textColourId = 0x1000600, /**< The colour for normal menu item text, (unless the
  492. colour is specified when the item is added). */
  493. headerTextColourId = 0x1000601, /**< The colour for section header item text (see the
  494. addSectionHeader() method). */
  495. highlightedBackgroundColourId = 0x1000900, /**< The colour to fill the background of the currently
  496. highlighted menu item. */
  497. highlightedTextColourId = 0x1000800, /**< The colour to use for the text of the currently
  498. highlighted item. */
  499. };
  500. //==============================================================================
  501. /**
  502. Allows you to iterate through the items in a pop-up menu, and examine
  503. their properties.
  504. To use this, just create one and repeatedly call its next() method. When this
  505. returns true, all the member variables of the iterator are filled-out with
  506. information describing the menu item. When it returns false, the end of the
  507. list has been reached.
  508. */
  509. class JUCE_API MenuItemIterator
  510. {
  511. public:
  512. //==============================================================================
  513. /** Creates an iterator that will scan through the items in the specified
  514. menu.
  515. Be careful not to add any items to a menu while it is being iterated,
  516. or things could get out of step.
  517. @param menu the menu that needs to be scanned
  518. @param searchRecursively if true, all submenus will be recursed into to
  519. do an exhaustive search
  520. */
  521. MenuItemIterator (const PopupMenu& menu, bool searchRecursively = false);
  522. /** Destructor. */
  523. ~MenuItemIterator();
  524. /** Returns true if there is another item, and sets up all this object's
  525. member variables to reflect that item's properties.
  526. */
  527. bool next();
  528. /** Returns a reference to the description of the current item.
  529. It is only valid to call this after next() has returned true!
  530. */
  531. Item& getItem() const;
  532. private:
  533. //==============================================================================
  534. bool searchRecursively;
  535. Array<int> index;
  536. Array<const PopupMenu*> menus;
  537. PopupMenu::Item* currentItem = nullptr;
  538. MenuItemIterator& operator= (const MenuItemIterator&);
  539. JUCE_LEAK_DETECTOR (MenuItemIterator)
  540. };
  541. //==============================================================================
  542. /** A user-defined component that can be used as an item in a popup menu.
  543. @see PopupMenu::addCustomItem
  544. */
  545. class JUCE_API CustomComponent : public Component,
  546. public SingleThreadedReferenceCountedObject
  547. {
  548. public:
  549. /** Creates a custom item.
  550. If isTriggeredAutomatically is true, then the menu will automatically detect
  551. a mouse-click on this component and use that to invoke the menu item. If it's
  552. false, then it's up to your class to manually trigger the item when it wants to.
  553. */
  554. CustomComponent (bool isTriggeredAutomatically = true);
  555. /** Destructor. */
  556. ~CustomComponent() override;
  557. /** Returns a rectangle with the size that this component would like to have.
  558. Note that the size which this method returns isn't necessarily the one that
  559. the menu will give it, as the items will be stretched to have a uniform width.
  560. */
  561. virtual void getIdealSize (int& idealWidth, int& idealHeight) = 0;
  562. /** Dismisses the menu, indicating that this item has been chosen.
  563. This will cause the menu to exit from its modal state, returning
  564. this item's id as the result.
  565. */
  566. void triggerMenuItem();
  567. /** Returns true if this item should be highlighted because the mouse is over it.
  568. You can call this method in your paint() method to find out whether
  569. to draw a highlight.
  570. */
  571. bool isItemHighlighted() const noexcept { return isHighlighted; }
  572. /** @internal */
  573. bool isTriggeredAutomatically() const noexcept { return triggeredAutomatically; }
  574. /** @internal */
  575. void setHighlighted (bool shouldBeHighlighted);
  576. private:
  577. //==============================================================================
  578. bool isHighlighted = false, triggeredAutomatically;
  579. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomComponent)
  580. };
  581. //==============================================================================
  582. /** A user-defined callback that can be used for specific items in a popup menu.
  583. @see PopupMenu::Item::customCallback
  584. */
  585. class JUCE_API CustomCallback : public SingleThreadedReferenceCountedObject
  586. {
  587. public:
  588. CustomCallback();
  589. ~CustomCallback() override;
  590. /** Callback to indicate this item has been triggered.
  591. @returns true if the itemID should be sent to the exitModalState method, or
  592. false if it should send 0, indicating no further action should be taken
  593. */
  594. virtual bool menuItemTriggered() = 0;
  595. private:
  596. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomCallback)
  597. };
  598. //==============================================================================
  599. /** This abstract base class is implemented by LookAndFeel classes to provide
  600. menu drawing functionality.
  601. */
  602. struct JUCE_API LookAndFeelMethods
  603. {
  604. virtual ~LookAndFeelMethods() = default;
  605. /** Fills the background of a popup menu component. */
  606. virtual void drawPopupMenuBackground (Graphics&, int width, int height) = 0;
  607. /** Draws one of the items in a popup menu. */
  608. virtual void drawPopupMenuItem (Graphics&, const Rectangle<int>& area,
  609. bool isSeparator, bool isActive, bool isHighlighted,
  610. bool isTicked, bool hasSubMenu,
  611. const String& text,
  612. const String& shortcutKeyText,
  613. const Drawable* icon,
  614. const Colour* textColour) = 0;
  615. virtual void drawPopupMenuSectionHeader (Graphics&, const Rectangle<int>& area,
  616. const String& sectionName) = 0;
  617. /** Returns the size and style of font to use in popup menus. */
  618. virtual Font getPopupMenuFont() = 0;
  619. virtual void drawPopupMenuUpDownArrow (Graphics&,
  620. int width, int height,
  621. bool isScrollUpArrow) = 0;
  622. /** Finds the best size for an item in a popup menu. */
  623. virtual void getIdealPopupMenuItemSize (const String& text,
  624. bool isSeparator,
  625. int standardMenuItemHeight,
  626. int& idealWidth,
  627. int& idealHeight) = 0;
  628. virtual int getMenuWindowFlags() = 0;
  629. virtual void drawMenuBarBackground (Graphics&, int width, int height,
  630. bool isMouseOverBar,
  631. MenuBarComponent&) = 0;
  632. virtual int getDefaultMenuBarHeight() = 0;
  633. virtual int getMenuBarItemWidth (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
  634. virtual Font getMenuBarFont (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
  635. virtual void drawMenuBarItem (Graphics&, int width, int height,
  636. int itemIndex,
  637. const String& itemText,
  638. bool isMouseOverItem,
  639. bool isMenuOpen,
  640. bool isMouseOverBar,
  641. MenuBarComponent&) = 0;
  642. virtual Component* getParentComponentForMenuOptions (const PopupMenu::Options& options) = 0;
  643. virtual void preparePopupMenuWindow (Component& newWindow) = 0;
  644. /** Return true if you want your popup menus to scale with the target component's AffineTransform
  645. or scale factor */
  646. virtual bool shouldPopupMenuScaleWithTargetComponent (const PopupMenu::Options& options) = 0;
  647. virtual int getPopupMenuBorderSize() = 0;
  648. };
  649. private:
  650. //==============================================================================
  651. JUCE_PUBLIC_IN_DLL_BUILD (struct HelperClasses)
  652. class Window;
  653. friend struct HelperClasses;
  654. friend class MenuBarComponent;
  655. std::vector<Item> items;
  656. WeakReference<LookAndFeel> lookAndFeel;
  657. Component* createWindow (const Options&, ApplicationCommandManager**) const;
  658. int showWithOptionalCallback (const Options&, ModalComponentManager::Callback*, bool);
  659. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  660. // These methods have new implementations now - see its new definition
  661. int drawPopupMenuItem (Graphics&, int, int, bool, bool, bool, bool, bool, const String&, const String&, Image*, const Colour*) { return 0; }
  662. #endif
  663. JUCE_LEAK_DETECTOR (PopupMenu)
  664. };
  665. } // namespace juce