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.

577 lines
23KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. class ModuleItem : public ConfigTreeItemBase
  20. {
  21. public:
  22. ModuleItem (Project& p, const String& modID)
  23. : project (p), moduleID (modID)
  24. {
  25. }
  26. bool canBeSelected() const override { return true; }
  27. bool mightContainSubItems() override { return false; }
  28. String getUniqueName() const override { return "module_" + moduleID; }
  29. String getDisplayName() const override
  30. {
  31. auto versionNum = project.getModules().getModuleInfo (moduleID).getVersion();
  32. return moduleID + (versionNum != ProjucerApplication::getApp().getApplicationVersion() ? String (" (" + versionNum + ")") : "");
  33. }
  34. String getRenamingName() const override { return getDisplayName(); }
  35. void setName (const String&) override {}
  36. bool isMissing() const override { return hasMissingDependencies(); }
  37. void showDocument() override
  38. {
  39. showSettingsPage (new ModuleSettingsPanel (project, moduleID, *this));
  40. }
  41. void deleteItem() override
  42. {
  43. closeSettingsPage();
  44. project.getModules().removeModule (moduleID);
  45. }
  46. Icon getIcon() const override
  47. {
  48. auto iconColour = getOwnerView()->findColour (isSelected() ? defaultHighlightedTextColourId
  49. : treeIconColourId);
  50. if (! isSelected())
  51. {
  52. auto info = project.getModules().getModuleInfo (moduleID);
  53. if (info.isValid() && info.getVendor() == "juce")
  54. {
  55. if (info.getLicense() == "ISC")
  56. iconColour = Colours::lightblue;
  57. else if (info.getLicense() == "GPL/Commercial")
  58. iconColour = Colours::orange;
  59. }
  60. }
  61. return Icon (getIcons().singleModule, iconColour);
  62. }
  63. void showPopupMenu() override
  64. {
  65. PopupMenu menu;
  66. menu.addItem (1, "Remove this module");
  67. launchPopupMenu (menu);
  68. }
  69. void handlePopupMenuResult (int resultCode) override
  70. {
  71. if (resultCode == 1)
  72. deleteItem();
  73. }
  74. Project& project;
  75. String moduleID;
  76. private:
  77. bool hasMissingDependencies() const
  78. {
  79. return project.getModules().getExtraDependenciesNeeded (moduleID).size() > 0;
  80. }
  81. //==============================================================================
  82. class ModuleSettingsPanel : public Component,
  83. private Value::Listener
  84. {
  85. public:
  86. ModuleSettingsPanel (Project& p, const String& modID, ModuleItem& o)
  87. : group (p.getModules().getModuleInfo (modID).getID(),
  88. Icon (getIcons().singleModule, Colours::transparentBlack)),
  89. project (p), owner (o), moduleID (modID)
  90. {
  91. defaultJuceModulePathValue.referTo (getAppSettings().getStoredPath (Ids::defaultJuceModulePath));
  92. defaultUserModulePathValue.referTo (getAppSettings().getStoredPath (Ids::defaultUserModulePath));
  93. defaultJuceModulePathValue.addListener (this);
  94. defaultUserModulePathValue.addListener (this);
  95. addAndMakeVisible (group);
  96. refresh();
  97. }
  98. void refresh()
  99. {
  100. auto& modules = project.getModules();
  101. const auto& isUsingGlobalPathValue = modules.shouldUseGlobalPath (moduleID);
  102. setEnabled (modules.isModuleEnabled (moduleID));
  103. PropertyListBuilder props;
  104. props.add (new ModuleInfoComponent (project, moduleID));
  105. if (modules.getExtraDependenciesNeeded (moduleID).size() > 0)
  106. props.add (new MissingDependenciesComponent (project, moduleID));
  107. modulePathValueSources.clear();
  108. for (Project::ExporterIterator exporter (project); exporter.next();)
  109. {
  110. auto key = modules.isJuceModule (moduleID) ? Ids::defaultJuceModulePath
  111. : Ids::defaultUserModulePath;
  112. Value src (modulePathValueSources.add (new DependencyPathValueSource (exporter->getPathForModuleValue (moduleID),
  113. key, exporter->getTargetOSForExporter())));
  114. auto* pathComponent = new DependencyFilePathPropertyComponent (src, "Path for " + exporter->getName().quoted(),
  115. true, "*", project.getProjectFolder());
  116. props.add (pathComponent,
  117. "A path to the folder that contains the " + moduleID + " module when compiling the "
  118. + exporter->getName().quoted() + " target. "
  119. "This can be an absolute path, or relative to the jucer project folder, but it "
  120. "must be valid on the filesystem of the target machine that will be performing this build. If this "
  121. "is empty then the global path will be used.");
  122. pathComponent->setEnabled (! isUsingGlobalPathValue.getValue());
  123. }
  124. globalPathValue.referTo (isUsingGlobalPathValue);
  125. auto menuItemString = (TargetOS::getThisOS() == TargetOS::osx ? "\"Projucer->Global Search Paths...\""
  126. : "\"File->Global Search Paths...\"");
  127. props.add (new BooleanPropertyComponent (globalPathValue,
  128. "Use global path", "Use global path for this module"),
  129. String ("If this is enabled, then the locally-stored global path (set in the ") + menuItemString + " menu item) "
  130. "will be used as the path to this module. "
  131. "This means that if this Projucer project is opened on another machine it will use that machine's global path as the path to this module.");
  132. globalPathValue.addListener (this);
  133. props.add (new BooleanPropertyComponent (modules.shouldCopyModuleFilesLocally (moduleID),
  134. "Create local copy", "Copy the module into the project folder"),
  135. "If this is enabled, then a local copy of the entire module will be made inside your project (in the auto-generated JuceLibraryFiles folder), "
  136. "so that your project will be self-contained, and won't need to contain any references to files in other folders. "
  137. "This also means that you can check the module into your source-control system to make sure it is always in sync with your own code.");
  138. props.add (new BooleanPropertyComponent (modules.shouldShowAllModuleFilesInProject (moduleID),
  139. "Add source to project", "Make module files browsable in projects"),
  140. "If this is enabled, then the entire source tree from this module will be shown inside your project, "
  141. "making it easy to browse/edit the module's classes. If disabled, then only the minimum number of files "
  142. "required to compile it will appear inside your project.");
  143. StringArray possibleValues;
  144. possibleValues.add ("(Use Default)");
  145. possibleValues.add ("Enabled");
  146. possibleValues.add ("Disabled");
  147. Array<var> mappings;
  148. mappings.add (Project::configFlagDefault);
  149. mappings.add (Project::configFlagEnabled);
  150. mappings.add (Project::configFlagDisabled);
  151. ModuleDescription info (modules.getModuleInfo (moduleID));
  152. if (info.isValid())
  153. {
  154. OwnedArray <Project::ConfigFlag> configFlags;
  155. LibraryModule (info).getConfigFlags (project, configFlags);
  156. for (int i = 0; i < configFlags.size(); ++i)
  157. {
  158. ChoicePropertyComponent* c = new ChoicePropertyComponent (configFlags[i]->value,
  159. configFlags[i]->symbol,
  160. possibleValues, mappings);
  161. c->setTooltip (configFlags[i]->description);
  162. props.add (c);
  163. }
  164. }
  165. group.setProperties (props);
  166. parentSizeChanged();
  167. }
  168. void parentSizeChanged() override { updateSize (*this, group); }
  169. void resized() override { group.setBounds (getLocalBounds().withTrimmedLeft (12)); }
  170. private:
  171. PropertyGroupComponent group;
  172. Project& project;
  173. ModuleItem& owner;
  174. String moduleID;
  175. Value globalPathValue;
  176. Value defaultJuceModulePathValue, defaultUserModulePathValue;
  177. ReferenceCountedArray<Value::ValueSource> modulePathValueSources;
  178. //==============================================================================
  179. void valueChanged (Value& v) override
  180. {
  181. if (v == globalPathValue)
  182. {
  183. auto useGlobalPath = globalPathValue.getValue();
  184. for (auto prop : group.properties)
  185. {
  186. if (auto* pathPropertyComponent = dynamic_cast<DependencyFilePathPropertyComponent*> (prop))
  187. pathPropertyComponent->setEnabled (! useGlobalPath);
  188. }
  189. }
  190. if (auto* moduleInfo = dynamic_cast<ModuleInfoComponent*> (group.properties.getUnchecked (0)))
  191. moduleInfo->refresh();
  192. owner.treeHasChanged();
  193. }
  194. //==============================================================================
  195. class ModuleInfoComponent : public PropertyComponent,
  196. private Value::Listener
  197. {
  198. public:
  199. ModuleInfoComponent (Project& p, const String& modID)
  200. : PropertyComponent ("Module", 150), project (p), moduleID (modID)
  201. {
  202. for (Project::ExporterIterator exporter (project); exporter.next();)
  203. listeningValues.add (new Value (exporter->getPathForModuleValue (moduleID)))
  204. ->addListener (this);
  205. refresh();
  206. }
  207. void refresh() override
  208. {
  209. info = project.getModules().getModuleInfo (moduleID);
  210. repaint();
  211. }
  212. private:
  213. void paint (Graphics& g) override
  214. {
  215. auto bounds = getLocalBounds().reduced (10);
  216. bounds.removeFromTop (5);
  217. if (info.isValid())
  218. {
  219. auto topSlice = bounds.removeFromTop (bounds.getHeight() / 2);
  220. bounds.removeFromTop (bounds.getHeight() / 6);
  221. auto bottomSlice = bounds;
  222. g.setColour (findColour (defaultTextColourId));
  223. g.drawFittedText (info.getName(), topSlice.removeFromTop (topSlice.getHeight() / 4), Justification::centredLeft, 1);
  224. g.drawFittedText ("Version: " + info.getVersion(), topSlice.removeFromTop (topSlice.getHeight() / 3), Justification::centredLeft, 1);
  225. g.drawFittedText ("License: " + info.getLicense(), topSlice.removeFromTop (topSlice.getHeight() / 2), Justification::centredLeft, 1);
  226. g.drawFittedText ("Location: " + info.getFolder().getParentDirectory().getFullPathName(),
  227. topSlice.removeFromTop (topSlice.getHeight()), Justification::centredLeft, 1);
  228. g.drawFittedText (info.getDescription(), bottomSlice, Justification::topLeft, 3, 1.0f);
  229. }
  230. else
  231. {
  232. g.setColour (Colours::red);
  233. g.drawFittedText ("Cannot find this module at the specified path!", bounds, Justification::centred, 1);
  234. }
  235. }
  236. void valueChanged (Value&) override
  237. {
  238. refresh();
  239. }
  240. Project& project;
  241. String moduleID;
  242. OwnedArray<Value> listeningValues;
  243. ModuleDescription info;
  244. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModuleInfoComponent)
  245. };
  246. //==============================================================================
  247. class MissingDependenciesComponent : public PropertyComponent,
  248. public ButtonListener
  249. {
  250. public:
  251. MissingDependenciesComponent (Project& p, const String& modID)
  252. : PropertyComponent ("Dependencies", 100),
  253. project (p), moduleID (modID),
  254. missingDependencies (project.getModules().getExtraDependenciesNeeded (modID)),
  255. fixButton ("Add Required Modules")
  256. {
  257. addAndMakeVisible (fixButton);
  258. fixButton.setColour (TextButton::buttonColourId, Colours::red);
  259. fixButton.setColour (TextButton::textColourOffId, Colours::white);
  260. fixButton.addListener (this);
  261. }
  262. void refresh() override {}
  263. void paint (Graphics& g) override
  264. {
  265. String text ("This module has missing dependencies!\n\n"
  266. "To build correctly, it requires the following modules to be added:\n");
  267. text << missingDependencies.joinIntoString (", ");
  268. g.setColour (Colours::red);
  269. g.drawFittedText (text, getLocalBounds().reduced (4, 16), Justification::topLeft, 3);
  270. }
  271. void buttonClicked (Button*) override
  272. {
  273. ModuleList list;
  274. list.scanGlobalJuceModulePath();
  275. if (! tryToFix (list))
  276. list.scanGlobalUserModulePath();
  277. if (! tryToFix (list))
  278. list.scanProjectExporterModulePaths (project);
  279. bool fixed = tryToFix (list);
  280. if (ModuleSettingsPanel* p = findParentComponentOfClass<ModuleSettingsPanel>())
  281. p->refresh();
  282. if (! fixed)
  283. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  284. "Adding Missing Dependencies",
  285. "Couldn't locate some of these modules - you'll need to find their "
  286. "folders manually and add them to the list.");
  287. }
  288. void resized() override
  289. {
  290. fixButton.setBounds (getWidth() - 168, getHeight() - 26, 160, 22);
  291. }
  292. private:
  293. Project& project;
  294. String moduleID;
  295. StringArray missingDependencies;
  296. TextButton fixButton;
  297. bool tryToFix (ModuleList& list)
  298. {
  299. auto& modules = project.getModules();
  300. auto copyLocally = modules.areMostModulesCopiedLocally();
  301. auto useGlobalPath = modules.areMostModulesUsingGlobalPath();
  302. StringArray missing;
  303. for (auto missingModule : missingDependencies)
  304. {
  305. if (auto* info = list.getModuleWithID (missingModule))
  306. modules.addModule (info->moduleFolder, copyLocally, useGlobalPath);
  307. else
  308. missing.add (missingModule);
  309. }
  310. missingDependencies.swapWith (missing);
  311. return (missingDependencies.size() == 0);
  312. }
  313. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MissingDependenciesComponent)
  314. };
  315. };
  316. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModuleItem)
  317. };
  318. //==============================================================================
  319. class EnabledModulesItem : public ConfigTreeItemBase
  320. {
  321. public:
  322. EnabledModulesItem (Project& p)
  323. : project (p),
  324. moduleListTree (p.getModules().state)
  325. {
  326. moduleListTree.addListener (this);
  327. }
  328. int getItemHeight() const override { return 22; }
  329. bool isModulesList() const override { return true; }
  330. bool canBeSelected() const override { return true; }
  331. bool mightContainSubItems() override { return true; }
  332. String getUniqueName() const override { return "modules"; }
  333. String getRenamingName() const override { return getDisplayName(); }
  334. String getDisplayName() const override { return "Modules"; }
  335. void setName (const String&) override {}
  336. bool isMissing() const override { return false; }
  337. Icon getIcon() const override { return Icon (getIcons().graph, getContentColour (true)); }
  338. void showDocument() override
  339. {
  340. if (ProjectContentComponent* pcc = getProjectContentComponent())
  341. pcc->setEditorComponent (new ModulesPanel (project), nullptr);
  342. }
  343. static File getModuleFolder (const File& draggedFile)
  344. {
  345. if (draggedFile.hasFileExtension (headerFileExtensions))
  346. return draggedFile.getParentDirectory();
  347. return draggedFile;
  348. }
  349. bool isInterestedInFileDrag (const StringArray& files) override
  350. {
  351. for (int i = files.size(); --i >= 0;)
  352. if (ModuleDescription (getModuleFolder (files[i])).isValid())
  353. return true;
  354. return false;
  355. }
  356. void filesDropped (const StringArray& files, int /*insertIndex*/) override
  357. {
  358. Array<ModuleDescription> modules;
  359. for (int i = files.size(); --i >= 0;)
  360. {
  361. ModuleDescription m (getModuleFolder (files[i]));
  362. if (m.isValid())
  363. modules.add (m);
  364. }
  365. for (int i = 0; i < modules.size(); ++i)
  366. project.getModules().addModule (modules.getReference(i).moduleFolder,
  367. project.getModules().areMostModulesCopiedLocally(),
  368. project.getModules().areMostModulesUsingGlobalPath());
  369. }
  370. void addSubItems() override
  371. {
  372. for (int i = 0; i < project.getModules().getNumModules(); ++i)
  373. addSubItem (new ModuleItem (project, project.getModules().getModuleID (i)));
  374. }
  375. void showPopupMenu() override
  376. {
  377. auto& modules = project.getModules();
  378. PopupMenu knownModules, jucePathModules, userPathModules, exporterPathsModules;
  379. auto index = 100;
  380. auto globalJucePathModules = getAvailableModulesInGlobalJucePath();
  381. for (auto m : globalJucePathModules)
  382. jucePathModules.addItem (index++, m, ! modules.isModuleEnabled (m));
  383. knownModules.addSubMenu ("Global JUCE modules path", jucePathModules);
  384. index = 200;
  385. auto globalUserPathModules = getAvailableModulesInGlobalUserPath();
  386. for (auto m : getAvailableModulesInGlobalUserPath())
  387. {
  388. if (! globalJucePathModules.contains (m))
  389. userPathModules.addItem (index++, m, ! modules.isModuleEnabled (m));
  390. }
  391. knownModules.addSubMenu ("Global user modules path", userPathModules);
  392. index = 300;
  393. for (auto m : getAvailableModulesInExporterPaths())
  394. {
  395. if (! globalJucePathModules.contains (m) && ! globalUserPathModules.contains (m))
  396. exporterPathsModules.addItem (index++, m, ! modules.isModuleEnabled (m));
  397. }
  398. knownModules.addSubMenu ("Exporter paths", exporterPathsModules);
  399. PopupMenu menu;
  400. menu.addSubMenu ("Add a module", knownModules);
  401. menu.addSeparator();
  402. menu.addItem (1001, "Add a module from a specified folder...");
  403. launchPopupMenu (menu);
  404. }
  405. void handlePopupMenuResult (int resultCode) override
  406. {
  407. auto& modules = project.getModules();
  408. if (resultCode == 1001)
  409. {
  410. modules.addModuleFromUserSelectedFile();
  411. }
  412. else if (resultCode > 0)
  413. {
  414. if (resultCode < 200)
  415. modules.addModuleInteractive (getAvailableModulesInGlobalJucePath() [resultCode - 100]);
  416. else if (resultCode < 300)
  417. modules.addModuleInteractive (getAvailableModulesInGlobalUserPath() [resultCode - 200]);
  418. else if (resultCode < 400)
  419. modules.addModuleInteractive (getAvailableModulesInExporterPaths() [resultCode - 300]);
  420. }
  421. }
  422. StringArray getAvailableModulesInExporterPaths()
  423. {
  424. ModuleList list;
  425. list.scanProjectExporterModulePaths (project);
  426. return list.getIDs();
  427. }
  428. StringArray getAvailableModulesInGlobalJucePath()
  429. {
  430. ModuleList list;
  431. list.addAllModulesInFolder ({ getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString() });
  432. return list.getIDs();
  433. }
  434. StringArray getAvailableModulesInGlobalUserPath()
  435. {
  436. ModuleList list;
  437. auto paths = StringArray::fromTokens (getAppSettings().getStoredPath (Ids::defaultUserModulePath).toString(), ";", {});
  438. for (auto p : paths)
  439. {
  440. auto f = File::createFileWithoutCheckingPath (p.trim());
  441. if (f.exists())
  442. list.addAllModulesInFolder (f);
  443. }
  444. return list.getIDs();
  445. }
  446. //==============================================================================
  447. void valueTreeChildAdded (ValueTree& parentTree, ValueTree&) override { refreshIfNeeded (parentTree); }
  448. void valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int) override { refreshIfNeeded (parentTree); }
  449. void valueTreeChildOrderChanged (ValueTree& parentTree, int, int) override { refreshIfNeeded (parentTree); }
  450. void refreshIfNeeded (ValueTree& changedTree)
  451. {
  452. if (changedTree == moduleListTree)
  453. refreshSubItems();
  454. }
  455. private:
  456. Project& project;
  457. ValueTree moduleListTree;
  458. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EnabledModulesItem)
  459. };