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.

476 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. }
  201. else
  202. {
  203. createPlugin (getChosenType (menuItemID),
  204. proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  205. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f));
  206. }
  207. }
  208. void MainHostWindow::createPlugin (const PluginDescription* desc, int x, int y)
  209. {
  210. GraphDocumentComponent* const graphEditor = getGraphEditor();
  211. if (graphEditor != nullptr)
  212. graphEditor->createNewPlugin (desc, x, y);
  213. }
  214. void MainHostWindow::addPluginsToMenu (PopupMenu& m) const
  215. {
  216. for (int i = 0; i < internalTypes.size(); ++i)
  217. m.addItem (i + 1, internalTypes.getUnchecked(i)->name);
  218. m.addSeparator();
  219. knownPluginList.addToMenu (m, pluginSortMethod);
  220. }
  221. const PluginDescription* MainHostWindow::getChosenType (const int menuID) const
  222. {
  223. if (menuID >= 1 && menuID < 1 + internalTypes.size())
  224. return internalTypes [menuID - 1];
  225. return knownPluginList.getType (knownPluginList.getIndexChosenByMenu (menuID));
  226. }
  227. //==============================================================================
  228. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  229. {
  230. return findFirstTargetParentComponent();
  231. }
  232. void MainHostWindow::getAllCommands (Array <CommandID>& commands)
  233. {
  234. // this returns the set of all commands that this target can perform..
  235. const CommandID ids[] = { CommandIDs::open,
  236. CommandIDs::save,
  237. CommandIDs::saveAs,
  238. CommandIDs::showPluginListEditor,
  239. CommandIDs::showAudioSettings,
  240. CommandIDs::aboutBox
  241. };
  242. commands.addArray (ids, numElementsInArray (ids));
  243. }
  244. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  245. {
  246. const String category ("General");
  247. switch (commandID)
  248. {
  249. case CommandIDs::open:
  250. result.setInfo ("Open...",
  251. "Opens a filter graph file",
  252. category, 0);
  253. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  254. break;
  255. case CommandIDs::save:
  256. result.setInfo ("Save",
  257. "Saves the current graph to a file",
  258. category, 0);
  259. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  260. break;
  261. case CommandIDs::saveAs:
  262. result.setInfo ("Save As...",
  263. "Saves a copy of the current graph to a file",
  264. category, 0);
  265. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  266. break;
  267. case CommandIDs::showPluginListEditor:
  268. result.setInfo ("Edit the list of available plug-Ins...", String::empty, category, 0);
  269. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  270. break;
  271. case CommandIDs::showAudioSettings:
  272. result.setInfo ("Change the audio device settings", String::empty, category, 0);
  273. result.addDefaultKeypress ('a', ModifierKeys::commandModifier);
  274. break;
  275. case CommandIDs::aboutBox:
  276. result.setInfo ("About...", String::empty, category, 0);
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. bool MainHostWindow::perform (const InvocationInfo& info)
  283. {
  284. GraphDocumentComponent* const graphEditor = getGraphEditor();
  285. switch (info.commandID)
  286. {
  287. case CommandIDs::open:
  288. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  289. graphEditor->graph.loadFromUserSpecifiedFile (true);
  290. break;
  291. case CommandIDs::save:
  292. if (graphEditor != nullptr)
  293. graphEditor->graph.save (true, true);
  294. break;
  295. case CommandIDs::saveAs:
  296. if (graphEditor != nullptr)
  297. graphEditor->graph.saveAs (File::nonexistent, true, true, true);
  298. break;
  299. case CommandIDs::showPluginListEditor:
  300. if (pluginListWindow == nullptr)
  301. pluginListWindow = new PluginListWindow (*this, formatManager);
  302. pluginListWindow->toFront (true);
  303. break;
  304. case CommandIDs::showAudioSettings:
  305. showAudioSettings();
  306. break;
  307. case CommandIDs::aboutBox:
  308. // TODO
  309. break;
  310. default:
  311. return false;
  312. }
  313. return true;
  314. }
  315. void MainHostWindow::showAudioSettings()
  316. {
  317. AudioDeviceSelectorComponent audioSettingsComp (deviceManager,
  318. 0, 256,
  319. 0, 256,
  320. true, true, true, false);
  321. audioSettingsComp.setSize (500, 450);
  322. DialogWindow::showModalDialog ("Audio Settings",
  323. &audioSettingsComp,
  324. this,
  325. Colours::azure,
  326. true);
  327. ScopedPointer<XmlElement> audioState (deviceManager.createStateXml());
  328. appProperties->getUserSettings()->setValue ("audioDeviceState", audioState);
  329. appProperties->getUserSettings()->saveIfNeeded();
  330. GraphDocumentComponent* const graphEditor = getGraphEditor();
  331. if (graphEditor != nullptr)
  332. graphEditor->graph.removeIllegalConnections();
  333. }
  334. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  335. {
  336. return true;
  337. }
  338. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  339. {
  340. }
  341. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  342. {
  343. }
  344. void MainHostWindow::fileDragExit (const StringArray&)
  345. {
  346. }
  347. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  348. {
  349. GraphDocumentComponent* const graphEditor = getGraphEditor();
  350. if (graphEditor != nullptr)
  351. {
  352. if (files.size() == 1 && File (files[0]).hasFileExtension (filenameSuffix))
  353. {
  354. if (graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  355. graphEditor->graph.loadFrom (File (files[0]), true);
  356. }
  357. else
  358. {
  359. OwnedArray <PluginDescription> typesFound;
  360. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  361. Point<int> pos (graphEditor->getLocalPoint (this, Point<int> (x, y)));
  362. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  363. createPlugin (typesFound.getUnchecked(i), pos.getX(), pos.getY());
  364. }
  365. }
  366. }
  367. GraphDocumentComponent* MainHostWindow::getGraphEditor() const
  368. {
  369. return dynamic_cast <GraphDocumentComponent*> (getContentComponent());
  370. }