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 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*)
  63. {
  64. return new GenericComponent();
  65. }
  66. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  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)
  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, Array<PropertyComponent*>& props)
  82. {
  83. ComponentTypeHandler::getEditableProperties (component, document, props);
  84. props.add (new GenericCompClassProperty (dynamic_cast<GenericComponent*> (component), document));
  85. props.add (new GenericCompParamsProperty (dynamic_cast<GenericComponent*> (component), document));
  86. }
  87. String getClassName (Component* comp) const
  88. {
  89. return static_cast<GenericComponent*> (comp)->actualClassName;
  90. }
  91. String getCreationParameters (GeneratedCode&, Component* comp)
  92. {
  93. return static_cast<GenericComponent*> (comp)->constructorParams;
  94. }
  95. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  96. {
  97. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  98. if (component->getName().isNotEmpty())
  99. code.constructorCode
  100. << memberVariableName << "->setName ("
  101. << quotedString (component->getName(), false)
  102. << ");\n\n";
  103. else
  104. code.constructorCode << "\n";
  105. }
  106. private:
  107. class GenericCompClassProperty : public ComponentTextProperty <GenericComponent>
  108. {
  109. public:
  110. GenericCompClassProperty (GenericComponent* comp, JucerDocument& doc)
  111. : ComponentTextProperty <GenericComponent> ("class", 300, false, comp, doc)
  112. {
  113. }
  114. void setText (const String& newText) override
  115. {
  116. document.perform (new GenericCompClassChangeAction (component, *document.getComponentLayout(),
  117. CodeHelpers::makeValidIdentifier (newText, false, false, true)),
  118. "Change generic component class");
  119. }
  120. String getText() const override
  121. {
  122. return component->actualClassName;
  123. }
  124. private:
  125. class GenericCompClassChangeAction : public ComponentUndoableAction <GenericComponent>
  126. {
  127. public:
  128. GenericCompClassChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  129. : ComponentUndoableAction <GenericComponent> (comp, l),
  130. newState (newState_)
  131. {
  132. oldState = comp->actualClassName;
  133. }
  134. bool perform()
  135. {
  136. showCorrectTab();
  137. getComponent()->setClassName (newState);
  138. changed();
  139. return true;
  140. }
  141. bool undo()
  142. {
  143. showCorrectTab();
  144. getComponent()->setClassName (oldState);
  145. changed();
  146. return true;
  147. }
  148. String newState, oldState;
  149. };
  150. };
  151. class GenericCompParamsProperty : public ComponentTextProperty <GenericComponent>
  152. {
  153. public:
  154. GenericCompParamsProperty (GenericComponent* comp, JucerDocument& doc)
  155. : ComponentTextProperty <GenericComponent> ("constructor params", 1024, true, comp, doc)
  156. {
  157. }
  158. void setText (const String& newText) override
  159. {
  160. document.perform (new GenericCompParamsChangeAction (component, *document.getComponentLayout(), newText),
  161. "Change generic component class");
  162. }
  163. String getText() const override
  164. {
  165. return component->constructorParams;
  166. }
  167. private:
  168. class GenericCompParamsChangeAction : public ComponentUndoableAction <GenericComponent>
  169. {
  170. public:
  171. GenericCompParamsChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  172. : ComponentUndoableAction <GenericComponent> (comp, l),
  173. newState (newState_)
  174. {
  175. oldState = comp->constructorParams;
  176. }
  177. bool perform()
  178. {
  179. showCorrectTab();
  180. getComponent()->setParams (newState);
  181. changed();
  182. return true;
  183. }
  184. bool undo()
  185. {
  186. showCorrectTab();
  187. getComponent()->setParams (oldState);
  188. changed();
  189. return true;
  190. }
  191. String newState, oldState;
  192. };
  193. };
  194. };