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.

484 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. addKeyListener (getCommandManager().getKeyMappings());
  79. Process::setPriority (Process::HighPriority);
  80. #if JUCE_MAC
  81. setMacMainMenu (this);
  82. #else
  83. setMenuBar (this);
  84. #endif
  85. getCommandManager().setFirstCommandTarget (this);
  86. }
  87. MainHostWindow::~MainHostWindow()
  88. {
  89. pluginListWindow = nullptr;
  90. #if JUCE_MAC
  91. setMacMainMenu (nullptr);
  92. #else
  93. setMenuBar (nullptr);
  94. #endif
  95. knownPluginList.removeChangeListener (this);
  96. getAppProperties().getUserSettings()->setValue ("mainWindowPos", getWindowStateAsString());
  97. clearContentComponent();
  98. }
  99. void MainHostWindow::closeButtonPressed()
  100. {
  101. tryToQuitApplication();
  102. }
  103. bool MainHostWindow::tryToQuitApplication()
  104. {
  105. PluginWindow::closeAllCurrentlyOpenWindows();
  106. if (getGraphEditor() == nullptr
  107. || getGraphEditor()->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  108. {
  109. JUCEApplication::quit();
  110. return true;
  111. }
  112. return false;
  113. }
  114. void MainHostWindow::changeListenerCallback (ChangeBroadcaster*)
  115. {
  116. menuItemsChanged();
  117. // save the plugin list every time it gets chnaged, so that if we're scanning
  118. // and it crashes, we've still saved the previous ones
  119. ScopedPointer<XmlElement> savedPluginList (knownPluginList.createXml());
  120. if (savedPluginList != nullptr)
  121. {
  122. getAppProperties().getUserSettings()->setValue ("pluginList", savedPluginList);
  123. getAppProperties().saveIfNeeded();
  124. }
  125. }
  126. StringArray MainHostWindow::getMenuBarNames()
  127. {
  128. const char* const names[] = { "File", "Plugins", "Options", nullptr };
  129. return StringArray (names);
  130. }
  131. PopupMenu MainHostWindow::getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  132. {
  133. PopupMenu menu;
  134. if (topLevelMenuIndex == 0)
  135. {
  136. // "File" menu
  137. menu.addCommandItem (&getCommandManager(), CommandIDs::open);
  138. RecentlyOpenedFilesList recentFiles;
  139. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  140. ->getValue ("recentFilterGraphFiles"));
  141. PopupMenu recentFilesMenu;
  142. recentFiles.createPopupMenuItems (recentFilesMenu, 100, true, true);
  143. menu.addSubMenu ("Open recent file", recentFilesMenu);
  144. menu.addCommandItem (&getCommandManager(), CommandIDs::save);
  145. menu.addCommandItem (&getCommandManager(), CommandIDs::saveAs);
  146. menu.addSeparator();
  147. menu.addCommandItem (&getCommandManager(), StandardApplicationCommandIDs::quit);
  148. }
  149. else if (topLevelMenuIndex == 1)
  150. {
  151. // "Plugins" menu
  152. PopupMenu pluginsMenu;
  153. addPluginsToMenu (pluginsMenu);
  154. menu.addSubMenu ("Create plugin", pluginsMenu);
  155. menu.addSeparator();
  156. menu.addItem (250, "Delete all plugins");
  157. }
  158. else if (topLevelMenuIndex == 2)
  159. {
  160. // "Options" menu
  161. menu.addCommandItem (&getCommandManager(), CommandIDs::showPluginListEditor);
  162. PopupMenu sortTypeMenu;
  163. sortTypeMenu.addItem (200, "List plugins in default order", true, pluginSortMethod == KnownPluginList::defaultOrder);
  164. sortTypeMenu.addItem (201, "List plugins in alphabetical order", true, pluginSortMethod == KnownPluginList::sortAlphabetically);
  165. sortTypeMenu.addItem (202, "List plugins by category", true, pluginSortMethod == KnownPluginList::sortByCategory);
  166. sortTypeMenu.addItem (203, "List plugins by manufacturer", true, pluginSortMethod == KnownPluginList::sortByManufacturer);
  167. sortTypeMenu.addItem (204, "List plugins based on the directory structure", true, pluginSortMethod == KnownPluginList::sortByFileSystemLocation);
  168. menu.addSubMenu ("Plugin menu type", sortTypeMenu);
  169. menu.addSeparator();
  170. menu.addCommandItem (&getCommandManager(), CommandIDs::showAudioSettings);
  171. menu.addSeparator();
  172. menu.addCommandItem (&getCommandManager(), CommandIDs::aboutBox);
  173. }
  174. return menu;
  175. }
  176. void MainHostWindow::menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  177. {
  178. GraphDocumentComponent* const graphEditor = getGraphEditor();
  179. if (menuItemID == 250)
  180. {
  181. if (graphEditor != nullptr)
  182. graphEditor->graph.clear();
  183. }
  184. else if (menuItemID >= 100 && menuItemID < 200)
  185. {
  186. RecentlyOpenedFilesList recentFiles;
  187. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  188. ->getValue ("recentFilterGraphFiles"));
  189. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  190. graphEditor->graph.loadFrom (recentFiles.getFile (menuItemID - 100), true);
  191. }
  192. else if (menuItemID >= 200 && menuItemID < 210)
  193. {
  194. if (menuItemID == 200) pluginSortMethod = KnownPluginList::defaultOrder;
  195. else if (menuItemID == 201) pluginSortMethod = KnownPluginList::sortAlphabetically;
  196. else if (menuItemID == 202) pluginSortMethod = KnownPluginList::sortByCategory;
  197. else if (menuItemID == 203) pluginSortMethod = KnownPluginList::sortByManufacturer;
  198. else if (menuItemID == 204) pluginSortMethod = KnownPluginList::sortByFileSystemLocation;
  199. getAppProperties().getUserSettings()->setValue ("pluginSortMethod", (int) pluginSortMethod);
  200. menuItemsChanged();
  201. }
  202. else
  203. {
  204. createPlugin (getChosenType (menuItemID),
  205. proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  206. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f));
  207. }
  208. }
  209. void MainHostWindow::createPlugin (const PluginDescription* desc, int x, int y)
  210. {
  211. GraphDocumentComponent* const graphEditor = getGraphEditor();
  212. if (graphEditor != nullptr)
  213. graphEditor->createNewPlugin (desc, x, y);
  214. }
  215. void MainHostWindow::addPluginsToMenu (PopupMenu& m) const
  216. {
  217. for (int i = 0; i < internalTypes.size(); ++i)
  218. m.addItem (i + 1, internalTypes.getUnchecked(i)->name);
  219. m.addSeparator();
  220. knownPluginList.addToMenu (m, pluginSortMethod);
  221. }
  222. const PluginDescription* MainHostWindow::getChosenType (const int menuID) const
  223. {
  224. if (menuID >= 1 && menuID < 1 + internalTypes.size())
  225. return internalTypes [menuID - 1];
  226. return knownPluginList.getType (knownPluginList.getIndexChosenByMenu (menuID));
  227. }
  228. //==============================================================================
  229. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  230. {
  231. return findFirstTargetParentComponent();
  232. }
  233. void MainHostWindow::getAllCommands (Array <CommandID>& commands)
  234. {
  235. // this returns the set of all commands that this target can perform..
  236. const CommandID ids[] = { CommandIDs::open,
  237. CommandIDs::save,
  238. CommandIDs::saveAs,
  239. CommandIDs::showPluginListEditor,
  240. CommandIDs::showAudioSettings,
  241. CommandIDs::aboutBox
  242. };
  243. commands.addArray (ids, numElementsInArray (ids));
  244. }
  245. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  246. {
  247. const String category ("General");
  248. switch (commandID)
  249. {
  250. case CommandIDs::open:
  251. result.setInfo ("Open...",
  252. "Opens a filter graph file",
  253. category, 0);
  254. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  255. break;
  256. case CommandIDs::save:
  257. result.setInfo ("Save",
  258. "Saves the current graph to a file",
  259. category, 0);
  260. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  261. break;
  262. case CommandIDs::saveAs:
  263. result.setInfo ("Save As...",
  264. "Saves a copy of the current graph to a file",
  265. category, 0);
  266. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  267. break;
  268. case CommandIDs::showPluginListEditor:
  269. result.setInfo ("Edit the list of available plug-Ins...", String::empty, category, 0);
  270. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  271. break;
  272. case CommandIDs::showAudioSettings:
  273. result.setInfo ("Change the audio device settings", String::empty, category, 0);
  274. result.addDefaultKeypress ('a', ModifierKeys::commandModifier);
  275. break;
  276. case CommandIDs::aboutBox:
  277. result.setInfo ("About...", String::empty, category, 0);
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. bool MainHostWindow::perform (const InvocationInfo& info)
  284. {
  285. GraphDocumentComponent* const graphEditor = getGraphEditor();
  286. switch (info.commandID)
  287. {
  288. case CommandIDs::open:
  289. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  290. graphEditor->graph.loadFromUserSpecifiedFile (true);
  291. break;
  292. case CommandIDs::save:
  293. if (graphEditor != nullptr)
  294. graphEditor->graph.save (true, true);
  295. break;
  296. case CommandIDs::saveAs:
  297. if (graphEditor != nullptr)
  298. graphEditor->graph.saveAs (File::nonexistent, true, true, true);
  299. break;
  300. case CommandIDs::showPluginListEditor:
  301. if (pluginListWindow == nullptr)
  302. pluginListWindow = new PluginListWindow (*this, formatManager);
  303. pluginListWindow->toFront (true);
  304. break;
  305. case CommandIDs::showAudioSettings:
  306. showAudioSettings();
  307. break;
  308. case CommandIDs::aboutBox:
  309. // TODO
  310. break;
  311. default:
  312. return false;
  313. }
  314. return true;
  315. }
  316. void MainHostWindow::showAudioSettings()
  317. {
  318. AudioDeviceSelectorComponent audioSettingsComp (deviceManager,
  319. 0, 256,
  320. 0, 256,
  321. true, true, true, false);
  322. audioSettingsComp.setSize (500, 450);
  323. DialogWindow::LaunchOptions o;
  324. o.content.setNonOwned (&audioSettingsComp);
  325. o.dialogTitle = "Audio Settings";
  326. o.componentToCentreAround = this;
  327. o.dialogBackgroundColour = Colours::azure;
  328. o.escapeKeyTriggersCloseButton = true;
  329. o.useNativeTitleBar = false;
  330. o.resizable = false;
  331. o.runModal();
  332. ScopedPointer<XmlElement> audioState (deviceManager.createStateXml());
  333. getAppProperties().getUserSettings()->setValue ("audioDeviceState", audioState);
  334. getAppProperties().getUserSettings()->saveIfNeeded();
  335. GraphDocumentComponent* const graphEditor = getGraphEditor();
  336. if (graphEditor != nullptr)
  337. graphEditor->graph.removeIllegalConnections();
  338. }
  339. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  340. {
  341. return true;
  342. }
  343. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  344. {
  345. }
  346. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  347. {
  348. }
  349. void MainHostWindow::fileDragExit (const StringArray&)
  350. {
  351. }
  352. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  353. {
  354. GraphDocumentComponent* const graphEditor = getGraphEditor();
  355. if (graphEditor != nullptr)
  356. {
  357. if (files.size() == 1 && File (files[0]).hasFileExtension (filenameSuffix))
  358. {
  359. if (graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  360. graphEditor->graph.loadFrom (File (files[0]), true);
  361. }
  362. else
  363. {
  364. OwnedArray <PluginDescription> typesFound;
  365. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  366. Point<int> pos (graphEditor->getLocalPoint (this, Point<int> (x, y)));
  367. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  368. createPlugin (typesFound.getUnchecked(i), pos.getX(), pos.getY());
  369. }
  370. }
  371. }
  372. GraphDocumentComponent* MainHostWindow::getGraphEditor() const
  373. {
  374. return dynamic_cast <GraphDocumentComponent*> (getContentComponent());
  375. }