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.

295 lines
11KB

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