Add remaining files
[juce-lv2.git] / juce / source / src / gui / components / buttons / juce_DrawableButton.cpp
blobdf9fc6b2a9c9d6cdef09ce80cbf10d6132487763
1 /*
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"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_DrawableButton.h"
31 #include "../lookandfeel/juce_LookAndFeel.h"
34 //==============================================================================
35 DrawableButton::DrawableButton (const String& name,
36 const DrawableButton::ButtonStyle buttonStyle)
37 : Button (name),
38 style (buttonStyle),
39 currentImage (nullptr),
40 edgeIndent (3)
42 if (buttonStyle == ImageOnButtonBackground)
44 backgroundOff = Colour (0xffbbbbff);
45 backgroundOn = Colour (0xff3333ff);
47 else
49 backgroundOff = Colours::transparentBlack;
50 backgroundOn = Colour (0xaabbbbff);
54 DrawableButton::~DrawableButton()
58 //==============================================================================
59 void DrawableButton::setImages (const Drawable* normal,
60 const Drawable* over,
61 const Drawable* down,
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();
79 buttonStateChanged();
82 //==============================================================================
83 void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle)
85 if (style != newStyle)
87 style = newStyle;
88 buttonStateChanged();
92 void DrawableButton::setBackgroundColours (const Colour& toggledOffColour,
93 const Colour& toggledOnColour)
95 if (backgroundOff != toggledOffColour
96 || backgroundOn != toggledOnColour)
98 backgroundOff = toggledOffColour;
99 backgroundOn = toggledOnColour;
101 repaint();
105 const Colour& DrawableButton::getBackgroundColour() const noexcept
107 return getToggleState() ? backgroundOn
108 : backgroundOff;
111 void DrawableButton::setEdgeIndent (const int numPixelsIndent)
113 edgeIndent = numPixelsIndent;
114 repaint();
115 resized();
118 void DrawableButton::resized()
120 Button::resized();
122 if (currentImage != nullptr)
124 if (style == ImageRaw)
126 currentImage->setOriginWithOriginalSize (Point<float>());
128 else
130 Rectangle<int> imageSpace;
132 if (style == ImageOnButtonBackground)
134 imageSpace = getLocalBounds().reduced (getWidth() / 4, getHeight() / 4);
136 else
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()
155 repaint();
157 Drawable* imageToDraw = nullptr;
158 float opacity = 1.0f;
160 if (isEnabled())
162 imageToDraw = getCurrentImage();
164 else
166 imageToDraw = getToggleState() ? disabledImageOn
167 : disabledImage;
169 if (imageToDraw == nullptr)
171 opacity = 0.4f;
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,
195 bool isButtonDown)
197 if (style == ImageOnButtonBackground)
199 getLookAndFeel().drawButtonBackground (g, *this,
200 getBackgroundColour(),
201 isMouseOverButton,
202 isButtonDown);
204 else
206 g.fillAll (getBackgroundColour());
208 const int textH = (style == ImageAboveTextLabel)
209 ? jmin (16, proportionOfHeight (0.25f))
210 : 0;
212 if (textH > 0)
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
230 if (isDown())
231 return getDownImage();
233 if (isOver())
234 return getOverImage();
236 return getNormalImage();
239 Drawable* DrawableButton::getNormalImage() const noexcept
241 return (getToggleState() && normalImageOn != nullptr) ? normalImageOn
242 : normalImage;
245 Drawable* DrawableButton::getOverImage() const noexcept
247 Drawable* d = normalImage;
249 if (getToggleState())
251 if (overImageOn != nullptr)
252 d = overImageOn;
253 else if (normalImageOn != nullptr)
254 d = normalImageOn;
255 else if (overImage != nullptr)
256 d = overImage;
258 else
260 if (overImage != nullptr)
261 d = overImage;
264 return d;
267 Drawable* DrawableButton::getDownImage() const noexcept
269 Drawable* d = normalImage;
271 if (getToggleState())
273 if (downImageOn != nullptr)
274 d = downImageOn;
275 else if (overImageOn != nullptr)
276 d = overImageOn;
277 else if (normalImageOn != nullptr)
278 d = normalImageOn;
279 else if (downImage != nullptr)
280 d = downImage;
281 else
282 d = getOverImage();
284 else
286 if (downImage != nullptr)
287 d = downImage;
288 else
289 d = getOverImage();
292 return d;
295 END_JUCE_NAMESPACE