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.

630 lines
27KB

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