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.

242 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. class GenericComponent : public Component
  21. {
  22. public:
  23. GenericComponent()
  24. : Component ("new component"),
  25. actualClassName ("juce::Component")
  26. {
  27. }
  28. void paint (Graphics& g) override
  29. {
  30. g.fillAll (Colours::white.withAlpha (0.25f));
  31. g.setColour (Colours::black.withAlpha (0.5f));
  32. g.drawRect (getLocalBounds());
  33. g.drawLine (0.0f, 0.0f, (float) getWidth(), (float) getHeight());
  34. g.drawLine (0.0f, (float) getHeight(), (float) getWidth(), 0.0f);
  35. g.setFont (14.0f);
  36. g.drawText (actualClassName, 0, 0, getWidth(), getHeight() / 2, Justification::centred, true);
  37. }
  38. void setClassName (const String& newName)
  39. {
  40. if (actualClassName != newName)
  41. {
  42. actualClassName = newName;
  43. repaint();
  44. }
  45. }
  46. void setParams (const String& newParams)
  47. {
  48. if (constructorParams != newParams)
  49. {
  50. constructorParams = newParams;
  51. repaint();
  52. }
  53. }
  54. String actualClassName, constructorParams;
  55. };
  56. //==============================================================================
  57. class GenericComponentHandler : public ComponentTypeHandler
  58. {
  59. public:
  60. GenericComponentHandler()
  61. : ComponentTypeHandler ("Generic Component", "GenericComponent", typeid (GenericComponent), 150, 24)
  62. {}
  63. Component* createNewComponent (JucerDocument*) override
  64. {
  65. return new GenericComponent();
  66. }
  67. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout) override
  68. {
  69. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  70. e->setAttribute ("class", ((GenericComponent*) comp)->actualClassName);
  71. e->setAttribute ("params", ((GenericComponent*) comp)->constructorParams);
  72. return e;
  73. }
  74. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout) override
  75. {
  76. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  77. return false;
  78. ((GenericComponent*) comp)->actualClassName = xml.getStringAttribute ("class", "juce::Component");
  79. ((GenericComponent*) comp)->constructorParams = xml.getStringAttribute ("params", String());
  80. return true;
  81. }
  82. void getEditableProperties (Component* component, JucerDocument& document,
  83. Array<PropertyComponent*>& props, bool multipleSelected) override
  84. {
  85. ComponentTypeHandler::getEditableProperties (component, document, props, multipleSelected);
  86. if (multipleSelected)
  87. return;
  88. props.add (new GenericCompClassProperty (dynamic_cast<GenericComponent*> (component), document));
  89. props.add (new GenericCompParamsProperty (dynamic_cast<GenericComponent*> (component), document));
  90. }
  91. String getClassName (Component* comp) const override
  92. {
  93. return static_cast<GenericComponent*> (comp)->actualClassName;
  94. }
  95. String getCreationParameters (GeneratedCode&, Component* comp) override
  96. {
  97. return static_cast<GenericComponent*> (comp)->constructorParams;
  98. }
  99. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName) override
  100. {
  101. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  102. if (component->getName().isNotEmpty())
  103. code.constructorCode
  104. << memberVariableName << "->setName ("
  105. << quotedString (component->getName(), false)
  106. << ");\n\n";
  107. else
  108. code.constructorCode << "\n";
  109. }
  110. private:
  111. class GenericCompClassProperty : public ComponentTextProperty <GenericComponent>
  112. {
  113. public:
  114. GenericCompClassProperty (GenericComponent* comp, JucerDocument& doc)
  115. : ComponentTextProperty <GenericComponent> ("class", 300, false, comp, doc)
  116. {
  117. }
  118. void setText (const String& newText) override
  119. {
  120. document.perform (new GenericCompClassChangeAction (component, *document.getComponentLayout(),
  121. build_tools::makeValidIdentifier (newText, false, false, true)),
  122. "Change generic component class");
  123. }
  124. String getText() const override
  125. {
  126. return component->actualClassName;
  127. }
  128. private:
  129. class GenericCompClassChangeAction : public ComponentUndoableAction <GenericComponent>
  130. {
  131. public:
  132. GenericCompClassChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  133. : ComponentUndoableAction <GenericComponent> (comp, l),
  134. newState (newState_)
  135. {
  136. oldState = comp->actualClassName;
  137. }
  138. bool perform()
  139. {
  140. showCorrectTab();
  141. getComponent()->setClassName (newState);
  142. changed();
  143. return true;
  144. }
  145. bool undo()
  146. {
  147. showCorrectTab();
  148. getComponent()->setClassName (oldState);
  149. changed();
  150. return true;
  151. }
  152. String newState, oldState;
  153. };
  154. };
  155. class GenericCompParamsProperty : public ComponentTextProperty <GenericComponent>
  156. {
  157. public:
  158. GenericCompParamsProperty (GenericComponent* comp, JucerDocument& doc)
  159. : ComponentTextProperty <GenericComponent> ("constructor params", 1024, true, comp, doc)
  160. {
  161. }
  162. void setText (const String& newText) override
  163. {
  164. document.perform (new GenericCompParamsChangeAction (component, *document.getComponentLayout(), newText),
  165. "Change generic component class");
  166. }
  167. String getText() const override
  168. {
  169. return component->constructorParams;
  170. }
  171. private:
  172. class GenericCompParamsChangeAction : public ComponentUndoableAction <GenericComponent>
  173. {
  174. public:
  175. GenericCompParamsChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  176. : ComponentUndoableAction <GenericComponent> (comp, l),
  177. newState (newState_)
  178. {
  179. oldState = comp->constructorParams;
  180. }
  181. bool perform()
  182. {
  183. showCorrectTab();
  184. getComponent()->setParams (newState);
  185. changed();
  186. return true;
  187. }
  188. bool undo()
  189. {
  190. showCorrectTab();
  191. getComponent()->setParams (oldState);
  192. changed();
  193. return true;
  194. }
  195. String newState, oldState;
  196. };
  197. };
  198. };