# A more flexible DrawableButton?

**URL:** <https://forum.juce.com/t/a-more-flexible-drawablebutton/12960>\
**Category:** General JUCE discussion\
**Created:** [June 6, 2014, 3:16pm UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960 "2014-06-06T15:16:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hladik](https://avatars.discourse-cdn.com/v4/letter/h/3ab097/32.png) [@hladik](https://forum.juce.com/u/hladik)\
**Post date:** [June 6, 2014, 3:16pm UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960/1 "2014-06-06T15:16:50Z")

</div>

Hello Jules,

I have tried without success to reduce the size of the border that a DrawableButton puts around its Image when using the ImageOnButtonBackground style. Looking at the code in DrawableButton::resized(), I see that the minimum allowed border is 1/4 of the button dimension per side.

I also tried deriving a class, overriding resized(), and apart from the fact that the way to access the various images for each state is quite annoying without some sort of getImage(index) method, this does not work, since DrawableButton::buttonStateChanged() explicitly calls DrawableButton::resized() instead of just resized().

Yes, I could write an almost identical class that does what I need, but would you consider making DrawableButton a little more customizable?

Thanks  
&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 7, 2014, 12:01pm UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960/2 "2014-06-07T12:01:19Z")

</div>

What would you suggest adding to the class that would do what you need?

---

<div class="post-metadata">

**Author:** ![hladik](https://avatars.discourse-cdn.com/v4/letter/h/3ab097/32.png) [@hladik](https://forum.juce.com/u/hladik)\
**Post date:** [June 8, 2014, 7:49am UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960/3 "2014-06-08T07:49:40Z")

</div>

A minimal modification that would not break existing code and would consent a derived class to change the layout, would be to eliminate the explicit reference to DrawableButton::resized() inside buttonStateChanged():

```
void DrawableButton::buttonStateChanged()
{
...

    if (imageToDraw != currentImage)
    {
        removeChildComponent (currentImage);
        currentImage = imageToDraw;

        if (currentImage != nullptr)
        {
            currentImage->setInterceptsMouseClicks (false, false);
            addAndMakeVisible (currentImage);
            /*DrawableButton::*/resized();
        }
    }

...
}
```

Another nice change that would make the 'edgeIndent' variable more effective would be:

```
void DrawableButton::resized()
{
...

        else
        {
            Rectangle<int> imageSpace;

            const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
            const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
            

           if (style == ImageOnButtonBackground)
            {
        /*
                imageSpace = getLocalBounds().reduced (jmax (getWidth() / 4, indentX),
                                                       jmax (getHeight() / 4, indentY));
        */
                imageSpace = getLocalBounds().reduced(indentX, indentY);

            }

...

}
```

Although this would break existing code.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 8, 2014, 10:07am UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960/4 "2014-06-08T10:07:38Z")

</div>

Ok, I've cleaned up the code a bit and added a virtual method&nbsp;getImageBounds() that will probably do the job for you.

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [June 8, 2014, 10:14am UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960/5 "2014-06-08T10:14:31Z")

</div>

I don't' see the commit in git yet. but (if it is not what you've done) i think it would be better to move the resized() and getImageBounds()&nbsp;into the lookandfeel no?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 8, 2014, 11:05am UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960/6 "2014-06-08T11:05:24Z")

</div>

Whoops, forgot to push. Yeah, it could be argued that it belongs in the L+F but TBH this all seems like a lot of fuss to me over such a simple class. If you have a custom button&nbsp;that deviates dramatically from it then it probably belongs in a class of its own.

---

<div class="post-metadata">

**Author:** ![hladik](https://avatars.discourse-cdn.com/v4/letter/h/3ab097/32.png) [@hladik](https://forum.juce.com/u/hladik)\
**Post date:** [June 8, 2014, 1:55pm UTC](https://forum.juce.com/t/a-more-flexible-drawablebutton/12960/7 "2014-06-08T13:55:29Z")

</div>

Thank you Jules.
