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.

234 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. #include "jucer_DrawableDocument.h"
  20. #include "jucer_ComponentDocument.h"
  21. //==============================================================================
  22. static bool fillInNewCppFileTemplate (const File& file, const Project::Item& item, const char* templateName)
  23. {
  24. String s = item.getProject().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", makeHeaderGuardName (file), false);
  29. return overwriteFileWithNewDataIfDifferent (file, s);
  30. }
  31. //==============================================================================
  32. class NewCppFileWizard : public NewFileWizard
  33. {
  34. public:
  35. NewCppFileWizard() {}
  36. ~NewCppFileWizard() {}
  37. const String getName() { return "CPP File"; }
  38. void createNewFile (Project::Item parent)
  39. {
  40. File newFile = askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent);
  41. if (newFile != File::nonexistent)
  42. {
  43. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_cpp"))
  44. parent.addFile (newFile, 0);
  45. else
  46. showFailedToWriteMessage (newFile);
  47. }
  48. }
  49. };
  50. //==============================================================================
  51. class NewHeaderFileWizard : public NewFileWizard
  52. {
  53. public:
  54. NewHeaderFileWizard() {}
  55. ~NewHeaderFileWizard() {}
  56. const String getName() { return "Header File"; }
  57. void createNewFile (Project::Item parent)
  58. {
  59. File newFile = askUserToChooseNewFile ("SourceCode.h", "*.h", parent);
  60. if (newFile != File::nonexistent)
  61. {
  62. if (fillInNewCppFileTemplate (newFile, parent, "jucer_NewCppFileTemplate_h"))
  63. parent.addFile (newFile, 0);
  64. else
  65. showFailedToWriteMessage (newFile);
  66. }
  67. }
  68. };
  69. //==============================================================================
  70. class NewComponentWizard : public NewFileWizard
  71. {
  72. public:
  73. NewComponentWizard() {}
  74. ~NewComponentWizard() {}
  75. const String getName() { return "Component"; }
  76. void createNewFile (Project::Item parent)
  77. {
  78. File cppFile = askUserToChooseNewFile ("Component.cpp", "*.cpp", parent);
  79. if (cppFile != File::nonexistent)
  80. {
  81. File header (cppFile.withFileExtension (".h"));
  82. if (header.exists())
  83. {
  84. if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon, "Create New Component",
  85. "The file " + header.getFileName() + " already exists...\n\nDo you want to overwrite it?",
  86. "Overwrite", "Cancel"))
  87. return;
  88. }
  89. ComponentDocument doc (&parent.getProject(), cppFile);
  90. if (doc.save())
  91. {
  92. parent.addFile (header, 0);
  93. parent.addFile (cppFile, 0);
  94. }
  95. else
  96. {
  97. showFailedToWriteMessage (cppFile);
  98. }
  99. }
  100. }
  101. };
  102. //==============================================================================
  103. class NewDrawableWizard : public NewFileWizard
  104. {
  105. public:
  106. NewDrawableWizard() {}
  107. ~NewDrawableWizard() {}
  108. const String getName() { return "Drawable"; }
  109. void createNewFile (Project::Item parent)
  110. {
  111. File newFile = askUserToChooseNewFile ("New Drawable.drawable", "*.drawable", parent);
  112. if (newFile != File::nonexistent)
  113. {
  114. DrawableDocument newDrawable (&(parent.getProject()), newFile);
  115. if (newDrawable.save())
  116. parent.addFile (newFile, 0);
  117. else
  118. showFailedToWriteMessage (newFile);
  119. }
  120. }
  121. };
  122. //==============================================================================
  123. const StringArray NewFileWizard::getWizards()
  124. {
  125. StringArray s;
  126. for (int i = 0; i < getNumWizards(); ++i)
  127. {
  128. ScopedPointer <NewFileWizard> wiz (createWizard (i));
  129. s.add (wiz->getName());
  130. }
  131. return s;
  132. }
  133. int NewFileWizard::getNumWizards()
  134. {
  135. return 2;
  136. }
  137. NewFileWizard* NewFileWizard::createWizard (int index)
  138. {
  139. switch (index)
  140. {
  141. case 0: return new NewCppFileWizard();
  142. case 1: return new NewHeaderFileWizard();
  143. case 2: return new NewComponentWizard();
  144. case 3: return new NewDrawableWizard();
  145. default: jassertfalse; break;
  146. }
  147. return 0;
  148. }
  149. static const int menuBaseID = 0x12d83f0;
  150. void NewFileWizard::addWizardsToMenu (PopupMenu& m)
  151. {
  152. for (int i = 0; i < getNumWizards(); ++i)
  153. {
  154. ScopedPointer <NewFileWizard> wiz (createWizard (i));
  155. m.addItem (menuBaseID + i, "Add New " + wiz->getName() + "...");
  156. }
  157. }
  158. bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID, const Project::Item& projectGroupToAddTo)
  159. {
  160. int index = chosenMenuItemID - menuBaseID;
  161. if (index >= 0 && index < getNumWizards())
  162. {
  163. ScopedPointer <NewFileWizard> wiz (createWizard (index));
  164. wiz->createNewFile (projectGroupToAddTo);
  165. return true;
  166. }
  167. return false;
  168. }
  169. void NewFileWizard::showFailedToWriteMessage (const File& file)
  170. {
  171. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  172. "Failed to Create File!",
  173. "Couldn't write to the file: " + file.getFullPathName());
  174. }
  175. const File NewFileWizard::askUserToChooseNewFile (const String& suggestedFilename, const String& wildcard,
  176. const Project::Item& projectGroupToAddTo)
  177. {
  178. FileChooser fc ("Select File to Create",
  179. projectGroupToAddTo.determineGroupFolder()
  180. .getChildFile (suggestedFilename)
  181. .getNonexistentSibling(),
  182. wildcard);
  183. if (fc.browseForFileToSave (true))
  184. return fc.getResult();
  185. return File::nonexistent;
  186. }