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.

527 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceLibraryCode/JuceHeader.h"
  18. #include "MainHostWindow.h"
  19. #include "InternalFilters.h"
  20. //==============================================================================
  21. class MainHostWindow::PluginListWindow : public DocumentWindow
  22. {
  23. public:
  24. PluginListWindow (MainHostWindow& owner_, AudioPluginFormatManager& formatManager)
  25. : DocumentWindow ("Available Plugins", Colours::white,
  26. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  27. owner (owner_)
  28. {
  29. const File deadMansPedalFile (getAppProperties().getUserSettings()
  30. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList"));
  31. setContentOwned (new PluginListComponent (formatManager,
  32. owner.knownPluginList,
  33. deadMansPedalFile,
  34. getAppProperties().getUserSettings()), true);
  35. setResizable (true, false);
  36. setResizeLimits (300, 400, 800, 1500);
  37. setTopLeftPosition (60, 60);
  38. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("listWindowPos"));
  39. setVisible (true);
  40. }
  41. ~PluginListWindow()
  42. {
  43. getAppProperties().getUserSettings()->setValue ("listWindowPos", getWindowStateAsString());
  44. clearContentComponent();
  45. }
  46. void closeButtonPressed()
  47. {
  48. owner.pluginListWindow = nullptr;
  49. }
  50. private:
  51. MainHostWindow& owner;
  52. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginListWindow)
  53. };
  54. //==============================================================================
  55. MainHostWindow::MainHostWindow()
  56. : DocumentWindow (JUCEApplication::getInstance()->getApplicationName(), Colours::lightgrey,
  57. DocumentWindow::allButtons)
  58. {
  59. formatManager.addDefaultFormats();
  60. formatManager.addFormat (new InternalPluginFormat());
  61. ScopedPointer<XmlElement> savedAudioState (getAppProperties().getUserSettings()
  62. ->getXmlValue ("audioDeviceState"));
  63. deviceManager.initialise (256, 256, savedAudioState, true);
  64. setResizable (true, false);
  65. setResizeLimits (500, 400, 10000, 10000);
  66. centreWithSize (800, 600);
  67. setContentOwned (new GraphDocumentComponent (formatManager, &deviceManager), false);
  68. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("mainWindowPos"));
  69. setVisible (true);
  70. InternalPluginFormat internalFormat;
  71. internalFormat.getAllTypes (internalTypes);
  72. ScopedPointer<XmlElement> savedPluginList (getAppProperties().getUserSettings()->getXmlValue ("pluginList"));
  73. if (savedPluginList != nullptr)
  74. knownPluginList.recreateFromXml (*savedPluginList);
  75. pluginSortMethod = (KnownPluginList::SortMethod) getAppProperties().getUserSettings()
  76. ->getIntValue ("pluginSortMethod", KnownPluginList::sortByManufacturer);
  77. knownPluginList.addChangeListener (this);
  78. getGraphEditor()->graph.addChangeListener (this);
  79. addKeyListener (getCommandManager().getKeyMappings());
  80. Process::setPriority (Process::HighPriority);
  81. #if JUCE_MAC
  82. setMacMainMenu (this);
  83. #else
  84. setMenuBar (this);
  85. #endif
  86. getCommandManager().setFirstCommandTarget (this);
  87. }
  88. MainHostWindow::~MainHostWindow()
  89. {
  90. pluginListWindow = nullptr;
  91. #if JUCE_MAC
  92. setMacMainMenu (nullptr);
  93. #else
  94. setMenuBar (nullptr);
  95. #endif
  96. knownPluginList.removeChangeListener (this);
  97. getGraphEditor()->graph.removeChangeListener (this);
  98. getAppProperties().getUserSettings()->setValue ("mainWindowPos", getWindowStateAsString());
  99. clearContentComponent();
  100. }
  101. void MainHostWindow::closeButtonPressed()
  102. {
  103. tryToQuitApplication();
  104. }
  105. bool MainHostWindow::tryToQuitApplication()
  106. {
  107. PluginWindow::closeAllCurrentlyOpenWindows();
  108. if (getGraphEditor() == nullptr
  109. || getGraphEditor()->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  110. {
  111. JUCEApplication::quit();
  112. return true;
  113. }
  114. return false;
  115. }
  116. void MainHostWindow::changeListenerCallback (ChangeBroadcaster* changed)
  117. {
  118. if (changed == &knownPluginList)
  119. {
  120. menuItemsChanged();
  121. // save the plugin list every time it gets chnaged, so that if we're scanning
  122. // and it crashes, we've still saved the previous ones
  123. ScopedPointer<XmlElement> savedPluginList (knownPluginList.createXml());
  124. if (savedPluginList != nullptr)
  125. {
  126. getAppProperties().getUserSettings()->setValue ("pluginList", savedPluginList);
  127. getAppProperties().saveIfNeeded();
  128. }
  129. }
  130. else if (changed == &getGraphEditor()->graph)
  131. {
  132. String title = JUCEApplication::getInstance()->getApplicationName();
  133. File f = getGraphEditor()->graph.getFile();
  134. if (f.existsAsFile())
  135. title = f.getFileName() + " - " + title;
  136. setName (title);
  137. }
  138. }
  139. StringArray MainHostWindow::getMenuBarNames()
  140. {
  141. const char* const names[] = { "File", "Plugins", "Options", "Windows", nullptr };
  142. return StringArray (names);
  143. }
  144. PopupMenu MainHostWindow::getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  145. {
  146. PopupMenu menu;
  147. if (topLevelMenuIndex == 0)
  148. {
  149. // "File" menu
  150. menu.addCommandItem (&getCommandManager(), CommandIDs::newFile);
  151. menu.addCommandItem (&getCommandManager(), CommandIDs::open);
  152. RecentlyOpenedFilesList recentFiles;
  153. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  154. ->getValue ("recentFilterGraphFiles"));
  155. PopupMenu recentFilesMenu;
  156. recentFiles.createPopupMenuItems (recentFilesMenu, 100, true, true);
  157. menu.addSubMenu ("Open recent file", recentFilesMenu);
  158. menu.addCommandItem (&getCommandManager(), CommandIDs::save);
  159. menu.addCommandItem (&getCommandManager(), CommandIDs::saveAs);
  160. menu.addSeparator();
  161. menu.addCommandItem (&getCommandManager(), StandardApplicationCommandIDs::quit);
  162. }
  163. else if (topLevelMenuIndex == 1)
  164. {
  165. // "Plugins" menu
  166. PopupMenu pluginsMenu;
  167. addPluginsToMenu (pluginsMenu);
  168. menu.addSubMenu ("Create plugin", pluginsMenu);
  169. menu.addSeparator();
  170. menu.addItem (250, "Delete all plugins");
  171. }
  172. else if (topLevelMenuIndex == 2)
  173. {
  174. // "Options" menu
  175. menu.addCommandItem (&getCommandManager(), CommandIDs::showPluginListEditor);
  176. PopupMenu sortTypeMenu;
  177. sortTypeMenu.addItem (200, "List plugins in default order", true, pluginSortMethod == KnownPluginList::defaultOrder);
  178. sortTypeMenu.addItem (201, "List plugins in alphabetical order", true, pluginSortMethod == KnownPluginList::sortAlphabetically);
  179. sortTypeMenu.addItem (202, "List plugins by category", true, pluginSortMethod == KnownPluginList::sortByCategory);
  180. sortTypeMenu.addItem (203, "List plugins by manufacturer", true, pluginSortMethod == KnownPluginList::sortByManufacturer);
  181. sortTypeMenu.addItem (204, "List plugins based on the directory structure", true, pluginSortMethod == KnownPluginList::sortByFileSystemLocation);
  182. menu.addSubMenu ("Plugin menu type", sortTypeMenu);
  183. menu.addSeparator();
  184. menu.addCommandItem (&getCommandManager(), CommandIDs::showAudioSettings);
  185. menu.addSeparator();
  186. menu.addCommandItem (&getCommandManager(), CommandIDs::aboutBox);
  187. }
  188. else if (topLevelMenuIndex == 3)
  189. {
  190. menu.addCommandItem (&getCommandManager(), CommandIDs::allWindowsForward);
  191. }
  192. return menu;
  193. }
  194. void MainHostWindow::menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  195. {
  196. GraphDocumentComponent* const graphEditor = getGraphEditor();
  197. if (menuItemID == 250)
  198. {
  199. if (graphEditor != nullptr)
  200. graphEditor->graph.clear();
  201. }
  202. else if (menuItemID >= 100 && menuItemID < 200)
  203. {
  204. RecentlyOpenedFilesList recentFiles;
  205. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  206. ->getValue ("recentFilterGraphFiles"));
  207. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  208. graphEditor->graph.loadFrom (recentFiles.getFile (menuItemID - 100), true);
  209. }
  210. else if (menuItemID >= 200 && menuItemID < 210)
  211. {
  212. if (menuItemID == 200) pluginSortMethod = KnownPluginList::defaultOrder;
  213. else if (menuItemID == 201) pluginSortMethod = KnownPluginList::sortAlphabetically;
  214. else if (menuItemID == 202) pluginSortMethod = KnownPluginList::sortByCategory;
  215. else if (menuItemID == 203) pluginSortMethod = KnownPluginList::sortByManufacturer;
  216. else if (menuItemID == 204) pluginSortMethod = KnownPluginList::sortByFileSystemLocation;
  217. getAppProperties().getUserSettings()->setValue ("pluginSortMethod", (int) pluginSortMethod);
  218. menuItemsChanged();
  219. }
  220. else
  221. {
  222. createPlugin (getChosenType (menuItemID),
  223. proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  224. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f));
  225. }
  226. }
  227. void MainHostWindow::createPlugin (const PluginDescription* desc, int x, int y)
  228. {
  229. GraphDocumentComponent* const graphEditor = getGraphEditor();
  230. if (graphEditor != nullptr)
  231. graphEditor->createNewPlugin (desc, x, y);
  232. }
  233. void MainHostWindow::addPluginsToMenu (PopupMenu& m) const
  234. {
  235. for (int i = 0; i < internalTypes.size(); ++i)
  236. m.addItem (i + 1, internalTypes.getUnchecked(i)->name);
  237. m.addSeparator();
  238. knownPluginList.addToMenu (m, pluginSortMethod);
  239. }
  240. const PluginDescription* MainHostWindow::getChosenType (const int menuID) const
  241. {
  242. if (menuID >= 1 && menuID < 1 + internalTypes.size())
  243. return internalTypes [menuID - 1];
  244. return knownPluginList.getType (knownPluginList.getIndexChosenByMenu (menuID));
  245. }
  246. //==============================================================================
  247. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  248. {
  249. return findFirstTargetParentComponent();
  250. }
  251. void MainHostWindow::getAllCommands (Array <CommandID>& commands)
  252. {
  253. // this returns the set of all commands that this target can perform..
  254. const CommandID ids[] = { CommandIDs::newFile,
  255. CommandIDs::open,
  256. CommandIDs::save,
  257. CommandIDs::saveAs,
  258. CommandIDs::showPluginListEditor,
  259. CommandIDs::showAudioSettings,
  260. CommandIDs::aboutBox,
  261. CommandIDs::allWindowsForward
  262. };
  263. commands.addArray (ids, numElementsInArray (ids));
  264. }
  265. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  266. {
  267. const String category ("General");
  268. switch (commandID)
  269. {
  270. case CommandIDs::newFile:
  271. result.setInfo ("New", "Creates a new filter graph file", category, 0);
  272. result.defaultKeypresses.add(KeyPress('n', ModifierKeys::commandModifier, 0));
  273. break;
  274. case CommandIDs::open:
  275. result.setInfo ("Open...", "Opens a filter graph file", category, 0);
  276. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  277. break;
  278. case CommandIDs::save:
  279. result.setInfo ("Save", "Saves the current graph to a file", category, 0);
  280. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  281. break;
  282. case CommandIDs::saveAs:
  283. result.setInfo ("Save As...",
  284. "Saves a copy of the current graph to a file",
  285. category, 0);
  286. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  287. break;
  288. case CommandIDs::showPluginListEditor:
  289. result.setInfo ("Edit the list of available plug-Ins...", String::empty, category, 0);
  290. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  291. break;
  292. case CommandIDs::showAudioSettings:
  293. result.setInfo ("Change the audio device settings", String::empty, category, 0);
  294. result.addDefaultKeypress ('a', ModifierKeys::commandModifier);
  295. break;
  296. case CommandIDs::aboutBox:
  297. result.setInfo ("About...", String::empty, category, 0);
  298. break;
  299. case CommandIDs::allWindowsForward:
  300. result.setInfo ("All Windows Forward", "Bring all plug-in windows forward", category, 0);
  301. result.addDefaultKeypress ('w', ModifierKeys::commandModifier);
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. bool MainHostWindow::perform (const InvocationInfo& info)
  308. {
  309. GraphDocumentComponent* const graphEditor = getGraphEditor();
  310. switch (info.commandID)
  311. {
  312. case CommandIDs::newFile:
  313. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  314. graphEditor->graph.newDocument();
  315. break;
  316. case CommandIDs::open:
  317. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  318. graphEditor->graph.loadFromUserSpecifiedFile (true);
  319. break;
  320. case CommandIDs::save:
  321. if (graphEditor != nullptr)
  322. graphEditor->graph.save (true, true);
  323. break;
  324. case CommandIDs::saveAs:
  325. if (graphEditor != nullptr)
  326. graphEditor->graph.saveAs (File::nonexistent, true, true, true);
  327. break;
  328. case CommandIDs::showPluginListEditor:
  329. if (pluginListWindow == nullptr)
  330. pluginListWindow = new PluginListWindow (*this, formatManager);
  331. pluginListWindow->toFront (true);
  332. break;
  333. case CommandIDs::showAudioSettings:
  334. showAudioSettings();
  335. break;
  336. case CommandIDs::aboutBox:
  337. // TODO
  338. break;
  339. case CommandIDs::allWindowsForward:
  340. {
  341. Desktop& desktop = Desktop::getInstance();
  342. for (int i = 0; i < desktop.getNumComponents(); ++i)
  343. desktop.getComponent (i)->toBehind (this);
  344. break;
  345. }
  346. default:
  347. return false;
  348. }
  349. return true;
  350. }
  351. void MainHostWindow::showAudioSettings()
  352. {
  353. AudioDeviceSelectorComponent audioSettingsComp (deviceManager,
  354. 0, 256,
  355. 0, 256,
  356. true, true, true, false);
  357. audioSettingsComp.setSize (500, 450);
  358. DialogWindow::LaunchOptions o;
  359. o.content.setNonOwned (&audioSettingsComp);
  360. o.dialogTitle = "Audio Settings";
  361. o.componentToCentreAround = this;
  362. o.dialogBackgroundColour = Colours::azure;
  363. o.escapeKeyTriggersCloseButton = true;
  364. o.useNativeTitleBar = false;
  365. o.resizable = false;
  366. o.runModal();
  367. ScopedPointer<XmlElement> audioState (deviceManager.createStateXml());
  368. getAppProperties().getUserSettings()->setValue ("audioDeviceState", audioState);
  369. getAppProperties().getUserSettings()->saveIfNeeded();
  370. GraphDocumentComponent* const graphEditor = getGraphEditor();
  371. if (graphEditor != nullptr)
  372. graphEditor->graph.removeIllegalConnections();
  373. }
  374. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  375. {
  376. return true;
  377. }
  378. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  379. {
  380. }
  381. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  382. {
  383. }
  384. void MainHostWindow::fileDragExit (const StringArray&)
  385. {
  386. }
  387. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  388. {
  389. GraphDocumentComponent* const graphEditor = getGraphEditor();
  390. if (graphEditor != nullptr)
  391. {
  392. if (files.size() == 1 && File (files[0]).hasFileExtension (filenameSuffix))
  393. {
  394. if (graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  395. graphEditor->graph.loadFrom (File (files[0]), true);
  396. }
  397. else
  398. {
  399. OwnedArray <PluginDescription> typesFound;
  400. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  401. Point<int> pos (graphEditor->getLocalPoint (this, Point<int> (x, y)));
  402. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  403. createPlugin (typesFound.getUnchecked(i), pos.getX(), pos.getY());
  404. }
  405. }
  406. }
  407. GraphDocumentComponent* MainHostWindow::getGraphEditor() const
  408. {
  409. return dynamic_cast <GraphDocumentComponent*> (getContentComponent());
  410. }