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.

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