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.

186 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_NewFileWizard.h"
  19. //==============================================================================
  20. namespace
  21. {
  22. bool fillInNewCppFileTemplate (const File& file, const Project::Item& item, const char* templateName)
  23. {
  24. String s = 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. return FileHelpers::overwriteFileWithNewDataIfDifferent (file, s);
  30. }
  31. const int menuBaseID = 0x12d83f0;
  32. }
  33. //==============================================================================
  34. class NewCppFileWizard : public NewFileWizard::Type
  35. {
  36. public:
  37. NewCppFileWizard() {}
  38. String getName() { return "CPP File"; }
  39. void createNewFile (Project::Item parent)
  40. {
  41. const File newFile (askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent));
  42. if (newFile != File::nonexistent)
  43. create (parent, newFile);
  44. }
  45. static bool create (Project::Item parent, const File& newFile)
  46. {
  47. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_cpp"))
  48. {
  49. parent.addFile (newFile, 0, true);
  50. return true;
  51. }
  52. showFailedToWriteMessage (newFile);
  53. return false;
  54. }
  55. };
  56. //==============================================================================
  57. class NewHeaderFileWizard : public NewFileWizard::Type
  58. {
  59. public:
  60. NewHeaderFileWizard() {}
  61. String getName() { return "Header File"; }
  62. void createNewFile (Project::Item parent)
  63. {
  64. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h", parent));
  65. if (newFile != File::nonexistent)
  66. create (parent, newFile);
  67. }
  68. static bool create (Project::Item parent, const File& newFile)
  69. {
  70. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_h"))
  71. {
  72. parent.addFile (newFile, 0, true);
  73. return true;
  74. }
  75. showFailedToWriteMessage (newFile);
  76. return false;
  77. }
  78. };
  79. //==============================================================================
  80. class NewCppAndHeaderFileWizard : public NewFileWizard::Type
  81. {
  82. public:
  83. NewCppAndHeaderFileWizard() {}
  84. String getName() { return "CPP & Header File"; }
  85. void createNewFile (Project::Item parent)
  86. {
  87. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h;*.cpp", parent));
  88. if (newFile != File::nonexistent)
  89. {
  90. if (NewHeaderFileWizard::create (parent, newFile.withFileExtension ("h")))
  91. NewCppFileWizard::create (parent, newFile.withFileExtension ("cpp"));
  92. }
  93. }
  94. };
  95. //==============================================================================
  96. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  97. {
  98. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  99. "Failed to Create File!",
  100. "Couldn't write to the file: " + file.getFullPathName());
  101. }
  102. File NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  103. const Project::Item& projectGroupToAddTo)
  104. {
  105. FileChooser fc ("Select File to Create",
  106. projectGroupToAddTo.determineGroupFolder()
  107. .getChildFile (suggestedFilename)
  108. .getNonexistentSibling(),
  109. wildcard);
  110. if (fc.browseForFileToSave (true))
  111. return fc.getResult();
  112. return File::nonexistent;
  113. }
  114. //==============================================================================
  115. NewFileWizard::NewFileWizard()
  116. {
  117. registerWizard (new NewCppFileWizard());
  118. registerWizard (new NewHeaderFileWizard());
  119. registerWizard (new NewCppAndHeaderFileWizard());
  120. }
  121. NewFileWizard::~NewFileWizard()
  122. {
  123. clearSingletonInstance();
  124. }
  125. juce_ImplementSingleton_SingleThreaded (NewFileWizard)
  126. void NewFileWizard::addWizardsToMenu (PopupMenu& m) const
  127. {
  128. for (int i = 0; i < wizards.size(); ++i)
  129. m.addItem (menuBaseID + i, "Add New " + wizards.getUnchecked(i)->getName() + "...");
  130. }
  131. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, const Project::Item& projectGroupToAddTo) const
  132. {
  133. Type* wiz = wizards [chosenMenuItemID - menuBaseID];
  134. if (wiz != nullptr)
  135. {
  136. wiz->createNewFile (projectGroupToAddTo);
  137. return true;
  138. }
  139. return false;
  140. }
  141. void NewFileWizard::registerWizard (Type* newWizard)
  142. {
  143. wizards.add (newWizard);
  144. }