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.

164 lines
4.8KB

  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. #include "../../Application/jucer_Headers.h"
  14. #include "jucer_ComponentDocument.h"
  15. //==============================================================================
  16. ComponentDocument::ComponentDocument (SourceCodeDocument* c)
  17. : JucerDocument (c)
  18. {
  19. components.reset (new ComponentLayout());
  20. components->setDocument (this);
  21. backgroundGraphics.reset (new PaintRoutine());
  22. backgroundGraphics->setDocument (this);
  23. }
  24. ComponentDocument::~ComponentDocument()
  25. {
  26. }
  27. //==============================================================================
  28. String ComponentDocument::getTypeName() const
  29. {
  30. return "Component";
  31. }
  32. JucerDocument* ComponentDocument::createCopy()
  33. {
  34. auto newOne = new ComponentDocument (cpp);
  35. newOne->resources = resources;
  36. newOne->loadFromXml (*createXml());
  37. return newOne;
  38. }
  39. std::unique_ptr<XmlElement> ComponentDocument::createXml() const
  40. {
  41. auto doc = JucerDocument::createXml();
  42. doc->addChildElement (backgroundGraphics->createXml());
  43. components->addToXml (*doc);
  44. return doc;
  45. }
  46. bool ComponentDocument::loadFromXml (const XmlElement& xml)
  47. {
  48. if (JucerDocument::loadFromXml (xml))
  49. {
  50. components->clearComponents();
  51. forEachXmlChildElement (xml, e)
  52. {
  53. if (e->hasTagName (PaintRoutine::xmlTagName))
  54. backgroundGraphics->loadFromXml (*e);
  55. else
  56. components->addComponentFromXml (*e, false);
  57. }
  58. changed();
  59. getUndoManager().clearUndoHistory();
  60. return true;
  61. }
  62. return false;
  63. }
  64. void ComponentDocument::applyCustomPaintSnippets (StringArray& snippets)
  65. {
  66. backgroundGraphics->applyCustomPaintSnippets (snippets);
  67. }
  68. //==============================================================================
  69. class NormalTestComponent : public Component
  70. {
  71. public:
  72. NormalTestComponent (ComponentDocument* const doc, const bool fillBackground)
  73. : document (doc),
  74. alwaysFillBackground (fillBackground)
  75. {
  76. ComponentLayout* const layout = document->getComponentLayout();
  77. for (int i = 0; i < layout->getNumComponents(); ++i)
  78. addAndMakeVisible (layout->getComponent (i));
  79. }
  80. ~NormalTestComponent() override
  81. {
  82. for (int i = getNumChildComponents(); --i >= 0;)
  83. removeChildComponent (i);
  84. }
  85. void paint (Graphics& g) override
  86. {
  87. document->getPaintRoutine (0)->fillWithBackground (g, alwaysFillBackground);
  88. document->getPaintRoutine (0)->drawElements (g, getLocalBounds());
  89. }
  90. void resized() override
  91. {
  92. if (! getBounds().isEmpty())
  93. {
  94. int numTimesToTry = 10;
  95. while (--numTimesToTry >= 0)
  96. {
  97. bool anyCompsMoved = false;
  98. for (int i = 0; i < getNumChildComponents(); ++i)
  99. {
  100. Component* comp = getChildComponent (i);
  101. if (ComponentTypeHandler* const type = ComponentTypeHandler::getHandlerFor (*comp))
  102. {
  103. const Rectangle<int> newBounds (type->getComponentPosition (comp)
  104. .getRectangle (getLocalBounds(),
  105. document->getComponentLayout()));
  106. anyCompsMoved = anyCompsMoved || (comp->getBounds() != newBounds);
  107. comp->setBounds (newBounds);
  108. }
  109. }
  110. // repeat this loop until they've all stopped shuffling (might require a few
  111. // loops for all the relative positioned comps to settle down)
  112. if (! anyCompsMoved)
  113. break;
  114. }
  115. }
  116. }
  117. private:
  118. ComponentDocument* const document;
  119. const bool alwaysFillBackground;
  120. };
  121. Component* ComponentDocument::createTestComponent (const bool alwaysFillBackground)
  122. {
  123. return new NormalTestComponent (this, alwaysFillBackground);
  124. }
  125. void ComponentDocument::fillInGeneratedCode (GeneratedCode& code) const
  126. {
  127. JucerDocument::fillInGeneratedCode (code);
  128. }