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.

295 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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A class to take care of the logic involved with the loading/saving of some kind
  24. of document.
  25. There's quite a lot of tedious logic involved in writing all the load/save/save-as
  26. functions you need for documents that get saved to a file, so this class attempts
  27. to abstract most of the boring stuff.
  28. Your subclass should just implement all the pure virtual methods, and you can
  29. then use the higher-level public methods to do the load/save dialogs, to warn the user
  30. about overwriting files, etc.
  31. The document object keeps track of whether it has changed since it was last saved or
  32. loaded, so when you change something, call its changed() method. This will set a
  33. flag so it knows it needs saving, and will also broadcast a change message using the
  34. ChangeBroadcaster base class.
  35. @see ChangeBroadcaster
  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. virtual ~FileBasedDocument();
  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. /** Asks the user for a file and tries to load it.
  84. This will pop up a dialog box using the title, file extension and
  85. wildcard specified in the document's constructor, and asks the user
  86. for a file. If they pick one, the loadFrom() method is used to
  87. try to load it, optionally showing a message if it fails.
  88. @returns a result indicating success; This will be a failure message if the user
  89. cancelled or if they picked a file which failed to load correctly
  90. @see loadFrom
  91. */
  92. Result loadFromUserSpecifiedFile (bool showMessageOnFailure);
  93. //==============================================================================
  94. /** A set of possible outcomes of one of the save() methods
  95. */
  96. enum SaveResult
  97. {
  98. savedOk = 0, /**< indicates that a file was saved successfully. */
  99. userCancelledSave, /**< indicates that the user aborted the save operation. */
  100. failedToWriteToFile /**< indicates that it tried to write to a file but this failed. */
  101. };
  102. /** Tries to save the document to the last file it was saved or loaded from.
  103. This will always try to write to the file, even if the document isn't flagged as
  104. having changed.
  105. @param askUserForFileIfNotSpecified if there's no file currently specified and this is
  106. true, it will prompt the user to pick a file, as if
  107. saveAsInteractive() was called.
  108. @param showMessageOnFailure if true it will show a warning message when if the
  109. save operation fails
  110. @see saveIfNeededAndUserAgrees, saveAs, saveAsInteractive
  111. */
  112. SaveResult save (bool askUserForFileIfNotSpecified,
  113. bool showMessageOnFailure);
  114. /** If the file needs saving, it'll ask the user if that's what they want to do, and save
  115. it if they say yes.
  116. If you've got a document open and want to close it (e.g. to quit the app), this is the
  117. method to call.
  118. If the document doesn't need saving it'll return the value savedOk so
  119. you can go ahead and delete the document.
  120. If it does need saving it'll prompt the user, and if they say "discard changes" it'll
  121. return savedOk, so again, you can safely delete the document.
  122. If the user clicks "cancel", it'll return userCancelledSave, so if you can abort the
  123. close-document operation.
  124. And if they click "save changes", it'll try to save and either return savedOk, or
  125. failedToWriteToFile if there was a problem.
  126. @see save, saveAs, saveAsInteractive
  127. */
  128. SaveResult saveIfNeededAndUserAgrees();
  129. /** Tries to save the document to a specified file.
  130. If this succeeds, it'll also change the document's internal file (as returned by
  131. the getFile() method). If it fails, the file will be left unchanged.
  132. @param newFile the file to try to write to
  133. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  134. the user first if they want to overwrite it
  135. @param askUserForFileIfNotSpecified if the file is non-existent and this is true, it'll
  136. use the saveAsInteractive() method to ask the user for a
  137. filename
  138. @param showMessageOnFailure if true and the write operation fails, it'll show
  139. a message box to warn the user
  140. @see saveIfNeededAndUserAgrees, save, saveAsInteractive
  141. */
  142. SaveResult saveAs (const File& newFile,
  143. bool warnAboutOverwritingExistingFiles,
  144. bool askUserForFileIfNotSpecified,
  145. bool showMessageOnFailure);
  146. /** Prompts the user for a filename and tries to save to it.
  147. This will pop up a dialog box using the title, file extension and
  148. wildcard specified in the document's constructor, and asks the user
  149. for a file. If they pick one, the saveAs() method is used to try to save
  150. to this file.
  151. @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
  152. the user first if they want to overwrite it
  153. @see saveIfNeededAndUserAgrees, save, saveAs
  154. */
  155. SaveResult saveAsInteractive (bool warnAboutOverwritingExistingFiles);
  156. //==============================================================================
  157. /** Returns the file that this document was last successfully saved or loaded from.
  158. When the document object is created, this will be set to File().
  159. It is changed when one of the load or save methods is used, or when setFile()
  160. is used to explicitly set it.
  161. */
  162. const File& getFile() const { return documentFile; }
  163. /** Sets the file that this document thinks it was loaded from.
  164. This won't actually load anything - it just changes the file stored internally.
  165. @see getFile
  166. */
  167. void setFile (const File& newFile);
  168. protected:
  169. //==============================================================================
  170. /** Overload this to return the title of the document.
  171. This is used in message boxes, filenames and file choosers, so it should be
  172. something sensible.
  173. */
  174. virtual String getDocumentTitle() = 0;
  175. /** This method should try to load your document from the given file.
  176. @returns a Result object to indicate the whether there was an error.
  177. */
  178. virtual Result loadDocument (const File& file) = 0;
  179. /** This method should try to write your document to the given file.
  180. @returns a Result object to indicate the whether there was an error.
  181. */
  182. virtual Result saveDocument (const File& file) = 0;
  183. /** This is used for dialog boxes to make them open at the last folder you
  184. were using.
  185. getLastDocumentOpened() and setLastDocumentOpened() are used to store
  186. the last document that was used - you might want to store this value
  187. in a static variable, or even in your application's properties. It should
  188. be a global setting rather than a property of this object.
  189. This method works very well in conjunction with a RecentlyOpenedFilesList
  190. object to manage your recent-files list.
  191. As a default value, it's ok to return File(), and the document object will
  192. use a sensible one instead.
  193. @see RecentlyOpenedFilesList
  194. */
  195. virtual File getLastDocumentOpened() = 0;
  196. /** This is used for dialog boxes to make them open at the last folder you
  197. were using.
  198. getLastDocumentOpened() and setLastDocumentOpened() are used to store
  199. the last document that was used - you might want to store this value
  200. in a static variable, or even in your application's properties. It should
  201. be a global setting rather than a property of this object.
  202. This method works very well in conjunction with a RecentlyOpenedFilesList
  203. object to manage your recent-files list.
  204. @see RecentlyOpenedFilesList
  205. */
  206. virtual void setLastDocumentOpened (const File& file) = 0;
  207. #if JUCE_MODAL_LOOPS_PERMITTED
  208. /** This is called by saveAsInteractive() to allow you to optionally customise the
  209. filename that the user is presented with in the save dialog.
  210. The defaultFile parameter is an initial suggestion based on what the class knows
  211. about the current document - you can return a variation on this file with a different
  212. extension, etc, or just return something completely different.
  213. */
  214. virtual File getSuggestedSaveAsFile (const File& defaultFile);
  215. #endif
  216. private:
  217. //==============================================================================
  218. File documentFile;
  219. bool changedSinceSave;
  220. String fileExtension, fileWildcard, openFileDialogTitle, saveFileDialogTitle;
  221. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBasedDocument)
  222. };
  223. } // namespace juce