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.

483 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceLibraryCode/JuceHeader.h"
  19. #include "MainHostWindow.h"
  20. #include "InternalFilters.h"
  21. //==============================================================================
  22. class MainHostWindow::PluginListWindow : public DocumentWindow
  23. {
  24. public:
  25. PluginListWindow (MainHostWindow& owner_, AudioPluginFormatManager& formatManager)
  26. : DocumentWindow ("Available Plugins", Colours::white,
  27. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  28. owner (owner_)
  29. {
  30. const File deadMansPedalFile (appProperties->getUserSettings()
  31. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList"));
  32. setContentOwned (new PluginListComponent (formatManager,
  33. owner.knownPluginList,
  34. deadMansPedalFile,
  35. appProperties->getUserSettings()), true);
  36. setResizable (true, false);
  37. setResizeLimits (300, 400, 800, 1500);
  38. setTopLeftPosition (60, 60);
  39. restoreWindowStateFromString (appProperties->getUserSettings()->getValue ("listWindowPos"));
  40. setVisible (true);
  41. }
  42. ~PluginListWindow()
  43. {
  44. appProperties->getUserSettings()->setValue ("listWindowPos", getWindowStateAsString());
  45. clearContentComponent();
  46. }
  47. void closeButtonPressed()
  48. {
  49. owner.pluginListWindow = nullptr;
  50. }
  51. private:
  52. MainHostWindow& owner;
  53. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginListWindow);
  54. };
  55. //==============================================================================
  56. MainHostWindow::MainHostWindow()
  57. : DocumentWindow (JUCEApplication::getInstance()->getApplicationName(), Colours::lightgrey,
  58. DocumentWindow::allButtons)
  59. {
  60. formatManager.addDefaultFormats();
  61. formatManager.addFormat (new InternalPluginFormat());
  62. ScopedPointer<XmlElement> savedAudioState (appProperties->getUserSettings()
  63. ->getXmlValue ("audioDeviceState"));
  64. deviceManager.initialise (256, 256, savedAudioState, true);
  65. setResizable (true, false);
  66. setResizeLimits (500, 400, 10000, 10000);
  67. centreWithSize (800, 600);
  68. setContentOwned (new GraphDocumentComponent (formatManager, &deviceManager), false);
  69. restoreWindowStateFromString (appProperties->getUserSettings()->getValue ("mainWindowPos"));
  70. setVisible (true);
  71. InternalPluginFormat internalFormat;
  72. internalFormat.getAllTypes (internalTypes);
  73. ScopedPointer<XmlElement> savedPluginList (appProperties->getUserSettings()->getXmlValue ("pluginList"));
  74. if (savedPluginList != nullptr)
  75. knownPluginList.recreateFromXml (*savedPluginList);
  76. pluginSortMethod = (KnownPluginList::SortMethod) appProperties->getUserSettings()
  77. ->getIntValue ("pluginSortMethod", KnownPluginList::sortByManufacturer);
  78. knownPluginList.addChangeListener (this);
  79. addKeyListener (commandManager->getKeyMappings());
  80. Process::setPriority (Process::HighPriority);
  81. #if JUCE_MAC
  82. setMacMainMenu (this);
  83. #else
  84. setMenuBar (this);
  85. #endif
  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. appProperties->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. appProperties->getUserSettings()->setValue ("pluginList", savedPluginList);
  123. appProperties->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 (commandManager, CommandIDs::open);
  138. RecentlyOpenedFilesList recentFiles;
  139. recentFiles.restoreFromString (appProperties->getUserSettings()
  140. ->getValue ("recentFilterGraphFiles"));
  141. PopupMenu recentFilesMenu;
  142. recentFiles.createPopupMenuItems (recentFilesMenu, 100, true, true);
  143. menu.addSubMenu ("Open recent file", recentFilesMenu);
  144. menu.addCommandItem (commandManager, CommandIDs::save);
  145. menu.addCommandItem (commandManager, CommandIDs::saveAs);
  146. menu.addSeparator();
  147. menu.addCommandItem (commandManager, 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 (commandManager, 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 (commandManager, CommandIDs::showAudioSettings);
  171. menu.addSeparator();
  172. menu.addCommandItem (commandManager, 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 (appProperties->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. appProperties->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. appProperties->getUserSettings()->setValue ("audioDeviceState", audioState);
  334. appProperties->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. }