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.

231 lines
7.6KB

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