Browse Source

Refactored a couple of old methods relating to TextButton sizing: Removed TextButton::getFont() (use the LookAndFeel method instead), and replaced the LookAndFeel::changeTextButtonWidthToFitText method with one called getTextButtonWidthToFitText()

tags/2021-05-28
jules 11 years ago
parent
commit
b65dfbfc7c
6 changed files with 70 additions and 51 deletions
  1. +3
    -3
      extras/Demo/Source/Demos/DialogsDemo.cpp
  2. +9
    -3
      modules/juce_gui_basics/buttons/juce_Button.h
  3. +14
    -7
      modules/juce_gui_basics/buttons/juce_TextButton.cpp
  4. +23
    -10
      modules/juce_gui_basics/buttons/juce_TextButton.h
  5. +5
    -10
      modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp
  6. +16
    -18
      modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.h

+ 3
- 3
extras/Demo/Source/Demos/DialogsDemo.cpp View File

@@ -209,7 +209,7 @@ private:
AlertWindow::showMessageBoxAsync (icon,
"This is an AlertWindow",
"And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
"ok");
"OK");
}
else if (type == okCancelAlertWindow)
{
@@ -243,8 +243,8 @@ private:
const char* options[] = { "option 1", "option 2", "option 3", "option 4", nullptr };
w.addComboBox ("option", StringArray (options), "some options");
w.addButton ("ok", 1, KeyPress (KeyPress::returnKey, 0, 0));
w.addButton ("cancel", 0, KeyPress (KeyPress::escapeKey, 0, 0));
w.addButton ("OK", 1, KeyPress (KeyPress::returnKey, 0, 0));
w.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey, 0, 0));
if (w.runModalLoop() != 0) // is they picked 'ok'
{


+ 9
- 3
modules/juce_gui_basics/buttons/juce_Button.h View File

@@ -363,9 +363,8 @@ public:
virtual void drawButtonBackground (Graphics&, Button&, const Colour& backgroundColour,
bool isMouseOverButton, bool isButtonDown) = 0;
virtual Font getTextButtonFont (TextButton&) = 0;
virtual void changeTextButtonWidthToFitText (TextButton&, int newHeight) = 0;
virtual Font getTextButtonFont (TextButton&, int buttonHeight) = 0;
virtual int getTextButtonWidthToFitText (TextButton&, int buttonHeight) = 0;
/** Draws the text for a TextButton. */
virtual void drawButtonText (Graphics&, TextButton&, bool isMouseOverButton, bool isButtonDown) = 0;
@@ -379,6 +378,13 @@ public:
bool ticked, bool isEnabled, bool isMouseOverButton, bool isButtonDown) = 0;
virtual void drawDrawableButton (Graphics&, DrawableButton&, bool isMouseOverButton, bool isButtonDown) = 0;
private:
#if JUCE_CATCH_DEPRECATED_CODE_MISUSE
// These method have been deprecated: see their replacements above.
virtual int getTextButtonFont (TextButton&) { return 0; }
virtual int changeTextButtonWidthToFitText (TextButton&, int) { return 0; }
#endif
};
protected:


+ 14
- 7
modules/juce_gui_basics/buttons/juce_TextButton.cpp View File

@@ -1,7 +1,6 @@
/*
==============================================================================
This file is part of the JUCE library.
file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
@@ -26,8 +25,11 @@ TextButton::TextButton() : Button (String())
{
}
TextButton::TextButton (const String& name, const String& toolTip)
: Button (name)
TextButton::TextButton (const String& name) : Button (name)
{
}
TextButton::TextButton (const String& name, const String& toolTip) : Button (name)
{
setTooltip (toolTip);
}
@@ -52,12 +54,17 @@ void TextButton::colourChanged()
repaint();
}
Font TextButton::getFont()
void TextButton::changeWidthToFitText()
{
return Font (jmin (15.0f, getHeight() * 0.6f));
changeWidthToFitText (getHeight());
}
void TextButton::changeWidthToFitText (const int newHeight)
{
getLookAndFeel().changeTextButtonWidthToFitText (*this, newHeight);
setSize (getBestWidthForHeight (newHeight), newHeight);
}
int TextButton::getBestWidthForHeight (int buttonHeight)
{
return getLookAndFeel().getTextButtonWidthToFitText (*this, buttonHeight);
}

+ 23
- 10
modules/juce_gui_basics/buttons/juce_TextButton.h View File

@@ -40,15 +40,20 @@ public:
/** 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)
*/
explicit TextButton (const String& buttonName);
/** 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
*/
explicit TextButton (const String& buttonName,
const String& toolTip = String::empty);
TextButton (const String& buttonName, const String& toolTip);
/** Destructor. */
~TextButton();
@@ -74,17 +79,20 @@ 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.
/** Changes this button's width to fit neatly around its current text, without
changing its height.
*/
void changeWidthToFitText (int newHeight = -1);
void changeWidthToFitText();
/** 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.
/** Resizes the button's width to fit neatly around its current text, and gives it
the specified height.
*/
virtual Font getFont();
void changeWidthToFitText (int newHeight);
/** Returns the width that the LookAndFeel suggests would be best for this button if it
had the given height.
*/
int getBestWidthForHeight (int buttonHeight);
//==============================================================================
/** @internal */
@@ -93,6 +101,11 @@ public:
void colourChanged() override;
private:
#if JUCE_CATCH_DEPRECATED_CODE_MISUSE
// Note that this method has been removed - instead, see LookAndFeel::getTextButtonWidthToFitText()
virtual int getFont() { return 0; }
#endif
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextButton)
};


+ 5
- 10
modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.cpp View File

@@ -238,24 +238,19 @@ void LookAndFeel_V2::drawButtonBackground (Graphics& g,
button.isConnectedOnBottom());
}
Font LookAndFeel_V2::getTextButtonFont (TextButton& button)
Font LookAndFeel_V2::getTextButtonFont (TextButton&, int buttonHeight)
{
return button.getFont();
return Font (jmin (15.0f, buttonHeight * 0.6f));
}
void LookAndFeel_V2::changeTextButtonWidthToFitText (TextButton& b, int newHeight)
int LookAndFeel_V2::getTextButtonWidthToFitText (TextButton& b, int buttonHeight)
{
if (newHeight >= 0)
b.setSize (jmax (1, b.getWidth()), newHeight);
else
newHeight = b.getHeight();
b.setSize (getTextButtonFont (b).getStringWidth (b.getButtonText()) + newHeight, newHeight);
return getTextButtonFont (b, buttonHeight).getStringWidth (b.getButtonText()) + buttonHeight;
}
void LookAndFeel_V2::drawButtonText (Graphics& g, TextButton& button, bool /*isMouseOverButton*/, bool /*isButtonDown*/)
{
Font font (getTextButtonFont (button));
Font font (getTextButtonFont (button, button.getHeight()));
g.setFont (font);
g.setColour (button.findColour (button.getToggleState() ? TextButton::textColourOnId
: TextButton::textColourOffId)


+ 16
- 18
modules/juce_gui_basics/lookandfeel/juce_LookAndFeel_V2.h View File

@@ -39,16 +39,14 @@ public:
~LookAndFeel_V2();
//==============================================================================
void drawButtonBackground (Graphics&, Button& button, const Colour& backgroundColour,
void drawButtonBackground (Graphics&, Button&, const Colour& backgroundColour,
bool isMouseOverButton, bool isButtonDown) override;
Font getTextButtonFont (TextButton&, int buttonHeight) override;
Font getTextButtonFont (TextButton&) override;
void drawButtonText (Graphics&, TextButton&, bool isMouseOverButton, bool isButtonDown) override;
int getTextButtonWidthToFitText (TextButton&, int buttonHeight) override;
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;
void drawToggleButton (Graphics&, ToggleButton&, bool isMouseOverButton, bool isButtonDown) override;
void changeToggleButtonWidthToFitText (ToggleButton&) override;
@@ -78,17 +76,17 @@ public:
//==============================================================================
bool areScrollbarButtonsVisible() override;
void drawScrollbarButton (Graphics& g, ScrollBar&, int width, int height, int buttonDirection,
void drawScrollbarButton (Graphics&, ScrollBar&, int width, int height, int buttonDirection,
bool isScrollbarVertical, bool isMouseOverButton, bool isButtonDown) override;
void drawScrollbar (Graphics& g, ScrollBar&, int x, int y, int width, int height,
void drawScrollbar (Graphics&, ScrollBar&, int x, int y, int width, int height,
bool isScrollbarVertical, int thumbStartPosition, int thumbSize,
bool isMouseOver, bool isMouseDown) override;
ImageEffectFilter* getScrollbarEffect() override;
int getMinimumScrollbarThumbSize (ScrollBar&) override;
int getDefaultScrollbarWidth() override;
int getScrollbarButtonSize (ScrollBar& scrollbar) override;
int getScrollbarButtonSize (ScrollBar&) override;
//==============================================================================
Path getTickShape (float height) override;
@@ -199,7 +197,7 @@ public:
//==============================================================================
Button* createFilenameComponentBrowseButton (const String& text) override;
void layoutFilenameComponent (FilenameComponent& filenameComp, ComboBox* filenameBox, Button* browseButton) override;
void layoutFilenameComponent (FilenameComponent&, ComboBox* filenameBox, Button* browseButton) override;
//==============================================================================
void drawConcertinaPanelHeader (Graphics&, const Rectangle<int>& area,
@@ -250,8 +248,8 @@ public:
void drawTabbedButtonBarBackground (TabbedButtonBar&, Graphics&) override;
void drawTabAreaBehindFrontButton (TabbedButtonBar&, Graphics&, int w, int h) override;
void createTabButtonShape (TabBarButton&, Path& path, bool isMouseOver, bool isMouseDown) override;
void fillTabButtonShape (TabBarButton&, Graphics&, const Path& path, bool isMouseOver, bool isMouseDown) override;
void createTabButtonShape (TabBarButton&, Path&, bool isMouseOver, bool isMouseDown) override;
void fillTabButtonShape (TabBarButton&, Graphics&, const Path&, bool isMouseOver, bool isMouseDown) override;
Button* createTabBarExtrasButton() override;
@@ -291,7 +289,7 @@ public:
//==============================================================================
void drawLevelMeter (Graphics&, int width, int height, float level) override;
void drawKeymapChangeButton (Graphics&, int width, int height, Button& button, const String& keyDescription) override;
void drawKeymapChangeButton (Graphics&, int width, int height, Button&, const String& keyDescription) override;
//==============================================================================
/** Draws a 3D raised (or indented) bevel using two colours.
@@ -318,15 +316,15 @@ public:
/** Utility function to draw a shiny, glassy circle (for round LED-type buttons). */
static void drawGlassSphere (Graphics&, float x, float y, float diameter,
const Colour& colour, float outlineThickness) noexcept;
const Colour&, float outlineThickness) noexcept;
static void drawGlassPointer (Graphics&, float x, float y, float diameter,
const Colour& colour, float outlineThickness, int direction) noexcept;
const Colour&, float outlineThickness, int direction) noexcept;
/** Utility function to draw a shiny, glassy oblong (for text buttons). */
static void drawGlassLozenge (Graphics&,
float x, float y, float width, float height,
const Colour& colour, float outlineThickness, float cornerSize,
const Colour&, float outlineThickness, float cornerSize,
bool flatOnLeft, bool flatOnRight, bool flatOnTop, bool flatOnBottom) noexcept;
private:
@@ -335,7 +333,7 @@ private:
void drawShinyButtonShape (Graphics&,
float x, float y, float w, float h, float maxCornerSize,
const Colour& baseColour, float strokeWidth,
const Colour&, float strokeWidth,
bool flatOnLeft, bool flatOnRight, bool flatOnTop, bool flatOnBottom) noexcept;
class GlassWindowButton;


Loading…
Cancel
Save