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.

423 lines
17KB

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