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.

235 lines
7.8KB

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