From dd24e058c7d3d3c93650d6f08e2a0bc0901d0815 Mon Sep 17 00:00:00 2001 From: jules Date: Sat, 22 Mar 2014 14:24:09 +0000 Subject: [PATCH] Added changeTextButtonWidthToFitText to the look + feel classes. --- modules/juce_gui_basics/buttons/juce_Button.h | 6 +++-- .../buttons/juce_TextButton.cpp | 26 +++++++------------ .../juce_gui_basics/buttons/juce_TextButton.h | 14 +++++----- .../lookandfeel/juce_LookAndFeel_V2.cpp | 10 +++++++ .../lookandfeel/juce_LookAndFeel_V2.h | 1 + 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/modules/juce_gui_basics/buttons/juce_Button.h b/modules/juce_gui_basics/buttons/juce_Button.h index 26613548b9..aef6c9eb5b 100644 --- a/modules/juce_gui_basics/buttons/juce_Button.h +++ b/modules/juce_gui_basics/buttons/juce_Button.h @@ -360,10 +360,12 @@ public: { virtual ~LookAndFeelMethods() {} - virtual void drawButtonBackground (Graphics&, Button& button, const Colour& backgroundColour, + virtual void drawButtonBackground (Graphics&, Button&, const Colour& backgroundColour, bool isMouseOverButton, bool isButtonDown) = 0; - virtual Font getTextButtonFont (TextButton& button) = 0; + virtual Font getTextButtonFont (TextButton&) = 0; + + virtual void changeTextButtonWidthToFitText (TextButton&, int newHeight) = 0; /** Draws the text for a TextButton. */ virtual void drawButtonText (Graphics&, TextButton&, bool isMouseOverButton, bool isButtonDown) = 0; diff --git a/modules/juce_gui_basics/buttons/juce_TextButton.cpp b/modules/juce_gui_basics/buttons/juce_TextButton.cpp index eafffd4167..e2b885e41b 100644 --- a/modules/juce_gui_basics/buttons/juce_TextButton.cpp +++ b/modules/juce_gui_basics/buttons/juce_TextButton.cpp @@ -22,6 +22,10 @@ ============================================================================== */ +TextButton::TextButton() : Button (String()) +{ +} + TextButton::TextButton (const String& name, const String& toolTip) : Button (name) { @@ -32,21 +36,15 @@ TextButton::~TextButton() { } -void TextButton::paintButton (Graphics& g, - bool isMouseOverButton, - bool isButtonDown) +void TextButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) { LookAndFeel& lf = getLookAndFeel(); lf.drawButtonBackground (g, *this, - findColour (getToggleState() ? buttonOnColourId - : buttonColourId), - isMouseOverButton, - isButtonDown); - - lf.drawButtonText (g, *this, - isMouseOverButton, - isButtonDown); + findColour (getToggleState() ? buttonOnColourId : buttonColourId), + isMouseOverButton, isButtonDown); + + lf.drawButtonText (g, *this, isMouseOverButton, isButtonDown); } void TextButton::colourChanged() @@ -61,9 +59,5 @@ Font TextButton::getFont() void TextButton::changeWidthToFitText (const int newHeight) { - if (newHeight >= 0) - setSize (jmax (1, getWidth()), newHeight); - - setSize (getFont().getStringWidth (getButtonText()) + getHeight(), - getHeight()); + getLookAndFeel().changeTextButtonWidthToFitText (*this, newHeight); } diff --git a/modules/juce_gui_basics/buttons/juce_TextButton.h b/modules/juce_gui_basics/buttons/juce_TextButton.h index 67c0ae48eb..f630cbb888 100644 --- a/modules/juce_gui_basics/buttons/juce_TextButton.h +++ b/modules/juce_gui_basics/buttons/juce_TextButton.h @@ -37,17 +37,18 @@ class JUCE_API TextButton : public Button { public: //============================================================================== - /** Creates a TextButton. + /** Creates a TextButton. */ + TextButton(); + /** Creates a TextButton. @param buttonName the text to put in the button (the component's name is also initially set to this string, but these can be changed later using the setName() and setButtonText() methods) @param toolTip an optional string to use as a toolip - @see Button */ - TextButton (const String& buttonName = String::empty, - const String& toolTip = String::empty); + explicit TextButton (const String& buttonName, + const String& toolTip = String::empty); /** Destructor. */ ~TextButton(); @@ -74,19 +75,18 @@ public: //============================================================================== /** Resizes the button to fit neatly around its current text. - If newHeight is >= 0, the button's height will be changed to this value. If it's less than zero, its height will be unaffected. */ void changeWidthToFitText (int newHeight = -1); /** This can be overridden to use different fonts than the default one. - Note that you'll need to set the font's size appropriately, too. */ virtual Font getFont(); -protected: + + //============================================================================== /** @internal */ void paintButton (Graphics&, bool isMouseOverButton, bool isButtonDown) override; /** @internal */ diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp index 85045f7b2c..f4212595c6 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp @@ -243,6 +243,16 @@ Font LookAndFeel_V2::getTextButtonFont (TextButton& button) return button.getFont(); } +void LookAndFeel_V2::changeTextButtonWidthToFitText (TextButton& b, int newHeight) +{ + if (newHeight >= 0) + b.setSize (jmax (1, b.getWidth()), newHeight); + else + newHeight = b.getHeight(); + + b.setSize (getTextButtonFont (b).getStringWidth (b.getButtonText()) + newHeight, newHeight); +} + void LookAndFeel_V2::drawButtonText (Graphics& g, TextButton& button, bool /*isMouseOverButton*/, bool /*isButtonDown*/) { Font font (getTextButtonFont (button)); diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.h b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.h index 8079e5a133..8115ec32af 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.h +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.h @@ -46,6 +46,7 @@ public: void drawButtonText (Graphics&, TextButton& button, bool isMouseOverButton, bool isButtonDown) override; + void changeTextButtonWidthToFitText (TextButton&, int newHeight) override; void drawToggleButton (Graphics&, ToggleButton& button, bool isMouseOverButton, bool isButtonDown) override;