The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_DrawableButton.h"
  21. #include "../lookandfeel/juce_LookAndFeel.h"
  22. //==============================================================================
  23. DrawableButton::DrawableButton (const String& name,
  24. const DrawableButton::ButtonStyle buttonStyle)
  25. : Button (name),
  26. style (buttonStyle),
  27. normalImage (0),
  28. overImage (0),
  29. downImage (0),
  30. disabledImage (0),
  31. normalImageOn (0),
  32. overImageOn (0),
  33. downImageOn (0),
  34. disabledImageOn (0),
  35. edgeIndent (3)
  36. {
  37. if (buttonStyle == ImageOnButtonBackground)
  38. {
  39. backgroundOff = Colour (0xffbbbbff);
  40. backgroundOn = Colour (0xff3333ff);
  41. }
  42. else
  43. {
  44. backgroundOff = Colours::transparentBlack;
  45. backgroundOn = Colour (0xaabbbbff);
  46. }
  47. }
  48. DrawableButton::~DrawableButton()
  49. {
  50. deleteImages();
  51. }
  52. //==============================================================================
  53. void DrawableButton::deleteImages()
  54. {
  55. deleteAndZero (normalImage);
  56. deleteAndZero (overImage);
  57. deleteAndZero (downImage);
  58. deleteAndZero (disabledImage);
  59. deleteAndZero (normalImageOn);
  60. deleteAndZero (overImageOn);
  61. deleteAndZero (downImageOn);
  62. deleteAndZero (disabledImageOn);
  63. }
  64. void DrawableButton::setImages (const Drawable* normal,
  65. const Drawable* over,
  66. const Drawable* down,
  67. const Drawable* disabled,
  68. const Drawable* normalOn,
  69. const Drawable* overOn,
  70. const Drawable* downOn,
  71. const Drawable* disabledOn)
  72. {
  73. deleteImages();
  74. jassert (normal != 0); // you really need to give it at least a normal image..
  75. if (normal != 0)
  76. normalImage = normal->createCopy();
  77. if (over != 0)
  78. overImage = over->createCopy();
  79. if (down != 0)
  80. downImage = down->createCopy();
  81. if (disabled != 0)
  82. disabledImage = disabled->createCopy();
  83. if (normalOn != 0)
  84. normalImageOn = normalOn->createCopy();
  85. if (overOn != 0)
  86. overImageOn = overOn->createCopy();
  87. if (downOn != 0)
  88. downImageOn = downOn->createCopy();
  89. if (disabledOn != 0)
  90. disabledImageOn = disabledOn->createCopy();
  91. repaint();
  92. }
  93. //==============================================================================
  94. void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle)
  95. {
  96. if (style != newStyle)
  97. {
  98. style = newStyle;
  99. repaint();
  100. }
  101. }
  102. void DrawableButton::setBackgroundColours (const Colour& toggledOffColour,
  103. const Colour& toggledOnColour)
  104. {
  105. if (backgroundOff != toggledOffColour
  106. || backgroundOn != toggledOnColour)
  107. {
  108. backgroundOff = toggledOffColour;
  109. backgroundOn = toggledOnColour;
  110. repaint();
  111. }
  112. }
  113. const Colour& DrawableButton::getBackgroundColour() const throw()
  114. {
  115. return getToggleState() ? backgroundOn
  116. : backgroundOff;
  117. }
  118. void DrawableButton::setEdgeIndent (const int numPixelsIndent)
  119. {
  120. edgeIndent = numPixelsIndent;
  121. repaint();
  122. }
  123. void DrawableButton::paintButton (Graphics& g,
  124. bool isMouseOverButton,
  125. bool isButtonDown)
  126. {
  127. Rectangle imageSpace;
  128. if (style == ImageOnButtonBackground)
  129. {
  130. const int insetX = getWidth() / 4;
  131. const int insetY = getHeight() / 4;
  132. imageSpace.setBounds (insetX, insetY, getWidth() - insetX * 2, getHeight() - insetY * 2);
  133. getLookAndFeel().drawButtonBackground (g, *this,
  134. getBackgroundColour(),
  135. isMouseOverButton,
  136. isButtonDown);
  137. }
  138. else
  139. {
  140. g.fillAll (getBackgroundColour());
  141. const int textH = (style == ImageAboveTextLabel)
  142. ? jmin (16, proportionOfHeight (0.25f))
  143. : 0;
  144. const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
  145. const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));
  146. imageSpace.setBounds (indentX, indentY,
  147. getWidth() - indentX * 2,
  148. getHeight() - indentY * 2 - textH);
  149. if (textH > 0)
  150. {
  151. g.setFont ((float) textH);
  152. g.setColour (Colours::black.withAlpha (isEnabled() ? 1.0f : 0.4f));
  153. g.drawFittedText (getButtonText(),
  154. 2, getHeight() - textH - 1,
  155. getWidth() - 4, textH,
  156. Justification::centred, 1);
  157. }
  158. }
  159. g.setImageResamplingQuality (Graphics::mediumResamplingQuality);
  160. g.setOpacity (1.0f);
  161. const Drawable* imageToDraw = 0;
  162. if (isEnabled())
  163. {
  164. imageToDraw = getCurrentImage();
  165. }
  166. else
  167. {
  168. imageToDraw = getToggleState() ? disabledImageOn
  169. : disabledImage;
  170. if (imageToDraw == 0)
  171. {
  172. g.setOpacity (0.4f);
  173. imageToDraw = getNormalImage();
  174. }
  175. }
  176. if (imageToDraw != 0)
  177. {
  178. if (style == ImageRaw)
  179. {
  180. imageToDraw->draw (g);
  181. }
  182. else
  183. {
  184. imageToDraw->drawWithin (g,
  185. imageSpace.getX(),
  186. imageSpace.getY(),
  187. imageSpace.getWidth(),
  188. imageSpace.getHeight(),
  189. RectanglePlacement::centred);
  190. }
  191. }
  192. }
  193. //==============================================================================
  194. const Drawable* DrawableButton::getCurrentImage() const throw()
  195. {
  196. if (isDown())
  197. return getDownImage();
  198. if (isOver())
  199. return getOverImage();
  200. return getNormalImage();
  201. }
  202. const Drawable* DrawableButton::getNormalImage() const throw()
  203. {
  204. return (getToggleState() && normalImageOn != 0) ? normalImageOn
  205. : normalImage;
  206. }
  207. const Drawable* DrawableButton::getOverImage() const throw()
  208. {
  209. const Drawable* d = normalImage;
  210. if (getToggleState())
  211. {
  212. if (overImageOn != 0)
  213. d = overImageOn;
  214. else if (normalImageOn != 0)
  215. d = normalImageOn;
  216. else if (overImage != 0)
  217. d = overImage;
  218. }
  219. else
  220. {
  221. if (overImage != 0)
  222. d = overImage;
  223. }
  224. return d;
  225. }
  226. const Drawable* DrawableButton::getDownImage() const throw()
  227. {
  228. const Drawable* d = normalImage;
  229. if (getToggleState())
  230. {
  231. if (downImageOn != 0)
  232. d = downImageOn;
  233. else if (overImageOn != 0)
  234. d = overImageOn;
  235. else if (normalImageOn != 0)
  236. d = normalImageOn;
  237. else if (downImage != 0)
  238. d = downImage;
  239. else
  240. d = getOverImage();
  241. }
  242. else
  243. {
  244. if (downImage != 0)
  245. d = downImage;
  246. else
  247. d = getOverImage();
  248. }
  249. return d;
  250. }
  251. END_JUCE_NAMESPACE