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.

510 lines
22KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_POPUPMENU_JUCEHEADER__
  19. #define __JUCE_POPUPMENU_JUCEHEADER__
  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. */
  59. class JUCE_API PopupMenu
  60. {
  61. private:
  62. class Window;
  63. public:
  64. //==============================================================================
  65. /** Creates an empty popup menu. */
  66. PopupMenu();
  67. /** Creates a copy of another menu. */
  68. PopupMenu (const PopupMenu& other);
  69. /** Destructor. */
  70. ~PopupMenu();
  71. /** Copies this menu from another one. */
  72. PopupMenu& operator= (const PopupMenu& other);
  73. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  74. PopupMenu (PopupMenu&& other) noexcept;
  75. PopupMenu& operator= (PopupMenu&& other) noexcept;
  76. #endif
  77. //==============================================================================
  78. /** Resets the menu, removing all its items. */
  79. void clear();
  80. /** Appends a new text item for this menu to show.
  81. @param itemResultID the number that will be returned from the show() method
  82. if the user picks this item. The value should never be
  83. zero, because that's used to indicate that the user didn't
  84. select anything.
  85. @param itemText the text to show.
  86. @param isEnabled if false, the item will be shown 'greyed-out' and can't be picked
  87. @param isTicked if true, the item will be shown with a tick next to it
  88. @param iconToUse if this is non-zero, it should be an image that will be
  89. displayed to the left of the item. This method will take its
  90. own copy of the image passed-in, so there's no need to keep
  91. it hanging around.
  92. @see addSeparator, addColouredItem, addCustomItem, addSubMenu
  93. */
  94. void addItem (int itemResultID,
  95. const String& itemText,
  96. bool isEnabled = true,
  97. bool isTicked = false,
  98. const Image& iconToUse = Image::null);
  99. /** Adds an item that represents one of the commands in a command manager object.
  100. @param commandManager the manager to use to trigger the command and get information
  101. about it
  102. @param commandID the ID of the command
  103. @param displayName if this is non-empty, then this string will be used instead of
  104. the command's registered name
  105. */
  106. void addCommandItem (ApplicationCommandManager* commandManager,
  107. int commandID,
  108. const String& displayName = String::empty);
  109. /** Appends a text item with a special colour.
  110. This is the same as addItem(), but specifies a colour to use for the
  111. text, which will override the default colours that are used by the
  112. current look-and-feel. See addItem() for a description of the parameters.
  113. */
  114. void addColouredItem (int itemResultID,
  115. const String& itemText,
  116. const Colour& itemTextColour,
  117. bool isEnabled = true,
  118. bool isTicked = false,
  119. const Image& iconToUse = Image::null);
  120. /** Appends a custom menu item that can't be used to trigger a result.
  121. This will add a user-defined component to use as a menu item. Unlike the
  122. addCustomItem() method that takes a PopupMenu::CustomComponent, this version
  123. can't trigger a result from it, so doesn't take a menu ID. It also doesn't
  124. delete the component when it's finished, so it's the caller's responsibility
  125. to manage the component that is passed-in.
  126. if triggerMenuItemAutomaticallyWhenClicked is true, the menu itself will handle
  127. detection of a mouse-click on your component, and use that to trigger the
  128. menu ID specified in itemResultID. If this is false, the menu item can't
  129. be triggered, so itemResultID is not used.
  130. @see CustomComponent
  131. */
  132. void addCustomItem (int itemResultID,
  133. Component* customComponent,
  134. int idealWidth, int idealHeight,
  135. bool triggerMenuItemAutomaticallyWhenClicked);
  136. /** Appends a sub-menu.
  137. If the menu that's passed in is empty, it will appear as an inactive item.
  138. If the itemResultID argument is non-zero, then the sub-menu item itself can be
  139. clicked to trigger it as a command.
  140. */
  141. void addSubMenu (const String& subMenuName,
  142. const PopupMenu& subMenu,
  143. bool isEnabled = true,
  144. const Image& iconToUse = Image::null,
  145. bool isTicked = false,
  146. int itemResultID = 0);
  147. /** Appends a separator to the menu, to help break it up into sections.
  148. The menu class is smart enough not to display separators at the top or bottom
  149. of the menu, and it will replace mutliple adjacent separators with a single
  150. one, so your code can be quite free and easy about adding these, and it'll
  151. always look ok.
  152. */
  153. void addSeparator();
  154. /** Adds a non-clickable text item to the menu.
  155. This is a bold-font items which can be used as a header to separate the items
  156. into named groups.
  157. */
  158. void addSectionHeader (const String& title);
  159. /** Returns the number of items that the menu currently contains.
  160. (This doesn't count separators).
  161. */
  162. int getNumItems() const noexcept;
  163. /** Returns true if the menu contains a command item that triggers the given command. */
  164. bool containsCommandItem (int commandID) const;
  165. /** Returns true if the menu contains any items that can be used. */
  166. bool containsAnyActiveItems() const noexcept;
  167. //==============================================================================
  168. /** Class used to create a set of options to pass to the show() method.
  169. You can chain together a series of calls to this class's methods to create
  170. a set of whatever options you want to specify.
  171. E.g. @code
  172. PopupMenu menu;
  173. ...
  174. menu.showMenu (PopupMenu::Options().withMinimumWidth (100)
  175. .withMaximumNumColumns (3)
  176. .withTargetComponent (myComp));
  177. @endcode
  178. */
  179. class JUCE_API Options
  180. {
  181. public:
  182. Options();
  183. Options withTargetComponent (Component* targetComponent) const noexcept;
  184. Options withTargetScreenArea (const Rectangle<int>& targetArea) const noexcept;
  185. Options withMinimumWidth (int minWidth) const noexcept;
  186. Options withMaximumNumColumns (int maxNumColumns) const noexcept;
  187. Options withStandardItemHeight (int standardHeight) const noexcept;
  188. Options withItemThatMustBeVisible (int idOfItemToBeVisible) const noexcept;
  189. private:
  190. friend class PopupMenu;
  191. friend class PopupMenu::Window;
  192. Rectangle<int> targetArea;
  193. Component* targetComponent;
  194. int visibleItemID, minWidth, maxColumns, standardHeight;
  195. };
  196. //==============================================================================
  197. #if JUCE_MODAL_LOOPS_PERMITTED
  198. /** Displays the menu and waits for the user to pick something.
  199. This will display the menu modally, and return the ID of the item that the
  200. user picks. If they click somewhere off the menu to get rid of it without
  201. choosing anything, this will return 0.
  202. The current location of the mouse will be used as the position to show the
  203. menu - to explicitly set the menu's position, use showAt() instead. Depending
  204. on where this point is on the screen, the menu will appear above, below or
  205. to the side of the point.
  206. @param itemIDThatMustBeVisible if you set this to the ID of one of the menu items,
  207. then when the menu first appears, it will make sure
  208. that this item is visible. So if the menu has too many
  209. items to fit on the screen, it will be scrolled to a
  210. position where this item is visible.
  211. @param minimumWidth a minimum width for the menu, in pixels. It may be wider
  212. than this if some items are too long to fit.
  213. @param maximumNumColumns if there are too many items to fit on-screen in a single
  214. vertical column, the menu may be laid out as a series of
  215. columns - this is the maximum number allowed. To use the
  216. default value for this (probably about 7), you can pass
  217. in zero.
  218. @param standardItemHeight if this is non-zero, it will be used as the standard
  219. height for menu items (apart from custom items)
  220. @param callback if this is non-zero, the menu will be launched asynchronously,
  221. returning immediately, and the callback will receive a
  222. call when the menu is either dismissed or has an item
  223. selected. This object will be owned and deleted by the
  224. system, so make sure that it works safely and that any
  225. pointers that it uses are safely within scope.
  226. @see showAt
  227. */
  228. int show (int itemIDThatMustBeVisible = 0,
  229. int minimumWidth = 0,
  230. int maximumNumColumns = 0,
  231. int standardItemHeight = 0,
  232. ModalComponentManager::Callback* callback = nullptr);
  233. /** Displays the menu at a specific location.
  234. This is the same as show(), but uses a specific location (in global screen
  235. co-ordinates) rather than the current mouse position.
  236. The screenAreaToAttachTo parameter indicates a screen area to which the menu
  237. will be adjacent. Depending on where this is, the menu will decide which edge to
  238. attach itself to, in order to fit itself fully on-screen. If you just want to
  239. trigger a menu at a specific point, you can pass in a rectangle of size (0, 0)
  240. with the position that you want.
  241. @see show()
  242. */
  243. int showAt (const Rectangle<int>& screenAreaToAttachTo,
  244. int itemIDThatMustBeVisible = 0,
  245. int minimumWidth = 0,
  246. int maximumNumColumns = 0,
  247. int standardItemHeight = 0,
  248. ModalComponentManager::Callback* callback = nullptr);
  249. /** Displays the menu as if it's attached to a component such as a button.
  250. This is similar to showAt(), but will position it next to the given component, e.g.
  251. so that the menu's edge is aligned with that of the component. This is intended for
  252. things like buttons that trigger a pop-up menu.
  253. */
  254. int showAt (Component* componentToAttachTo,
  255. int itemIDThatMustBeVisible = 0,
  256. int minimumWidth = 0,
  257. int maximumNumColumns = 0,
  258. int standardItemHeight = 0,
  259. ModalComponentManager::Callback* callback = nullptr);
  260. /** Displays and runs the menu modally, with a set of options.
  261. */
  262. int showMenu (const Options& options);
  263. #endif
  264. /** Runs the menu asynchronously, with a user-provided callback that will receive the result. */
  265. void showMenuAsync (const Options& options,
  266. ModalComponentManager::Callback* callback);
  267. //==============================================================================
  268. /** Closes any menus that are currently open.
  269. This might be useful if you have a situation where your window is being closed
  270. by some means other than a user action, and you'd like to make sure that menus
  271. aren't left hanging around.
  272. */
  273. static bool JUCE_CALLTYPE dismissAllActiveMenus();
  274. //==============================================================================
  275. /** Specifies a look-and-feel for the menu and any sub-menus that it has.
  276. This can be called before show() if you need a customised menu. Be careful
  277. not to delete the LookAndFeel object before the menu has been deleted.
  278. */
  279. void setLookAndFeel (LookAndFeel* newLookAndFeel);
  280. //==============================================================================
  281. /** A set of colour IDs to use to change the colour of various aspects of the menu.
  282. These constants can be used either via the LookAndFeel::setColour()
  283. method for the look and feel that is set for this menu with setLookAndFeel()
  284. @see setLookAndFeel, LookAndFeel::setColour, LookAndFeel::findColour
  285. */
  286. enum ColourIds
  287. {
  288. backgroundColourId = 0x1000700, /**< The colour to fill the menu's background with. */
  289. textColourId = 0x1000600, /**< The colour for normal menu item text, (unless the
  290. colour is specified when the item is added). */
  291. headerTextColourId = 0x1000601, /**< The colour for section header item text (see the
  292. addSectionHeader() method). */
  293. highlightedBackgroundColourId = 0x1000900, /**< The colour to fill the background of the currently
  294. highlighted menu item. */
  295. highlightedTextColourId = 0x1000800, /**< The colour to use for the text of the currently
  296. highlighted item. */
  297. };
  298. //==============================================================================
  299. /**
  300. Allows you to iterate through the items in a pop-up menu, and examine
  301. their properties.
  302. To use this, just create one and repeatedly call its next() method. When this
  303. returns true, all the member variables of the iterator are filled-out with
  304. information describing the menu item. When it returns false, the end of the
  305. list has been reached.
  306. */
  307. class JUCE_API MenuItemIterator
  308. {
  309. public:
  310. //==============================================================================
  311. /** Creates an iterator that will scan through the items in the specified
  312. menu.
  313. Be careful not to add any items to a menu while it is being iterated,
  314. or things could get out of step.
  315. */
  316. MenuItemIterator (const PopupMenu& menu);
  317. /** Destructor. */
  318. ~MenuItemIterator();
  319. /** Returns true if there is another item, and sets up all this object's
  320. member variables to reflect that item's properties.
  321. */
  322. bool next();
  323. /** Adds an item to the target menu which has all the properties of this item. */
  324. void addItemTo (PopupMenu& targetMenu);
  325. //==============================================================================
  326. String itemName;
  327. const PopupMenu* subMenu;
  328. int itemId;
  329. bool isSeparator;
  330. bool isTicked;
  331. bool isEnabled;
  332. bool isCustomComponent;
  333. bool isSectionHeader;
  334. const Colour* customColour;
  335. Image customImage;
  336. ApplicationCommandManager* commandManager;
  337. private:
  338. //==============================================================================
  339. const PopupMenu& menu;
  340. int index;
  341. MenuItemIterator& operator= (const MenuItemIterator&);
  342. JUCE_LEAK_DETECTOR (MenuItemIterator);
  343. };
  344. //==============================================================================
  345. /** A user-defined copmonent that can be used as an item in a popup menu.
  346. @see PopupMenu::addCustomItem
  347. */
  348. class JUCE_API CustomComponent : public Component,
  349. public SingleThreadedReferenceCountedObject
  350. {
  351. public:
  352. /** Creates a custom item.
  353. If isTriggeredAutomatically is true, then the menu will automatically detect
  354. a mouse-click on this component and use that to invoke the menu item. If it's
  355. false, then it's up to your class to manually trigger the item when it wants to.
  356. */
  357. CustomComponent (bool isTriggeredAutomatically = true);
  358. /** Destructor. */
  359. ~CustomComponent();
  360. /** Returns a rectangle with the size that this component would like to have.
  361. Note that the size which this method returns isn't necessarily the one that
  362. the menu will give it, as the items will be stretched to have a uniform width.
  363. */
  364. virtual void getIdealSize (int& idealWidth, int& idealHeight) = 0;
  365. /** Dismisses the menu, indicating that this item has been chosen.
  366. This will cause the menu to exit from its modal state, returning
  367. this item's id as the result.
  368. */
  369. void triggerMenuItem();
  370. /** Returns true if this item should be highlighted because the mouse is over it.
  371. You can call this method in your paint() method to find out whether
  372. to draw a highlight.
  373. */
  374. bool isItemHighlighted() const noexcept { return isHighlighted; }
  375. /** @internal */
  376. bool isTriggeredAutomatically() const noexcept { return triggeredAutomatically; }
  377. /** @internal */
  378. void setHighlighted (bool shouldBeHighlighted);
  379. private:
  380. //==============================================================================
  381. bool isHighlighted, triggeredAutomatically;
  382. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomComponent);
  383. };
  384. /** Appends a custom menu item.
  385. This will add a user-defined component to use as a menu item. The component
  386. passed in will be deleted by this menu when it's no longer needed.
  387. @see CustomComponent
  388. */
  389. void addCustomItem (int itemResultID, CustomComponent* customComponent);
  390. private:
  391. //==============================================================================
  392. class Item;
  393. class ItemComponent;
  394. class HeaderItemComponent;
  395. class NormalComponentWrapper;
  396. friend class MenuItemIterator;
  397. friend class ItemComponent;
  398. friend class Window;
  399. friend class CustomComponent;
  400. friend class MenuBarComponent;
  401. friend class OwnedArray <Item>;
  402. friend class OwnedArray <ItemComponent>;
  403. friend class ScopedPointer <Window>;
  404. OwnedArray <Item> items;
  405. LookAndFeel* lookAndFeel;
  406. Component* createWindow (const Options&, ApplicationCommandManager**) const;
  407. int showWithOptionalCallback (const Options&, ModalComponentManager::Callback*, bool);
  408. JUCE_LEAK_DETECTOR (PopupMenu);
  409. };
  410. #endif // __JUCE_POPUPMENU_JUCEHEADER__