ToggleButton::changeWidthToFitText()


#1

When I call ToggleButton::changeWidthToFitText() it seems to leave a lot of extra space to the right of the text, rather than fitting “neatly around its current text” as the docs state.

I’m curious why 60 is added to the width in the code below, when the tick box’s width is at most 20:

[code]void LookAndFeel::changeToggleButtonWidthToFitText (ToggleButton& button)
{
Font font (jmin (15.0f, button.getHeight() * 0.6f));

button.setSize (font.getStringWidth (button.getButtonText()) + 60,
                button.getHeight());

}[/code]


#2

This comes out much nicer (borrowing a line from LookAndFeel::drawToggleButton()):

[code]void LookAndFeel::changeToggleButtonWidthToFitText (ToggleButton& button)
{
Font font (jmin (15.0f, button.getHeight() * 0.6f));

const int tickWidth = jmin (20, button.getHeight() - 4);

button.setSize (font.getStringWidth (button.getButtonText()) + tickWidth + 4,
                button.getHeight());

}[/code]


#3

ah yes, that line was a bit hacky! Thanks, I’ll tidy it up!


#4