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 17KB

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