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.

167 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../../jucer_Headers.h"
  18. #include "jucer_ComponentDocument.h"
  19. //==============================================================================
  20. ComponentDocument::ComponentDocument (SourceCodeDocument* c)
  21. : JucerDocument (c)
  22. {
  23. components = new ComponentLayout();
  24. components->setDocument (this);
  25. backgroundGraphics = new PaintRoutine();
  26. backgroundGraphics->setDocument (this);
  27. }
  28. ComponentDocument::~ComponentDocument()
  29. {
  30. }
  31. //==============================================================================
  32. String ComponentDocument::getTypeName() const
  33. {
  34. return "Component";
  35. }
  36. JucerDocument* ComponentDocument::createCopy()
  37. {
  38. ComponentDocument* newOne = new ComponentDocument (cpp);
  39. newOne->resources = resources;
  40. ScopedPointer<XmlElement> xml (createXml());
  41. newOne->loadFromXml (*xml);
  42. return newOne;
  43. }
  44. XmlElement* ComponentDocument::createXml() const
  45. {
  46. XmlElement* const doc = JucerDocument::createXml();
  47. doc->addChildElement (backgroundGraphics->createXml());
  48. components->addToXml (*doc);
  49. return doc;
  50. }
  51. bool ComponentDocument::loadFromXml (const XmlElement& xml)
  52. {
  53. if (JucerDocument::loadFromXml (xml))
  54. {
  55. components->clearComponents();
  56. forEachXmlChildElement (xml, e)
  57. {
  58. if (e->hasTagName (PaintRoutine::xmlTagName))
  59. backgroundGraphics->loadFromXml (*e);
  60. else
  61. components->addComponentFromXml (*e, false);
  62. }
  63. changed();
  64. getUndoManager().clearUndoHistory();
  65. return true;
  66. }
  67. return false;
  68. }
  69. //==============================================================================
  70. class NormalTestComponent : public Component
  71. {
  72. public:
  73. NormalTestComponent (ComponentDocument* const doc, const bool fillBackground)
  74. : document (doc),
  75. alwaysFillBackground (fillBackground)
  76. {
  77. ComponentLayout* const layout = document->getComponentLayout();
  78. for (int i = 0; i < layout->getNumComponents(); ++i)
  79. addAndMakeVisible (layout->getComponent (i));
  80. }
  81. ~NormalTestComponent()
  82. {
  83. for (int i = getNumChildComponents(); --i >= 0;)
  84. removeChildComponent (i);
  85. }
  86. void paint (Graphics& g) override
  87. {
  88. document->getPaintRoutine (0)->fillWithBackground (g, alwaysFillBackground);
  89. document->getPaintRoutine (0)->drawElements (g, getLocalBounds());
  90. }
  91. void resized() override
  92. {
  93. if (! getBounds().isEmpty())
  94. {
  95. int numTimesToTry = 10;
  96. while (--numTimesToTry >= 0)
  97. {
  98. bool anyCompsMoved = false;
  99. for (int i = 0; i < getNumChildComponents(); ++i)
  100. {
  101. Component* comp = getChildComponent (i);
  102. if (ComponentTypeHandler* const type = ComponentTypeHandler::getHandlerFor (*comp))
  103. {
  104. const Rectangle<int> newBounds (type->getComponentPosition (comp)
  105. .getRectangle (getLocalBounds(),
  106. document->getComponentLayout()));
  107. anyCompsMoved = anyCompsMoved || (comp->getBounds() != newBounds);
  108. comp->setBounds (newBounds);
  109. }
  110. }
  111. // repeat this loop until they've all stopped shuffling (might require a few
  112. // loops for all the relative positioned comps to settle down)
  113. if (! anyCompsMoved)
  114. break;
  115. }
  116. }
  117. }
  118. private:
  119. ComponentDocument* const document;
  120. const bool alwaysFillBackground;
  121. };
  122. Component* ComponentDocument::createTestComponent (const bool alwaysFillBackground)
  123. {
  124. return new NormalTestComponent (this, alwaysFillBackground);
  125. }
  126. void ComponentDocument::fillInGeneratedCode (GeneratedCode& code) const
  127. {
  128. JucerDocument::fillInGeneratedCode (code);
  129. }