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

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