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.

568 lines
22KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. #include "jucer_ProjectType.h"
  20. #include "jucer_Module.h"
  21. #include "../Project Saving/jucer_ProjectExporter.h"
  22. #include "../Application/jucer_Application.h"
  23. #include "../Application/jucer_MainWindow.h"
  24. static void createFileCreationOptionComboBox (Component& setupComp,
  25. OwnedArray<Component>& itemsCreated,
  26. const char** types)
  27. {
  28. ComboBox* c = new ComboBox();
  29. itemsCreated.add (c);
  30. setupComp.addChildAndSetID (c, "filesToCreate");
  31. const char* fileOptions[] = { "Create a Main.cpp file",
  32. "Create a Main.cpp file and a basic window",
  33. "Don't create any files", 0 };
  34. c->addItemList (StringArray (fileOptions), 1);
  35. c->setSelectedId (1, false);
  36. Label* l = new Label (String::empty, "Files to Auto-Generate:");
  37. l->attachToComponent (c, true);
  38. itemsCreated.add (l);
  39. c->setBounds ("parent.width / 2 + 160, 10, parent.width - 10, top + 22");
  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. //==============================================================================
  48. class GUIAppWizard : public NewProjectWizard
  49. {
  50. public:
  51. GUIAppWizard() {}
  52. String getName() { return "GUI Application"; }
  53. String getDescription() { return "Creates a standard application"; }
  54. void addSetupItems (Component& setupComp, OwnedArray<Component>& itemsCreated)
  55. {
  56. const char* fileOptions[] = { "Create a Main.cpp file",
  57. "Create a Main.cpp file and a basic window",
  58. "Don't create any files", 0 };
  59. createFileCreationOptionComboBox (setupComp, itemsCreated, fileOptions);
  60. }
  61. Result processResultsFromSetupItems (Component& setupComp)
  62. {
  63. ComboBox* cb = dynamic_cast<ComboBox*> (setupComp.findChildWithID ("filesToCreate"));
  64. jassert (cb != nullptr);
  65. createMainCpp = createWindow = false;
  66. switch (cb->getSelectedItemIndex())
  67. {
  68. case 0: createMainCpp = true; break;
  69. case 1: createMainCpp = createWindow = true; break;
  70. case 2: break;
  71. default: jassertfalse; break;
  72. }
  73. return Result::ok();
  74. }
  75. bool initialiseProject (Project& project)
  76. {
  77. if (! getSourceFilesFolder().createDirectory())
  78. failedFiles.add (getSourceFilesFolder().getFullPathName());
  79. File mainCppFile = getSourceFilesFolder().getChildFile ("Main.cpp");
  80. File mainWindowCpp = getSourceFilesFolder().getChildFile ("MainWindow.cpp");
  81. File mainWindowH = mainWindowCpp.withFileExtension (".h");
  82. String windowClassName = "MainAppWindow";
  83. project.getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  84. Project::Item sourceGroup (project.getMainGroup().addNewSubGroup ("Source", 0));
  85. setExecutableNameForAllTargets (project, File::createLegalFileName (appTitle));
  86. String appHeaders (CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), mainCppFile));
  87. String initCode, shutdownCode, anotherInstanceStartedCode, privateMembers, memberInitialisers;
  88. if (createWindow)
  89. {
  90. appHeaders << newLine << CodeHelpers::createIncludeStatement (mainWindowH, mainCppFile);
  91. initCode = "mainWindow = new " + windowClassName + "();";
  92. shutdownCode = "mainWindow = 0;";
  93. privateMembers = "ScopedPointer <" + windowClassName + "> mainWindow;";
  94. String windowH = project.getFileTemplate ("jucer_WindowTemplate_h")
  95. .replace ("INCLUDES", CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), mainWindowH), false)
  96. .replace ("WINDOWCLASS", windowClassName, false)
  97. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (mainWindowH), false);
  98. String windowCpp = project.getFileTemplate ("jucer_WindowTemplate_cpp")
  99. .replace ("INCLUDES", CodeHelpers::createIncludeStatement (mainWindowH, mainWindowCpp), false)
  100. .replace ("WINDOWCLASS", windowClassName, false);
  101. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainWindowH, windowH))
  102. failedFiles.add (mainWindowH.getFullPathName());
  103. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainWindowCpp, windowCpp))
  104. failedFiles.add (mainWindowCpp.getFullPathName());
  105. sourceGroup.addFile (mainWindowCpp, -1, true);
  106. sourceGroup.addFile (mainWindowH, -1, false);
  107. }
  108. if (createMainCpp)
  109. {
  110. String mainCpp = project.getFileTemplate ("jucer_MainTemplate_cpp")
  111. .replace ("APPHEADERS", appHeaders, false)
  112. .replace ("APPCLASSNAME", CodeHelpers::makeValidIdentifier (appTitle + "Application", false, true, false), false)
  113. .replace ("MEMBERINITIALISERS", memberInitialisers, false)
  114. .replace ("APPINITCODE", initCode, false)
  115. .replace ("APPSHUTDOWNCODE", shutdownCode, false)
  116. .replace ("APPNAME", CodeHelpers::addEscapeChars (appTitle), false)
  117. .replace ("APPVERSION", "1.0", false)
  118. .replace ("ALLOWMORETHANONEINSTANCE", "true", false)
  119. .replace ("ANOTHERINSTANCECODE", anotherInstanceStartedCode, false)
  120. .replace ("PRIVATEMEMBERS", privateMembers, false);
  121. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainCppFile, mainCpp))
  122. failedFiles.add (mainCppFile.getFullPathName());
  123. sourceGroup.addFile (mainCppFile, -1, true);
  124. }
  125. return true;
  126. }
  127. private:
  128. bool createMainCpp, createWindow;
  129. };
  130. //==============================================================================
  131. class ConsoleAppWizard : public NewProjectWizard
  132. {
  133. public:
  134. ConsoleAppWizard() {}
  135. String getName() { return "Console Application"; }
  136. String getDescription() { return "Creates a command-line application with no GUI features"; }
  137. void addSetupItems (Component& setupComp, OwnedArray<Component>& itemsCreated)
  138. {
  139. const char* fileOptions[] = { "Create a Main.cpp file",
  140. "Don't create any files", 0 };
  141. createFileCreationOptionComboBox (setupComp, itemsCreated, fileOptions);
  142. }
  143. Result processResultsFromSetupItems (Component& setupComp)
  144. {
  145. ComboBox* cb = dynamic_cast<ComboBox*> (setupComp.findChildWithID ("filesToCreate"));
  146. jassert (cb != nullptr);
  147. createMainCpp = false;
  148. switch (cb->getSelectedItemIndex())
  149. {
  150. case 0: createMainCpp = true; break;
  151. case 1: break;
  152. default: jassertfalse; break;
  153. }
  154. return Result::ok();
  155. }
  156. bool initialiseProject (Project& project)
  157. {
  158. if (! getSourceFilesFolder().createDirectory())
  159. failedFiles.add (getSourceFilesFolder().getFullPathName());
  160. File mainCppFile = getSourceFilesFolder().getChildFile ("Main.cpp");
  161. project.getProjectTypeValue() = ProjectType::getConsoleAppTypeName();
  162. Project::Item sourceGroup (project.getMainGroup().addNewSubGroup ("Source", 0));
  163. setExecutableNameForAllTargets (project, File::createLegalFileName (appTitle));
  164. if (createMainCpp)
  165. {
  166. String appHeaders (CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), mainCppFile));
  167. String mainCpp = project.getFileTemplate ("jucer_MainConsoleAppTemplate_cpp")
  168. .replace ("APPHEADERS", appHeaders, false);
  169. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (mainCppFile, mainCpp))
  170. failedFiles.add (mainCppFile.getFullPathName());
  171. sourceGroup.addFile (mainCppFile, -1, true);
  172. }
  173. return true;
  174. }
  175. private:
  176. bool createMainCpp;
  177. };
  178. //==============================================================================
  179. class AudioPluginAppWizard : public NewProjectWizard
  180. {
  181. public:
  182. AudioPluginAppWizard() {}
  183. String getName() { return "Audio Plug-In"; }
  184. String getDescription() { return "Creates an audio plugin project"; }
  185. void addSetupItems (Component& setupComp, OwnedArray<Component>& itemsCreated)
  186. {
  187. }
  188. Result processResultsFromSetupItems (Component& setupComp)
  189. {
  190. return Result::ok();
  191. }
  192. bool initialiseProject (Project& project)
  193. {
  194. if (! getSourceFilesFolder().createDirectory())
  195. failedFiles.add (getSourceFilesFolder().getFullPathName());
  196. String filterClassName = CodeHelpers::makeValidIdentifier (appTitle, true, true, false) + "AudioProcessor";
  197. filterClassName = filterClassName.substring (0, 1).toUpperCase() + filterClassName.substring (1);
  198. String editorClassName = filterClassName + "Editor";
  199. File filterCppFile = getSourceFilesFolder().getChildFile ("PluginProcessor.cpp");
  200. File filterHFile = filterCppFile.withFileExtension (".h");
  201. File editorCppFile = getSourceFilesFolder().getChildFile ("PluginEditor.cpp");
  202. File editorHFile = editorCppFile.withFileExtension (".h");
  203. project.getProjectTypeValue() = ProjectType::getAudioPluginTypeName();
  204. project.addModule ("juce_audio_plugin_client", true);
  205. Project::Item sourceGroup (project.getMainGroup().addNewSubGroup ("Source", 0));
  206. project.getConfigFlag ("JUCE_QUICKTIME") = Project::configFlagDisabled; // disabled because it interferes with RTAS build on PC
  207. setExecutableNameForAllTargets (project, File::createLegalFileName (appTitle));
  208. String appHeaders (CodeHelpers::createIncludeStatement (project.getAppIncludeFile(), filterCppFile));
  209. String filterCpp = project.getFileTemplate ("jucer_AudioPluginFilterTemplate_cpp")
  210. .replace ("FILTERHEADERS", CodeHelpers::createIncludeStatement (filterHFile, filterCppFile)
  211. + newLine + CodeHelpers::createIncludeStatement (editorHFile, filterCppFile), false)
  212. .replace ("FILTERCLASSNAME", filterClassName, false)
  213. .replace ("EDITORCLASSNAME", editorClassName, false);
  214. String filterH = project.getFileTemplate ("jucer_AudioPluginFilterTemplate_h")
  215. .replace ("APPHEADERS", appHeaders, false)
  216. .replace ("FILTERCLASSNAME", filterClassName, false)
  217. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (filterHFile), false);
  218. String editorCpp = project.getFileTemplate ("jucer_AudioPluginEditorTemplate_cpp")
  219. .replace ("EDITORCPPHEADERS", CodeHelpers::createIncludeStatement (filterHFile, filterCppFile)
  220. + newLine + CodeHelpers::createIncludeStatement (editorHFile, filterCppFile), false)
  221. .replace ("FILTERCLASSNAME", filterClassName, false)
  222. .replace ("EDITORCLASSNAME", editorClassName, false);
  223. String editorH = project.getFileTemplate ("jucer_AudioPluginEditorTemplate_h")
  224. .replace ("EDITORHEADERS", appHeaders + newLine + CodeHelpers::createIncludeStatement (filterHFile, filterCppFile), false)
  225. .replace ("FILTERCLASSNAME", filterClassName, false)
  226. .replace ("EDITORCLASSNAME", editorClassName, false)
  227. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (editorHFile), false);
  228. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (filterCppFile, filterCpp))
  229. failedFiles.add (filterCppFile.getFullPathName());
  230. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (filterHFile, filterH))
  231. failedFiles.add (filterHFile.getFullPathName());
  232. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (editorCppFile, editorCpp))
  233. failedFiles.add (editorCppFile.getFullPathName());
  234. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (editorHFile, editorH))
  235. failedFiles.add (editorHFile.getFullPathName());
  236. sourceGroup.addFile (filterCppFile, -1, true);
  237. sourceGroup.addFile (filterHFile, -1, false);
  238. sourceGroup.addFile (editorCppFile, -1, true);
  239. sourceGroup.addFile (editorHFile, -1, false);
  240. return true;
  241. }
  242. };
  243. //==============================================================================
  244. //==============================================================================
  245. NewProjectWizard::NewProjectWizard() {}
  246. NewProjectWizard::~NewProjectWizard() {}
  247. StringArray NewProjectWizard::getWizards()
  248. {
  249. StringArray s;
  250. for (int i = 0; i < getNumWizards(); ++i)
  251. {
  252. ScopedPointer <NewProjectWizard> wiz (createWizard (i));
  253. s.add (wiz->getName());
  254. }
  255. return s;
  256. }
  257. int NewProjectWizard::getNumWizards()
  258. {
  259. return 3;
  260. }
  261. NewProjectWizard* NewProjectWizard::createWizard (int index)
  262. {
  263. switch (index)
  264. {
  265. case 0: return new GUIAppWizard();
  266. case 1: return new ConsoleAppWizard();
  267. case 2: return new AudioPluginAppWizard();
  268. //case 3: return new BrowserPluginAppWizard();
  269. default: jassertfalse; break;
  270. }
  271. return 0;
  272. }
  273. File& NewProjectWizard::getLastWizardFolder()
  274. {
  275. #if JUCE_WINDOWS
  276. static File lastFolder (File::getSpecialLocation (File::userDocumentsDirectory));
  277. #else
  278. static File lastFolder (File::getSpecialLocation (File::userHomeDirectory));
  279. #endif
  280. return lastFolder;
  281. }
  282. //==============================================================================
  283. Project* NewProjectWizard::runWizard (Component* ownerWindow_,
  284. const String& projectName,
  285. const File& targetFolder_)
  286. {
  287. ownerWindow = ownerWindow_;
  288. appTitle = projectName;
  289. targetFolder = targetFolder_;
  290. if (! targetFolder.exists())
  291. {
  292. if (! targetFolder.createDirectory())
  293. failedFiles.add (targetFolder.getFullPathName());
  294. }
  295. else if (FileHelpers::containsAnyNonHiddenFiles (targetFolder))
  296. {
  297. if (! AlertWindow::showOkCancelBox (AlertWindow::InfoIcon, "New Juce Project",
  298. "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."))
  299. return nullptr;
  300. }
  301. projectFile = targetFolder.getChildFile (File::createLegalFileName (appTitle))
  302. .withFileExtension (Project::projectFileExtension);
  303. ScopedPointer<Project> project (new Project (projectFile));
  304. project->addDefaultModules (true);
  305. if (failedFiles.size() == 0)
  306. {
  307. project->setFile (projectFile);
  308. project->setTitle (appTitle);
  309. project->setBundleIdentifierToDefault();
  310. if (! initialiseProject (*project))
  311. return nullptr;
  312. if (project->save (false, true) != FileBasedDocument::savedOk)
  313. return nullptr;
  314. project->setChangedFlag (false);
  315. }
  316. if (failedFiles.size() > 0)
  317. {
  318. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  319. "Errors in Creating Project!",
  320. "The following files couldn't be written:\n\n"
  321. + failedFiles.joinIntoString ("\n", 0, 10));
  322. return nullptr;
  323. }
  324. return project.release();
  325. }
  326. //==============================================================================
  327. class NewProjectWizard::WizardComp : public Component,
  328. private ButtonListener,
  329. private ComboBoxListener,
  330. private TextEditorListener
  331. {
  332. public:
  333. WizardComp()
  334. : projectName ("Project name"),
  335. nameLabel (String::empty, "Project Name:"),
  336. typeLabel (String::empty, "Project Type:"),
  337. fileBrowser (FileBrowserComponent::saveMode | FileBrowserComponent::canSelectDirectories,
  338. getLastWizardFolder(), nullptr, nullptr),
  339. fileOutline (String::empty, "Project Folder:"),
  340. createButton ("Create..."),
  341. cancelButton ("Cancel")
  342. {
  343. setOpaque (true);
  344. setSize (600, 500);
  345. addChildAndSetID (&projectName, "projectName");
  346. projectName.setText ("NewProject");
  347. projectName.setBounds ("100, 14, parent.width / 2 - 10, top + 22");
  348. nameLabel.attachToComponent (&projectName, true);
  349. projectName.addListener (this);
  350. addChildAndSetID (&projectType, "projectType");
  351. projectType.addItemList (getWizards(), 1);
  352. projectType.setSelectedId (1, true);
  353. projectType.setBounds ("100, projectName.bottom + 4, projectName.right, top + 22");
  354. typeLabel.attachToComponent (&projectType, true);
  355. projectType.addListener (this);
  356. addChildAndSetID (&fileOutline, "fileOutline");
  357. fileOutline.setColour (GroupComponent::outlineColourId, Colours::black.withAlpha (0.2f));
  358. fileOutline.setTextLabelPosition (Justification::centred);
  359. fileOutline.setBounds ("10, projectType.bottom + 20, projectType.right, parent.height - 10");
  360. addChildAndSetID (&fileBrowser, "fileBrowser");
  361. fileBrowser.setBounds ("fileOutline.left + 10, fileOutline.top + 20, fileOutline.right - 10, fileOutline.bottom - 12");
  362. fileBrowser.setFilenameBoxLabel ("Folder:");
  363. addChildAndSetID (&createButton, "createButton");
  364. createButton.setBounds ("right - 140, bottom - 24, parent.width - 10, parent.height - 10");
  365. createButton.addListener (this);
  366. addChildAndSetID (&cancelButton, "cancelButton");
  367. cancelButton.setBounds ("right - 140, createButton.top, createButton.left - 10, createButton.bottom");
  368. cancelButton.addListener (this);
  369. updateCustomItems();
  370. updateCreateButton();
  371. }
  372. void paint (Graphics& g)
  373. {
  374. g.fillAll (Colour::greyLevel (0.93f));
  375. }
  376. void buttonClicked (Button* b)
  377. {
  378. if (b == &createButton)
  379. {
  380. createProject();
  381. }
  382. else
  383. {
  384. MainWindow* mw = dynamic_cast<MainWindow*> (getTopLevelComponent());
  385. jassert (mw != nullptr);
  386. JucerApplication::getApp()->closeWindow (mw);
  387. }
  388. }
  389. void createProject()
  390. {
  391. MainWindow* mw = Component::findParentComponentOfClass<MainWindow>();
  392. jassert (mw != nullptr);
  393. ScopedPointer <NewProjectWizard> wizard (createWizard());
  394. if (wizard != nullptr)
  395. {
  396. Result result (wizard->processResultsFromSetupItems (*this));
  397. if (result.failed())
  398. {
  399. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "Create Project", result.getErrorMessage());
  400. return;
  401. }
  402. ScopedPointer<Project> project (wizard->runWizard (mw, projectName.getText(),
  403. fileBrowser.getSelectedFile (0)));
  404. if (project != nullptr)
  405. mw->setProject (project.release());
  406. }
  407. }
  408. void updateCustomItems()
  409. {
  410. customItems.clear();
  411. ScopedPointer <NewProjectWizard> wizard (createWizard());
  412. if (wizard != nullptr)
  413. wizard->addSetupItems (*this, customItems);
  414. }
  415. void comboBoxChanged (ComboBox*)
  416. {
  417. updateCustomItems();
  418. }
  419. void textEditorTextChanged (TextEditor&)
  420. {
  421. updateCreateButton();
  422. fileBrowser.setFileName (File::createLegalFileName (projectName.getText()));
  423. }
  424. private:
  425. ComboBox projectType;
  426. TextEditor projectName;
  427. Label nameLabel, typeLabel;
  428. FileBrowserComponent fileBrowser;
  429. GroupComponent fileOutline;
  430. TextButton createButton, cancelButton;
  431. OwnedArray<Component> customItems;
  432. NewProjectWizard* createWizard()
  433. {
  434. return NewProjectWizard::createWizard (projectType.getSelectedItemIndex());
  435. }
  436. void updateCreateButton()
  437. {
  438. createButton.setEnabled (projectName.getText().trim().isNotEmpty());
  439. }
  440. };
  441. Component* NewProjectWizard::createComponent()
  442. {
  443. return new WizardComp();
  444. }