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.

269 lines
8.9KB

  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. #pragma once
  18. #include "../ui/jucer_TestComponent.h"
  19. #include "../properties/jucer_FilePropertyComponent.h"
  20. #include "../properties/jucer_ComponentTextProperty.h"
  21. #include "jucer_ComponentUndoableAction.h"
  22. #include "../../Project/jucer_ProjectContentComponent.h"
  23. //==============================================================================
  24. class JucerComponentHandler : public ComponentTypeHandler
  25. {
  26. public:
  27. JucerComponentHandler()
  28. : ComponentTypeHandler ("Projucer Component", "xxx",
  29. typeid (TestComponent), 300, 200)
  30. {}
  31. Component* createNewComponent (JucerDocument* doc)
  32. {
  33. return new TestComponent (doc, 0, false);
  34. }
  35. String getXmlTagName() const noexcept { return "JUCERCOMP"; }
  36. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  37. {
  38. TestComponent* const tc = dynamic_cast<TestComponent*> (comp);
  39. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  40. e->setAttribute ("sourceFile", tc->getFilename());
  41. e->setAttribute ("constructorParams", tc->getConstructorParams());
  42. return e;
  43. }
  44. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  45. {
  46. TestComponent* const tc = dynamic_cast<TestComponent*> (comp);
  47. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  48. return false;
  49. tc->setFilename (xml.getStringAttribute ("sourceFile", tc->getFilename()));
  50. tc->setConstructorParams (xml.getStringAttribute ("constructorParams"));
  51. return true;
  52. }
  53. String getClassName (Component* comp) const
  54. {
  55. TestComponent* const tc = dynamic_cast<TestComponent*> (comp);
  56. String jucerCompClassName;
  57. if (tc->getDocument() != 0)
  58. jucerCompClassName = tc->getDocument()->getClassName();
  59. if (jucerCompClassName.isEmpty())
  60. jucerCompClassName = "Component";
  61. return jucerCompClassName;
  62. }
  63. void getEditableProperties (Component* component, JucerDocument& document, Array<PropertyComponent*>& props)
  64. {
  65. TestComponent* const tc = dynamic_cast<TestComponent*> (component);
  66. ComponentTypeHandler::getEditableProperties (component, document, props);
  67. props.add (new JucerCompFileProperty (tc, document));
  68. props.add (new ConstructorParamsProperty (tc, document));
  69. props.add (new JucerCompOpenDocProperty (tc));
  70. }
  71. String getCreationParameters (GeneratedCode&, Component* component)
  72. {
  73. return dynamic_cast<TestComponent*> (component)->getConstructorParams().trim();
  74. }
  75. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  76. {
  77. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  78. if (TestComponent* const tc = dynamic_cast<TestComponent*> (component))
  79. code.includeFilesH.add (tc->findFile().withFileExtension (".h"));
  80. else
  81. jassertfalse;
  82. }
  83. //==============================================================================
  84. class JucerCompFileChangeAction : public ComponentUndoableAction <TestComponent>
  85. {
  86. public:
  87. JucerCompFileChangeAction (TestComponent* const comp, ComponentLayout& l, const String& newState_)
  88. : ComponentUndoableAction <TestComponent> (comp, l),
  89. newState (newState_)
  90. {
  91. oldState = comp->getFilename();
  92. }
  93. bool perform()
  94. {
  95. showCorrectTab();
  96. getComponent()->setFilename (newState);
  97. changed();
  98. return true;
  99. }
  100. bool undo()
  101. {
  102. showCorrectTab();
  103. getComponent()->setFilename (oldState);
  104. changed();
  105. return true;
  106. }
  107. String newState, oldState;
  108. };
  109. static void setJucerComponentFile (JucerDocument& document, TestComponent* comp, const String& newFilename)
  110. {
  111. jassert (comp != nullptr);
  112. if (comp != nullptr)
  113. document.perform (new JucerCompFileChangeAction (comp, *document.getComponentLayout(), newFilename),
  114. "Change Projucer component file");
  115. }
  116. private:
  117. //==============================================================================
  118. class JucerCompFileProperty : public FilePropertyComponent,
  119. public ChangeListener
  120. {
  121. public:
  122. JucerCompFileProperty (TestComponent* const comp, JucerDocument& doc)
  123. : FilePropertyComponent ("Jucer file", false, true),
  124. component (comp),
  125. document (doc)
  126. {
  127. document.addChangeListener (this);
  128. }
  129. ~JucerCompFileProperty()
  130. {
  131. document.removeChangeListener (this);
  132. }
  133. void setFile (const File& newFile)
  134. {
  135. setJucerComponentFile (document, component,
  136. newFile.getRelativePathFrom (document.getCppFile().getParentDirectory())
  137. .replaceCharacter ('\\', '/'));
  138. }
  139. File getFile() const
  140. {
  141. return component->findFile();
  142. }
  143. void changeListenerCallback (ChangeBroadcaster*)
  144. {
  145. refresh();
  146. }
  147. private:
  148. TestComponent* const component;
  149. JucerDocument& document;
  150. };
  151. //==============================================================================
  152. struct JucerCompOpenDocProperty : public ButtonPropertyComponent
  153. {
  154. JucerCompOpenDocProperty (TestComponent* const c)
  155. : ButtonPropertyComponent ("edit", false),
  156. component (c)
  157. {
  158. }
  159. void buttonClicked()
  160. {
  161. if (ProjectContentComponent* const pcc = findParentComponentOfClass<ProjectContentComponent>())
  162. pcc->showEditorForFile (component->findFile(), true);
  163. }
  164. String getButtonText() const
  165. {
  166. return "Open file for editing";
  167. }
  168. TestComponent* const component;
  169. };
  170. //==============================================================================
  171. struct ConstructorParamsProperty : public ComponentTextProperty <TestComponent>
  172. {
  173. ConstructorParamsProperty (TestComponent* comp, JucerDocument& doc)
  174. : ComponentTextProperty <TestComponent> ("constructor params", 512, false, comp, doc)
  175. {
  176. }
  177. void setText (const String& newText) override
  178. {
  179. document.perform (new ConstructorParamChangeAction (component, *document.getComponentLayout(), newText),
  180. "Change Viewport content constructor params");
  181. }
  182. String getText() const override
  183. {
  184. return component->getConstructorParams();
  185. }
  186. private:
  187. struct ConstructorParamChangeAction : public ComponentUndoableAction <TestComponent>
  188. {
  189. ConstructorParamChangeAction (TestComponent* const comp, ComponentLayout& l, const String& newValue_)
  190. : ComponentUndoableAction <TestComponent> (comp, l),
  191. newValue (newValue_)
  192. {
  193. oldValue = comp->getConstructorParams();
  194. }
  195. bool perform()
  196. {
  197. showCorrectTab();
  198. getComponent()->setConstructorParams (newValue);
  199. changed();
  200. layout.getDocument()->refreshAllPropertyComps();
  201. return true;
  202. }
  203. bool undo()
  204. {
  205. showCorrectTab();
  206. getComponent()->setConstructorParams (oldValue);
  207. changed();
  208. layout.getDocument()->refreshAllPropertyComps();
  209. return true;
  210. }
  211. String newValue, oldValue;
  212. };
  213. };
  214. };