From 4fefd0bb52083a1b6aa55f4ca356c0af7f230e4a Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 21 Sep 2012 13:26:08 +0100 Subject: [PATCH] Added a stretch-to-fit option to DrawableButton. --- .../buttons/juce_DrawableButton.cpp | 28 ++++++++++++------- .../buttons/juce_DrawableButton.h | 3 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp b/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp index c2e5c5b8e0..687d1efa19 100644 --- a/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp +++ b/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp @@ -23,8 +23,7 @@ ============================================================================== */ -DrawableButton::DrawableButton (const String& name, - const DrawableButton::ButtonStyle buttonStyle) +DrawableButton::DrawableButton (const String& name, const DrawableButton::ButtonStyle buttonStyle) : Button (name), style (buttonStyle), currentImage (nullptr), @@ -37,6 +36,11 @@ DrawableButton::~DrawableButton() } //============================================================================== +static Drawable* copyDrawableIfNotNull (const Drawable* const d) +{ + return d != nullptr ? d->createCopy() : nullptr; +} + void DrawableButton::setImages (const Drawable* normal, const Drawable* over, const Drawable* down, @@ -48,14 +52,14 @@ void DrawableButton::setImages (const Drawable* normal, { jassert (normal != nullptr); // you really need to give it at least a normal image.. - if (normal != nullptr) normalImage = normal->createCopy(); - if (over != nullptr) overImage = over->createCopy(); - if (down != nullptr) downImage = down->createCopy(); - if (disabled != nullptr) disabledImage = disabled->createCopy(); - if (normalOn != nullptr) normalImageOn = normalOn->createCopy(); - if (overOn != nullptr) overImageOn = overOn->createCopy(); - if (downOn != nullptr) downImageOn = downOn->createCopy(); - if (disabledOn != nullptr) disabledImageOn = disabledOn->createCopy(); + normalImage = copyDrawableIfNotNull (normal); + overImage = copyDrawableIfNotNull (over); + downImage = copyDrawableIfNotNull (down); + disabledImage = copyDrawableIfNotNull (disabled); + normalImageOn = copyDrawableIfNotNull (normalOn); + overImageOn = copyDrawableIfNotNull (overOn); + downImageOn = copyDrawableIfNotNull (downOn); + disabledImageOn = copyDrawableIfNotNull (disabledOn); buttonStateChanged(); } @@ -87,6 +91,10 @@ void DrawableButton::resized() { currentImage->setOriginWithOriginalSize (Point()); } + else if (style == ImageStretched) + { + currentImage->setTransformToFit (getLocalBounds().toFloat(), RectanglePlacement::stretchToFit); + } else { Rectangle imageSpace; diff --git a/modules/juce_gui_basics/buttons/juce_DrawableButton.h b/modules/juce_gui_basics/buttons/juce_DrawableButton.h index a3c68c1864..624c07842a 100644 --- a/modules/juce_gui_basics/buttons/juce_DrawableButton.h +++ b/modules/juce_gui_basics/buttons/juce_DrawableButton.h @@ -49,9 +49,10 @@ public: ImageRaw, /**< The button will just display the images in their normal size and position. This leaves it up to the caller to make sure the images are the correct size and position for the button. */ ImageAboveTextLabel, /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */ - ImageOnButtonBackground /**< Draws the button as a standard rounded-rectangle button with the image on top. + ImageOnButtonBackground, /**< Draws the button as a standard rounded-rectangle button with the image on top. Note that if you use this style, the colour IDs that control the button colour are TextButton::buttonColourId and TextButton::buttonOnColourId. */ + ImageStretched /**< Fills the button with a stretched version of the image. */ }; //==============================================================================