Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_PopupMenu.h 33KB

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