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.6KB

  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_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 (&lookAndFeel);
  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 File::nonexistent;
  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 = nullptr;
  75. filename = newName;
  76. lastModificationTime = findFile().getLastModificationTime();
  77. loadedDocument = 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. resized();
  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 ("Jucer 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::resized()
  125. {
  126. if (Component* const c = getChildComponent (0))
  127. {
  128. setOpaque (c->isOpaque());
  129. c->setBounds (getLocalBounds());
  130. }
  131. }
  132. //==============================================================================
  133. void TestComponent::showInDialogBox (JucerDocument& document)
  134. {
  135. DialogWindow::LaunchOptions o;
  136. o.content.setOwned (new TestComponent (nullptr, document.createCopy(), true));
  137. o.dialogTitle = "Testing: " + document.getClassName();
  138. o.dialogBackgroundColour = Colours::azure;
  139. o.escapeKeyTriggersCloseButton = true;
  140. o.useNativeTitleBar = false;
  141. o.resizable = true;
  142. o.launchAsync();
  143. }