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.

521 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_MODULESPANEL_H_D0C12034__
  19. #define __JUCER_MODULESPANEL_H_D0C12034__
  20. class ModulesPanel : public PropertyComponent,
  21. public FilenameComponentListener,
  22. public ButtonListener
  23. {
  24. public:
  25. ModulesPanel (Project& p)
  26. : PropertyComponent ("Modules", 500),
  27. project (p),
  28. modulesLocation ("modules", ModuleList::getLocalModulesFolder (&project),
  29. true, true, false, "*", String::empty,
  30. "Select a folder containing your JUCE modules..."),
  31. modulesLabel (String::empty, "Module source folder:"),
  32. updateModulesButton ("Check for module updates..."),
  33. moduleListBox (moduleList),
  34. copyingMessage (p, moduleList)
  35. {
  36. moduleList.rescan (ModuleList::getLocalModulesFolder (&project));
  37. addAndMakeVisible (&modulesLocation);
  38. modulesLocation.addListener (this);
  39. modulesLabel.attachToComponent (&modulesLocation, true);
  40. addAndMakeVisible (&updateModulesButton);
  41. updateModulesButton.addListener (this);
  42. moduleListBox.setOwner (this);
  43. addAndMakeVisible (&moduleListBox);
  44. addAndMakeVisible (&copyingMessage);
  45. copyingMessage.refresh();
  46. }
  47. void filenameComponentChanged (FilenameComponent*)
  48. {
  49. moduleList.rescan (modulesLocation.getCurrentFile());
  50. modulesLocation.setCurrentFile (moduleList.getModulesFolder(), false, false);
  51. ModuleList::setLocalModulesFolder (moduleList.getModulesFolder());
  52. moduleListBox.refresh();
  53. }
  54. void buttonClicked (Button*)
  55. {
  56. JuceUpdater::show (moduleList, getTopLevelComponent(), "");
  57. filenameComponentChanged (nullptr);
  58. }
  59. bool isEnabled (const ModuleList::Module* m) const
  60. {
  61. return project.isModuleEnabled (m->uid);
  62. }
  63. void setEnabled (const ModuleList::Module* m, bool enable)
  64. {
  65. if (enable)
  66. project.addModule (m->uid, true);
  67. else
  68. project.removeModule (m->uid);
  69. refresh();
  70. }
  71. bool areDependenciesMissing (const ModuleList::Module* m)
  72. {
  73. return moduleList.getExtraDependenciesNeeded (project, *m).size() > 0;
  74. }
  75. void selectionChanged (const ModuleList::Module* selectedModule)
  76. {
  77. settings = nullptr;
  78. if (selectedModule != nullptr)
  79. addAndMakeVisible (settings = new ModuleSettingsPanel (project, moduleList, selectedModule->uid));
  80. copyingMessage.refresh();
  81. resized();
  82. }
  83. void refresh()
  84. {
  85. moduleListBox.refresh();
  86. if (settings != nullptr)
  87. settings->refreshAll();
  88. copyingMessage.refresh();
  89. }
  90. void paint (Graphics& g) // (overridden to avoid drawing the name)
  91. {
  92. getLookAndFeel().drawPropertyComponentBackground (g, getWidth(), getHeight(), *this);
  93. }
  94. void resized()
  95. {
  96. modulesLocation.setBounds (150, 3, getWidth() - 180 - 150, 25);
  97. updateModulesButton.setBounds (modulesLocation.getRight() + 6, 3, getWidth() - modulesLocation.getRight() - 12, 25);
  98. moduleListBox.setBounds (5, 34, getWidth() / 3, getHeight() - 72);
  99. copyingMessage.setBounds (5, moduleListBox.getBottom() + 2, getWidth() - 10, getHeight() - moduleListBox.getBottom() - 4);
  100. if (settings != nullptr)
  101. settings->setBounds (moduleListBox.getRight() + 5, moduleListBox.getY(),
  102. getWidth() - moduleListBox.getRight() - 9, moduleListBox.getHeight());
  103. }
  104. //==============================================================================
  105. class ModuleSelectionListBox : public ListBox,
  106. public ListBoxModel
  107. {
  108. public:
  109. ModuleSelectionListBox (ModuleList& ml)
  110. : list (ml), owner (nullptr)
  111. {
  112. setColour (ListBox::backgroundColourId, Colours::white.withAlpha (0.4f));
  113. setTooltip ("Use this list to select which modules should be included in your app.\n"
  114. "Any modules which have missing dependencies will be shown in red.");
  115. }
  116. void setOwner (ModulesPanel* newOwner)
  117. {
  118. owner = newOwner;
  119. setModel (this);
  120. }
  121. void refresh()
  122. {
  123. updateContent();
  124. repaint();
  125. }
  126. int getNumRows()
  127. {
  128. return list.modules.size();
  129. }
  130. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected)
  131. {
  132. if (rowIsSelected)
  133. g.fillAll (findColour (TextEditor::highlightColourId));
  134. const ModuleList::Module* const m = list.modules [rowNumber];
  135. if (m != nullptr)
  136. {
  137. const float tickSize = height * 0.7f;
  138. getLookAndFeel().drawTickBox (g, *this, (height - tickSize) / 2, (height - tickSize) / 2, tickSize, tickSize,
  139. owner->isEnabled (m), true, false, false);
  140. if (owner->isEnabled (m) && owner->areDependenciesMissing (m))
  141. g.setColour (Colours::red);
  142. else
  143. g.setColour (Colours::black);
  144. g.setFont (Font (height * 0.7f, Font::bold));
  145. g.drawFittedText (m->uid, height, 0, width - height, height, Justification::centredLeft, 1);
  146. }
  147. }
  148. void listBoxItemClicked (int row, const MouseEvent& e)
  149. {
  150. if (e.x < getRowHeight())
  151. flipRow (row);
  152. }
  153. void listBoxItemDoubleClicked (int row, const MouseEvent& e)
  154. {
  155. flipRow (row);
  156. }
  157. void returnKeyPressed (int row)
  158. {
  159. flipRow (row);
  160. }
  161. void selectedRowsChanged (int lastRowSelected)
  162. {
  163. owner->selectionChanged (list.modules [lastRowSelected]);
  164. }
  165. void flipRow (int row)
  166. {
  167. const ModuleList::Module* const m = list.modules [row];
  168. if (m != nullptr)
  169. owner->setEnabled (m, ! owner->isEnabled (m));
  170. }
  171. private:
  172. ModuleList& list;
  173. ModulesPanel* owner;
  174. };
  175. //==============================================================================
  176. class ModuleSettingsPanel : public PropertyPanel
  177. {
  178. public:
  179. ModuleSettingsPanel (Project& p, ModuleList& list, const String& modID)
  180. : project (p), moduleList (list), moduleID (modID)
  181. {
  182. refreshAll();
  183. }
  184. void refreshAll()
  185. {
  186. setEnabled (project.isModuleEnabled (moduleID));
  187. clear();
  188. PropertyListBuilder props;
  189. ScopedPointer<LibraryModule> module (moduleList.loadModule (moduleID));
  190. if (module != nullptr)
  191. {
  192. props.add (new ModuleInfoComponent (project, moduleList, moduleID));
  193. if (project.isModuleEnabled (moduleID))
  194. {
  195. const ModuleList::Module* m = moduleList.findModuleInfo (moduleID);
  196. if (m != nullptr && moduleList.getExtraDependenciesNeeded (project, *m).size() > 0)
  197. props.add (new MissingDependenciesComponent (project, moduleList, moduleID));
  198. }
  199. props.add (new BooleanPropertyComponent (project.shouldShowAllModuleFilesInProject (moduleID),
  200. "Add source to project", "Make module files browsable in projects"),
  201. "If this is enabled, then the entire source tree from this module will be shown inside your project, "
  202. "making it easy to browse/edit the module's classes. If disabled, then only the minimum number of files "
  203. "required to compile it will appear inside your project.");
  204. props.add (new BooleanPropertyComponent (project.shouldCopyModuleFilesLocally (moduleID),
  205. "Create local copy", "Copy the module into the project folder"),
  206. "If this is enabled, then a local copy of the entire module will be made inside your project (in the auto-generated JuceLibraryFiles folder), "
  207. "so that your project will be self-contained, and won't need to contain any references to files in other folders. "
  208. "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.");
  209. StringArray possibleValues;
  210. possibleValues.add ("(Use Default)");
  211. possibleValues.add ("Enabled");
  212. possibleValues.add ("Disabled");
  213. Array<var> mappings;
  214. mappings.add (Project::configFlagDefault);
  215. mappings.add (Project::configFlagEnabled);
  216. mappings.add (Project::configFlagDisabled);
  217. OwnedArray <Project::ConfigFlag> flags;
  218. module->getConfigFlags (project, flags);
  219. for (int i = 0; i < flags.size(); ++i)
  220. {
  221. ChoicePropertyComponent* c = new ChoicePropertyComponent (flags[i]->value, flags[i]->symbol, possibleValues, mappings);
  222. c->setTooltip (flags[i]->description);
  223. props.add (c);
  224. }
  225. }
  226. addProperties (props.components);
  227. }
  228. private:
  229. Project& project;
  230. ModuleList& moduleList;
  231. String moduleID;
  232. //==============================================================================
  233. class ModuleInfoComponent : public PropertyComponent
  234. {
  235. public:
  236. ModuleInfoComponent (Project& p, ModuleList& list, const String& modID)
  237. : PropertyComponent ("Module", 100), project (p), moduleList (list), moduleID (modID)
  238. {
  239. }
  240. void refresh() {}
  241. void paint (Graphics& g)
  242. {
  243. g.setColour (Colours::white.withAlpha (0.4f));
  244. g.fillRect (0, 0, getWidth(), getHeight() - 1);
  245. const ModuleList::Module* module = moduleList.findModuleInfo (moduleID);
  246. if (module != nullptr)
  247. {
  248. String text;
  249. text << module->name << newLine << "Version: " << module->version << newLine << newLine
  250. << module->description;
  251. GlyphArrangement ga;
  252. ga.addJustifiedText (Font (13.0f), text, 4.0f, 16.0f, getWidth() - 8.0f, Justification::topLeft);
  253. g.setColour (Colours::black);
  254. ga.draw (g);
  255. }
  256. }
  257. private:
  258. Project& project;
  259. ModuleList& moduleList;
  260. String moduleID;
  261. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModuleInfoComponent);
  262. };
  263. //==============================================================================
  264. class MissingDependenciesComponent : public PropertyComponent,
  265. public ButtonListener
  266. {
  267. public:
  268. MissingDependenciesComponent (Project& p, ModuleList& list, const String& modID)
  269. : PropertyComponent ("Dependencies", 100),
  270. project (p), moduleList (list), moduleID (modID),
  271. fixButton ("Enable Required Modules")
  272. {
  273. const ModuleList::Module* module = moduleList.findModuleInfo (moduleID);
  274. if (module != nullptr)
  275. missingDependencies = moduleList.getExtraDependenciesNeeded (project, *module);
  276. addAndMakeVisible (&fixButton);
  277. fixButton.setColour (TextButton::buttonColourId, Colours::red);
  278. fixButton.setColour (TextButton::textColourOffId, Colours::white);
  279. fixButton.addListener (this);
  280. }
  281. void refresh() {}
  282. void paint (Graphics& g)
  283. {
  284. g.setColour (Colours::white.withAlpha (0.4f));
  285. g.fillRect (0, 0, getWidth(), getHeight() - 1);
  286. String text ("This module requires the following dependencies:\n");
  287. text << missingDependencies.joinIntoString (", ");
  288. GlyphArrangement ga;
  289. ga.addJustifiedText (Font (13.0f), text, 4.0f, 16.0f, getWidth() - 8.0f, Justification::topLeft);
  290. g.setColour (Colours::red);
  291. ga.draw (g);
  292. }
  293. void buttonClicked (Button*)
  294. {
  295. bool isModuleCopiedLocally = project.shouldCopyModuleFilesLocally (moduleID).getValue();
  296. for (int i = missingDependencies.size(); --i >= 0;)
  297. project.addModule (missingDependencies[i], isModuleCopiedLocally);
  298. ModulesPanel* mp = findParentComponentOfClass<ModulesPanel>();
  299. if (mp != nullptr)
  300. mp->refresh();
  301. }
  302. void resized()
  303. {
  304. fixButton.setBounds (getWidth() - 168, getHeight() - 26, 160, 22);
  305. }
  306. private:
  307. Project& project;
  308. ModuleList& moduleList;
  309. String moduleID;
  310. StringArray missingDependencies;
  311. TextButton fixButton;
  312. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MissingDependenciesComponent);
  313. };
  314. };
  315. //==============================================================================
  316. class ModuleCopyingInfo : public Component,
  317. public ButtonListener,
  318. public Timer
  319. {
  320. public:
  321. ModuleCopyingInfo (Project& p, ModuleList& modules)
  322. : project (p), list (modules),
  323. copyModeButton ("Set Copying Mode...")
  324. {
  325. addAndMakeVisible (&copyModeButton);
  326. copyModeButton.addListener (this);
  327. startTimer (1500);
  328. }
  329. void paint (Graphics& g)
  330. {
  331. g.setFont (11.0f);
  332. g.setColour (Colours::darkred);
  333. g.drawFittedText (getName(), copyModeButton.getRight() + 10, 0,
  334. getWidth() - copyModeButton.getRight() - 16, getHeight(),
  335. Justification::centredRight, 4);
  336. }
  337. void resized()
  338. {
  339. copyModeButton.setBounds (0, getHeight() / 2 - 10, 160, 20);
  340. }
  341. void refresh()
  342. {
  343. int numCopied, numNonCopied;
  344. countCopiedModules (numCopied, numNonCopied);
  345. String newName;
  346. if (numCopied > 0 && numNonCopied > 0)
  347. newName = "Warning! Some of your modules are set to use local copies, and others are using remote references.\n"
  348. "This may create problems if some modules expect to share the same parent folder, so you may "
  349. "want to make sure that they are all either copied or not.";
  350. if (project.isAudioPluginModuleMissing())
  351. newName = "Warning! Your project is an audio plugin, but you haven't enabled the 'juce_audio_plugin_client' module!";
  352. if (newName != getName())
  353. {
  354. setName (newName);
  355. repaint();
  356. }
  357. }
  358. void countCopiedModules (int& numCopied, int& numNonCopied)
  359. {
  360. numCopied = numNonCopied = 0;
  361. for (int i = list.modules.size(); --i >= 0;)
  362. {
  363. const String moduleID (list.modules.getUnchecked(i)->uid);
  364. if (project.isModuleEnabled (moduleID))
  365. {
  366. if (project.shouldCopyModuleFilesLocally (moduleID).getValue())
  367. ++numCopied;
  368. else
  369. ++numNonCopied;
  370. }
  371. }
  372. }
  373. void buttonClicked (Button*)
  374. {
  375. PopupMenu menu;
  376. menu.addItem (1, "Enable local copying for all modules");
  377. menu.addItem (2, "Disable local copying for all modules");
  378. menu.showMenuAsync (PopupMenu::Options().withTargetComponent (&copyModeButton),
  379. ModalCallbackFunction::forComponent (copyMenuItemChosen, this));
  380. }
  381. static void copyMenuItemChosen (int resultCode, ModuleCopyingInfo* comp)
  382. {
  383. if (resultCode > 0 && comp != nullptr)
  384. comp->setCopyModeForAllModules (resultCode == 1);
  385. }
  386. void setCopyModeForAllModules (bool copyEnabled)
  387. {
  388. for (int i = list.modules.size(); --i >= 0;)
  389. {
  390. const String moduleID (list.modules.getUnchecked(i)->uid);
  391. if (project.isModuleEnabled (moduleID))
  392. project.shouldCopyModuleFilesLocally (moduleID) = copyEnabled;
  393. }
  394. refresh();
  395. }
  396. void timerCallback()
  397. {
  398. refresh();
  399. }
  400. private:
  401. Project& project;
  402. ModuleList& list;
  403. TextButton copyModeButton;
  404. };
  405. private:
  406. Project& project;
  407. ModuleList moduleList;
  408. FilenameComponent modulesLocation;
  409. Label modulesLabel;
  410. TextButton updateModulesButton;
  411. ModuleSelectionListBox moduleListBox;
  412. ModuleCopyingInfo copyingMessage;
  413. ScopedPointer<ModuleSettingsPanel> settings;
  414. };
  415. #endif