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.

266 lines
9.4KB

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