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.

168 lines
5.2KB

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