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.

670 lines
27KB

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