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.

212 lines
8.9KB

  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_TOOLBARITEMCOMPONENT_JUCEHEADER__
  19. #define __JUCE_TOOLBARITEMCOMPONENT_JUCEHEADER__
  20. #include "../buttons/juce_Button.h"
  21. #include "../drawables/juce_Drawable.h"
  22. #include "juce_Toolbar.h"
  23. class ItemDragAndDropOverlayComponent;
  24. //==============================================================================
  25. /**
  26. A component that can be used as one of the items in a Toolbar.
  27. Each of the items on a toolbar must be a component derived from ToolbarItemComponent,
  28. and these objects are always created by a ToolbarItemFactory - see the ToolbarItemFactory
  29. class for further info about creating them.
  30. The ToolbarItemComponent class is actually a button, but can be used to hold non-button
  31. components too. To do this, set the value of isBeingUsedAsAButton to false when
  32. calling the constructor, and override contentAreaChanged(), in which you can position
  33. any sub-components you need to add.
  34. To add basic buttons without writing a special subclass, have a look at the
  35. ToolbarButton class.
  36. @see ToolbarButton, Toolbar, ToolbarItemFactory
  37. */
  38. class JUCE_API ToolbarItemComponent : public Button
  39. {
  40. public:
  41. //==============================================================================
  42. /** Constructor.
  43. @param itemId the ID of the type of toolbar item which this represents
  44. @param labelText the text to display if the toolbar's style is set to
  45. Toolbar::iconsWithText or Toolbar::textOnly
  46. @param isBeingUsedAsAButton set this to false if you don't want the button
  47. to draw itself with button over/down states when the mouse
  48. moves over it or clicks
  49. */
  50. ToolbarItemComponent (int itemId,
  51. const String& labelText,
  52. bool isBeingUsedAsAButton);
  53. /** Destructor. */
  54. ~ToolbarItemComponent();
  55. //==============================================================================
  56. /** Returns the item type ID that this component represents.
  57. This value is in the constructor.
  58. */
  59. int getItemId() const noexcept { return itemId; }
  60. /** Returns the toolbar that contains this component, or 0 if it's not currently
  61. inside one.
  62. */
  63. Toolbar* getToolbar() const;
  64. /** Returns true if this component is currently inside a toolbar which is vertical.
  65. @see Toolbar::isVertical
  66. */
  67. bool isToolbarVertical() const;
  68. /** Returns the current style setting of this item.
  69. Styles are listed in the Toolbar::ToolbarItemStyle enum.
  70. @see setStyle, Toolbar::getStyle
  71. */
  72. Toolbar::ToolbarItemStyle getStyle() const noexcept { return toolbarStyle; }
  73. /** Changes the current style setting of this item.
  74. Styles are listed in the Toolbar::ToolbarItemStyle enum, and are automatically updated
  75. by the toolbar that holds this item.
  76. @see setStyle, Toolbar::setStyle
  77. */
  78. virtual void setStyle (const Toolbar::ToolbarItemStyle& newStyle);
  79. /** Returns the area of the component that should be used to display the button image or
  80. other contents of the item.
  81. This content area may change when the item's style changes, and may leave a space around the
  82. edge of the component where the text label can be shown.
  83. @see contentAreaChanged
  84. */
  85. const Rectangle<int>& getContentArea() const noexcept { return contentArea; }
  86. //==============================================================================
  87. /** This method must return the size criteria for this item, based on a given toolbar
  88. size and orientation.
  89. The preferredSize, minSize and maxSize values must all be set by your implementation
  90. method. If the toolbar is horizontal, these will be the width of the item; for a vertical
  91. toolbar, they refer to the item's height.
  92. The preferredSize is the size that the component would like to be, and this must be
  93. between the min and max sizes. For a fixed-size item, simply set all three variables to
  94. the same value.
  95. The toolbarThickness parameter tells you the depth of the toolbar - the same as calling
  96. Toolbar::getThickness().
  97. The isToolbarVertical parameter tells you whether the bar is oriented horizontally or
  98. vertically.
  99. */
  100. virtual bool getToolbarItemSizes (int toolbarThickness,
  101. bool isToolbarVertical,
  102. int& preferredSize,
  103. int& minSize,
  104. int& maxSize) = 0;
  105. /** Your subclass should use this method to draw its content area.
  106. The graphics object that is passed-in will have been clipped and had its origin
  107. moved to fit the content area as specified get getContentArea(). The width and height
  108. parameters are the width and height of the content area.
  109. If the component you're writing isn't a button, you can just do nothing in this method.
  110. */
  111. virtual void paintButtonArea (Graphics& g,
  112. int width, int height,
  113. bool isMouseOver, bool isMouseDown) = 0;
  114. /** Callback to indicate that the content area of this item has changed.
  115. This might be because the component was resized, or because the style changed and
  116. the space needed for the text label is different.
  117. See getContentArea() for a description of what the area is.
  118. */
  119. virtual void contentAreaChanged (const Rectangle<int>& newBounds) = 0;
  120. //==============================================================================
  121. /** Editing modes.
  122. These are used by setEditingMode(), but will be rarely needed in user code.
  123. */
  124. enum ToolbarEditingMode
  125. {
  126. normalMode = 0, /**< Means that the component is active, inside a toolbar. */
  127. editableOnToolbar, /**< Means that the component is on a toolbar, but the toolbar is in
  128. customisation mode, and the items can be dragged around. */
  129. editableOnPalette /**< Means that the component is on an new-item palette, so it can be
  130. dragged onto a toolbar to add it to that bar.*/
  131. };
  132. /** Changes the editing mode of this component.
  133. This is used by the ToolbarItemPalette and related classes for making the items draggable,
  134. and is unlikely to be of much use in end-user-code.
  135. */
  136. void setEditingMode (const ToolbarEditingMode newMode);
  137. /** Returns the current editing mode of this component.
  138. This is used by the ToolbarItemPalette and related classes for making the items draggable,
  139. and is unlikely to be of much use in end-user-code.
  140. */
  141. ToolbarEditingMode getEditingMode() const noexcept { return mode; }
  142. //==============================================================================
  143. /** @internal */
  144. void paintButton (Graphics& g, bool isMouseOver, bool isMouseDown);
  145. /** @internal */
  146. void resized();
  147. private:
  148. friend class Toolbar;
  149. class ItemDragAndDropOverlayComponent;
  150. friend class ItemDragAndDropOverlayComponent;
  151. const int itemId;
  152. ToolbarEditingMode mode;
  153. Toolbar::ToolbarItemStyle toolbarStyle;
  154. ScopedPointer <Component> overlayComp;
  155. int dragOffsetX, dragOffsetY;
  156. bool isActive, isBeingDragged, isBeingUsedAsAButton;
  157. Rectangle<int> contentArea;
  158. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ToolbarItemComponent);
  159. };
  160. #endif // __JUCE_TOOLBARITEMCOMPONENT_JUCEHEADER__