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.

518 lines
18KB

  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 ModulesFolderPathBox : public Component,
  22. private ComboBox::Listener
  23. {
  24. public:
  25. ModulesFolderPathBox (File initialFileOrDirectory)
  26. : currentPathBox ("currentPathBox"),
  27. openFolderButton (TRANS("...")),
  28. modulesLabel (String(), TRANS("Modules Folder") + ":"),
  29. useGlobalPathsToggle ("Use global module path")
  30. {
  31. if (initialFileOrDirectory == File())
  32. initialFileOrDirectory = EnabledModuleList::findGlobalModulesFolder();
  33. setModulesFolder (initialFileOrDirectory);
  34. addAndMakeVisible (currentPathBox);
  35. currentPathBox.setEditableText (true);
  36. currentPathBox.addListener (this);
  37. addAndMakeVisible (openFolderButton);
  38. openFolderButton.setTooltip (TRANS ("Select JUCE modules folder"));
  39. openFolderButton.onClick = [this] { selectJuceFolder(); };
  40. addAndMakeVisible (modulesLabel);
  41. modulesLabel.attachToComponent (&currentPathBox, true);
  42. addAndMakeVisible (useGlobalPathsToggle);
  43. useGlobalPathsToggle.setToggleState (true, sendNotification);
  44. useGlobalPathsToggle.onClick = [this]
  45. {
  46. isUsingGlobalPaths = useGlobalPathsToggle.getToggleState();
  47. currentPathBox.setEnabled (! isUsingGlobalPaths);
  48. openFolderButton.setEnabled (! isUsingGlobalPaths);
  49. modulesLabel.setEnabled (! isUsingGlobalPaths);
  50. };
  51. }
  52. void resized() override
  53. {
  54. auto b = getLocalBounds();
  55. auto topSlice = b.removeFromTop (b.getHeight() / 2);
  56. openFolderButton.setBounds (topSlice.removeFromRight (30));
  57. modulesLabel.setBounds (topSlice.removeFromLeft (110));
  58. currentPathBox.setBounds (topSlice);
  59. b.removeFromTop (5);
  60. useGlobalPathsToggle.setBounds (b.translated (20, 0));
  61. }
  62. static bool selectJuceFolder (File& result)
  63. {
  64. for (;;)
  65. {
  66. FileChooser fc ("Select your JUCE modules folder...",
  67. EnabledModuleList::findGlobalModulesFolder(),
  68. "*");
  69. if (! fc.browseForDirectory())
  70. return false;
  71. if (isJuceModulesFolder (fc.getResult()))
  72. {
  73. result = fc.getResult();
  74. return true;
  75. }
  76. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  77. "Not a valid JUCE modules folder!",
  78. "Please select the folder containing your juce_* modules!\n\n"
  79. "This is required so that the new project can be given some essential core modules.");
  80. }
  81. }
  82. void selectJuceFolder()
  83. {
  84. File result;
  85. if (selectJuceFolder (result))
  86. setModulesFolder (result);
  87. }
  88. void setModulesFolder (const File& newFolder)
  89. {
  90. if (modulesFolder != newFolder)
  91. {
  92. modulesFolder = newFolder;
  93. currentPathBox.setText (modulesFolder.getFullPathName(), dontSendNotification);
  94. }
  95. }
  96. void comboBoxChanged (ComboBox*) override
  97. {
  98. setModulesFolder (File::getCurrentWorkingDirectory().getChildFile (currentPathBox.getText()));
  99. }
  100. File modulesFolder;
  101. bool isUsingGlobalPaths;
  102. private:
  103. ComboBox currentPathBox;
  104. TextButton openFolderButton;
  105. Label modulesLabel;
  106. ToggleButton useGlobalPathsToggle;
  107. };
  108. /** The target platforms chooser for the chosen template. */
  109. class PlatformTargetsComp : public Component,
  110. private ListBoxModel
  111. {
  112. public:
  113. PlatformTargetsComp()
  114. {
  115. setOpaque (false);
  116. const Array<ProjectExporter::ExporterTypeInfo> types (ProjectExporter::getExporterTypes());
  117. for (auto& type : types)
  118. {
  119. platforms.add (new PlatformType { type.getIcon(), type.name });
  120. addAndMakeVisible (toggles.add (new ToggleButton (String())));
  121. }
  122. listBox.setRowHeight (30);
  123. listBox.setModel (this);
  124. listBox.setOpaque (false);
  125. listBox.setMultipleSelectionEnabled (true);
  126. listBox.setClickingTogglesRowSelection (true);
  127. listBox.setColour (ListBox::backgroundColourId, Colours::transparentBlack);
  128. addAndMakeVisible (listBox);
  129. selectDefaultExporterIfNoneSelected();
  130. }
  131. StringArray getSelectedPlatforms() const
  132. {
  133. StringArray list;
  134. for (int i = 0; i < platforms.size(); ++i)
  135. if (listBox.isRowSelected (i))
  136. list.add (platforms.getUnchecked(i)->name);
  137. return list;
  138. }
  139. void selectDefaultExporterIfNoneSelected()
  140. {
  141. if (listBox.getNumSelectedRows() == 0)
  142. {
  143. for (int i = platforms.size(); --i >= 0;)
  144. {
  145. if (platforms.getUnchecked(i)->name == ProjectExporter::getCurrentPlatformExporterName())
  146. {
  147. listBox.selectRow (i);
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. void resized() override
  154. {
  155. listBox.setBounds (getLocalBounds());
  156. }
  157. int getNumRows() override
  158. {
  159. return platforms.size();
  160. }
  161. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  162. {
  163. ignoreUnused (width);
  164. if (auto* platform = platforms[rowNumber])
  165. {
  166. auto bounds = getLocalBounds().withHeight (height).withTrimmedBottom (1);
  167. g.setColour (findColour (rowNumber % 2 == 0 ? widgetBackgroundColourId
  168. : secondaryWidgetBackgroundColourId));
  169. g.fillRect (bounds);
  170. bounds.removeFromLeft (10);
  171. auto toggleBounds = bounds.removeFromLeft (height);
  172. drawToggle (g, toggleBounds, rowIsSelected);
  173. auto iconBounds = bounds.removeFromLeft (height).reduced (5);
  174. g.drawImageWithin (platform->icon, iconBounds.getX(), iconBounds.getY(), iconBounds.getWidth(),
  175. iconBounds.getHeight(), RectanglePlacement::fillDestination);
  176. bounds.removeFromLeft (10);
  177. g.setColour (findColour (widgetTextColourId));
  178. g.drawFittedText (platform->name, bounds, Justification::centredLeft, 1);
  179. }
  180. }
  181. void selectedRowsChanged (int) override
  182. {
  183. selectDefaultExporterIfNoneSelected();
  184. }
  185. private:
  186. struct PlatformType
  187. {
  188. Image icon;
  189. String name;
  190. };
  191. void drawToggle (Graphics& g, Rectangle<int> bounds, bool isToggled)
  192. {
  193. auto sideLength = jmin (bounds.getWidth(), bounds.getHeight());
  194. bounds = bounds.withSizeKeepingCentre (sideLength, sideLength).reduced (4);
  195. g.setColour (findColour (ToggleButton::tickDisabledColourId));
  196. g.drawRoundedRectangle (bounds.toFloat(), 2.0f, 1.0f);
  197. if (isToggled)
  198. {
  199. g.setColour (findColour (ToggleButton::tickColourId));
  200. const auto tick = getTickShape (0.75f);
  201. g.fillPath (tick, tick.getTransformToScaleToFit (bounds.reduced (4, 5).toFloat(), false));
  202. }
  203. }
  204. Path getTickShape (float height)
  205. {
  206. static const unsigned char pathData[] = { 110,109,32,210,202,64,126,183,148,64,108,39,244,247,64,245,76,124,64,108,178,131,27,65,246,76,252,64,108,175,242,4,65,246,76,252,
  207. 64,108,236,5,68,65,0,0,160,180,108,240,150,90,65,21,136,52,63,108,48,59,16,65,0,0,32,65,108,32,210,202,64,126,183,148,64, 99,101,0,0 };
  208. Path path;
  209. path.loadPathFromData (pathData, sizeof (pathData));
  210. path.scaleToFit (0, 0, height * 2.0f, height, true);
  211. return path;
  212. }
  213. ListBox listBox;
  214. OwnedArray<PlatformType> platforms;
  215. OwnedArray<ToggleButton> toggles;
  216. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PlatformTargetsComp)
  217. };
  218. //==============================================================================
  219. /**
  220. The Component for project creation.
  221. Features a file browser to select project destination and
  222. a list box of platform targets to generate.
  223. */
  224. class WizardComp : public Component,
  225. private ComboBox::Listener,
  226. private TextEditor::Listener,
  227. private FileBrowserListener
  228. {
  229. public:
  230. WizardComp()
  231. : platformTargets(),
  232. projectName (TRANS("Project name")),
  233. modulesPathBox (EnabledModuleList::findGlobalModulesFolder())
  234. {
  235. setOpaque (false);
  236. addChildAndSetID (&projectName, "projectName");
  237. projectName.setText ("NewProject");
  238. nameLabel.attachToComponent (&projectName, true);
  239. projectName.addListener (this);
  240. addChildAndSetID (&projectType, "projectType");
  241. projectType.addItemList (getWizardNames(), 1);
  242. projectType.setSelectedId (1, dontSendNotification);
  243. typeLabel.attachToComponent (&projectType, true);
  244. projectType.addListener (this);
  245. addChildAndSetID (&fileOutline, "fileOutline");
  246. fileOutline.setColour (GroupComponent::outlineColourId, Colours::black.withAlpha (0.2f));
  247. fileOutline.setTextLabelPosition (Justification::centred);
  248. addChildAndSetID (&targetsOutline, "targetsOutline");
  249. targetsOutline.setColour (GroupComponent::outlineColourId, Colours::black.withAlpha (0.2f));
  250. targetsOutline.setTextLabelPosition (Justification::centred);
  251. addChildAndSetID (&platformTargets, "platformTargets");
  252. addChildAndSetID (&fileBrowser, "fileBrowser");
  253. fileBrowser.setFilenameBoxLabel ("Folder:");
  254. fileBrowser.setFileName (File::createLegalFileName (projectName.getText()));
  255. fileBrowser.addListener (this);
  256. addChildAndSetID (&createButton, "createButton");
  257. createButton.onClick = [this] { createProject(); };
  258. addChildAndSetID (&cancelButton, "cancelButton");
  259. cancelButton.addShortcut (KeyPress (KeyPress::escapeKey));
  260. cancelButton.onClick = [this] { returnToTemplatesPage(); };
  261. addChildAndSetID (&modulesPathBox, "modulesPathBox");
  262. addChildAndSetID (&filesToCreate, "filesToCreate");
  263. filesToCreateLabel.attachToComponent (&filesToCreate, true);
  264. updateFileCreationTypes();
  265. updateCreateButton();
  266. lookAndFeelChanged();
  267. }
  268. void paint (Graphics& g) override
  269. {
  270. g.fillAll (findColour (backgroundColourId));
  271. }
  272. void resized() override
  273. {
  274. auto r = getLocalBounds();
  275. auto left = r.removeFromLeft (getWidth() / 2).reduced (15);
  276. auto right = r.reduced (15);
  277. projectName.setBounds (left.removeFromTop (22).withTrimmedLeft (120));
  278. left.removeFromTop (20);
  279. projectType.setBounds (left.removeFromTop (22).withTrimmedLeft (120));
  280. left.removeFromTop (20);
  281. fileOutline.setBounds (left);
  282. fileBrowser.setBounds (left.reduced (25));
  283. auto buttons = right.removeFromBottom (30);
  284. right.removeFromBottom (10);
  285. createButton.setBounds (buttons.removeFromRight (130));
  286. buttons.removeFromRight (10);
  287. cancelButton.setBounds (buttons.removeFromRight (130));
  288. filesToCreate.setBounds (right.removeFromTop (22).withTrimmedLeft (150));
  289. right.removeFromTop (20);
  290. modulesPathBox.setBounds (right.removeFromTop (50));
  291. right.removeFromTop (20);
  292. targetsOutline.setBounds (right);
  293. platformTargets.setBounds (right.reduced (25));
  294. }
  295. void returnToTemplatesPage()
  296. {
  297. if (auto* parent = findParentComponentOfClass<SlidingPanelComponent>())
  298. {
  299. if (parent->getNumTabs() > 0)
  300. parent->goToTab (parent->getCurrentTabIndex() - 1);
  301. }
  302. else
  303. {
  304. jassertfalse;
  305. }
  306. }
  307. void createProject()
  308. {
  309. auto* mw = Component::findParentComponentOfClass<MainWindow>();
  310. jassert (mw != nullptr);
  311. if (ScopedPointer<NewProjectWizardClasses::NewProjectWizard> wizard = createWizard())
  312. {
  313. Result result (wizard->processResultsFromSetupItems (*this));
  314. if (result.failed())
  315. {
  316. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  317. TRANS("Create Project"),
  318. result.getErrorMessage());
  319. return;
  320. }
  321. wizard->modulesFolder = modulesPathBox.isUsingGlobalPaths ? File (getAppSettings().getStoredPath (Ids::defaultJuceModulePath).toString())
  322. : modulesPathBox.modulesFolder;
  323. if (! isJuceModulesFolder (wizard->modulesFolder))
  324. {
  325. if (modulesPathBox.isUsingGlobalPaths)
  326. AlertWindow::showMessageBox (AlertWindow::AlertIconType::WarningIcon, "Invalid Global Path",
  327. "Your global JUCE module search path is invalid. Please select the folder containing your JUCE modules "
  328. "to set as the default path.");
  329. if (! wizard->selectJuceFolder())
  330. return;
  331. if (modulesPathBox.isUsingGlobalPaths)
  332. getAppSettings().getStoredPath (Ids::defaultJuceModulePath).setValue (wizard->modulesFolder.getFullPathName());
  333. }
  334. auto projectDir = fileBrowser.getSelectedFile (0);
  335. if (ScopedPointer<Project> project = wizard->runWizard (*this, projectName.getText(),
  336. projectDir,
  337. modulesPathBox.isUsingGlobalPaths))
  338. {
  339. mw->setProject (project.release());
  340. getAppSettings().lastWizardFolder = projectDir.getParentDirectory();
  341. }
  342. }
  343. }
  344. void updateFileCreationTypes()
  345. {
  346. StringArray items;
  347. if (ScopedPointer<NewProjectWizardClasses::NewProjectWizard> wizard = createWizard())
  348. items = wizard->getFileCreationOptions();
  349. filesToCreate.clear();
  350. filesToCreate.addItemList (items, 1);
  351. filesToCreate.setSelectedId (1, dontSendNotification);
  352. }
  353. void comboBoxChanged (ComboBox*) override
  354. {
  355. updateFileCreationTypes();
  356. }
  357. void textEditorTextChanged (TextEditor&) override
  358. {
  359. updateCreateButton();
  360. fileBrowser.setFileName (File::createLegalFileName (projectName.getText()));
  361. }
  362. void selectionChanged() override {}
  363. void fileClicked (const File&, const MouseEvent&) override {}
  364. void fileDoubleClicked (const File&) override {}
  365. void browserRootChanged (const File&) override
  366. {
  367. fileBrowser.setFileName (File::createLegalFileName (projectName.getText()));
  368. }
  369. int getFileCreationComboID() const
  370. {
  371. return filesToCreate.getSelectedItemIndex();
  372. }
  373. ComboBox projectType, filesToCreate;
  374. PlatformTargetsComp platformTargets;
  375. private:
  376. TextEditor projectName;
  377. Label nameLabel { {}, TRANS("Project Name") + ":" };
  378. Label typeLabel { {}, TRANS("Project Type") + ":" };
  379. Label filesToCreateLabel { {}, TRANS("Files to Auto-Generate") + ":" };
  380. FileBrowserComponent fileBrowser { FileBrowserComponent::saveMode
  381. | FileBrowserComponent::canSelectDirectories
  382. | FileBrowserComponent::doNotClearFileNameOnRootChange,
  383. NewProjectWizardClasses::getLastWizardFolder(), nullptr, nullptr };
  384. GroupComponent fileOutline { {}, TRANS("Project Folder") + ":" };
  385. GroupComponent targetsOutline { {}, TRANS("Target Platforms") + ":" };
  386. TextButton createButton { TRANS("Create") + "..." };
  387. TextButton cancelButton { TRANS("Cancel") };
  388. ModulesFolderPathBox modulesPathBox;
  389. ScopedPointer<NewProjectWizardClasses::NewProjectWizard> createWizard()
  390. {
  391. return createWizardType (projectType.getSelectedItemIndex());
  392. }
  393. void updateCreateButton()
  394. {
  395. createButton.setEnabled (projectName.getText().trim().isNotEmpty());
  396. }
  397. void lookAndFeelChanged() override
  398. {
  399. projectName.setColour (TextEditor::backgroundColourId, findColour (backgroundColourId));
  400. projectName.setColour (TextEditor::textColourId, findColour (defaultTextColourId));
  401. projectName.setColour (TextEditor::outlineColourId, findColour (defaultTextColourId));
  402. projectName.applyFontToAllText (projectName.getFont());
  403. fileBrowser.resized();
  404. }
  405. };