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.

155 lines
8.0KB

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