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.

432 lines
17KB

  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. //==============================================================================
  237. /** A set of colour IDs to use to change the colour of various aspects of the combo box.
  238. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  239. methods.
  240. To change the colours of the menu that pops up
  241. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  242. */
  243. enum ColourIds
  244. {
  245. backgroundColourId = 0x1000b00, /**< The background colour to fill the box with. */
  246. textColourId = 0x1000a00, /**< The colour for the text in the box. */
  247. outlineColourId = 0x1000c00, /**< The colour for an outline around the box. */
  248. buttonColourId = 0x1000d00, /**< The base colour for the button (a LookAndFeel class will probably use variations on this). */
  249. arrowColourId = 0x1000e00, /**< The colour for the arrow shape that pops up the menu */
  250. };
  251. //==============================================================================
  252. /** This abstract base class is implemented by LookAndFeel classes to provide
  253. ComboBox functionality.
  254. */
  255. struct JUCE_API LookAndFeelMethods
  256. {
  257. virtual ~LookAndFeelMethods() {}
  258. virtual void drawComboBox (Graphics&, int width, int height, bool isButtonDown,
  259. int buttonX, int buttonY, int buttonW, int buttonH,
  260. ComboBox&) = 0;
  261. virtual Font getComboBoxFont (ComboBox&) = 0;
  262. virtual Label* createComboBoxTextBox (ComboBox&) = 0;
  263. virtual void positionComboBoxText (ComboBox&, Label& labelToPosition) = 0;
  264. };
  265. //==============================================================================
  266. /** @internal */
  267. void labelTextChanged (Label*) override;
  268. /** @internal */
  269. void enablementChanged() override;
  270. /** @internal */
  271. void colourChanged() override;
  272. /** @internal */
  273. void focusGained (Component::FocusChangeType) override;
  274. /** @internal */
  275. void focusLost (Component::FocusChangeType) override;
  276. /** @internal */
  277. void handleAsyncUpdate() override;
  278. /** @internal */
  279. String getTooltip() override { return label->getTooltip(); }
  280. /** @internal */
  281. void mouseDown (const MouseEvent&) override;
  282. /** @internal */
  283. void mouseDrag (const MouseEvent&) override;
  284. /** @internal */
  285. void mouseUp (const MouseEvent&) override;
  286. /** @internal */
  287. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  288. /** @internal */
  289. void lookAndFeelChanged() override;
  290. /** @internal */
  291. void paint (Graphics&) override;
  292. /** @internal */
  293. void resized() override;
  294. /** @internal */
  295. bool keyStateChanged (bool) override;
  296. /** @internal */
  297. bool keyPressed (const KeyPress&) override;
  298. /** @internal */
  299. void valueChanged (Value&) override;
  300. // These methods' bool parameters have changed: see their new method signatures.
  301. JUCE_DEPRECATED (void clear (bool));
  302. JUCE_DEPRECATED (void setSelectedId (int, bool));
  303. JUCE_DEPRECATED (void setSelectedItemIndex (int, bool));
  304. JUCE_DEPRECATED (void setText (const String&, bool));
  305. private:
  306. //==============================================================================
  307. struct ItemInfo
  308. {
  309. ItemInfo (const String&, int itemId, bool isEnabled, bool isHeading);
  310. bool isSeparator() const noexcept;
  311. bool isRealItem() const noexcept;
  312. String name;
  313. int itemId;
  314. bool isEnabled : 1, isHeading : 1;
  315. };
  316. OwnedArray <ItemInfo> items;
  317. Value currentId;
  318. int lastCurrentId;
  319. bool isButtonDown, separatorPending, menuActive;
  320. ListenerList <Listener> listeners;
  321. ScopedPointer<Label> label;
  322. String textWhenNothingSelected, noChoicesMessage;
  323. ItemInfo* getItemForId (int) const noexcept;
  324. ItemInfo* getItemForIndex (int) const noexcept;
  325. bool selectIfEnabled (int index);
  326. bool nudgeSelectedItem (int delta);
  327. void sendChange (NotificationType);
  328. static void popupMenuFinishedCallback (int, ComboBox*);
  329. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBox)
  330. };
  331. /** This typedef is just for compatibility with old code - newer code should use the ComboBox::Listener class directly. */
  332. typedef ComboBox::Listener ComboBoxListener;
  333. #endif // JUCE_COMBOBOX_H_INCLUDED