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_Toolbar.h 13KB

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