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.

213 lines
6.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 "DemoContentComponent.h"
  20. #include "SettingsContent.h"
  21. #include "MainComponent.h"
  22. //==============================================================================
  23. struct DemoContent : public Component
  24. {
  25. DemoContent() noexcept {}
  26. void resized() override
  27. {
  28. if (comp != nullptr)
  29. comp->setBounds (getLocalBounds());
  30. }
  31. void setComponent (Component* newComponent)
  32. {
  33. comp.reset (newComponent);
  34. if (comp != nullptr)
  35. {
  36. addAndMakeVisible (comp.get());
  37. resized();
  38. }
  39. }
  40. Component* getComponent() const noexcept { return comp.get(); }
  41. void showHomeScreen() { setComponent (createIntroDemo()); }
  42. private:
  43. std::unique_ptr<Component> comp;
  44. };
  45. //==============================================================================
  46. #if ! (JUCE_ANDROID || JUCE_IOS)
  47. struct CodeContent : public Component
  48. {
  49. CodeContent()
  50. {
  51. addAndMakeVisible (codeEditor);
  52. codeEditor.setReadOnly (true);
  53. codeEditor.setScrollbarThickness (8);
  54. lookAndFeelChanged();
  55. }
  56. void resized() override
  57. {
  58. codeEditor.setBounds (getLocalBounds());
  59. }
  60. void setDefaultCodeContent()
  61. {
  62. document.replaceAllContent ("\n/*******************************************************************************\n"
  63. " Select one of the demos from the side panel on the left to see\n"
  64. " its code here and an instance running in the \"Demo\" tab!\n"
  65. "*******************************************************************************/\n");
  66. }
  67. void lookAndFeelChanged() override
  68. {
  69. auto* v4 = dynamic_cast <LookAndFeel_V4*> (&Desktop::getInstance().getDefaultLookAndFeel());
  70. if (v4 != nullptr && (v4->getCurrentColourScheme() != LookAndFeel_V4::getLightColourScheme()))
  71. codeEditor.setColourScheme (getDarkColourScheme());
  72. else
  73. codeEditor.setColourScheme (getLightColourScheme());
  74. }
  75. CodeDocument document;
  76. CPlusPlusCodeTokeniser cppTokensier;
  77. CodeEditorComponent codeEditor { document, &cppTokensier };
  78. };
  79. #endif
  80. //==============================================================================
  81. DemoContentComponent::DemoContentComponent (Component& mainComponent, std::function<void(bool)> callback)
  82. : TabbedComponent (TabbedButtonBar::Orientation::TabsAtTop),
  83. demoChangedCallback (callback)
  84. {
  85. demoContent.reset (new DemoContent());
  86. addTab ("Demo", Colours::transparentBlack, demoContent.get(), false);
  87. #if ! (JUCE_ANDROID || JUCE_IOS)
  88. codeContent.reset (new CodeContent());
  89. addTab ("Code", Colours::transparentBlack, codeContent.get(), false);
  90. #endif
  91. addTab ("Settings", Colours::transparentBlack, new SettingsContent (dynamic_cast<MainComponent&> (mainComponent)), true);
  92. setTabBarDepth (40);
  93. lookAndFeelChanged();
  94. }
  95. DemoContentComponent::~DemoContentComponent()
  96. {
  97. }
  98. void DemoContentComponent::resized()
  99. {
  100. TabbedComponent::resized();
  101. if (tabBarIndent > 0)
  102. getTabbedButtonBar().setBounds (getTabbedButtonBar().getBounds().withTrimmedLeft (tabBarIndent));
  103. }
  104. void DemoContentComponent::setDemo (const String& category, int selectedDemoIndex)
  105. {
  106. if ((currentDemoCategory == category)
  107. && (currentDemoIndex == selectedDemoIndex))
  108. return;
  109. auto demo = JUCEDemos::getCategory (category).demos[(size_t) selectedDemoIndex];
  110. #if ! (JUCE_ANDROID || JUCE_IOS)
  111. codeContent->document.replaceAllContent (trimPIP (demo.demoFile.loadFileAsString()));
  112. codeContent->codeEditor.scrollToLine (0);
  113. #endif
  114. auto* content = demo.callback();
  115. demoContent->setComponent (content);
  116. demoChangedCallback (demo.isHeavyweight);
  117. ensureDemoIsShowing();
  118. currentDemoCategory = category;
  119. currentDemoIndex = selectedDemoIndex;
  120. }
  121. bool DemoContentComponent::isShowingHomeScreen() const noexcept
  122. {
  123. return isComponentIntroDemo (demoContent->getComponent());
  124. }
  125. void DemoContentComponent::showHomeScreen()
  126. {
  127. demoContent->showHomeScreen();
  128. #if ! (JUCE_ANDROID || JUCE_IOS)
  129. codeContent->setDefaultCodeContent();
  130. #endif
  131. demoChangedCallback (false);
  132. ensureDemoIsShowing();
  133. resized();
  134. currentDemoCategory = {};
  135. currentDemoIndex = -1;
  136. }
  137. void DemoContentComponent::clearCurrentDemo()
  138. {
  139. demoContent->setComponent (nullptr);
  140. demoChangedCallback (false);
  141. }
  142. void DemoContentComponent::lookAndFeelChanged()
  143. {
  144. auto backgroundColour = findColour (ResizableWindow::backgroundColourId);
  145. for (int i = 0; i < getNumTabs(); ++i)
  146. setTabBackgroundColour (i, backgroundColour);
  147. }
  148. String DemoContentComponent::trimPIP (const String& fileContents)
  149. {
  150. auto lines = StringArray::fromLines (fileContents);
  151. auto metadataEndIndex = lines.indexOf (" END_JUCE_PIP_METADATA");
  152. if (metadataEndIndex == -1)
  153. return fileContents;
  154. lines.removeRange (0, metadataEndIndex + 3); // account for newline and comment block end
  155. return lines.joinIntoString ("\n");
  156. }
  157. void DemoContentComponent::ensureDemoIsShowing()
  158. {
  159. if (getCurrentTabIndex() == (getNumTabs() - 1))
  160. setCurrentTabIndex (0);
  161. }