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.

267 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_NewFileWizard.h"
  19. NewFileWizard::Type* createGUIComponentWizard();
  20. //==============================================================================
  21. namespace
  22. {
  23. static String fillInBasicTemplateFields (const File& file, const Project::Item& item, const char* templateName)
  24. {
  25. return item.project.getFileTemplate (templateName)
  26. .replace ("FILENAME", file.getFileName(), false)
  27. .replace ("DATE", Time::getCurrentTime().toString (true, true, true), false)
  28. .replace ("AUTHOR", SystemStats::getFullUserName(), false)
  29. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (file), false)
  30. .replace ("INCLUDE_CORRESPONDING_HEADER", CodeHelpers::createIncludeStatement (file.withFileExtension (".h"), file));
  31. }
  32. static bool fillInNewCppFileTemplate (const File& file, const Project::Item& item, const char* templateName)
  33. {
  34. return FileHelpers::overwriteFileWithNewDataIfDifferent (file, fillInBasicTemplateFields (file, item, templateName));
  35. }
  36. const int menuBaseID = 0x12d83f0;
  37. }
  38. //==============================================================================
  39. class NewCppFileWizard : public NewFileWizard::Type
  40. {
  41. public:
  42. NewCppFileWizard() {}
  43. String getName() override { return "CPP File"; }
  44. void createNewFile (Project&, Project::Item parent) override
  45. {
  46. const File newFile (askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent));
  47. if (newFile != File())
  48. create (parent, newFile, "jucer_NewCppFileTemplate_cpp");
  49. }
  50. static bool create (Project::Item parent, const File& newFile, const char* templateName)
  51. {
  52. if (fillInNewCppFileTemplate (newFile, parent, templateName))
  53. {
  54. parent.addFileRetainingSortOrder (newFile, true);
  55. return true;
  56. }
  57. showFailedToWriteMessage (newFile);
  58. return false;
  59. }
  60. };
  61. //==============================================================================
  62. class NewHeaderFileWizard : public NewFileWizard::Type
  63. {
  64. public:
  65. NewHeaderFileWizard() {}
  66. String getName() override { return "Header File"; }
  67. void createNewFile (Project&, Project::Item parent) override
  68. {
  69. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h", parent));
  70. if (newFile != File())
  71. create (parent, newFile, "jucer_NewCppFileTemplate_h");
  72. }
  73. static bool create (Project::Item parent, const File& newFile, const char* templateName)
  74. {
  75. if (fillInNewCppFileTemplate (newFile, parent, templateName))
  76. {
  77. parent.addFileRetainingSortOrder (newFile, true);
  78. return true;
  79. }
  80. showFailedToWriteMessage (newFile);
  81. return false;
  82. }
  83. };
  84. //==============================================================================
  85. class NewCppAndHeaderFileWizard : public NewFileWizard::Type
  86. {
  87. public:
  88. NewCppAndHeaderFileWizard() {}
  89. String getName() override { return "CPP & Header File"; }
  90. void createNewFile (Project&, Project::Item parent) override
  91. {
  92. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h;*.cpp", parent));
  93. if (newFile != File())
  94. {
  95. if (NewCppFileWizard::create (parent, newFile.withFileExtension ("h"), "jucer_NewCppFileTemplate_h"))
  96. NewCppFileWizard::create (parent, newFile.withFileExtension ("cpp"), "jucer_NewCppFileTemplate_cpp");
  97. }
  98. }
  99. };
  100. //==============================================================================
  101. class NewComponentFileWizard : public NewFileWizard::Type
  102. {
  103. public:
  104. NewComponentFileWizard() {}
  105. String getName() override { return "Component class (split between a CPP & header)"; }
  106. void createNewFile (Project&, Project::Item parent) override
  107. {
  108. for (;;)
  109. {
  110. AlertWindow aw (TRANS ("Create new Component class"),
  111. TRANS ("Please enter the name for the new class"),
  112. AlertWindow::NoIcon, nullptr);
  113. aw.addTextEditor (getClassNameFieldName(), String(), String(), false);
  114. aw.addButton (TRANS ("Create Files"), 1, KeyPress (KeyPress::returnKey));
  115. aw.addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  116. if (aw.runModalLoop() == 0)
  117. break;
  118. const String className (aw.getTextEditorContents (getClassNameFieldName()).trim());
  119. if (className == CodeHelpers::makeValidIdentifier (className, false, true, false))
  120. {
  121. const File newFile (askUserToChooseNewFile (className + ".h", "*.h;*.cpp", parent));
  122. if (newFile != File())
  123. createFiles (parent, className, newFile);
  124. break;
  125. }
  126. }
  127. }
  128. static bool create (const String& className, Project::Item parent,
  129. const File& newFile, const char* templateName)
  130. {
  131. String content = fillInBasicTemplateFields (newFile, parent, templateName)
  132. .replace ("COMPONENTCLASS", className)
  133. .replace ("INCLUDE_JUCE", CodeHelpers::createIncludeStatement (parent.project.getAppIncludeFile(), newFile));
  134. if (FileHelpers::overwriteFileWithNewDataIfDifferent (newFile, content))
  135. {
  136. parent.addFileRetainingSortOrder (newFile, true);
  137. return true;
  138. }
  139. showFailedToWriteMessage (newFile);
  140. return false;
  141. }
  142. private:
  143. virtual void createFiles (Project::Item parent, const String& className, const File& newFile)
  144. {
  145. if (create (className, parent, newFile.withFileExtension ("h"), "jucer_NewComponentTemplate_h"))
  146. create (className, parent, newFile.withFileExtension ("cpp"), "jucer_NewComponentTemplate_cpp");
  147. }
  148. static String getClassNameFieldName() { return "Class Name"; }
  149. };
  150. //==============================================================================
  151. class NewSingleFileComponentFileWizard : public NewComponentFileWizard
  152. {
  153. public:
  154. NewSingleFileComponentFileWizard() {}
  155. String getName() override { return "Component class (in a single source file)"; }
  156. void createFiles (Project::Item parent, const String& className, const File& newFile) override
  157. {
  158. create (className, parent, newFile.withFileExtension ("h"), "jucer_NewInlineComponentTemplate_h");
  159. }
  160. };
  161. //==============================================================================
  162. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  163. {
  164. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  165. "Failed to Create File!",
  166. "Couldn't write to the file: " + file.getFullPathName());
  167. }
  168. File NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  169. const Project::Item& projectGroupToAddTo)
  170. {
  171. FileChooser fc ("Select File to Create",
  172. projectGroupToAddTo.determineGroupFolder()
  173. .getChildFile (suggestedFilename)
  174. .getNonexistentSibling(),
  175. wildcard);
  176. if (fc.browseForFileToSave (true))
  177. return fc.getResult();
  178. return {};
  179. }
  180. //==============================================================================
  181. NewFileWizard::NewFileWizard()
  182. {
  183. registerWizard (new NewCppFileWizard());
  184. registerWizard (new NewHeaderFileWizard());
  185. registerWizard (new NewCppAndHeaderFileWizard());
  186. registerWizard (new NewComponentFileWizard());
  187. registerWizard (new NewSingleFileComponentFileWizard());
  188. registerWizard (createGUIComponentWizard());
  189. }
  190. NewFileWizard::~NewFileWizard()
  191. {
  192. }
  193. void NewFileWizard::addWizardsToMenu (PopupMenu& m) const
  194. {
  195. for (int i = 0; i < wizards.size(); ++i)
  196. m.addItem (menuBaseID + i, "Add New " + wizards.getUnchecked(i)->getName() + "...");
  197. }
  198. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, Project& project, const Project::Item& projectGroupToAddTo) const
  199. {
  200. if (Type* wiz = wizards [chosenMenuItemID - menuBaseID])
  201. {
  202. wiz->createNewFile (project, projectGroupToAddTo);
  203. return true;
  204. }
  205. return false;
  206. }
  207. void NewFileWizard::registerWizard (Type* newWizard)
  208. {
  209. wizards.add (newWizard);
  210. }