Audio plugin host https://kx.studio/carla
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.

409 lines
14KB

  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 "MainHostWindow.h"
  18. #include "InternalFilters.h"
  19. //==============================================================================
  20. class MainHostWindow::PluginListWindow : public DocumentWindow
  21. {
  22. public:
  23. PluginListWindow (MainHostWindow& owner_, AudioPluginFormatManager& formatManager)
  24. : DocumentWindow ("Available Plugins", Colours::white,
  25. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  26. owner (owner_)
  27. {
  28. const File deadMansPedalFile (owner.appProperties.getUserSettings()
  29. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList"));
  30. setContentOwned (new PluginListComponent (formatManager,
  31. owner.knownPluginList,
  32. deadMansPedalFile,
  33. owner.appProperties.getUserSettings()), true);
  34. setOpaque (true);
  35. setResizable (true, false);
  36. setResizeLimits (300, 400, 800, 1500);
  37. setTopLeftPosition (60, 60);
  38. restoreWindowStateFromString (owner.appProperties.getUserSettings()->getValue ("listWindowPos"));
  39. setUsingNativeTitleBar (true);
  40. setVisible (true);
  41. }
  42. ~PluginListWindow()
  43. {
  44. owner.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 (AudioPluginFormatManager& fm, FilterGraph& graph, ApplicationProperties& ap)
  57. : DocumentWindow ("Juce Patchbay", Colours::lightgrey, DocumentWindow::allButtons),
  58. formatManager (fm),
  59. appProperties (ap),
  60. closed (false)
  61. {
  62. LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  63. setOpaque (true);
  64. setResizable (true, false);
  65. setResizeLimits (500, 400, 10000, 10000);
  66. centreWithSize (800, 600);
  67. setContentOwned (new GraphDocumentComponent (graph), false);
  68. setUsingNativeTitleBar (true);
  69. restoreWindowStateFromString (appProperties.getUserSettings()->getValue ("mainWindowPos"));
  70. setVisible (true);
  71. ScopedPointer<XmlElement> savedPluginList (appProperties.getUserSettings()->getXmlValue ("pluginList"));
  72. if (savedPluginList != nullptr)
  73. knownPluginList.recreateFromXml (*savedPluginList);
  74. pluginSortMethod = (KnownPluginList::SortMethod) appProperties.getUserSettings()
  75. ->getIntValue ("pluginSortMethod", KnownPluginList::sortByManufacturer);
  76. knownPluginList.addChangeListener (this);
  77. addKeyListener (commandManager.getKeyMappings());
  78. //Process::setPriority (Process::HighPriority);
  79. setMenuBar (this);
  80. commandManager.setFirstCommandTarget (this);
  81. commandManager.registerAllCommandsForTarget (this);
  82. menuItemsChanged();
  83. }
  84. MainHostWindow::~MainHostWindow()
  85. {
  86. pluginListWindow = nullptr;
  87. setMenuBar (nullptr);
  88. knownPluginList.removeChangeListener (this);
  89. appProperties.getUserSettings()->setValue ("mainWindowPos", getWindowStateAsString());
  90. clearContentComponent();
  91. LookAndFeel::setDefaultLookAndFeel (nullptr);
  92. }
  93. void MainHostWindow::closeButtonPressed()
  94. {
  95. getGraphEditor()->closeAllCurrentlyOpenWindows();
  96. closed = true;
  97. }
  98. void MainHostWindow::changeListenerCallback (ChangeBroadcaster*)
  99. {
  100. menuItemsChanged();
  101. // save the plugin list every time it gets chnaged, so that if we're scanning
  102. // and it crashes, we've still saved the previous ones
  103. ScopedPointer<XmlElement> savedPluginList (knownPluginList.createXml());
  104. if (savedPluginList != nullptr)
  105. {
  106. appProperties.getUserSettings()->setValue ("pluginList", savedPluginList);
  107. appProperties.saveIfNeeded();
  108. }
  109. }
  110. StringArray MainHostWindow::getMenuBarNames()
  111. {
  112. const char* const names[] = { "File", "Plugins", "Options", nullptr };
  113. return StringArray (names);
  114. }
  115. PopupMenu MainHostWindow::getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  116. {
  117. PopupMenu menu;
  118. if (topLevelMenuIndex == 0)
  119. {
  120. // "File" menu
  121. menu.addCommandItem (&commandManager, CommandIDs::open);
  122. RecentlyOpenedFilesList recentFiles;
  123. recentFiles.restoreFromString (appProperties.getUserSettings()
  124. ->getValue ("recentFilterGraphFiles"));
  125. PopupMenu recentFilesMenu;
  126. recentFiles.createPopupMenuItems (recentFilesMenu, 100, true, true);
  127. menu.addSubMenu ("Open recent file", recentFilesMenu);
  128. menu.addCommandItem (&commandManager, CommandIDs::save);
  129. menu.addCommandItem (&commandManager, CommandIDs::saveAs);
  130. }
  131. else if (topLevelMenuIndex == 1)
  132. {
  133. // "Plugins" menu
  134. PopupMenu pluginsMenu;
  135. addPluginsToMenu (pluginsMenu);
  136. menu.addSubMenu ("Create plugin", pluginsMenu);
  137. menu.addSeparator();
  138. menu.addItem (250, "Delete all plugins");
  139. }
  140. else if (topLevelMenuIndex == 2)
  141. {
  142. // "Options" menu
  143. menu.addCommandItem (&commandManager, CommandIDs::showPluginListEditor);
  144. PopupMenu sortTypeMenu;
  145. sortTypeMenu.addItem (200, "List plugins in default order", true, pluginSortMethod == KnownPluginList::defaultOrder);
  146. sortTypeMenu.addItem (201, "List plugins in alphabetical order", true, pluginSortMethod == KnownPluginList::sortAlphabetically);
  147. sortTypeMenu.addItem (202, "List plugins by category", true, pluginSortMethod == KnownPluginList::sortByCategory);
  148. sortTypeMenu.addItem (203, "List plugins by manufacturer", true, pluginSortMethod == KnownPluginList::sortByManufacturer);
  149. sortTypeMenu.addItem (204, "List plugins based on the directory structure", true, pluginSortMethod == KnownPluginList::sortByFileSystemLocation);
  150. menu.addSubMenu ("Plugin menu type", sortTypeMenu);
  151. }
  152. return menu;
  153. }
  154. void MainHostWindow::menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  155. {
  156. GraphDocumentComponent* const graphEditor = getGraphEditor();
  157. if (menuItemID == 250)
  158. {
  159. if (graphEditor != nullptr)
  160. graphEditor->graph.clearKeepingInternals();
  161. }
  162. else if (menuItemID >= 100 && menuItemID < 200)
  163. {
  164. RecentlyOpenedFilesList recentFiles;
  165. recentFiles.restoreFromString (appProperties.getUserSettings()
  166. ->getValue ("recentFilterGraphFiles"));
  167. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  168. graphEditor->graph.loadFrom (recentFiles.getFile (menuItemID - 100), true);
  169. }
  170. else if (menuItemID >= 200 && menuItemID < 210)
  171. {
  172. if (menuItemID == 200) pluginSortMethod = KnownPluginList::defaultOrder;
  173. else if (menuItemID == 201) pluginSortMethod = KnownPluginList::sortAlphabetically;
  174. else if (menuItemID == 202) pluginSortMethod = KnownPluginList::sortByCategory;
  175. else if (menuItemID == 203) pluginSortMethod = KnownPluginList::sortByManufacturer;
  176. else if (menuItemID == 204) pluginSortMethod = KnownPluginList::sortByFileSystemLocation;
  177. appProperties.getUserSettings()->setValue ("pluginSortMethod", (int) pluginSortMethod);
  178. menuItemsChanged();
  179. }
  180. else
  181. {
  182. createPlugin (getChosenType (menuItemID),
  183. proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  184. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f));
  185. }
  186. }
  187. void MainHostWindow::createPlugin (const PluginDescription* desc, int x, int y)
  188. {
  189. GraphDocumentComponent* const graphEditor = getGraphEditor();
  190. if (graphEditor != nullptr)
  191. graphEditor->createNewPlugin (desc, x, y);
  192. }
  193. void MainHostWindow::addPluginsToMenu (PopupMenu& m) const
  194. {
  195. knownPluginList.addToMenu (m, pluginSortMethod);
  196. }
  197. const PluginDescription* MainHostWindow::getChosenType (const int menuID) const
  198. {
  199. return knownPluginList.getType (knownPluginList.getIndexChosenByMenu (menuID));
  200. }
  201. //==============================================================================
  202. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  203. {
  204. return findFirstTargetParentComponent();
  205. }
  206. void MainHostWindow::getAllCommands (Array <CommandID>& commands)
  207. {
  208. // this returns the set of all commands that this target can perform..
  209. const CommandID ids[] = { CommandIDs::open,
  210. CommandIDs::save,
  211. CommandIDs::saveAs,
  212. CommandIDs::showPluginListEditor
  213. };
  214. commands.addArray (ids, numElementsInArray (ids));
  215. }
  216. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  217. {
  218. const String category ("General");
  219. switch (commandID)
  220. {
  221. case CommandIDs::open:
  222. result.setInfo ("Open...",
  223. "Opens a filter graph file",
  224. category, 0);
  225. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  226. break;
  227. case CommandIDs::save:
  228. result.setInfo ("Save",
  229. "Saves the current graph to a file",
  230. category, 0);
  231. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  232. break;
  233. case CommandIDs::saveAs:
  234. result.setInfo ("Save As...",
  235. "Saves a copy of the current graph to a file",
  236. category, 0);
  237. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  238. break;
  239. case CommandIDs::showPluginListEditor:
  240. result.setInfo ("Edit the list of available plug-Ins...", String::empty, category, 0);
  241. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. bool MainHostWindow::perform (const InvocationInfo& info)
  248. {
  249. GraphDocumentComponent* const graphEditor = getGraphEditor();
  250. switch (info.commandID)
  251. {
  252. case CommandIDs::open:
  253. if (graphEditor != nullptr && graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  254. graphEditor->graph.loadFromUserSpecifiedFile (true);
  255. break;
  256. case CommandIDs::save:
  257. if (graphEditor != nullptr)
  258. graphEditor->graph.save (true, true);
  259. break;
  260. case CommandIDs::saveAs:
  261. if (graphEditor != nullptr)
  262. graphEditor->graph.saveAs (File::nonexistent, true, true, true);
  263. break;
  264. case CommandIDs::showPluginListEditor:
  265. if (pluginListWindow == nullptr)
  266. pluginListWindow = new PluginListWindow (*this, formatManager);
  267. pluginListWindow->toFront (true);
  268. break;
  269. default:
  270. return false;
  271. }
  272. return true;
  273. }
  274. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  275. {
  276. return true;
  277. }
  278. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  279. {
  280. }
  281. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  282. {
  283. }
  284. void MainHostWindow::fileDragExit (const StringArray&)
  285. {
  286. }
  287. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  288. {
  289. GraphDocumentComponent* const graphEditor = getGraphEditor();
  290. if (graphEditor != nullptr)
  291. {
  292. if (files.size() == 1 && File (files[0]).hasFileExtension (filenameSuffix))
  293. {
  294. if (graphEditor->graph.saveIfNeededAndUserAgrees() == FileBasedDocument::savedOk)
  295. graphEditor->graph.loadFrom (File (files[0]), true);
  296. }
  297. else
  298. {
  299. OwnedArray <PluginDescription> typesFound;
  300. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  301. Point<int> pos (graphEditor->getLocalPoint (this, Point<int> (x, y)));
  302. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  303. createPlugin (typesFound.getUnchecked(i), pos.getX(), pos.getY());
  304. }
  305. }
  306. }
  307. GraphDocumentComponent* MainHostWindow::getGraphEditor() const
  308. {
  309. return dynamic_cast <GraphDocumentComponent*> (getContentComponent());
  310. }
  311. MidiKeyboardState* MainHostWindow::getMidiState() noexcept
  312. {
  313. return getGraphEditor()->getMidiState();
  314. }
  315. bool MainHostWindow::wasClosedByUser() const noexcept
  316. {
  317. return closed;
  318. }