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.

272 lines
9.2KB

  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 JUCER_NEWPROJECTWIZARD_H_INCLUDED
  18. #define JUCER_NEWPROJECTWIZARD_H_INCLUDED
  19. //==============================================================================
  20. static void createFileCreationOptionComboBox (Component& setupComp,
  21. OwnedArray<Component>& itemsCreated,
  22. const StringArray& fileOptions)
  23. {
  24. ComboBox* c = new ComboBox();
  25. itemsCreated.add (c);
  26. setupComp.addChildAndSetID (c, "filesToCreate");
  27. c->addItemList (fileOptions, 1);
  28. c->setSelectedId (1, dontSendNotification);
  29. Label* l = new Label (String::empty, TRANS("Files to Auto-Generate") + ":");
  30. l->attachToComponent (c, true);
  31. itemsCreated.add (l);
  32. c->setBounds ("parent.width / 2 + 160, 30, parent.width - 30, top + 22");
  33. }
  34. static int getFileCreationComboResult (WizardComp& setupComp)
  35. {
  36. if (ComboBox* cb = dynamic_cast<ComboBox*> (setupComp.findChildWithID ("filesToCreate")))
  37. return cb->getSelectedItemIndex();
  38. jassertfalse;
  39. return 0;
  40. }
  41. static void setExecutableNameForAllTargets (Project& project, const String& exeName)
  42. {
  43. for (Project::ExporterIterator exporter (project); exporter.next();)
  44. for (ProjectExporter::ConfigIterator config (*exporter); config.next();)
  45. config->getTargetBinaryName() = exeName;
  46. }
  47. static Project::Item createSourceGroup (Project& project)
  48. {
  49. return project.getMainGroup().addNewSubGroup ("Source", 0);
  50. }
  51. static File& getLastWizardFolder()
  52. {
  53. #if JUCE_WINDOWS
  54. static File lastFolder (File::getSpecialLocation (File::userDocumentsDirectory));
  55. #else
  56. static File lastFolder (File::getSpecialLocation (File::userHomeDirectory));
  57. #endif
  58. return lastFolder;
  59. }
  60. static bool isJuceModulesFolder (const File& f)
  61. {
  62. return f.isDirectory()
  63. && f.getChildFile ("juce_core").isDirectory();
  64. }
  65. static File findDefaultModulesFolder (bool mustContainJuceCoreModule = true)
  66. {
  67. const MainWindowList& windows = IntrojucerApp::getApp().mainWindowList;
  68. for (int i = windows.windows.size(); --i >= 0;)
  69. {
  70. if (Project* p = windows.windows.getUnchecked (i)->getProject())
  71. {
  72. const File f (EnabledModuleList::findDefaultModulesFolder (*p));
  73. if (isJuceModulesFolder (f) || (f.isDirectory() && ! mustContainJuceCoreModule))
  74. return f;
  75. }
  76. }
  77. if (mustContainJuceCoreModule)
  78. return findDefaultModulesFolder (false);
  79. return File::nonexistent;
  80. }
  81. //==============================================================================
  82. struct NewProjectWizard
  83. {
  84. NewProjectWizard() {}
  85. virtual ~NewProjectWizard() {}
  86. //==============================================================================
  87. virtual String getName() = 0;
  88. virtual String getDescription() = 0;
  89. virtual void addSetupItems (Component&, OwnedArray<Component>&) {}
  90. virtual Result processResultsFromSetupItems (WizardComp&) { return Result::ok(); }
  91. virtual bool initialiseProject (Project& project) = 0;
  92. virtual StringArray getDefaultModules()
  93. {
  94. static const char* mods[] =
  95. {
  96. "juce_core",
  97. "juce_events",
  98. "juce_graphics",
  99. "juce_data_structures",
  100. "juce_gui_basics",
  101. "juce_gui_extra",
  102. "juce_cryptography",
  103. "juce_video",
  104. "juce_opengl",
  105. "juce_audio_basics",
  106. "juce_audio_devices",
  107. "juce_audio_formats",
  108. "juce_audio_processors",
  109. nullptr
  110. };
  111. return StringArray (mods);
  112. }
  113. String appTitle;
  114. File targetFolder, projectFile, modulesFolder;
  115. WizardComp* ownerWizardComp;
  116. StringArray failedFiles;
  117. //==============================================================================
  118. Project* runWizard (WizardComp& wc,
  119. const String& projectName,
  120. const File& target)
  121. {
  122. ownerWizardComp = &wc;
  123. appTitle = projectName;
  124. targetFolder = target;
  125. if (! targetFolder.exists())
  126. {
  127. if (! targetFolder.createDirectory())
  128. failedFiles.add (targetFolder.getFullPathName());
  129. }
  130. else if (FileHelpers::containsAnyNonHiddenFiles (targetFolder))
  131. {
  132. if (! AlertWindow::showOkCancelBox (AlertWindow::InfoIcon,
  133. TRANS("New Juce Project"),
  134. TRANS("The folder you chose isn't empty - are you sure you want to create the project there?")
  135. + "\n\n"
  136. + TRANS("Any existing files with the same names may be overwritten by the new files.")))
  137. return nullptr;
  138. }
  139. projectFile = targetFolder.getChildFile (File::createLegalFileName (appTitle))
  140. .withFileExtension (Project::projectFileExtension);
  141. ScopedPointer<Project> project (new Project (projectFile));
  142. if (failedFiles.size() == 0)
  143. {
  144. project->setFile (projectFile);
  145. project->setTitle (appTitle);
  146. project->getBundleIdentifier() = project->getDefaultBundleIdentifier();
  147. if (! initialiseProject (*project))
  148. return nullptr;
  149. addExporters (*project, wc);
  150. addDefaultModules (*project);
  151. if (project->save (false, true) != FileBasedDocument::savedOk)
  152. return nullptr;
  153. project->setChangedFlag (false);
  154. }
  155. if (failedFiles.size() > 0)
  156. {
  157. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  158. TRANS("Errors in Creating Project!"),
  159. TRANS("The following files couldn't be written:")
  160. + "\n\n"
  161. + failedFiles.joinIntoString ("\n", 0, 10));
  162. return nullptr;
  163. }
  164. return project.release();
  165. }
  166. bool selectJuceFolder()
  167. {
  168. for (;;)
  169. {
  170. FileChooser fc ("Select your JUCE modules folder...",
  171. findDefaultModulesFolder(),
  172. "*");
  173. if (! fc.browseForDirectory())
  174. return false;
  175. if (isJuceModulesFolder (fc.getResult()))
  176. {
  177. modulesFolder = fc.getResult();
  178. return true;
  179. }
  180. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  181. "Not a valid JUCE modules folder!",
  182. "Please select the folder containing your juce_* modules!\n\n"
  183. "This is required so that the new project can be given some essential core modules.");
  184. }
  185. }
  186. //==============================================================================
  187. File getSourceFilesFolder() const
  188. {
  189. return projectFile.getSiblingFile ("Source");
  190. }
  191. void createSourceFolder()
  192. {
  193. if (! getSourceFilesFolder().createDirectory())
  194. failedFiles.add (getSourceFilesFolder().getFullPathName());
  195. }
  196. void addDefaultModules (Project& project)
  197. {
  198. StringArray mods (getDefaultModules());
  199. ModuleList list;
  200. list.addAllModulesInFolder (modulesFolder);
  201. for (int i = 0; i < mods.size(); ++i)
  202. if (const ModuleDescription* info = list.getModuleWithID (mods[i]))
  203. project.getModules().addModule (info->manifestFile, true);
  204. }
  205. void addExporters (Project& project, WizardComp& wizardComp)
  206. {
  207. StringArray types (wizardComp.platformTargets.getSelectedPlatforms());
  208. for (int i = 0; i < types.size(); ++i)
  209. project.addNewExporter (types[i]);
  210. if (project.getNumExporters() == 0)
  211. project.createExporterForCurrentPlatform();
  212. }
  213. };
  214. #endif // JUCER_NEWPROJECTWIZARD_H_INCLUDED