Add remaining files
[juce-lv2.git] / juce / source / src / utilities / juce_FileBasedDocument.h
blob65df6eb15ff79684263637860b668837bfd79f3c
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_FILEBASEDDOCUMENT_JUCEHEADER__
27 #define __JUCE_FILEBASEDDOCUMENT_JUCEHEADER__
29 #include "../io/files/juce_File.h"
30 #include "../events/juce_ChangeBroadcaster.h"
33 //==============================================================================
34 /**
35 A class to take care of the logic involved with the loading/saving of some kind
36 of document.
38 There's quite a lot of tedious logic involved in writing all the load/save/save-as
39 functions you need for documents that get saved to a file, so this class attempts
40 to abstract most of the boring stuff.
42 Your subclass should just implement all the pure virtual methods, and you can
43 then use the higher-level public methods to do the load/save dialogs, to warn the user
44 about overwriting files, etc.
46 The document object keeps track of whether it has changed since it was last saved or
47 loaded, so when you change something, call its changed() method. This will set a
48 flag so it knows it needs saving, and will also broadcast a change message using the
49 ChangeBroadcaster base class.
51 @see ChangeBroadcaster
53 class JUCE_API FileBasedDocument : public ChangeBroadcaster
55 public:
56 /** Creates a FileBasedDocument.
58 @param fileExtension the extension to use when loading/saving files, e.g. ".doc"
59 @param fileWildCard the wildcard to use in file dialogs, e.g. "*.doc"
60 @param openFileDialogTitle the title to show on an open-file dialog, e.g. "Choose a file to open.."
61 @param saveFileDialogTitle the title to show on an save-file dialog, e.g. "Choose a file to save as.."
63 FileBasedDocument (const String& fileExtension,
64 const String& fileWildCard,
65 const String& openFileDialogTitle,
66 const String& saveFileDialogTitle);
68 /** Destructor. */
69 virtual ~FileBasedDocument();
71 //==============================================================================
72 /** Returns true if the changed() method has been called since the file was
73 last saved or loaded.
75 @see resetChangedFlag, changed
77 bool hasChangedSinceSaved() const { return changedSinceSave; }
79 /** Called to indicate that the document has changed and needs saving.
81 This method will also trigger a change message to be sent out using the
82 ChangeBroadcaster base class.
84 After calling the method, the hasChangedSinceSaved() method will return true, until
85 it is reset either by saving to a file or using the resetChangedFlag() method.
87 @see hasChangedSinceSaved, resetChangedFlag
89 virtual void changed();
91 /** Sets the state of the 'changed' flag.
93 The 'changed' flag is set to true when the changed() method is called - use this method
94 to reset it or to set it without also broadcasting a change message.
96 @see changed, hasChangedSinceSaved
98 void setChangedFlag (bool hasChanged);
100 //==============================================================================
101 /** Tries to open a file.
103 If the file opens correctly, the document's file (see the getFile() method) is set
104 to this new one; if it fails, the document's file is left unchanged, and optionally
105 a message box is shown telling the user there was an error.
107 @returns true if the new file loaded successfully
108 @see loadDocument, loadFromUserSpecifiedFile
110 bool loadFrom (const File& fileToLoadFrom,
111 bool showMessageOnFailure);
113 /** Asks the user for a file and tries to load it.
115 This will pop up a dialog box using the title, file extension and
116 wildcard specified in the document's constructor, and asks the user
117 for a file. If they pick one, the loadFrom() method is used to
118 try to load it, optionally showing a message if it fails.
120 @returns true if a file was loaded; false if the user cancelled or if they
121 picked a file which failed to load correctly
122 @see loadFrom
124 bool loadFromUserSpecifiedFile (bool showMessageOnFailure);
126 //==============================================================================
127 /** A set of possible outcomes of one of the save() methods
129 enum SaveResult
131 savedOk = 0, /**< indicates that a file was saved successfully. */
132 userCancelledSave, /**< indicates that the user aborted the save operation. */
133 failedToWriteToFile /**< indicates that it tried to write to a file but this failed. */
136 /** Tries to save the document to the last file it was saved or loaded from.
138 This will always try to write to the file, even if the document isn't flagged as
139 having changed.
141 @param askUserForFileIfNotSpecified if there's no file currently specified and this is
142 true, it will prompt the user to pick a file, as if
143 saveAsInteractive() was called.
144 @param showMessageOnFailure if true it will show a warning message when if the
145 save operation fails
146 @see saveIfNeededAndUserAgrees, saveAs, saveAsInteractive
148 SaveResult save (bool askUserForFileIfNotSpecified,
149 bool showMessageOnFailure);
151 /** If the file needs saving, it'll ask the user if that's what they want to do, and save
152 it if they say yes.
154 If you've got a document open and want to close it (e.g. to quit the app), this is the
155 method to call.
157 If the document doesn't need saving it'll return the value savedOk so
158 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.
163 If the user clicks "cancel", it'll return userCancelledSave, so if you can abort the
164 close-document operation.
166 And if they click "save changes", it'll try to save and either return savedOk, or
167 failedToWriteToFile if there was a problem.
169 @see save, saveAs, saveAsInteractive
171 SaveResult saveIfNeededAndUserAgrees();
173 /** Tries to save the document to a specified file.
175 If this succeeds, it'll also change the document's internal file (as returned by
176 the getFile() method). If it fails, the file will be left unchanged.
178 @param newFile the file to try to write to
179 @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
180 the user first if they want to overwrite it
181 @param askUserForFileIfNotSpecified if the file is non-existent and this is true, it'll
182 use the saveAsInteractive() method to ask the user for a
183 filename
184 @param showMessageOnFailure if true and the write operation fails, it'll show
185 a message box to warn the user
186 @see saveIfNeededAndUserAgrees, save, saveAsInteractive
188 SaveResult saveAs (const File& newFile,
189 bool warnAboutOverwritingExistingFiles,
190 bool askUserForFileIfNotSpecified,
191 bool showMessageOnFailure);
193 /** Prompts the user for a filename and tries to save to it.
195 This will pop up a dialog box using the title, file extension and
196 wildcard specified in the document's constructor, and asks the user
197 for a file. If they pick one, the saveAs() method is used to try to save
198 to this file.
200 @param warnAboutOverwritingExistingFiles if true and the file exists, it'll ask
201 the user first if they want to overwrite it
202 @see saveIfNeededAndUserAgrees, save, saveAs
204 SaveResult saveAsInteractive (bool warnAboutOverwritingExistingFiles);
206 //==============================================================================
207 /** Returns the file that this document was last successfully saved or loaded from.
209 When the document object is created, this will be set to File::nonexistent.
211 It is changed when one of the load or save methods is used, or when setFile()
212 is used to explicitly set it.
214 const File& getFile() const { return documentFile; }
216 /** Sets the file that this document thinks it was loaded from.
218 This won't actually load anything - it just changes the file stored internally.
220 @see getFile
222 void setFile (const File& newFile);
225 protected:
226 //==============================================================================
227 /** Overload this to return the title of the document.
229 This is used in message boxes, filenames and file choosers, so it should be
230 something sensible.
232 virtual const String getDocumentTitle() = 0;
234 /** This method should try to load your document from the given file.
236 If it fails, it should return an error message. If it succeeds, it should return
237 an empty string.
239 virtual const String loadDocument (const File& file) = 0;
241 /** This method should try to write your document to the given file.
243 If it fails, it should return an error message. If it succeeds, it should return
244 an empty string.
246 virtual const String saveDocument (const File& file) = 0;
248 /** This is used for dialog boxes to make them open at the last folder you
249 were using.
251 getLastDocumentOpened() and setLastDocumentOpened() are used to store
252 the last document that was used - you might want to store this value
253 in a static variable, or even in your application's properties. It should
254 be a global setting rather than a property of this object.
256 This method works very well in conjunction with a RecentlyOpenedFilesList
257 object to manage your recent-files list.
259 As a default value, it's ok to return File::nonexistent, and the document
260 object will use a sensible one instead.
262 @see RecentlyOpenedFilesList
264 virtual const File getLastDocumentOpened() = 0;
266 /** This is used for dialog boxes to make them open at the last folder you
267 were using.
269 getLastDocumentOpened() and setLastDocumentOpened() are used to store
270 the last document that was used - you might want to store this value
271 in a static variable, or even in your application's properties. It should
272 be a global setting rather than a property of this object.
274 This method works very well in conjunction with a RecentlyOpenedFilesList
275 object to manage your recent-files list.
277 @see RecentlyOpenedFilesList
279 virtual void setLastDocumentOpened (const File& file) = 0;
282 private:
283 //==============================================================================
284 File documentFile;
285 bool changedSinceSave;
286 String fileExtension, fileWildcard, openFileDialogTitle, saveFileDialogTitle;
288 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBasedDocument);
292 #endif // __JUCE_FILEBASEDDOCUMENT_JUCEHEADER__