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.

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