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.

495 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. #include "jucer_NewProjectWizard.h"
  19. //==============================================================================
  20. class GUIAppWizard : public NewProjectWizard
  21. {
  22. public:
  23. GUIAppWizard() {}
  24. ~GUIAppWizard() {}
  25. const String getName() { return "GUI Application"; }
  26. const String getDescription() { return "Creates a standard application"; }
  27. void addItemsToAlertWindow (AlertWindow& aw)
  28. {
  29. const char* fileOptions[] = { "Create a Main.cpp file",
  30. "Create a Main.cpp file and a basic window",
  31. "Don't create any files", 0 };
  32. aw.addComboBox ("files", StringArray (fileOptions), "Files to Auto-Generate");
  33. }
  34. const String processResultsFromAlertWindow (AlertWindow& aw)
  35. {
  36. createMainCpp = createWindow = false;
  37. switch (aw.getComboBoxComponent ("files")->getSelectedItemIndex())
  38. {
  39. case 0: createMainCpp = true; break;
  40. case 1: createMainCpp = createWindow = true; break;
  41. case 2: break;
  42. default: jassertfalse; break;
  43. }
  44. return String::empty;
  45. }
  46. bool initialiseProject (Project& project)
  47. {
  48. if (! getSourceFilesFolder().createDirectory())
  49. failedFiles.add (getSourceFilesFolder().getFullPathName());
  50. File mainCppFile = getSourceFilesFolder().getChildFile ("Main.cpp");
  51. File mainWindowCpp = getSourceFilesFolder().getChildFile ("MainWindow.cpp");
  52. File mainWindowH = mainWindowCpp.withFileExtension (".h");
  53. String windowClassName = "MainAppWindow";
  54. project.getProjectType() = Project::application;
  55. Project::Item group (project.createNewGroup());
  56. project.getMainGroup().addChild (group, 0);
  57. group.getName() = "Source";
  58. for (int i = project.getNumConfigurations(); --i >= 0;)
  59. project.getConfiguration(i).getTargetBinaryName() = File::createLegalFileName (appTitle);
  60. String appHeaders (CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), mainCppFile));
  61. String initCode, shutdownCode, anotherInstanceStartedCode, privateMembers, memberInitialisers;
  62. if (createWindow)
  63. {
  64. appHeaders << newLine << CodeHelpers::createIncludeStatement (mainWindowH, mainCppFile);
  65. initCode = "mainWindow = new " + windowClassName + "();";
  66. shutdownCode = "mainWindow = 0;";
  67. privateMembers = "ScopedPointer <" + windowClassName + "> mainWindow;";
  68. String windowH = project.getFileTemplate ("jucer_WindowTemplate_h")
  69. .replace ("INCLUDES", CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), mainWindowH), false)
  70. .replace ("WINDOWCLASS", windowClassName, false)
  71. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (mainWindowH), false);
  72. String windowCpp = project.getFileTemplate ("jucer_WindowTemplate_cpp")
  73. .replace ("INCLUDES", CodeHelpers::createIncludeStatement (mainWindowH, mainWindowCpp), false)
  74. .replace ("WINDOWCLASS", windowClassName, false);
  75. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainWindowH, windowH))
  76. failedFiles.add (mainWindowH.getFullPathName());
  77. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainWindowCpp, windowCpp))
  78. failedFiles.add (mainWindowCpp.getFullPathName());
  79. group.addFile (mainWindowCpp, -1);
  80. group.addFile (mainWindowH, -1);
  81. }
  82. if (createMainCpp)
  83. {
  84. String mainCpp = project.getFileTemplate ("jucer_MainTemplate_cpp")
  85. .replace ("APPHEADERS", appHeaders, false)
  86. .replace ("APPCLASSNAME", CodeHelpers::makeValidIdentifier (appTitle + "Application", false, true, false), false)
  87. .replace ("MEMBERINITIALISERS", memberInitialisers, false)
  88. .replace ("APPINITCODE", initCode, false)
  89. .replace ("APPSHUTDOWNCODE", shutdownCode, false)
  90. .replace ("APPNAME", CodeHelpers::addEscapeChars (appTitle), false)
  91. .replace ("APPVERSION", "1.0", false)
  92. .replace ("ALLOWMORETHANONEINSTANCE", "true", false)
  93. .replace ("ANOTHERINSTANCECODE", anotherInstanceStartedCode, false)
  94. .replace ("PRIVATEMEMBERS", privateMembers, false);
  95. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainCppFile, mainCpp))
  96. failedFiles.add (mainCppFile.getFullPathName());
  97. group.addFile (mainCppFile, -1);
  98. }
  99. return true;
  100. }
  101. private:
  102. bool createMainCpp, createWindow;
  103. };
  104. //==============================================================================
  105. class ConsoleAppWizard : public NewProjectWizard
  106. {
  107. public:
  108. ConsoleAppWizard() {}
  109. ~ConsoleAppWizard() {}
  110. const String getName() { return "Console Application"; }
  111. const String getDescription() { return "Creates a command-line application with no GUI features"; }
  112. void addItemsToAlertWindow (AlertWindow& aw)
  113. {
  114. const char* fileOptions[] = { "Create a Main.cpp file",
  115. "Don't create any files", 0 };
  116. aw.addComboBox ("files", StringArray (fileOptions), "Files to Auto-Generate");
  117. }
  118. const String processResultsFromAlertWindow (AlertWindow& aw)
  119. {
  120. createMainCpp = false;
  121. switch (aw.getComboBoxComponent ("files")->getSelectedItemIndex())
  122. {
  123. case 0: createMainCpp = true; break;
  124. case 1: break;
  125. default: jassertfalse; break;
  126. }
  127. return String::empty;
  128. }
  129. bool initialiseProject (Project& project)
  130. {
  131. if (! getSourceFilesFolder().createDirectory())
  132. failedFiles.add (getSourceFilesFolder().getFullPathName());
  133. File mainCppFile = getSourceFilesFolder().getChildFile ("Main.cpp");
  134. project.getProjectType() = Project::commandLineApp;
  135. Project::Item group (project.createNewGroup());
  136. project.getMainGroup().addChild (group, 0);
  137. group.getName() = "Source";
  138. for (int i = project.getNumConfigurations(); --i >= 0;)
  139. project.getConfiguration(i).getTargetBinaryName() = File::createLegalFileName (appTitle);
  140. if (createMainCpp)
  141. {
  142. String appHeaders (CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), mainCppFile));
  143. String mainCpp = project.getFileTemplate ("jucer_MainConsoleAppTemplate_cpp")
  144. .replace ("APPHEADERS", appHeaders, false);
  145. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainCppFile, mainCpp))
  146. failedFiles.add (mainCppFile.getFullPathName());
  147. group.addFile (mainCppFile, -1);
  148. }
  149. return true;
  150. }
  151. private:
  152. bool createMainCpp;
  153. };
  154. //==============================================================================
  155. class AudioPluginAppWizard : public NewProjectWizard
  156. {
  157. public:
  158. AudioPluginAppWizard() {}
  159. ~AudioPluginAppWizard() {}
  160. const String getName() { return "Audio Plug-In"; }
  161. const String getDescription() { return "Creates an audio plugin project"; }
  162. void addItemsToAlertWindow (AlertWindow& aw)
  163. {
  164. }
  165. const String processResultsFromAlertWindow (AlertWindow& aw)
  166. {
  167. return String::empty;
  168. }
  169. bool initialiseProject (Project& project)
  170. {
  171. if (! getSourceFilesFolder().createDirectory())
  172. failedFiles.add (getSourceFilesFolder().getFullPathName());
  173. String filterClassName = CodeHelpers::makeValidIdentifier (appTitle, true, true, false) + "AudioProcessor";
  174. filterClassName = filterClassName.substring (0, 1).toUpperCase() + filterClassName.substring (1);
  175. String editorClassName = filterClassName + "Editor";
  176. File filterCppFile = getSourceFilesFolder().getChildFile ("PluginProcessor.cpp");
  177. File filterHFile = filterCppFile.withFileExtension (".h");
  178. File editorCppFile = getSourceFilesFolder().getChildFile ("PluginEditor.cpp");
  179. File editorHFile = editorCppFile.withFileExtension (".h");
  180. project.getProjectType() = Project::audioPlugin;
  181. Project::Item group (project.createNewGroup());
  182. project.getMainGroup().addChild (group, 0);
  183. group.getName() = "Source";
  184. project.getJuceConfigFlag ("JUCE_QUICKTIME") = Project::configFlagDisabled; // disabled because it interferes with RTAS build on PC
  185. for (int i = project.getNumConfigurations(); --i >= 0;)
  186. project.getConfiguration(i).getTargetBinaryName() = File::createLegalFileName (appTitle);
  187. String appHeaders (CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), filterCppFile));
  188. appHeaders << newLine << CodeHelpers::createIncludeStatement (project.getPluginCharacteristicsFile(), filterCppFile);
  189. String filterCpp = project.getFileTemplate ("jucer_AudioPluginFilterTemplate_cpp")
  190. .replace ("FILTERHEADERS", CodeHelpers::createIncludeStatement (filterHFile, filterCppFile)
  191. + newLine + CodeHelpers::createIncludeStatement (editorHFile, filterCppFile), false)
  192. .replace ("FILTERCLASSNAME", filterClassName, false)
  193. .replace ("EDITORCLASSNAME", editorClassName, false);
  194. String filterH = project.getFileTemplate ("jucer_AudioPluginFilterTemplate_h")
  195. .replace ("APPHEADERS", appHeaders, false)
  196. .replace ("FILTERCLASSNAME", filterClassName, false)
  197. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (filterHFile), false);
  198. String editorCpp = project.getFileTemplate ("jucer_AudioPluginEditorTemplate_cpp")
  199. .replace ("EDITORCPPHEADERS", CodeHelpers::createIncludeStatement (filterHFile, filterCppFile)
  200. + newLine + CodeHelpers::createIncludeStatement (editorHFile, filterCppFile), false)
  201. .replace ("FILTERCLASSNAME", filterClassName, false)
  202. .replace ("EDITORCLASSNAME", editorClassName, false);
  203. String editorH = project.getFileTemplate ("jucer_AudioPluginEditorTemplate_h")
  204. .replace ("EDITORHEADERS", appHeaders + newLine + CodeHelpers::createIncludeStatement (filterHFile, filterCppFile), false)
  205. .replace ("FILTERCLASSNAME", filterClassName, false)
  206. .replace ("EDITORCLASSNAME", editorClassName, false)
  207. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (editorHFile), false);
  208. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (filterCppFile, filterCpp))
  209. failedFiles.add (filterCppFile.getFullPathName());
  210. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (filterHFile, filterH))
  211. failedFiles.add (filterHFile.getFullPathName());
  212. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (editorCppFile, editorCpp))
  213. failedFiles.add (editorCppFile.getFullPathName());
  214. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (editorHFile, editorH))
  215. failedFiles.add (editorHFile.getFullPathName());
  216. group.addFile (filterCppFile, -1);
  217. group.addFile (filterHFile, -1);
  218. group.addFile (editorCppFile, -1);
  219. group.addFile (editorHFile, -1);
  220. return true;
  221. }
  222. };
  223. //==============================================================================
  224. /*class BrowserPluginAppWizard : public NewProjectWizard
  225. {
  226. public:
  227. BrowserPluginAppWizard() {}
  228. ~BrowserPluginAppWizard() {}
  229. const String getName() { return "Browser Plug-In"; }
  230. const String getDescription() { return "Creates an audio plugin project"; }
  231. void addItemsToAlertWindow (AlertWindow& aw)
  232. {
  233. }
  234. const String processResultsFromAlertWindow (AlertWindow& aw)
  235. {
  236. return String::empty;
  237. }
  238. bool initialiseProject (Project& project)
  239. {
  240. return true;
  241. }
  242. };*/
  243. //==============================================================================
  244. //==============================================================================
  245. NewProjectWizard::NewProjectWizard()
  246. {
  247. }
  248. NewProjectWizard::~NewProjectWizard()
  249. {
  250. }
  251. const StringArray NewProjectWizard::getWizards()
  252. {
  253. StringArray s;
  254. for (int i = 0; i < getNumWizards(); ++i)
  255. {
  256. ScopedPointer <NewProjectWizard> wiz (createWizard (i));
  257. s.add (wiz->getName());
  258. }
  259. return s;
  260. }
  261. int NewProjectWizard::getNumWizards()
  262. {
  263. return 3;
  264. }
  265. NewProjectWizard* NewProjectWizard::createWizard (int index)
  266. {
  267. switch (index)
  268. {
  269. case 0: return new GUIAppWizard();
  270. case 1: return new ConsoleAppWizard();
  271. case 2: return new AudioPluginAppWizard();
  272. //case 3: return new BrowserPluginAppWizard();
  273. default: jassertfalse; break;
  274. }
  275. return 0;
  276. }
  277. //==============================================================================
  278. Project* NewProjectWizard::runWizard (Component* ownerWindow_)
  279. {
  280. ownerWindow = ownerWindow_;
  281. {
  282. static File newProjectFolder;
  283. FileChooser fc ("New Juce Project", newProjectFolder, "*");
  284. if (! fc.browseForDirectory())
  285. return 0;
  286. targetFolder = newProjectFolder = fc.getResult();
  287. if (! newProjectFolder.exists())
  288. {
  289. if (! newProjectFolder.createDirectory())
  290. failedFiles.add (newProjectFolder.getFullPathName());
  291. }
  292. if (FileHelpers::containsAnyNonHiddenFiles (newProjectFolder))
  293. {
  294. if (! AlertWindow::showOkCancelBox (AlertWindow::InfoIcon, "New Juce Project",
  295. "The folder you chose isn't empty - are you sure you want to create the project there?\n\nAny existing files with the same names may be overwritten by the new files."))
  296. return 0;
  297. }
  298. }
  299. if (failedFiles.size() == 0)
  300. {
  301. AlertWindow aw ("New " + getName(),
  302. "Please choose some basic project options...",
  303. AlertWindow::NoIcon, ownerWindow);
  304. aw.addTextEditor ("name", "", "Project Name", false);
  305. addItemsToAlertWindow (aw);
  306. aw.addButton ("Create Project", 1, KeyPress (KeyPress::returnKey));
  307. aw.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey));
  308. for (;;)
  309. {
  310. if (aw.runModalLoop() == 0)
  311. return 0;
  312. appTitle = aw.getTextEditorContents ("name").trim();
  313. String error (processResultsFromAlertWindow (aw));
  314. if (error.isEmpty() && appTitle.isEmpty())
  315. error = "Please enter a sensible project title!";
  316. if (error.isEmpty())
  317. break;
  318. aw.setColour (AlertWindow::textColourId, Colours::red);
  319. aw.setMessage (error);
  320. }
  321. }
  322. projectFile = targetFolder.getChildFile (File::createLegalFileName (appTitle))
  323. .withFileExtension (Project::projectFileExtension);
  324. ScopedPointer <Project> project (new Project (projectFile));
  325. if (failedFiles.size() == 0)
  326. {
  327. project->setFile (projectFile);
  328. project->setTitle (appTitle);
  329. project->setBundleIdentifierToDefault();
  330. if (! initialiseProject (*project))
  331. return 0;
  332. if (project->save (false, true) != FileBasedDocument::savedOk)
  333. return 0;
  334. project->setChangedFlag (false);
  335. }
  336. if (failedFiles.size() > 0)
  337. {
  338. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  339. "Errors in Creating Project!",
  340. "The following files couldn't be written:\n\n"
  341. + failedFiles.joinIntoString ("\n", 0, 10));
  342. return 0;
  343. }
  344. return project.release();
  345. }
  346. Project* NewProjectWizard::runNewProjectWizard (Component* ownerWindow)
  347. {
  348. ScopedPointer <NewProjectWizard> wizard;
  349. {
  350. AlertWindow aw ("New Juce Project",
  351. "Select the type of project to create, and the location of your Juce folder",
  352. AlertWindow::NoIcon,
  353. ownerWindow);
  354. aw.addComboBox ("type", getWizards(), "Project Type");
  355. FilenameComponent juceFolderSelector ("Juce Library Location", StoredSettings::getInstance()->getLastKnownJuceFolder(),
  356. true, true, false, "*", String::empty, "(Please select the folder containing Juce!)");
  357. juceFolderSelector.setSize (350, 22);
  358. aw.addCustomComponent (&juceFolderSelector);
  359. aw.addButton ("Next", 1, KeyPress (KeyPress::returnKey));
  360. aw.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey));
  361. for (;;)
  362. {
  363. if (aw.runModalLoop() == 0)
  364. return 0;
  365. if (FileHelpers::isJuceFolder (juceFolderSelector.getCurrentFile()))
  366. {
  367. wizard = createWizard (aw.getComboBoxComponent ("type")->getSelectedItemIndex());
  368. break;
  369. }
  370. aw.setColour (AlertWindow::textColourId, Colours::red);
  371. aw.setMessage ("Please select a valid Juce folder for the project to use!");
  372. }
  373. }
  374. return wizard != 0 ? wizard->runWizard (ownerWindow) : 0;
  375. }