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.

599 lines
21KB

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