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.

641 lines
26KB

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