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.

271 lines
8.9KB

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