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.

234 lines
7.9KB

  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. class GenericComponent : public Component
  19. {
  20. public:
  21. GenericComponent()
  22. : Component ("new component"),
  23. actualClassName ("Component")
  24. {
  25. }
  26. void paint (Graphics& g)
  27. {
  28. g.fillAll (Colours::white.withAlpha (0.25f));
  29. g.setColour (Colours::black.withAlpha (0.5f));
  30. g.drawRect (getLocalBounds());
  31. g.drawLine (0.0f, 0.0f, (float) getWidth(), (float) getHeight());
  32. g.drawLine (0.0f, (float) getHeight(), (float) getWidth(), 0.0f);
  33. g.setFont (14.0f);
  34. g.drawText (actualClassName, 0, 0, getWidth(), getHeight() / 2, Justification::centred, true);
  35. }
  36. void setClassName (const String& newName)
  37. {
  38. if (actualClassName != newName)
  39. {
  40. actualClassName = newName;
  41. repaint();
  42. }
  43. }
  44. void setParams (const String& newParams)
  45. {
  46. if (constructorParams != newParams)
  47. {
  48. constructorParams = newParams;
  49. repaint();
  50. }
  51. }
  52. String actualClassName, constructorParams;
  53. };
  54. //==============================================================================
  55. class GenericComponentHandler : public ComponentTypeHandler
  56. {
  57. public:
  58. GenericComponentHandler()
  59. : ComponentTypeHandler ("Generic Component", "GenericComponent", typeid (GenericComponent), 150, 24)
  60. {}
  61. Component* createNewComponent (JucerDocument*)
  62. {
  63. return new GenericComponent();
  64. }
  65. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  66. {
  67. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  68. e->setAttribute ("class", ((GenericComponent*) comp)->actualClassName);
  69. e->setAttribute ("params", ((GenericComponent*) comp)->constructorParams);
  70. return e;
  71. }
  72. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  73. {
  74. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  75. return false;
  76. ((GenericComponent*) comp)->actualClassName = xml.getStringAttribute ("class", "Component");
  77. ((GenericComponent*) comp)->constructorParams = xml.getStringAttribute ("params", String::empty);
  78. return true;
  79. }
  80. void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
  81. {
  82. ComponentTypeHandler::getEditableProperties (component, document, properties);
  83. properties.add (new GenericCompClassProperty (dynamic_cast <GenericComponent*> (component), document));
  84. properties.add (new GenericCompParamsProperty (dynamic_cast <GenericComponent*> (component), document));
  85. }
  86. String getClassName (Component* comp) const
  87. {
  88. return ((GenericComponent*) comp)->actualClassName;
  89. }
  90. String getCreationParameters (Component* comp)
  91. {
  92. return ((GenericComponent*) comp)->constructorParams;
  93. }
  94. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  95. {
  96. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  97. if (component->getName().isNotEmpty())
  98. code.constructorCode
  99. << memberVariableName << "->setName ("
  100. << quotedString (component->getName())
  101. << ");\n\n";
  102. else
  103. code.constructorCode << "\n";
  104. }
  105. private:
  106. class GenericCompClassProperty : public ComponentTextProperty <GenericComponent>
  107. {
  108. public:
  109. GenericCompClassProperty (GenericComponent* comp, JucerDocument& document)
  110. : ComponentTextProperty <GenericComponent> ("class", 300, false, comp, document)
  111. {
  112. }
  113. void setText (const String& newText)
  114. {
  115. document.perform (new GenericCompClassChangeAction (component, *document.getComponentLayout(),
  116. CodeHelpers::makeValidIdentifier (newText, false, false, true)),
  117. "Change generic component class");
  118. }
  119. String getText() const
  120. {
  121. return component->actualClassName;
  122. }
  123. private:
  124. class GenericCompClassChangeAction : public ComponentUndoableAction <GenericComponent>
  125. {
  126. public:
  127. GenericCompClassChangeAction (GenericComponent* const comp, ComponentLayout& layout, const String& newState_)
  128. : ComponentUndoableAction <GenericComponent> (comp, layout),
  129. newState (newState_)
  130. {
  131. oldState = comp->actualClassName;
  132. }
  133. bool perform()
  134. {
  135. showCorrectTab();
  136. getComponent()->setClassName (newState);
  137. changed();
  138. return true;
  139. }
  140. bool undo()
  141. {
  142. showCorrectTab();
  143. getComponent()->setClassName (oldState);
  144. changed();
  145. return true;
  146. }
  147. String newState, oldState;
  148. };
  149. };
  150. class GenericCompParamsProperty : public ComponentTextProperty <GenericComponent>
  151. {
  152. public:
  153. GenericCompParamsProperty (GenericComponent* comp, JucerDocument& document)
  154. : ComponentTextProperty <GenericComponent> ("constructor params", 1024, true, comp, document)
  155. {
  156. }
  157. void setText (const String& newText)
  158. {
  159. document.perform (new GenericCompParamsChangeAction (component, *document.getComponentLayout(), newText),
  160. "Change generic component class");
  161. }
  162. String getText() const
  163. {
  164. return component->constructorParams;
  165. }
  166. private:
  167. class GenericCompParamsChangeAction : public ComponentUndoableAction <GenericComponent>
  168. {
  169. public:
  170. GenericCompParamsChangeAction (GenericComponent* const comp, ComponentLayout& layout, const String& newState_)
  171. : ComponentUndoableAction <GenericComponent> (comp, layout),
  172. newState (newState_)
  173. {
  174. oldState = comp->constructorParams;
  175. }
  176. bool perform()
  177. {
  178. showCorrectTab();
  179. getComponent()->setParams (newState);
  180. changed();
  181. return true;
  182. }
  183. bool undo()
  184. {
  185. showCorrectTab();
  186. getComponent()->setParams (oldState);
  187. changed();
  188. return true;
  189. }
  190. String newState, oldState;
  191. };
  192. };
  193. };