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

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