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.

416 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../../Application/jucer_Headers.h"
  20. #include "jucer_ButtonDocument.h"
  21. #include "../jucer_UtilityFunctions.h"
  22. //==============================================================================
  23. static const int normalOff = 0;
  24. static const int overOff = 1;
  25. static const int downOff = 2;
  26. static const int normalOn = 3;
  27. static const int overOn = 4;
  28. static const int downOn = 5;
  29. static const int background = 6;
  30. //==============================================================================
  31. ButtonDocument::ButtonDocument (SourceCodeDocument* c)
  32. : JucerDocument (c)
  33. {
  34. paintStatesEnabled [normalOff] = true;
  35. paintStatesEnabled [overOff] = true;
  36. paintStatesEnabled [downOff] = true;
  37. paintStatesEnabled [normalOn] = false;
  38. paintStatesEnabled [overOn] = false;
  39. paintStatesEnabled [downOn] = false;
  40. paintStatesEnabled [background] = false;
  41. parentClasses = "public Button";
  42. for (int i = 7; --i >= 0;)
  43. {
  44. paintRoutines[i].reset (new PaintRoutine());
  45. paintRoutines[i]->setDocument (this);
  46. paintRoutines[i]->setBackgroundColour (Colours::transparentBlack);
  47. }
  48. }
  49. ButtonDocument::~ButtonDocument()
  50. {
  51. }
  52. static const char* const stateNames[] =
  53. {
  54. "normal", "over", "down",
  55. "normal on", "over on", "down on",
  56. "common background"
  57. };
  58. int stateNameToIndex (const String& name)
  59. {
  60. for (int i = 7; --i >= 0;)
  61. if (name.equalsIgnoreCase (stateNames[i]))
  62. return i;
  63. jassertfalse;
  64. return normalOff;
  65. }
  66. int ButtonDocument::getNumPaintRoutines() const
  67. {
  68. int n = 0;
  69. for (int i = 7; --i >= 0;)
  70. if (paintStatesEnabled [i])
  71. ++n;
  72. return n;
  73. }
  74. StringArray ButtonDocument::getPaintRoutineNames() const
  75. {
  76. StringArray s;
  77. for (int i = 0; i < 7; ++i)
  78. if (paintStatesEnabled [i])
  79. s.add (stateNames [i]);
  80. return s;
  81. }
  82. PaintRoutine* ButtonDocument::getPaintRoutine (const int index) const
  83. {
  84. int n = 0;
  85. for (int i = 0; i < 7; ++i)
  86. {
  87. if (paintStatesEnabled [i])
  88. {
  89. if (index == n)
  90. return paintRoutines[i].get();
  91. ++n;
  92. }
  93. }
  94. jassertfalse;
  95. return {};
  96. }
  97. void ButtonDocument::setStatePaintRoutineEnabled (const int index, bool b)
  98. {
  99. jassert (index > 0 && index < 7);
  100. if (paintStatesEnabled [index] != b)
  101. {
  102. paintStatesEnabled [index] = b;
  103. changed();
  104. }
  105. }
  106. bool ButtonDocument::isStatePaintRoutineEnabled (const int index) const
  107. {
  108. return paintStatesEnabled [index];
  109. }
  110. int ButtonDocument::chooseBestEnabledPaintRoutine (int paintRoutineWanted) const
  111. {
  112. switch (paintRoutineWanted)
  113. {
  114. case normalOff: return normalOff;
  115. case overOff: return paintStatesEnabled [overOff] ? overOff : normalOff;
  116. case downOff: return paintStatesEnabled [downOff] ? downOff : chooseBestEnabledPaintRoutine (overOff);
  117. case normalOn: return paintStatesEnabled [normalOn] ? normalOn : normalOff;
  118. case overOn: return paintStatesEnabled [overOn] ? overOn : (paintStatesEnabled [normalOn] ? normalOn : chooseBestEnabledPaintRoutine (overOff));
  119. case downOn: return paintStatesEnabled [downOn] ? downOn : ((paintStatesEnabled [overOn] || paintStatesEnabled [normalOn])
  120. ? chooseBestEnabledPaintRoutine (overOn)
  121. : chooseBestEnabledPaintRoutine (downOff));
  122. default: jassertfalse; break;
  123. }
  124. return normalOff;
  125. }
  126. //==============================================================================
  127. String ButtonDocument::getTypeName() const
  128. {
  129. return "Button";
  130. }
  131. JucerDocument* ButtonDocument::createCopy()
  132. {
  133. ButtonDocument* newOne = new ButtonDocument (cpp);
  134. newOne->resources = resources;
  135. ScopedPointer<XmlElement> xml (createXml());
  136. newOne->loadFromXml (*xml);
  137. return newOne;
  138. }
  139. XmlElement* ButtonDocument::createXml() const
  140. {
  141. XmlElement* const doc = JucerDocument::createXml();
  142. for (int i = 0; i < 7; ++i)
  143. {
  144. XmlElement* e = paintRoutines [i]->createXml();
  145. e->setAttribute ("buttonState", stateNames [i]);
  146. e->setAttribute ("enabled", paintStatesEnabled [i]);
  147. doc->addChildElement (e);
  148. }
  149. return doc;
  150. }
  151. bool ButtonDocument::loadFromXml (const XmlElement& xml)
  152. {
  153. if (JucerDocument::loadFromXml (xml))
  154. {
  155. for (int i = 7; --i >= 0;)
  156. paintStatesEnabled [i] = false;
  157. forEachXmlChildElementWithTagName (xml, e, PaintRoutine::xmlTagName)
  158. {
  159. const int stateIndex = stateNameToIndex (e->getStringAttribute ("buttonState"));
  160. paintRoutines [stateIndex]->loadFromXml (*e);
  161. paintStatesEnabled [stateIndex] = e->getBoolAttribute ("enabled", stateIndex < normalOn);
  162. }
  163. changed();
  164. getUndoManager().clearUndoHistory();
  165. return true;
  166. }
  167. return false;
  168. }
  169. void ButtonDocument::getOptionalMethods (StringArray& baseClasses,
  170. StringArray& returnValues,
  171. StringArray& methods,
  172. StringArray& initialContents) const
  173. {
  174. JucerDocument::getOptionalMethods (baseClasses, returnValues, methods, initialContents);
  175. addMethod ("Button", "void", "clicked()", "", baseClasses, returnValues, methods, initialContents);
  176. addMethod ("Button", "void", "buttonStateChanged()", "", baseClasses, returnValues, methods, initialContents);
  177. }
  178. //==============================================================================
  179. class ButtonStatePaintEnabledProperty : public BooleanPropertyComponent,
  180. private ChangeListener
  181. {
  182. public:
  183. ButtonStatePaintEnabledProperty (const String& name, ButtonDocument& doc, const int stateMethod_)
  184. : BooleanPropertyComponent (name, "enabled", "disabled"),
  185. document (doc),
  186. stateMethod (stateMethod_)
  187. {
  188. document.addChangeListener (this);
  189. }
  190. ~ButtonStatePaintEnabledProperty()
  191. {
  192. document.removeChangeListener (this);
  193. }
  194. void setState (bool newState)
  195. {
  196. document.setStatePaintRoutineEnabled (stateMethod, newState);
  197. }
  198. bool getState() const
  199. {
  200. return document.isStatePaintRoutineEnabled (stateMethod);
  201. }
  202. private:
  203. void changeListenerCallback (ChangeBroadcaster*)
  204. {
  205. refresh();
  206. }
  207. ButtonDocument& document;
  208. const int stateMethod;
  209. };
  210. void ButtonDocument::addExtraClassProperties (PropertyPanel& panel)
  211. {
  212. Array <PropertyComponent*> props;
  213. for (int i = 1; i < 7; ++i)
  214. props.add (new ButtonStatePaintEnabledProperty (stateNames[i], *this, i));
  215. panel.addSection ("Button paint routines", props);
  216. }
  217. //==============================================================================
  218. class ButtonTestComponent : public Button
  219. {
  220. public:
  221. ButtonTestComponent (ButtonDocument* const doc, const bool fillBackground)
  222. : Button (String()),
  223. document (doc),
  224. alwaysFillBackground (fillBackground)
  225. {
  226. setClickingTogglesState (true);
  227. }
  228. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  229. {
  230. if (document->paintStatesEnabled [background])
  231. {
  232. document->paintRoutines [background]->fillWithBackground (g, alwaysFillBackground);
  233. document->paintRoutines [background]->drawElements (g, getLocalBounds());
  234. }
  235. const int stateIndex
  236. = getToggleState()
  237. ? (isButtonDown ? document->chooseBestEnabledPaintRoutine (downOn)
  238. : (isMouseOverButton ? document->chooseBestEnabledPaintRoutine (overOn)
  239. : document->chooseBestEnabledPaintRoutine (normalOn)))
  240. : (isButtonDown ? document->chooseBestEnabledPaintRoutine (downOff)
  241. : (isMouseOverButton ? document->chooseBestEnabledPaintRoutine (overOff)
  242. : normalOff));
  243. document->paintRoutines [stateIndex]->fillWithBackground (g, ! document->paintStatesEnabled [background]);
  244. document->paintRoutines [stateIndex]->drawElements (g, getLocalBounds());
  245. }
  246. private:
  247. ButtonDocument* const document;
  248. const bool alwaysFillBackground;
  249. };
  250. Component* ButtonDocument::createTestComponent (const bool alwaysFillBackground)
  251. {
  252. return new ButtonTestComponent (this, alwaysFillBackground);
  253. }
  254. //==============================================================================
  255. void ButtonDocument::fillInGeneratedCode (GeneratedCode& code) const
  256. {
  257. JucerDocument::fillInGeneratedCode (code);
  258. code.parentClassInitialiser = "Button (" + quotedString (code.componentName, false) + ")";
  259. code.removeCallback ("void", "paint (Graphics& g)");
  260. }
  261. void ButtonDocument::fillInPaintCode (GeneratedCode& code) const
  262. {
  263. jassert (paintStatesEnabled [normalOff]);
  264. String paintCode [7];
  265. for (int i = 0; i < 7; ++i)
  266. if (paintStatesEnabled [i])
  267. paintRoutines[i]->fillInGeneratedCode (code, paintCode [i]);
  268. String& s = code.getCallbackCode ("public Button",
  269. "void",
  270. "paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)",
  271. false);
  272. int numPaintRoutines = getNumPaintRoutines();
  273. if (paintStatesEnabled [background])
  274. {
  275. s << paintCode [background] << "\n";
  276. --numPaintRoutines;
  277. }
  278. if (numPaintRoutines == 1)
  279. {
  280. s << paintCode [normalOff];
  281. }
  282. else if (numPaintRoutines == downOff && (paintStatesEnabled [overOff] || paintStatesEnabled [downOff] || paintStatesEnabled [normalOn]))
  283. {
  284. if (paintStatesEnabled [normalOn])
  285. {
  286. s << "if (getToggleState())\n{\n "
  287. << CodeHelpers::indent (paintCode [normalOn], 4, false).trimEnd();
  288. }
  289. else if (paintStatesEnabled [overOff])
  290. {
  291. s << "if (isButtonDown || isMouseOverButton)\n{\n "
  292. << CodeHelpers::indent (paintCode [overOff], 4, false).trimEnd();
  293. }
  294. else
  295. {
  296. s << "if (isButtonDown)\n{\n "
  297. << CodeHelpers::indent (paintCode [downOff], 4, false).trimEnd();
  298. }
  299. s << "\n}\nelse\n{\n "
  300. << CodeHelpers::indent (paintCode [normalOff], 4, false).trimEnd()
  301. << "\n}\n";
  302. }
  303. else if (numPaintRoutines == normalOn && paintStatesEnabled [overOff] && paintStatesEnabled [downOff])
  304. {
  305. s << "if (isButtonDown)\n{\n "
  306. << CodeHelpers::indent (paintCode [downOff], 4, false).trimEnd()
  307. << "\n}\nelse if (isMouseOverButton)\n{\n "
  308. << CodeHelpers::indent (paintCode [overOff], 4, false).trimEnd()
  309. << "\n}\nelse\n{\n "
  310. << CodeHelpers::indent (paintCode [normalOff], 4, false).trimEnd()
  311. << "\n}\n";
  312. }
  313. else
  314. {
  315. if (paintStatesEnabled [normalOn] || paintStatesEnabled [overOn] || paintStatesEnabled [downOn])
  316. {
  317. s << "switch (getToggleState() ? (isButtonDown ? "
  318. << chooseBestEnabledPaintRoutine (downOn) << " : (isMouseOverButton ? "
  319. << chooseBestEnabledPaintRoutine (overOn) << " : "
  320. << chooseBestEnabledPaintRoutine (normalOn) << "))\n : (isButtonDown ? "
  321. << chooseBestEnabledPaintRoutine (downOff) << " : (isMouseOverButton ? "
  322. << chooseBestEnabledPaintRoutine (overOff) << " : 0)))\n{\n";
  323. }
  324. else
  325. {
  326. s << "switch (isButtonDown ? " << chooseBestEnabledPaintRoutine (downOff)
  327. << " : (isMouseOverButton ? " << chooseBestEnabledPaintRoutine (overOff)
  328. << " : 0))\n{\n";
  329. }
  330. for (int i = 0; i < 6; ++i)
  331. {
  332. if (paintStatesEnabled [i])
  333. {
  334. s << "case " << i << ":\n {\n "
  335. << CodeHelpers::indent (paintCode [i], 8, false).trimEnd()
  336. << "\n break;\n }\n\n";
  337. }
  338. }
  339. s << "default:\n break;\n}\n";
  340. }
  341. }