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.

185 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include "../../Application/jucer_Headers.h"
  19. #include "jucer_TestComponent.h"
  20. #include "../jucer_ObjectTypes.h"
  21. static Array <TestComponent*> testComponents;
  22. //==============================================================================
  23. TestComponent::TestComponent (JucerDocument* const doc,
  24. JucerDocument* const loaded,
  25. const bool alwaysFill)
  26. : ownerDocument (doc),
  27. loadedDocument (loaded),
  28. alwaysFillBackground (alwaysFill)
  29. {
  30. setToInitialSize();
  31. updateContents();
  32. testComponents.add (this);
  33. setLookAndFeel (&getLookAndFeel());
  34. }
  35. TestComponent::~TestComponent()
  36. {
  37. testComponents.removeFirstMatchingValue (this);
  38. deleteAllChildren();
  39. }
  40. //==============================================================================
  41. void TestComponent::reloadAll()
  42. {
  43. for (int i = testComponents.size(); --i >= 0;)
  44. testComponents.getUnchecked (i)->reload();
  45. }
  46. void TestComponent::reload()
  47. {
  48. if (findFile().exists() && lastModificationTime != findFile().getLastModificationTime())
  49. setFilename (filename);
  50. }
  51. //==============================================================================
  52. static StringArray recursiveFiles;
  53. File TestComponent::findFile() const
  54. {
  55. if (filename.isEmpty())
  56. return {};
  57. if (ownerDocument != nullptr)
  58. return ownerDocument->getCppFile().getSiblingFile (filename);
  59. return File::getCurrentWorkingDirectory().getChildFile (filename);
  60. }
  61. void TestComponent::setFilename (const String& newName)
  62. {
  63. File newFile;
  64. if (newName.isNotEmpty())
  65. {
  66. if (ownerDocument != nullptr)
  67. newFile = ownerDocument->getCppFile().getSiblingFile (newName);
  68. else
  69. newFile = File::getCurrentWorkingDirectory().getChildFile (newName);
  70. }
  71. if (! recursiveFiles.contains (newFile.getFullPathName()))
  72. {
  73. recursiveFiles.add (newFile.getFullPathName());
  74. loadedDocument.reset();
  75. filename = newName;
  76. lastModificationTime = findFile().getLastModificationTime();
  77. loadedDocument.reset (JucerDocument::createForCppFile (nullptr, findFile()));
  78. updateContents();
  79. repaint();
  80. recursiveFiles.remove (recursiveFiles.size() - 1);
  81. }
  82. }
  83. void TestComponent::setConstructorParams (const String& newParams)
  84. {
  85. constructorParams = newParams;
  86. }
  87. void TestComponent::updateContents()
  88. {
  89. deleteAllChildren();
  90. repaint();
  91. if (loadedDocument != nullptr)
  92. {
  93. addAndMakeVisible (loadedDocument->createTestComponent (alwaysFillBackground));
  94. handleResize();
  95. }
  96. }
  97. void TestComponent::setToInitialSize()
  98. {
  99. if (loadedDocument != nullptr)
  100. setSize (loadedDocument->getInitialWidth(),
  101. loadedDocument->getInitialHeight());
  102. else
  103. setSize (100, 100);
  104. }
  105. //==============================================================================
  106. void TestComponent::paint (Graphics& g)
  107. {
  108. if (loadedDocument == nullptr)
  109. {
  110. g.fillAll (Colours::white.withAlpha (0.25f));
  111. g.setColour (Colours::black.withAlpha (0.5f));
  112. g.drawRect (getLocalBounds());
  113. g.drawLine (0.0f, 0.0f, (float) getWidth(), (float) getHeight());
  114. g.drawLine (0.0f, (float) getHeight(), (float) getWidth(), 0.0f);
  115. g.setFont (14.0f);
  116. g.drawText ("Projucer Component",
  117. 0, 0, getWidth(), getHeight() / 2,
  118. Justification::centred, true);
  119. g.drawText ("(no file loaded)",
  120. 0, getHeight() / 2, getWidth(), getHeight() / 2,
  121. Justification::centred, true);
  122. }
  123. }
  124. void TestComponent::handleResize()
  125. {
  126. if (Component* const c = getChildComponent (0))
  127. {
  128. setOpaque (c->isOpaque());
  129. c->setBounds (getLocalBounds());
  130. }
  131. }
  132. void TestComponent::resized()
  133. {
  134. handleResize();
  135. }
  136. //==============================================================================
  137. void TestComponent::showInDialogBox (JucerDocument& document)
  138. {
  139. DialogWindow::LaunchOptions o;
  140. o.content.setOwned (new TestComponent (nullptr, document.createCopy(), true));
  141. o.dialogTitle = "Testing: " + document.getClassName();
  142. o.dialogBackgroundColour = Colours::azure;
  143. o.escapeKeyTriggersCloseButton = true;
  144. o.useNativeTitleBar = false;
  145. o.resizable = true;
  146. o.launchAsync();
  147. }