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.

496 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.getObjectiveCClassSuffix() = project.getProjectUID();
  182. Project::Item group (project.createNewGroup());
  183. project.getMainGroup().addChild (group, 0);
  184. group.getName() = "Source";
  185. project.getJuceConfigFlag ("JUCE_QUICKTIME") = Project::configFlagDisabled; // disabled because it interferes with RTAS build on PC
  186. for (int i = project.getNumConfigurations(); --i >= 0;)
  187. project.getConfiguration(i).getTargetBinaryName() = File::createLegalFileName (appTitle);
  188. String appHeaders (CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), filterCppFile));
  189. appHeaders << newLine << CodeHelpers::createIncludeStatement (project.getPluginCharacteristicsFile(), filterCppFile);
  190. String filterCpp = project.getFileTemplate ("jucer_AudioPluginFilterTemplate_cpp")
  191. .replace ("FILTERHEADERS", CodeHelpers::createIncludeStatement (filterHFile, filterCppFile)
  192. + newLine + CodeHelpers::createIncludeStatement (editorHFile, filterCppFile), false)
  193. .replace ("FILTERCLASSNAME", filterClassName, false)
  194. .replace ("EDITORCLASSNAME", editorClassName, false);
  195. String filterH = project.getFileTemplate ("jucer_AudioPluginFilterTemplate_h")
  196. .replace ("APPHEADERS", appHeaders, false)
  197. .replace ("FILTERCLASSNAME", filterClassName, false)
  198. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (filterHFile), false);
  199. String editorCpp = project.getFileTemplate ("jucer_AudioPluginEditorTemplate_cpp")
  200. .replace ("EDITORCPPHEADERS", CodeHelpers::createIncludeStatement (filterHFile, filterCppFile)
  201. + newLine + CodeHelpers::createIncludeStatement (editorHFile, filterCppFile), false)
  202. .replace ("FILTERCLASSNAME", filterClassName, false)
  203. .replace ("EDITORCLASSNAME", editorClassName, false);
  204. String editorH = project.getFileTemplate ("jucer_AudioPluginEditorTemplate_h")
  205. .replace ("EDITORHEADERS", appHeaders + newLine + CodeHelpers::createIncludeStatement (filterHFile, filterCppFile), false)
  206. .replace ("FILTERCLASSNAME", filterClassName, false)
  207. .replace ("EDITORCLASSNAME", editorClassName, false)
  208. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (editorHFile), false);
  209. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (filterCppFile, filterCpp))
  210. failedFiles.add (filterCppFile.getFullPathName());
  211. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (filterHFile, filterH))
  212. failedFiles.add (filterHFile.getFullPathName());
  213. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (editorCppFile, editorCpp))
  214. failedFiles.add (editorCppFile.getFullPathName());
  215. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (editorHFile, editorH))
  216. failedFiles.add (editorHFile.getFullPathName());
  217. group.addFile (filterCppFile, -1);
  218. group.addFile (filterHFile, -1);
  219. group.addFile (editorCppFile, -1);
  220. group.addFile (editorHFile, -1);
  221. return true;
  222. }
  223. };
  224. //==============================================================================
  225. /*class BrowserPluginAppWizard : public NewProjectWizard
  226. {
  227. public:
  228. BrowserPluginAppWizard() {}
  229. ~BrowserPluginAppWizard() {}
  230. const String getName() { return "Browser Plug-In"; }
  231. const String getDescription() { return "Creates an audio plugin project"; }
  232. void addItemsToAlertWindow (AlertWindow& aw)
  233. {
  234. }
  235. const String processResultsFromAlertWindow (AlertWindow& aw)
  236. {
  237. return String::empty;
  238. }
  239. bool initialiseProject (Project& project)
  240. {
  241. return true;
  242. }
  243. };*/
  244. //==============================================================================
  245. //==============================================================================
  246. NewProjectWizard::NewProjectWizard()
  247. {
  248. }
  249. NewProjectWizard::~NewProjectWizard()
  250. {
  251. }
  252. const StringArray NewProjectWizard::getWizards()
  253. {
  254. StringArray s;
  255. for (int i = 0; i < getNumWizards(); ++i)
  256. {
  257. ScopedPointer <NewProjectWizard> wiz (createWizard (i));
  258. s.add (wiz->getName());
  259. }
  260. return s;
  261. }
  262. int NewProjectWizard::getNumWizards()
  263. {
  264. return 3;
  265. }
  266. NewProjectWizard* NewProjectWizard::createWizard (int index)
  267. {
  268. switch (index)
  269. {
  270. case 0: return new GUIAppWizard();
  271. case 1: return new ConsoleAppWizard();
  272. case 2: return new AudioPluginAppWizard();
  273. //case 3: return new BrowserPluginAppWizard();
  274. default: jassertfalse; break;
  275. }
  276. return 0;
  277. }
  278. //==============================================================================
  279. Project* NewProjectWizard::runWizard (Component* ownerWindow_)
  280. {
  281. ownerWindow = ownerWindow_;
  282. {
  283. static File newProjectFolder;
  284. FileChooser fc ("New Juce Project", newProjectFolder, "*");
  285. if (! fc.browseForDirectory())
  286. return 0;
  287. targetFolder = newProjectFolder = fc.getResult();
  288. if (! newProjectFolder.exists())
  289. {
  290. if (! newProjectFolder.createDirectory())
  291. failedFiles.add (newProjectFolder.getFullPathName());
  292. }
  293. if (FileHelpers::containsAnyNonHiddenFiles (newProjectFolder))
  294. {
  295. if (! AlertWindow::showOkCancelBox (AlertWindow::InfoIcon, "New Juce Project",
  296. "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."))
  297. return 0;
  298. }
  299. }
  300. if (failedFiles.size() == 0)
  301. {
  302. AlertWindow aw ("New " + getName(),
  303. "Please choose some basic project options...",
  304. AlertWindow::NoIcon, ownerWindow);
  305. aw.addTextEditor ("name", "", "Project Name", false);
  306. addItemsToAlertWindow (aw);
  307. aw.addButton ("Create Project", 1, KeyPress (KeyPress::returnKey));
  308. aw.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey));
  309. for (;;)
  310. {
  311. if (aw.runModalLoop() == 0)
  312. return 0;
  313. appTitle = aw.getTextEditorContents ("name").trim();
  314. String error (processResultsFromAlertWindow (aw));
  315. if (error.isEmpty() && appTitle.isEmpty())
  316. error = "Please enter a sensible project title!";
  317. if (error.isEmpty())
  318. break;
  319. aw.setColour (AlertWindow::textColourId, Colours::red);
  320. aw.setMessage (error);
  321. }
  322. }
  323. projectFile = targetFolder.getChildFile (File::createLegalFileName (appTitle))
  324. .withFileExtension (Project::projectFileExtension);
  325. ScopedPointer <Project> project (new Project (projectFile));
  326. if (failedFiles.size() == 0)
  327. {
  328. project->setFile (projectFile);
  329. project->setTitle (appTitle);
  330. project->setBundleIdentifierToDefault();
  331. if (! initialiseProject (*project))
  332. return 0;
  333. if (project->save (false, true) != FileBasedDocument::savedOk)
  334. return 0;
  335. project->setChangedFlag (false);
  336. }
  337. if (failedFiles.size() > 0)
  338. {
  339. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  340. "Errors in Creating Project!",
  341. "The following files couldn't be written:\n\n"
  342. + failedFiles.joinIntoString ("\n", 0, 10));
  343. return 0;
  344. }
  345. return project.release();
  346. }
  347. Project* NewProjectWizard::runNewProjectWizard (Component* ownerWindow)
  348. {
  349. ScopedPointer <NewProjectWizard> wizard;
  350. {
  351. AlertWindow aw ("New Juce Project",
  352. "Select the type of project to create, and the location of your Juce folder",
  353. AlertWindow::NoIcon,
  354. ownerWindow);
  355. aw.addComboBox ("type", getWizards(), "Project Type");
  356. FilenameComponent juceFolderSelector ("Juce Library Location", StoredSettings::getInstance()->getLastKnownJuceFolder(),
  357. true, true, false, "*", String::empty, "(Please select the folder containing Juce!)");
  358. juceFolderSelector.setSize (350, 22);
  359. aw.addCustomComponent (&juceFolderSelector);
  360. aw.addButton ("Next", 1, KeyPress (KeyPress::returnKey));
  361. aw.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey));
  362. for (;;)
  363. {
  364. if (aw.runModalLoop() == 0)
  365. return 0;
  366. if (FileHelpers::isJuceFolder (juceFolderSelector.getCurrentFile()))
  367. {
  368. wizard = createWizard (aw.getComboBoxComponent ("type")->getSelectedItemIndex());
  369. break;
  370. }
  371. aw.setColour (AlertWindow::textColourId, Colours::red);
  372. aw.setMessage ("Please select a valid Juce folder for the project to use!");
  373. }
  374. }
  375. return wizard != 0 ? wizard->runWizard (ownerWindow) : 0;
  376. }