juce_Toolbar.h 13 KB

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