Default look and feel drawButtonBackground() is override by Button::paintButton()

I have problem to understand how to properly use the LookAndFeel_V4::getDefaultLookAndFeel()

In my default look and feel I have override method drawButtonBackground(), like that:

class MyDefaultLAF : public LookAndFeel_V4
    // ... some code ...

    void drawButtonBackground (Graphics& g, Button& b, const Colour& backgroundColour,
                               bool mouseOver, bool mouseDown) override
        // some drawings

But my Button class looks like that:

class MyButton : public Button
    MyButton() { setLookAndFeel(&LookAndFeel_V4::getDefaultLookAndFeel()); };

    // ... some code

And this of course will not even compile, because in MyButton class I need to override paintButton().
But if I override paintButton() then it is used instead MyDefaultLAF::drawButtonBackground().

So I can’t understand how can I use one default look and feel for all my components?

For any help great thanks in advance.

Best Regards

If you want a default global LnF for all your app just use LookAndFeel::setDefaultLookAndFeel at initialisation step.

Things are more hard if you need to customize various components with multiple LnF.

Button is a common base class for e.g. TextButton, DrawableButton, etc.
If you use one of the subclasses, they will have the paintButton implemented so they use your LNF method.
If you want to inherit from Button, you need to call the drawButtonBackground() yourself from your paintButton. I would recommend choosing the best fitting subclass of Button, but it’s a matter of preference.

1 Like

imagine this

A) TextButton → is of type → Button [ overriding functions ]

now imagine this

B) TextButton [ override function ] → is of type → Button

when they are overriding …

A overrides a base button class, but not the Type of button being used
B overrides the actual button type you’re using and has power over Button

I just did a full text search for drawButtonBackground().
Only the TextButton ever calls that method, which makes sense: the background for the other buttons is most likely not identical.

It is probably historical or an oversight that drawButtonBackground() is not in TextButton::LookAndFeelMethods.

I’m nitpicking but It seems that DrawableButton also calls it.

void DrawableButton::paintButton (Graphics& g,
                                  const bool shouldDrawButtonAsHighlighted,
                                  const bool shouldDrawButtonAsDown)
    auto& lf = getLookAndFeel();

    if (shouldDrawButtonBackground())
        lf.drawButtonBackground (g, *this,
                                 findColour (getToggleState() ? TextButton::buttonOnColourId
                                                              : TextButton::buttonColourId),
                                 shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);
        lf.drawDrawableButton (g, *this, shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);

Anyway i’m agree with @daniel : inherit from a Button’s subclass or call it yourself.
And sorry for my first response that was a bit OT. :sweat_smile:

@pajczur For fun I did a quick example here:

Great thanks for all your answers. Now u=it is clear for me.
Best Regards

1 Like