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_ToolbarItemComponent.h 8.5KB

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