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.

574 lines
23KB

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