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.

173 lines
5.1KB

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