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.

438 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. #ifndef NEWPROJECTWIZARDCOMPONENTS_H_INCLUDED
  18. #define NEWPROJECTWIZARDCOMPONENTS_H_INCLUDED
  19. class ModulesFolderPathBox : public Component,
  20. private ButtonListener,
  21. private ComboBoxListener
  22. {
  23. public:
  24. ModulesFolderPathBox (File initialFileOrDirectory)
  25. : currentPathBox ("currentPathBox"),
  26. openFolderButton (TRANS("...")),
  27. modulesLabel (String::empty, TRANS("Modules Folder") + ":")
  28. {
  29. if (initialFileOrDirectory == File::nonexistent)
  30. initialFileOrDirectory = findDefaultModulesFolder();
  31. setModulesFolder (initialFileOrDirectory);
  32. addAndMakeVisible (currentPathBox);
  33. currentPathBox.setEditableText (true);
  34. currentPathBox.addListener (this);
  35. addAndMakeVisible (openFolderButton);
  36. openFolderButton.addListener (this);
  37. openFolderButton.setTooltip (TRANS ("Select JUCE modules folder"));
  38. addAndMakeVisible (modulesLabel);
  39. modulesLabel.attachToComponent (&currentPathBox, true);
  40. }
  41. void resized() override
  42. {
  43. Rectangle<int> bounds = getLocalBounds();
  44. modulesLabel.setBounds (bounds.removeFromLeft (110));
  45. openFolderButton.setBounds (bounds.removeFromRight (40));
  46. bounds.removeFromRight (5);
  47. currentPathBox.setBounds (bounds);
  48. }
  49. static bool selectJuceFolder (File& result)
  50. {
  51. for (;;)
  52. {
  53. FileChooser fc ("Select your JUCE modules folder...",
  54. findDefaultModulesFolder(),
  55. "*");
  56. if (! fc.browseForDirectory())
  57. return false;
  58. if (isJuceModulesFolder (fc.getResult()))
  59. {
  60. result = fc.getResult();
  61. return true;
  62. }
  63. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  64. "Not a valid JUCE modules folder!",
  65. "Please select the folder containing your juce_* modules!\n\n"
  66. "This is required so that the new project can be given some essential core modules.");
  67. }
  68. }
  69. void selectJuceFolder()
  70. {
  71. File result;
  72. if (selectJuceFolder (result))
  73. setModulesFolder (result);
  74. }
  75. void setModulesFolder (const File& newFolder)
  76. {
  77. if (modulesFolder != newFolder)
  78. {
  79. modulesFolder = newFolder;
  80. currentPathBox.setText (modulesFolder.getFullPathName(), dontSendNotification);
  81. }
  82. }
  83. void buttonClicked (Button*) override
  84. {
  85. selectJuceFolder();
  86. }
  87. void comboBoxChanged (ComboBox*) override
  88. {
  89. setModulesFolder (File::getCurrentWorkingDirectory().getChildFile (currentPathBox.getText()));
  90. }
  91. File modulesFolder;
  92. private:
  93. ComboBox currentPathBox;
  94. TextButton openFolderButton;
  95. Label modulesLabel;
  96. };
  97. /** The target platforms chooser for the chosen template. */
  98. class PlatformTargetsComp : public Component,
  99. private ListBoxModel
  100. {
  101. public:
  102. PlatformTargetsComp()
  103. {
  104. setOpaque (false);
  105. const Array<ProjectExporter::ExporterTypeInfo> types (ProjectExporter::getExporterTypes());
  106. for (int i = 0; i < types.size(); ++i)
  107. {
  108. const ProjectExporter::ExporterTypeInfo& type = types.getReference (i);
  109. platforms.add (new PlatformType (ImageCache::getFromMemory (type.iconData, type.iconDataSize), type.name));
  110. }
  111. listBox.setRowHeight (35);
  112. listBox.setModel (this);
  113. listBox.setOpaque (false);
  114. listBox.setMultipleSelectionEnabled (true);
  115. listBox.setClickingTogglesRowSelection (true);
  116. listBox.setColour (ListBox::backgroundColourId, Colours::white.withAlpha (0.0f));
  117. addAndMakeVisible (listBox);
  118. selectDefaultExporterIfNoneSelected();
  119. }
  120. StringArray getSelectedPlatforms() const
  121. {
  122. StringArray list;
  123. for (int i = 0; i < platforms.size(); ++i)
  124. if (listBox.isRowSelected (i))
  125. list.add (platforms.getUnchecked(i)->name);
  126. return list;
  127. }
  128. void selectDefaultExporterIfNoneSelected()
  129. {
  130. if (listBox.getNumSelectedRows() == 0)
  131. {
  132. for (int i = platforms.size(); --i >= 0;)
  133. {
  134. if (platforms.getUnchecked(i)->name == ProjectExporter::getCurrentPlatformExporterName())
  135. {
  136. listBox.selectRow (i);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. void resized() override
  143. {
  144. listBox.setBounds (getLocalBounds());
  145. }
  146. int getNumRows() override
  147. {
  148. return platforms.size();
  149. }
  150. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  151. {
  152. if (PlatformType* platform = platforms[rowNumber])
  153. {
  154. if (rowIsSelected)
  155. g.fillAll (Colour (0x99f29000));
  156. Rectangle<float> dotSelect ((float) height, (float) height);
  157. dotSelect.reduce (12, 12);
  158. g.setColour (Colour (0x33ffffff));
  159. g.fillEllipse (dotSelect);
  160. if (rowIsSelected)
  161. {
  162. const float tx = dotSelect.getCentreX();
  163. const float ty = dotSelect.getCentreY() + 1.0f;
  164. Path tick;
  165. tick.startNewSubPath (tx - 5.0f, ty - 6.0f);
  166. tick.lineTo (tx, ty);
  167. tick.lineTo (tx + 8.0f, ty - 13.0f);
  168. g.setColour (Colours::white);
  169. g.strokePath (tick, PathStrokeType (3.0f));
  170. }
  171. g.setColour (Colours::black);
  172. g.drawImageWithin (platform->icon, 40, 0, height, height, RectanglePlacement::stretchToFit);
  173. g.drawText (platform->name, 90, 0, width, height, Justification::left);
  174. }
  175. }
  176. void selectedRowsChanged (int) override
  177. {
  178. selectDefaultExporterIfNoneSelected();
  179. }
  180. private:
  181. struct PlatformType
  182. {
  183. PlatformType (const Image& platformIcon, const String& platformName)
  184. : icon (platformIcon), name (platformName)
  185. {
  186. }
  187. Image icon;
  188. String name;
  189. };
  190. ListBox listBox;
  191. OwnedArray<PlatformType> platforms;
  192. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PlatformTargetsComp)
  193. };
  194. //==============================================================================
  195. /**
  196. The Component for project creation.
  197. Features a file browser to select project destination and
  198. a list box of platform targets to generate.
  199. */
  200. class WizardComp : public Component,
  201. private ButtonListener,
  202. private ComboBoxListener,
  203. private TextEditorListener
  204. {
  205. public:
  206. WizardComp()
  207. : platformTargets(),
  208. projectName (TRANS("Project name")),
  209. nameLabel (String::empty, TRANS("Project Name") + ":"),
  210. typeLabel (String::empty, TRANS("Project Type") + ":"),
  211. fileBrowser (FileBrowserComponent::saveMode | FileBrowserComponent::canSelectDirectories,
  212. NewProjectWizardClasses::getLastWizardFolder(), nullptr, nullptr),
  213. fileOutline (String::empty, TRANS("Project Folder") + ":"),
  214. targetsOutline (String::empty, TRANS("Target Platforms") + ":"),
  215. createButton (TRANS("Create") + "..."),
  216. cancelButton (TRANS("Cancel")),
  217. modulesPathBox (findDefaultModulesFolder())
  218. {
  219. setOpaque (false);
  220. addChildAndSetID (&projectName, "projectName");
  221. projectName.setText ("NewProject");
  222. projectName.setBounds ("120, 34, parent.width / 2 - 10, top + 22");
  223. nameLabel.attachToComponent (&projectName, true);
  224. projectName.addListener (this);
  225. addChildAndSetID (&projectType, "projectType");
  226. projectType.addItemList (getWizardNames(), 1);
  227. projectType.setSelectedId (1, dontSendNotification);
  228. projectType.setBounds ("120, projectName.bottom + 4, projectName.right, top + 22");
  229. typeLabel.attachToComponent (&projectType, true);
  230. projectType.addListener (this);
  231. addChildAndSetID (&fileOutline, "fileOutline");
  232. fileOutline.setColour (GroupComponent::outlineColourId, Colours::black.withAlpha (0.2f));
  233. fileOutline.setTextLabelPosition (Justification::centred);
  234. fileOutline.setBounds ("30, projectType.bottom + 20, projectType.right, parent.height - 30");
  235. addChildAndSetID (&targetsOutline, "targetsOutline");
  236. targetsOutline.setColour (GroupComponent::outlineColourId, Colours::black.withAlpha (0.2f));
  237. targetsOutline.setTextLabelPosition (Justification::centred);
  238. targetsOutline.setBounds ("fileOutline.right + 20, projectType.bottom + 20, parent.width - 30, parent.height - 70");
  239. addChildAndSetID (&platformTargets, "platformTargets");
  240. platformTargets.setBounds ("targetsOutline.left + 15, projectType.bottom + 45, parent.width - 40, parent.height - 90");
  241. addChildAndSetID (&fileBrowser, "fileBrowser");
  242. fileBrowser.setBounds ("fileOutline.left + 10, fileOutline.top + 20, fileOutline.right - 10, fileOutline.bottom - 32");
  243. fileBrowser.setFilenameBoxLabel ("Folder:");
  244. addChildAndSetID (&createButton, "createButton");
  245. createButton.setBounds ("right - 130, bottom - 34, parent.width - 30, parent.height - 30");
  246. createButton.addListener (this);
  247. addChildAndSetID (&cancelButton, "cancelButton");
  248. cancelButton.addShortcut (KeyPress (KeyPress::escapeKey));
  249. cancelButton.setBounds ("right - 130, createButton.top, createButton.left - 10, createButton.bottom");
  250. cancelButton.addListener (this);
  251. addChildAndSetID (&modulesPathBox, "modulesPathBox");
  252. modulesPathBox.setBounds ("targetsOutline.left, targetsOutline.top - 45, targetsOutline.right, targetsOutline.top - 20");
  253. updateCustomItems();
  254. updateCreateButton();
  255. }
  256. void paint (Graphics& g) override
  257. {
  258. Rectangle<int> rect = getLocalBounds().reduced (10, 10);
  259. g.setColour (Colours::white.withAlpha (0.3f));
  260. g.fillRect (rect);
  261. g.fillRect (rect.reduced (10, 10));
  262. }
  263. void buttonClicked (Button* b) override
  264. {
  265. if (b == &createButton)
  266. {
  267. createProject();
  268. }
  269. else if (b == &cancelButton)
  270. {
  271. returnToTemplatesPage();
  272. }
  273. }
  274. void returnToTemplatesPage()
  275. {
  276. if (SlidingPanelComponent* parent = findParentComponentOfClass<SlidingPanelComponent>())
  277. {
  278. if (parent->getNumTabs() > 0)
  279. parent->goToTab (parent->getCurrentTabIndex() - 1);
  280. }
  281. else
  282. {
  283. jassertfalse;
  284. }
  285. }
  286. void createProject()
  287. {
  288. MainWindow* mw = Component::findParentComponentOfClass<MainWindow>();
  289. jassert (mw != nullptr);
  290. ScopedPointer<NewProjectWizardClasses::NewProjectWizard> wizard (createWizard());
  291. if (wizard != nullptr)
  292. {
  293. Result result (wizard->processResultsFromSetupItems (*this));
  294. if (result.failed())
  295. {
  296. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  297. TRANS("Create Project"),
  298. result.getErrorMessage());
  299. return;
  300. }
  301. wizard->modulesFolder = modulesPathBox.modulesFolder;
  302. if (! isJuceModulesFolder (wizard->modulesFolder))
  303. if (! wizard->selectJuceFolder())
  304. return;
  305. ScopedPointer<Project> project (wizard->runWizard (*this, projectName.getText(),
  306. fileBrowser.getSelectedFile (0)));
  307. if (project != nullptr)
  308. mw->setProject (project.release());
  309. }
  310. }
  311. void updateCustomItems()
  312. {
  313. customItems.clear();
  314. ScopedPointer<NewProjectWizardClasses::NewProjectWizard> wizard (createWizard());
  315. if (wizard != nullptr)
  316. wizard->addSetupItems (*this, customItems);
  317. }
  318. void comboBoxChanged (ComboBox*) override
  319. {
  320. updateCustomItems();
  321. }
  322. void textEditorTextChanged (TextEditor&) override
  323. {
  324. updateCreateButton();
  325. fileBrowser.setFileName (File::createLegalFileName (projectName.getText()));
  326. }
  327. ComboBox projectType;
  328. PlatformTargetsComp platformTargets;
  329. private:
  330. TextEditor projectName;
  331. Label nameLabel, typeLabel;
  332. FileBrowserComponent fileBrowser;
  333. GroupComponent fileOutline;
  334. GroupComponent targetsOutline;
  335. TextButton createButton, cancelButton;
  336. OwnedArray<Component> customItems;
  337. ModulesFolderPathBox modulesPathBox;
  338. NewProjectWizardClasses::NewProjectWizard* createWizard()
  339. {
  340. return createWizardType (projectType.getSelectedItemIndex());
  341. }
  342. void updateCreateButton()
  343. {
  344. createButton.setEnabled (projectName.getText().trim().isNotEmpty());
  345. }
  346. };
  347. #endif // NEWPROJECTWIZARDCOMPONENTS_H_INCLUDED