2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
30 #include "juce_DrawableButton.h"
31 #include "../lookandfeel/juce_LookAndFeel.h"
34 //==============================================================================
35 DrawableButton::DrawableButton (const String
& name
,
36 const DrawableButton::ButtonStyle buttonStyle
)
39 currentImage (nullptr),
42 if (buttonStyle
== ImageOnButtonBackground
)
44 backgroundOff
= Colour (0xffbbbbff);
45 backgroundOn
= Colour (0xff3333ff);
49 backgroundOff
= Colours::transparentBlack
;
50 backgroundOn
= Colour (0xaabbbbff);
54 DrawableButton::~DrawableButton()
58 //==============================================================================
59 void DrawableButton::setImages (const Drawable
* normal
,
62 const Drawable
* disabled
,
63 const Drawable
* normalOn
,
64 const Drawable
* overOn
,
65 const Drawable
* downOn
,
66 const Drawable
* disabledOn
)
68 jassert (normal
!= nullptr); // you really need to give it at least a normal image..
70 if (normal
!= nullptr) normalImage
= normal
->createCopy();
71 if (over
!= nullptr) overImage
= over
->createCopy();
72 if (down
!= nullptr) downImage
= down
->createCopy();
73 if (disabled
!= nullptr) disabledImage
= disabled
->createCopy();
74 if (normalOn
!= nullptr) normalImageOn
= normalOn
->createCopy();
75 if (overOn
!= nullptr) overImageOn
= overOn
->createCopy();
76 if (downOn
!= nullptr) downImageOn
= downOn
->createCopy();
77 if (disabledOn
!= nullptr) disabledImageOn
= disabledOn
->createCopy();
82 //==============================================================================
83 void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle
)
85 if (style
!= newStyle
)
92 void DrawableButton::setBackgroundColours (const Colour
& toggledOffColour
,
93 const Colour
& toggledOnColour
)
95 if (backgroundOff
!= toggledOffColour
96 || backgroundOn
!= toggledOnColour
)
98 backgroundOff
= toggledOffColour
;
99 backgroundOn
= toggledOnColour
;
105 const Colour
& DrawableButton::getBackgroundColour() const noexcept
107 return getToggleState() ? backgroundOn
111 void DrawableButton::setEdgeIndent (const int numPixelsIndent
)
113 edgeIndent
= numPixelsIndent
;
118 void DrawableButton::resized()
122 if (currentImage
!= nullptr)
124 if (style
== ImageRaw
)
126 currentImage
->setOriginWithOriginalSize (Point
<float>());
130 Rectangle
<int> imageSpace
;
132 if (style
== ImageOnButtonBackground
)
134 imageSpace
= getLocalBounds().reduced (getWidth() / 4, getHeight() / 4);
138 const int textH
= (style
== ImageAboveTextLabel
) ? jmin (16, proportionOfHeight (0.25f
)) : 0;
140 const int indentX
= jmin (edgeIndent
, proportionOfWidth (0.3f
));
141 const int indentY
= jmin (edgeIndent
, proportionOfHeight (0.3f
));
143 imageSpace
.setBounds (indentX
, indentY
,
144 getWidth() - indentX
* 2,
145 getHeight() - indentY
* 2 - textH
);
148 currentImage
->setTransformToFit (imageSpace
.toFloat(), RectanglePlacement::centred
);
153 void DrawableButton::buttonStateChanged()
157 Drawable
* imageToDraw
= nullptr;
158 float opacity
= 1.0f
;
162 imageToDraw
= getCurrentImage();
166 imageToDraw
= getToggleState() ? disabledImageOn
169 if (imageToDraw
== nullptr)
172 imageToDraw
= getNormalImage();
176 if (imageToDraw
!= currentImage
)
178 removeChildComponent (currentImage
);
179 currentImage
= imageToDraw
;
181 if (currentImage
!= nullptr)
183 currentImage
->setInterceptsMouseClicks (false, false);
184 addAndMakeVisible (currentImage
);
185 DrawableButton::resized();
189 if (currentImage
!= nullptr)
190 currentImage
->setAlpha (opacity
);
193 void DrawableButton::paintButton (Graphics
& g
,
194 bool isMouseOverButton
,
197 if (style
== ImageOnButtonBackground
)
199 getLookAndFeel().drawButtonBackground (g
, *this,
200 getBackgroundColour(),
206 g
.fillAll (getBackgroundColour());
208 const int textH
= (style
== ImageAboveTextLabel
)
209 ? jmin (16, proportionOfHeight (0.25f
))
214 g
.setFont ((float) textH
);
216 g
.setColour (findColour (DrawableButton::textColourId
)
217 .withMultipliedAlpha (isEnabled() ? 1.0f
: 0.4f
));
219 g
.drawFittedText (getButtonText(),
220 2, getHeight() - textH
- 1,
221 getWidth() - 4, textH
,
222 Justification::centred
, 1);
227 //==============================================================================
228 Drawable
* DrawableButton::getCurrentImage() const noexcept
231 return getDownImage();
234 return getOverImage();
236 return getNormalImage();
239 Drawable
* DrawableButton::getNormalImage() const noexcept
241 return (getToggleState() && normalImageOn
!= nullptr) ? normalImageOn
245 Drawable
* DrawableButton::getOverImage() const noexcept
247 Drawable
* d
= normalImage
;
249 if (getToggleState())
251 if (overImageOn
!= nullptr)
253 else if (normalImageOn
!= nullptr)
255 else if (overImage
!= nullptr)
260 if (overImage
!= nullptr)
267 Drawable
* DrawableButton::getDownImage() const noexcept
269 Drawable
* d
= normalImage
;
271 if (getToggleState())
273 if (downImageOn
!= nullptr)
275 else if (overImageOn
!= nullptr)
277 else if (normalImageOn
!= nullptr)
279 else if (downImage
!= nullptr)
286 if (downImage
!= nullptr)