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.

268 lines
9.4KB

  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 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. }
  33. static bool fillInNewCppFileTemplate (const File& file, const Project::Item& item, const char* templateName)
  34. {
  35. return FileHelpers::overwriteFileWithNewDataIfDifferent (file, fillInBasicTemplateFields (file, item, templateName));
  36. }
  37. const int menuBaseID = 0x12d83f0;
  38. }
  39. //==============================================================================
  40. class NewCppFileWizard : public NewFileWizard::Type
  41. {
  42. public:
  43. NewCppFileWizard() {}
  44. String getName() override { return "CPP File"; }
  45. void createNewFile (Project&, Project::Item parent) override
  46. {
  47. const File newFile (askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent));
  48. if (newFile != File())
  49. create (parent, newFile, "jucer_NewCppFileTemplate_cpp");
  50. }
  51. static bool create (Project::Item parent, const File& newFile, const char* templateName)
  52. {
  53. if (fillInNewCppFileTemplate (newFile, parent, templateName))
  54. {
  55. parent.addFileRetainingSortOrder (newFile, true);
  56. return true;
  57. }
  58. showFailedToWriteMessage (newFile);
  59. return false;
  60. }
  61. };
  62. //==============================================================================
  63. class NewHeaderFileWizard : public NewFileWizard::Type
  64. {
  65. public:
  66. NewHeaderFileWizard() {}
  67. String getName() override { return "Header File"; }
  68. void createNewFile (Project&, Project::Item parent) override
  69. {
  70. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h", parent));
  71. if (newFile != File())
  72. create (parent, newFile, "jucer_NewCppFileTemplate_h");
  73. }
  74. static bool create (Project::Item parent, const File& newFile, const char* templateName)
  75. {
  76. if (fillInNewCppFileTemplate (newFile, parent, templateName))
  77. {
  78. parent.addFileRetainingSortOrder (newFile, true);
  79. return true;
  80. }
  81. showFailedToWriteMessage (newFile);
  82. return false;
  83. }
  84. };
  85. //==============================================================================
  86. class NewCppAndHeaderFileWizard : public NewFileWizard::Type
  87. {
  88. public:
  89. NewCppAndHeaderFileWizard() {}
  90. String getName() override { return "CPP & Header File"; }
  91. void createNewFile (Project&, Project::Item parent) override
  92. {
  93. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h;*.cpp", parent));
  94. if (newFile != File())
  95. {
  96. if (NewCppFileWizard::create (parent, newFile.withFileExtension ("h"), "jucer_NewCppFileTemplate_h"))
  97. NewCppFileWizard::create (parent, newFile.withFileExtension ("cpp"), "jucer_NewCppFileTemplate_cpp");
  98. }
  99. }
  100. };
  101. //==============================================================================
  102. class NewComponentFileWizard : public NewFileWizard::Type
  103. {
  104. public:
  105. NewComponentFileWizard() {}
  106. String getName() override { return "Component class (split between a CPP & header)"; }
  107. void createNewFile (Project&, Project::Item parent) override
  108. {
  109. for (;;)
  110. {
  111. AlertWindow aw (TRANS ("Create new Component class"),
  112. TRANS ("Please enter the name for the new class"),
  113. AlertWindow::NoIcon, nullptr);
  114. aw.addTextEditor (getClassNameFieldName(), String(), String(), false);
  115. aw.addButton (TRANS ("Create Files"), 1, KeyPress (KeyPress::returnKey));
  116. aw.addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  117. if (aw.runModalLoop() == 0)
  118. break;
  119. const String className (aw.getTextEditorContents (getClassNameFieldName()).trim());
  120. if (className == CodeHelpers::makeValidIdentifier (className, false, true, false))
  121. {
  122. const File newFile (askUserToChooseNewFile (className + ".h", "*.h;*.cpp", parent));
  123. if (newFile != File())
  124. createFiles (parent, className, newFile);
  125. break;
  126. }
  127. }
  128. }
  129. static bool create (const String& className, Project::Item parent,
  130. const File& newFile, const char* templateName)
  131. {
  132. String content = fillInBasicTemplateFields (newFile, parent, templateName)
  133. .replace ("%%component_class%%", className)
  134. .replace ("%%include_juce%%", CodeHelpers::createIncludeStatement (parent.project.getAppIncludeFile(), newFile));
  135. if (FileHelpers::overwriteFileWithNewDataIfDifferent (newFile, content))
  136. {
  137. parent.addFileRetainingSortOrder (newFile, true);
  138. return true;
  139. }
  140. showFailedToWriteMessage (newFile);
  141. return false;
  142. }
  143. private:
  144. virtual void createFiles (Project::Item parent, const String& className, const File& newFile)
  145. {
  146. if (create (className, parent, newFile.withFileExtension ("h"), "jucer_NewComponentTemplate_h"))
  147. create (className, parent, newFile.withFileExtension ("cpp"), "jucer_NewComponentTemplate_cpp");
  148. }
  149. static String getClassNameFieldName() { return "Class Name"; }
  150. };
  151. //==============================================================================
  152. class NewSingleFileComponentFileWizard : public NewComponentFileWizard
  153. {
  154. public:
  155. NewSingleFileComponentFileWizard() {}
  156. String getName() override { return "Component class (in a single source file)"; }
  157. void createFiles (Project::Item parent, const String& className, const File& newFile) override
  158. {
  159. create (className, parent, newFile.withFileExtension ("h"), "jucer_NewInlineComponentTemplate_h");
  160. }
  161. };
  162. //==============================================================================
  163. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  164. {
  165. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  166. "Failed to Create File!",
  167. "Couldn't write to the file: " + file.getFullPathName());
  168. }
  169. File NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  170. const Project::Item& projectGroupToAddTo)
  171. {
  172. FileChooser fc ("Select File to Create",
  173. projectGroupToAddTo.determineGroupFolder()
  174. .getChildFile (suggestedFilename)
  175. .getNonexistentSibling(),
  176. wildcard);
  177. if (fc.browseForFileToSave (true))
  178. return fc.getResult();
  179. return {};
  180. }
  181. //==============================================================================
  182. NewFileWizard::NewFileWizard()
  183. {
  184. registerWizard (new NewCppFileWizard());
  185. registerWizard (new NewHeaderFileWizard());
  186. registerWizard (new NewCppAndHeaderFileWizard());
  187. registerWizard (new NewComponentFileWizard());
  188. registerWizard (new NewSingleFileComponentFileWizard());
  189. registerWizard (createGUIComponentWizard());
  190. }
  191. NewFileWizard::~NewFileWizard()
  192. {
  193. }
  194. void NewFileWizard::addWizardsToMenu (PopupMenu& m) const
  195. {
  196. for (int i = 0; i < wizards.size(); ++i)
  197. m.addItem (menuBaseID + i, "Add New " + wizards.getUnchecked(i)->getName() + "...");
  198. }
  199. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, Project& project, const Project::Item& projectGroupToAddTo) const
  200. {
  201. if (Type* wiz = wizards [chosenMenuItemID - menuBaseID])
  202. {
  203. wiz->createNewFile (project, projectGroupToAddTo);
  204. return true;
  205. }
  206. return false;
  207. }
  208. void NewFileWizard::registerWizard (Type* newWizard)
  209. {
  210. wizards.add (newWizard);
  211. }