Audio plugin host https://kx.studio/carla
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.

juce_FileBasedDocument.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. A class to take care of the logic involved with the loading/saving of some kind
  18. of document.
  19. There's quite a lot of tedious logic involved in writing all the load/save/save-as
  20. functions you need for documents that get saved to a file, so this class attempts
  21. to abstract most of the boring stuff.
  22. Your subclass should just implement all the pure virtual methods, and you can
  23. then use the higher-level public methods to do the load/save dialogs, to warn the user
  24. about overwriting files, etc.
  25. The document object keeps track of whether it has changed since it was last saved or
  26. loaded, so when you change something, call its changed() method. This will set a
  27. flag so it knows it needs saving, and will also broadcast a change message using the
  28. ChangeBroadcaster base class.
  29. @see ChangeBroadcaster
  30. @tags{GUI}
  31. */
  32. class JUCE_API FileBasedDocument : public ChangeBroadcaster
  33. {
  34. public:
  35. /** Creates a FileBasedDocument.
  36. @param fileExtension the extension to use when loading/saving files, e.g. ".doc"
  37. @param fileWildCard the wildcard to use in file dialogs, e.g. "*.doc"
  38. @param openFileDialogTitle the title to show on an open-file dialog, e.g. "Choose a file to open.."
  39. @param saveFileDialogTitle the title to show on an save-file dialog, e.g. "Choose a file to save as.."
  40. */
  41. FileBasedDocument (const String& fileExtension,
  42. const String& fileWildCard,
  43. const String& openFileDialogTitle,
  44. const String& saveFileDialogTitle);
  45. /** Destructor. */
  46. ~FileBasedDocument() override;
  47. //==============================================================================
  48. /** Returns true if the changed() method has been called since the file was
  49. last saved or loaded.
  50. @see setChangedFlag, changed
  51. */
  52. bool hasChangedSinceSaved() const { return changedSinceSave; }
  53. /** Called to indicate that the document has changed and needs saving.
  54. This method will also trigger a change message to be sent out using the
  55. ChangeBroadcaster base class.
  56. After calling the method, the hasChangedSinceSaved() method will return true, until
  57. it is reset either by saving to a file or using the setChangedFlag() method.
  58. @see hasChangedSinceSaved, setChangedFlag
  59. */
  60. virtual void changed();
  61. /** Sets the state of the 'changed' flag.
  62. The 'changed' flag is set to true when the changed() method is called - use this method
  63. to reset it or to set it without also broadcasting a change message.
  64. @see changed, hasChangedSinceSaved
  65. */
  66. void setChangedFlag (bool hasChanged);
  67. //==============================================================================
  68. /** Tries to open a file.
  69. If the file opens correctly, the document's file (see the getFile() method) is set
  70. to this new one; if it fails, the document's file is left unchanged, and optionally
  71. a message box is shown telling the user there was an error.
  72. @returns A result indicating whether the new file loaded successfully, or the error
  73. message if it failed.
  74. @see loadDocument, loadFromUserSpecifiedFile
  75. */
  76. Result loadFrom (const File& fileToLoadFrom,
  77. bool showMessageOnFailure,
  78. bool showWaitCursor = true);
  79. /** Asks the user for a file and tries to load it.
  80. This will pop up a dialog box using the title, file extension and
  81. wildcard specified in the document's constructor, and asks the user
  82. for a file. If they pick one, the loadFrom() method is used to
  83. try to load it, optionally showing a message if it fails.
  84. @returns a result indicating success; This will be a failure message if the user
  85. cancelled or if they picked a file which failed to load correctly
  86. @see loadFrom
  87. */
  88. Result loadFromUserSpecifiedFile (bool showMessageOnFailure);
  89. //==============================================================================
  90. /** A set of possible outcomes of one of the save() methods
  91. */
  92. enum SaveResult
  93. {
  94. savedOk = 0, /**< indicates that a file was saved successfully. */
  95. userCancelledSave, /**< indicates that the user aborted the save operation. */
  96. failedToWriteToFile /**< indicates that it tried to write to a file but this failed. */
  97. };
  98. /** Tries to save the document to the last file it was saved or loaded from.
  99. This will always try to write to the file, even if the document isn't flagged as
  100. having changed.
  101. @param askUserForFileIfNotSpecified if there's no file currently specified and this is
  102. true, it will prompt the user to pick a file, as if
  103. saveAsInteractive() was called.
  104. @param showMessageOnFailure if true it will show a warning message when if the
  105. save operation fails
  106. @see saveIfNeededAndUserAgrees, saveAs, saveAsInteractive
  107. */
  108. SaveResult save (bool askUserForFileIfNotSpecified,
  109. bool showMessageOnFailure);
  110. /** If the file needs saving, it'll ask the user if that's what they want to do, and save
  111. it if they say yes.
  112. If you've got a document open and want to close it (e.g. to quit the app), this is the
  113. method to call.
  114. If the document doesn't need saving it'll return the value savedOk so
  115. you can go ahead and delete the document.
  116. If it does need saving it'll prompt the user, and if they say "discard changes" it'll
  117. return savedOk, so again, you can safely delete the document.
  118. If the user clicks "cancel", it'll return userCancelledSave, so if you can abort the
  119. close-document operation.
  120. And if they click "save changes", it'll try to save and either return savedOk, or
  121. failedToWriteToFile if there was a problem.
  122. @see save, saveAs, saveAsInteractive
  123. */
  124. SaveResult saveIfNeededAndUserAgrees();
  125. /** Tries to save the document to a specified file.
  126. If this succeeds, it'll also change the document's internal file (as returned by
  127. the getFile() method). If it fails, the file will be left unchanged.
  128. @param newFile the file to try to write to
  129. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  130. the user first if they want to overwrite it
  131. @param askUserForFileIfNotSpecified if the file is non-existent and this is true, it'll
  132. use the saveAsInteractive() method to ask the user for a
  133. filename
  134. @param showMessageOnFailure if true and the write operation fails, it'll show
  135. a message box to warn the user
  136. @param showWaitCursor if true, the 'wait' mouse cursor will be showin during
  137. saving
  138. @see saveIfNeededAndUserAgrees, save, saveAsInteractive
  139. */
  140. SaveResult saveAs (const File& newFile,
  141. bool warnAboutOverwritingExistingFiles,
  142. bool askUserForFileIfNotSpecified,
  143. bool showMessageOnFailure,
  144. bool showWaitCursor = true);
  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 = false;
  219. String fileExtension, fileWildcard, openFileDialogTitle, saveFileDialogTitle;
  220. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBasedDocument)
  221. };
  222. } // namespace juce