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.

647 lines
23KB

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