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.

444 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, 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, 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. This is virtual so that you can override it with your own custom popup
  195. mechanism if you need some really unusual behaviour.
  196. */
  197. virtual void showPopup();
  198. //==============================================================================
  199. /**
  200. A class for receiving events from a ComboBox.
  201. You can register a ComboBox::Listener with a ComboBox using the ComboBox::addListener()
  202. method, and it will be called when the selected item in the box changes.
  203. @see ComboBox::addListener, ComboBox::removeListener
  204. */
  205. class JUCE_API Listener
  206. {
  207. public:
  208. /** Destructor. */
  209. virtual ~Listener() {}
  210. /** Called when a ComboBox has its selected item changed. */
  211. virtual void comboBoxChanged (ComboBox* comboBoxThatHasChanged) = 0;
  212. };
  213. /** Registers a listener that will be called when the box's content changes. */
  214. void addListener (Listener* listener);
  215. /** Deregisters a previously-registered listener. */
  216. void removeListener (Listener* listener);
  217. //==============================================================================
  218. /** Sets a message to display when there is no item currently selected.
  219. @see getTextWhenNothingSelected
  220. */
  221. void setTextWhenNothingSelected (const String& newMessage);
  222. /** Returns the text that is shown when no item is selected.
  223. @see setTextWhenNothingSelected
  224. */
  225. String getTextWhenNothingSelected() const;
  226. /** Sets the message to show when there are no items in the list, and the user clicks
  227. on the drop-down box.
  228. By default it just says "no choices", but this lets you change it to something more
  229. meaningful.
  230. */
  231. void setTextWhenNoChoicesAvailable (const String& newMessage);
  232. /** Returns the text shown when no items have been added to the list.
  233. @see setTextWhenNoChoicesAvailable
  234. */
  235. String getTextWhenNoChoicesAvailable() const;
  236. //==============================================================================
  237. /** Gives the ComboBox a tooltip. */
  238. void setTooltip (const String& newTooltip) override;
  239. /** This can be used to allow the scroll-wheel to nudge the chosen item.
  240. By default it's disabled, and I'd recommend leaving it disabled if there's any
  241. chance that the control might be inside a scrollable list or viewport.
  242. */
  243. void setScrollWheelEnabled (bool enabled) noexcept;
  244. //==============================================================================
  245. /** A set of colour IDs to use to change the colour of various aspects of the combo box.
  246. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  247. methods.
  248. To change the colours of the menu that pops up
  249. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  250. */
  251. enum ColourIds
  252. {
  253. backgroundColourId = 0x1000b00, /**< The background colour to fill the box with. */
  254. textColourId = 0x1000a00, /**< The colour for the text in the box. */
  255. outlineColourId = 0x1000c00, /**< The colour for an outline around the box. */
  256. buttonColourId = 0x1000d00, /**< The base colour for the button (a LookAndFeel class will probably use variations on this). */
  257. arrowColourId = 0x1000e00, /**< The colour for the arrow shape that pops up the menu */
  258. };
  259. //==============================================================================
  260. /** This abstract base class is implemented by LookAndFeel classes to provide
  261. ComboBox functionality.
  262. */
  263. struct JUCE_API LookAndFeelMethods
  264. {
  265. virtual ~LookAndFeelMethods() {}
  266. virtual void drawComboBox (Graphics&, int width, int height, bool isButtonDown,
  267. int buttonX, int buttonY, int buttonW, int buttonH,
  268. ComboBox&) = 0;
  269. virtual Font getComboBoxFont (ComboBox&) = 0;
  270. virtual Label* createComboBoxTextBox (ComboBox&) = 0;
  271. virtual void positionComboBoxText (ComboBox&, Label& labelToPosition) = 0;
  272. };
  273. //==============================================================================
  274. /** @internal */
  275. void labelTextChanged (Label*) override;
  276. /** @internal */
  277. void enablementChanged() override;
  278. /** @internal */
  279. void colourChanged() override;
  280. /** @internal */
  281. void focusGained (Component::FocusChangeType) override;
  282. /** @internal */
  283. void focusLost (Component::FocusChangeType) override;
  284. /** @internal */
  285. void handleAsyncUpdate() override;
  286. /** @internal */
  287. String getTooltip() override { return label->getTooltip(); }
  288. /** @internal */
  289. void mouseDown (const MouseEvent&) override;
  290. /** @internal */
  291. void mouseDrag (const MouseEvent&) override;
  292. /** @internal */
  293. void mouseUp (const MouseEvent&) override;
  294. /** @internal */
  295. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  296. /** @internal */
  297. void lookAndFeelChanged() override;
  298. /** @internal */
  299. void paint (Graphics&) override;
  300. /** @internal */
  301. void resized() override;
  302. /** @internal */
  303. bool keyStateChanged (bool) override;
  304. /** @internal */
  305. bool keyPressed (const KeyPress&) override;
  306. /** @internal */
  307. void valueChanged (Value&) override;
  308. /** @internal */
  309. void parentHierarchyChanged() override;
  310. // These methods' bool parameters have changed: see their new method signatures.
  311. JUCE_DEPRECATED (void clear (bool));
  312. JUCE_DEPRECATED (void setSelectedId (int, bool));
  313. JUCE_DEPRECATED (void setSelectedItemIndex (int, bool));
  314. JUCE_DEPRECATED (void setText (const String&, bool));
  315. private:
  316. //==============================================================================
  317. struct ItemInfo
  318. {
  319. ItemInfo (const String&, int itemId, bool isEnabled, bool isHeading);
  320. bool isSeparator() const noexcept;
  321. bool isRealItem() const noexcept;
  322. String name;
  323. int itemId;
  324. bool isEnabled : 1, isHeading : 1;
  325. };
  326. OwnedArray<ItemInfo> items;
  327. Value currentId;
  328. int lastCurrentId;
  329. bool isButtonDown, separatorPending, menuActive, scrollWheelEnabled;
  330. float mouseWheelAccumulator;
  331. ListenerList <Listener> listeners;
  332. ScopedPointer<Label> label;
  333. String textWhenNothingSelected, noChoicesMessage;
  334. ItemInfo* getItemForId (int) const noexcept;
  335. ItemInfo* getItemForIndex (int) const noexcept;
  336. bool selectIfEnabled (int index);
  337. bool nudgeSelectedItem (int delta);
  338. void sendChange (NotificationType);
  339. static void popupMenuFinishedCallback (int, ComboBox*);
  340. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBox)
  341. };
  342. /** This typedef is just for compatibility with old code - newer code should use the ComboBox::Listener class directly. */
  343. typedef ComboBox::Listener ComboBoxListener;
  344. #endif // JUCE_COMBOBOX_H_INCLUDED