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.

185 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. static bool fillInNewCppFileTemplate (const File& file, const Project::Item& item, const char* templateName)
  21. {
  22. String s = item.getProject().getFileTemplate (templateName)
  23. .replace ("FILENAME", file.getFileName(), false)
  24. .replace ("DATE", Time::getCurrentTime().toString (true, true, true), false)
  25. .replace ("AUTHOR", SystemStats::getFullUserName(), false)
  26. .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (file), false);
  27. return FileHelpers::overwriteFileWithNewDataIfDifferent (file, s);
  28. }
  29. //==============================================================================
  30. class NewCppFileWizard : public NewFileWizard::Type
  31. {
  32. public:
  33. NewCppFileWizard() {}
  34. ~NewCppFileWizard() {}
  35. const String getName() { return "CPP File"; }
  36. void createNewFile (Project::Item parent)
  37. {
  38. const File newFile (askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent));
  39. if (newFile != File::nonexistent)
  40. create (parent, newFile);
  41. }
  42. static bool create (Project::Item parent, const File& newFile)
  43. {
  44. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_cpp"))
  45. {
  46. parent.addFile (newFile, 0);
  47. return true;
  48. }
  49. showFailedToWriteMessage (newFile);
  50. return false;
  51. }
  52. };
  53. //==============================================================================
  54. class NewHeaderFileWizard : public NewFileWizard::Type
  55. {
  56. public:
  57. NewHeaderFileWizard() {}
  58. ~NewHeaderFileWizard() {}
  59. const String getName() { return "Header File"; }
  60. void createNewFile (Project::Item parent)
  61. {
  62. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h", parent));
  63. if (newFile != File::nonexistent)
  64. create (parent, newFile);
  65. }
  66. static bool create (Project::Item parent, const File& newFile)
  67. {
  68. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_h"))
  69. {
  70. parent.addFile (newFile, 0);
  71. return true;
  72. }
  73. showFailedToWriteMessage (newFile);
  74. return false;
  75. }
  76. };
  77. //==============================================================================
  78. class NewCppAndHeaderFileWizard : public NewFileWizard::Type
  79. {
  80. public:
  81. NewCppAndHeaderFileWizard() {}
  82. ~NewCppAndHeaderFileWizard() {}
  83. const String getName() { return "CPP & Header File"; }
  84. void createNewFile (Project::Item parent)
  85. {
  86. const File newFile (askUserToChooseNewFile ("SourceCode.h", "*.h;*.cpp", parent));
  87. if (newFile != File::nonexistent)
  88. {
  89. if (NewHeaderFileWizard::create (parent, newFile.withFileExtension ("h")))
  90. NewCppFileWizard::create (parent, newFile.withFileExtension ("cpp"));
  91. }
  92. }
  93. };
  94. //==============================================================================
  95. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  96. {
  97. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  98. "Failed to Create File!",
  99. "Couldn't write to the file: " + file.getFullPathName());
  100. }
  101. const File NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  102. const Project::Item& projectGroupToAddTo)
  103. {
  104. FileChooser fc ("Select File to Create",
  105. projectGroupToAddTo.determineGroupFolder()
  106. .getChildFile (suggestedFilename)
  107. .getNonexistentSibling(),
  108. wildcard);
  109. if (fc.browseForFileToSave (true))
  110. return fc.getResult();
  111. return File::nonexistent;
  112. }
  113. //==============================================================================
  114. NewFileWizard::NewFileWizard()
  115. {
  116. registerWizard (new NewCppFileWizard());
  117. registerWizard (new NewHeaderFileWizard());
  118. registerWizard (new NewCppAndHeaderFileWizard());
  119. }
  120. NewFileWizard::~NewFileWizard()
  121. {
  122. clearSingletonInstance();
  123. }
  124. juce_ImplementSingleton_SingleThreaded (NewFileWizard)
  125. static const int menuBaseID = 0x12d83f0;
  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 != 0)
  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. }