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.

239 lines
7.9KB

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