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.

276 lines
9.8KB

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