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.

649 lines
23KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include <JuceHeader.h>
  19. #include "MainHostWindow.h"
  20. #include "../Plugins/InternalPlugins.h"
  21. //==============================================================================
  22. class MainHostWindow::PluginListWindow : public DocumentWindow
  23. {
  24. public:
  25. PluginListWindow (MainHostWindow& mw, AudioPluginFormatManager& pluginFormatManager)
  26. : DocumentWindow ("Available Plugins",
  27. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  28. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  29. owner (mw)
  30. {
  31. auto deadMansPedalFile = getAppProperties().getUserSettings()
  32. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList");
  33. setContentOwned (new PluginListComponent (pluginFormatManager,
  34. owner.knownPluginList,
  35. deadMansPedalFile,
  36. getAppProperties().getUserSettings(), true), true);
  37. setResizable (true, false);
  38. setResizeLimits (300, 400, 800, 1500);
  39. setTopLeftPosition (60, 60);
  40. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("listWindowPos"));
  41. setVisible (true);
  42. }
  43. ~PluginListWindow() override
  44. {
  45. getAppProperties().getUserSettings()->setValue ("listWindowPos", getWindowStateAsString());
  46. clearContentComponent();
  47. }
  48. void closeButtonPressed() override
  49. {
  50. owner.pluginListWindow = nullptr;
  51. }
  52. private:
  53. MainHostWindow& owner;
  54. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginListWindow)
  55. };
  56. //==============================================================================
  57. MainHostWindow::MainHostWindow()
  58. : DocumentWindow (JUCEApplication::getInstance()->getApplicationName(),
  59. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  60. DocumentWindow::allButtons)
  61. {
  62. formatManager.addDefaultFormats();
  63. formatManager.addFormat (new InternalPluginFormat());
  64. auto safeThis = SafePointer<MainHostWindow> (this);
  65. RuntimePermissions::request (RuntimePermissions::recordAudio,
  66. [safeThis] (bool granted) mutable
  67. {
  68. auto savedState = getAppProperties().getUserSettings()->getXmlValue ("audioDeviceState");
  69. safeThis->deviceManager.initialise (granted ? 256 : 0, 256, savedState.get(), true);
  70. });
  71. #if JUCE_IOS || JUCE_ANDROID
  72. setFullScreen (true);
  73. #else
  74. setResizable (true, false);
  75. setResizeLimits (500, 400, 10000, 10000);
  76. centreWithSize (800, 600);
  77. #endif
  78. graphHolder.reset (new GraphDocumentComponent (formatManager, deviceManager, knownPluginList));
  79. setContentNonOwned (graphHolder.get(), false);
  80. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("mainWindowPos"));
  81. setVisible (true);
  82. InternalPluginFormat internalFormat;
  83. internalTypes = internalFormat.getAllTypes();
  84. if (auto savedPluginList = getAppProperties().getUserSettings()->getXmlValue ("pluginList"))
  85. knownPluginList.recreateFromXml (*savedPluginList);
  86. for (auto& t : internalTypes)
  87. knownPluginList.addType (t);
  88. pluginSortMethod = (KnownPluginList::SortMethod) getAppProperties().getUserSettings()
  89. ->getIntValue ("pluginSortMethod", KnownPluginList::sortByManufacturer);
  90. knownPluginList.addChangeListener (this);
  91. if (auto* g = graphHolder->graph.get())
  92. g->addChangeListener (this);
  93. addKeyListener (getCommandManager().getKeyMappings());
  94. Process::setPriority (Process::HighPriority);
  95. #if JUCE_IOS || JUCE_ANDROID
  96. graphHolder->burgerMenu.setModel (this);
  97. #else
  98. #if JUCE_MAC
  99. setMacMainMenu (this);
  100. #else
  101. setMenuBar (this);
  102. #endif
  103. #endif
  104. getCommandManager().setFirstCommandTarget (this);
  105. }
  106. MainHostWindow::~MainHostWindow()
  107. {
  108. pluginListWindow = nullptr;
  109. knownPluginList.removeChangeListener (this);
  110. if (auto* g = graphHolder->graph.get())
  111. g->removeChangeListener (this);
  112. getAppProperties().getUserSettings()->setValue ("mainWindowPos", getWindowStateAsString());
  113. clearContentComponent();
  114. #if ! (JUCE_ANDROID || JUCE_IOS)
  115. #if JUCE_MAC
  116. setMacMainMenu (nullptr);
  117. #else
  118. setMenuBar (nullptr);
  119. #endif
  120. #endif
  121. graphHolder = nullptr;
  122. }
  123. void MainHostWindow::closeButtonPressed()
  124. {
  125. tryToQuitApplication();
  126. }
  127. struct AsyncQuitRetrier : private Timer
  128. {
  129. AsyncQuitRetrier() { startTimer (500); }
  130. void timerCallback() override
  131. {
  132. stopTimer();
  133. delete this;
  134. if (auto app = JUCEApplicationBase::getInstance())
  135. app->systemRequestedQuit();
  136. }
  137. };
  138. void MainHostWindow::tryToQuitApplication()
  139. {
  140. if (graphHolder->closeAnyOpenPluginWindows())
  141. {
  142. // Really important thing to note here: if the last call just deleted any plugin windows,
  143. // we won't exit immediately - instead we'll use our AsyncQuitRetrier to let the message
  144. // loop run for another brief moment, then try again. This will give any plugins a chance
  145. // to flush any GUI events that may have been in transit before the app forces them to
  146. // be unloaded
  147. new AsyncQuitRetrier();
  148. }
  149. else if (ModalComponentManager::getInstance()->cancelAllModalComponents())
  150. {
  151. new AsyncQuitRetrier();
  152. }
  153. #if JUCE_ANDROID || JUCE_IOS
  154. else if (graphHolder == nullptr || graphHolder->graph->saveDocument (PluginGraph::getDefaultGraphDocumentOnMobile()))
  155. #else
  156. else if (graphHolder == nullptr || graphHolder->graph->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  157. #endif
  158. {
  159. // Some plug-ins do not want [NSApp stop] to be called
  160. // before the plug-ins are not deallocated.
  161. graphHolder->releaseGraph();
  162. JUCEApplication::quit();
  163. }
  164. }
  165. void MainHostWindow::changeListenerCallback (ChangeBroadcaster* changed)
  166. {
  167. if (changed == &knownPluginList)
  168. {
  169. menuItemsChanged();
  170. // save the plugin list every time it gets changed, so that if we're scanning
  171. // and it crashes, we've still saved the previous ones
  172. if (auto savedPluginList = std::unique_ptr<XmlElement> (knownPluginList.createXml()))
  173. {
  174. getAppProperties().getUserSettings()->setValue ("pluginList", savedPluginList.get());
  175. getAppProperties().saveIfNeeded();
  176. }
  177. }
  178. else if (graphHolder != nullptr && changed == graphHolder->graph.get())
  179. {
  180. auto title = JUCEApplication::getInstance()->getApplicationName();
  181. auto f = graphHolder->graph->getFile();
  182. if (f.existsAsFile())
  183. title = f.getFileName() + " - " + title;
  184. setName (title);
  185. }
  186. }
  187. StringArray MainHostWindow::getMenuBarNames()
  188. {
  189. StringArray names;
  190. names.add ("File");
  191. names.add ("Plugins");
  192. names.add ("Options");
  193. names.add ("Windows");
  194. return names;
  195. }
  196. PopupMenu MainHostWindow::getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  197. {
  198. PopupMenu menu;
  199. if (topLevelMenuIndex == 0)
  200. {
  201. // "File" menu
  202. #if ! (JUCE_IOS || JUCE_ANDROID)
  203. menu.addCommandItem (&getCommandManager(), CommandIDs::newFile);
  204. menu.addCommandItem (&getCommandManager(), CommandIDs::open);
  205. #endif
  206. RecentlyOpenedFilesList recentFiles;
  207. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  208. ->getValue ("recentFilterGraphFiles"));
  209. PopupMenu recentFilesMenu;
  210. recentFiles.createPopupMenuItems (recentFilesMenu, 100, true, true);
  211. menu.addSubMenu ("Open recent file", recentFilesMenu);
  212. #if ! (JUCE_IOS || JUCE_ANDROID)
  213. menu.addCommandItem (&getCommandManager(), CommandIDs::save);
  214. menu.addCommandItem (&getCommandManager(), CommandIDs::saveAs);
  215. #endif
  216. menu.addSeparator();
  217. menu.addCommandItem (&getCommandManager(), StandardApplicationCommandIDs::quit);
  218. }
  219. else if (topLevelMenuIndex == 1)
  220. {
  221. // "Plugins" menu
  222. PopupMenu pluginsMenu;
  223. addPluginsToMenu (pluginsMenu);
  224. menu.addSubMenu ("Create plugin", pluginsMenu);
  225. menu.addSeparator();
  226. menu.addItem (250, "Delete all plugins");
  227. }
  228. else if (topLevelMenuIndex == 2)
  229. {
  230. // "Options" menu
  231. menu.addCommandItem (&getCommandManager(), CommandIDs::showPluginListEditor);
  232. PopupMenu sortTypeMenu;
  233. sortTypeMenu.addItem (200, "List plugins in default order", true, pluginSortMethod == KnownPluginList::defaultOrder);
  234. sortTypeMenu.addItem (201, "List plugins in alphabetical order", true, pluginSortMethod == KnownPluginList::sortAlphabetically);
  235. sortTypeMenu.addItem (202, "List plugins by category", true, pluginSortMethod == KnownPluginList::sortByCategory);
  236. sortTypeMenu.addItem (203, "List plugins by manufacturer", true, pluginSortMethod == KnownPluginList::sortByManufacturer);
  237. sortTypeMenu.addItem (204, "List plugins based on the directory structure", true, pluginSortMethod == KnownPluginList::sortByFileSystemLocation);
  238. menu.addSubMenu ("Plugin menu type", sortTypeMenu);
  239. menu.addSeparator();
  240. menu.addCommandItem (&getCommandManager(), CommandIDs::showAudioSettings);
  241. menu.addCommandItem (&getCommandManager(), CommandIDs::toggleDoublePrecision);
  242. menu.addSeparator();
  243. menu.addCommandItem (&getCommandManager(), CommandIDs::aboutBox);
  244. }
  245. else if (topLevelMenuIndex == 3)
  246. {
  247. menu.addCommandItem (&getCommandManager(), CommandIDs::allWindowsForward);
  248. }
  249. return menu;
  250. }
  251. void MainHostWindow::menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  252. {
  253. if (menuItemID == 250)
  254. {
  255. if (graphHolder != nullptr)
  256. if (auto* graph = graphHolder->graph.get())
  257. graph->clear();
  258. }
  259. #if ! (JUCE_ANDROID || JUCE_IOS)
  260. else if (menuItemID >= 100 && menuItemID < 200)
  261. {
  262. RecentlyOpenedFilesList recentFiles;
  263. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  264. ->getValue ("recentFilterGraphFiles"));
  265. if (graphHolder != nullptr)
  266. if (auto* graph = graphHolder->graph.get())
  267. if (graph != nullptr && graph->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  268. graph->loadFrom (recentFiles.getFile (menuItemID - 100), true);
  269. }
  270. #endif
  271. else if (menuItemID >= 200 && menuItemID < 210)
  272. {
  273. if (menuItemID == 200) pluginSortMethod = KnownPluginList::defaultOrder;
  274. else if (menuItemID == 201) pluginSortMethod = KnownPluginList::sortAlphabetically;
  275. else if (menuItemID == 202) pluginSortMethod = KnownPluginList::sortByCategory;
  276. else if (menuItemID == 203) pluginSortMethod = KnownPluginList::sortByManufacturer;
  277. else if (menuItemID == 204) pluginSortMethod = KnownPluginList::sortByFileSystemLocation;
  278. getAppProperties().getUserSettings()->setValue ("pluginSortMethod", (int) pluginSortMethod);
  279. menuItemsChanged();
  280. }
  281. else
  282. {
  283. if (KnownPluginList::getIndexChosenByMenu (pluginDescriptions, menuItemID) >= 0)
  284. createPlugin (getChosenType (menuItemID), { proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  285. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f) });
  286. }
  287. }
  288. void MainHostWindow::menuBarActivated (bool isActivated)
  289. {
  290. if (isActivated && graphHolder != nullptr)
  291. graphHolder->unfocusKeyboardComponent();
  292. }
  293. void MainHostWindow::createPlugin (const PluginDescription& desc, Point<int> pos)
  294. {
  295. if (graphHolder != nullptr)
  296. graphHolder->createNewPlugin (desc, pos);
  297. }
  298. void MainHostWindow::addPluginsToMenu (PopupMenu& m)
  299. {
  300. if (graphHolder != nullptr)
  301. {
  302. int i = 0;
  303. for (auto& t : internalTypes)
  304. m.addItem (++i, t.name + " (" + t.pluginFormatName + ")");
  305. }
  306. m.addSeparator();
  307. pluginDescriptions = knownPluginList.getTypes();
  308. // This avoids showing the internal types again later on in the list
  309. pluginDescriptions.removeIf ([] (PluginDescription& desc)
  310. {
  311. return desc.pluginFormatName == InternalPluginFormat::getIdentifier();
  312. });
  313. KnownPluginList::addToMenu (m, pluginDescriptions, pluginSortMethod);
  314. }
  315. PluginDescription MainHostWindow::getChosenType (const int menuID) const
  316. {
  317. if (menuID >= 1 && menuID < (int) (1 + internalTypes.size()))
  318. return internalTypes[(size_t) (menuID - 1)];
  319. return pluginDescriptions[KnownPluginList::getIndexChosenByMenu (pluginDescriptions, menuID)];
  320. }
  321. //==============================================================================
  322. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  323. {
  324. return findFirstTargetParentComponent();
  325. }
  326. void MainHostWindow::getAllCommands (Array<CommandID>& commands)
  327. {
  328. // this returns the set of all commands that this target can perform..
  329. const CommandID ids[] = {
  330. #if ! (JUCE_IOS || JUCE_ANDROID)
  331. CommandIDs::newFile,
  332. CommandIDs::open,
  333. CommandIDs::save,
  334. CommandIDs::saveAs,
  335. #endif
  336. CommandIDs::showPluginListEditor,
  337. CommandIDs::showAudioSettings,
  338. CommandIDs::toggleDoublePrecision,
  339. CommandIDs::aboutBox,
  340. CommandIDs::allWindowsForward
  341. };
  342. commands.addArray (ids, numElementsInArray (ids));
  343. }
  344. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  345. {
  346. const String category ("General");
  347. switch (commandID)
  348. {
  349. #if ! (JUCE_IOS || JUCE_ANDROID)
  350. case CommandIDs::newFile:
  351. result.setInfo ("New", "Creates a new filter graph file", category, 0);
  352. result.defaultKeypresses.add(KeyPress('n', ModifierKeys::commandModifier, 0));
  353. break;
  354. case CommandIDs::open:
  355. result.setInfo ("Open...", "Opens a filter graph file", category, 0);
  356. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  357. break;
  358. case CommandIDs::save:
  359. result.setInfo ("Save", "Saves the current graph to a file", category, 0);
  360. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  361. break;
  362. case CommandIDs::saveAs:
  363. result.setInfo ("Save As...",
  364. "Saves a copy of the current graph to a file",
  365. category, 0);
  366. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  367. break;
  368. #endif
  369. case CommandIDs::showPluginListEditor:
  370. result.setInfo ("Edit the list of available plug-Ins...", String(), category, 0);
  371. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  372. break;
  373. case CommandIDs::showAudioSettings:
  374. result.setInfo ("Change the audio device settings", String(), category, 0);
  375. result.addDefaultKeypress ('a', ModifierKeys::commandModifier);
  376. break;
  377. case CommandIDs::toggleDoublePrecision:
  378. updatePrecisionMenuItem (result);
  379. break;
  380. case CommandIDs::aboutBox:
  381. result.setInfo ("About...", String(), category, 0);
  382. break;
  383. case CommandIDs::allWindowsForward:
  384. result.setInfo ("All Windows Forward", "Bring all plug-in windows forward", category, 0);
  385. result.addDefaultKeypress ('w', ModifierKeys::commandModifier);
  386. break;
  387. default:
  388. break;
  389. }
  390. }
  391. bool MainHostWindow::perform (const InvocationInfo& info)
  392. {
  393. switch (info.commandID)
  394. {
  395. #if ! (JUCE_IOS || JUCE_ANDROID)
  396. case CommandIDs::newFile:
  397. if (graphHolder != nullptr && graphHolder->graph != nullptr && graphHolder->graph->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  398. graphHolder->graph->newDocument();
  399. break;
  400. case CommandIDs::open:
  401. if (graphHolder != nullptr && graphHolder->graph != nullptr && graphHolder->graph->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  402. graphHolder->graph->loadFromUserSpecifiedFile (true);
  403. break;
  404. case CommandIDs::save:
  405. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  406. graphHolder->graph->save (true, true);
  407. break;
  408. case CommandIDs::saveAs:
  409. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  410. graphHolder->graph->saveAs (File(), true, true, true);
  411. break;
  412. #endif
  413. case CommandIDs::showPluginListEditor:
  414. if (pluginListWindow == nullptr)
  415. pluginListWindow.reset (new PluginListWindow (*this, formatManager));
  416. pluginListWindow->toFront (true);
  417. break;
  418. case CommandIDs::showAudioSettings:
  419. showAudioSettings();
  420. break;
  421. case CommandIDs::toggleDoublePrecision:
  422. if (auto* props = getAppProperties().getUserSettings())
  423. {
  424. bool newIsDoublePrecision = ! isDoublePrecisionProcessing();
  425. props->setValue ("doublePrecisionProcessing", var (newIsDoublePrecision));
  426. {
  427. ApplicationCommandInfo cmdInfo (info.commandID);
  428. updatePrecisionMenuItem (cmdInfo);
  429. menuItemsChanged();
  430. }
  431. if (graphHolder != nullptr)
  432. graphHolder->setDoublePrecision (newIsDoublePrecision);
  433. }
  434. break;
  435. case CommandIDs::aboutBox:
  436. // TODO
  437. break;
  438. case CommandIDs::allWindowsForward:
  439. {
  440. auto& desktop = Desktop::getInstance();
  441. for (int i = 0; i < desktop.getNumComponents(); ++i)
  442. desktop.getComponent (i)->toBehind (this);
  443. break;
  444. }
  445. default:
  446. return false;
  447. }
  448. return true;
  449. }
  450. void MainHostWindow::showAudioSettings()
  451. {
  452. auto* audioSettingsComp = new AudioDeviceSelectorComponent (deviceManager,
  453. 0, 256,
  454. 0, 256,
  455. true, true,
  456. true, false);
  457. audioSettingsComp->setSize (500, 450);
  458. DialogWindow::LaunchOptions o;
  459. o.content.setOwned (audioSettingsComp);
  460. o.dialogTitle = "Audio Settings";
  461. o.componentToCentreAround = this;
  462. o.dialogBackgroundColour = getLookAndFeel().findColour (ResizableWindow::backgroundColourId);
  463. o.escapeKeyTriggersCloseButton = true;
  464. o.useNativeTitleBar = false;
  465. o.resizable = false;
  466. auto* w = o.create();
  467. auto safeThis = SafePointer<MainHostWindow> (this);
  468. w->enterModalState (true,
  469. ModalCallbackFunction::create
  470. ([safeThis] (int)
  471. {
  472. auto audioState = safeThis->deviceManager.createStateXml();
  473. getAppProperties().getUserSettings()->setValue ("audioDeviceState", audioState.get());
  474. getAppProperties().getUserSettings()->saveIfNeeded();
  475. if (safeThis->graphHolder != nullptr)
  476. if (safeThis->graphHolder->graph != nullptr)
  477. safeThis->graphHolder->graph->graph.removeIllegalConnections();
  478. }), true);
  479. }
  480. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  481. {
  482. return true;
  483. }
  484. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  485. {
  486. }
  487. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  488. {
  489. }
  490. void MainHostWindow::fileDragExit (const StringArray&)
  491. {
  492. }
  493. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  494. {
  495. if (graphHolder != nullptr)
  496. {
  497. #if ! (JUCE_ANDROID || JUCE_IOS)
  498. if (files.size() == 1 && File (files[0]).hasFileExtension (PluginGraph::getFilenameSuffix()))
  499. {
  500. if (auto* g = graphHolder->graph.get())
  501. if (g->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  502. g->loadFrom (File (files[0]), true);
  503. }
  504. else
  505. #endif
  506. {
  507. OwnedArray<PluginDescription> typesFound;
  508. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  509. auto pos = graphHolder->getLocalPoint (this, Point<int> (x, y));
  510. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  511. if (auto* desc = typesFound.getUnchecked(i))
  512. createPlugin (*desc, pos);
  513. }
  514. }
  515. }
  516. bool MainHostWindow::isDoublePrecisionProcessing()
  517. {
  518. if (auto* props = getAppProperties().getUserSettings())
  519. return props->getBoolValue ("doublePrecisionProcessing", false);
  520. return false;
  521. }
  522. void MainHostWindow::updatePrecisionMenuItem (ApplicationCommandInfo& info)
  523. {
  524. info.setInfo ("Double floating point precision rendering", String(), "General", 0);
  525. info.setTicked (isDoublePrecisionProcessing());
  526. }