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.

290 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../../Application/jucer_Headers.h"
  14. #include "jucer_NewFileWizard.h"
  15. //==============================================================================
  16. namespace
  17. {
  18. static String fillInBasicTemplateFields (const File& file, const Project::Item& item, const char* templateName)
  19. {
  20. int dataSize;
  21. if (auto* data = BinaryData::getNamedResource (templateName, dataSize))
  22. {
  23. auto fileTemplate = String::fromUTF8 (data, dataSize);
  24. return replaceLineFeeds (fileTemplate.replace ("%%filename%%", file.getFileName(), false)
  25. .replace ("%%date%%", Time::getCurrentTime().toString (true, true, true), false)
  26. .replace ("%%author%%", SystemStats::getFullUserName(), false)
  27. .replace ("%%include_corresponding_header%%", CodeHelpers::createIncludeStatement (file.withFileExtension (".h"), file)),
  28. item.project.getProjectLineFeed());
  29. }
  30. jassertfalse;
  31. return {};
  32. }
  33. static bool fillInNewCppFileTemplate (const File& file, const Project::Item& item, const char* templateName)
  34. {
  35. return build_tools::overwriteFileWithNewDataIfDifferent (file, fillInBasicTemplateFields (file, item, templateName));
  36. }
  37. const int menuBaseID = 0x12d83f0;
  38. }
  39. //==============================================================================
  40. class NewCppFileWizard : public NewFileWizard::Type
  41. {
  42. public:
  43. String getName() override { return "CPP File"; }
  44. void createNewFile (Project&, Project::Item parent) override
  45. {
  46. askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent, [parent] (File newFile)
  47. {
  48. if (newFile != File())
  49. create (parent, newFile, "jucer_NewCppFileTemplate_cpp");
  50. });
  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. String getName() override { return "Header File"; }
  68. void createNewFile (Project&, Project::Item parent) override
  69. {
  70. askUserToChooseNewFile ("SourceCode.h", "*.h", parent, [parent] (File newFile)
  71. {
  72. if (newFile != File())
  73. create (parent, newFile, "jucer_NewCppFileTemplate_h");
  74. });
  75. }
  76. static bool create (Project::Item parent, const File& newFile, const char* templateName)
  77. {
  78. if (fillInNewCppFileTemplate (newFile, parent, templateName))
  79. {
  80. parent.addFileRetainingSortOrder (newFile, true);
  81. return true;
  82. }
  83. showFailedToWriteMessage (newFile);
  84. return false;
  85. }
  86. };
  87. //==============================================================================
  88. class NewCppAndHeaderFileWizard : public NewFileWizard::Type
  89. {
  90. public:
  91. String getName() override { return "CPP & Header File"; }
  92. void createNewFile (Project&, Project::Item parent) override
  93. {
  94. askUserToChooseNewFile ("SourceCode.h", "*.h;*.cpp", parent, [parent] (File newFile)
  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. String getName() override { return "Component class (split between a CPP & header)"; }
  106. void createNewFile (Project&, Project::Item parent) override
  107. {
  108. createNewFileInternal (parent);
  109. }
  110. static bool create (const String& className, Project::Item parent,
  111. const File& newFile, const char* templateName)
  112. {
  113. auto content = fillInBasicTemplateFields (newFile, parent, templateName)
  114. .replace ("%%component_class%%", className)
  115. .replace ("%%include_juce%%", CodeHelpers::createIncludePathIncludeStatement (Project::getJuceSourceHFilename()));
  116. content = replaceLineFeeds (content, parent.project.getProjectLineFeed());
  117. if (build_tools::overwriteFileWithNewDataIfDifferent (newFile, content))
  118. {
  119. parent.addFileRetainingSortOrder (newFile, true);
  120. return true;
  121. }
  122. showFailedToWriteMessage (newFile);
  123. return false;
  124. }
  125. private:
  126. virtual void createFiles (Project::Item parent, const String& className, const File& newFile)
  127. {
  128. if (create (className, parent, newFile.withFileExtension ("h"), "jucer_NewComponentTemplate_h"))
  129. create (className, parent, newFile.withFileExtension ("cpp"), "jucer_NewComponentTemplate_cpp");
  130. }
  131. static String getClassNameFieldName() { return "Class Name"; }
  132. void createNewFileInternal (Project::Item parent)
  133. {
  134. asyncAlertWindow = std::make_unique<AlertWindow> (TRANS ("Create new Component class"),
  135. TRANS ("Please enter the name for the new class"),
  136. MessageBoxIconType::NoIcon, nullptr);
  137. asyncAlertWindow->addTextEditor (getClassNameFieldName(), String(), String(), false);
  138. asyncAlertWindow->addButton (TRANS ("Create Files"), 1, KeyPress (KeyPress::returnKey));
  139. asyncAlertWindow->addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  140. auto resultCallback = [safeThis = WeakReference<NewComponentFileWizard> { this }, parent] (int result)
  141. {
  142. if (safeThis == nullptr)
  143. return;
  144. auto& aw = *(safeThis->asyncAlertWindow);
  145. aw.exitModalState (result);
  146. aw.setVisible (false);
  147. if (result == 0)
  148. return;
  149. const String className (aw.getTextEditorContents (getClassNameFieldName()).trim());
  150. if (className == build_tools::makeValidIdentifier (className, false, true, false))
  151. {
  152. safeThis->askUserToChooseNewFile (className + ".h", "*.h;*.cpp",
  153. parent,
  154. [safeThis, parent, className] (File newFile)
  155. {
  156. if (safeThis == nullptr)
  157. return;
  158. if (newFile != File())
  159. safeThis->createFiles (parent, className, newFile);
  160. });
  161. return;
  162. }
  163. safeThis->createNewFileInternal (parent);
  164. };
  165. asyncAlertWindow->enterModalState (true, ModalCallbackFunction::create (std::move (resultCallback)), false);
  166. }
  167. std::unique_ptr<AlertWindow> asyncAlertWindow;
  168. JUCE_DECLARE_WEAK_REFERENCEABLE (NewComponentFileWizard)
  169. };
  170. //==============================================================================
  171. class NewSingleFileComponentFileWizard : public NewComponentFileWizard
  172. {
  173. public:
  174. String getName() override { return "Component class (in a single source file)"; }
  175. void createFiles (Project::Item parent, const String& className, const File& newFile) override
  176. {
  177. create (className, parent, newFile.withFileExtension ("h"), "jucer_NewInlineComponentTemplate_h");
  178. }
  179. };
  180. //==============================================================================
  181. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  182. {
  183. AlertWindow::showMessageBoxAsync (MessageBoxIconType::WarningIcon,
  184. "Failed to Create File!",
  185. "Couldn't write to the file: " + file.getFullPathName());
  186. }
  187. void NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  188. const Project::Item& projectGroupToAddTo,
  189. std::function<void (File)> callback)
  190. {
  191. chooser = std::make_unique<FileChooser> ("Select File to Create",
  192. projectGroupToAddTo.determineGroupFolder()
  193. .getChildFile (suggestedFilename)
  194. .getNonexistentSibling(),
  195. wildcard);
  196. auto flags = FileBrowserComponent::saveMode
  197. | FileBrowserComponent::canSelectFiles
  198. | FileBrowserComponent::warnAboutOverwriting;
  199. chooser->launchAsync (flags, [callback] (const FileChooser& fc)
  200. {
  201. callback (fc.getResult());
  202. });
  203. }
  204. //==============================================================================
  205. NewFileWizard::NewFileWizard()
  206. {
  207. registerWizard (new NewCppFileWizard());
  208. registerWizard (new NewHeaderFileWizard());
  209. registerWizard (new NewCppAndHeaderFileWizard());
  210. registerWizard (new NewComponentFileWizard());
  211. registerWizard (new NewSingleFileComponentFileWizard());
  212. }
  213. NewFileWizard::~NewFileWizard()
  214. {
  215. }
  216. void NewFileWizard::addWizardsToMenu (PopupMenu& m) const
  217. {
  218. for (int i = 0; i < wizards.size(); ++i)
  219. m.addItem (menuBaseID + i, "Add New " + wizards.getUnchecked(i)->getName() + "...");
  220. }
  221. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, Project& project, const Project::Item& projectGroupToAddTo) const
  222. {
  223. if (Type* wiz = wizards [chosenMenuItemID - menuBaseID])
  224. {
  225. wiz->createNewFile (project, projectGroupToAddTo);
  226. return true;
  227. }
  228. return false;
  229. }
  230. void NewFileWizard::registerWizard (Type* newWizard)
  231. {
  232. wizards.add (newWizard);
  233. }