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.

525 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. class ImageButtonHandler : public ButtonHandler
  19. {
  20. public:
  21. enum ImageRole
  22. {
  23. normalImage = 0,
  24. overImage = 1,
  25. downImage = 2
  26. };
  27. //==============================================================================
  28. ImageButtonHandler()
  29. : ButtonHandler ("Image Button", "ImageButton", typeid (ImageButton), 150, 24)
  30. {
  31. }
  32. Component* createNewComponent (JucerDocument*)
  33. {
  34. return new ImageButton ("new button");
  35. }
  36. void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
  37. {
  38. ButtonHandler::getEditableProperties (component, document, properties);
  39. addColourProperties (component, document, properties);
  40. ImageButton* const ib = (ImageButton*) component;
  41. ComponentLayout& layout = *document.getComponentLayout();
  42. properties.add (new ImageButtonProportionProperty (layout, ib));
  43. properties.add (new ImageButtonResourceProperty (layout, ib, normalImage, "normal image"));
  44. properties.add (new ImageButtonOpacityProperty (layout, ib, "opacity", normalImage));
  45. properties.add (new ImageButtonColourProperty (layout, ib, "overlay col.", normalImage));
  46. properties.add (new ImageButtonResourceProperty (layout, ib, overImage, "over image"));
  47. properties.add (new ImageButtonOpacityProperty (layout, ib, "opacity", overImage));
  48. properties.add (new ImageButtonColourProperty (layout, ib, "overlay col.", overImage));
  49. properties.add (new ImageButtonResourceProperty (layout, ib, downImage, "down image"));
  50. properties.add (new ImageButtonOpacityProperty (layout, ib, "opacity", downImage));
  51. properties.add (new ImageButtonColourProperty (layout, ib, "overlay col.", downImage));
  52. }
  53. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  54. {
  55. XmlElement* e = ButtonHandler::createXmlFor (comp, layout);
  56. ImageButton* const ib = (ImageButton*) comp;
  57. e->setAttribute ("keepProportions", doesImageKeepProportions (ib));
  58. e->setAttribute ("resourceNormal", getImageResource (ib, normalImage));
  59. e->setAttribute ("opacityNormal", getImageOpacity (ib, normalImage));
  60. e->setAttribute ("colourNormal", getImageColour (ib, normalImage).toString());
  61. e->setAttribute ("resourceOver", getImageResource (ib, overImage));
  62. e->setAttribute ("opacityOver", getImageOpacity (ib, overImage));
  63. e->setAttribute ("colourOver", getImageColour (ib, overImage).toString());
  64. e->setAttribute ("resourceDown", getImageResource (ib, downImage));
  65. e->setAttribute ("opacityDown", getImageOpacity (ib, downImage));
  66. e->setAttribute ("colourDown", getImageColour (ib, downImage).toString());
  67. return e;
  68. }
  69. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  70. {
  71. if (! ButtonHandler::restoreFromXml (xml, comp, layout))
  72. return false;
  73. ImageButton* const ib = (ImageButton*) comp;
  74. ComponentLayout& l = const_cast <ComponentLayout&> (*layout);
  75. setImageKeepProportions (l, ib, xml.getBoolAttribute ("keepProportions", true), false);
  76. setImageResource (l, ib, normalImage, xml.getStringAttribute ("resourceNormal", String::empty), false);
  77. setImageOpacity (l, ib, normalImage, (float) xml.getDoubleAttribute ("opacityNormal", 1.0f), false);
  78. setImageColour (l, ib, normalImage, Colour::fromString (xml.getStringAttribute ("colourNormal", "0")), false);
  79. setImageResource (l, ib, overImage, xml.getStringAttribute ("resourceOver", String::empty), false);
  80. setImageOpacity (l, ib, overImage, (float) xml.getDoubleAttribute ("opacityOver", 1.0f), false);
  81. setImageColour (l, ib, overImage, Colour::fromString (xml.getStringAttribute ("colourOver", "0")), false);
  82. setImageResource (l, ib, downImage, xml.getStringAttribute ("resourceDown", String::empty), false);
  83. setImageOpacity (l, ib, downImage, (float) xml.getDoubleAttribute ("opacityDown", 1.0f), false);
  84. setImageColour (l, ib, downImage, Colour::fromString (xml.getStringAttribute ("colourDown", "0")), false);
  85. return true;
  86. }
  87. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  88. {
  89. ButtonHandler::fillInCreationCode (code, component, memberVariableName);
  90. ImageButton* const ib = dynamic_cast <ImageButton*> (component);
  91. String s;
  92. s << getColourIntialisationCode (component, memberVariableName)
  93. << '\n';
  94. const String indent (String::repeatedString (" ", memberVariableName.length() + 13));
  95. s << memberVariableName << "->setImages (false, true, "
  96. << CodeHelpers::boolLiteral (doesImageKeepProportions (ib)) << ",\n"
  97. << indent
  98. << getImageCreationCode (ib, normalImage) << ", "
  99. << CodeHelpers::floatLiteral (getImageOpacity (ib, normalImage), 3) << ", "
  100. << CodeHelpers::colourToCode (getImageColour (ib, normalImage)) << ",\n"
  101. << indent
  102. << getImageCreationCode (ib, overImage) << ", "
  103. << CodeHelpers::floatLiteral (getImageOpacity (ib, overImage), 3) << ", "
  104. << CodeHelpers::colourToCode (getImageColour (ib, overImage)) << ",\n"
  105. << indent
  106. << getImageCreationCode (ib, downImage) << ", "
  107. << CodeHelpers::floatLiteral (getImageOpacity (ib, downImage), 3) << ", "
  108. << CodeHelpers::colourToCode (getImageColour (ib, downImage))
  109. << ");\n";
  110. code.constructorCode += s;
  111. }
  112. static String getImageCreationCode (ImageButton* ib, const ImageRole role)
  113. {
  114. const String resName (getImageResource (ib, role));
  115. if (resName.isEmpty())
  116. return "Image()";
  117. return "ImageCache::getFromMemory (" + resName + ", " + resName + "Size)";
  118. }
  119. //==============================================================================
  120. class ImageButtonResourceProperty : public ImageResourceProperty <ImageButton>
  121. {
  122. public:
  123. ImageButtonResourceProperty (ComponentLayout& layout_, ImageButton* const owner_, const ImageRole role_, const String& name)
  124. : ImageResourceProperty <ImageButton> (*layout_.getDocument(), owner_, name, true),
  125. role (role_),
  126. layout (layout_)
  127. {
  128. }
  129. void setResource (const String& newName)
  130. {
  131. setImageResource (layout, element, role, newName, true);
  132. }
  133. String getResource() const
  134. {
  135. return getImageResource (element, role);
  136. }
  137. private:
  138. const ImageRole role;
  139. ComponentLayout& layout;
  140. };
  141. class SetImageResourceAction : public ComponentUndoableAction <ImageButton>
  142. {
  143. public:
  144. SetImageResourceAction (ImageButton* const button,
  145. ComponentLayout& layout_,
  146. const ImageRole role_,
  147. const String& newResource_)
  148. : ComponentUndoableAction <ImageButton> (button, layout_),
  149. newResource (newResource_),
  150. role (role_),
  151. layout (layout_)
  152. {
  153. oldResource = ImageButtonHandler::getImageResource (button, role_);
  154. }
  155. bool perform()
  156. {
  157. showCorrectTab();
  158. ImageButtonHandler::setImageResource (layout, getComponent(), role, newResource, false);
  159. return true;
  160. }
  161. bool undo()
  162. {
  163. showCorrectTab();
  164. ImageButtonHandler::setImageResource (layout, getComponent(), role, oldResource, false);
  165. return true;
  166. }
  167. private:
  168. String newResource, oldResource;
  169. const ImageRole role;
  170. ComponentLayout& layout;
  171. };
  172. //==============================================================================
  173. static void setImageResource (ComponentLayout& layout, ImageButton* button, const ImageRole role, const String& newName, const bool undoable)
  174. {
  175. jassert (role < 3);
  176. if (role < 3 && getImageResource (button, role) != newName)
  177. {
  178. if (undoable)
  179. {
  180. layout.getDocument()->perform (new SetImageResourceAction (button, layout, role, newName),
  181. "Change image resource");
  182. }
  183. else
  184. {
  185. button->getProperties().set ("resource" + String ((int) role), newName);
  186. updateButtonImages (*layout.getDocument(), button);
  187. layout.changed();
  188. }
  189. }
  190. }
  191. static String getImageResource (ImageButton* button, const ImageRole role)
  192. {
  193. jassert (role < 3);
  194. return button->getProperties() ["resource" + String ((int) role)].toString();
  195. }
  196. //==============================================================================
  197. class SetImageKeepsPropAction : public ComponentUndoableAction <ImageButton>
  198. {
  199. public:
  200. SetImageKeepsPropAction (ImageButton* const button,
  201. ComponentLayout& layout_,
  202. const bool newState_)
  203. : ComponentUndoableAction <ImageButton> (button, layout_),
  204. newState (newState_),
  205. layout (layout_)
  206. {
  207. oldState = ImageButtonHandler::doesImageKeepProportions (button);
  208. }
  209. bool perform()
  210. {
  211. showCorrectTab();
  212. ImageButtonHandler::setImageKeepProportions (layout, getComponent(), newState, false);
  213. return true;
  214. }
  215. bool undo()
  216. {
  217. showCorrectTab();
  218. ImageButtonHandler::setImageKeepProportions (layout, getComponent(), oldState, false);
  219. return true;
  220. }
  221. private:
  222. bool newState, oldState;
  223. ComponentLayout& layout;
  224. };
  225. static bool doesImageKeepProportions (ImageButton* button)
  226. {
  227. return button->getProperties().getWithDefault ("keepImageProp", true);
  228. }
  229. static void setImageKeepProportions (ComponentLayout& layout, ImageButton* button, const bool newState, const bool undoable)
  230. {
  231. if (undoable)
  232. {
  233. layout.perform (new SetImageKeepsPropAction (button, layout, newState), "change imagebutton proportion mode");
  234. }
  235. else
  236. {
  237. button->getProperties().set ("keepImageProp", newState);
  238. updateButtonImages (*layout.getDocument(), button);
  239. layout.changed();
  240. }
  241. }
  242. class ImageButtonProportionProperty : public ComponentBooleanProperty <ImageButton>
  243. {
  244. public:
  245. ImageButtonProportionProperty (ComponentLayout& layout_, ImageButton* const owner_)
  246. : ComponentBooleanProperty <ImageButton> ("proportional", "maintain image proportions", "scale to fit",
  247. owner_, *layout_.getDocument()),
  248. layout (layout_)
  249. {
  250. }
  251. void setState (bool newState)
  252. {
  253. setImageKeepProportions (layout, component, newState, true);
  254. }
  255. bool getState() const
  256. {
  257. return doesImageKeepProportions (component);
  258. }
  259. private:
  260. ComponentLayout& layout;
  261. };
  262. //==============================================================================
  263. class SetImageOpacityAction : public ComponentUndoableAction <ImageButton>
  264. {
  265. public:
  266. SetImageOpacityAction (ImageButton* const button,
  267. ComponentLayout& layout_,
  268. const ImageRole role_,
  269. const float newState_)
  270. : ComponentUndoableAction <ImageButton> (button, layout_),
  271. role (role_),
  272. newState (newState_),
  273. layout (layout_)
  274. {
  275. oldState = ImageButtonHandler::getImageOpacity (button, role_);
  276. }
  277. bool perform()
  278. {
  279. showCorrectTab();
  280. ImageButtonHandler::setImageOpacity (layout, getComponent(), role, newState, false);
  281. return true;
  282. }
  283. bool undo()
  284. {
  285. showCorrectTab();
  286. ImageButtonHandler::setImageOpacity (layout, getComponent(), role, oldState, false);
  287. return true;
  288. }
  289. private:
  290. const ImageRole role;
  291. float newState, oldState;
  292. ComponentLayout& layout;
  293. };
  294. static float getImageOpacity (ImageButton* button, const ImageRole role)
  295. {
  296. return (float) button->getProperties().getWithDefault ("imageOpacity" + String ((int) role), 1.0f);
  297. }
  298. static void setImageOpacity (ComponentLayout& layout, ImageButton* button, const ImageRole role, const float opacity, const bool undoable)
  299. {
  300. if (undoable)
  301. {
  302. layout.perform (new SetImageOpacityAction (button, layout, role, opacity), "change imagebutton opacity");
  303. }
  304. else
  305. {
  306. button->getProperties().set ("imageOpacity" + String ((int) role), opacity);
  307. updateButtonImages (*layout.getDocument(), button);
  308. layout.changed();
  309. }
  310. }
  311. class ImageButtonOpacityProperty : public SliderPropertyComponent
  312. {
  313. public:
  314. ImageButtonOpacityProperty (ComponentLayout& layout_, ImageButton* const owner_,
  315. const String& name, const ImageRole role_)
  316. : SliderPropertyComponent (name, 0.0, 1.0, 0.0),
  317. owner (owner_),
  318. layout (layout_),
  319. role (role_)
  320. {
  321. }
  322. void setValue (double newValue)
  323. {
  324. setImageOpacity (layout, owner, role, (float) newValue, true);
  325. }
  326. double getValue() const
  327. {
  328. return getImageOpacity (owner, role);
  329. }
  330. private:
  331. ImageButton* const owner;
  332. ComponentLayout& layout;
  333. const ImageRole role;
  334. };
  335. //==============================================================================
  336. class SetImageColourAction : public ComponentUndoableAction <ImageButton>
  337. {
  338. public:
  339. SetImageColourAction (ImageButton* const button,
  340. ComponentLayout& layout_,
  341. const ImageRole role_,
  342. const Colour& newState_)
  343. : ComponentUndoableAction <ImageButton> (button, layout_),
  344. role (role_),
  345. newState (newState_),
  346. layout (layout_)
  347. {
  348. oldState = ImageButtonHandler::getImageColour (button, role_);
  349. }
  350. bool perform()
  351. {
  352. showCorrectTab();
  353. ImageButtonHandler::setImageColour (layout, getComponent(), role, newState, false);
  354. return true;
  355. }
  356. bool undo()
  357. {
  358. showCorrectTab();
  359. ImageButtonHandler::setImageColour (layout, getComponent(), role, oldState, false);
  360. return true;
  361. }
  362. private:
  363. const ImageRole role;
  364. Colour newState, oldState;
  365. ComponentLayout& layout;
  366. };
  367. static Colour getImageColour (ImageButton* button, const ImageRole role)
  368. {
  369. return Colour::fromString (button->getProperties().getWithDefault ("imageColour" + String ((int) role), "0").toString());
  370. }
  371. static void setImageColour (ComponentLayout& layout, ImageButton* button, const ImageRole role, const Colour& colour, const bool undoable)
  372. {
  373. if (undoable)
  374. {
  375. layout.perform (new SetImageColourAction (button, layout, role, colour), "change imagebutton colour");
  376. }
  377. else
  378. {
  379. button->getProperties().set ("imageColour" + String ((int) role), colour.toString());
  380. updateButtonImages (*layout.getDocument(), button);
  381. layout.changed();
  382. }
  383. }
  384. class ImageButtonColourProperty : public JucerColourPropertyComponent,
  385. public ChangeListener
  386. {
  387. public:
  388. ImageButtonColourProperty (ComponentLayout& layout_, ImageButton* const owner_,
  389. const String& name, const ImageRole role_)
  390. : JucerColourPropertyComponent (name, false),
  391. owner (owner_),
  392. layout (layout_),
  393. role (role_)
  394. {
  395. layout_.getDocument()->addChangeListener (this);
  396. }
  397. ~ImageButtonColourProperty()
  398. {
  399. layout.getDocument()->removeChangeListener (this);
  400. }
  401. void setColour (const Colour& newColour)
  402. {
  403. setImageColour (layout, owner, role, newColour, true);
  404. }
  405. Colour getColour() const
  406. {
  407. return getImageColour (owner, role);
  408. }
  409. void resetToDefault() {}
  410. void changeListenerCallback (ChangeBroadcaster*)
  411. {
  412. refresh();
  413. }
  414. private:
  415. ImageButton* const owner;
  416. ComponentLayout& layout;
  417. const ImageRole role;
  418. };
  419. //==============================================================================
  420. static void updateButtonImages (JucerDocument& document, ImageButton* const ib)
  421. {
  422. Image norm = document.getResources().getImageFromCache (getImageResource (ib, normalImage));
  423. Image over = document.getResources().getImageFromCache (getImageResource (ib, overImage));
  424. Image down = document.getResources().getImageFromCache (getImageResource (ib, downImage));
  425. ib->setImages (false, true, doesImageKeepProportions (ib),
  426. norm,
  427. getImageOpacity (ib, normalImage),
  428. getImageColour (ib, normalImage),
  429. over,
  430. getImageOpacity (ib, overImage),
  431. getImageColour (ib, overImage),
  432. down,
  433. getImageOpacity (ib, downImage),
  434. getImageColour (ib, downImage));
  435. }
  436. };