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.

233 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class GenericComponent : public Component
  18. {
  19. public:
  20. GenericComponent()
  21. : Component ("new component"),
  22. actualClassName ("Component")
  23. {
  24. }
  25. void paint (Graphics& g) override
  26. {
  27. g.fillAll (Colours::white.withAlpha (0.25f));
  28. g.setColour (Colours::black.withAlpha (0.5f));
  29. g.drawRect (getLocalBounds());
  30. g.drawLine (0.0f, 0.0f, (float) getWidth(), (float) getHeight());
  31. g.drawLine (0.0f, (float) getHeight(), (float) getWidth(), 0.0f);
  32. g.setFont (14.0f);
  33. g.drawText (actualClassName, 0, 0, getWidth(), getHeight() / 2, Justification::centred, true);
  34. }
  35. void setClassName (const String& newName)
  36. {
  37. if (actualClassName != newName)
  38. {
  39. actualClassName = newName;
  40. repaint();
  41. }
  42. }
  43. void setParams (const String& newParams)
  44. {
  45. if (constructorParams != newParams)
  46. {
  47. constructorParams = newParams;
  48. repaint();
  49. }
  50. }
  51. String actualClassName, constructorParams;
  52. };
  53. //==============================================================================
  54. class GenericComponentHandler : public ComponentTypeHandler
  55. {
  56. public:
  57. GenericComponentHandler()
  58. : ComponentTypeHandler ("Generic Component", "GenericComponent", typeid (GenericComponent), 150, 24)
  59. {}
  60. Component* createNewComponent (JucerDocument*)
  61. {
  62. return new GenericComponent();
  63. }
  64. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  65. {
  66. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  67. e->setAttribute ("class", ((GenericComponent*) comp)->actualClassName);
  68. e->setAttribute ("params", ((GenericComponent*) comp)->constructorParams);
  69. return e;
  70. }
  71. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  72. {
  73. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  74. return false;
  75. ((GenericComponent*) comp)->actualClassName = xml.getStringAttribute ("class", "Component");
  76. ((GenericComponent*) comp)->constructorParams = xml.getStringAttribute ("params", String());
  77. return true;
  78. }
  79. void getEditableProperties (Component* component, JucerDocument& document, Array<PropertyComponent*>& props)
  80. {
  81. ComponentTypeHandler::getEditableProperties (component, document, props);
  82. props.add (new GenericCompClassProperty (dynamic_cast<GenericComponent*> (component), document));
  83. props.add (new GenericCompParamsProperty (dynamic_cast<GenericComponent*> (component), document));
  84. }
  85. String getClassName (Component* comp) const
  86. {
  87. return static_cast<GenericComponent*> (comp)->actualClassName;
  88. }
  89. String getCreationParameters (GeneratedCode&, Component* comp)
  90. {
  91. return static_cast<GenericComponent*> (comp)->constructorParams;
  92. }
  93. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  94. {
  95. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  96. if (component->getName().isNotEmpty())
  97. code.constructorCode
  98. << memberVariableName << "->setName ("
  99. << quotedString (component->getName(), false)
  100. << ");\n\n";
  101. else
  102. code.constructorCode << "\n";
  103. }
  104. private:
  105. class GenericCompClassProperty : public ComponentTextProperty <GenericComponent>
  106. {
  107. public:
  108. GenericCompClassProperty (GenericComponent* comp, JucerDocument& doc)
  109. : ComponentTextProperty <GenericComponent> ("class", 300, false, comp, doc)
  110. {
  111. }
  112. void setText (const String& newText) override
  113. {
  114. document.perform (new GenericCompClassChangeAction (component, *document.getComponentLayout(),
  115. CodeHelpers::makeValidIdentifier (newText, false, false, true)),
  116. "Change generic component class");
  117. }
  118. String getText() const override
  119. {
  120. return component->actualClassName;
  121. }
  122. private:
  123. class GenericCompClassChangeAction : public ComponentUndoableAction <GenericComponent>
  124. {
  125. public:
  126. GenericCompClassChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  127. : ComponentUndoableAction <GenericComponent> (comp, l),
  128. newState (newState_)
  129. {
  130. oldState = comp->actualClassName;
  131. }
  132. bool perform()
  133. {
  134. showCorrectTab();
  135. getComponent()->setClassName (newState);
  136. changed();
  137. return true;
  138. }
  139. bool undo()
  140. {
  141. showCorrectTab();
  142. getComponent()->setClassName (oldState);
  143. changed();
  144. return true;
  145. }
  146. String newState, oldState;
  147. };
  148. };
  149. class GenericCompParamsProperty : public ComponentTextProperty <GenericComponent>
  150. {
  151. public:
  152. GenericCompParamsProperty (GenericComponent* comp, JucerDocument& doc)
  153. : ComponentTextProperty <GenericComponent> ("constructor params", 1024, true, comp, doc)
  154. {
  155. }
  156. void setText (const String& newText) override
  157. {
  158. document.perform (new GenericCompParamsChangeAction (component, *document.getComponentLayout(), newText),
  159. "Change generic component class");
  160. }
  161. String getText() const override
  162. {
  163. return component->constructorParams;
  164. }
  165. private:
  166. class GenericCompParamsChangeAction : public ComponentUndoableAction <GenericComponent>
  167. {
  168. public:
  169. GenericCompParamsChangeAction (GenericComponent* const comp, ComponentLayout& l, const String& newState_)
  170. : ComponentUndoableAction <GenericComponent> (comp, l),
  171. newState (newState_)
  172. {
  173. oldState = comp->constructorParams;
  174. }
  175. bool perform()
  176. {
  177. showCorrectTab();
  178. getComponent()->setParams (newState);
  179. changed();
  180. return true;
  181. }
  182. bool undo()
  183. {
  184. showCorrectTab();
  185. getComponent()->setParams (oldState);
  186. changed();
  187. return true;
  188. }
  189. String newState, oldState;
  190. };
  191. };
  192. };