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.

945 lines
32KB

  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. constexpr const char* scanModeKey = "pluginScanMode";
  22. class CustomPluginScanner : public KnownPluginList::CustomScanner,
  23. private ChangeListener
  24. {
  25. public:
  26. CustomPluginScanner()
  27. {
  28. if (auto* file = getAppProperties().getUserSettings())
  29. file->addChangeListener (this);
  30. changeListenerCallback (nullptr);
  31. }
  32. ~CustomPluginScanner() override
  33. {
  34. if (auto* file = getAppProperties().getUserSettings())
  35. file->removeChangeListener (this);
  36. }
  37. bool findPluginTypesFor (AudioPluginFormat& format,
  38. OwnedArray<PluginDescription>& result,
  39. const String& fileOrIdentifier) override
  40. {
  41. if (scanInProcess)
  42. {
  43. superprocess = nullptr;
  44. format.findAllTypesForFile (result, fileOrIdentifier);
  45. return true;
  46. }
  47. if (superprocess == nullptr)
  48. {
  49. superprocess = std::make_unique<Superprocess> (*this);
  50. std::unique_lock<std::mutex> lock (mutex);
  51. connectionLost = false;
  52. }
  53. MemoryBlock block;
  54. MemoryOutputStream stream { block, true };
  55. stream.writeString (format.getName());
  56. stream.writeString (fileOrIdentifier);
  57. if (superprocess->sendMessageToWorker (block))
  58. {
  59. std::unique_lock<std::mutex> lock (mutex);
  60. gotResponse = false;
  61. pluginDescription = nullptr;
  62. for (;;)
  63. {
  64. if (condvar.wait_for (lock,
  65. std::chrono::milliseconds (50),
  66. [this] { return gotResponse || shouldExit(); }))
  67. {
  68. break;
  69. }
  70. }
  71. if (shouldExit())
  72. {
  73. superprocess = nullptr;
  74. return true;
  75. }
  76. if (connectionLost)
  77. {
  78. superprocess = nullptr;
  79. return false;
  80. }
  81. if (pluginDescription != nullptr)
  82. {
  83. for (const auto* item : pluginDescription->getChildIterator())
  84. {
  85. auto desc = std::make_unique<PluginDescription>();
  86. if (desc->loadFromXml (*item))
  87. result.add (std::move (desc));
  88. }
  89. }
  90. return true;
  91. }
  92. superprocess = nullptr;
  93. return false;
  94. }
  95. void scanFinished() override
  96. {
  97. superprocess = nullptr;
  98. }
  99. private:
  100. class Superprocess : private ChildProcessCoordinator
  101. {
  102. public:
  103. explicit Superprocess (CustomPluginScanner& o)
  104. : owner (o)
  105. {
  106. launchWorkerProcess (File::getSpecialLocation (File::currentExecutableFile), processUID, 0, 0);
  107. }
  108. using ChildProcessCoordinator::sendMessageToWorker;
  109. private:
  110. void handleMessageFromWorker (const MemoryBlock& mb) override
  111. {
  112. auto xml = parseXML (mb.toString());
  113. const std::lock_guard<std::mutex> lock (owner.mutex);
  114. owner.pluginDescription = std::move (xml);
  115. owner.gotResponse = true;
  116. owner.condvar.notify_one();
  117. }
  118. void handleConnectionLost() override
  119. {
  120. const std::lock_guard<std::mutex> lock (owner.mutex);
  121. owner.pluginDescription = nullptr;
  122. owner.gotResponse = true;
  123. owner.connectionLost = true;
  124. owner.condvar.notify_one();
  125. }
  126. CustomPluginScanner& owner;
  127. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Superprocess)
  128. };
  129. void changeListenerCallback (ChangeBroadcaster*) override
  130. {
  131. if (auto* file = getAppProperties().getUserSettings())
  132. scanInProcess = (file->getIntValue (scanModeKey) == 0);
  133. }
  134. std::unique_ptr<Superprocess> superprocess;
  135. std::mutex mutex;
  136. std::condition_variable condvar;
  137. std::unique_ptr<XmlElement> pluginDescription;
  138. bool gotResponse = false;
  139. bool connectionLost = false;
  140. std::atomic<bool> scanInProcess { true };
  141. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomPluginScanner)
  142. };
  143. //==============================================================================
  144. class CustomPluginListComponent : public PluginListComponent
  145. {
  146. public:
  147. CustomPluginListComponent (AudioPluginFormatManager& manager,
  148. KnownPluginList& listToRepresent,
  149. const File& pedal,
  150. PropertiesFile* props,
  151. bool async)
  152. : PluginListComponent (manager, listToRepresent, pedal, props, async)
  153. {
  154. addAndMakeVisible (validationModeLabel);
  155. addAndMakeVisible (validationModeBox);
  156. validationModeLabel.attachToComponent (&validationModeBox, true);
  157. validationModeLabel.setJustificationType (Justification::right);
  158. validationModeLabel.setSize (100, 30);
  159. auto unusedId = 1;
  160. for (const auto mode : { "In-process", "Out-of-process" })
  161. validationModeBox.addItem (mode, unusedId++);
  162. validationModeBox.setSelectedItemIndex (getAppProperties().getUserSettings()->getIntValue (scanModeKey));
  163. validationModeBox.onChange = [this]
  164. {
  165. getAppProperties().getUserSettings()->setValue (scanModeKey, validationModeBox.getSelectedItemIndex());
  166. };
  167. resized();
  168. }
  169. void resized() override
  170. {
  171. PluginListComponent::resized();
  172. const auto& buttonBounds = getOptionsButton().getBounds();
  173. validationModeBox.setBounds (buttonBounds.withWidth (130).withRightX (getWidth() - buttonBounds.getX()));
  174. }
  175. private:
  176. Label validationModeLabel { {}, "Scan mode" };
  177. ComboBox validationModeBox;
  178. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomPluginListComponent)
  179. };
  180. //==============================================================================
  181. class MainHostWindow::PluginListWindow : public DocumentWindow
  182. {
  183. public:
  184. PluginListWindow (MainHostWindow& mw, AudioPluginFormatManager& pluginFormatManager)
  185. : DocumentWindow ("Available Plugins",
  186. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  187. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  188. owner (mw)
  189. {
  190. auto deadMansPedalFile = getAppProperties().getUserSettings()
  191. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList");
  192. setContentOwned (new CustomPluginListComponent (pluginFormatManager,
  193. owner.knownPluginList,
  194. deadMansPedalFile,
  195. getAppProperties().getUserSettings(),
  196. true), true);
  197. setResizable (true, false);
  198. setResizeLimits (300, 400, 800, 1500);
  199. setTopLeftPosition (60, 60);
  200. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("listWindowPos"));
  201. setVisible (true);
  202. }
  203. ~PluginListWindow() override
  204. {
  205. getAppProperties().getUserSettings()->setValue ("listWindowPos", getWindowStateAsString());
  206. clearContentComponent();
  207. }
  208. void closeButtonPressed() override
  209. {
  210. owner.pluginListWindow = nullptr;
  211. }
  212. private:
  213. MainHostWindow& owner;
  214. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginListWindow)
  215. };
  216. //==============================================================================
  217. MainHostWindow::MainHostWindow()
  218. : DocumentWindow (JUCEApplication::getInstance()->getApplicationName(),
  219. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  220. DocumentWindow::allButtons)
  221. {
  222. formatManager.addDefaultFormats();
  223. formatManager.addFormat (new InternalPluginFormat());
  224. auto safeThis = SafePointer<MainHostWindow> (this);
  225. RuntimePermissions::request (RuntimePermissions::recordAudio,
  226. [safeThis] (bool granted) mutable
  227. {
  228. auto savedState = getAppProperties().getUserSettings()->getXmlValue ("audioDeviceState");
  229. safeThis->deviceManager.initialise (granted ? 256 : 0, 256, savedState.get(), true);
  230. });
  231. #if JUCE_IOS || JUCE_ANDROID
  232. setFullScreen (true);
  233. #else
  234. setResizable (true, false);
  235. setResizeLimits (500, 400, 10000, 10000);
  236. centreWithSize (800, 600);
  237. #endif
  238. knownPluginList.setCustomScanner (std::make_unique<CustomPluginScanner>());
  239. graphHolder.reset (new GraphDocumentComponent (formatManager, deviceManager, knownPluginList));
  240. setContentNonOwned (graphHolder.get(), false);
  241. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("mainWindowPos"));
  242. setVisible (true);
  243. InternalPluginFormat internalFormat;
  244. internalTypes = internalFormat.getAllTypes();
  245. if (auto savedPluginList = getAppProperties().getUserSettings()->getXmlValue ("pluginList"))
  246. knownPluginList.recreateFromXml (*savedPluginList);
  247. for (auto& t : internalTypes)
  248. knownPluginList.addType (t);
  249. pluginSortMethod = (KnownPluginList::SortMethod) getAppProperties().getUserSettings()
  250. ->getIntValue ("pluginSortMethod", KnownPluginList::sortByManufacturer);
  251. knownPluginList.addChangeListener (this);
  252. if (auto* g = graphHolder->graph.get())
  253. g->addChangeListener (this);
  254. addKeyListener (getCommandManager().getKeyMappings());
  255. Process::setPriority (Process::HighPriority);
  256. #if JUCE_IOS || JUCE_ANDROID
  257. graphHolder->burgerMenu.setModel (this);
  258. #else
  259. #if JUCE_MAC
  260. setMacMainMenu (this);
  261. #else
  262. setMenuBar (this);
  263. #endif
  264. #endif
  265. getCommandManager().setFirstCommandTarget (this);
  266. }
  267. MainHostWindow::~MainHostWindow()
  268. {
  269. pluginListWindow = nullptr;
  270. knownPluginList.removeChangeListener (this);
  271. if (auto* g = graphHolder->graph.get())
  272. g->removeChangeListener (this);
  273. getAppProperties().getUserSettings()->setValue ("mainWindowPos", getWindowStateAsString());
  274. clearContentComponent();
  275. #if ! (JUCE_ANDROID || JUCE_IOS)
  276. #if JUCE_MAC
  277. setMacMainMenu (nullptr);
  278. #else
  279. setMenuBar (nullptr);
  280. #endif
  281. #endif
  282. graphHolder = nullptr;
  283. }
  284. void MainHostWindow::closeButtonPressed()
  285. {
  286. tryToQuitApplication();
  287. }
  288. struct AsyncQuitRetrier : private Timer
  289. {
  290. AsyncQuitRetrier() { startTimer (500); }
  291. void timerCallback() override
  292. {
  293. stopTimer();
  294. delete this;
  295. if (auto app = JUCEApplicationBase::getInstance())
  296. app->systemRequestedQuit();
  297. }
  298. };
  299. void MainHostWindow::tryToQuitApplication()
  300. {
  301. if (graphHolder->closeAnyOpenPluginWindows())
  302. {
  303. // Really important thing to note here: if the last call just deleted any plugin windows,
  304. // we won't exit immediately - instead we'll use our AsyncQuitRetrier to let the message
  305. // loop run for another brief moment, then try again. This will give any plugins a chance
  306. // to flush any GUI events that may have been in transit before the app forces them to
  307. // be unloaded
  308. new AsyncQuitRetrier();
  309. return;
  310. }
  311. if (ModalComponentManager::getInstance()->cancelAllModalComponents())
  312. {
  313. new AsyncQuitRetrier();
  314. return;
  315. }
  316. if (graphHolder != nullptr)
  317. {
  318. auto releaseAndQuit = [this]
  319. {
  320. // Some plug-ins do not want [NSApp stop] to be called
  321. // before the plug-ins are not deallocated.
  322. graphHolder->releaseGraph();
  323. JUCEApplication::quit();
  324. };
  325. #if JUCE_ANDROID || JUCE_IOS
  326. if (graphHolder->graph->saveDocument (PluginGraph::getDefaultGraphDocumentOnMobile()))
  327. releaseAndQuit();
  328. #else
  329. SafePointer<MainHostWindow> parent { this };
  330. graphHolder->graph->saveIfNeededAndUserAgreesAsync ([parent, releaseAndQuit] (FileBasedDocument::SaveResult r)
  331. {
  332. if (parent == nullptr)
  333. return;
  334. if (r == FileBasedDocument::savedOk)
  335. releaseAndQuit();
  336. });
  337. #endif
  338. return;
  339. }
  340. JUCEApplication::quit();
  341. }
  342. void MainHostWindow::changeListenerCallback (ChangeBroadcaster* changed)
  343. {
  344. if (changed == &knownPluginList)
  345. {
  346. menuItemsChanged();
  347. // save the plugin list every time it gets changed, so that if we're scanning
  348. // and it crashes, we've still saved the previous ones
  349. if (auto savedPluginList = std::unique_ptr<XmlElement> (knownPluginList.createXml()))
  350. {
  351. getAppProperties().getUserSettings()->setValue ("pluginList", savedPluginList.get());
  352. getAppProperties().saveIfNeeded();
  353. }
  354. }
  355. else if (graphHolder != nullptr && changed == graphHolder->graph.get())
  356. {
  357. auto title = JUCEApplication::getInstance()->getApplicationName();
  358. auto f = graphHolder->graph->getFile();
  359. if (f.existsAsFile())
  360. title = f.getFileName() + " - " + title;
  361. setName (title);
  362. }
  363. }
  364. StringArray MainHostWindow::getMenuBarNames()
  365. {
  366. StringArray names;
  367. names.add ("File");
  368. names.add ("Plugins");
  369. names.add ("Options");
  370. names.add ("Windows");
  371. return names;
  372. }
  373. PopupMenu MainHostWindow::getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  374. {
  375. PopupMenu menu;
  376. if (topLevelMenuIndex == 0)
  377. {
  378. // "File" menu
  379. #if ! (JUCE_IOS || JUCE_ANDROID)
  380. menu.addCommandItem (&getCommandManager(), CommandIDs::newFile);
  381. menu.addCommandItem (&getCommandManager(), CommandIDs::open);
  382. #endif
  383. RecentlyOpenedFilesList recentFiles;
  384. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  385. ->getValue ("recentFilterGraphFiles"));
  386. PopupMenu recentFilesMenu;
  387. recentFiles.createPopupMenuItems (recentFilesMenu, 100, true, true);
  388. menu.addSubMenu ("Open recent file", recentFilesMenu);
  389. #if ! (JUCE_IOS || JUCE_ANDROID)
  390. menu.addCommandItem (&getCommandManager(), CommandIDs::save);
  391. menu.addCommandItem (&getCommandManager(), CommandIDs::saveAs);
  392. #endif
  393. menu.addSeparator();
  394. menu.addCommandItem (&getCommandManager(), StandardApplicationCommandIDs::quit);
  395. }
  396. else if (topLevelMenuIndex == 1)
  397. {
  398. // "Plugins" menu
  399. PopupMenu pluginsMenu;
  400. addPluginsToMenu (pluginsMenu);
  401. menu.addSubMenu ("Create Plug-in", pluginsMenu);
  402. menu.addSeparator();
  403. menu.addItem (250, "Delete All Plug-ins");
  404. }
  405. else if (topLevelMenuIndex == 2)
  406. {
  407. // "Options" menu
  408. menu.addCommandItem (&getCommandManager(), CommandIDs::showPluginListEditor);
  409. PopupMenu sortTypeMenu;
  410. sortTypeMenu.addItem (200, "List Plug-ins in Default Order", true, pluginSortMethod == KnownPluginList::defaultOrder);
  411. sortTypeMenu.addItem (201, "List Plug-ins in Alphabetical Order", true, pluginSortMethod == KnownPluginList::sortAlphabetically);
  412. sortTypeMenu.addItem (202, "List Plug-ins by Category", true, pluginSortMethod == KnownPluginList::sortByCategory);
  413. sortTypeMenu.addItem (203, "List Plug-ins by Manufacturer", true, pluginSortMethod == KnownPluginList::sortByManufacturer);
  414. sortTypeMenu.addItem (204, "List Plug-ins Based on the Directory Structure", true, pluginSortMethod == KnownPluginList::sortByFileSystemLocation);
  415. menu.addSubMenu ("Plug-in Menu Type", sortTypeMenu);
  416. menu.addSeparator();
  417. menu.addCommandItem (&getCommandManager(), CommandIDs::showAudioSettings);
  418. menu.addCommandItem (&getCommandManager(), CommandIDs::toggleDoublePrecision);
  419. if (autoScaleOptionAvailable)
  420. menu.addCommandItem (&getCommandManager(), CommandIDs::autoScalePluginWindows);
  421. menu.addSeparator();
  422. menu.addCommandItem (&getCommandManager(), CommandIDs::aboutBox);
  423. }
  424. else if (topLevelMenuIndex == 3)
  425. {
  426. menu.addCommandItem (&getCommandManager(), CommandIDs::allWindowsForward);
  427. }
  428. return menu;
  429. }
  430. void MainHostWindow::menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  431. {
  432. if (menuItemID == 250)
  433. {
  434. if (graphHolder != nullptr)
  435. if (auto* graph = graphHolder->graph.get())
  436. graph->clear();
  437. }
  438. #if ! (JUCE_ANDROID || JUCE_IOS)
  439. else if (menuItemID >= 100 && menuItemID < 200)
  440. {
  441. RecentlyOpenedFilesList recentFiles;
  442. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  443. ->getValue ("recentFilterGraphFiles"));
  444. if (graphHolder != nullptr)
  445. {
  446. if (auto* graph = graphHolder->graph.get())
  447. {
  448. SafePointer<MainHostWindow> parent { this };
  449. graph->saveIfNeededAndUserAgreesAsync ([parent, recentFiles, menuItemID] (FileBasedDocument::SaveResult r)
  450. {
  451. if (parent == nullptr)
  452. return;
  453. if (r == FileBasedDocument::savedOk)
  454. parent->graphHolder->graph->loadFrom (recentFiles.getFile (menuItemID - 100), true);
  455. });
  456. }
  457. }
  458. }
  459. #endif
  460. else if (menuItemID >= 200 && menuItemID < 210)
  461. {
  462. if (menuItemID == 200) pluginSortMethod = KnownPluginList::defaultOrder;
  463. else if (menuItemID == 201) pluginSortMethod = KnownPluginList::sortAlphabetically;
  464. else if (menuItemID == 202) pluginSortMethod = KnownPluginList::sortByCategory;
  465. else if (menuItemID == 203) pluginSortMethod = KnownPluginList::sortByManufacturer;
  466. else if (menuItemID == 204) pluginSortMethod = KnownPluginList::sortByFileSystemLocation;
  467. getAppProperties().getUserSettings()->setValue ("pluginSortMethod", (int) pluginSortMethod);
  468. menuItemsChanged();
  469. }
  470. else
  471. {
  472. if (KnownPluginList::getIndexChosenByMenu (pluginDescriptions, menuItemID) >= 0)
  473. createPlugin (getChosenType (menuItemID), { proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  474. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f) });
  475. }
  476. }
  477. void MainHostWindow::menuBarActivated (bool isActivated)
  478. {
  479. if (isActivated && graphHolder != nullptr)
  480. graphHolder->unfocusKeyboardComponent();
  481. }
  482. void MainHostWindow::createPlugin (const PluginDescription& desc, Point<int> pos)
  483. {
  484. if (graphHolder != nullptr)
  485. graphHolder->createNewPlugin (desc, pos);
  486. }
  487. void MainHostWindow::addPluginsToMenu (PopupMenu& m)
  488. {
  489. if (graphHolder != nullptr)
  490. {
  491. int i = 0;
  492. for (auto& t : internalTypes)
  493. m.addItem (++i, t.name + " (" + t.pluginFormatName + ")");
  494. }
  495. m.addSeparator();
  496. pluginDescriptions = knownPluginList.getTypes();
  497. // This avoids showing the internal types again later on in the list
  498. pluginDescriptions.removeIf ([] (PluginDescription& desc)
  499. {
  500. return desc.pluginFormatName == InternalPluginFormat::getIdentifier();
  501. });
  502. KnownPluginList::addToMenu (m, pluginDescriptions, pluginSortMethod);
  503. }
  504. PluginDescription MainHostWindow::getChosenType (const int menuID) const
  505. {
  506. if (menuID >= 1 && menuID < (int) (1 + internalTypes.size()))
  507. return internalTypes[(size_t) (menuID - 1)];
  508. return pluginDescriptions[KnownPluginList::getIndexChosenByMenu (pluginDescriptions, menuID)];
  509. }
  510. //==============================================================================
  511. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  512. {
  513. return findFirstTargetParentComponent();
  514. }
  515. void MainHostWindow::getAllCommands (Array<CommandID>& commands)
  516. {
  517. // this returns the set of all commands that this target can perform..
  518. const CommandID ids[] = {
  519. #if ! (JUCE_IOS || JUCE_ANDROID)
  520. CommandIDs::newFile,
  521. CommandIDs::open,
  522. CommandIDs::save,
  523. CommandIDs::saveAs,
  524. #endif
  525. CommandIDs::showPluginListEditor,
  526. CommandIDs::showAudioSettings,
  527. CommandIDs::toggleDoublePrecision,
  528. CommandIDs::aboutBox,
  529. CommandIDs::allWindowsForward,
  530. CommandIDs::autoScalePluginWindows
  531. };
  532. commands.addArray (ids, numElementsInArray (ids));
  533. }
  534. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  535. {
  536. const String category ("General");
  537. switch (commandID)
  538. {
  539. #if ! (JUCE_IOS || JUCE_ANDROID)
  540. case CommandIDs::newFile:
  541. result.setInfo ("New", "Creates a new filter graph file", category, 0);
  542. result.defaultKeypresses.add(KeyPress('n', ModifierKeys::commandModifier, 0));
  543. break;
  544. case CommandIDs::open:
  545. result.setInfo ("Open...", "Opens a filter graph file", category, 0);
  546. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  547. break;
  548. case CommandIDs::save:
  549. result.setInfo ("Save", "Saves the current graph to a file", category, 0);
  550. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  551. break;
  552. case CommandIDs::saveAs:
  553. result.setInfo ("Save As...",
  554. "Saves a copy of the current graph to a file",
  555. category, 0);
  556. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  557. break;
  558. #endif
  559. case CommandIDs::showPluginListEditor:
  560. result.setInfo ("Edit the List of Available Plug-ins...", {}, category, 0);
  561. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  562. break;
  563. case CommandIDs::showAudioSettings:
  564. result.setInfo ("Change the Audio Device Settings", {}, category, 0);
  565. result.addDefaultKeypress ('a', ModifierKeys::commandModifier);
  566. break;
  567. case CommandIDs::toggleDoublePrecision:
  568. updatePrecisionMenuItem (result);
  569. break;
  570. case CommandIDs::aboutBox:
  571. result.setInfo ("About...", {}, category, 0);
  572. break;
  573. case CommandIDs::allWindowsForward:
  574. result.setInfo ("All Windows Forward", "Bring all plug-in windows forward", category, 0);
  575. result.addDefaultKeypress ('w', ModifierKeys::commandModifier);
  576. break;
  577. case CommandIDs::autoScalePluginWindows:
  578. updateAutoScaleMenuItem (result);
  579. break;
  580. default:
  581. break;
  582. }
  583. }
  584. bool MainHostWindow::perform (const InvocationInfo& info)
  585. {
  586. switch (info.commandID)
  587. {
  588. #if ! (JUCE_IOS || JUCE_ANDROID)
  589. case CommandIDs::newFile:
  590. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  591. {
  592. SafePointer<MainHostWindow> parent { this };
  593. graphHolder->graph->saveIfNeededAndUserAgreesAsync ([parent] (FileBasedDocument::SaveResult r)
  594. {
  595. if (parent == nullptr)
  596. return;
  597. if (r == FileBasedDocument::savedOk)
  598. parent->graphHolder->graph->newDocument();
  599. });
  600. }
  601. break;
  602. case CommandIDs::open:
  603. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  604. {
  605. SafePointer<MainHostWindow> parent { this };
  606. graphHolder->graph->saveIfNeededAndUserAgreesAsync ([parent] (FileBasedDocument::SaveResult r)
  607. {
  608. if (parent == nullptr)
  609. return;
  610. if (r == FileBasedDocument::savedOk)
  611. parent->graphHolder->graph->loadFromUserSpecifiedFileAsync (true, [] (Result) {});
  612. });
  613. }
  614. break;
  615. case CommandIDs::save:
  616. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  617. graphHolder->graph->saveAsync (true, true, nullptr);
  618. break;
  619. case CommandIDs::saveAs:
  620. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  621. graphHolder->graph->saveAsAsync ({}, true, true, true, nullptr);
  622. break;
  623. #endif
  624. case CommandIDs::showPluginListEditor:
  625. if (pluginListWindow == nullptr)
  626. pluginListWindow.reset (new PluginListWindow (*this, formatManager));
  627. pluginListWindow->toFront (true);
  628. break;
  629. case CommandIDs::showAudioSettings:
  630. showAudioSettings();
  631. break;
  632. case CommandIDs::toggleDoublePrecision:
  633. if (auto* props = getAppProperties().getUserSettings())
  634. {
  635. auto newIsDoublePrecision = ! isDoublePrecisionProcessingEnabled();
  636. props->setValue ("doublePrecisionProcessing", var (newIsDoublePrecision));
  637. ApplicationCommandInfo cmdInfo (info.commandID);
  638. updatePrecisionMenuItem (cmdInfo);
  639. menuItemsChanged();
  640. if (graphHolder != nullptr)
  641. graphHolder->setDoublePrecision (newIsDoublePrecision);
  642. }
  643. break;
  644. case CommandIDs::autoScalePluginWindows:
  645. if (auto* props = getAppProperties().getUserSettings())
  646. {
  647. auto newAutoScale = ! isAutoScalePluginWindowsEnabled();
  648. props->setValue ("autoScalePluginWindows", var (newAutoScale));
  649. ApplicationCommandInfo cmdInfo (info.commandID);
  650. updateAutoScaleMenuItem (cmdInfo);
  651. menuItemsChanged();
  652. }
  653. break;
  654. case CommandIDs::aboutBox:
  655. // TODO
  656. break;
  657. case CommandIDs::allWindowsForward:
  658. {
  659. auto& desktop = Desktop::getInstance();
  660. for (int i = 0; i < desktop.getNumComponents(); ++i)
  661. desktop.getComponent (i)->toBehind (this);
  662. break;
  663. }
  664. default:
  665. return false;
  666. }
  667. return true;
  668. }
  669. void MainHostWindow::showAudioSettings()
  670. {
  671. auto* audioSettingsComp = new AudioDeviceSelectorComponent (deviceManager,
  672. 0, 256,
  673. 0, 256,
  674. true, true,
  675. true, false);
  676. audioSettingsComp->setSize (500, 450);
  677. DialogWindow::LaunchOptions o;
  678. o.content.setOwned (audioSettingsComp);
  679. o.dialogTitle = "Audio Settings";
  680. o.componentToCentreAround = this;
  681. o.dialogBackgroundColour = getLookAndFeel().findColour (ResizableWindow::backgroundColourId);
  682. o.escapeKeyTriggersCloseButton = true;
  683. o.useNativeTitleBar = false;
  684. o.resizable = false;
  685. auto* w = o.create();
  686. auto safeThis = SafePointer<MainHostWindow> (this);
  687. w->enterModalState (true,
  688. ModalCallbackFunction::create
  689. ([safeThis] (int)
  690. {
  691. auto audioState = safeThis->deviceManager.createStateXml();
  692. getAppProperties().getUserSettings()->setValue ("audioDeviceState", audioState.get());
  693. getAppProperties().getUserSettings()->saveIfNeeded();
  694. if (safeThis->graphHolder != nullptr)
  695. if (safeThis->graphHolder->graph != nullptr)
  696. safeThis->graphHolder->graph->graph.removeIllegalConnections();
  697. }), true);
  698. }
  699. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  700. {
  701. return true;
  702. }
  703. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  704. {
  705. }
  706. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  707. {
  708. }
  709. void MainHostWindow::fileDragExit (const StringArray&)
  710. {
  711. }
  712. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  713. {
  714. if (graphHolder != nullptr)
  715. {
  716. #if ! (JUCE_ANDROID || JUCE_IOS)
  717. File firstFile { files[0] };
  718. if (files.size() == 1 && firstFile.hasFileExtension (PluginGraph::getFilenameSuffix()))
  719. {
  720. if (auto* g = graphHolder->graph.get())
  721. {
  722. SafePointer<MainHostWindow> parent;
  723. g->saveIfNeededAndUserAgreesAsync ([parent, g, firstFile] (FileBasedDocument::SaveResult r)
  724. {
  725. if (parent == nullptr)
  726. return;
  727. if (r == FileBasedDocument::savedOk)
  728. g->loadFrom (firstFile, true);
  729. });
  730. }
  731. }
  732. else
  733. #endif
  734. {
  735. OwnedArray<PluginDescription> typesFound;
  736. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  737. auto pos = graphHolder->getLocalPoint (this, Point<int> (x, y));
  738. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  739. if (auto* desc = typesFound.getUnchecked(i))
  740. createPlugin (*desc, pos);
  741. }
  742. }
  743. }
  744. bool MainHostWindow::isDoublePrecisionProcessingEnabled()
  745. {
  746. if (auto* props = getAppProperties().getUserSettings())
  747. return props->getBoolValue ("doublePrecisionProcessing", false);
  748. return false;
  749. }
  750. bool MainHostWindow::isAutoScalePluginWindowsEnabled()
  751. {
  752. if (auto* props = getAppProperties().getUserSettings())
  753. return props->getBoolValue ("autoScalePluginWindows", false);
  754. return false;
  755. }
  756. void MainHostWindow::updatePrecisionMenuItem (ApplicationCommandInfo& info)
  757. {
  758. info.setInfo ("Double Floating-Point Precision Rendering", {}, "General", 0);
  759. info.setTicked (isDoublePrecisionProcessingEnabled());
  760. }
  761. void MainHostWindow::updateAutoScaleMenuItem (ApplicationCommandInfo& info)
  762. {
  763. info.setInfo ("Auto-Scale Plug-in Windows", {}, "General", 0);
  764. info.setTicked (isAutoScalePluginWindowsEnabled());
  765. }