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.

765 lines
31KB

  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 <typeinfo>
  20. #include "JuceDemoHeader.h"
  21. //==============================================================================
  22. struct AlphabeticDemoSorter
  23. {
  24. static int compareElements (const JuceDemoTypeBase* first, const JuceDemoTypeBase* second)
  25. {
  26. return first->name.compare (second->name);
  27. }
  28. };
  29. JuceDemoTypeBase::JuceDemoTypeBase (const String& demoName) : name (demoName)
  30. {
  31. AlphabeticDemoSorter sorter;
  32. getDemoTypeList().addSorted (sorter, this);
  33. }
  34. JuceDemoTypeBase::~JuceDemoTypeBase()
  35. {
  36. getDemoTypeList().removeFirstMatchingValue (this);
  37. }
  38. Array<JuceDemoTypeBase*>& JuceDemoTypeBase::getDemoTypeList()
  39. {
  40. static Array<JuceDemoTypeBase*> list;
  41. return list;
  42. }
  43. //==============================================================================
  44. #if JUCE_WINDOWS || JUCE_LINUX || JUCE_MAC
  45. // Just add a simple icon to the Window system tray area or Mac menu bar..
  46. struct DemoTaskbarComponent : public SystemTrayIconComponent,
  47. private Timer
  48. {
  49. DemoTaskbarComponent()
  50. {
  51. setIconImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  52. setIconTooltip ("Juce Demo App!");
  53. }
  54. void mouseDown (const MouseEvent&) override
  55. {
  56. // On OSX, there can be problems launching a menu when we're not the foreground
  57. // process, so just in case, we'll first make our process active, and then use a
  58. // timer to wait a moment before opening our menu, which gives the OS some time to
  59. // get its act together and bring our windows to the front.
  60. Process::makeForegroundProcess();
  61. startTimer (50);
  62. }
  63. // This is invoked when the menu is clicked or dismissed
  64. static void menuInvocationCallback (int chosenItemID, DemoTaskbarComponent*)
  65. {
  66. if (chosenItemID == 1)
  67. JUCEApplication::getInstance()->systemRequestedQuit();
  68. }
  69. void timerCallback() override
  70. {
  71. stopTimer();
  72. PopupMenu m;
  73. m.addItem (1, "Quit the Juce demo");
  74. // It's always better to open menus asynchronously when possible.
  75. m.showMenuAsync (PopupMenu::Options(),
  76. ModalCallbackFunction::forComponent (menuInvocationCallback, this));
  77. }
  78. };
  79. #endif
  80. bool juceDemoRepaintDebuggingActive = false;
  81. //==============================================================================
  82. class ContentComponent : public Component,
  83. public ListBoxModel,
  84. public ApplicationCommandTarget
  85. {
  86. public:
  87. ContentComponent()
  88. {
  89. // set lookAndFeel colour properties
  90. lookAndFeelV3.setColour (Label::textColourId, Colours::white);
  91. lookAndFeelV3.setColour (Label::textColourId, Colours::white);
  92. lookAndFeelV3.setColour (ToggleButton::textColourId, Colours::white);
  93. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV4);
  94. demoList.setModel (this);
  95. updateDemoListColours();
  96. demoList.selectRow (0);
  97. if (Desktop::getInstance().getMainMouseSource().isTouch())
  98. demoList.getViewport()->setScrollOnDragEnabled (true);
  99. addAndMakeVisible (demoList);
  100. }
  101. ~ContentComponent()
  102. {
  103. // before deleting our lookandfeel object, make sure it's no longer in use
  104. LookAndFeel::setDefaultLookAndFeel (nullptr);
  105. }
  106. void clearCurrentDemo()
  107. {
  108. currentDemo = nullptr;
  109. }
  110. void resized() override
  111. {
  112. auto r = getLocalBounds();
  113. if (r.getWidth() > 600)
  114. {
  115. demoList.setBounds (r.removeFromLeft (210));
  116. demoList.setRowHeight (20);
  117. }
  118. else
  119. {
  120. demoList.setBounds (r.removeFromLeft (130));
  121. demoList.setRowHeight (30);
  122. }
  123. if (currentDemo != nullptr)
  124. currentDemo->setBounds (r);
  125. }
  126. int getNumRows() override
  127. {
  128. return JuceDemoTypeBase::getDemoTypeList().size();
  129. }
  130. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  131. {
  132. if (rowIsSelected)
  133. g.fillAll (Colours::deepskyblue);
  134. if (auto* type = JuceDemoTypeBase::getDemoTypeList() [rowNumber])
  135. {
  136. auto name = type->name.trimCharactersAtStart ("0123456789").trimStart();
  137. AttributedString a;
  138. a.setJustification (Justification::centredLeft);
  139. String category;
  140. if (name.containsChar (':'))
  141. {
  142. category = name.upToFirstOccurrenceOf (":", true, false);
  143. name = name.fromFirstOccurrenceOf (":", false, false).trim();
  144. if (height > 20)
  145. category << "\n";
  146. else
  147. category << " ";
  148. }
  149. auto categoryColour = demoList.findColour (ListBox::outlineColourId);
  150. auto nameColour = demoList.findColour (ListBox::textColourId);
  151. if (category.isNotEmpty())
  152. a.append (category, Font (10.0f), categoryColour);
  153. a.append (name, Font (13.0f), nameColour);
  154. a.draw (g, Rectangle<int> (width + 10, height).reduced (6, 0).toFloat());
  155. }
  156. }
  157. void selectedRowsChanged (int lastRowSelected) override
  158. {
  159. if (auto* selectedDemoType = JuceDemoTypeBase::getDemoTypeList() [lastRowSelected])
  160. {
  161. currentDemo = nullptr;
  162. addAndMakeVisible (currentDemo = selectedDemoType->createComponent());
  163. currentDemo->setName (selectedDemoType->name);
  164. resized();
  165. }
  166. }
  167. MouseCursor getMouseCursorForRow (int /*row*/) override
  168. {
  169. return MouseCursor::PointingHandCursor;
  170. }
  171. int getCurrentPageIndex() const noexcept
  172. {
  173. if (currentDemo == nullptr)
  174. return -1;
  175. auto& demos = JuceDemoTypeBase::getDemoTypeList();
  176. for (int i = demos.size(); --i >= 0;)
  177. if (demos.getUnchecked (i)->name == currentDemo->getName())
  178. return i;
  179. return -1;
  180. }
  181. void moveDemoPages (int numPagesToMove)
  182. {
  183. demoList.selectRow (negativeAwareModulo (getCurrentPageIndex() + numPagesToMove,
  184. JuceDemoTypeBase::getDemoTypeList().size()));
  185. }
  186. bool isShowingOpenGLDemo() const
  187. {
  188. return currentDemo != nullptr
  189. && currentDemo->getName().contains ("OpenGL")
  190. && ! isShowingOpenGL2DDemo();
  191. }
  192. bool isShowingOpenGL2DDemo() const
  193. {
  194. return currentDemo != nullptr && currentDemo->getName().contains ("OpenGL 2D");
  195. }
  196. private:
  197. ListBox demoList;
  198. ScopedPointer<Component> currentDemo;
  199. LookAndFeel_V1 lookAndFeelV1;
  200. LookAndFeel_V2 lookAndFeelV2;
  201. LookAndFeel_V3 lookAndFeelV3;
  202. LookAndFeel_V4 lookAndFeelV4;
  203. //==============================================================================
  204. // The following methods implement the ApplicationCommandTarget interface, allowing
  205. // this window to publish a set of actions it can perform, and which can be mapped
  206. // onto menus, keypresses, etc.
  207. ApplicationCommandTarget* getNextCommandTarget() override
  208. {
  209. // this will return the next parent component that is an ApplicationCommandTarget (in this
  210. // case, there probably isn't one, but it's best to use this method in your own apps).
  211. return findFirstTargetParentComponent();
  212. }
  213. void getAllCommands (Array<CommandID>& commands) override
  214. {
  215. // this returns the set of all commands that this target can perform..
  216. const CommandID ids[] = { MainAppWindow::showPreviousDemo,
  217. MainAppWindow::showNextDemo,
  218. MainAppWindow::welcome,
  219. MainAppWindow::componentsAnimation,
  220. MainAppWindow::componentsDialogBoxes,
  221. MainAppWindow::componentsKeyMappings,
  222. MainAppWindow::componentsMDI,
  223. MainAppWindow::componentsPropertyEditors,
  224. MainAppWindow::componentsTransforms,
  225. MainAppWindow::componentsWebBrowsers,
  226. MainAppWindow::componentsWidgets,
  227. MainAppWindow::useLookAndFeelV1,
  228. MainAppWindow::useLookAndFeelV2,
  229. MainAppWindow::useLookAndFeelV3,
  230. MainAppWindow::useLookAndFeelV4Dark,
  231. MainAppWindow::useLookAndFeelV4Midnight,
  232. MainAppWindow::useLookAndFeelV4Grey,
  233. MainAppWindow::useLookAndFeelV4Light,
  234. MainAppWindow::toggleRepaintDebugging,
  235. #if ! JUCE_LINUX
  236. MainAppWindow::goToKioskMode,
  237. #endif
  238. MainAppWindow::useNativeTitleBar
  239. };
  240. commands.addArray (ids, numElementsInArray (ids));
  241. const CommandID engineIDs[] = { MainAppWindow::renderingEngineOne,
  242. MainAppWindow::renderingEngineTwo,
  243. MainAppWindow::renderingEngineThree };
  244. auto renderingEngines = MainAppWindow::getMainAppWindow()->getRenderingEngines();
  245. commands.addArray (engineIDs, renderingEngines.size());
  246. }
  247. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  248. {
  249. const String generalCategory ("General");
  250. const String demosCategory ("Demos");
  251. switch (commandID)
  252. {
  253. case MainAppWindow::showPreviousDemo:
  254. result.setInfo ("Previous Demo", "Shows the previous demo in the list", demosCategory, 0);
  255. result.addDefaultKeypress ('-', ModifierKeys::commandModifier);
  256. break;
  257. case MainAppWindow::showNextDemo:
  258. result.setInfo ("Next Demo", "Shows the next demo in the list", demosCategory, 0);
  259. result.addDefaultKeypress ('=', ModifierKeys::commandModifier);
  260. break;
  261. case MainAppWindow::welcome:
  262. result.setInfo ("Welcome Demo", "Shows the 'Welcome' demo", demosCategory, 0);
  263. result.addDefaultKeypress ('1', ModifierKeys::commandModifier);
  264. break;
  265. case MainAppWindow::componentsAnimation:
  266. result.setInfo ("Animation Demo", "Shows the 'Animation' demo", demosCategory, 0);
  267. result.addDefaultKeypress ('2', ModifierKeys::commandModifier);
  268. break;
  269. case MainAppWindow::componentsDialogBoxes:
  270. result.setInfo ("Dialog Boxes Demo", "Shows the 'Dialog Boxes' demo", demosCategory, 0);
  271. result.addDefaultKeypress ('3', ModifierKeys::commandModifier);
  272. break;
  273. case MainAppWindow::componentsKeyMappings:
  274. result.setInfo ("Key Mappings Demo", "Shows the 'Key Mappings' demo", demosCategory, 0);
  275. result.addDefaultKeypress ('4', ModifierKeys::commandModifier);
  276. break;
  277. case MainAppWindow::componentsMDI:
  278. result.setInfo ("Multi-Document Demo", "Shows the 'Multi-Document' demo", demosCategory, 0);
  279. result.addDefaultKeypress ('5', ModifierKeys::commandModifier);
  280. break;
  281. case MainAppWindow::componentsPropertyEditors:
  282. result.setInfo ("Property Editor Demo", "Shows the 'Property Editor' demo", demosCategory, 0);
  283. result.addDefaultKeypress ('6', ModifierKeys::commandModifier);
  284. break;
  285. case MainAppWindow::componentsTransforms:
  286. result.setInfo ("Component Transforms Demo", "Shows the 'Transforms' demo", demosCategory, 0);
  287. result.addDefaultKeypress ('7', ModifierKeys::commandModifier);
  288. break;
  289. case MainAppWindow::componentsWebBrowsers:
  290. result.setInfo ("Web Browser Demo", "Shows the 'Web Browser' demo", demosCategory, 0);
  291. result.addDefaultKeypress ('8', ModifierKeys::commandModifier);
  292. break;
  293. case MainAppWindow::componentsWidgets:
  294. result.setInfo ("Widgets Demo", "Shows the 'Widgets' demo", demosCategory, 0);
  295. result.addDefaultKeypress ('9', ModifierKeys::commandModifier);
  296. break;
  297. case MainAppWindow::renderingEngineOne:
  298. case MainAppWindow::renderingEngineTwo:
  299. case MainAppWindow::renderingEngineThree:
  300. {
  301. auto& mainWindow = *MainAppWindow::getMainAppWindow();
  302. auto engines = mainWindow.getRenderingEngines();
  303. const int index = commandID - MainAppWindow::renderingEngineOne;
  304. result.setInfo ("Use " + engines[index], "Uses the " + engines[index] + " engine to render the UI", generalCategory, 0);
  305. result.setTicked (mainWindow.getActiveRenderingEngine() == index);
  306. result.addDefaultKeypress ('1' + index, ModifierKeys::noModifiers);
  307. break;
  308. }
  309. case MainAppWindow::useLookAndFeelV1:
  310. result.setInfo ("Use LookAndFeel_V1", String(), generalCategory, 0);
  311. result.addDefaultKeypress ('i', ModifierKeys::commandModifier);
  312. result.setTicked (isLookAndFeelSelected<LookAndFeel_V1>());
  313. break;
  314. case MainAppWindow::useLookAndFeelV2:
  315. result.setInfo ("Use LookAndFeel_V2", String(), generalCategory, 0);
  316. result.addDefaultKeypress ('o', ModifierKeys::commandModifier);
  317. result.setTicked (isLookAndFeelSelected<LookAndFeel_V2>());
  318. break;
  319. case MainAppWindow::useLookAndFeelV3:
  320. result.setInfo ("Use LookAndFeel_V3", String(), generalCategory, 0);
  321. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  322. result.setTicked (isLookAndFeelSelected<LookAndFeel_V3>());
  323. break;
  324. case MainAppWindow::useLookAndFeelV4Dark:
  325. result.setInfo ("Use LookAndFeel_V4 Dark", String(), generalCategory, 0);
  326. result.addDefaultKeypress ('k', ModifierKeys::commandModifier);
  327. result.setTicked (isColourSchemeActive (LookAndFeel_V4::getDarkColourScheme()));
  328. break;
  329. case MainAppWindow::useLookAndFeelV4Midnight:
  330. result.setInfo ("Use LookAndFeel_V4 Midnight", String(), generalCategory, 0);
  331. result.setTicked (isColourSchemeActive (LookAndFeel_V4::getMidnightColourScheme()));
  332. break;
  333. case MainAppWindow::useLookAndFeelV4Grey:
  334. result.setInfo ("Use LookAndFeel_V4 Grey", String(), generalCategory, 0);
  335. result.setTicked (isColourSchemeActive (LookAndFeel_V4::getGreyColourScheme()));
  336. break;
  337. case MainAppWindow::useLookAndFeelV4Light:
  338. result.setInfo ("Use LookAndFeel_V4 Light", String(), generalCategory, 0);
  339. result.setTicked (isColourSchemeActive (LookAndFeel_V4::getLightColourScheme()));
  340. break;
  341. case MainAppWindow::toggleRepaintDebugging:
  342. result.setInfo ("Toggle repaint display", String(), generalCategory, 0);
  343. result.addDefaultKeypress ('r', ModifierKeys());
  344. result.setTicked (juceDemoRepaintDebuggingActive);
  345. break;
  346. case MainAppWindow::useNativeTitleBar:
  347. {
  348. result.setInfo ("Use native window title bar", String(), generalCategory, 0);
  349. result.addDefaultKeypress ('n', ModifierKeys::commandModifier);
  350. bool nativeTitlebar = false;
  351. if (auto* mainWindow = MainAppWindow::getMainAppWindow())
  352. nativeTitlebar = mainWindow->isUsingNativeTitleBar();
  353. result.setTicked (nativeTitlebar);
  354. break;
  355. }
  356. #if ! JUCE_LINUX
  357. case MainAppWindow::goToKioskMode:
  358. result.setInfo ("Show full-screen kiosk mode", String(), generalCategory, 0);
  359. result.addDefaultKeypress ('f', ModifierKeys::commandModifier);
  360. result.setTicked (Desktop::getInstance().getKioskModeComponent() != 0);
  361. break;
  362. #endif
  363. default:
  364. break;
  365. }
  366. }
  367. bool perform (const InvocationInfo& info) override
  368. {
  369. if (auto* mainWindow = MainAppWindow::getMainAppWindow())
  370. {
  371. switch (info.commandID)
  372. {
  373. case MainAppWindow::showPreviousDemo: moveDemoPages (-1); break;
  374. case MainAppWindow::showNextDemo: moveDemoPages ( 1); break;
  375. case MainAppWindow::welcome:
  376. case MainAppWindow::componentsAnimation:
  377. case MainAppWindow::componentsDialogBoxes:
  378. case MainAppWindow::componentsKeyMappings:
  379. case MainAppWindow::componentsMDI:
  380. case MainAppWindow::componentsPropertyEditors:
  381. case MainAppWindow::componentsTransforms:
  382. case MainAppWindow::componentsWebBrowsers:
  383. case MainAppWindow::componentsWidgets:
  384. demoList.selectRow (info.commandID - MainAppWindow::welcome);
  385. break;
  386. case MainAppWindow::renderingEngineOne:
  387. case MainAppWindow::renderingEngineTwo:
  388. case MainAppWindow::renderingEngineThree:
  389. mainWindow->setRenderingEngine (info.commandID - MainAppWindow::renderingEngineOne);
  390. break;
  391. case MainAppWindow::useLookAndFeelV1:
  392. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV1);
  393. updateDemoListColours();
  394. break;
  395. case MainAppWindow::useLookAndFeelV2:
  396. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV2);
  397. updateDemoListColours();
  398. break;
  399. case MainAppWindow::useLookAndFeelV3:
  400. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV3);
  401. updateDemoListColours();
  402. break;
  403. case MainAppWindow::useLookAndFeelV4Dark:
  404. lookAndFeelV4.setColourScheme (LookAndFeel_V4::getDarkColourScheme());
  405. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV4);
  406. updateDemoListColours();
  407. break;
  408. case MainAppWindow::useLookAndFeelV4Midnight:
  409. lookAndFeelV4.setColourScheme (LookAndFeel_V4::getMidnightColourScheme());
  410. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV4);
  411. updateDemoListColours();
  412. break;
  413. case MainAppWindow::useLookAndFeelV4Grey:
  414. lookAndFeelV4.setColourScheme (LookAndFeel_V4::getGreyColourScheme());
  415. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV4);
  416. updateDemoListColours();
  417. break;
  418. case MainAppWindow::useLookAndFeelV4Light:
  419. lookAndFeelV4.setColourScheme (LookAndFeel_V4::getLightColourScheme());
  420. LookAndFeel::setDefaultLookAndFeel (&lookAndFeelV4);
  421. updateDemoListColours();
  422. break;
  423. case MainAppWindow::toggleRepaintDebugging:
  424. juceDemoRepaintDebuggingActive = ! juceDemoRepaintDebuggingActive;
  425. mainWindow->repaint();
  426. break;
  427. case MainAppWindow::useNativeTitleBar:
  428. mainWindow->setUsingNativeTitleBar (! mainWindow->isUsingNativeTitleBar());
  429. break;
  430. #if ! JUCE_LINUX
  431. case MainAppWindow::goToKioskMode:
  432. {
  433. auto& desktop = Desktop::getInstance();
  434. if (desktop.getKioskModeComponent() == nullptr)
  435. desktop.setKioskModeComponent (getTopLevelComponent());
  436. else
  437. desktop.setKioskModeComponent (nullptr);
  438. break;
  439. }
  440. #endif
  441. default:
  442. return false;
  443. }
  444. }
  445. return true;
  446. }
  447. template <typename LookAndFeelType>
  448. bool isLookAndFeelSelected()
  449. {
  450. LookAndFeel& lf = getLookAndFeel();
  451. return typeid (LookAndFeelType) == typeid (lf);
  452. }
  453. bool isColourSchemeActive (LookAndFeel_V4::ColourScheme scheme)
  454. {
  455. if (auto* v4 = dynamic_cast<LookAndFeel_V4*> (&LookAndFeel::getDefaultLookAndFeel()))
  456. if (v4->getCurrentColourScheme() == scheme)
  457. return true;
  458. return false;
  459. }
  460. void updateDemoListColours()
  461. {
  462. demoList.setColour (ListBox::backgroundColourId,
  463. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::widgetBackground, Colour::greyLevel (0.2f)));
  464. demoList.setColour (ListBox::textColourId,
  465. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  466. Colours::white.withAlpha (0.9f)));
  467. demoList.setColour (ListBox::outlineColourId,
  468. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  469. Colour::greyLevel (0.5f)).interpolatedWith (Colours::red, 0.4f));
  470. }
  471. };
  472. //==============================================================================
  473. static ScopedPointer<ApplicationCommandManager> applicationCommandManager;
  474. static ScopedPointer<AudioDeviceManager> sharedAudioDeviceManager;
  475. MainAppWindow::MainAppWindow()
  476. : DocumentWindow (JUCEApplication::getInstance()->getApplicationName(),
  477. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  478. DocumentWindow::allButtons)
  479. {
  480. setUsingNativeTitleBar (true);
  481. setResizable (true, false);
  482. setResizeLimits (400, 400, 10000, 10000);
  483. #if JUCE_IOS || JUCE_ANDROID
  484. setFullScreen (true);
  485. #else
  486. setBounds ((int) (0.1f * getParentWidth()),
  487. (int) (0.1f * getParentHeight()),
  488. jmax (850, (int) (0.5f * getParentWidth())),
  489. jmax (600, (int) (0.7f * getParentHeight())));
  490. #endif
  491. contentComponent = new ContentComponent();
  492. setContentNonOwned (contentComponent, false);
  493. setVisible (true);
  494. // this lets the command manager use keypresses that arrive in our window to send out commands
  495. addKeyListener (getApplicationCommandManager().getKeyMappings());
  496. #if JUCE_WINDOWS || JUCE_LINUX || JUCE_MAC
  497. taskbarIcon = new DemoTaskbarComponent();
  498. #endif
  499. #if JUCE_ANDROID
  500. setOpenGLRenderingEngine();
  501. #endif
  502. triggerAsyncUpdate();
  503. }
  504. MainAppWindow::~MainAppWindow()
  505. {
  506. contentComponent->clearCurrentDemo();
  507. clearContentComponent();
  508. contentComponent = nullptr;
  509. applicationCommandManager = nullptr;
  510. sharedAudioDeviceManager = nullptr;
  511. #if JUCE_OPENGL
  512. openGLContext.detach();
  513. #endif
  514. }
  515. void MainAppWindow::closeButtonPressed()
  516. {
  517. JUCEApplication::getInstance()->systemRequestedQuit();
  518. }
  519. ApplicationCommandManager& MainAppWindow::getApplicationCommandManager()
  520. {
  521. if (applicationCommandManager == nullptr)
  522. applicationCommandManager = new ApplicationCommandManager();
  523. return *applicationCommandManager;
  524. }
  525. AudioDeviceManager& MainAppWindow::getSharedAudioDeviceManager()
  526. {
  527. if (sharedAudioDeviceManager == nullptr)
  528. {
  529. sharedAudioDeviceManager = new AudioDeviceManager();
  530. RuntimePermissions::request (RuntimePermissions::recordAudio, runtimePermissionsCallback);
  531. }
  532. return *sharedAudioDeviceManager;
  533. }
  534. void MainAppWindow::runtimePermissionsCallback (bool wasGranted)
  535. {
  536. int numInputChannels = wasGranted ? 2 : 0;
  537. sharedAudioDeviceManager->initialise (numInputChannels, 2, nullptr, true, String(), nullptr);
  538. }
  539. MainAppWindow* MainAppWindow::getMainAppWindow()
  540. {
  541. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  542. if (auto* maw = dynamic_cast<MainAppWindow*> (TopLevelWindow::getTopLevelWindow (i)))
  543. return maw;
  544. return nullptr;
  545. }
  546. void MainAppWindow::handleAsyncUpdate()
  547. {
  548. // This registers all of our commands with the command manager but has to be done after the window has
  549. // been created so we can find the number of rendering engines available
  550. auto& commandManager = MainAppWindow::getApplicationCommandManager();
  551. commandManager.registerAllCommandsForTarget (contentComponent);
  552. commandManager.registerAllCommandsForTarget (JUCEApplication::getInstance());
  553. }
  554. void MainAppWindow::showMessageBubble (const String& text)
  555. {
  556. currentBubbleMessage = new BubbleMessageComponent (500);
  557. getContentComponent()->addChildComponent (currentBubbleMessage);
  558. AttributedString attString;
  559. attString.append (text, Font (15.0f));
  560. currentBubbleMessage->showAt ({ getLocalBounds().getCentreX(), 10, 1, 1 },
  561. attString,
  562. 500, // numMillisecondsBeforeRemoving
  563. true, // removeWhenMouseClicked
  564. false); // deleteSelfAfterUse
  565. }
  566. static const char* openGLRendererName = "OpenGL Renderer";
  567. StringArray MainAppWindow::getRenderingEngines() const
  568. {
  569. StringArray renderingEngines;
  570. if (auto* peer = getPeer())
  571. renderingEngines = peer->getAvailableRenderingEngines();
  572. #if JUCE_OPENGL
  573. renderingEngines.add (openGLRendererName);
  574. #endif
  575. return renderingEngines;
  576. }
  577. void MainAppWindow::setRenderingEngine (int index)
  578. {
  579. showMessageBubble (getRenderingEngines()[index]);
  580. #if JUCE_OPENGL
  581. if (getRenderingEngines()[index] == openGLRendererName
  582. && contentComponent != nullptr
  583. && ! contentComponent->isShowingOpenGLDemo())
  584. {
  585. openGLContext.attachTo (*getTopLevelComponent());
  586. return;
  587. }
  588. openGLContext.detach();
  589. #endif
  590. if (auto* peer = getPeer())
  591. peer->setCurrentRenderingEngine (index);
  592. }
  593. void MainAppWindow::setOpenGLRenderingEngine()
  594. {
  595. setRenderingEngine (getRenderingEngines().indexOf (openGLRendererName));
  596. }
  597. int MainAppWindow::getActiveRenderingEngine() const
  598. {
  599. #if JUCE_OPENGL
  600. if (openGLContext.isAttached())
  601. return getRenderingEngines().indexOf (openGLRendererName);
  602. #endif
  603. if (auto* peer = getPeer())
  604. return peer->getCurrentRenderingEngine();
  605. return 0;
  606. }
  607. Path MainAppWindow::getJUCELogoPath()
  608. {
  609. return Drawable::parseSVGPath (
  610. "M250,301.3c-37.2,0-67.5-30.3-67.5-67.5s30.3-67.5,67.5-67.5s67.5,30.3,67.5,67.5S287.2,301.3,250,301.3zM250,170.8c-34.7,0-63,28.3-63,63s28.3,63,63,63s63-28.3,63-63S284.7,170.8,250,170.8z"
  611. "M247.8,180.4c0-2.3-1.8-4.1-4.1-4.1c-0.2,0-0.3,0-0.5,0c-10.6,1.2-20.6,5.4-29,12c-1,0.8-1.5,1.8-1.6,2.9c-0.1,1.2,0.4,2.3,1.3,3.2l32.5,32.5c0.5,0.5,1.4,0.1,1.4-0.6V180.4z"
  612. "M303.2,231.6c1.2,0,2.3-0.4,3.1-1.2c0.9-0.9,1.3-2.1,1.1-3.3c-1.2-10.6-5.4-20.6-12-29c-0.8-1-1.9-1.6-3.2-1.6c-1.1,0-2.1,0.5-3,1.3l-32.5,32.5c-0.5,0.5-0.1,1.4,0.6,1.4L303.2,231.6z"
  613. "M287.4,191.3c-0.1-1.1-0.6-2.2-1.6-2.9c-8.4-6.6-18.4-10.8-29-12c-0.2,0-0.3,0-0.5,0c-2.3,0-4.1,1.9-4.1,4.1v46c0,0.7,0.9,1.1,1.4,0.6l32.5-32.5C287,193.6,287.5,192.5,287.4,191.3z"
  614. "M252.2,287.2c0,2.3,1.8,4.1,4.1,4.1c0.2,0,0.3,0,0.5,0c10.6-1.2,20.6-5.4,29-12c1-0.8,1.5-1.8,1.6-2.9c0.1-1.2-0.4-2.3-1.3-3.2l-32.5-32.5c-0.5-0.5-1.4-0.1-1.4,0.6V287.2z"
  615. "M292.3,271.2L292.3,271.2c1.2,0,2.4-0.6,3.2-1.6c6.6-8.4,10.8-18.4,12-29c0.1-1.2-0.3-2.4-1.1-3.3c-0.8-0.8-1.9-1.2-3.1-1.2l-45.9,0c-0.7,0-1.1,0.9-0.6,1.4l32.5,32.5C290.2,270.8,291.2,271.2,292.3,271.2z"
  616. "M207.7,196.4c-1.2,0-2.4,0.6-3.2,1.6c-6.6,8.4-10.8,18.4-12,29c-0.1,1.2,0.3,2.4,1.1,3.3c0.8,0.8,1.9,1.2,3.1,1.2l45.9,0c0.7,0,1.1-0.9,0.6-1.4l-32.5-32.5C209.8,196.8,208.8,196.4,207.7,196.4z"
  617. "M242.6,236.1l-45.9,0c-1.2,0-2.3,0.4-3.1,1.2c-0.9,0.9-1.3,2.1-1.1,3.3c1.2,10.6,5.4,20.6,12,29c0.8,1,1.9,1.6,3.2,1.6c1.1,0,2.1-0.5,3-1.3c0,0,0,0,0,0l32.5-32.5C243.7,236.9,243.4,236.1,242.6,236.1z"
  618. "M213.8,273.1L213.8,273.1c-0.9,0.9-1.3,2-1.3,3.2c0.1,1.1,0.6,2.2,1.6,2.9c8.4,6.6,18.4,10.8,29,12c0.2,0,0.3,0,0.5,0h0c1.2,0,2.3-0.5,3.1-1.4c0.7-0.8,1-1.8,1-2.9v-45.9c0-0.7-0.9-1.1-1.4-0.6l-13.9,13.9L213.8,273.1z"
  619. "M197.2,353c-4.1,0-7.4-1.5-10.4-5.4l4-3.5c2,2.6,3.9,3.6,6.4,3.6c4.4,0,7.4-3.3,7.4-8.3v-24.7h5.6v24.7C210.2,347.5,204.8,353,197.2,353z"
  620. "M232.4,353c-8.1,0-15-6-15-15.8v-22.5h5.6v22.2c0,6.6,3.9,10.8,9.5,10.8c5.6,0,9.5-4.3,9.5-10.8v-22.2h5.6v22.5C247.5,347,240.5,353,232.4,353z"
  621. "M272,353c-10.8,0-19.5-8.6-19.5-19.3c0-10.8,8.8-19.3,19.5-19.3c4.8,0,9,1.6,12.3,4.4l-3.3,4.1c-3.4-2.4-5.7-3.2-8.9-3.2c-7.7,0-13.8,6.2-13.8,14.1c0,7.9,6.1,14.1,13.8,14.1c3.1,0,5.6-1,8.8-3.2l3.3,4.1C280.1,351.9,276.4,353,272,353z"
  622. "M290.4,352.5v-37.8h22.7v5H296v11.2h16.5v5H296v11.6h17.2v5H290.4z");
  623. }