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.

180 lines
5.3KB

  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_TestComponent.h"
  19. #include "../jucer_ObjectTypes.h"
  20. static Array <TestComponent*> testComponents;
  21. //==============================================================================
  22. TestComponent::TestComponent (JucerDocument* const doc,
  23. JucerDocument* const loaded,
  24. const bool alwaysFill)
  25. : ownerDocument (doc),
  26. loadedDocument (loaded),
  27. alwaysFillBackground (alwaysFill)
  28. {
  29. setToInitialSize();
  30. updateContents();
  31. testComponents.add (this);
  32. setLookAndFeel (&lookAndFeel);
  33. }
  34. TestComponent::~TestComponent()
  35. {
  36. testComponents.removeFirstMatchingValue (this);
  37. deleteAllChildren();
  38. }
  39. //==============================================================================
  40. void TestComponent::reloadAll()
  41. {
  42. for (int i = testComponents.size(); --i >= 0;)
  43. testComponents.getUnchecked(i)->reload();
  44. }
  45. void TestComponent::reload()
  46. {
  47. if (findFile().exists() && lastModificationTime != findFile().getLastModificationTime())
  48. setFilename (filename);
  49. }
  50. //==============================================================================
  51. static StringArray recursiveFiles;
  52. File TestComponent::findFile() const
  53. {
  54. if (filename.isEmpty())
  55. return {};
  56. if (ownerDocument != nullptr)
  57. return ownerDocument->getCppFile().getSiblingFile (filename);
  58. return File::getCurrentWorkingDirectory().getChildFile (filename);
  59. }
  60. void TestComponent::setFilename (const String& newName)
  61. {
  62. File newFile;
  63. if (newName.isNotEmpty())
  64. {
  65. if (ownerDocument != nullptr)
  66. newFile = ownerDocument->getCppFile().getSiblingFile (newName);
  67. else
  68. newFile = File::getCurrentWorkingDirectory().getChildFile (newName);
  69. }
  70. if (! recursiveFiles.contains (newFile.getFullPathName()))
  71. {
  72. recursiveFiles.add (newFile.getFullPathName());
  73. loadedDocument = nullptr;
  74. filename = newName;
  75. lastModificationTime = findFile().getLastModificationTime();
  76. loadedDocument = JucerDocument::createForCppFile (nullptr, findFile());
  77. updateContents();
  78. repaint();
  79. recursiveFiles.remove (recursiveFiles.size() - 1);
  80. }
  81. }
  82. void TestComponent::setConstructorParams (const String& newParams)
  83. {
  84. constructorParams = newParams;
  85. }
  86. void TestComponent::updateContents()
  87. {
  88. deleteAllChildren();
  89. repaint();
  90. if (loadedDocument != nullptr)
  91. {
  92. addAndMakeVisible (loadedDocument->createTestComponent (alwaysFillBackground));
  93. resized();
  94. }
  95. }
  96. void TestComponent::setToInitialSize()
  97. {
  98. if (loadedDocument != nullptr)
  99. setSize (loadedDocument->getInitialWidth(),
  100. loadedDocument->getInitialHeight());
  101. else
  102. setSize (100, 100);
  103. }
  104. //==============================================================================
  105. void TestComponent::paint (Graphics& g)
  106. {
  107. if (loadedDocument == nullptr)
  108. {
  109. g.fillAll (Colours::white.withAlpha (0.25f));
  110. g.setColour (Colours::black.withAlpha (0.5f));
  111. g.drawRect (getLocalBounds());
  112. g.drawLine (0.0f, 0.0f, (float) getWidth(), (float) getHeight());
  113. g.drawLine (0.0f, (float) getHeight(), (float) getWidth(), 0.0f);
  114. g.setFont (14.0f);
  115. g.drawText ("Projucer Component",
  116. 0, 0, getWidth(), getHeight() / 2,
  117. Justification::centred, true);
  118. g.drawText ("(no file loaded)",
  119. 0, getHeight() / 2, getWidth(), getHeight() / 2,
  120. Justification::centred, true);
  121. }
  122. }
  123. void TestComponent::resized()
  124. {
  125. if (Component* const c = getChildComponent (0))
  126. {
  127. setOpaque (c->isOpaque());
  128. c->setBounds (getLocalBounds());
  129. }
  130. }
  131. //==============================================================================
  132. void TestComponent::showInDialogBox (JucerDocument& document)
  133. {
  134. DialogWindow::LaunchOptions o;
  135. o.content.setOwned (new TestComponent (nullptr, document.createCopy(), true));
  136. o.dialogTitle = "Testing: " + document.getClassName();
  137. o.dialogBackgroundColour = Colours::azure;
  138. o.escapeKeyTriggersCloseButton = true;
  139. o.useNativeTitleBar = false;
  140. o.resizable = true;
  141. o.launchAsync();
  142. }