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.

278 lines
9.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__
  19. #define __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__
  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 ("Jucer 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*>& properties)
  66. {
  67. TestComponent* const tc = dynamic_cast <TestComponent*> (component);
  68. ComponentTypeHandler::getEditableProperties (component, document, properties);
  69. properties.add (new JucerCompFileProperty (tc, document));
  70. properties.add (new ConstructorParamsProperty (tc, document));
  71. properties.add (new JucerCompOpenDocProperty (tc));
  72. }
  73. String getCreationParameters (Component* component)
  74. {
  75. TestComponent* const tc = dynamic_cast <TestComponent*> (component);
  76. return tc->getConstructorParams().trim();
  77. }
  78. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  79. {
  80. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  81. TestComponent* const tc = dynamic_cast <TestComponent*> (component);
  82. code.includeFilesH.add (tc->getFilename().replace (".cpp", ".h"));
  83. }
  84. //==============================================================================
  85. class JucerCompFileChangeAction : public ComponentUndoableAction <TestComponent>
  86. {
  87. public:
  88. JucerCompFileChangeAction (TestComponent* const comp, ComponentLayout& layout, const String& newState_)
  89. : ComponentUndoableAction <TestComponent> (comp, layout),
  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 != 0);
  113. if (comp != 0)
  114. document.perform (new JucerCompFileChangeAction (comp, *document.getComponentLayout(), newFilename),
  115. "Change Jucer 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. class JucerCompOpenDocProperty : public ButtonPropertyComponent
  154. {
  155. public:
  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. private:
  171. TestComponent* const component;
  172. };
  173. //==============================================================================
  174. class ConstructorParamsProperty : public ComponentTextProperty <TestComponent>
  175. {
  176. public:
  177. ConstructorParamsProperty (TestComponent* comp, JucerDocument& document)
  178. : ComponentTextProperty <TestComponent> ("constructor params", 512, false, comp, document)
  179. {
  180. }
  181. void setText (const String& newText)
  182. {
  183. document.perform (new ConstructorParamChangeAction (component, *document.getComponentLayout(), newText),
  184. "Change Viewport content constructor params");
  185. }
  186. String getText() const
  187. {
  188. return component->getConstructorParams();
  189. }
  190. private:
  191. class ConstructorParamChangeAction : public ComponentUndoableAction <TestComponent>
  192. {
  193. public:
  194. ConstructorParamChangeAction (TestComponent* const comp, ComponentLayout& layout, const String& newValue_)
  195. : ComponentUndoableAction <TestComponent> (comp, layout),
  196. newValue (newValue_)
  197. {
  198. oldValue = comp->getConstructorParams();
  199. }
  200. bool perform()
  201. {
  202. showCorrectTab();
  203. getComponent()->setConstructorParams (newValue);
  204. changed();
  205. layout.getDocument()->refreshAllPropertyComps();
  206. return true;
  207. }
  208. bool undo()
  209. {
  210. showCorrectTab();
  211. getComponent()->setConstructorParams (oldValue);
  212. changed();
  213. layout.getDocument()->refreshAllPropertyComps();
  214. return true;
  215. }
  216. String newValue, oldValue;
  217. };
  218. };
  219. };
  220. #endif // __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__