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.

238 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. class GroupComponentHandler : public ComponentTypeHandler
  21. {
  22. public:
  23. GroupComponentHandler()
  24. : ComponentTypeHandler ("Group Box", "juce::GroupComponent", typeid (GroupComponent), 200, 150)
  25. {
  26. registerColour (juce::GroupComponent::outlineColourId, "outline", "outlinecol");
  27. registerColour (juce::GroupComponent::textColourId, "text", "textcol");
  28. }
  29. Component* createNewComponent (JucerDocument*) override
  30. {
  31. return new GroupComponent ("new group", "group");
  32. }
  33. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout) override
  34. {
  35. GroupComponent* const g = (GroupComponent*) comp;
  36. XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
  37. e->setAttribute ("title", g->getText());
  38. GroupComponent defaultComp;
  39. if (g->getTextLabelPosition().getFlags() != defaultComp.getTextLabelPosition().getFlags())
  40. e->setAttribute ("textpos", g->getTextLabelPosition().getFlags());
  41. return e;
  42. }
  43. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout) override
  44. {
  45. GroupComponent* const g = (GroupComponent*) comp;
  46. if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
  47. return false;
  48. g->setText (xml.getStringAttribute ("title", g->getText()));
  49. g->setTextLabelPosition (Justification (xml.getIntAttribute ("textpos", g->getTextLabelPosition().getFlags())));
  50. return true;
  51. }
  52. String getCreationParameters (GeneratedCode& code, Component* component) override
  53. {
  54. GroupComponent* g = dynamic_cast<GroupComponent*> (component);
  55. return quotedString (component->getName(), false)
  56. + ",\n"
  57. + quotedString (g->getText(), code.shouldUseTransMacro());
  58. }
  59. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName) override
  60. {
  61. ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
  62. GroupComponent* const g = dynamic_cast<GroupComponent*> (component);
  63. String s;
  64. GroupComponent defaultComp;
  65. if (g->getTextLabelPosition().getFlags() != defaultComp.getTextLabelPosition().getFlags())
  66. {
  67. s << memberVariableName << "->setTextLabelPosition ("
  68. << CodeHelpers::justificationToCode (g->getTextLabelPosition())
  69. << ");\n";
  70. }
  71. s << getColourIntialisationCode (component, memberVariableName)
  72. << '\n';
  73. code.constructorCode += s;
  74. }
  75. void getEditableProperties (Component* component, JucerDocument& document,
  76. Array<PropertyComponent*>& props, bool multipleSelected) override
  77. {
  78. ComponentTypeHandler::getEditableProperties (component, document, props, multipleSelected);
  79. if (multipleSelected)
  80. return;
  81. if (auto* gc = dynamic_cast<GroupComponent*> (component))
  82. {
  83. props.add (new GroupTitleProperty (gc, document));
  84. props.add (new GroupJustificationProperty (gc, document));
  85. }
  86. addColourProperties (component, document, props);
  87. }
  88. private:
  89. //==============================================================================
  90. class GroupTitleProperty : public ComponentTextProperty <GroupComponent>
  91. {
  92. public:
  93. GroupTitleProperty (GroupComponent* comp, JucerDocument& doc)
  94. : ComponentTextProperty <GroupComponent> ("text", 200, false, comp, doc)
  95. {}
  96. void setText (const String& newText) override
  97. {
  98. document.perform (new GroupTitleChangeAction (component, *document.getComponentLayout(), newText),
  99. "Change group title");
  100. }
  101. String getText() const override
  102. {
  103. return component->getText();
  104. }
  105. private:
  106. class GroupTitleChangeAction : public ComponentUndoableAction <GroupComponent>
  107. {
  108. public:
  109. GroupTitleChangeAction (GroupComponent* const comp, ComponentLayout& l, const String& newName_)
  110. : ComponentUndoableAction <GroupComponent> (comp, l),
  111. newName (newName_)
  112. {
  113. oldName = comp->getText();
  114. }
  115. bool perform()
  116. {
  117. showCorrectTab();
  118. getComponent()->setText (newName);
  119. changed();
  120. return true;
  121. }
  122. bool undo()
  123. {
  124. showCorrectTab();
  125. getComponent()->setText (oldName);
  126. changed();
  127. return true;
  128. }
  129. String newName, oldName;
  130. };
  131. };
  132. //==============================================================================
  133. class GroupJustificationProperty : public JustificationProperty,
  134. private ChangeListener
  135. {
  136. public:
  137. GroupJustificationProperty (GroupComponent* const group_, JucerDocument& doc)
  138. : JustificationProperty ("layout", true),
  139. group (group_),
  140. document (doc)
  141. {
  142. document.addChangeListener (this);
  143. }
  144. ~GroupJustificationProperty() override
  145. {
  146. document.removeChangeListener (this);
  147. }
  148. void setJustification (Justification newJustification) override
  149. {
  150. document.perform (new GroupJustifyChangeAction (group, *document.getComponentLayout(), newJustification),
  151. "Change text label position");
  152. }
  153. Justification getJustification() const override
  154. {
  155. return group->getTextLabelPosition();
  156. }
  157. private:
  158. void changeListenerCallback (ChangeBroadcaster*) override { refresh(); }
  159. GroupComponent* const group;
  160. JucerDocument& document;
  161. class GroupJustifyChangeAction : public ComponentUndoableAction <GroupComponent>
  162. {
  163. public:
  164. GroupJustifyChangeAction (GroupComponent* const comp, ComponentLayout& l, Justification newState_)
  165. : ComponentUndoableAction <GroupComponent> (comp, l),
  166. newState (newState_),
  167. oldState (comp->getTextLabelPosition())
  168. {
  169. }
  170. bool perform()
  171. {
  172. showCorrectTab();
  173. getComponent()->setTextLabelPosition (newState);
  174. changed();
  175. return true;
  176. }
  177. bool undo()
  178. {
  179. showCorrectTab();
  180. getComponent()->setTextLabelPosition (oldState);
  181. changed();
  182. return true;
  183. }
  184. Justification newState, oldState;
  185. };
  186. };
  187. };