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.

239 lines
7.8KB

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