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.

584 lines
20KB

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