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.

273 lines
9.4KB

  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. FileBasedDocument::FileBasedDocument (const String& fileExtension_,
  19. const String& fileWildcard_,
  20. const String& openFileDialogTitle_,
  21. const String& saveFileDialogTitle_)
  22. : changedSinceSave (false),
  23. fileExtension (fileExtension_),
  24. fileWildcard (fileWildcard_),
  25. openFileDialogTitle (openFileDialogTitle_),
  26. saveFileDialogTitle (saveFileDialogTitle_)
  27. {
  28. }
  29. FileBasedDocument::~FileBasedDocument()
  30. {
  31. }
  32. //==============================================================================
  33. void FileBasedDocument::setChangedFlag (const bool hasChanged)
  34. {
  35. if (changedSinceSave != hasChanged)
  36. {
  37. changedSinceSave = hasChanged;
  38. sendChangeMessage();
  39. }
  40. }
  41. void FileBasedDocument::changed()
  42. {
  43. changedSinceSave = true;
  44. sendChangeMessage();
  45. }
  46. //==============================================================================
  47. void FileBasedDocument::setFile (const File& newFile)
  48. {
  49. if (documentFile != newFile)
  50. {
  51. documentFile = newFile;
  52. changed();
  53. }
  54. }
  55. //==============================================================================
  56. #if JUCE_MODAL_LOOPS_PERMITTED
  57. bool FileBasedDocument::loadFrom (const File& newFile,
  58. const bool showMessageOnFailure)
  59. {
  60. MouseCursor::showWaitCursor();
  61. const File oldFile (documentFile);
  62. documentFile = newFile;
  63. Result result (Result::fail ("The file doesn't exist"));
  64. if (newFile.existsAsFile())
  65. {
  66. result = loadDocument (newFile);
  67. if (result.wasOk())
  68. {
  69. setChangedFlag (false);
  70. MouseCursor::hideWaitCursor();
  71. setLastDocumentOpened (newFile);
  72. return true;
  73. }
  74. }
  75. documentFile = oldFile;
  76. MouseCursor::hideWaitCursor();
  77. if (showMessageOnFailure)
  78. {
  79. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  80. TRANS("Failed to open file..."),
  81. TRANS("There was an error while trying to load the file:\n\n")
  82. + newFile.getFullPathName()
  83. + "\n\n"
  84. + result.getErrorMessage());
  85. }
  86. return false;
  87. }
  88. bool FileBasedDocument::loadFromUserSpecifiedFile (const bool showMessageOnFailure)
  89. {
  90. FileChooser fc (openFileDialogTitle,
  91. getLastDocumentOpened(),
  92. fileWildcard);
  93. if (fc.browseForFileToOpen())
  94. return loadFrom (fc.getResult(), showMessageOnFailure);
  95. return false;
  96. }
  97. //==============================================================================
  98. FileBasedDocument::SaveResult FileBasedDocument::save (const bool askUserForFileIfNotSpecified,
  99. const bool showMessageOnFailure)
  100. {
  101. return saveAs (documentFile,
  102. false,
  103. askUserForFileIfNotSpecified,
  104. showMessageOnFailure);
  105. }
  106. FileBasedDocument::SaveResult FileBasedDocument::saveAs (const File& newFile,
  107. const bool warnAboutOverwritingExistingFiles,
  108. const bool askUserForFileIfNotSpecified,
  109. const bool showMessageOnFailure)
  110. {
  111. if (newFile == File::nonexistent)
  112. {
  113. if (askUserForFileIfNotSpecified)
  114. return saveAsInteractive (true);
  115. // can't save to an unspecified file
  116. jassertfalse;
  117. return failedToWriteToFile;
  118. }
  119. if (warnAboutOverwritingExistingFiles && newFile.exists())
  120. {
  121. if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  122. TRANS("File already exists"),
  123. TRANS("There's already a file called:\n\n")
  124. + newFile.getFullPathName()
  125. + TRANS("\n\nAre you sure you want to overwrite it?"),
  126. TRANS("overwrite"),
  127. TRANS("cancel")))
  128. {
  129. return userCancelledSave;
  130. }
  131. }
  132. MouseCursor::showWaitCursor();
  133. const File oldFile (documentFile);
  134. documentFile = newFile;
  135. const Result result (saveDocument (newFile));
  136. if (result.wasOk())
  137. {
  138. setChangedFlag (false);
  139. MouseCursor::hideWaitCursor();
  140. return savedOk;
  141. }
  142. documentFile = oldFile;
  143. MouseCursor::hideWaitCursor();
  144. if (showMessageOnFailure)
  145. {
  146. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  147. TRANS("Error writing to file..."),
  148. TRANS("An error occurred while trying to save \"")
  149. + getDocumentTitle()
  150. + TRANS("\" to the file:\n\n")
  151. + newFile.getFullPathName()
  152. + "\n\n"
  153. + result.getErrorMessage());
  154. }
  155. return failedToWriteToFile;
  156. }
  157. FileBasedDocument::SaveResult FileBasedDocument::saveIfNeededAndUserAgrees()
  158. {
  159. if (! hasChangedSinceSaved())
  160. return savedOk;
  161. const int r = AlertWindow::showYesNoCancelBox (AlertWindow::QuestionIcon,
  162. TRANS("Closing document..."),
  163. TRANS("Do you want to save the changes to \"")
  164. + getDocumentTitle() + "\"?",
  165. TRANS("save"),
  166. TRANS("discard changes"),
  167. TRANS("cancel"));
  168. if (r == 1) // save changes
  169. return save (true, true);
  170. if (r == 2) // discard changes
  171. return savedOk;
  172. return userCancelledSave;
  173. }
  174. File FileBasedDocument::getSuggestedSaveAsFile (const File& defaultFile)
  175. {
  176. return defaultFile.withFileExtension (fileExtension).getNonexistentSibling (true);
  177. }
  178. FileBasedDocument::SaveResult FileBasedDocument::saveAsInteractive (const bool warnAboutOverwritingExistingFiles)
  179. {
  180. File f;
  181. if (documentFile.existsAsFile())
  182. f = documentFile;
  183. else
  184. f = getLastDocumentOpened();
  185. String legalFilename (File::createLegalFileName (getDocumentTitle()));
  186. if (legalFilename.isEmpty())
  187. legalFilename = "unnamed";
  188. if (f.existsAsFile() || f.getParentDirectory().isDirectory())
  189. f = f.getSiblingFile (legalFilename);
  190. else
  191. f = File::getSpecialLocation (File::userDocumentsDirectory).getChildFile (legalFilename);
  192. f = getSuggestedSaveAsFile (f);
  193. FileChooser fc (saveFileDialogTitle, f, fileWildcard);
  194. if (fc.browseForFileToSave (warnAboutOverwritingExistingFiles))
  195. {
  196. File chosen (fc.getResult());
  197. if (chosen.getFileExtension().isEmpty())
  198. {
  199. chosen = chosen.withFileExtension (fileExtension);
  200. if (chosen.exists())
  201. {
  202. if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  203. TRANS("File already exists"),
  204. TRANS("There's already a file called:")
  205. + "\n\n" + chosen.getFullPathName()
  206. + "\n\n" + TRANS("Are you sure you want to overwrite it?"),
  207. TRANS("overwrite"),
  208. TRANS("cancel")))
  209. {
  210. return userCancelledSave;
  211. }
  212. }
  213. }
  214. setLastDocumentOpened (chosen);
  215. return saveAs (chosen, false, false, true);
  216. }
  217. return userCancelledSave;
  218. }
  219. #endif