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.

226 lines
7.6KB

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