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.

228 lines
7.8KB

  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 GroupComponentHandler : public ComponentTypeHandler
  19. {
  20. public:
  21. GroupComponentHandler()
  22. : ComponentTypeHandler ("Group Box", "GroupComponent", typeid (GroupComponent), 200, 150)
  23. {
  24. registerColour (GroupComponent::outlineColourId, "outline", "outlinecol");
  25. registerColour (GroupComponent::textColourId, "text", "textcol");
  26. }
  27. Component* createNewComponent (JucerDocument*)
  28. {
  29. return new GroupComponent ("new group", "group");
  30. }
  31. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  32. {
  33. GroupComponent* const g = (GroupComponent*) comp;
  34. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  35. e->setAttribute ("title", g->getText());
  36. GroupComponent defaultComp (String::empty, String::empty);
  37. if (g->getTextLabelPosition().getFlags() != defaultComp.getTextLabelPosition().getFlags())
  38. e->setAttribute ("textpos", g->getTextLabelPosition().getFlags());
  39. return e;
  40. }
  41. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  42. {
  43. GroupComponent* const g = (GroupComponent*) comp;
  44. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  45. return false;
  46. g->setText (xml.getStringAttribute ("title", g->getText()));
  47. g->setTextLabelPosition (Justification (xml.getIntAttribute ("textpos", g->getTextLabelPosition().getFlags())));
  48. return true;
  49. }
  50. String getCreationParameters (Component* component)
  51. {
  52. GroupComponent* g = dynamic_cast <GroupComponent*> (component);
  53. return quotedString (component->getName())
  54. + ",\n"
  55. + quotedString (g->getText());
  56. }
  57. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  58. {
  59. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  60. GroupComponent* const g = dynamic_cast <GroupComponent*> (component);
  61. String s;
  62. GroupComponent defaultComp (String::empty, String::empty);
  63. if (g->getTextLabelPosition().getFlags() != defaultComp.getTextLabelPosition().getFlags())
  64. {
  65. s << memberVariableName << "->setTextLabelPosition ("
  66. << CodeHelpers::justificationToCode (g->getTextLabelPosition())
  67. << ");\n";
  68. }
  69. s << getColourIntialisationCode (component, memberVariableName)
  70. << '\n';
  71. code.constructorCode += s;
  72. }
  73. void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
  74. {
  75. ComponentTypeHandler::getEditableProperties (component, document, properties);
  76. properties.add (new GroupTitleProperty ((GroupComponent*) component, document));
  77. properties.add (new GroupJustificationProperty ((GroupComponent*) component, document));
  78. addColourProperties (component, document, properties);
  79. }
  80. private:
  81. //==============================================================================
  82. class GroupTitleProperty : public ComponentTextProperty <GroupComponent>
  83. {
  84. public:
  85. GroupTitleProperty (GroupComponent* comp, JucerDocument& doc)
  86. : ComponentTextProperty <GroupComponent> ("text", 200, false, comp, doc)
  87. {}
  88. void setText (const String& newText)
  89. {
  90. document.perform (new GroupTitleChangeAction (component, *document.getComponentLayout(), newText),
  91. "Change group title");
  92. }
  93. String getText() const
  94. {
  95. return component->getText();
  96. }
  97. private:
  98. class GroupTitleChangeAction : public ComponentUndoableAction <GroupComponent>
  99. {
  100. public:
  101. GroupTitleChangeAction (GroupComponent* const comp, ComponentLayout& layout, const String& newName_)
  102. : ComponentUndoableAction <GroupComponent> (comp, layout),
  103. newName (newName_)
  104. {
  105. oldName = comp->getText();
  106. }
  107. bool perform()
  108. {
  109. showCorrectTab();
  110. getComponent()->setText (newName);
  111. changed();
  112. return true;
  113. }
  114. bool undo()
  115. {
  116. showCorrectTab();
  117. getComponent()->setText (oldName);
  118. changed();
  119. return true;
  120. }
  121. String newName, oldName;
  122. };
  123. };
  124. //==============================================================================
  125. class GroupJustificationProperty : public JustificationProperty,
  126. public ChangeListener
  127. {
  128. public:
  129. GroupJustificationProperty (GroupComponent* const group_, JucerDocument& doc)
  130. : JustificationProperty ("layout", true),
  131. group (group_),
  132. document (doc)
  133. {
  134. document.addChangeListener (this);
  135. }
  136. ~GroupJustificationProperty()
  137. {
  138. document.removeChangeListener (this);
  139. }
  140. void setJustification (const Justification& newJustification)
  141. {
  142. document.perform (new GroupJustifyChangeAction (group, *document.getComponentLayout(), newJustification),
  143. "Change text label position");
  144. }
  145. const Justification getJustification() const
  146. {
  147. return group->getTextLabelPosition();
  148. }
  149. void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
  150. private:
  151. GroupComponent* const group;
  152. JucerDocument& document;
  153. class GroupJustifyChangeAction : public ComponentUndoableAction <GroupComponent>
  154. {
  155. public:
  156. GroupJustifyChangeAction (GroupComponent* const comp, ComponentLayout& layout, const Justification& newState_)
  157. : ComponentUndoableAction <GroupComponent> (comp, layout),
  158. newState (newState_),
  159. oldState (comp->getTextLabelPosition())
  160. {
  161. }
  162. bool perform()
  163. {
  164. showCorrectTab();
  165. getComponent()->setTextLabelPosition (newState);
  166. changed();
  167. return true;
  168. }
  169. bool undo()
  170. {
  171. showCorrectTab();
  172. getComponent()->setTextLabelPosition (oldState);
  173. changed();
  174. return true;
  175. }
  176. Justification newState, oldState;
  177. };
  178. };
  179. };