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.

196 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_DRAWABLEBUTTON_JUCEHEADER__
  19. #define __JUCE_DRAWABLEBUTTON_JUCEHEADER__
  20. #include "juce_Button.h"
  21. #include "../drawables/juce_Drawable.h"
  22. //==============================================================================
  23. /**
  24. A button that displays a Drawable.
  25. Up to three Drawable objects can be given to this button, to represent the
  26. 'normal', 'over' and 'down' states.
  27. @see Button
  28. */
  29. class JUCE_API DrawableButton : public Button
  30. {
  31. public:
  32. //==============================================================================
  33. enum ButtonStyle
  34. {
  35. ImageFitted, /**< The button will just display the images, but will resize and centre them to fit inside it. */
  36. ImageRaw, /**< The button will just display the images in their normal size and position.
  37. This leaves it up to the caller to make sure the images are the correct size and position for the button. */
  38. ImageAboveTextLabel, /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */
  39. ImageOnButtonBackground /**< Draws the button as a standard rounded-rectangle button with the image on top. */
  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 0, 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 0, 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 0, 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 0, 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. //==============================================================================
  93. /** Changes the button's background colours.
  94. The toggledOffColour is the colour to use when the button's toggle state
  95. is off, and toggledOnColour when it's on.
  96. For an ImageOnly or ImageAboveTextLabel style, the background colour is
  97. used to fill the background of the component.
  98. For an ImageOnButtonBackground style, the colour is used to draw the
  99. button's lozenge shape and exactly how the colour's used will depend
  100. on the LookAndFeel.
  101. */
  102. void setBackgroundColours (const Colour& toggledOffColour,
  103. const Colour& toggledOnColour);
  104. /** Returns the current background colour being used.
  105. @see setBackgroundColour
  106. */
  107. const Colour& getBackgroundColour() const noexcept;
  108. /** Gives the button an optional amount of space around the edge of the drawable.
  109. This will only apply to ImageFitted or ImageRaw styles, it won't affect the
  110. ones on a button background. If the button is too small for the given gap, a
  111. smaller gap will be used.
  112. By default there's a gap of about 3 pixels.
  113. */
  114. void setEdgeIndent (int numPixelsIndent);
  115. //==============================================================================
  116. /** Returns the image that the button is currently displaying. */
  117. Drawable* getCurrentImage() const noexcept;
  118. Drawable* getNormalImage() const noexcept;
  119. Drawable* getOverImage() const noexcept;
  120. Drawable* getDownImage() const noexcept;
  121. //==============================================================================
  122. /** A set of colour IDs to use to change the colour of various aspects of the link.
  123. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  124. methods.
  125. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  126. */
  127. enum ColourIds
  128. {
  129. textColourId = 0x1004010, /**< The colour to use for the URL text. */
  130. };
  131. protected:
  132. //==============================================================================
  133. /** @internal */
  134. void paintButton (Graphics& g,
  135. bool isMouseOverButton,
  136. bool isButtonDown);
  137. /** @internal */
  138. void buttonStateChanged();
  139. /** @internal */
  140. void resized();
  141. /** @internal */
  142. void enablementChanged();
  143. private:
  144. //==============================================================================
  145. ButtonStyle style;
  146. ScopedPointer <Drawable> normalImage, overImage, downImage, disabledImage;
  147. ScopedPointer <Drawable> normalImageOn, overImageOn, downImageOn, disabledImageOn;
  148. Drawable* currentImage;
  149. Colour backgroundOff, backgroundOn;
  150. int edgeIndent;
  151. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrawableButton);
  152. };
  153. #endif // __JUCE_DRAWABLEBUTTON_JUCEHEADER__