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.

271 lines
9.7KB

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