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.

235 lines
7.7KB

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