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.

293 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. /**
  22. A class to take care of the logic involved with the loading/saving of some kind
  23. of document.
  24. There's quite a lot of tedious logic involved in writing all the load/save/save-as
  25. functions you need for documents that get saved to a file, so this class attempts
  26. to abstract most of the boring stuff.
  27. Your subclass should just implement all the pure virtual methods, and you can
  28. then use the higher-level public methods to do the load/save dialogs, to warn the user
  29. about overwriting files, etc.
  30. The document object keeps track of whether it has changed since it was last saved or
  31. loaded, so when you change something, call its changed() method. This will set a
  32. flag so it knows it needs saving, and will also broadcast a change message using the
  33. ChangeBroadcaster base class.
  34. @see ChangeBroadcaster
  35. */
  36. class JUCE_API FileBasedDocument : public ChangeBroadcaster
  37. {
  38. public:
  39. /** Creates a FileBasedDocument.
  40. @param fileExtension the extension to use when loading/saving files, e.g. ".doc"
  41. @param fileWildCard the wildcard to use in file dialogs, e.g. "*.doc"
  42. @param openFileDialogTitle the title to show on an open-file dialog, e.g. "Choose a file to open.."
  43. @param saveFileDialogTitle the title to show on an save-file dialog, e.g. "Choose a file to save as.."
  44. */
  45. FileBasedDocument (const String& fileExtension,
  46. const String& fileWildCard,
  47. const String& openFileDialogTitle,
  48. const String& saveFileDialogTitle);
  49. /** Destructor. */
  50. virtual ~FileBasedDocument();
  51. //==============================================================================
  52. /** Returns true if the changed() method has been called since the file was
  53. last saved or loaded.
  54. @see setChangedFlag, changed
  55. */
  56. bool hasChangedSinceSaved() const { return changedSinceSave; }
  57. /** Called to indicate that the document has changed and needs saving.
  58. This method will also trigger a change message to be sent out using the
  59. ChangeBroadcaster base class.
  60. After calling the method, the hasChangedSinceSaved() method will return true, until
  61. it is reset either by saving to a file or using the setChangedFlag() method.
  62. @see hasChangedSinceSaved, setChangedFlag
  63. */
  64. virtual void changed();
  65. /** Sets the state of the 'changed' flag.
  66. The 'changed' flag is set to true when the changed() method is called - use this method
  67. to reset it or to set it without also broadcasting a change message.
  68. @see changed, hasChangedSinceSaved
  69. */
  70. void setChangedFlag (bool hasChanged);
  71. //==============================================================================
  72. /** Tries to open a file.
  73. If the file opens correctly, the document's file (see the getFile() method) is set
  74. to this new one; if it fails, the document's file is left unchanged, and optionally
  75. a message box is shown telling the user there was an error.
  76. @returns A result indicating whether the new file loaded successfully, or the error
  77. message if it failed.
  78. @see loadDocument, loadFromUserSpecifiedFile
  79. */
  80. Result loadFrom (const File& fileToLoadFrom,
  81. bool showMessageOnFailure);
  82. /** Asks the user for a file and tries to load it.
  83. This will pop up a dialog box using the title, file extension and
  84. wildcard specified in the document's constructor, and asks the user
  85. for a file. If they pick one, the loadFrom() method is used to
  86. try to load it, optionally showing a message if it fails.
  87. @returns a result indicating success; This will be a failure message if the user
  88. cancelled or if they picked a file which failed to load correctly
  89. @see loadFrom
  90. */
  91. Result loadFromUserSpecifiedFile (bool showMessageOnFailure);
  92. //==============================================================================
  93. /** A set of possible outcomes of one of the save() methods
  94. */
  95. enum SaveResult
  96. {
  97. savedOk = 0, /**< indicates that a file was saved successfully. */
  98. userCancelledSave, /**< indicates that the user aborted the save operation. */
  99. failedToWriteToFile /**< indicates that it tried to write to a file but this failed. */
  100. };
  101. /** Tries to save the document to the last file it was saved or loaded from.
  102. This will always try to write to the file, even if the document isn't flagged as
  103. having changed.
  104. @param askUserForFileIfNotSpecified if there's no file currently specified and this is
  105. true, it will prompt the user to pick a file, as if
  106. saveAsInteractive() was called.
  107. @param showMessageOnFailure if true it will show a warning message when if the
  108. save operation fails
  109. @see saveIfNeededAndUserAgrees, saveAs, saveAsInteractive
  110. */
  111. SaveResult save (bool askUserForFileIfNotSpecified,
  112. bool showMessageOnFailure);
  113. /** If the file needs saving, it'll ask the user if that's what they want to do, and save
  114. it if they say yes.
  115. If you've got a document open and want to close it (e.g. to quit the app), this is the
  116. method to call.
  117. If the document doesn't need saving it'll return the value savedOk so
  118. you can go ahead and delete the document.
  119. If it does need saving it'll prompt the user, and if they say "discard changes" it'll
  120. return savedOk, so again, you can safely delete the document.
  121. If the user clicks "cancel", it'll return userCancelledSave, so if you can abort the
  122. close-document operation.
  123. And if they click "save changes", it'll try to save and either return savedOk, or
  124. failedToWriteToFile if there was a problem.
  125. @see save, saveAs, saveAsInteractive
  126. */
  127. SaveResult saveIfNeededAndUserAgrees();
  128. /** Tries to save the document to a specified file.
  129. If this succeeds, it'll also change the document's internal file (as returned by
  130. the getFile() method). If it fails, the file will be left unchanged.
  131. @param newFile the file to try to write to
  132. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  133. the user first if they want to overwrite it
  134. @param askUserForFileIfNotSpecified if the file is non-existent and this is true, it'll
  135. use the saveAsInteractive() method to ask the user for a
  136. filename
  137. @param showMessageOnFailure if true and the write operation fails, it'll show
  138. a message box to warn the user
  139. @see saveIfNeededAndUserAgrees, save, saveAsInteractive
  140. */
  141. SaveResult saveAs (const File& newFile,
  142. bool warnAboutOverwritingExistingFiles,
  143. bool askUserForFileIfNotSpecified,
  144. bool showMessageOnFailure);
  145. /** Prompts the user for a filename and tries to save to it.
  146. This will pop up a dialog box using the title, file extension and
  147. wildcard specified in the document's constructor, and asks the user
  148. for a file. If they pick one, the saveAs() method is used to try to save
  149. to this file.
  150. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  151. the user first if they want to overwrite it
  152. @see saveIfNeededAndUserAgrees, save, saveAs
  153. */
  154. SaveResult saveAsInteractive (bool warnAboutOverwritingExistingFiles);
  155. //==============================================================================
  156. /** Returns the file that this document was last successfully saved or loaded from.
  157. When the document object is created, this will be set to File().
  158. It is changed when one of the load or save methods is used, or when setFile()
  159. is used to explicitly set it.
  160. */
  161. const File& getFile() const { return documentFile; }
  162. /** Sets the file that this document thinks it was loaded from.
  163. This won't actually load anything - it just changes the file stored internally.
  164. @see getFile
  165. */
  166. void setFile (const File& newFile);
  167. protected:
  168. //==============================================================================
  169. /** Overload this to return the title of the document.
  170. This is used in message boxes, filenames and file choosers, so it should be
  171. something sensible.
  172. */
  173. virtual String getDocumentTitle() = 0;
  174. /** This method should try to load your document from the given file.
  175. @returns a Result object to indicate the whether there was an error.
  176. */
  177. virtual Result loadDocument (const File& file) = 0;
  178. /** This method should try to write your document to the given file.
  179. @returns a Result object to indicate the whether there was an error.
  180. */
  181. virtual Result saveDocument (const File& file) = 0;
  182. /** This is used for dialog boxes to make them open at the last folder you
  183. were using.
  184. getLastDocumentOpened() and setLastDocumentOpened() are used to store
  185. the last document that was used - you might want to store this value
  186. in a static variable, or even in your application's properties. It should
  187. be a global setting rather than a property of this object.
  188. This method works very well in conjunction with a RecentlyOpenedFilesList
  189. object to manage your recent-files list.
  190. As a default value, it's ok to return File(), and the document object will
  191. use a sensible one instead.
  192. @see RecentlyOpenedFilesList
  193. */
  194. virtual File getLastDocumentOpened() = 0;
  195. /** This is used for dialog boxes to make them open at the last folder you
  196. were using.
  197. getLastDocumentOpened() and setLastDocumentOpened() are used to store
  198. the last document that was used - you might want to store this value
  199. in a static variable, or even in your application's properties. It should
  200. be a global setting rather than a property of this object.
  201. This method works very well in conjunction with a RecentlyOpenedFilesList
  202. object to manage your recent-files list.
  203. @see RecentlyOpenedFilesList
  204. */
  205. virtual void setLastDocumentOpened (const File& file) = 0;
  206. #if JUCE_MODAL_LOOPS_PERMITTED
  207. /** This is called by saveAsInteractive() to allow you to optionally customise the
  208. filename that the user is presented with in the save dialog.
  209. The defaultFile parameter is an initial suggestion based on what the class knows
  210. about the current document - you can return a variation on this file with a different
  211. extension, etc, or just return something completely different.
  212. */
  213. virtual File getSuggestedSaveAsFile (const File& defaultFile);
  214. #endif
  215. private:
  216. //==============================================================================
  217. File documentFile;
  218. bool changedSinceSave;
  219. String fileExtension, fileWildcard, openFileDialogTitle, saveFileDialogTitle;
  220. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBasedDocument)
  221. };