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.

228 lines
7.6KB

  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*)
  29. {
  30. return new GroupComponent ("new group", "group");
  31. }
  32. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  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)
  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)
  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)
  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, Array<PropertyComponent*>& props)
  75. {
  76. ComponentTypeHandler::getEditableProperties (component, document, props);
  77. props.add (new GroupTitleProperty ((GroupComponent*) component, document));
  78. props.add (new GroupJustificationProperty ((GroupComponent*) component, document));
  79. addColourProperties (component, document, props);
  80. }
  81. private:
  82. //==============================================================================
  83. class GroupTitleProperty : public ComponentTextProperty <GroupComponent>
  84. {
  85. public:
  86. GroupTitleProperty (GroupComponent* comp, JucerDocument& doc)
  87. : ComponentTextProperty <GroupComponent> ("text", 200, false, comp, doc)
  88. {}
  89. void setText (const String& newText) override
  90. {
  91. document.perform (new GroupTitleChangeAction (component, *document.getComponentLayout(), newText),
  92. "Change group title");
  93. }
  94. String getText() const override
  95. {
  96. return component->getText();
  97. }
  98. private:
  99. class GroupTitleChangeAction : public ComponentUndoableAction <GroupComponent>
  100. {
  101. public:
  102. GroupTitleChangeAction (GroupComponent* const comp, ComponentLayout& l, const String& newName_)
  103. : ComponentUndoableAction <GroupComponent> (comp, l),
  104. newName (newName_)
  105. {
  106. oldName = comp->getText();
  107. }
  108. bool perform()
  109. {
  110. showCorrectTab();
  111. getComponent()->setText (newName);
  112. changed();
  113. return true;
  114. }
  115. bool undo()
  116. {
  117. showCorrectTab();
  118. getComponent()->setText (oldName);
  119. changed();
  120. return true;
  121. }
  122. String newName, oldName;
  123. };
  124. };
  125. //==============================================================================
  126. class GroupJustificationProperty : public JustificationProperty,
  127. public ChangeListener
  128. {
  129. public:
  130. GroupJustificationProperty (GroupComponent* const group_, JucerDocument& doc)
  131. : JustificationProperty ("layout", true),
  132. group (group_),
  133. document (doc)
  134. {
  135. document.addChangeListener (this);
  136. }
  137. ~GroupJustificationProperty()
  138. {
  139. document.removeChangeListener (this);
  140. }
  141. void setJustification (Justification newJustification)
  142. {
  143. document.perform (new GroupJustifyChangeAction (group, *document.getComponentLayout(), newJustification),
  144. "Change text label position");
  145. }
  146. Justification getJustification() const
  147. {
  148. return group->getTextLabelPosition();
  149. }
  150. void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
  151. private:
  152. GroupComponent* const group;
  153. JucerDocument& document;
  154. class GroupJustifyChangeAction : public ComponentUndoableAction <GroupComponent>
  155. {
  156. public:
  157. GroupJustifyChangeAction (GroupComponent* const comp, ComponentLayout& l, Justification newState_)
  158. : ComponentUndoableAction <GroupComponent> (comp, l),
  159. newState (newState_),
  160. oldState (comp->getTextLabelPosition())
  161. {
  162. }
  163. bool perform()
  164. {
  165. showCorrectTab();
  166. getComponent()->setTextLabelPosition (newState);
  167. changed();
  168. return true;
  169. }
  170. bool undo()
  171. {
  172. showCorrectTab();
  173. getComponent()->setTextLabelPosition (oldState);
  174. changed();
  175. return true;
  176. }
  177. Justification newState, oldState;
  178. };
  179. };
  180. };