/* Copyright (c) 2006-2007 Christopher J. W. Lloyd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #import #import #import #import #import #import #import #import #import #import #import #import "NSKeyValueBinding/NSObject+BindingSupport.h" @implementation NSDocument +(NSArray *)readableTypes { int i; NSArray *knownDocTypes = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDocumentTypes"]; NSMutableArray *readableTypes = [NSMutableArray array]; NSDictionary *typeDict; NSString *typeName, *typeRole; for (i = 0; i < [knownDocTypes count]; i++) { typeDict = [knownDocTypes objectAtIndex:i]; typeRole = [typeDict objectForKey:@"CFBundleTypeRole"]; if (NSClassFromString((NSString *)[typeDict objectForKey:@"NSDocumentClass"]) == self && ([typeRole isEqualToString:@"Viewer"] || [typeRole isEqualToString:@"Editor"])) { typeName = [typeDict objectForKey:@"CFBundleTypeName"]; if (typeName) [readableTypes addObject:typeName]; } } return [NSArray arrayWithArray:readableTypes]; } +(NSArray *)writableTypes { int i; NSArray *knownDocTypes = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDocumentTypes"]; NSMutableArray *writableTypes = [NSMutableArray array]; NSDictionary *typeDict; NSString *typeName; for (i = 0; i < [knownDocTypes count]; i++) { typeDict = [knownDocTypes objectAtIndex:i]; if (NSClassFromString((NSString *)[typeDict objectForKey:@"NSDocumentClass"]) == self && [(NSString *)[typeDict objectForKey:@"CFBundleTypeRole"] isEqualToString:@"Editor"]) { typeName = [typeDict objectForKey:@"CFBundleTypeName"]; if (typeName) [writableTypes addObject:typeName]; } } return [NSArray arrayWithArray:writableTypes]; } +(BOOL)isNativeType:(NSString *)type { BOOL result = NO; int i; NSArray *knownDocTypes = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDocumentTypes"]; NSDictionary *typeDict; for (i = 0; i < [knownDocTypes count]; i++) { typeDict = [knownDocTypes objectAtIndex:i]; result |= NSClassFromString((NSString *)[typeDict objectForKey:@"NSDocumentClass"]) == self && [(NSString *)[typeDict objectForKey:@"CFBundleTypeRole"] isEqualToString:@"Editor"] && [(NSString *)[typeDict objectForKey:@"CFBundleTypeName"] isEqualToString:type]; } return result; } -(BOOL)_isSelectorOverridden:(SEL)selector { IMP mine=[NSDocument instanceMethodForSelector:selector]; IMP theirs=[self methodForSelector:selector]; return (mine!=theirs)?YES:NO; } -init { self = [super init]; if (self) { _windowControllers=[NSMutableArray new]; _fileURL=nil; _fileType=nil; _changeCount=0; _untitledNumber=0; _hasUndoManager=YES; _activeEditors=[NSMutableArray new]; } return self; } -initWithType:(NSString *)type error:(NSError **)error { [self init]; [self setFileType:type]; return self; } -(void)_updateFileModificationDate { NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * path = [_fileURL path]; NSDictionary * attributes = [fileManager fileAttributesAtPath:path traverseLink:YES]; [self setFileModificationDate:[attributes objectForKey:NSFileModificationDate]]; } -initWithContentsOfURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)error { if([self _isSelectorOverridden:@selector(initWithContentsOfFile:ofType:)]){ if([self initWithContentsOfFile:[url path] ofType:type]==nil) return nil; } else { [self init]; if(![self readFromURL:url ofType:type error:error]){ [self dealloc]; return nil; } [self setFileURL:url]; [self setFileType:type]; } [self _updateFileModificationDate]; return self; } -initForURL:(NSURL *)url withContentsOfURL:(NSURL *)contentsURL ofType:(NSString *)type error:(NSError **)error { [self init]; if(contentsURL!=nil){ if(![self readFromURL:contentsURL ofType:type error:error]){ [self dealloc]; return nil; } } [self setFileURL:url]; [self setFileType:type]; [self _updateFileModificationDate]; return self; } -(void)dealloc { [_windowControllers release]; [_fileURL release]; [_fileType release]; [_fileModificationDate release]; [_lastComponentOfFileName release]; [_autosavedContentsFileURL release]; [_printInfo release]; [_undoManager release]; [_activeEditors release]; [super dealloc]; } -(NSURL *)autosavedContentsFileURL { return _autosavedContentsFileURL; } -(NSDate *)fileModificationDate { return _fileModificationDate; } -(NSURL *)fileURL { return _fileURL; } -(NSPrintInfo *)printInfo { return _printInfo; } -(NSString *)fileType { return _fileType; } -(BOOL)hasUndoManager { return _hasUndoManager; } -(NSUndoManager *)undoManager { if (_undoManager == nil && _hasUndoManager == YES) { [self setUndoManager:[NSUndoManager new]]; [_undoManager beginUndoGrouping]; } return _undoManager; } -(void)setAutosavedContentsFileURL:(NSURL *)url { url=[url copy]; [_autosavedContentsFileURL release]; _autosavedContentsFileURL=url; } -(void)setFileModificationDate:(NSDate *)value { value=[value copy]; [_fileModificationDate release]; _fileModificationDate=value; } -(void)setFileURL:(NSURL *)url { url=[url copy]; [_fileURL release]; _fileURL=url; [_windowControllers makeObjectsPerformSelector:@selector(synchronizeWindowTitleWithDocumentName)]; } -(void)setPrintInfo:(NSPrintInfo *)value { value=[value copy]; [_printInfo release]; _printInfo=value; } -(void)setFileType:(NSString *)type { type=[type copy]; [_fileType release]; _fileType=type; } -(void)setHasUndoManager:(BOOL)flag { _hasUndoManager = flag; if (flag == YES && _undoManager == nil) [self undoManager]; else if (flag == NO && _undoManager != nil) [self setUndoManager:nil]; } -(void)setUndoManager:(NSUndoManager *)undoManager { if (_undoManager != nil) { [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUndoManagerDidUndoChangeNotification object:_undoManager]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUndoManagerDidRedoChangeNotification object:_undoManager]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUndoManagerWillCloseUndoGroupNotification object:_undoManager]; [_undoManager release]; } _undoManager = [undoManager retain]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_undoManagerDidUndoChange:) name:NSUndoManagerDidUndoChangeNotification object:_undoManager]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_undoManagerDidRedoChange:) name:NSUndoManagerDidRedoChangeNotification object:_undoManager]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_undoManagerDidCloseGroup:) name:NSUndoManagerWillCloseUndoGroupNotification object:_undoManager]; } -(NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window { if (_hasUndoManager) return [self undoManager]; return nil; } -(BOOL)hasUnautosavedChanges { NSUnimplementedMethod(); return 0; } -(NSString *)autosavingFileType { return [self fileType]; } -(void)setLastComponentOfFileName:(NSString *)name { name=[name copy]; [_lastComponentOfFileName release]; _lastComponentOfFileName=name; } -(NSString *)windowNibName { return nil; } -(void)setWindow:(NSWindow *)window { [[_windowControllers objectAtIndex:0] setWindow:window]; [window release]; } -(void)windowControllerDidLoadNib:(NSWindowController *)controller { // do nothing } -(void)windowControllerWillLoadNib:(NSWindowController *)controller { // do nothing } -(void)showWindows { [_windowControllers makeObjectsPerformSelector:@selector(showWindow:) withObject:self]; } -(void)makeWindowControllers { NSString *nibName=[self windowNibName]; if(nibName!=nil){ NSWindowController *controller=[[[NSWindowController alloc] initWithWindowNibName:nibName owner:self] autorelease]; [self addWindowController:controller]; } } -(NSArray *)windowControllers { return _windowControllers; } -(void)addWindowController:(NSWindowController *)controller { [_windowControllers addObject:controller]; if([controller document]==nil) [controller setDocument:self]; } -(void)removeWindowController:(NSWindowController *)controller { BOOL shouldCloseDocument = [controller shouldCloseDocument]; [_windowControllers removeObjectIdenticalTo:controller]; if (shouldCloseDocument || [_windowControllers count] == 0) [self close]; } -(NSString *)displayName { if(_fileURL==nil) { if(_untitledNumber > 1) return [NSString stringWithFormat:@"Untitled %d", _untitledNumber]; else return @"Untitled"; } else { return [[NSFileManager defaultManager] displayNameAtPath:[_fileURL path]]; } } -(NSWindow *)windowForSheet { if([_windowControllers count]>0){ NSWindow *check=[[_windowControllers objectAtIndex:0] window]; if(check!=nil) return check; } return [NSApp mainWindow]; } -(BOOL)isDocumentEdited { return _changeCount != 0 || [_activeEditors count] > 0; } -(void)updateChangeCount:(NSDocumentChangeType)changeType { int count=[_windowControllers count]; switch(changeType){ case NSChangeDone: _changeCount++; break; case NSChangeUndone: _changeCount--; break; case NSChangeCleared: _changeCount=0; [self _updateFileModificationDate]; // Since file was just saved or reverted break; } BOOL edited = [self isDocumentEdited]; while(--count>=0) [[_windowControllers objectAtIndex:count] setDocumentEdited:edited]; } -(BOOL)readFromData:(NSData *)data ofType:(NSString *)type error:(NSError **)error { if([self _isSelectorOverridden:@selector(loadDataRepresentation:ofType:)]) return [self loadDataRepresentation:data ofType:type]; else { [NSException raise:NSInternalInconsistencyException format:@"-[%@ %s]",isa,sel_getName(_cmd)]; return NO; } } -(BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)type error:(NSError **)error { if([self _isSelectorOverridden:@selector(loadFileWrapperRepresentation:ofType:)]) return [self loadFileWrapperRepresentation:fileWrapper ofType:type]; else return [self readFromData:[fileWrapper regularFileContents] ofType:type error:error]; } -(BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)error { if([url isFileURL]){ if([self _isSelectorOverridden:@selector(readFromFile:ofType:)]){ return [self readFromFile:[url path] ofType:type]; } else { NSFileWrapper *fileWrapper=[[[NSFileWrapper alloc] initWithPath:[url path]] autorelease]; return [self readFromFileWrapper:fileWrapper ofType:type error:error]; } } return NO; } -(BOOL)revertToContentsOfURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)error { if(![self readFromURL:url ofType:type error:error]) return NO; [self updateChangeCount:NSChangeCleared]; return YES; } -(NSData *)dataOfType:(NSString *)type error:(NSError **)error { if([self _isSelectorOverridden:@selector(dataRepresentationOfType:)]) return [self dataRepresentationOfType:type]; [NSException raise:NSInternalInconsistencyException format:@"-[%@ %s]",isa,sel_getName(_cmd)]; return nil; } -(NSFileWrapper *)fileWrapperOfType:(NSString *)type error:(NSError **)error { if([self _isSelectorOverridden:@selector(fileWrapperRepresentationOfType:)]) return [self fileWrapperRepresentationOfType:type]; else { NSData *data=[self dataOfType:type error:error]; if(data==nil) return nil; return [[[NSFileWrapper alloc] initRegularFileWithContents:data] autorelease]; } } -(BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)error { if([self _isSelectorOverridden:@selector(writeToFile:ofType:)]){ return [self writeToFile:[url path] ofType:type]; } else { NSFileWrapper *wrapper=[self fileWrapperOfType:type error:error]; if(wrapper==nil) return NO; if(![wrapper writeToFile:[url path] atomically:YES updateFilenames:YES]) return NO; return YES; } } -(BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type forSaveOperation:(NSSaveOperationType)operation originalContentsURL:(NSURL *)contentsURL error:(NSError **)error { if([self _isSelectorOverridden:@selector(writeToFile:ofType:originalFile:saveOperation:)]){ return [self writeToFile:[url path] ofType:type originalFile:[contentsURL path] saveOperation:operation]; } else { return [self writeToURL:url ofType:type error:error]; } } -(BOOL)writeSafelyToURL:(NSURL *)url ofType:(NSString *)type forSaveOperation:(NSSaveOperationType)operation error:(NSError **)error { if(![self writeToURL:url ofType:type forSaveOperation:operation originalContentsURL:url error:error]) return NO; NSDictionary *attributes=[self fileAttributesToWriteToURL:url ofType:type forSaveOperation:operation originalContentsURL:url error:error]; if([attributes count]) [[NSFileManager defaultManager] changeFileAttributes:attributes atPath:[url path]]; return YES; } -(NSDictionary *)fileAttributesToWriteToURL:(NSURL *)url ofType:(NSString *)type forSaveOperation:(NSSaveOperationType)operation originalContentsURL:(NSURL *)contentsURL error:(NSError **)error { NSMutableDictionary *result=[NSMutableDictionary dictionary]; return result; } -(BOOL)keepBackupFile { return NO; } -(void)autosaveDocumentWithDelegate:delegate didAutosaveSelector:(SEL)selector contextInfo:(void *)info { NSError *error; if(![self writeToURL:[self autosavedContentsFileURL] ofType:[self autosavingFileType] forSaveOperation:NSAutosaveOperation originalContentsURL:[self fileURL] error:&error]){ } NSUnimplementedMethod(); } -(NSError *)willPresentError:(NSError *)error { // do nothing return error; } -(BOOL)presentError:(NSError *)error { return [[NSDocumentController sharedDocumentController] presentError:[self willPresentError:error]]; } -(void)presentError:(NSError *)error modalForWindow:(NSWindow *)window delegate:delegate didPresentSelector:(SEL)selector contextInfo:(void *)info { [[NSDocumentController sharedDocumentController] presentError:[self willPresentError:error] modalForWindow:window delegate:delegate didPresentSelector:selector contextInfo:info]; } -(NSArray *)writableTypesForSaveOperation:(NSSaveOperationType)operation { NSArray *result=[[self class] writableTypes]; if(operation==NSSaveToOperation){ NSMutableArray *filtered=[NSMutableArray array]; int i,count=[result count]; for(i=0;i=0) [[_windowControllers objectAtIndex:count] close]; [[NSDocumentController sharedDocumentController] removeDocument:self]; } -(void)canCloseDocumentWithDelegate:delegate shouldCloseSelector:(SEL)selector contextInfo:(void *)info { BOOL OKToClose; if ([self isDocumentEdited]) { NSString * fileName = [self fileName]; if (fileName == nil) fileName = [self displayName]; int result = NSRunAlertPanel([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"], @"The %@ file has changed. \n\nDo you want to save the changes?", @"Yes", @"No", @"Cancel", fileName); if (result == NSAlertDefaultReturn) { [self saveDocumentWithDelegate:delegate didSaveSelector:selector contextInfo:info]; return; } else if (result == NSAlertAlternateReturn) { // Don't save OKToClose = YES; } else { // Canceled OKToClose = NO; } } else { // No unsaved changes OKToClose = YES; } if ([delegate respondsToSelector:selector]) { void (*delegateMethod)(id, SEL, id, BOOL, void *); delegateMethod = (void (*)(id, SEL, id, BOOL, void *))[delegate methodForSelector:selector]; delegateMethod(delegate, selector, self, OKToClose, info); } } -(void)shouldCloseWindowController:(NSWindowController *)controller delegate:delegate shouldCloseSelector:(SEL)selector contextInfo:(void *)info { if ([controller shouldCloseDocument] || [_windowControllers count] <= 1) { [self canCloseDocumentWithDelegate:delegate shouldCloseSelector:selector contextInfo:info]; } else if ([delegate respondsToSelector:selector]) { void (*delegateMethod)(id, SEL, id, BOOL, void *); delegateMethod = (void (*)(id, SEL, id, BOOL, void *))[delegate methodForSelector:selector]; delegateMethod(delegate, selector, self, YES, info); } } -(void)revertDocumentToSaved:sender { int result=NSRunAlertPanel(nil,@"%@ has been edited. Are you sure you want to undo changes?", @"Revert",@"Cancel",nil,[self displayName]); if(result==NSAlertDefaultReturn) { for (id editor in [_activeEditors copy]) [editor discardEditing]; [self revertToSavedFromFile:[self fileName] ofType:[self fileType]]; } } -(void)saveDocument:sender { [self saveDocumentWithDelegate:nil didSaveSelector:NULL contextInfo:NULL]; } -(void)saveDocumentAs:sender { [self runModalSavePanelForSaveOperation:NSSaveAsOperation delegate:nil didSaveSelector:NULL contextInfo:NULL]; } -(void)saveDocumentTo:sender { [self runModalSavePanelForSaveOperation:NSSaveToOperation delegate:nil didSaveSelector:NULL contextInfo:NULL]; } -(void)printDocument:sender { [self printDocumentWithSettings:nil showPrintPanel:YES delegate:nil didPrintSelector:NULL contextInfo:NULL]; } -(void)runPageLayout:sender { [[NSPageLayout pageLayout] runModal]; } -(BOOL)validateUserInterfaceItem:(id )item { if([item action]==@selector(revertDocumentToSaved:)) return (_fileURL!=nil)?YES:NO; if([item action]==@selector(saveDocument:)) return YES; if([self respondsToSelector:[item action]]) return YES; return NO; } -(void)objectDidBeginEditing:editor { [_activeEditors addObject:editor]; BOOL edited = [self isDocumentEdited]; for (NSWindowController * wc in _windowControllers) [wc setDocumentEdited:edited]; } -(void)objectDidEndEditing:editor { [_activeEditors removeObject:editor]; BOOL edited = [self isDocumentEdited]; for (NSWindowController * wc in _windowControllers) [wc setDocumentEdited:edited]; } -(BOOL)validateMenuItem:(NSMenuItem *)item { if([item action]==@selector(revertDocumentToSaved:)) return (_fileURL!=nil)?YES:NO; if([item action]==@selector(saveDocument:)) return YES; if([self respondsToSelector:[item action]]) return YES; return NO; } -(BOOL)canCloseDocument { return YES; } -(NSData *)dataRepresentationOfType:(NSString *)type { [NSException raise:NSInternalInconsistencyException format:@"-[%@ %s]",isa,sel_getName(_cmd)]; return nil; } -(NSDictionary *)fileAttributesToWriteToFile:(NSString *)path ofType:(NSString *)type saveOperation:(NSSaveOperationType)operation { return [NSDictionary dictionary]; } -(NSString *)fileName { return [_fileURL path]; } -(NSString *)fileNameFromRunningSavePanelForSaveOperation:(NSSaveOperationType)operation { NSUnimplementedMethod(); return nil; } -(NSFileWrapper *)fileWrapperRepresentationOfType:(NSString *)type { NSData *data=[self dataRepresentationOfType:type]; if(data==nil) return nil; return [[[NSFileWrapper alloc] initRegularFileWithContents:data] autorelease]; } -initWithContentsOfFile:(NSString *)path ofType:(NSString *)type { NSURL *url=[NSURL fileURLWithPath:path]; NSError *error; [self init]; error=nil; if(![self readFromURL:url ofType:type error:&error]){ NSRunAlertPanel(nil,@"Can't open file '%@'. Error = %@",@"Ok",nil,nil,path,error); [self dealloc]; return nil; } [self setFileName:path]; [self setFileType:type]; [self _updateFileModificationDate]; return self; } -initWithContentsOfURL:(NSURL *)url ofType:(NSString *)type { NSError *error; [self init]; error=nil; if(![self readFromURL:url ofType:type error:&error]){ NSRunAlertPanel(nil,@"Can't open URL '%@'. Error = %@",@"Ok",nil,nil,url,error); [self dealloc]; return nil; } [self setFileURL:url]; [self setFileType:type]; [self _updateFileModificationDate]; return self; } -(BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)type { [NSException raise:NSInternalInconsistencyException format:@"-[%@ %s]",isa,sel_getName(_cmd)]; return NO; } -(BOOL)loadFileWrapperRepresentation:(NSFileWrapper *)fileWrapper ofType:(NSString *)type { if([fileWrapper isRegularFile]) return [self loadDataRepresentation:[fileWrapper regularFileContents] ofType:type]; return NO; } -(void)printShowingPrintPanel:(BOOL)flag { // do nothing } -(BOOL)readFromFile:(NSString *)path ofType:(NSString *)type { NSData *data=[[NSData alloc] initWithContentsOfFile:path]; if(data==nil) return NO; if(![self loadDataRepresentation:data ofType:type]){ [data release]; return NO; } [data release]; return YES; } -(BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type { return [self readFromFile:[url path] ofType:type]; } -(BOOL)revertToSavedFromFile:(NSString *)path ofType:(NSString *)type { if([self readFromFile:path ofType:type]){ [self updateChangeCount:NSChangeCleared]; return YES; } return NO; } -(BOOL)revertToSavedFromURL:(NSURL *)url ofType:(NSString *)type { if([self readFromURL:url ofType:type]){ [self updateChangeCount:NSChangeCleared]; return YES; } return NO; } -(int)runModalSavePanel:(NSSavePanel *)savePanel withAccessoryView:(NSView *)accessoryView { NSUnimplementedMethod(); return 0; } -(int)runModalPageLayoutWithPrintInfo:(NSPrintInfo *)printInfo { return [[NSPageLayout pageLayout] runModalWithPrintInfo:printInfo]; } -(void)setFileName:(NSString *)path { if (path!=nil) [self setFileURL:[NSURL fileURLWithPath:path]]; else [self setFileURL:nil]; } -(void)saveToFile:(NSString *)path saveOperation:(NSSaveOperationType)operation delegate:delegate didSaveSelector:(SEL)selector contextInfo:(void *)context { if(path!=nil){ BOOL success=[self writeWithBackupToFile:path ofType:_fileType saveOperation:operation]; if(success){ if(operation!=NSSaveToOperation) [self setFileName:path]; [self updateChangeCount:NSChangeCleared]; } if ([delegate respondsToSelector:selector]) { void (*delegateMethod)(id, SEL, id, BOOL, void *); delegateMethod = (void (*)(id, SEL, id, BOOL, void *))[delegate methodForSelector:selector]; delegateMethod(delegate, selector, self, success, context); } } } -(BOOL)shouldCloseWindowController:(NSWindowController *)controller { if(![controller shouldCloseDocument]) return NO; [self canCloseDocumentWithDelegate:nil shouldCloseSelector:NULL contextInfo:NULL]; return YES; } -(BOOL)writeToFile:(NSString *)path ofType:(NSString *)type { NSData *data=[self dataRepresentationOfType:type]; return [data writeToFile:path atomically:YES]; } -(BOOL)writeToFile:(NSString *)path ofType:(NSString *)type originalFile:(NSString *)original saveOperation:(NSSaveOperationType)operation { NSUnimplementedMethod(); return 0; } -(BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type { NSUnimplementedMethod(); return 0; } -(BOOL)writeWithBackupToFile:(NSString *)path ofType:(NSString *)type saveOperation:(NSSaveOperationType)operation { // move original to backup if(![self writeToFile:path ofType:type]) return NO; if(![self keepBackupFile]){ // delete backup } return YES; } -(void)_setUntitledNumber:(int)number { _untitledNumber=number; } -(void)_undoManagerDidUndoChange:(NSNotification *)note { [self updateChangeCount:NSChangeUndone]; } -(void)_undoManagerDidRedoChange:(NSNotification *)note { [self updateChangeCount:NSChangeDone]; } -(void)_undoManagerDidCloseGroup:(NSNotification *)note { [self updateChangeCount:NSChangeDone]; } -(BOOL)windowShouldClose:sender { if([[NSUserDefaults standardUserDefaults] boolForKey:@"useSheets"]){ NSBeginAlertSheet(nil,@"Save",@"Don't Save",@"Cancel",sender,self,@selector(didEndShouldCloseSheet:returnCode:contextInfo:),NULL,sender,@"%@ has changed. Save?",[self displayName]); return NO; } else { if(![self isDocumentEdited]) return YES; else { int result=NSRunAlertPanel(nil,@"%@ has changed. Save?",@"Save",@"Don't Save",@"Cancel",[self displayName]); switch(result){ case NSAlertDefaultReturn: [self saveDocument:nil]; return YES; case NSAlertAlternateReturn: return YES; case NSAlertOtherReturn: default: return NO; } } } } -(void)didEndShouldCloseSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo { NSWindow *window=(NSWindow *)contextInfo; switch(returnCode){ case NSAlertDefaultReturn: [self saveDocument:nil]; [window close]; break; case NSAlertAlternateReturn: [window close]; break; case NSAlertOtherReturn: default: break; } } #if 0 -(void)windowWillClose:(NSNotification *)note { NSWindow *window=[note object]; int count=[_windowControllers count]; while(--count>=0){ NSWindowController *controller=[_windowControllers objectAtIndex:count]; if([controller isWindowLoaded] && window==[controller window]){ BOOL closeMe = [controller shouldCloseDocument]; [_windowControllers removeObjectAtIndex:count]; if (closeMe) [self close]; return; } } } #endif - (void)textDidChange:(NSNotification *)aNotification { [self updateChangeCount:NSChangeDone]; } @end