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.

282 lines
9.5KB

  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. {
  115. return saveAsInteractive (true);
  116. }
  117. else
  118. {
  119. // can't save to an unspecified file
  120. jassertfalse;
  121. return failedToWriteToFile;
  122. }
  123. }
  124. if (warnAboutOverwritingExistingFiles && newFile.exists())
  125. {
  126. if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  127. TRANS("File already exists"),
  128. TRANS("There's already a file called:\n\n")
  129. + newFile.getFullPathName()
  130. + TRANS("\n\nAre you sure you want to overwrite it?"),
  131. TRANS("overwrite"),
  132. TRANS("cancel")))
  133. {
  134. return userCancelledSave;
  135. }
  136. }
  137. MouseCursor::showWaitCursor();
  138. const File oldFile (documentFile);
  139. documentFile = newFile;
  140. const Result result (saveDocument (newFile));
  141. if (result.wasOk())
  142. {
  143. setChangedFlag (false);
  144. MouseCursor::hideWaitCursor();
  145. return savedOk;
  146. }
  147. documentFile = oldFile;
  148. MouseCursor::hideWaitCursor();
  149. if (showMessageOnFailure)
  150. {
  151. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  152. TRANS("Error writing to file..."),
  153. TRANS("An error occurred while trying to save \"")
  154. + getDocumentTitle()
  155. + TRANS("\" to the file:\n\n")
  156. + newFile.getFullPathName()
  157. + "\n\n"
  158. + result.getErrorMessage());
  159. }
  160. return failedToWriteToFile;
  161. }
  162. FileBasedDocument::SaveResult FileBasedDocument::saveIfNeededAndUserAgrees()
  163. {
  164. if (! hasChangedSinceSaved())
  165. return savedOk;
  166. const int r = AlertWindow::showYesNoCancelBox (AlertWindow::QuestionIcon,
  167. TRANS("Closing document..."),
  168. TRANS("Do you want to save the changes to \"")
  169. + getDocumentTitle() + "\"?",
  170. TRANS("save"),
  171. TRANS("discard changes"),
  172. TRANS("cancel"));
  173. if (r == 1)
  174. {
  175. // save changes
  176. return save (true, true);
  177. }
  178. else if (r == 2)
  179. {
  180. // discard changes
  181. return savedOk;
  182. }
  183. return userCancelledSave;
  184. }
  185. File FileBasedDocument::getSuggestedSaveAsFile (const File& defaultFile)
  186. {
  187. return defaultFile.withFileExtension (fileExtension).getNonexistentSibling (true);
  188. }
  189. FileBasedDocument::SaveResult FileBasedDocument::saveAsInteractive (const bool warnAboutOverwritingExistingFiles)
  190. {
  191. File f;
  192. if (documentFile.existsAsFile())
  193. f = documentFile;
  194. else
  195. f = getLastDocumentOpened();
  196. String legalFilename (File::createLegalFileName (getDocumentTitle()));
  197. if (legalFilename.isEmpty())
  198. legalFilename = "unnamed";
  199. if (f.existsAsFile() || f.getParentDirectory().isDirectory())
  200. f = f.getSiblingFile (legalFilename);
  201. else
  202. f = File::getSpecialLocation (File::userDocumentsDirectory).getChildFile (legalFilename);
  203. f = getSuggestedSaveAsFile (f);
  204. FileChooser fc (saveFileDialogTitle, f, fileWildcard);
  205. if (fc.browseForFileToSave (warnAboutOverwritingExistingFiles))
  206. {
  207. File chosen (fc.getResult());
  208. if (chosen.getFileExtension().isEmpty())
  209. {
  210. chosen = chosen.withFileExtension (fileExtension);
  211. if (chosen.exists())
  212. {
  213. if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  214. TRANS("File already exists"),
  215. TRANS("There's already a file called:")
  216. + "\n\n" + chosen.getFullPathName()
  217. + "\n\n" + TRANS("Are you sure you want to overwrite it?"),
  218. TRANS("overwrite"),
  219. TRANS("cancel")))
  220. {
  221. return userCancelledSave;
  222. }
  223. }
  224. }
  225. setLastDocumentOpened (chosen);
  226. return saveAs (chosen, false, false, true);
  227. }
  228. return userCancelledSave;
  229. }
  230. #endif