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.

267 lines
8.8KB

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