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.

336 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. class ToolbarItemComponent;
  21. class ToolbarItemFactory;
  22. //==============================================================================
  23. /**
  24. A toolbar component.
  25. A toolbar contains a horizontal or vertical strip of ToolbarItemComponents,
  26. and looks after their order and layout.
  27. Items (icon buttons or other custom components) are added to a toolbar using a
  28. ToolbarItemFactory - each type of item is given a unique ID number, and a
  29. toolbar might contain more than one instance of a particular item type.
  30. Toolbars can be interactively customised, allowing the user to drag the items
  31. around, and to drag items onto or off the toolbar, using the ToolbarItemPalette
  32. component as a source of new items.
  33. @see ToolbarItemFactory, ToolbarItemComponent, ToolbarItemPalette
  34. @tags{GUI}
  35. */
  36. class JUCE_API Toolbar : public Component,
  37. public DragAndDropContainer,
  38. public DragAndDropTarget
  39. {
  40. public:
  41. //==============================================================================
  42. /** Creates an empty toolbar component.
  43. To add some icons or other components to your toolbar, you'll need to
  44. create a ToolbarItemFactory class that can create a suitable set of
  45. ToolbarItemComponents.
  46. @see ToolbarItemFactory, ToolbarItemComponents
  47. */
  48. Toolbar();
  49. /** Destructor.
  50. Any items on the bar will be deleted when the toolbar is deleted.
  51. */
  52. ~Toolbar() override;
  53. //==============================================================================
  54. /** Changes the bar's orientation.
  55. @see isVertical
  56. */
  57. void setVertical (bool shouldBeVertical);
  58. /** Returns true if the bar is set to be vertical, or false if it's horizontal.
  59. You can change the bar's orientation with setVertical().
  60. */
  61. bool isVertical() const noexcept { return vertical; }
  62. /** Returns the depth of the bar.
  63. If the bar is horizontal, this will return its height; if it's vertical, it
  64. will return its width.
  65. @see getLength
  66. */
  67. int getThickness() const noexcept;
  68. /** Returns the length of the bar.
  69. If the bar is horizontal, this will return its width; if it's vertical, it
  70. will return its height.
  71. @see getThickness
  72. */
  73. int getLength() const noexcept;
  74. //==============================================================================
  75. /** Deletes all items from the bar.
  76. */
  77. void clear();
  78. /** Adds an item to the toolbar.
  79. The factory's ToolbarItemFactory::createItem() will be called by this method
  80. to create the component that will actually be added to the bar.
  81. The new item will be inserted at the specified index (if the index is -1, it
  82. will be added to the right-hand or bottom end of the bar).
  83. Once added, the component will be automatically deleted by this object when it
  84. is no longer needed.
  85. @see ToolbarItemFactory
  86. */
  87. void addItem (ToolbarItemFactory& factory,
  88. int itemId,
  89. int insertIndex = -1);
  90. /** Deletes one of the items from the bar. */
  91. void removeToolbarItem (int itemIndex);
  92. /** Removes an item from the bar and returns it. */
  93. ToolbarItemComponent* removeAndReturnItem (int itemIndex);
  94. /** Returns the number of items currently on the toolbar.
  95. @see getItemId, getItemComponent
  96. */
  97. int getNumItems() const noexcept;
  98. /** Returns the ID of the item with the given index.
  99. If the index is less than zero or greater than the number of items,
  100. this will return nullptr.
  101. @see getNumItems
  102. */
  103. int getItemId (int itemIndex) const noexcept;
  104. /** Returns the component being used for the item with the given index.
  105. If the index is less than zero or greater than the number of items,
  106. this will return nullptr.
  107. @see getNumItems
  108. */
  109. ToolbarItemComponent* getItemComponent (int itemIndex) const noexcept;
  110. /** Clears this toolbar and adds to it the default set of items that the specified
  111. factory creates.
  112. @see ToolbarItemFactory::getDefaultItemSet
  113. */
  114. void addDefaultItems (ToolbarItemFactory& factoryToUse);
  115. //==============================================================================
  116. /** Options for the way items should be displayed.
  117. @see setStyle, getStyle
  118. */
  119. enum ToolbarItemStyle
  120. {
  121. iconsOnly, /**< Means that the toolbar should just contain icons. */
  122. iconsWithText, /**< Means that the toolbar should have text labels under each icon. */
  123. textOnly /**< Means that the toolbar only display text labels for each item. */
  124. };
  125. /** Returns the toolbar's current style.
  126. @see ToolbarItemStyle, setStyle
  127. */
  128. ToolbarItemStyle getStyle() const noexcept { return toolbarStyle; }
  129. /** Changes the toolbar's current style.
  130. @see ToolbarItemStyle, getStyle, ToolbarItemComponent::setStyle
  131. */
  132. void setStyle (const ToolbarItemStyle& newStyle);
  133. //==============================================================================
  134. /** Flags used by the showCustomisationDialog() method. */
  135. enum CustomisationFlags
  136. {
  137. allowIconsOnlyChoice = 1, /**< If this flag is specified, the customisation dialog can
  138. show the "icons only" option on its choice of toolbar styles. */
  139. allowIconsWithTextChoice = 2, /**< If this flag is specified, the customisation dialog can
  140. show the "icons with text" option on its choice of toolbar styles. */
  141. allowTextOnlyChoice = 4, /**< If this flag is specified, the customisation dialog can
  142. show the "text only" option on its choice of toolbar styles. */
  143. showResetToDefaultsButton = 8, /**< If this flag is specified, the customisation dialog can
  144. show a button to reset the toolbar to its default set of items. */
  145. allCustomisationOptionsEnabled = (allowIconsOnlyChoice | allowIconsWithTextChoice | allowTextOnlyChoice | showResetToDefaultsButton)
  146. };
  147. /** Pops up a modal dialog box that allows this toolbar to be customised by the user.
  148. The dialog contains a ToolbarItemPalette and various controls for editing other
  149. aspects of the toolbar. The dialog box will be opened modally, but the method will
  150. return immediately.
  151. The factory is used to determine the set of items that will be shown on the
  152. palette.
  153. The optionFlags parameter is a bitwise-or of values from the CustomisationFlags
  154. enum.
  155. @see ToolbarItemPalette
  156. */
  157. void showCustomisationDialog (ToolbarItemFactory& factory,
  158. int optionFlags = allCustomisationOptionsEnabled);
  159. /** Turns on or off the toolbar's editing mode, in which its items can be
  160. rearranged by the user.
  161. (In most cases it's easier just to use showCustomisationDialog() instead of
  162. trying to enable editing directly).
  163. @see ToolbarItemPalette
  164. */
  165. void setEditingActive (bool editingEnabled);
  166. //==============================================================================
  167. /** A set of colour IDs to use to change the colour of various aspects of the toolbar.
  168. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  169. methods.
  170. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  171. */
  172. enum ColourIds
  173. {
  174. backgroundColourId = 0x1003200, /**< A colour to use to fill the toolbar's background. For
  175. more control over this, override LookAndFeel::paintToolbarBackground(). */
  176. separatorColourId = 0x1003210, /**< A colour to use to draw the separator lines. */
  177. buttonMouseOverBackgroundColourId = 0x1003220, /**< A colour used to paint the background of buttons when the mouse is
  178. over them. */
  179. buttonMouseDownBackgroundColourId = 0x1003230, /**< A colour used to paint the background of buttons when the mouse is
  180. held down on them. */
  181. labelTextColourId = 0x1003240, /**< A colour to use for drawing the text under buttons
  182. when the style is set to iconsWithText or textOnly. */
  183. editingModeOutlineColourId = 0x1003250 /**< A colour to use for an outline around buttons when
  184. the customisation dialog is active and the mouse moves over them. */
  185. };
  186. //==============================================================================
  187. /** Returns a string that represents the toolbar's current set of items.
  188. This lets you later restore the same item layout using restoreFromString().
  189. @see restoreFromString
  190. */
  191. String toString() const;
  192. /** Restores a set of items that was previously stored in a string by the toString()
  193. method.
  194. The factory object is used to create any item components that are needed.
  195. @see toString
  196. */
  197. bool restoreFromString (ToolbarItemFactory& factoryToUse,
  198. const String& savedVersion);
  199. //==============================================================================
  200. /** This abstract base class is implemented by LookAndFeel classes. */
  201. struct JUCE_API LookAndFeelMethods
  202. {
  203. virtual ~LookAndFeelMethods() = default;
  204. virtual void paintToolbarBackground (Graphics&, int width, int height, Toolbar&) = 0;
  205. virtual Button* createToolbarMissingItemsButton (Toolbar&) = 0;
  206. virtual void paintToolbarButtonBackground (Graphics&, int width, int height,
  207. bool isMouseOver, bool isMouseDown,
  208. ToolbarItemComponent&) = 0;
  209. virtual void paintToolbarButtonLabel (Graphics&, int x, int y, int width, int height,
  210. const String& text, ToolbarItemComponent&) = 0;
  211. };
  212. //==============================================================================
  213. /** @internal */
  214. void paint (Graphics&) override;
  215. /** @internal */
  216. void resized() override;
  217. /** @internal */
  218. void mouseDown (const MouseEvent&) override;
  219. /** @internal */
  220. bool isInterestedInDragSource (const SourceDetails&) override;
  221. /** @internal */
  222. void itemDragMove (const SourceDetails&) override;
  223. /** @internal */
  224. void itemDragExit (const SourceDetails&) override;
  225. /** @internal */
  226. void itemDropped (const SourceDetails&) override;
  227. /** @internal */
  228. void lookAndFeelChanged() override;
  229. /** @internal */
  230. void updateAllItemPositions (bool animate);
  231. /** @internal */
  232. static ToolbarItemComponent* createItem (ToolbarItemFactory&, int itemId);
  233. /** @internal */
  234. static const char* const toolbarDragDescriptor;
  235. private:
  236. //==============================================================================
  237. std::unique_ptr<Button> missingItemsButton;
  238. bool vertical = false, isEditingActive = false;
  239. ToolbarItemStyle toolbarStyle = iconsOnly;
  240. class MissingItemsComponent;
  241. friend class MissingItemsComponent;
  242. OwnedArray<ToolbarItemComponent> items;
  243. class Spacer;
  244. class CustomisationDialog;
  245. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  246. void initMissingItemButton();
  247. void showMissingItems();
  248. void addItemInternal (ToolbarItemFactory& factory, int itemId, int insertIndex);
  249. ToolbarItemComponent* getNextActiveComponent (int index, int delta) const;
  250. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Toolbar)
  251. };
  252. } // namespace juce