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.

260 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../Application/jucer_Headers.h"
  14. #include "jucer_NewFileWizard.h"
  15. //==============================================================================
  16. namespace
  17. {
  18. static String fillInBasicTemplateFields (const File& file, const Project::Item& item, const char* templateName)
  19. {
  20. return replaceLineFeeds (item.project.getFileTemplate (templateName)
  21. .replace ("%%filename%%", file.getFileName(), false)
  22. .replace ("%%date%%", Time::getCurrentTime().toString (true, true, true), false)
  23. .replace ("%%author%%", SystemStats::getFullUserName(), false)
  24. .replace ("%%include_corresponding_header%%", CodeHelpers::createIncludeStatement (file.withFileExtension (".h"), file)),
  25. item.project.getProjectLineFeed());
  26. }
  27. static bool fillInNewCppFileTemplate (const File& file, const Project::Item& item, const char* templateName)
  28. {
  29. return build_tools::overwriteFileWithNewDataIfDifferent (file, fillInBasicTemplateFields (file, item, templateName));
  30. }
  31. const int menuBaseID = 0x12d83f0;
  32. }
  33. //==============================================================================
  34. class NewCppFileWizard : public NewFileWizard::Type
  35. {
  36. public:
  37. NewCppFileWizard() {}
  38. String getName() override { return "CPP File"; }
  39. void createNewFile (Project&, Project::Item parent) override
  40. {
  41. const File newFile (askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent));
  42. if (newFile != File())
  43. create (parent, newFile, "jucer_NewCppFileTemplate_cpp");
  44. }
  45. static bool create (Project::Item parent, const File& newFile, const char* templateName)
  46. {
  47. if (fillInNewCppFileTemplate (newFile, parent, templateName))
  48. {
  49. parent.addFileRetainingSortOrder (newFile, true);
  50. return true;
  51. }
  52. showFailedToWriteMessage (newFile);
  53. return false;
  54. }
  55. };
  56. //==============================================================================
  57. class NewHeaderFileWizard : public NewFileWizard::Type
  58. {
  59. public:
  60. NewHeaderFileWizard() {}
  61. String getName() override { return "Header File"; }
  62. void createNewFile (Project&, Project::Item parent) override
  63. {
  64. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h", parent));
  65. if (newFile != File())
  66. create (parent, newFile, "jucer_NewCppFileTemplate_h");
  67. }
  68. static bool create (Project::Item parent, const File& newFile, const char* templateName)
  69. {
  70. if (fillInNewCppFileTemplate (newFile, parent, templateName))
  71. {
  72. parent.addFileRetainingSortOrder (newFile, true);
  73. return true;
  74. }
  75. showFailedToWriteMessage (newFile);
  76. return false;
  77. }
  78. };
  79. //==============================================================================
  80. class NewCppAndHeaderFileWizard : public NewFileWizard::Type
  81. {
  82. public:
  83. NewCppAndHeaderFileWizard() {}
  84. String getName() override { return "CPP & Header File"; }
  85. void createNewFile (Project&, Project::Item parent) override
  86. {
  87. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h;*.cpp", parent));
  88. if (newFile != File())
  89. {
  90. if (NewCppFileWizard::create (parent, newFile.withFileExtension ("h"), "jucer_NewCppFileTemplate_h"))
  91. NewCppFileWizard::create (parent, newFile.withFileExtension ("cpp"), "jucer_NewCppFileTemplate_cpp");
  92. }
  93. }
  94. };
  95. //==============================================================================
  96. class NewComponentFileWizard : public NewFileWizard::Type
  97. {
  98. public:
  99. NewComponentFileWizard() {}
  100. String getName() override { return "Component class (split between a CPP & header)"; }
  101. void createNewFile (Project&, Project::Item parent) override
  102. {
  103. for (;;)
  104. {
  105. AlertWindow aw (TRANS ("Create new Component class"),
  106. TRANS ("Please enter the name for the new class"),
  107. AlertWindow::NoIcon, nullptr);
  108. aw.addTextEditor (getClassNameFieldName(), String(), String(), false);
  109. aw.addButton (TRANS ("Create Files"), 1, KeyPress (KeyPress::returnKey));
  110. aw.addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  111. if (aw.runModalLoop() == 0)
  112. break;
  113. const String className (aw.getTextEditorContents (getClassNameFieldName()).trim());
  114. if (className == build_tools::makeValidIdentifier (className, false, true, false))
  115. {
  116. const File newFile (askUserToChooseNewFile (className + ".h", "*.h;*.cpp", parent));
  117. if (newFile != File())
  118. createFiles (parent, className, newFile);
  119. break;
  120. }
  121. }
  122. }
  123. static bool create (const String& className, Project::Item parent,
  124. const File& newFile, const char* templateName)
  125. {
  126. auto content = fillInBasicTemplateFields (newFile, parent, templateName)
  127. .replace ("%%component_class%%", className)
  128. .replace ("%%include_juce%%", CodeHelpers::createIncludePathIncludeStatement (Project::getJuceSourceHFilename()));
  129. content = replaceLineFeeds (content, parent.project.getProjectLineFeed());
  130. if (build_tools::overwriteFileWithNewDataIfDifferent (newFile, content))
  131. {
  132. parent.addFileRetainingSortOrder (newFile, true);
  133. return true;
  134. }
  135. showFailedToWriteMessage (newFile);
  136. return false;
  137. }
  138. private:
  139. virtual void createFiles (Project::Item parent, const String& className, const File& newFile)
  140. {
  141. if (create (className, parent, newFile.withFileExtension ("h"), "jucer_NewComponentTemplate_h"))
  142. create (className, parent, newFile.withFileExtension ("cpp"), "jucer_NewComponentTemplate_cpp");
  143. }
  144. static String getClassNameFieldName() { return "Class Name"; }
  145. };
  146. //==============================================================================
  147. class NewSingleFileComponentFileWizard : public NewComponentFileWizard
  148. {
  149. public:
  150. NewSingleFileComponentFileWizard() {}
  151. String getName() override { return "Component class (in a single source file)"; }
  152. void createFiles (Project::Item parent, const String& className, const File& newFile) override
  153. {
  154. create (className, parent, newFile.withFileExtension ("h"), "jucer_NewInlineComponentTemplate_h");
  155. }
  156. };
  157. //==============================================================================
  158. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  159. {
  160. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  161. "Failed to Create File!",
  162. "Couldn't write to the file: " + file.getFullPathName());
  163. }
  164. File NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  165. const Project::Item& projectGroupToAddTo)
  166. {
  167. FileChooser fc ("Select File to Create",
  168. projectGroupToAddTo.determineGroupFolder()
  169. .getChildFile (suggestedFilename)
  170. .getNonexistentSibling(),
  171. wildcard);
  172. if (fc.browseForFileToSave (true))
  173. return fc.getResult();
  174. return {};
  175. }
  176. //==============================================================================
  177. NewFileWizard::NewFileWizard()
  178. {
  179. registerWizard (new NewCppFileWizard());
  180. registerWizard (new NewHeaderFileWizard());
  181. registerWizard (new NewCppAndHeaderFileWizard());
  182. registerWizard (new NewComponentFileWizard());
  183. registerWizard (new NewSingleFileComponentFileWizard());
  184. }
  185. NewFileWizard::~NewFileWizard()
  186. {
  187. }
  188. void NewFileWizard::addWizardsToMenu (PopupMenu& m) const
  189. {
  190. for (int i = 0; i < wizards.size(); ++i)
  191. m.addItem (menuBaseID + i, "Add New " + wizards.getUnchecked(i)->getName() + "...");
  192. }
  193. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, Project& project, const Project::Item& projectGroupToAddTo) const
  194. {
  195. if (Type* wiz = wizards [chosenMenuItemID - menuBaseID])
  196. {
  197. wiz->createNewFile (project, projectGroupToAddTo);
  198. return true;
  199. }
  200. return false;
  201. }
  202. void NewFileWizard::registerWizard (Type* newWizard)
  203. {
  204. wizards.add (newWizard);
  205. }