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.

581 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. createPlugin (getChosenType (menuItemID),
  233. proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  234. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f));
  235. }
  236. }
  237. void MainHostWindow::menuBarActivated (bool isActivated)
  238. {
  239. if (auto* graphEditor = getGraphEditor())
  240. if (isActivated)
  241. graphEditor->unfocusKeyboardComponent();
  242. }
  243. void MainHostWindow::createPlugin (const PluginDescription* desc, int x, int y)
  244. {
  245. if (auto* graphEditor = getGraphEditor())
  246. graphEditor->createNewPlugin (desc, x, y);
  247. }
  248. void MainHostWindow::addPluginsToMenu (PopupMenu& m) const
  249. {
  250. if (auto* graphEditor = getGraphEditor())
  251. {
  252. int i = 0;
  253. for (auto* t : internalTypes)
  254. m.addItem (++i, t->name, graphEditor->graph->getNodeForName (t->name) == nullptr);
  255. }
  256. m.addSeparator();
  257. knownPluginList.addToMenu (m, pluginSortMethod);
  258. }
  259. const PluginDescription* MainHostWindow::getChosenType (const int menuID) const
  260. {
  261. if (menuID >= 1 && menuID < 1 + internalTypes.size())
  262. return internalTypes [menuID - 1];
  263. return knownPluginList.getType (knownPluginList.getIndexChosenByMenu (menuID));
  264. }
  265. //==============================================================================
  266. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  267. {
  268. return findFirstTargetParentComponent();
  269. }
  270. void MainHostWindow::getAllCommands (Array<CommandID>& commands)
  271. {
  272. // this returns the set of all commands that this target can perform..
  273. const CommandID ids[] = { CommandIDs::newFile,
  274. CommandIDs::open,
  275. CommandIDs::save,
  276. CommandIDs::saveAs,
  277. CommandIDs::showPluginListEditor,
  278. CommandIDs::showAudioSettings,
  279. CommandIDs::toggleDoublePrecision,
  280. CommandIDs::aboutBox,
  281. CommandIDs::allWindowsForward
  282. };
  283. commands.addArray (ids, numElementsInArray (ids));
  284. }
  285. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  286. {
  287. const String category ("General");
  288. switch (commandID)
  289. {
  290. case CommandIDs::newFile:
  291. result.setInfo ("New", "Creates a new filter graph file", category, 0);
  292. result.defaultKeypresses.add(KeyPress('n', ModifierKeys::commandModifier, 0));
  293. break;
  294. case CommandIDs::open:
  295. result.setInfo ("Open...", "Opens a filter graph file", category, 0);
  296. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  297. break;
  298. case CommandIDs::save:
  299. result.setInfo ("Save", "Saves the current graph to a file", category, 0);
  300. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  301. break;
  302. case CommandIDs::saveAs:
  303. result.setInfo ("Save As...",
  304. "Saves a copy of the current graph to a file",
  305. category, 0);
  306. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  307. break;
  308. case CommandIDs::showPluginListEditor:
  309. result.setInfo ("Edit the list of available plug-Ins...", String(), category, 0);
  310. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  311. break;
  312. case CommandIDs::showAudioSettings:
  313. result.setInfo ("Change the audio device settings", String(), category, 0);
  314. result.addDefaultKeypress ('a', ModifierKeys::commandModifier);
  315. break;
  316. case CommandIDs::toggleDoublePrecision:
  317. updatePrecisionMenuItem (result);
  318. break;
  319. case CommandIDs::aboutBox:
  320. result.setInfo ("About...", String(), category, 0);
  321. break;
  322. case CommandIDs::allWindowsForward:
  323. result.setInfo ("All Windows Forward", "Bring all plug-in windows forward", category, 0);
  324. result.addDefaultKeypress ('w', ModifierKeys::commandModifier);
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. bool MainHostWindow::perform (const InvocationInfo& info)
  331. {
  332. auto* graphEditor = getGraphEditor();
  333. switch (info.commandID)
  334. {
  335. case CommandIDs::newFile:
  336. if (graphEditor != nullptr && graphEditor->graph != nullptr && graphEditor->graph->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  337. graphEditor->graph->newDocument();
  338. break;
  339. case CommandIDs::open:
  340. if (graphEditor != nullptr && graphEditor->graph != nullptr && graphEditor->graph->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  341. graphEditor->graph->loadFromUserSpecifiedFile (true);
  342. break;
  343. case CommandIDs::save:
  344. if (graphEditor != nullptr && graphEditor->graph != nullptr)
  345. graphEditor->graph->save (true, true);
  346. break;
  347. case CommandIDs::saveAs:
  348. if (graphEditor != nullptr && graphEditor->graph != nullptr)
  349. graphEditor->graph->saveAs (File(), true, true, true);
  350. break;
  351. case CommandIDs::showPluginListEditor:
  352. if (pluginListWindow == nullptr)
  353. pluginListWindow = new PluginListWindow (*this, formatManager);
  354. pluginListWindow->toFront (true);
  355. break;
  356. case CommandIDs::showAudioSettings:
  357. showAudioSettings();
  358. break;
  359. case CommandIDs::toggleDoublePrecision:
  360. if (auto* props = getAppProperties().getUserSettings())
  361. {
  362. bool newIsDoublePrecision = ! isDoublePrecisionProcessing();
  363. props->setValue ("doublePrecisionProcessing", var (newIsDoublePrecision));
  364. {
  365. ApplicationCommandInfo cmdInfo (info.commandID);
  366. updatePrecisionMenuItem (cmdInfo);
  367. menuItemsChanged();
  368. }
  369. if (graphEditor != nullptr)
  370. graphEditor->setDoublePrecision (newIsDoublePrecision);
  371. }
  372. break;
  373. case CommandIDs::aboutBox:
  374. // TODO
  375. break;
  376. case CommandIDs::allWindowsForward:
  377. {
  378. auto& desktop = Desktop::getInstance();
  379. for (int i = 0; i < desktop.getNumComponents(); ++i)
  380. desktop.getComponent (i)->toBehind (this);
  381. break;
  382. }
  383. default:
  384. return false;
  385. }
  386. return true;
  387. }
  388. void MainHostWindow::showAudioSettings()
  389. {
  390. AudioDeviceSelectorComponent audioSettingsComp (deviceManager,
  391. 0, 256,
  392. 0, 256,
  393. true, true, true, false);
  394. audioSettingsComp.setSize (500, 450);
  395. DialogWindow::LaunchOptions o;
  396. o.content.setNonOwned (&audioSettingsComp);
  397. o.dialogTitle = "Audio Settings";
  398. o.componentToCentreAround = this;
  399. o.dialogBackgroundColour = getLookAndFeel().findColour (ResizableWindow::backgroundColourId);
  400. o.escapeKeyTriggersCloseButton = true;
  401. o.useNativeTitleBar = false;
  402. o.resizable = false;
  403. o.runModal();
  404. ScopedPointer<XmlElement> audioState (deviceManager.createStateXml());
  405. getAppProperties().getUserSettings()->setValue ("audioDeviceState", audioState);
  406. getAppProperties().getUserSettings()->saveIfNeeded();
  407. if (auto* graphEditor = getGraphEditor())
  408. if (graphEditor->graph != nullptr)
  409. graphEditor->graph->removeIllegalConnections();
  410. }
  411. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  412. {
  413. return true;
  414. }
  415. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  416. {
  417. }
  418. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  419. {
  420. }
  421. void MainHostWindow::fileDragExit (const StringArray&)
  422. {
  423. }
  424. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  425. {
  426. if (auto* graphEditor = getGraphEditor())
  427. {
  428. if (files.size() == 1 && File (files[0]).hasFileExtension (filenameSuffix))
  429. {
  430. if (auto* filterGraph = graphEditor->graph.get())
  431. if (filterGraph->saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  432. filterGraph->loadFrom (File (files[0]), true);
  433. }
  434. else
  435. {
  436. OwnedArray<PluginDescription> typesFound;
  437. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  438. auto pos = graphEditor->getLocalPoint (this, Point<int> (x, y));
  439. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  440. createPlugin (typesFound.getUnchecked(i), pos.x, pos.y);
  441. }
  442. }
  443. }
  444. GraphDocumentComponent* MainHostWindow::getGraphEditor() const
  445. {
  446. return dynamic_cast<GraphDocumentComponent*> (getContentComponent());
  447. }
  448. bool MainHostWindow::isDoublePrecisionProcessing()
  449. {
  450. if (auto* props = getAppProperties().getUserSettings())
  451. return props->getBoolValue ("doublePrecisionProcessing", false);
  452. return false;
  453. }
  454. void MainHostWindow::updatePrecisionMenuItem (ApplicationCommandInfo& info)
  455. {
  456. info.setInfo ("Double floating point precision rendering", String(), "General", 0);
  457. info.setTicked (isDoublePrecisionProcessing());
  458. }