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.

315 lines
12KB

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