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.

211 lines
6.2KB

  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);
  37. resized();
  38. }
  39. }
  40. Component* getComponent() const noexcept { return comp.get(); }
  41. void showHomeScreen() { setComponent (createIntroDemo()); }
  42. private:
  43. ScopedPointer<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. addTab ("Demo", Colours::transparentBlack, demoContent = new DemoContent(), false);
  86. #if ! (JUCE_ANDROID || JUCE_IOS)
  87. addTab ("Code", Colours::transparentBlack, codeContent = new CodeContent(), false);
  88. #endif
  89. addTab ("Settings", Colours::transparentBlack, new SettingsContent (dynamic_cast<MainComponent&> (mainComponent)), true);
  90. setTabBarDepth (40);
  91. lookAndFeelChanged();
  92. }
  93. DemoContentComponent::~DemoContentComponent()
  94. {
  95. }
  96. void DemoContentComponent::resized()
  97. {
  98. TabbedComponent::resized();
  99. if (tabBarIndent > 0)
  100. getTabbedButtonBar().setBounds (getTabbedButtonBar().getBounds().withTrimmedLeft (tabBarIndent));
  101. }
  102. void DemoContentComponent::setDemo (const String& category, int selectedDemoIndex)
  103. {
  104. if ((currentDemoCategory == category)
  105. && (currentDemoIndex == selectedDemoIndex))
  106. return;
  107. auto demo = JUCEDemos::getCategory (category).demos[(size_t) selectedDemoIndex];
  108. #if ! (JUCE_ANDROID || JUCE_IOS)
  109. codeContent->document.replaceAllContent (trimPIP (demo.demoFile.loadFileAsString()));
  110. codeContent->codeEditor.scrollToLine (0);
  111. #endif
  112. auto* content = demo.callback();
  113. demoContent->setComponent (content);
  114. demoChangedCallback (demo.isHeavyweight);
  115. ensureDemoIsShowing();
  116. currentDemoCategory = category;
  117. currentDemoIndex = selectedDemoIndex;
  118. }
  119. bool DemoContentComponent::isShowingHomeScreen() const noexcept
  120. {
  121. return isComponentIntroDemo (demoContent->getComponent());
  122. }
  123. void DemoContentComponent::showHomeScreen()
  124. {
  125. demoContent->showHomeScreen();
  126. #if ! (JUCE_ANDROID || JUCE_IOS)
  127. codeContent->setDefaultCodeContent();
  128. #endif
  129. demoChangedCallback (false);
  130. ensureDemoIsShowing();
  131. resized();
  132. currentDemoCategory = {};
  133. currentDemoIndex = -1;
  134. }
  135. void DemoContentComponent::clearCurrentDemo()
  136. {
  137. demoContent->setComponent (nullptr);
  138. demoChangedCallback (false);
  139. }
  140. void DemoContentComponent::lookAndFeelChanged()
  141. {
  142. auto backgroundColour = findColour (ResizableWindow::backgroundColourId);
  143. for (int i = 0; i < getNumTabs(); ++i)
  144. setTabBackgroundColour (i, backgroundColour);
  145. }
  146. String DemoContentComponent::trimPIP (const String& fileContents)
  147. {
  148. auto lines = StringArray::fromLines (fileContents);
  149. auto metadataEndIndex = lines.indexOf (" END_JUCE_PIP_METADATA");
  150. if (metadataEndIndex == -1)
  151. return fileContents;
  152. lines.removeRange (0, metadataEndIndex + 3); // account for newline and comment block end
  153. return lines.joinIntoString ("\n");
  154. }
  155. void DemoContentComponent::ensureDemoIsShowing()
  156. {
  157. if (getCurrentTabIndex() == (getNumTabs() - 1))
  158. setCurrentTabIndex (0);
  159. }