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.

417 lines
14KB

  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 "MainComponent.h"
  20. //==============================================================================
  21. struct SidePanelHeader : public Component
  22. {
  23. SidePanelHeader (MainComponent& o)
  24. : owner (o)
  25. {
  26. setOpaque (true);
  27. static const unsigned char homeIconPathData[]
  28. = { 110,109,0,0,64,65,0,0,64,64,98,0,0,64,65,0,0,64,64,74,12,186,64,164,112,5,65,24,217,22,64,70,182,
  29. 51,65,98,35,219,9,64,240,167,54,65,0,0,0,64,170,241,58,65,0,0,0,64,0,0,64,65,98,0,0,0,64,22,217,
  30. 72,65,166,155,28,64,0,0,80,65,0,0,64,64,0,0,80,65,108,0,0,160,64,0,0,80,65,108,0,0,160,64,0,0,
  31. 160,65,98,0,0,160,64,139,108,164,65,211,77,174,64,0,0,168,65,0,0,192,64,0,0,168,65,108,0,0,16,65,
  32. 0,0,168,65,98,22,217,24,65,0,0,168,65,0,0,32,65,127,106,164,65,0,0,32,65,0,0,160,65,108,0,0,32,65,0,0,128,
  33. 65,108,0,0,96,65,0,0,128,65,108,0,0,96,65,0,0,160,65,98,0,0,96,65,127,106,164,65,233,38,103,65,0,0,168,
  34. 65,0,0,112,65,0,0,168,65,108,0,0,144,65,0,0,168,65,98,139,108,148,65,0,0,168,65,0,0,152,65,139,108,164,
  35. 65,0,0,152,65,0,0,160,65,108,0,0,152,65,0,0,80,65,108,0,0,168,65,0,0,80,65,98,139,108,172,65,0,0,80,65,0,
  36. 0,176,65,23,217,72,65,0,0,176,65,0,0,64,65,98,0,0,176,65,170,241,58,65,156,196,174,65,240,167,54,65,158,239,
  37. 172,65,70,182,51,65,98,213,120,145,65,164,112,5,65,0,0,64,65,0,0,64,64,0,0,64,65,0,0,64,64,99,
  38. 101,0,0 };
  39. static const unsigned char settingsIconPathData[]
  40. = { 110,109,202,111,210,64,243,226,61,64,108,0,0,224,64,0,0,0,0,108,0,0,48,65,0,0,0,0,108,27,200,54,65,243,
  41. 226,61,64,98,91,248,63,65,174,170,76,64,95,130,72,65,231,138,96,64,46,46,80,65,180,163,120,64,108,42,
  42. 181,124,65,20,38,49,64,108,149,90,142,65,246,108,199,64,108,68,249,118,65,2,85,1,65,98,112,166,119,65,
  43. 201,31,6,65,0,0,120,65,111,5,11,65,0,0,120,65,0,0,16,65,98,0,0,120,65,145,250,20,65,108,166,119,65,55,
  44. 224,25,65,72,249,118,65,254,170,30,65,108,151,90,142,65,133,73,60,65,108,46,181,124,65,123,182,115,65,
  45. 108,50,46,80,65,18,215,97,65,98,99,130,72,65,70,221,103,65,96,248,63,65,83,213,108,65,32,200,54,65,66,
  46. 135,112,65,108,0,0,48,65,0,0,144,65,108,0,0,224,64,0,0,144,65,108,202,111,210,64,67,135,112,65,98,74,
  47. 15,192,64,84,213,108,65,65,251,174,64,70,221,103,65,164,163,159,64,19,215,97,65,108,92,43,13,64,123,182,
  48. 115,65,108,187,181,82,62,133,73,60,65,108,244,26,36,64,254,170,30,65,98,64,102,33,64,55,224,25,5,0,0,32,
  49. 64,145,250,20,65,0,0,32,64,0,0,16,65,98,0,0,32,64,111,5,11,65,64,102,33,64,201,31,6,65,244,26,36,64,2,85,
  50. 1,65,108,187,181,82,62,246,108,199,64,108,92,43,13,64,20,38,49,64,108,164,163,159,64,180,163,120,64,98,65,
  51. 251,174,64,231,138,96,64,74,15,192,64,175,170,76,64,202,111,210,64,243,226,61,64,99,109,0,0,16,65,0,0,64,
  52. 65,98,121,130,42,65,0,0,64,65,0,0,64,65,121,130,42,65,0,0,64,65,0,0,16,65,98,0,0,64,65,13,251,234,64,121,
  53. 130,42,65,0,0,192,64,0,0,16,65,0,0,192,64,98,13,251,234,64,0,0,192,64,0,0,192,64,13,251,234,64,0,0,192,64,
  54. 0,0,16,65,98,0,0,192,64,121,130,42,65,13,251,234,64,0,0,64,65,0,0,16,65,0,0,64,65,99,101,0,0 };
  55. Path p;
  56. p.loadPathFromData (homeIconPathData, sizeof (homeIconPathData));
  57. homeButton.setShape (p, true, true, false);
  58. p.clear();
  59. p.loadPathFromData (settingsIconPathData, sizeof (settingsIconPathData));
  60. settingsButton.setShape (p, true, true, false);
  61. titleLabel.setText (owner.getSidePanel().getTitleText(), NotificationType::dontSendNotification);
  62. addAndMakeVisible (titleLabel);
  63. homeButton.onClick = [this] { owner.homeButtonClicked(); };
  64. addAndMakeVisible (homeButton);
  65. addAndMakeVisible (settingsButton);
  66. settingsButton.onClick = [this] { owner.settingsButtonClicked(); };
  67. lookAndFeelChanged();
  68. }
  69. void paint (Graphics& g) override
  70. {
  71. g.fillAll (owner.getSidePanel().findColour (SidePanel::backgroundColour));
  72. }
  73. void resized() override
  74. {
  75. auto bounds = getLocalBounds();
  76. auto buttonWidth = owner.getSidePanel().getTitleBarHeight();
  77. bounds.removeFromLeft (10);
  78. homeButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
  79. settingsButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
  80. bounds.removeFromLeft (10);
  81. bounds.removeFromRight (10);
  82. titleLabel.setBounds (bounds);
  83. }
  84. void lookAndFeelChanged() override
  85. {
  86. auto& sidePanel = owner.getSidePanel();
  87. auto& lf = sidePanel.getLookAndFeel();
  88. titleLabel.setFont (lf.getSidePanelTitleFont (sidePanel));
  89. titleLabel.setJustificationType (lf.getSidePanelTitleJustification (sidePanel));
  90. titleLabel.setColour (Label::textColourId, owner.findColour (SidePanel::titleTextColour));
  91. auto normal = sidePanel.findColour (SidePanel::dismissButtonNormalColour);
  92. auto over = sidePanel.findColour (SidePanel::dismissButtonOverColour);
  93. auto down = sidePanel.findColour (SidePanel::dismissButtonDownColour);
  94. homeButton.setColours (normal, over, down);
  95. settingsButton.setColours (normal, over, down);
  96. }
  97. MainComponent& owner;
  98. Label titleLabel;
  99. ShapeButton homeButton { "Home", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack },
  100. settingsButton { "Settings", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack };
  101. };
  102. //==============================================================================
  103. class DemoList : public Component,
  104. public ListBoxModel
  105. {
  106. public:
  107. DemoList (DemoContentComponent& holder)
  108. : demoHolder (holder)
  109. {
  110. addAndMakeVisible (demos);
  111. demos.setModel (this);
  112. demos.setRowHeight (40);
  113. }
  114. void resized() override
  115. {
  116. demos.setBounds (getLocalBounds());
  117. }
  118. //==============================================================================
  119. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  120. {
  121. Rectangle<int> bounds (0, 0, width, height);
  122. auto textColour = findColour (Label::textColourId);
  123. g.setColour (textColour.withAlpha (0.4f));
  124. if (rowNumber == 0)
  125. g.fillRect (bounds.removeFromTop (2).reduced (7, 0));
  126. g.fillRect (bounds.removeFromBottom (2).reduced (7, 0));
  127. if (rowIsSelected)
  128. {
  129. g.setColour (findColour (TextEditor::highlightColourId).withAlpha (0.4f));
  130. g.fillRect (bounds);
  131. textColour = findColour (TextEditor::highlightedTextColourId);
  132. }
  133. g.setColour (textColour);
  134. if (selectedCategory.isEmpty())
  135. {
  136. if (isPositiveAndBelow (rowNumber, JUCEDemos::getCategories().size()))
  137. g.drawFittedText (JUCEDemos::getCategories()[(size_t) rowNumber].name,
  138. bounds, Justification::centred, 1);
  139. }
  140. else
  141. {
  142. auto& category = JUCEDemos::getCategory (selectedCategory);
  143. if (isPositiveAndBelow (rowNumber, category.demos.size()))
  144. g.drawFittedText (category.demos[(size_t) rowNumber].demoFile.getFileName(),
  145. bounds, Justification::centred, 1);
  146. }
  147. }
  148. int getNumRows() override
  149. {
  150. return (int) (selectedCategory.isEmpty() ? JUCEDemos::getCategories().size()
  151. : JUCEDemos::getCategory (selectedCategory).demos.size());
  152. }
  153. void selectedRowsChanged (int row) override
  154. {
  155. if (row < 0)
  156. return;
  157. if (selectedCategory.isEmpty())
  158. showCategory (JUCEDemos::getCategories()[(size_t) row].name);
  159. else
  160. demoHolder.setDemo (selectedCategory, row);
  161. }
  162. //==============================================================================
  163. void showCategory (const String& categoryName) noexcept
  164. {
  165. selectedCategory = categoryName;
  166. demos.deselectAllRows();
  167. demos.setHeaderComponent (categoryName.isEmpty() ? nullptr
  168. : new Header (*this));
  169. demos.updateContent();
  170. }
  171. private:
  172. String selectedCategory;
  173. DemoContentComponent& demoHolder;
  174. ListBox demos;
  175. struct Header : public Component
  176. {
  177. Header (DemoList& o)
  178. : owner (o)
  179. {
  180. setSize (0, 30);
  181. }
  182. void paint (Graphics& g) override
  183. {
  184. g.setColour (findColour (Label::textColourId));
  185. g.drawFittedText ("<", getLocalBounds().reduced (20, 0), Justification::centredLeft, 1);
  186. }
  187. void mouseDown (const MouseEvent&) override
  188. {
  189. owner.showCategory ({});
  190. }
  191. DemoList& owner;
  192. };
  193. };
  194. //==============================================================================
  195. MainComponent::MainComponent()
  196. {
  197. contentComponent.reset (new DemoContentComponent (*this, [this] (bool isHeavyweight)
  198. {
  199. #if (JUCE_ANDROID || JUCE_IOS)
  200. demosPanel.showOrHide (false);
  201. #endif
  202. if (isHeavyweight)
  203. {
  204. #if JUCE_MAC && USE_COREGRAPHICS_RENDERING
  205. setRenderingEngine (1);
  206. #else
  207. setRenderingEngine (0);
  208. #endif
  209. }
  210. isShowingHeavyweightDemo = isHeavyweight;
  211. resized();
  212. }));
  213. demosPanel.setContent (new DemoList (*contentComponent));
  214. demosPanel.setTitleBarComponent (new SidePanelHeader (*this), true);
  215. addAndMakeVisible (contentComponent.get());
  216. addAndMakeVisible (showDemosButton);
  217. addAndMakeVisible (demosPanel);
  218. showDemosButton.onClick = [this] { demosPanel.showOrHide (true); };
  219. demosPanel.onPanelMove = [this]
  220. {
  221. sidePanelWidth = jmax (0, demosPanel.getRight());
  222. if (isShowingHeavyweightDemo)
  223. resized();
  224. };
  225. demosPanel.onPanelShowHide = [this] (bool isShowing)
  226. {
  227. if (isShowing)
  228. {
  229. sidePanelWidth = jmax (0, demosPanel.getWidth());
  230. if (isShowingHeavyweightDemo)
  231. resized();
  232. }
  233. else
  234. {
  235. sidePanelWidth = 0;
  236. if (isShowingHeavyweightDemo)
  237. Timer::callAfterDelay (250, [this] { resized(); });
  238. }
  239. };
  240. contentComponent->showHomeScreen();
  241. setOpaque (true);
  242. setSize (800, 800);
  243. }
  244. MainComponent::~MainComponent()
  245. {
  246. contentComponent->clearCurrentDemo();
  247. }
  248. //==============================================================================
  249. void MainComponent::paint (Graphics& g)
  250. {
  251. g.fillAll (findColour (ResizableWindow::backgroundColourId));
  252. }
  253. void MainComponent::resized()
  254. {
  255. auto bounds = getLocalBounds();
  256. showDemosButton.setBounds (0, 0, 150, contentComponent->getTabBarDepth());
  257. if (isShowingHeavyweightDemo)
  258. {
  259. bounds.removeFromLeft (sidePanelWidth);
  260. contentComponent->setTabBarIndent (jmax (0, 150 - sidePanelWidth));
  261. }
  262. else
  263. {
  264. contentComponent->setTabBarIndent (150);
  265. }
  266. contentComponent->setBounds (bounds);
  267. }
  268. void MainComponent::homeButtonClicked()
  269. {
  270. if (auto* list = dynamic_cast<DemoList*> (demosPanel.getContent()))
  271. list->showCategory ({});
  272. if (contentComponent != nullptr)
  273. {
  274. if (contentComponent->isShowingHomeScreen())
  275. return;
  276. contentComponent->showHomeScreen();
  277. if (isShowingHeavyweightDemo)
  278. {
  279. isShowingHeavyweightDemo = false;
  280. resized();
  281. }
  282. }
  283. }
  284. void MainComponent::settingsButtonClicked()
  285. {
  286. if (contentComponent != nullptr)
  287. contentComponent->setCurrentTabIndex (2);
  288. }
  289. void MainComponent::setRenderingEngine (int renderingEngineIndex)
  290. {
  291. if (renderingEngineIndex != currentRenderingEngineIdx)
  292. updateRenderingEngine (renderingEngineIndex);
  293. }
  294. void MainComponent::parentHierarchyChanged()
  295. {
  296. auto* newPeer = getPeer();
  297. if (peer != newPeer)
  298. {
  299. peer = newPeer;
  300. auto previousRenderingEngine = renderingEngines[currentRenderingEngineIdx];
  301. renderingEngines.clear();
  302. if (peer != nullptr)
  303. renderingEngines = peer->getAvailableRenderingEngines();
  304. renderingEngines.add ("OpenGL Renderer");
  305. currentRenderingEngineIdx = renderingEngines.indexOf (previousRenderingEngine);
  306. if (currentRenderingEngineIdx < 0)
  307. {
  308. #if JUCE_ANDROID
  309. currentRenderingEngineIdx = (renderingEngines.size() - 1);
  310. #else
  311. currentRenderingEngineIdx = peer->getCurrentRenderingEngine();
  312. #endif
  313. }
  314. updateRenderingEngine (currentRenderingEngineIdx);
  315. }
  316. }
  317. void MainComponent::updateRenderingEngine (int renderingEngineIndex)
  318. {
  319. if (renderingEngineIndex == (renderingEngines.size() - 1))
  320. {
  321. if (isShowingHeavyweightDemo)
  322. return;
  323. openGLContext.attachTo (*getTopLevelComponent());
  324. }
  325. else
  326. {
  327. openGLContext.detach();
  328. peer->setCurrentRenderingEngine (renderingEngineIndex);
  329. }
  330. currentRenderingEngineIdx = renderingEngineIndex;
  331. }