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.

187 lines
9.5KB

  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 button that displays a Drawable.
  23. Up to three Drawable objects can be given to this button, to represent the
  24. 'normal', 'over' and 'down' states.
  25. @see Button
  26. */
  27. class JUCE_API DrawableButton : public Button
  28. {
  29. public:
  30. //==============================================================================
  31. enum ButtonStyle
  32. {
  33. ImageFitted, /**< The button will just display the images, but will resize and centre them to fit inside it. */
  34. ImageRaw, /**< The button will just display the images in their normal size and position.
  35. This leaves it up to the caller to make sure the images are the correct size and position for the button. */
  36. ImageAboveTextLabel, /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */
  37. ImageOnButtonBackground, /**< Draws the button as a standard rounded-rectangle button with the image on top.
  38. Note that if you use this style, the colour IDs that control the button colour are
  39. TextButton::buttonColourId and TextButton::buttonOnColourId. */
  40. ImageStretched /**< Fills the button with a stretched version of the image. */
  41. };
  42. //==============================================================================
  43. /** Creates a DrawableButton.
  44. After creating one of these, use setImages() to specify the drawables to use.
  45. @param buttonName the name to give the component
  46. @param buttonStyle the layout to use
  47. @see ButtonStyle, setButtonStyle, setImages
  48. */
  49. DrawableButton (const String& buttonName,
  50. ButtonStyle buttonStyle);
  51. /** Destructor. */
  52. ~DrawableButton();
  53. //==============================================================================
  54. /** Sets up the images to draw for the various button states.
  55. The button will keep its own internal copies of these drawables.
  56. @param normalImage the thing to draw for the button's 'normal' state. An internal copy
  57. will be made of the object passed-in if it is non-null.
  58. @param overImage the thing to draw for the button's 'over' state - if this is
  59. null, the button's normal image will be used when the mouse is
  60. over it. An internal copy will be made of the object passed-in
  61. if it is non-null.
  62. @param downImage the thing to draw for the button's 'down' state - if this is
  63. null, the 'over' image will be used instead (or the normal image
  64. as a last resort). An internal copy will be made of the object
  65. passed-in if it is non-null.
  66. @param disabledImage an image to draw when the button is disabled. If this is null,
  67. the normal image will be drawn with a reduced opacity instead.
  68. An internal copy will be made of the object passed-in if it is
  69. non-null.
  70. @param normalImageOn same as the normalImage, but this is used when the button's toggle
  71. state is 'on'. If this is nullptr, the normal image is used instead
  72. @param overImageOn same as the overImage, but this is used when the button's toggle
  73. state is 'on'. If this is nullptr, the normalImageOn is drawn instead
  74. @param downImageOn same as the downImage, but this is used when the button's toggle
  75. state is 'on'. If this is nullptr, the overImageOn is drawn instead
  76. @param disabledImageOn same as the disabledImage, but this is used when the button's toggle
  77. state is 'on'. If this is nullptr, the normal image will be drawn instead
  78. with a reduced opacity
  79. */
  80. void setImages (const Drawable* normalImage,
  81. const Drawable* overImage = nullptr,
  82. const Drawable* downImage = nullptr,
  83. const Drawable* disabledImage = nullptr,
  84. const Drawable* normalImageOn = nullptr,
  85. const Drawable* overImageOn = nullptr,
  86. const Drawable* downImageOn = nullptr,
  87. const Drawable* disabledImageOn = nullptr);
  88. //==============================================================================
  89. /** Changes the button's style.
  90. @see ButtonStyle
  91. */
  92. void setButtonStyle (ButtonStyle newStyle);
  93. /** Returns the current style. */
  94. ButtonStyle getStyle() const noexcept { return style; }
  95. //==============================================================================
  96. /** Gives the button an optional amount of space around the edge of the drawable.
  97. By default there's a gap of about 3 pixels.
  98. */
  99. void setEdgeIndent (int numPixelsIndent);
  100. //==============================================================================
  101. /** Returns the image that the button is currently displaying. */
  102. Drawable* getCurrentImage() const noexcept;
  103. /** Returns the image that the button will use for its normal state. */
  104. Drawable* getNormalImage() const noexcept;
  105. /** Returns the image that the button will use when the mouse is over it. */
  106. Drawable* getOverImage() const noexcept;
  107. /** Returns the image that the button will use when the mouse is held down on it. */
  108. Drawable* getDownImage() const noexcept;
  109. /** Can be overridden to specify a custom position for the image within the button. */
  110. virtual Rectangle<float> getImageBounds() const;
  111. //==============================================================================
  112. /** A set of colour IDs to use to change the colour of various aspects of the link.
  113. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  114. methods.
  115. Note that when the ImageOnButtonBackground style is used, the colour IDs that control
  116. the button colour are TextButton::buttonColourId and TextButton::buttonOnColourId.
  117. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  118. */
  119. enum ColourIds
  120. {
  121. textColourId = 0x1004010, /**< The colour to use for the button's text label. */
  122. textColourOnId = 0x1004013, /**< The colour to use for the button's text when the button's toggle state is "on". */
  123. backgroundColourId = 0x1004011, /**< The colour used to fill the button's background (when
  124. the button is toggled 'off'). Note that if you use the
  125. ImageOnButtonBackground style, you should use TextButton::buttonColourId
  126. to change the button's colour. */
  127. backgroundOnColourId = 0x1004012, /**< The colour used to fill the button's background (when
  128. the button is toggled 'on'). Note that if you use the
  129. ImageOnButtonBackground style, you should use TextButton::buttonOnColourId
  130. to change the button's colour. */
  131. };
  132. //==============================================================================
  133. /** @internal */
  134. void paintButton (Graphics&, bool isMouseOverButton, bool isButtonDown) override;
  135. /** @internal */
  136. void buttonStateChanged() override;
  137. /** @internal */
  138. void resized() override;
  139. /** @internal */
  140. void enablementChanged() override;
  141. /** @internal */
  142. void colourChanged() override;
  143. private:
  144. //==============================================================================
  145. ButtonStyle style;
  146. ScopedPointer<Drawable> normalImage, overImage, downImage, disabledImage,
  147. normalImageOn, overImageOn, downImageOn, disabledImageOn;
  148. Drawable* currentImage;
  149. int edgeIndent;
  150. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrawableButton)
  151. };