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.

401 lines
16KB

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