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.

297 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 final : 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, [this, parent] (File newFile)
  52. {
  53. if (newFile != File())
  54. create (*this, parent, newFile, "jucer_NewCppFileTemplate_cpp");
  55. });
  56. }
  57. static bool create (NewFileWizard::Type& wizard, 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. wizard.showFailedToWriteMessage (newFile);
  65. return false;
  66. }
  67. };
  68. //==============================================================================
  69. class NewHeaderFileWizard final : 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, [this, parent] (File newFile)
  76. {
  77. if (newFile != File())
  78. create (*this, parent, newFile, "jucer_NewCppFileTemplate_h");
  79. });
  80. }
  81. static bool create (NewFileWizard::Type& wizard, 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. wizard.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, [this, parent] (File newFile)
  100. {
  101. if (NewCppFileWizard::create (*this, parent, newFile.withFileExtension ("h"), "jucer_NewCppFileTemplate_h"))
  102. NewCppFileWizard::create (*this, 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 (NewFileWizard::Type& wizard, 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. wizard.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 (*this, className, parent, newFile.withFileExtension ("h"), "jucer_NewComponentTemplate_h"))
  134. create (*this, 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. MessageBoxIconType::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. auto resultCallback = [safeThis = WeakReference<NewComponentFileWizard> { this }, parent] (int result)
  146. {
  147. if (safeThis == nullptr)
  148. return;
  149. auto& aw = *(safeThis->asyncAlertWindow);
  150. aw.exitModalState (result);
  151. aw.setVisible (false);
  152. if (result == 0)
  153. return;
  154. const String className (aw.getTextEditorContents (getClassNameFieldName()).trim());
  155. if (className == build_tools::makeValidIdentifier (className, false, true, false))
  156. {
  157. safeThis->askUserToChooseNewFile (className + ".h", "*.h;*.cpp",
  158. parent,
  159. [safeThis, parent, className] (File newFile)
  160. {
  161. if (safeThis == nullptr)
  162. return;
  163. if (newFile != File())
  164. safeThis->createFiles (parent, className, newFile);
  165. });
  166. return;
  167. }
  168. safeThis->createNewFileInternal (parent);
  169. };
  170. asyncAlertWindow->enterModalState (true, ModalCallbackFunction::create (std::move (resultCallback)), false);
  171. }
  172. std::unique_ptr<AlertWindow> asyncAlertWindow;
  173. JUCE_DECLARE_WEAK_REFERENCEABLE (NewComponentFileWizard)
  174. };
  175. //==============================================================================
  176. class NewSingleFileComponentFileWizard final : public NewComponentFileWizard
  177. {
  178. public:
  179. String getName() override { return "Component class (in a single source file)"; }
  180. void createFiles (Project::Item parent, const String& className, const File& newFile) override
  181. {
  182. create (*this, className, parent, newFile.withFileExtension ("h"), "jucer_NewInlineComponentTemplate_h");
  183. }
  184. };
  185. //==============================================================================
  186. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  187. {
  188. auto options = MessageBoxOptions::makeOptionsOk (MessageBoxIconType::WarningIcon,
  189. "Failed to Create File!",
  190. "Couldn't write to the file: " + file.getFullPathName());
  191. messageBox = AlertWindow::showScopedAsync (options, nullptr);
  192. }
  193. void NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  194. const Project::Item& projectGroupToAddTo,
  195. std::function<void (File)> callback)
  196. {
  197. chooser = std::make_unique<FileChooser> ("Select File to Create",
  198. projectGroupToAddTo.determineGroupFolder()
  199. .getChildFile (suggestedFilename)
  200. .getNonexistentSibling(),
  201. wildcard);
  202. auto flags = FileBrowserComponent::saveMode
  203. | FileBrowserComponent::canSelectFiles
  204. | FileBrowserComponent::warnAboutOverwriting;
  205. chooser->launchAsync (flags, [callback] (const FileChooser& fc)
  206. {
  207. callback (fc.getResult());
  208. });
  209. }
  210. //==============================================================================
  211. NewFileWizard::NewFileWizard()
  212. {
  213. registerWizard (new NewCppFileWizard());
  214. registerWizard (new NewHeaderFileWizard());
  215. registerWizard (new NewCppAndHeaderFileWizard());
  216. registerWizard (new NewComponentFileWizard());
  217. registerWizard (new NewSingleFileComponentFileWizard());
  218. }
  219. NewFileWizard::~NewFileWizard()
  220. {
  221. }
  222. void NewFileWizard::addWizardsToMenu (PopupMenu& m) const
  223. {
  224. for (int i = 0; i < wizards.size(); ++i)
  225. m.addItem (menuBaseID + i, "Add New " + wizards.getUnchecked (i)->getName() + "...");
  226. }
  227. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, Project& project, const Project::Item& projectGroupToAddTo) const
  228. {
  229. if (Type* wiz = wizards [chosenMenuItemID - menuBaseID])
  230. {
  231. wiz->createNewFile (project, projectGroupToAddTo);
  232. return true;
  233. }
  234. return false;
  235. }
  236. void NewFileWizard::registerWizard (Type* newWizard)
  237. {
  238. wizards.add (newWizard);
  239. }