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.

243 lines
7.9KB

  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. #pragma once
  20. //==============================================================================
  21. class GenericComponent : public Component
  22. {
  23. public:
  24. GenericComponent()
  25. : Component ("new component"),
  26. actualClassName ("Component")
  27. {
  28. }
  29. void paint (Graphics& g) override
  30. {
  31. g.fillAll (Colours::white.withAlpha (0.25f));
  32. g.setColour (Colours::black.withAlpha (0.5f));
  33. g.drawRect (getLocalBounds());
  34. g.drawLine (0.0f, 0.0f, (float) getWidth(), (float) getHeight());
  35. g.drawLine (0.0f, (float) getHeight(), (float) getWidth(), 0.0f);
  36. g.setFont (14.0f);
  37. g.drawText (actualClassName, 0, 0, getWidth(), getHeight() / 2, Justification::centred, true);
  38. }
  39. void setClassName (const String& newName)
  40. {
  41. if (actualClassName != newName)
  42. {
  43. actualClassName = newName;
  44. repaint();
  45. }
  46. }
  47. void setParams (const String& newParams)
  48. {
  49. if (constructorParams != newParams)
  50. {
  51. constructorParams = newParams;
  52. repaint();
  53. }
  54. }
  55. String actualClassName, constructorParams;
  56. };
  57. //==============================================================================
  58. class GenericComponentHandler : public ComponentTypeHandler
  59. {
  60. public:
  61. GenericComponentHandler()
  62. : ComponentTypeHandler ("Generic Component", "GenericComponent", typeid (GenericComponent), 150, 24)
  63. {}
  64. Component* createNewComponent (JucerDocument*) override
  65. {
  66. return new GenericComponent();
  67. }
  68. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout) override
  69. {
  70. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  71. e->setAttribute ("class", ((GenericComponent*) comp)->actualClassName);
  72. e->setAttribute ("params", ((GenericComponent*) comp)->constructorParams);
  73. return e;
  74. }
  75. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout) override
  76. {
  77. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  78. return false;
  79. ((GenericComponent*) comp)->actualClassName = xml.getStringAttribute ("class", "Component");
  80. ((GenericComponent*) comp)->constructorParams = xml.getStringAttribute ("params", String());
  81. return true;
  82. }
  83. void getEditableProperties (Component* component, JucerDocument& document,
  84. Array<PropertyComponent*>& props, bool multipleSelected) override
  85. {
  86. ComponentTypeHandler::getEditableProperties (component, document, props, multipleSelected);
  87. if (multipleSelected)
  88. return;
  89. props.add (new GenericCompClassProperty (dynamic_cast<GenericComponent*> (component), document));
  90. props.add (new GenericCompParamsProperty (dynamic_cast<GenericComponent*> (component), document));
  91. }
  92. String getClassName (Component* comp) const override
  93. {
  94. return static_cast<GenericComponent*> (comp)->actualClassName;
  95. }
  96. String getCreationParameters (GeneratedCode&, Component* comp) override
  97. {
  98. return static_cast<GenericComponent*> (comp)->constructorParams;
  99. }
  100. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName) override
  101. {
  102. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  103. if (component->getName().isNotEmpty())
  104. code.constructorCode
  105. << memberVariableName << "->setName ("
  106. << quotedString (component->getName(), false)
  107. << ");\n\n";
  108. else
  109. code.constructorCode << "\n";
  110. }
  111. private:
  112. class GenericCompClassProperty : public ComponentTextProperty <GenericComponent>
  113. {
  114. public:
  115. GenericCompClassProperty (GenericComponent* comp, JucerDocument& doc)
  116. : ComponentTextProperty <GenericComponent> ("class", 300, false, comp, doc)
  117. {
  118. }
  119. void setText (const String& newText) override
  120. {
  121. document.perform (new GenericCompClassChangeAction (component, *document.getComponentLayout(),
  122. CodeHelpers::makeValidIdentifier (newText, false, false, true)),
  123. "Change generic component class");
  124. }
  125. String getText() const override
  126. {
  127. return component->actualClassName;
  128. }
  129. private:
  130. class GenericCompClassChangeAction : public ComponentUndoableAction <GenericComponent>
  131. {
  132. public:
  133. GenericCompClassChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  134. : ComponentUndoableAction <GenericComponent> (comp, l),
  135. newState (newState_)
  136. {
  137. oldState = comp->actualClassName;
  138. }
  139. bool perform()
  140. {
  141. showCorrectTab();
  142. getComponent()->setClassName (newState);
  143. changed();
  144. return true;
  145. }
  146. bool undo()
  147. {
  148. showCorrectTab();
  149. getComponent()->setClassName (oldState);
  150. changed();
  151. return true;
  152. }
  153. String newState, oldState;
  154. };
  155. };
  156. class GenericCompParamsProperty : public ComponentTextProperty <GenericComponent>
  157. {
  158. public:
  159. GenericCompParamsProperty (GenericComponent* comp, JucerDocument& doc)
  160. : ComponentTextProperty <GenericComponent> ("constructor params", 1024, true, comp, doc)
  161. {
  162. }
  163. void setText (const String& newText) override
  164. {
  165. document.perform (new GenericCompParamsChangeAction (component, *document.getComponentLayout(), newText),
  166. "Change generic component class");
  167. }
  168. String getText() const override
  169. {
  170. return component->constructorParams;
  171. }
  172. private:
  173. class GenericCompParamsChangeAction : public ComponentUndoableAction <GenericComponent>
  174. {
  175. public:
  176. GenericCompParamsChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  177. : ComponentUndoableAction <GenericComponent> (comp, l),
  178. newState (newState_)
  179. {
  180. oldState = comp->constructorParams;
  181. }
  182. bool perform()
  183. {
  184. showCorrectTab();
  185. getComponent()->setParams (newState);
  186. changed();
  187. return true;
  188. }
  189. bool undo()
  190. {
  191. showCorrectTab();
  192. getComponent()->setParams (oldState);
  193. changed();
  194. return true;
  195. }
  196. String newState, oldState;
  197. };
  198. };
  199. };