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.

432 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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;
  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. /** Tries to open a file.
  85. The callback is called with the result indicating whether the new file loaded
  86. successfully, or the error message if it failed.
  87. If the file opens correctly the document's file (see the getFile() method) is set
  88. to this new one; if it fails, the document's file is left unchanged, and optionally
  89. a message box is shown telling the user there was an error.
  90. @see loadDocumentAsync, loadFromUserSpecifiedFileAsync
  91. */
  92. void loadFromAsync (const File& fileToLoadFrom,
  93. bool showMessageOnFailure,
  94. std::function<void (Result)> callback);
  95. #if JUCE_MODAL_LOOPS_PERMITTED
  96. /** Asks the user for a file and tries to load it.
  97. This will pop up a dialog box using the title, file extension and
  98. wildcard specified in the document's constructor, and asks the user
  99. for a file. If they pick one, the loadFrom() method is used to
  100. try to load it, optionally showing a message if it fails.
  101. @returns a result indicating success; This will be a failure message if the user
  102. cancelled or if they picked a file which failed to load correctly
  103. @see loadFrom
  104. */
  105. Result loadFromUserSpecifiedFile (bool showMessageOnFailure);
  106. #endif
  107. /** Asks the user for a file and tries to load it.
  108. This will pop up a dialog box using the title, file extension and
  109. wildcard specified in the document's constructor, and asks the user
  110. for a file. If they pick one, the loadFrom() method is used to
  111. try to load it, optionally showing a message if it fails. The result
  112. of the operation is provided in the callback function.
  113. @see loadFrom
  114. */
  115. void loadFromUserSpecifiedFileAsync (bool showMessageOnFailure, std::function<void (Result)> callback);
  116. //==============================================================================
  117. /** A set of possible outcomes of one of the save() methods
  118. */
  119. enum SaveResult
  120. {
  121. savedOk = 0, /**< indicates that a file was saved successfully. */
  122. userCancelledSave, /**< indicates that the user aborted the save operation. */
  123. failedToWriteToFile /**< indicates that it tried to write to a file but this failed. */
  124. };
  125. #if JUCE_MODAL_LOOPS_PERMITTED
  126. /** Tries to save the document to the last file it was saved or loaded from.
  127. This will always try to write to the file, even if the document isn't flagged as
  128. having changed.
  129. @param askUserForFileIfNotSpecified if there's no file currently specified and this is
  130. true, it will prompt the user to pick a file, as if
  131. saveAsInteractive() was called.
  132. @param showMessageOnFailure if true it will show a warning message when if the
  133. save operation fails
  134. @see saveIfNeededAndUserAgrees, saveAs, saveAsInteractive
  135. */
  136. SaveResult save (bool askUserForFileIfNotSpecified,
  137. bool showMessageOnFailure);
  138. #endif
  139. /** Tries to save the document to the last file it was saved or loaded from.
  140. This will always try to write to the file, even if the document isn't flagged as
  141. having changed.
  142. @param askUserForFileIfNotSpecified if there's no file currently specified and this is
  143. true, it will prompt the user to pick a file, as if
  144. saveAsInteractive() was called.
  145. @param showMessageOnFailure if true it will show a warning message when if the
  146. save operation fails
  147. @param callback called after the save operation with the result
  148. @see saveIfNeededAndUserAgrees, saveAs, saveAsInteractive
  149. */
  150. void saveAsync (bool askUserForFileIfNotSpecified,
  151. bool showMessageOnFailure,
  152. std::function<void (SaveResult)> callback);
  153. #if JUCE_MODAL_LOOPS_PERMITTED
  154. /** If the file needs saving, it'll ask the user if that's what they want to do, and save
  155. it if they say yes.
  156. If you've got a document open and want to close it (e.g. to quit the app), this is the
  157. method to call.
  158. If the document doesn't need saving it'll return the value savedOk so
  159. you can go ahead and delete the document.
  160. If it does need saving it'll prompt the user, and if they say "discard changes" it'll
  161. return savedOk, so again, you can safely delete the document.
  162. If the user clicks "cancel", it'll return userCancelledSave, so if you can abort the
  163. close-document operation.
  164. And if they click "save changes", it'll try to save and either return savedOk, or
  165. failedToWriteToFile if there was a problem.
  166. @see save, saveAs, saveAsInteractive
  167. */
  168. SaveResult saveIfNeededAndUserAgrees();
  169. #endif
  170. /** If the file needs saving, it'll ask the user if that's what they want to do, and save
  171. it if they say yes.
  172. If you've got a document open and want to close it (e.g. to quit the app), this is the
  173. method to call.
  174. If the document doesn't need saving the callback will be called with the value savedOk
  175. so you can go ahead and delete the document.
  176. If it does need saving it'll prompt the user, and if they say "discard changes" the
  177. callback will be called with savedOk, so again, you can safely delete the document.
  178. If the user clicks "cancel", the callback will be called with userCancelledSave, so
  179. you can abort the close-document operation.
  180. And if they click "save changes", it'll try to save and the callback will be called
  181. with either savedOk, or failedToWriteToFile if there was a problem.
  182. @see saveAsync, saveAsAsync, saveAsInteractiveAsync
  183. */
  184. void saveIfNeededAndUserAgreesAsync (std::function<void (SaveResult)> callback);
  185. #if JUCE_MODAL_LOOPS_PERMITTED
  186. /** Tries to save the document to a specified file.
  187. If this succeeds, it'll also change the document's internal file (as returned by
  188. the getFile() method). If it fails, the file will be left unchanged.
  189. @param newFile the file to try to write to
  190. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  191. the user first if they want to overwrite it
  192. @param askUserForFileIfNotSpecified if the file is non-existent and this is true, it'll
  193. use the saveAsInteractive() method to ask the user for a
  194. filename
  195. @param showMessageOnFailure if true and the write operation fails, it'll show
  196. a message box to warn the user
  197. @param showWaitCursor if true, the 'wait' mouse cursor will be showin during
  198. saving
  199. @see saveIfNeededAndUserAgrees, save, saveAsInteractive
  200. */
  201. SaveResult saveAs (const File& newFile,
  202. bool warnAboutOverwritingExistingFiles,
  203. bool askUserForFileIfNotSpecified,
  204. bool showMessageOnFailure,
  205. bool showWaitCursor = true);
  206. #endif
  207. /** Tries to save the document to a specified file.
  208. If this succeeds, it'll also change the document's internal file (as returned by
  209. the getFile() method). If it fails, the file will be left unchanged.
  210. @param newFile the file to try to write to
  211. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask the user
  212. first if they want to overwrite it
  213. @param askUserForFileIfNotSpecified if the file is non-existent and this is true, it'll
  214. use the saveAsInteractive() method to ask the user
  215. for a filename
  216. @param showMessageOnFailure if true and the write operation fails, it'll show
  217. a message box to warn the user
  218. @param callback called with the result of the save operation
  219. @see saveIfNeededAndUserAgreesAsync, saveAsync, saveAsInteractiveAsync
  220. */
  221. void saveAsAsync (const File& newFile,
  222. bool warnAboutOverwritingExistingFiles,
  223. bool askUserForFileIfNotSpecified,
  224. bool showMessageOnFailure,
  225. std::function<void (SaveResult)> callback);
  226. #if JUCE_MODAL_LOOPS_PERMITTED
  227. /** Prompts the user for a filename and tries to save to it.
  228. This will pop up a dialog box using the title, file extension and
  229. wildcard specified in the document's constructor, and asks the user
  230. for a file. If they pick one, the saveAs() method is used to try to save
  231. to this file.
  232. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  233. the user first if they want to overwrite it
  234. @see saveIfNeededAndUserAgrees, save, saveAs
  235. */
  236. SaveResult saveAsInteractive (bool warnAboutOverwritingExistingFiles);
  237. #endif
  238. /** Prompts the user for a filename and tries to save to it.
  239. This will pop up a dialog box using the title, file extension and
  240. wildcard specified in the document's constructor, and asks the user
  241. for a file. If they pick one, the saveAs() method is used to try to save
  242. to this file.
  243. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  244. the user first if they want to overwrite it
  245. @param callback called with the result of the save operation
  246. @see saveIfNeededAndUserAgreesAsync, saveAsync, saveAsAsync
  247. */
  248. void saveAsInteractiveAsync (bool warnAboutOverwritingExistingFiles,
  249. std::function<void (SaveResult)> callback);
  250. //==============================================================================
  251. /** Returns the file that this document was last successfully saved or loaded from.
  252. When the document object is created, this will be set to File().
  253. It is changed when one of the load or save methods is used, or when setFile()
  254. is used to explicitly set it.
  255. */
  256. const File& getFile() const;
  257. /** Sets the file that this document thinks it was loaded from.
  258. This won't actually load anything - it just changes the file stored internally.
  259. @see getFile
  260. */
  261. void setFile (const File& newFile);
  262. protected:
  263. //==============================================================================
  264. /** Overload this to return the title of the document.
  265. This is used in message boxes, filenames and file choosers, so it should be
  266. something sensible.
  267. */
  268. virtual String getDocumentTitle() = 0;
  269. /** This method should try to load your document from the given file.
  270. @returns a Result object to indicate the whether there was an error.
  271. */
  272. virtual Result loadDocument (const File& file) = 0;
  273. /** This method should try to load your document from the given file, then
  274. call the provided callback on the message thread, passing the result of the load.
  275. By default, this will synchronously call through to loadDocument.
  276. For longer-running load operations, you may wish to override this function to
  277. run the load on a background thread, and then to call the callback later on the
  278. message thread to signal that the load has completed.
  279. */
  280. virtual void loadDocumentAsync (const File& file, std::function<void (Result)> callback);
  281. /** This method should try to write your document to the given file.
  282. @returns a Result object to indicate the whether there was an error.
  283. */
  284. virtual Result saveDocument (const File& file) = 0;
  285. /** This method should try to write your document to the given file, then
  286. call the provided callback on the message thread, passing the result of the write.
  287. By default, this will synchronously call through to saveDocument.
  288. For longer-running save operations, you may wish to override this function to
  289. run the save on a background thread, and then to call the callback later on the
  290. message thread to signal that the save has completed.
  291. */
  292. virtual void saveDocumentAsync (const File& file, std::function<void (Result)> callback);
  293. /** This is used for dialog boxes to make them open at the last folder you
  294. were using.
  295. getLastDocumentOpened() and setLastDocumentOpened() are used to store
  296. the last document that was used - you might want to store this value
  297. in a static variable, or even in your application's properties. It should
  298. be a global setting rather than a property of this object.
  299. This method works very well in conjunction with a RecentlyOpenedFilesList
  300. object to manage your recent-files list.
  301. As a default value, it's ok to return File(), and the document object will
  302. use a sensible one instead.
  303. @see RecentlyOpenedFilesList
  304. */
  305. virtual File getLastDocumentOpened() = 0;
  306. /** This is used for dialog boxes to make them open at the last folder you
  307. were using.
  308. getLastDocumentOpened() and setLastDocumentOpened() are used to store
  309. the last document that was used - you might want to store this value
  310. in a static variable, or even in your application's properties. It should
  311. be a global setting rather than a property of this object.
  312. This method works very well in conjunction with a RecentlyOpenedFilesList
  313. object to manage your recent-files list.
  314. @see RecentlyOpenedFilesList
  315. */
  316. virtual void setLastDocumentOpened (const File& file) = 0;
  317. /** This is called by saveAsInteractiveAsync() to allow you to optionally customise the
  318. filename that the user is presented with in the save dialog.
  319. The defaultFile parameter is an initial suggestion based on what the class knows
  320. about the current document - you can return a variation on this file with a different
  321. extension, etc, or just return something completely different.
  322. */
  323. virtual File getSuggestedSaveAsFile (const File& defaultFile);
  324. private:
  325. //==============================================================================
  326. class Pimpl;
  327. std::unique_ptr<Pimpl> pimpl;
  328. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBasedDocument)
  329. };
  330. } // namespace juce