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.

499 lines
21KB

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