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.

150 lines
5.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. File newFile = askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent);
  39. if (newFile != File::nonexistent)
  40. {
  41. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_cpp"))
  42. parent.addFile (newFile, 0);
  43. else
  44. showFailedToWriteMessage (newFile);
  45. }
  46. }
  47. };
  48. //==============================================================================
  49. class NewHeaderFileWizard : public NewFileWizard::Type
  50. {
  51. public:
  52. NewHeaderFileWizard() {}
  53. ~NewHeaderFileWizard() {}
  54. const String getName() { return "Header File"; }
  55. void createNewFile (Project::Item parent)
  56. {
  57. File newFile = askUserToChooseNewFile ("SourceCode.h", "*.h", parent);
  58. if (newFile != File::nonexistent)
  59. {
  60. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_h"))
  61. parent.addFile (newFile, 0);
  62. else
  63. showFailedToWriteMessage (newFile);
  64. }
  65. }
  66. };
  67. //==============================================================================
  68. void NewFileWizard::Type::showFailedToWriteMessage (const File& file)
  69. {
  70. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  71. "Failed to Create File!",
  72. "Couldn't write to the file: " + file.getFullPathName());
  73. }
  74. const File NewFileWizard::Type::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  75. const Project::Item& projectGroupToAddTo)
  76. {
  77. FileChooser fc ("Select File to Create",
  78. projectGroupToAddTo.determineGroupFolder()
  79. .getChildFile (suggestedFilename)
  80. .getNonexistentSibling(),
  81. wildcard);
  82. if (fc.browseForFileToSave (true))
  83. return fc.getResult();
  84. return File::nonexistent;
  85. }
  86. //==============================================================================
  87. NewFileWizard::NewFileWizard()
  88. {
  89. registerWizard (new NewCppFileWizard());
  90. registerWizard (new NewHeaderFileWizard());
  91. }
  92. NewFileWizard::~NewFileWizard()
  93. {
  94. clearSingletonInstance();
  95. }
  96. juce_ImplementSingleton_SingleThreaded (NewFileWizard)
  97. static const int menuBaseID = 0x12d83f0;
  98. void NewFileWizard::addWizardsToMenu (PopupMenu& m) const
  99. {
  100. for (int i = 0; i < wizards.size(); ++i)
  101. m.addItem (menuBaseID + i, "Add New " + wizards.getUnchecked(i)->getName() + "...");
  102. }
  103. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, const Project::Item& projectGroupToAddTo) const
  104. {
  105. Type* wiz = wizards [chosenMenuItemID - menuBaseID];
  106. if (wiz != 0)
  107. {
  108. wiz->createNewFile (projectGroupToAddTo);
  109. return true;
  110. }
  111. return false;
  112. }
  113. void NewFileWizard::registerWizard (Type* newWizard)
  114. {
  115. wizards.add (newWizard);
  116. }