diff --git a/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp b/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp index 23225cf81d..83e9c893f4 100644 --- a/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp +++ b/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp @@ -80,6 +80,31 @@ void DrawableButton::setEdgeIndent (const int numPixelsIndent) resized(); } +Rectangle DrawableButton::getImageBounds() const +{ + Rectangle r (getLocalBounds()); + + if (style != ImageStretched) + { + int indentX = jmin (edgeIndent, proportionOfWidth (0.3f)); + int indentY = jmin (edgeIndent, proportionOfHeight (0.3f)); + + if (style == ImageOnButtonBackground) + { + indentX = jmax (getWidth() / 4, indentX); + indentY = jmax (getHeight() / 4, indentY); + } + else if (style == ImageAboveTextLabel) + { + r = r.withTrimmedBottom (jmin (16, proportionOfHeight (0.25f))); + } + + r = r.reduced (indentX, indentY); + } + + return r.toFloat(); +} + void DrawableButton::resized() { Button::resized(); @@ -87,36 +112,11 @@ void DrawableButton::resized() if (currentImage != nullptr) { if (style == ImageRaw) - { currentImage->setOriginWithOriginalSize (Point()); - } - else if (style == ImageStretched) - { - currentImage->setTransformToFit (getLocalBounds().toFloat(), RectanglePlacement::stretchToFit); - } else - { - Rectangle imageSpace; - - const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f)); - const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f)); - - if (style == ImageOnButtonBackground) - { - imageSpace = getLocalBounds().reduced (jmax (getWidth() / 4, indentX), - jmax (getHeight() / 4, indentY)); - } - else - { - const int textH = (style == ImageAboveTextLabel) ? jmin (16, proportionOfHeight (0.25f)) : 0; - - imageSpace.setBounds (indentX, indentY, - getWidth() - indentX * 2, - getHeight() - indentY * 2 - textH); - } - - currentImage->setTransformToFit (imageSpace.toFloat(), RectanglePlacement::centred); - } + currentImage->setTransformToFit (getImageBounds(), + style == ImageStretched ? RectanglePlacement::stretchToFit + : RectanglePlacement::centred); } } @@ -152,7 +152,7 @@ void DrawableButton::buttonStateChanged() { currentImage->setInterceptsMouseClicks (false, false); addAndMakeVisible (currentImage); - DrawableButton::resized(); + resized(); } } diff --git a/modules/juce_gui_basics/buttons/juce_DrawableButton.h b/modules/juce_gui_basics/buttons/juce_DrawableButton.h index 7f9116a6d7..f07616fd11 100644 --- a/modules/juce_gui_basics/buttons/juce_DrawableButton.h +++ b/modules/juce_gui_basics/buttons/juce_DrawableButton.h @@ -132,6 +132,9 @@ public: /** Returns the image that the button will use when the mouse is held down on it. */ Drawable* getDownImage() const noexcept; + /** Can be overridden to specify a custom position for the image within the button. */ + virtual Rectangle getImageBounds() const; + //============================================================================== /** A set of colour IDs to use to change the colour of various aspects of the link. @@ -173,8 +176,8 @@ public: private: //============================================================================== ButtonStyle style; - ScopedPointer normalImage, overImage, downImage, disabledImage, - normalImageOn, overImageOn, downImageOn, disabledImageOn; + ScopedPointer normalImage, overImage, downImage, disabledImage, + normalImageOn, overImageOn, downImageOn, disabledImageOn; Drawable* currentImage; int edgeIndent;