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.

163 lines
8.2KB

  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. As the title suggests, this is a button containing an image.
  24. The colour and transparency of the image can be set to vary when the
  25. button state changes.
  26. @see Button, ShapeButton, TextButton
  27. @tags{GUI}
  28. */
  29. class JUCE_API ImageButton : public Button
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an ImageButton.
  34. Use setImage() to specify the image to use. The colours and opacities that
  35. are specified here can be changed later using setImages().
  36. @param name the name to give the component
  37. */
  38. explicit ImageButton (const String& name = String());
  39. /** Destructor. */
  40. ~ImageButton();
  41. //==============================================================================
  42. /** Sets up the images to draw in various states.
  43. @param resizeButtonNowToFitThisImage if true, the button will be immediately
  44. resized to the same dimensions as the normal image
  45. @param rescaleImagesWhenButtonSizeChanges if true, the image will be rescaled to fit the
  46. button when the button's size changes
  47. @param preserveImageProportions if true then any rescaling of the image to fit
  48. the button will keep the image's x and y proportions
  49. correct - i.e. it won't distort its shape, although
  50. this might create gaps around the edges
  51. @param normalImage the image to use when the button is in its normal state.
  52. button no longer needs it.
  53. @param imageOpacityWhenNormal the opacity to use when drawing the normal image.
  54. @param overlayColourWhenNormal an overlay colour to use to fill the alpha channel of the
  55. normal image - if this colour is transparent, no overlay
  56. will be drawn. The overlay will be drawn over the top of the
  57. image, so you can basically add a solid or semi-transparent
  58. colour to the image to brighten or darken it
  59. @param overImage the image to use when the mouse is over the button. If
  60. you want to use the same image as was set in the normalImage
  61. parameter, this value can be a null image.
  62. @param imageOpacityWhenOver the opacity to use when drawing the image when the mouse
  63. is over the button
  64. @param overlayColourWhenOver an overlay colour to use to fill the alpha channel of the
  65. image when the mouse is over - if this colour is transparent,
  66. no overlay will be drawn
  67. @param downImage an image to use when the button is pressed down. If set
  68. to a null image, the 'over' image will be drawn instead (or the
  69. normal image if there isn't an 'over' image either).
  70. @param imageOpacityWhenDown the opacity to use when drawing the image when the button
  71. is pressed
  72. @param overlayColourWhenDown an overlay colour to use to fill the alpha channel of the
  73. image when the button is pressed down - if this colour is
  74. transparent, no overlay will be drawn
  75. @param hitTestAlphaThreshold if set to zero, the mouse is considered to be over the button
  76. whenever it's inside the button's bounding rectangle. If
  77. set to values higher than 0, the mouse will only be
  78. considered to be over the image when the value of the
  79. image's alpha channel at that position is greater than
  80. this level.
  81. */
  82. void setImages (bool resizeButtonNowToFitThisImage,
  83. bool rescaleImagesWhenButtonSizeChanges,
  84. bool preserveImageProportions,
  85. const Image& normalImage,
  86. float imageOpacityWhenNormal,
  87. Colour overlayColourWhenNormal,
  88. const Image& overImage,
  89. float imageOpacityWhenOver,
  90. Colour overlayColourWhenOver,
  91. const Image& downImage,
  92. float imageOpacityWhenDown,
  93. Colour overlayColourWhenDown,
  94. float hitTestAlphaThreshold = 0.0f);
  95. /** Returns the currently set 'normal' image. */
  96. Image getNormalImage() const;
  97. /** Returns the image that's drawn when the mouse is over the button.
  98. If a valid 'over' image has been set, this will return it; otherwise it'll
  99. just return the normal image.
  100. */
  101. Image getOverImage() const;
  102. /** Returns the image that's drawn when the button is held down.
  103. If a valid 'down' image has been set, this will return it; otherwise it'll
  104. return the 'over' image or normal image, depending on what's available.
  105. */
  106. Image getDownImage() const;
  107. //==============================================================================
  108. /** This abstract base class is implemented by LookAndFeel classes. */
  109. struct JUCE_API LookAndFeelMethods
  110. {
  111. virtual ~LookAndFeelMethods() {}
  112. virtual void drawImageButton (Graphics&, Image*,
  113. int imageX, int imageY, int imageW, int imageH,
  114. const Colour& overlayColour, float imageOpacity, ImageButton&) = 0;
  115. };
  116. protected:
  117. //==============================================================================
  118. /** @internal */
  119. bool hitTest (int x, int y) override;
  120. /** @internal */
  121. void paintButton (Graphics&, bool isMouseOver, bool isButtonDown) override;
  122. private:
  123. //==============================================================================
  124. bool scaleImageToFit, preserveProportions;
  125. uint8 alphaThreshold;
  126. Rectangle<int> imageBounds;
  127. Image normalImage, overImage, downImage;
  128. float normalOpacity, overOpacity, downOpacity;
  129. Colour normalOverlay, overOverlay, downOverlay;
  130. Image getCurrentImage() const;
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ImageButton)
  132. };
  133. } // namespace juce