1 /*
2    NSDocument.h
3 
4    The abstract document class
5 
6    Copyright (C) 1999 Free Software Foundation, Inc.
7 
8    Author: Carl Lindberg <Carl.Lindberg@hbo.com>
9    Date: 1999
10    Modifications: Fred Kiefer <fredkiefer@gmx.de>
11    Date: Dec 2006
12    Added MacOS 10.4 methods.
13 
14    This file is part of the GNUstep GUI Library.
15 
16    This library is free software; you can redistribute it and/or
17    modify it under the terms of the GNU Lesser General Public
18    License as published by the Free Software Foundation; either
19    version 2 of the License, or (at your option) any later version.
20 
21    This library is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
24    Lesser General Public License for more details.
25 
26    You should have received a copy of the GNU Lesser General Public
27    License along with this library; see the file COPYING.LIB.
28    If not, see <http://www.gnu.org/licenses/> or write to the
29    Free Software Foundation, 51 Franklin Street, Fifth Floor,
30    Boston, MA 02110-1301, USA.
31 */
32 
33 #ifndef _GNUstep_H_NSDocument
34 #define _GNUstep_H_NSDocument
35 #import <GNUstepBase/GSVersionMacros.h>
36 
37 #import <Foundation/NSObject.h>
38 #import <AppKit/NSNibDeclarations.h>
39 #import <AppKit/NSUserInterfaceValidation.h>
40 
41 
42 /* Foundation classes */
43 @class NSString;
44 @class NSArray;
45 @class NSMutableArray;
46 @class NSData;
47 @class NSDate;
48 @class NSDictionary;
49 @class NSError;
50 @class NSFileManager;
51 @class NSURL;
52 @class NSUndoManager;
53 
54 /* AppKit classes */
55 @class NSWindow;
56 @class NSView;
57 @class NSSavePanel;
58 @class NSMenuItem;
59 @class NSPageLayout;
60 @class NSPrintInfo;
61 @class NSPrintOperation;
62 @class NSPopUpButton;
63 @class NSFileWrapper;
64 @class NSDocumentController;
65 @class NSWindowController;
66 
67 
68 typedef enum _NSDocumentChangeType {
69     NSChangeDone 	= 0,
70     NSChangeUndone 	= 1,
71     NSChangeCleared 	= 2,
72 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
73     NSChangeReadOtherContents = 3,
74     NSChangeAutosaved   = 4
75 #endif
76 } NSDocumentChangeType;
77 
78 typedef enum _NSSaveOperationType {
79     NSSaveOperation		= 0,
80     NSSaveAsOperation		= 1,
81     NSSaveToOperation		= 2,
82 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
83     NSAutosaveOperation		= 3
84 #endif
85 } NSSaveOperationType;
86 
87 @interface NSDocument : NSObject
88 {
89   @private
90     NSWindow		*_window;		// Outlet for the single window case
91     NSMutableArray 	*_window_controllers;	// WindowControllers for this document
92     NSURL		*_file_url;		// Save location as URL
93     NSString		*_file_name;		// Save location
94     NSString 		*_file_type;		// file/document type
95     NSDate 		*_file_modification_date;// file modification date
96     NSString		*_last_component_file_name; // file name last component
97     NSURL		*_autosaved_file_url;	// Autosave location as URL
98     NSPrintInfo 	*_print_info;		// print info record
99     id			_printOp_delegate;	// delegate and selector called
100     SEL			_printOp_didRunSelector;//   after modal print operation
101     NSView 		*_save_panel_accessory;	// outlet for the accessory save-panel view
102     NSPopUpButton	*_spa_button;     	// outlet for "the File Format:" button in the save panel.
103     NSString            *_save_type;             // the currently selected extension.
104     NSUndoManager 	*_undo_manager;		// Undo manager for this document
105     long		_change_count;		// number of time the document has been changed
106     long		_autosave_change_count;	// number of time the document has been changed since the last autosave
107     int			_document_index;	// Untitled index
108     struct __docFlags {
109         unsigned int in_close:1;
110         unsigned int has_undo_manager:1;
111         unsigned int permanently_modified:1;
112         unsigned int autosave_permanently_modified:1;
113         unsigned int RESERVED:28;
114     } _doc_flags;
115     void 		*_reserved1;
116 }
117 
118 + (NSArray *)readableTypes;
119 + (NSArray *)writableTypes;
120 + (BOOL)isNativeType:(NSString *)type;
121 
122 /*" Initialization "*/
123 - (id)init;
124 - (id)initWithContentsOfFile:(NSString *)fileName ofType:(NSString *)fileType;
125 - (id)initWithContentsOfURL:(NSURL *)url ofType:(NSString *)fileType;
126 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
127 - (id)initForURL:(NSURL *)forUrl
128 withContentsOfURL:(NSURL *)url
129           ofType:(NSString *)type
130            error:(NSError **)error;
131 - (id)initWithContentsOfURL:(NSURL *)url
132                      ofType:(NSString *)type
133                       error:(NSError **)error;
134 - (id)initWithType:(NSString *)type
135              error:(NSError **)error;
136 #endif
137 
138 /*" Window management "*/
139 - (NSArray *)windowControllers;
140 - (void)addWindowController:(NSWindowController *)windowController;
141 #if OS_API_VERSION(GS_API_MACOSX, MAC_OS_X_VERSION_10_4)
142 - (BOOL)shouldCloseWindowController:(NSWindowController *)windowController;
143 #endif
144 #if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
145 - (void)shouldCloseWindowController:(NSWindowController *)windowController
146 			   delegate:(id)delegate
147 		shouldCloseSelector:(SEL)callback
148 			contextInfo:(void *)contextInfo;
149 #endif
150 - (void)showWindows;
151 - (void)removeWindowController:(NSWindowController *)windowController;
152 - (void)setWindow:(NSWindow *)aWindow;
153 #if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST)
154 - (NSWindow *)windowForSheet;
155 #endif
156 
157 /*" Window controller creation "*/
158 - (void)makeWindowControllers;  // Manual creation
159 - (NSString *)windowNibName;    // Automatic creation (Document will be the nib owner)
160 
161 /*" Window loading notifications "*/
162 // Only called if the document is the owner of the nib
163 - (void)windowControllerWillLoadNib:(NSWindowController *)windowController;
164 - (void)windowControllerDidLoadNib:(NSWindowController *)windowController;
165 
166 /*" Edited flag "*/
167 - (BOOL)isDocumentEdited;
168 - (void)updateChangeCount:(NSDocumentChangeType)change;
169 
170 /*" Display Name (window title) "*/
171 - (NSString *)displayName;
172 
173 /*" Backup file "*/
174 - (BOOL)keepBackupFile;
175 
176 /*" Closing "*/
177 - (void)close;
178 #if OS_API_VERSION(GS_API_MACOSX, MAC_OS_X_VERSION_10_4)
179 - (BOOL)canCloseDocument;
180 #endif
181 #if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
182 - (void)canCloseDocumentWithDelegate:(id)delegate
183 		 shouldCloseSelector:(SEL)shouldCloseSelector
184 			 contextInfo:(void *)contextInfo;
185 #endif
186 
187 /*" Type and location "*/
188 - (NSString *)fileName;
189 - (void)setFileName:(NSString *)fileName;
190 - (NSString *)fileType;
191 - (void)setFileType:(NSString *)type;
192 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
193 - (NSURL *)fileURL;
194 - (void)setFileURL:(NSURL *)url;
195 - (NSDate *)fileModificationDate;
196 - (void)setFileModificationDate: (NSDate *)date;
197 - (NSString *)lastComponentOfFileName;
198 - (void)setLastComponentOfFileName:(NSString *)str;
199 #endif
200 
201 /*" Read/Write/Revert "*/
202 
203 - (NSData *)dataRepresentationOfType:(NSString *)type;
204 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)type;
205 
206 - (NSFileWrapper *)fileWrapperRepresentationOfType:(NSString *)type;
207 - (BOOL)loadFileWrapperRepresentation:(NSFileWrapper *)wrapper
208 			       ofType:(NSString *)type;
209 
210 - (BOOL)writeToFile:(NSString *)fileName ofType:(NSString *)type;
211 - (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
212 - (BOOL)revertToSavedFromFile:(NSString *)fileName ofType:(NSString *)type;
213 
214 - (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type;
215 - (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type;
216 - (BOOL)revertToSavedFromURL:(NSURL *)url ofType:(NSString *)type;
217 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
218 - (NSData *)dataOfType:(NSString *)type
219                  error:(NSError **)error;
220 - (NSFileWrapper *)fileWrapperOfType:(NSString *)type
221                                error:(NSError **)error;
222 - (BOOL)readFromData:(NSData *)data
223               ofType:(NSString *)type
224                error:(NSError **)error;
225 - (BOOL)readFromFileWrapper:(NSFileWrapper *)wrapper
226                      ofType:(NSString *)type
227                       error:(NSError **)error;
228 - (BOOL)readFromURL:(NSURL *)url
229              ofType:(NSString *)type
230               error:(NSError **)error;
231 - (BOOL)revertToContentsOfURL:(NSURL *)url
232                        ofType:(NSString *)type
233                         error:(NSError **)error;
234 - (BOOL)writeSafelyToURL:(NSURL *)url
235                   ofType:(NSString *)type
236         forSaveOperation:(NSSaveOperationType)op
237                    error:(NSError **)error;
238 - (BOOL)writeToURL:(NSURL *)url
239             ofType:(NSString *)type
240              error:(NSError **)error;
241 - (BOOL)writeToURL:(NSURL *)url
242             ofType:(NSString *)type
243   forSaveOperation:(NSSaveOperationType)op
244 originalContentsURL:(NSURL *)orig
245              error:(NSError **)error;
246 #endif
247 
248 /*" Save panel "*/
249 - (BOOL)shouldRunSavePanelWithAccessoryView;
250 #if OS_API_VERSION(GS_API_MACOSX, MAC_OS_X_VERSION_10_4)
251 - (NSString *)fileNameFromRunningSavePanelForSaveOperation:(NSSaveOperationType)saveOperation;
252 - (NSInteger)runModalSavePanel:(NSSavePanel *)savePanel withAccessoryView:(NSView *)accessoryView;
253 #endif
254 - (NSString *)fileTypeFromLastRunSavePanel;
255 - (NSDictionary *)fileAttributesToWriteToFile: (NSString *)fullDocumentPath
256 				       ofType: (NSString *)docType
257 				saveOperation: (NSSaveOperationType)saveOperationType;
258 - (BOOL)writeToFile:(NSString *)fileName
259 	     ofType:(NSString *)type
260        originalFile:(NSString *)origFileName
261       saveOperation:(NSSaveOperationType)saveOp;
262 - (BOOL)writeWithBackupToFile:(NSString *)fileName
263 		       ofType:(NSString *)fileType
264 		saveOperation:(NSSaveOperationType)saveOp;
265 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
266 - (NSArray *)writableTypesForSaveOperation:(NSSaveOperationType)op;
267 - (NSDictionary *)fileAttributesToWriteToURL:(NSURL *)url
268                                       ofType:(NSString *)type
269                             forSaveOperation:(NSSaveOperationType)op
270                          originalContentsURL:(NSURL *)original
271                                        error:(NSError **)error;
272 #endif
273 #if OS_API_VERSION(MAC_OS_X_VERSION_10_1, GS_API_LATEST)
274 - (BOOL)fileNameExtensionWasHiddenInLastRunSavePanel;
275 #endif
276 #if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST)
277 - (NSString *)fileNameExtensionForType:(NSString *)typeName
278                          saveOperation:(NSSaveOperationType)saveOperation;
279 #endif
280 
281 /*" Printing "*/
282 - (NSPrintInfo *)printInfo;
283 - (void)setPrintInfo:(NSPrintInfo *)printInfo;
284 - (BOOL)shouldChangePrintInfo:(NSPrintInfo *)newPrintInfo;
285 - (IBAction)runPageLayout:(id)sender;
286 - (NSInteger)runModalPageLayoutWithPrintInfo:(NSPrintInfo *)printInfo;
287 - (IBAction)printDocument:(id)sender;
288 - (void)printShowingPrintPanel:(BOOL)flag;
289 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
290 - (BOOL)preparePageLayout:(NSPageLayout *)pageLayout;
291 - (void)runModalPageLayoutWithPrintInfo:(NSPrintInfo *)info
292                                delegate:(id)delegate
293                          didRunSelector:(SEL)sel
294                             contextInfo:(void *)context;
295 - (void)printDocumentWithSettings:(NSDictionary *)settings
296                    showPrintPanel:(BOOL)flag
297                          delegate:(id)delegate
298                  didPrintSelector:(SEL)sel
299                       contextInfo:(void *)context;
300 - (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)settings
301                                            error:(NSError **)error;
302 - (void)runModalPrintOperation:(NSPrintOperation *)op
303                       delegate:(id)delegate
304                 didRunSelector:(SEL)sel
305                    contextInfo:(void *)context;
306 #endif
307 
308 /*" IB Actions "*/
309 - (IBAction)saveDocument:(id)sender;
310 - (IBAction)saveDocumentAs:(id)sender;
311 - (IBAction)saveDocumentTo:(id)sender;
312 - (IBAction)revertDocumentToSaved:(id)sender;
313 
314 /*" Menus "*/
315 - (BOOL)validateMenuItem:(NSMenuItem *)anItem;
316 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
317 
318 /*" Undo "*/
319 - (NSUndoManager *)undoManager;
320 - (void)setUndoManager:(NSUndoManager *)undoManager;
321 - (BOOL)hasUndoManager;
322 - (void)setHasUndoManager:(BOOL)flag;
323 
324 /* NEW delegate operations*/
325 - (void)saveToFile:(NSString *)fileName
326      saveOperation:(NSSaveOperationType)saveOperation
327 	  delegate:(id)delegate
328    didSaveSelector:(SEL)didSaveSelector
329        contextInfo:(void *)contextInfo;
330 - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel;
331 - (void)saveDocumentWithDelegate:(id)delegate
332 		 didSaveSelector:(SEL)didSaveSelector
333 		     contextInfo:(void *)contextInfo;
334 - (void)runModalSavePanelForSaveOperation:(NSSaveOperationType)saveOperation
335 				 delegate:(id)delegate
336 			  didSaveSelector:(SEL)didSaveSelector
337 			      contextInfo:(void *)contextInfo;
338 
339 #if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
340 - (BOOL)saveToURL:(NSURL *)url
341            ofType:(NSString *)type
342  forSaveOperation:(NSSaveOperationType)op
343             error:(NSError **)error;
344 - (void)saveToURL:(NSURL *)url
345            ofType:(NSString *)type
346  forSaveOperation:(NSSaveOperationType)op
347          delegate:(id)delegate
348   didSaveSelector:(SEL)didSaveSelector
349       contextInfo:(void *)contextInfo;
350 
351 /* Autosaving */
352 - (NSURL *)autosavedContentsFileURL;
353 - (void)setAutosavedContentsFileURL:(NSURL *)url;
354 - (void)autosaveDocumentWithDelegate:(id)delegate
355                  didAutosaveSelector:(SEL)didAutosaveSelector
356                          contextInfo:(void *)context;
357 - (NSString *)autosavingFileType;
358 - (BOOL)hasUnautosavedChanges;
359 
360 
361 - (BOOL)presentError:(NSError *)error;
362 - (void)presentError:(NSError *)error
363       modalForWindow:(NSWindow *)window
364             delegate:(id)delegate
365   didPresentSelector:(SEL)sel
366          contextInfo:(void *)context;
367 - (NSError *)willPresentError:(NSError *)error;
368 #endif
369 @end
370 
371 #endif // _GNUstep_H_NSDocument
372