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.

276 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_JUCEHEADER__
  18. #define __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__
  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 ("Introjucer 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. TestComponent* const tc = dynamic_cast <TestComponent*> (component);
  80. code.includeFilesH.add (tc->getFilename().replace (".cpp", ".h"));
  81. }
  82. //==============================================================================
  83. class JucerCompFileChangeAction : public ComponentUndoableAction <TestComponent>
  84. {
  85. public:
  86. JucerCompFileChangeAction (TestComponent* const comp, ComponentLayout& l, const String& newState_)
  87. : ComponentUndoableAction <TestComponent> (comp, l),
  88. newState (newState_)
  89. {
  90. oldState = comp->getFilename();
  91. }
  92. bool perform()
  93. {
  94. showCorrectTab();
  95. getComponent()->setFilename (newState);
  96. changed();
  97. return true;
  98. }
  99. bool undo()
  100. {
  101. showCorrectTab();
  102. getComponent()->setFilename (oldState);
  103. changed();
  104. return true;
  105. }
  106. String newState, oldState;
  107. };
  108. static void setJucerComponentFile (JucerDocument& document, TestComponent* comp, const String& newFilename)
  109. {
  110. jassert (comp != nullptr);
  111. if (comp != nullptr)
  112. document.perform (new JucerCompFileChangeAction (comp, *document.getComponentLayout(), newFilename),
  113. "Change Introjucer component file");
  114. }
  115. private:
  116. //==============================================================================
  117. class JucerCompFileProperty : public FilePropertyComponent,
  118. public ChangeListener
  119. {
  120. public:
  121. JucerCompFileProperty (TestComponent* const comp, JucerDocument& doc)
  122. : FilePropertyComponent ("Jucer file", false, true),
  123. component (comp),
  124. document (doc)
  125. {
  126. document.addChangeListener (this);
  127. }
  128. ~JucerCompFileProperty()
  129. {
  130. document.removeChangeListener (this);
  131. }
  132. void setFile (const File& newFile)
  133. {
  134. setJucerComponentFile (document, component,
  135. newFile.getRelativePathFrom (document.getCppFile().getParentDirectory())
  136. .replaceCharacter ('\\', '/'));
  137. }
  138. File getFile() const
  139. {
  140. return component->findFile();
  141. }
  142. void changeListenerCallback (ChangeBroadcaster*)
  143. {
  144. refresh();
  145. }
  146. private:
  147. TestComponent* const component;
  148. JucerDocument& document;
  149. };
  150. //==============================================================================
  151. class JucerCompOpenDocProperty : public ButtonPropertyComponent
  152. {
  153. public:
  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. private:
  169. TestComponent* const component;
  170. };
  171. //==============================================================================
  172. class ConstructorParamsProperty : public ComponentTextProperty <TestComponent>
  173. {
  174. public:
  175. ConstructorParamsProperty (TestComponent* comp, JucerDocument& doc)
  176. : ComponentTextProperty <TestComponent> ("constructor params", 512, false, comp, doc)
  177. {
  178. }
  179. void setText (const String& newText)
  180. {
  181. document.perform (new ConstructorParamChangeAction (component, *document.getComponentLayout(), newText),
  182. "Change Viewport content constructor params");
  183. }
  184. String getText() const
  185. {
  186. return component->getConstructorParams();
  187. }
  188. private:
  189. class ConstructorParamChangeAction : public ComponentUndoableAction <TestComponent>
  190. {
  191. public:
  192. ConstructorParamChangeAction (TestComponent* const comp, ComponentLayout& l, const String& newValue_)
  193. : ComponentUndoableAction <TestComponent> (comp, l),
  194. newValue (newValue_)
  195. {
  196. oldValue = comp->getConstructorParams();
  197. }
  198. bool perform()
  199. {
  200. showCorrectTab();
  201. getComponent()->setConstructorParams (newValue);
  202. changed();
  203. layout.getDocument()->refreshAllPropertyComps();
  204. return true;
  205. }
  206. bool undo()
  207. {
  208. showCorrectTab();
  209. getComponent()->setConstructorParams (oldValue);
  210. changed();
  211. layout.getDocument()->refreshAllPropertyComps();
  212. return true;
  213. }
  214. String newValue, oldValue;
  215. };
  216. };
  217. };
  218. #endif // __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__