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_ComboBox.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A component that lets the user choose from a drop-down list of choices.
  23. The combo-box has a list of text strings, each with an associated id number,
  24. that will be shown in the drop-down list when the user clicks on the component.
  25. The currently selected choice is displayed in the combo-box, and this can
  26. either be read-only text, or editable.
  27. To find out when the user selects a different item or edits the text, you
  28. can assign a lambda to the onChange member, or register a ComboBox::Listener
  29. to receive callbacks.
  30. @tags{GUI}
  31. */
  32. class JUCE_API ComboBox : public Component,
  33. public SettableTooltipClient,
  34. public Value::Listener,
  35. private AsyncUpdater
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates a combo-box.
  40. On construction, the text field will be empty, so you should call the
  41. setSelectedId() or setText() method to choose the initial value before
  42. displaying it.
  43. @param componentName the name to set for the component (see Component::setName())
  44. */
  45. explicit ComboBox (const String& componentName = {});
  46. /** Destructor. */
  47. ~ComboBox() override;
  48. //==============================================================================
  49. /** Sets whether the text in the combo-box is editable.
  50. The default state for a new ComboBox is non-editable, and can only be changed
  51. by choosing from the drop-down list.
  52. */
  53. void setEditableText (bool isEditable);
  54. /** Returns true if the text is directly editable.
  55. @see setEditableText
  56. */
  57. bool isTextEditable() const noexcept;
  58. /** Sets the style of justification to be used for positioning the text.
  59. The default is Justification::centredLeft. The text is displayed using a
  60. Label component inside the ComboBox.
  61. */
  62. void setJustificationType (Justification justification);
  63. /** Returns the current justification for the text box.
  64. @see setJustificationType
  65. */
  66. Justification getJustificationType() const noexcept;
  67. //==============================================================================
  68. /** Adds an item to be shown in the drop-down list.
  69. @param newItemText the text of the item to show in the list
  70. @param newItemId an associated ID number that can be set or retrieved - see
  71. getSelectedId() and setSelectedId(). Note that this value can not
  72. be 0!
  73. @see setItemEnabled, addSeparator, addSectionHeading, getNumItems, getItemText, getItemId
  74. */
  75. void addItem (const String& newItemText, int newItemId);
  76. /** Adds an array of items to the drop-down list.
  77. The item ID of each item will be its index in the StringArray + firstItemIdOffset.
  78. */
  79. void addItemList (const StringArray& items, int firstItemIdOffset);
  80. /** Adds a separator line to the drop-down list.
  81. This is like adding a separator to a popup menu. See PopupMenu::addSeparator().
  82. */
  83. void addSeparator();
  84. /** Adds a heading to the drop-down list, so that you can group the items into
  85. different sections.
  86. The headings are indented slightly differently to set them apart from the
  87. items on the list, and obviously can't be selected. You might want to add
  88. separators between your sections too.
  89. @see addItem, addSeparator
  90. */
  91. void addSectionHeading (const String& headingName);
  92. /** This allows items in the drop-down list to be selectively disabled.
  93. When you add an item, it's enabled by default, but you can call this
  94. method to change its status.
  95. If you disable an item which is already selected, this won't change the
  96. current selection - it just stops the user choosing that item from the list.
  97. */
  98. void setItemEnabled (int itemId, bool shouldBeEnabled);
  99. /** Returns true if the given item is enabled. */
  100. bool isItemEnabled (int itemId) const noexcept;
  101. /** Changes the text for an existing item.
  102. */
  103. void changeItemText (int itemId, const String& newText);
  104. /** Removes all the items from the drop-down list.
  105. If this call causes the content to be cleared, and a change-message
  106. will be broadcast according to the notification parameter.
  107. @see addItem, getNumItems
  108. */
  109. void clear (NotificationType notification = sendNotificationAsync);
  110. /** Returns the number of items that have been added to the list.
  111. Note that this doesn't include headers or separators.
  112. */
  113. int getNumItems() const noexcept;
  114. /** Returns the text for one of the items in the list.
  115. Note that this doesn't include headers or separators.
  116. @param index the item's index from 0 to (getNumItems() - 1)
  117. */
  118. String getItemText (int index) const;
  119. /** Returns the ID for one of the items in the list.
  120. Note that this doesn't include headers or separators.
  121. @param index the item's index from 0 to (getNumItems() - 1)
  122. */
  123. int getItemId (int index) const noexcept;
  124. /** Returns the index in the list of a particular item ID.
  125. If no such ID is found, this will return -1.
  126. */
  127. int indexOfItemId (int itemId) const noexcept;
  128. //==============================================================================
  129. /** Returns the ID of the item that's currently shown in the box.
  130. If no item is selected, or if the text is editable and the user
  131. has entered something which isn't one of the items in the list, then
  132. this will return 0.
  133. @see setSelectedId, getSelectedItemIndex, getText
  134. */
  135. int getSelectedId() const noexcept;
  136. /** Returns a Value object that can be used to get or set the selected item's ID.
  137. You can call Value::referTo() on this object to make the combo box control
  138. another Value object.
  139. */
  140. Value& getSelectedIdAsValue() { return currentId; }
  141. /** Sets one of the items to be the current selection.
  142. This will set the ComboBox's text to that of the item that matches
  143. this ID.
  144. @param newItemId the new item to select
  145. @param notification determines the type of change notification that will
  146. be sent to listeners if the value changes
  147. @see getSelectedId, setSelectedItemIndex, setText
  148. */
  149. void setSelectedId (int newItemId,
  150. NotificationType notification = sendNotificationAsync);
  151. //==============================================================================
  152. /** Returns the index of the item that's currently shown in the box.
  153. If no item is selected, or if the text is editable and the user
  154. has entered something which isn't one of the items in the list, then
  155. this will return -1.
  156. @see setSelectedItemIndex, getSelectedId, getText
  157. */
  158. int getSelectedItemIndex() const;
  159. /** Sets one of the items to be the current selection.
  160. This will set the ComboBox's text to that of the item at the given
  161. index in the list.
  162. @param newItemIndex the new item to select
  163. @param notification determines the type of change notification that will
  164. be sent to listeners if the value changes
  165. @see getSelectedItemIndex, setSelectedId, setText
  166. */
  167. void setSelectedItemIndex (int newItemIndex,
  168. NotificationType notification = sendNotificationAsync);
  169. //==============================================================================
  170. /** Returns the text that is currently shown in the combo-box's text field.
  171. If the ComboBox has editable text, then this text may have been edited
  172. by the user; otherwise it will be one of the items from the list, or
  173. possibly an empty string if nothing was selected.
  174. @see setText, getSelectedId, getSelectedItemIndex
  175. */
  176. String getText() const;
  177. /** Sets the contents of the combo-box's text field.
  178. The text passed-in will be set as the current text regardless of whether
  179. it is one of the items in the list. If the current text isn't one of the
  180. items, then getSelectedId() will return 0, otherwise it will return
  181. the appropriate ID.
  182. @param newText the text to select
  183. @param notification determines the type of change notification that will
  184. be sent to listeners if the text changes
  185. @see getText
  186. */
  187. void setText (const String& newText,
  188. NotificationType notification = sendNotificationAsync);
  189. /** Programmatically opens the text editor to allow the user to edit the current item.
  190. This is the same effect as when the box is clicked-on.
  191. @see Label::showEditor();
  192. */
  193. void showEditor();
  194. /** Pops up the combo box's list.
  195. This is virtual so that you can override it with your own custom popup
  196. mechanism if you need some really unusual behaviour.
  197. */
  198. virtual void showPopup();
  199. /** Hides the combo box's popup list, if it's currently visible. */
  200. void hidePopup();
  201. /** Returns true if the popup menu is currently being shown. */
  202. bool isPopupActive() const noexcept { return menuActive; }
  203. /** Returns the PopupMenu object associated with the ComboBox.
  204. Can be useful for adding sub-menus to the ComboBox standard PopupMenu
  205. */
  206. PopupMenu* getRootMenu() noexcept { return &currentMenu; }
  207. /** Returns the PopupMenu object associated with the ComboBox. */
  208. const PopupMenu* getRootMenu() const noexcept { return &currentMenu; }
  209. //==============================================================================
  210. /**
  211. A class for receiving events from a ComboBox.
  212. You can register a ComboBox::Listener with a ComboBox using the ComboBox::addListener()
  213. method, and it will be called when the selected item in the box changes.
  214. @see ComboBox::addListener, ComboBox::removeListener
  215. */
  216. class JUCE_API Listener
  217. {
  218. public:
  219. /** Destructor. */
  220. virtual ~Listener() = default;
  221. /** Called when a ComboBox has its selected item changed. */
  222. virtual void comboBoxChanged (ComboBox* comboBoxThatHasChanged) = 0;
  223. };
  224. /** Registers a listener that will be called when the box's content changes. */
  225. void addListener (Listener* listener);
  226. /** Deregisters a previously-registered listener. */
  227. void removeListener (Listener* listener);
  228. //==============================================================================
  229. /** You can assign a lambda to this callback object to have it called when the selected ID is changed. */
  230. std::function<void()> onChange;
  231. //==============================================================================
  232. /** Sets a message to display when there is no item currently selected.
  233. @see getTextWhenNothingSelected
  234. */
  235. void setTextWhenNothingSelected (const String& newMessage);
  236. /** Returns the text that is shown when no item is selected.
  237. @see setTextWhenNothingSelected
  238. */
  239. String getTextWhenNothingSelected() const;
  240. /** Sets the message to show when there are no items in the list, and the user clicks
  241. on the drop-down box.
  242. By default it just says "no choices", but this lets you change it to something more
  243. meaningful.
  244. */
  245. void setTextWhenNoChoicesAvailable (const String& newMessage);
  246. /** Returns the text shown when no items have been added to the list.
  247. @see setTextWhenNoChoicesAvailable
  248. */
  249. String getTextWhenNoChoicesAvailable() const;
  250. //==============================================================================
  251. /** Gives the ComboBox a tooltip. */
  252. void setTooltip (const String& newTooltip) override;
  253. /** This can be used to allow the scroll-wheel to nudge the chosen item.
  254. By default it's disabled, and I'd recommend leaving it disabled if there's any
  255. chance that the control might be inside a scrollable list or viewport.
  256. */
  257. void setScrollWheelEnabled (bool enabled) noexcept;
  258. //==============================================================================
  259. /** A set of colour IDs to use to change the colour of various aspects of the combo box.
  260. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  261. methods.
  262. To change the colours of the menu that pops up, you can set the colour IDs in PopupMenu::ColourIDs.
  263. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  264. */
  265. enum ColourIds
  266. {
  267. backgroundColourId = 0x1000b00, /**< The background colour to fill the box with. */
  268. textColourId = 0x1000a00, /**< The colour for the text in the box. */
  269. outlineColourId = 0x1000c00, /**< The colour for an outline around the box. */
  270. buttonColourId = 0x1000d00, /**< The base colour for the button (a LookAndFeel class will probably use variations on this). */
  271. arrowColourId = 0x1000e00, /**< The colour for the arrow shape that pops up the menu */
  272. focusedOutlineColourId = 0x1000f00 /**< The colour that will be used to draw a box around the edge of the component when it has focus. */
  273. };
  274. //==============================================================================
  275. /** This abstract base class is implemented by LookAndFeel classes to provide
  276. ComboBox functionality.
  277. */
  278. struct JUCE_API LookAndFeelMethods
  279. {
  280. virtual ~LookAndFeelMethods() = default;
  281. virtual void drawComboBox (Graphics&, int width, int height, bool isButtonDown,
  282. int buttonX, int buttonY, int buttonW, int buttonH,
  283. ComboBox&) = 0;
  284. virtual Font getComboBoxFont (ComboBox&) = 0;
  285. virtual Label* createComboBoxTextBox (ComboBox&) = 0;
  286. virtual void positionComboBoxText (ComboBox&, Label& labelToPosition) = 0;
  287. virtual PopupMenu::Options getOptionsForComboBoxPopupMenu (ComboBox&, Label&) = 0;
  288. virtual void drawComboBoxTextWhenNothingSelected (Graphics&, ComboBox&, Label&) = 0;
  289. };
  290. //==============================================================================
  291. /** @internal */
  292. void enablementChanged() override;
  293. /** @internal */
  294. void colourChanged() override;
  295. /** @internal */
  296. void focusGained (Component::FocusChangeType) override;
  297. /** @internal */
  298. void focusLost (Component::FocusChangeType) override;
  299. /** @internal */
  300. void handleAsyncUpdate() override;
  301. /** @internal */
  302. String getTooltip() override { return label->getTooltip(); }
  303. /** @internal */
  304. void mouseDown (const MouseEvent&) override;
  305. /** @internal */
  306. void mouseDrag (const MouseEvent&) override;
  307. /** @internal */
  308. void mouseUp (const MouseEvent&) override;
  309. /** @internal */
  310. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  311. /** @internal */
  312. void lookAndFeelChanged() override;
  313. /** @internal */
  314. void paint (Graphics&) override;
  315. /** @internal */
  316. void resized() override;
  317. /** @internal */
  318. bool keyStateChanged (bool) override;
  319. /** @internal */
  320. bool keyPressed (const KeyPress&) override;
  321. /** @internal */
  322. void valueChanged (Value&) override;
  323. /** @internal */
  324. void parentHierarchyChanged() override;
  325. //==============================================================================
  326. #ifndef DOXYGEN
  327. // These methods' bool parameters have changed: see their new method signatures.
  328. [[deprecated]] void clear (bool);
  329. [[deprecated]] void setSelectedId (int, bool);
  330. [[deprecated]] void setSelectedItemIndex (int, bool);
  331. [[deprecated]] void setText (const String&, bool);
  332. #endif
  333. private:
  334. //==============================================================================
  335. enum EditableState
  336. {
  337. editableUnknown,
  338. labelIsNotEditable,
  339. labelIsEditable
  340. };
  341. PopupMenu currentMenu;
  342. Value currentId;
  343. int lastCurrentId = 0;
  344. bool isButtonDown = false, menuActive = false, scrollWheelEnabled = false;
  345. float mouseWheelAccumulator = 0;
  346. ListenerList<Listener> listeners;
  347. std::unique_ptr<Label> label;
  348. String textWhenNothingSelected, noChoicesMessage;
  349. EditableState labelEditableState = editableUnknown;
  350. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  351. PopupMenu::Item* getItemForId (int) const noexcept;
  352. PopupMenu::Item* getItemForIndex (int) const noexcept;
  353. bool selectIfEnabled (int index);
  354. bool nudgeSelectedItem (int delta);
  355. void sendChange (NotificationType);
  356. void showPopupIfNotActive();
  357. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBox)
  358. };
  359. } // namespace juce