juce_DrawableButton.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_DRAWABLEBUTTON_H_INCLUDED
  18. #define JUCE_DRAWABLEBUTTON_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A button that displays a Drawable.
  22. Up to three Drawable objects can be given to this button, to represent the
  23. 'normal', 'over' and 'down' states.
  24. @see Button
  25. */
  26. class JUCE_API DrawableButton : public Button
  27. {
  28. public:
  29. //==============================================================================
  30. enum ButtonStyle
  31. {
  32. ImageFitted, /**< The button will just display the images, but will resize and centre them to fit inside it. */
  33. ImageRaw, /**< The button will just display the images in their normal size and position.
  34. This leaves it up to the caller to make sure the images are the correct size and position for the button. */
  35. ImageAboveTextLabel, /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */
  36. ImageOnButtonBackground, /**< Draws the button as a standard rounded-rectangle button with the image on top.
  37. Note that if you use this style, the colour IDs that control the button colour are
  38. TextButton::buttonColourId and TextButton::buttonOnColourId. */
  39. ImageStretched /**< Fills the button with a stretched version of the image. */
  40. };
  41. //==============================================================================
  42. /** Creates a DrawableButton.
  43. After creating one of these, use setImages() to specify the drawables to use.
  44. @param buttonName the name to give the component
  45. @param buttonStyle the layout to use
  46. @see ButtonStyle, setButtonStyle, setImages
  47. */
  48. DrawableButton (const String& buttonName,
  49. ButtonStyle buttonStyle);
  50. /** Destructor. */
  51. ~DrawableButton();
  52. //==============================================================================
  53. /** Sets up the images to draw for the various button states.
  54. The button will keep its own internal copies of these drawables.
  55. @param normalImage the thing to draw for the button's 'normal' state. An internal copy
  56. will be made of the object passed-in if it is non-zero.
  57. @param overImage the thing to draw for the button's 'over' state - if this is
  58. zero, the button's normal image will be used when the mouse is
  59. over it. An internal copy will be made of the object passed-in
  60. if it is non-zero.
  61. @param downImage the thing to draw for the button's 'down' state - if this is
  62. zero, the 'over' image will be used instead (or the normal image
  63. as a last resort). An internal copy will be made of the object
  64. passed-in if it is non-zero.
  65. @param disabledImage an image to draw when the button is disabled. If this is zero,
  66. the normal image will be drawn with a reduced opacity instead.
  67. An internal copy will be made of the object passed-in if it is
  68. non-zero.
  69. @param normalImageOn same as the normalImage, but this is used when the button's toggle
  70. state is 'on'. If this is nullptr, the normal image is used instead
  71. @param overImageOn same as the overImage, but this is used when the button's toggle
  72. state is 'on'. If this is nullptr, the normalImageOn is drawn instead
  73. @param downImageOn same as the downImage, but this is used when the button's toggle
  74. state is 'on'. If this is nullptr, the overImageOn is drawn instead
  75. @param disabledImageOn same as the disabledImage, but this is used when the button's toggle
  76. state is 'on'. If this is nullptr, the normal image will be drawn instead
  77. with a reduced opacity
  78. */
  79. void setImages (const Drawable* normalImage,
  80. const Drawable* overImage = nullptr,
  81. const Drawable* downImage = nullptr,
  82. const Drawable* disabledImage = nullptr,
  83. const Drawable* normalImageOn = nullptr,
  84. const Drawable* overImageOn = nullptr,
  85. const Drawable* downImageOn = nullptr,
  86. const Drawable* disabledImageOn = nullptr);
  87. //==============================================================================
  88. /** Changes the button's style.
  89. @see ButtonStyle
  90. */
  91. void setButtonStyle (ButtonStyle newStyle);
  92. /** Returns the current style. */
  93. ButtonStyle getStyle() const noexcept { return style; }
  94. //==============================================================================
  95. /** Gives the button an optional amount of space around the edge of the drawable.
  96. By default there's a gap of about 3 pixels.
  97. */
  98. void setEdgeIndent (int numPixelsIndent);
  99. //==============================================================================
  100. /** Returns the image that the button is currently displaying. */
  101. Drawable* getCurrentImage() const noexcept;
  102. /** Returns the image that the button will use for its normal state. */
  103. Drawable* getNormalImage() const noexcept;
  104. /** Returns the image that the button will use when the mouse is over it. */
  105. Drawable* getOverImage() const noexcept;
  106. /** Returns the image that the button will use when the mouse is held down on it. */
  107. Drawable* getDownImage() const noexcept;
  108. /** Can be overridden to specify a custom position for the image within the button. */
  109. virtual Rectangle<float> getImageBounds() const;
  110. //==============================================================================
  111. /** A set of colour IDs to use to change the colour of various aspects of the link.
  112. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  113. methods.
  114. Note that when the ImageOnButtonBackground style is used, the colour IDs that control
  115. the button colour are TextButton::buttonColourId and TextButton::buttonOnColourId.
  116. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  117. */
  118. enum ColourIds
  119. {
  120. textColourId = 0x1004010, /**< The colour to use for the button's text label. */
  121. textColourOnId = 0x1004013, /**< The colour to use for the button's text.when the button's toggle state is "on". */
  122. backgroundColourId = 0x1004011, /**< The colour used to fill the button's background (when
  123. the button is toggled 'off'). Note that if you use the
  124. ImageOnButtonBackground style, you should use TextButton::buttonColourId
  125. to change the button's colour. */
  126. backgroundOnColourId = 0x1004012, /**< The colour used to fill the button's background (when
  127. the button is toggled 'on'). Note that if you use the
  128. ImageOnButtonBackground style, you should use TextButton::buttonOnColourId
  129. to change the button's colour. */
  130. };
  131. //==============================================================================
  132. /** @internal */
  133. void paintButton (Graphics&, bool isMouseOverButton, bool isButtonDown) override;
  134. /** @internal */
  135. void buttonStateChanged() override;
  136. /** @internal */
  137. void resized() override;
  138. /** @internal */
  139. void enablementChanged() override;
  140. /** @internal */
  141. void colourChanged() override;
  142. private:
  143. //==============================================================================
  144. ButtonStyle style;
  145. ScopedPointer<Drawable> normalImage, overImage, downImage, disabledImage,
  146. normalImageOn, overImageOn, downImageOn, disabledImageOn;
  147. Drawable* currentImage;
  148. int edgeIndent;
  149. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrawableButton)
  150. };
  151. #endif // JUCE_DRAWABLEBUTTON_H_INCLUDED