I found it was nice to be able to have an unscaled image alongside a text label with my DrawableButton. Rather than fiddle with button sizes to try and find the magical combination that would do this, it turns out to be a VERY easy little add…
class JUCE_API DrawableButton : public Button
{
public:
//==============================================================================
enum ButtonStyle
{
ImageFitted, /**< The button will just display the images, but will resize and centre them to fit inside it. */
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. */
ImageRawAboveTextLabel, /** As above but image is drawn normal size */
ImageOnButtonBackground /**< Draws the button as a standard rounded-rectangle button with the image on top. */
};
<SNIP>
[code]
void DrawableButton::paintButton (Graphics& g,
bool isMouseOverButton,
bool isButtonDown)
{
Rectangle imageSpace;
if (style == ImageOnButtonBackground)
{
const int insetX = getWidth() / 4;
const int insetY = getHeight() / 4;
imageSpace.setBounds (insetX, insetY, getWidth() - insetX * 2, getHeight() - insetY * 2);
getLookAndFeel().drawButtonBackground (g, *this,
getBackgroundColour(),
isMouseOverButton,
isButtonDown);
}
else
{
g.fillAll (getBackgroundColour());
const int textH = (style == ImageAboveTextLabel || style == ImageRawAboveTextLabel)
? jmin (16, proportionOfHeight (0.25f))
: 0;
const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
imageSpace.setBounds (indentX, indentY,
getWidth() - indentX * 2,
getHeight() - indentY * 2 - textH);
if (textH > 0)
{
g.setFont ((float) textH);
g.setColour (Colours::black.withAlpha (isEnabled() ? 1.0f : 0.4f));
g.drawFittedText (getName(),
2, getHeight() - textH - 1,
getWidth() - 4, textH,
Justification::centred, 1);
}
}
g.setImageResamplingQuality (Graphics::mediumResamplingQuality);
g.setOpacity (1.0f);
const Drawable* imageToDraw = 0;
if (isEnabled())
{
imageToDraw = getCurrentImage();
}
else
{
imageToDraw = getToggleState() ? disabledImageOn
: disabledImage;
if (imageToDraw == 0)
{
g.setOpacity (0.4f);
imageToDraw = getNormalImage();
}
}
if (imageToDraw != 0)
{
if (style == ImageRaw || style == ImageRawAboveTextLabel)
{
imageToDraw->draw (g);
}
else
{
imageToDraw->drawWithin (g,
imageSpace.getX(),
imageSpace.getY(),
imageSpace.getWidth(),
imageSpace.getHeight(),
RectanglePlacement::centred);
}
}
}[/code]