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.

181 lines
5.3KB

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