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.4KB

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