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.

277 lines
9.2KB

  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 ("Jucer 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*>& properties)
  65. {
  66. TestComponent* const tc = dynamic_cast <TestComponent*> (component);
  67. ComponentTypeHandler::getEditableProperties (component, document, properties);
  68. properties.add (new JucerCompFileProperty (tc, document));
  69. properties.add (new ConstructorParamsProperty (tc, document));
  70. properties.add (new JucerCompOpenDocProperty (tc));
  71. }
  72. String getCreationParameters (Component* component)
  73. {
  74. TestComponent* const tc = dynamic_cast <TestComponent*> (component);
  75. return tc->getConstructorParams().trim();
  76. }
  77. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  78. {
  79. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  80. TestComponent* const tc = dynamic_cast <TestComponent*> (component);
  81. code.includeFilesH.add (tc->getFilename().replace (".cpp", ".h"));
  82. }
  83. //==============================================================================
  84. class JucerCompFileChangeAction : public ComponentUndoableAction <TestComponent>
  85. {
  86. public:
  87. JucerCompFileChangeAction (TestComponent* const comp, ComponentLayout& layout, const String& newState_)
  88. : ComponentUndoableAction <TestComponent> (comp, layout),
  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 != 0);
  112. if (comp != 0)
  113. document.perform (new JucerCompFileChangeAction (comp, *document.getComponentLayout(), newFilename),
  114. "Change Jucer 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. class JucerCompOpenDocProperty : public ButtonPropertyComponent
  153. {
  154. public:
  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. private:
  170. TestComponent* const component;
  171. };
  172. //==============================================================================
  173. class ConstructorParamsProperty : public ComponentTextProperty <TestComponent>
  174. {
  175. public:
  176. ConstructorParamsProperty (TestComponent* comp, JucerDocument& document)
  177. : ComponentTextProperty <TestComponent> ("constructor params", 512, false, comp, document)
  178. {
  179. }
  180. void setText (const String& newText)
  181. {
  182. document.perform (new ConstructorParamChangeAction (component, *document.getComponentLayout(), newText),
  183. "Change Viewport content constructor params");
  184. }
  185. String getText() const
  186. {
  187. return component->getConstructorParams();
  188. }
  189. private:
  190. class ConstructorParamChangeAction : public ComponentUndoableAction <TestComponent>
  191. {
  192. public:
  193. ConstructorParamChangeAction (TestComponent* const comp, ComponentLayout& layout, const String& newValue_)
  194. : ComponentUndoableAction <TestComponent> (comp, layout),
  195. newValue (newValue_)
  196. {
  197. oldValue = comp->getConstructorParams();
  198. }
  199. bool perform()
  200. {
  201. showCorrectTab();
  202. getComponent()->setConstructorParams (newValue);
  203. changed();
  204. layout.getDocument()->refreshAllPropertyComps();
  205. return true;
  206. }
  207. bool undo()
  208. {
  209. showCorrectTab();
  210. getComponent()->setConstructorParams (oldValue);
  211. changed();
  212. layout.getDocument()->refreshAllPropertyComps();
  213. return true;
  214. }
  215. String newValue, oldValue;
  216. };
  217. };
  218. };
  219. #endif // __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__