The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

327 lines
13KB

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