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.

429 lines
17KB

  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. void showDocument() override
  34. {
  35. showSettingsPage (new ModuleSettingsPanel (project, moduleID));
  36. }
  37. void deleteItem() override
  38. {
  39. closeSettingsPage();
  40. project.getModules().removeModule (moduleID);
  41. }
  42. Icon getIcon() const override
  43. {
  44. auto iconColour = getOwnerView()->findColour (isSelected() ? defaultHighlightedTextColourId
  45. : treeIconColourId);
  46. if (! isSelected())
  47. {
  48. auto info = project.getModules().getModuleInfo (moduleID);
  49. if (info.isValid() && info.getVendor() == "juce")
  50. {
  51. if (info.getLicense() == "ISC")
  52. iconColour = Colours::lightblue;
  53. else if (info.getLicense() == "GPL/Commercial")
  54. iconColour = Colours::orange;
  55. }
  56. }
  57. return Icon (getIcons().singleModule, iconColour);
  58. }
  59. void showPopupMenu() override
  60. {
  61. PopupMenu menu;
  62. menu.addItem (1, "Remove this module");
  63. launchPopupMenu (menu);
  64. }
  65. void handlePopupMenuResult (int resultCode) override
  66. {
  67. if (resultCode == 1)
  68. deleteItem();
  69. }
  70. Project& project;
  71. String moduleID;
  72. private:
  73. bool hasMissingDependencies() const
  74. {
  75. return project.getModules().getExtraDependenciesNeeded (moduleID).size() > 0;
  76. }
  77. //==============================================================================
  78. class ModuleSettingsPanel : public Component
  79. {
  80. public:
  81. ModuleSettingsPanel (Project& p, const String& modID)
  82. : group (p.getModules().getModuleInfo (modID).getID(), Icon (getIcons().singleModule, Colours::transparentBlack)),
  83. project (p),
  84. moduleID (modID)
  85. {
  86. addAndMakeVisible (group);
  87. refresh();
  88. }
  89. void refresh()
  90. {
  91. setEnabled (project.getModules().isModuleEnabled (moduleID));
  92. PropertyListBuilder props;
  93. props.add (new ModuleInfoComponent (project, moduleID));
  94. if (project.getModules().getExtraDependenciesNeeded (moduleID).size() > 0)
  95. props.add (new MissingDependenciesComponent (project, moduleID));
  96. for (Project::ExporterIterator exporter (project); exporter.next();)
  97. props.add (new FilePathPropertyComponent (exporter->getPathForModuleValue (moduleID),
  98. "Path for " + exporter->getName().quoted(),
  99. true, "*", project.getProjectFolder()),
  100. "A path to the folder that contains the " + moduleID + " module when compiling the "
  101. + exporter->getName().quoted() + " target. "
  102. "This can be an absolute path, or relative to the jucer project folder, but it "
  103. "must be valid on the filesystem of the target machine that will be performing this build.");
  104. props.add (new BooleanPropertyComponent (project.getModules().shouldCopyModuleFilesLocally (moduleID),
  105. "Create local copy", "Copy the module into the project folder"),
  106. "If this is enabled, then a local copy of the entire module will be made inside your project (in the auto-generated JuceLibraryFiles folder), "
  107. "so that your project will be self-contained, and won't need to contain any references to files in other folders. "
  108. "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.");
  109. props.add (new BooleanPropertyComponent (project.getModules().shouldShowAllModuleFilesInProject (moduleID),
  110. "Add source to project", "Make module files browsable in projects"),
  111. "If this is enabled, then the entire source tree from this module will be shown inside your project, "
  112. "making it easy to browse/edit the module's classes. If disabled, then only the minimum number of files "
  113. "required to compile it will appear inside your project.");
  114. StringArray possibleValues;
  115. possibleValues.add ("(Use Default)");
  116. possibleValues.add ("Enabled");
  117. possibleValues.add ("Disabled");
  118. Array<var> mappings;
  119. mappings.add (Project::configFlagDefault);
  120. mappings.add (Project::configFlagEnabled);
  121. mappings.add (Project::configFlagDisabled);
  122. ModuleDescription info (project.getModules().getModuleInfo (moduleID));
  123. if (info.isValid())
  124. {
  125. OwnedArray <Project::ConfigFlag> configFlags;
  126. LibraryModule (info).getConfigFlags (project, configFlags);
  127. for (int i = 0; i < configFlags.size(); ++i)
  128. {
  129. ChoicePropertyComponent* c = new ChoicePropertyComponent (configFlags[i]->value,
  130. configFlags[i]->symbol,
  131. possibleValues, mappings);
  132. c->setTooltip (configFlags[i]->description);
  133. props.add (c);
  134. }
  135. }
  136. group.setProperties (props);
  137. parentSizeChanged();
  138. }
  139. void parentSizeChanged() override { updateSize (*this, group); }
  140. void resized() override { group.setBounds (getLocalBounds().withTrimmedLeft (12)); }
  141. private:
  142. PropertyGroupComponent group;
  143. Project& project;
  144. String moduleID;
  145. //==============================================================================
  146. class ModuleInfoComponent : public PropertyComponent,
  147. private Value::Listener
  148. {
  149. public:
  150. ModuleInfoComponent (Project& p, const String& modID)
  151. : PropertyComponent ("Module", 150), project (p), moduleID (modID)
  152. {
  153. for (Project::ExporterIterator exporter (project); exporter.next();)
  154. listeningValues.add (new Value (exporter->getPathForModuleValue (moduleID)))
  155. ->addListener (this);
  156. refresh();
  157. }
  158. private:
  159. void refresh() override
  160. {
  161. info = project.getModules().getModuleInfo (moduleID);
  162. repaint();
  163. }
  164. void paint (Graphics& g) override
  165. {
  166. auto bounds = getLocalBounds().reduced (10);
  167. bounds.removeFromTop (5);
  168. if (info.isValid())
  169. {
  170. auto topSlice = bounds.removeFromTop (bounds.getHeight() / 3);
  171. bounds.removeFromTop (bounds.getHeight() / 6);
  172. auto bottomSlice = bounds;
  173. g.setColour (findColour (defaultTextColourId));
  174. g.drawFittedText (info.getName(), topSlice.removeFromTop (topSlice.getHeight() / 3), Justification::centredLeft, 1);
  175. g.drawFittedText ("Version: " + info.getVersion(), topSlice.removeFromTop (topSlice.getHeight() / 2), Justification::centredLeft, 1);
  176. g.drawFittedText ("License: " + info.getLicense(), topSlice.removeFromTop (topSlice.getHeight()), Justification::centredLeft, 1);
  177. g.drawFittedText (info.getDescription(), bottomSlice, Justification::topLeft, 3, 1.0f);
  178. }
  179. else
  180. {
  181. g.setColour (Colours::red);
  182. g.drawFittedText ("Cannot find this module at the specified path!", bounds, Justification::centred, 1);
  183. }
  184. }
  185. void valueChanged (Value&) override
  186. {
  187. refresh();
  188. }
  189. Project& project;
  190. String moduleID;
  191. OwnedArray<Value> listeningValues;
  192. ModuleDescription info;
  193. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModuleInfoComponent)
  194. };
  195. //==============================================================================
  196. class MissingDependenciesComponent : public PropertyComponent,
  197. public ButtonListener
  198. {
  199. public:
  200. MissingDependenciesComponent (Project& p, const String& modID)
  201. : PropertyComponent ("Dependencies", 100),
  202. project (p), moduleID (modID),
  203. missingDependencies (project.getModules().getExtraDependenciesNeeded (modID)),
  204. fixButton ("Add Required Modules")
  205. {
  206. addAndMakeVisible (fixButton);
  207. fixButton.setColour (TextButton::buttonColourId, Colours::red);
  208. fixButton.setColour (TextButton::textColourOffId, Colours::white);
  209. fixButton.addListener (this);
  210. }
  211. void refresh() override {}
  212. void paint (Graphics& g) override
  213. {
  214. String text ("This module has missing dependencies!\n\n"
  215. "To build correctly, it requires the following modules to be added:\n");
  216. text << missingDependencies.joinIntoString (", ");
  217. g.setColour (Colours::red);
  218. g.drawFittedText (text, getLocalBounds().reduced (4, 16), Justification::topLeft, 3);
  219. }
  220. void buttonClicked (Button*) override
  221. {
  222. bool anyFailed = false;
  223. ModuleList list;
  224. list.scanAllKnownFolders (project);
  225. for (int i = missingDependencies.size(); --i >= 0;)
  226. {
  227. if (const ModuleDescription* info = list.getModuleWithID (missingDependencies[i]))
  228. project.getModules().addModule (info->moduleFolder, project.getModules().areMostModulesCopiedLocally());
  229. else
  230. anyFailed = true;
  231. }
  232. if (ModuleSettingsPanel* p = findParentComponentOfClass<ModuleSettingsPanel>())
  233. p->refresh();
  234. if (anyFailed)
  235. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  236. "Adding Missing Dependencies",
  237. "Couldn't locate some of these modules - you'll need to find their "
  238. "folders manually and add them to the list.");
  239. }
  240. void resized() override
  241. {
  242. fixButton.setBounds (getWidth() - 168, getHeight() - 26, 160, 22);
  243. }
  244. private:
  245. Project& project;
  246. String moduleID;
  247. StringArray missingDependencies;
  248. TextButton fixButton;
  249. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MissingDependenciesComponent)
  250. };
  251. };
  252. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModuleItem)
  253. };
  254. //==============================================================================
  255. class EnabledModulesItem : public ConfigTreeItemBase
  256. {
  257. public:
  258. EnabledModulesItem (Project& p)
  259. : project (p),
  260. moduleListTree (p.getModules().state)
  261. {
  262. moduleListTree.addListener (this);
  263. }
  264. int getItemHeight() const override { return 22; }
  265. bool isModulesList() const override { return true; }
  266. bool canBeSelected() const override { return true; }
  267. bool mightContainSubItems() override { return true; }
  268. String getUniqueName() const override { return "modules"; }
  269. String getRenamingName() const override { return getDisplayName(); }
  270. String getDisplayName() const override { return "Modules"; }
  271. void setName (const String&) override {}
  272. bool isMissing() const override { return false; }
  273. Icon getIcon() const override { return Icon (getIcons().graph, getContentColour (true)); }
  274. void showDocument() override
  275. {
  276. if (ProjectContentComponent* pcc = getProjectContentComponent())
  277. pcc->setEditorComponent (new ModulesPanel (project), nullptr);
  278. }
  279. static File getModuleFolder (const File& draggedFile)
  280. {
  281. if (draggedFile.hasFileExtension (headerFileExtensions))
  282. return draggedFile.getParentDirectory();
  283. return draggedFile;
  284. }
  285. bool isInterestedInFileDrag (const StringArray& files) override
  286. {
  287. for (int i = files.size(); --i >= 0;)
  288. if (ModuleDescription (getModuleFolder (files[i])).isValid())
  289. return true;
  290. return false;
  291. }
  292. void filesDropped (const StringArray& files, int /*insertIndex*/) override
  293. {
  294. Array<ModuleDescription> modules;
  295. for (int i = files.size(); --i >= 0;)
  296. {
  297. ModuleDescription m (getModuleFolder (files[i]));
  298. if (m.isValid())
  299. modules.add (m);
  300. }
  301. for (int i = 0; i < modules.size(); ++i)
  302. project.getModules().addModule (modules.getReference(i).moduleFolder,
  303. project.getModules().areMostModulesCopiedLocally());
  304. }
  305. void addSubItems() override
  306. {
  307. for (int i = 0; i < project.getModules().getNumModules(); ++i)
  308. addSubItem (new ModuleItem (project, project.getModules().getModuleID (i)));
  309. }
  310. void showPopupMenu() override
  311. {
  312. PopupMenu menu, knownModules, copyModeMenu;
  313. const StringArray modules (getAvailableModules());
  314. for (int i = 0; i < modules.size(); ++i)
  315. knownModules.addItem (1 + i, modules[i], ! project.getModules().isModuleEnabled (modules[i]));
  316. menu.addSubMenu ("Add a module", knownModules);
  317. menu.addSeparator();
  318. menu.addItem (1001, "Add a module from a specified folder...");
  319. launchPopupMenu (menu);
  320. }
  321. void handlePopupMenuResult (int resultCode) override
  322. {
  323. if (resultCode == 1001)
  324. project.getModules().addModuleFromUserSelectedFile();
  325. else if (resultCode > 0)
  326. project.getModules().addModuleInteractive (getAvailableModules() [resultCode - 1]);
  327. }
  328. StringArray getAvailableModules()
  329. {
  330. ModuleList list;
  331. list.scanAllKnownFolders (project);
  332. return list.getIDs();
  333. }
  334. //==============================================================================
  335. void valueTreeChildAdded (ValueTree& parentTree, ValueTree&) override { refreshIfNeeded (parentTree); }
  336. void valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int) override { refreshIfNeeded (parentTree); }
  337. void valueTreeChildOrderChanged (ValueTree& parentTree, int, int) override { refreshIfNeeded (parentTree); }
  338. void refreshIfNeeded (ValueTree& changedTree)
  339. {
  340. if (changedTree == moduleListTree)
  341. refreshSubItems();
  342. }
  343. private:
  344. Project& project;
  345. ValueTree moduleListTree;
  346. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EnabledModulesItem)
  347. };