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.

273 lines
9.1KB

  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. #ifndef JUCER_JUCERCOMPONENTHANDLER_H_INCLUDED
  18. #define JUCER_JUCERCOMPONENTHANDLER_H_INCLUDED
  19. #include "../ui/jucer_TestComponent.h"
  20. #include "../properties/jucer_FilePropertyComponent.h"
  21. #include "../properties/jucer_ComponentTextProperty.h"
  22. #include "jucer_ComponentUndoableAction.h"
  23. #include "../../Project/jucer_ProjectContentComponent.h"
  24. //==============================================================================
  25. class JucerComponentHandler : public ComponentTypeHandler
  26. {
  27. public:
  28. JucerComponentHandler()
  29. : ComponentTypeHandler ("Projucer Component", "xxx",
  30. typeid (TestComponent), 300, 200)
  31. {}
  32. Component* createNewComponent (JucerDocument* doc)
  33. {
  34. return new TestComponent (doc, 0, false);
  35. }
  36. String getXmlTagName() const noexcept { return "JUCERCOMP"; }
  37. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  38. {
  39. TestComponent* const tc = dynamic_cast<TestComponent*> (comp);
  40. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  41. e->setAttribute ("sourceFile", tc->getFilename());
  42. e->setAttribute ("constructorParams", tc->getConstructorParams());
  43. return e;
  44. }
  45. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  46. {
  47. TestComponent* const tc = dynamic_cast<TestComponent*> (comp);
  48. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  49. return false;
  50. tc->setFilename (xml.getStringAttribute ("sourceFile", tc->getFilename()));
  51. tc->setConstructorParams (xml.getStringAttribute ("constructorParams"));
  52. return true;
  53. }
  54. String getClassName (Component* comp) const
  55. {
  56. TestComponent* const tc = dynamic_cast<TestComponent*> (comp);
  57. String jucerCompClassName;
  58. if (tc->getDocument() != 0)
  59. jucerCompClassName = tc->getDocument()->getClassName();
  60. if (jucerCompClassName.isEmpty())
  61. jucerCompClassName = "Component";
  62. return jucerCompClassName;
  63. }
  64. void getEditableProperties (Component* component, JucerDocument& document, Array<PropertyComponent*>& props)
  65. {
  66. TestComponent* const tc = dynamic_cast<TestComponent*> (component);
  67. ComponentTypeHandler::getEditableProperties (component, document, props);
  68. props.add (new JucerCompFileProperty (tc, document));
  69. props.add (new ConstructorParamsProperty (tc, document));
  70. props.add (new JucerCompOpenDocProperty (tc));
  71. }
  72. String getCreationParameters (GeneratedCode&, Component* component)
  73. {
  74. return dynamic_cast<TestComponent*> (component)->getConstructorParams().trim();
  75. }
  76. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  77. {
  78. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  79. if (TestComponent* const 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. };
  216. #endif // JUCER_JUCERCOMPONENTHANDLER_H_INCLUDED