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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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() { return &currentMenu; }
  207. //==============================================================================
  208. /**
  209. A class for receiving events from a ComboBox.
  210. You can register a ComboBox::Listener with a ComboBox using the ComboBox::addListener()
  211. method, and it will be called when the selected item in the box changes.
  212. @see ComboBox::addListener, ComboBox::removeListener
  213. */
  214. class JUCE_API Listener
  215. {
  216. public:
  217. /** Destructor. */
  218. virtual ~Listener() = default;
  219. /** Called when a ComboBox has its selected item changed. */
  220. virtual void comboBoxChanged (ComboBox* comboBoxThatHasChanged) = 0;
  221. };
  222. /** Registers a listener that will be called when the box's content changes. */
  223. void addListener (Listener* listener);
  224. /** Deregisters a previously-registered listener. */
  225. void removeListener (Listener* listener);
  226. //==============================================================================
  227. /** You can assign a lambda to this callback object to have it called when the selected ID is changed. */
  228. std::function<void()> onChange;
  229. //==============================================================================
  230. /** Sets a message to display when there is no item currently selected.
  231. @see getTextWhenNothingSelected
  232. */
  233. void setTextWhenNothingSelected (const String& newMessage);
  234. /** Returns the text that is shown when no item is selected.
  235. @see setTextWhenNothingSelected
  236. */
  237. String getTextWhenNothingSelected() const;
  238. /** Sets the message to show when there are no items in the list, and the user clicks
  239. on the drop-down box.
  240. By default it just says "no choices", but this lets you change it to something more
  241. meaningful.
  242. */
  243. void setTextWhenNoChoicesAvailable (const String& newMessage);
  244. /** Returns the text shown when no items have been added to the list.
  245. @see setTextWhenNoChoicesAvailable
  246. */
  247. String getTextWhenNoChoicesAvailable() const;
  248. //==============================================================================
  249. /** Gives the ComboBox a tooltip. */
  250. void setTooltip (const String& newTooltip) override;
  251. /** This can be used to allow the scroll-wheel to nudge the chosen item.
  252. By default it's disabled, and I'd recommend leaving it disabled if there's any
  253. chance that the control might be inside a scrollable list or viewport.
  254. */
  255. void setScrollWheelEnabled (bool enabled) noexcept;
  256. //==============================================================================
  257. /** A set of colour IDs to use to change the colour of various aspects of the combo box.
  258. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  259. methods.
  260. To change the colours of the menu that pops up, you can set the colour IDs in PopupMenu::ColourIDs.
  261. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  262. */
  263. enum ColourIds
  264. {
  265. backgroundColourId = 0x1000b00, /**< The background colour to fill the box with. */
  266. textColourId = 0x1000a00, /**< The colour for the text in the box. */
  267. outlineColourId = 0x1000c00, /**< The colour for an outline around the box. */
  268. buttonColourId = 0x1000d00, /**< The base colour for the button (a LookAndFeel class will probably use variations on this). */
  269. arrowColourId = 0x1000e00, /**< The colour for the arrow shape that pops up the menu */
  270. focusedOutlineColourId = 0x1000f00 /**< The colour that will be used to draw a box around the edge of the component when it has focus. */
  271. };
  272. //==============================================================================
  273. /** This abstract base class is implemented by LookAndFeel classes to provide
  274. ComboBox functionality.
  275. */
  276. struct JUCE_API LookAndFeelMethods
  277. {
  278. virtual ~LookAndFeelMethods() = default;
  279. virtual void drawComboBox (Graphics&, int width, int height, bool isButtonDown,
  280. int buttonX, int buttonY, int buttonW, int buttonH,
  281. ComboBox&) = 0;
  282. virtual Font getComboBoxFont (ComboBox&) = 0;
  283. virtual Label* createComboBoxTextBox (ComboBox&) = 0;
  284. virtual void positionComboBoxText (ComboBox&, Label& labelToPosition) = 0;
  285. virtual PopupMenu::Options getOptionsForComboBoxPopupMenu (ComboBox&, Label&) = 0;
  286. virtual void drawComboBoxTextWhenNothingSelected (Graphics&, ComboBox&, Label&) = 0;
  287. };
  288. //==============================================================================
  289. /** @internal */
  290. void enablementChanged() override;
  291. /** @internal */
  292. void colourChanged() override;
  293. /** @internal */
  294. void focusGained (Component::FocusChangeType) override;
  295. /** @internal */
  296. void focusLost (Component::FocusChangeType) override;
  297. /** @internal */
  298. void handleAsyncUpdate() override;
  299. /** @internal */
  300. String getTooltip() override { return label->getTooltip(); }
  301. /** @internal */
  302. void mouseDown (const MouseEvent&) override;
  303. /** @internal */
  304. void mouseDrag (const MouseEvent&) override;
  305. /** @internal */
  306. void mouseUp (const MouseEvent&) override;
  307. /** @internal */
  308. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  309. /** @internal */
  310. void lookAndFeelChanged() override;
  311. /** @internal */
  312. void paint (Graphics&) override;
  313. /** @internal */
  314. void resized() override;
  315. /** @internal */
  316. bool keyStateChanged (bool) override;
  317. /** @internal */
  318. bool keyPressed (const KeyPress&) override;
  319. /** @internal */
  320. void valueChanged (Value&) override;
  321. /** @internal */
  322. void parentHierarchyChanged() override;
  323. // These methods' bool parameters have changed: see their new method signatures.
  324. JUCE_DEPRECATED (void clear (bool));
  325. JUCE_DEPRECATED (void setSelectedId (int, bool));
  326. JUCE_DEPRECATED (void setSelectedItemIndex (int, bool));
  327. JUCE_DEPRECATED (void setText (const String&, bool));
  328. private:
  329. //==============================================================================
  330. enum EditableState
  331. {
  332. editableUnknown,
  333. labelIsNotEditable,
  334. labelIsEditable
  335. };
  336. PopupMenu currentMenu;
  337. Value currentId;
  338. int lastCurrentId = 0;
  339. bool isButtonDown = false, menuActive = false, scrollWheelEnabled = false;
  340. float mouseWheelAccumulator = 0;
  341. ListenerList<Listener> listeners;
  342. std::unique_ptr<Label> label;
  343. String textWhenNothingSelected, noChoicesMessage;
  344. EditableState labelEditableState = editableUnknown;
  345. PopupMenu::Item* getItemForId (int) const noexcept;
  346. PopupMenu::Item* getItemForIndex (int) const noexcept;
  347. bool selectIfEnabled (int index);
  348. bool nudgeSelectedItem (int delta);
  349. void sendChange (NotificationType);
  350. void showPopupIfNotActive();
  351. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBox)
  352. };
  353. } // namespace juce