diff --git a/ChangeLog b/ChangeLog index 16a59993..b88c91fb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Jan 4 17:42:00 2000 Richard Frith-Macdonald + + Added 'miniaturize', 'close', and 'revert to saved' menu items and + implemented their actions. + Tue Jan 4 12:13:00 2000 Richard Frith-Macdonald Various tidyups diff --git a/Gorm.m b/Gorm.m index ecfa352b..48924828 100644 --- a/Gorm.m +++ b/Gorm.m @@ -478,7 +478,13 @@ NSString *GormLinkPboardType = @"GormLinkPboardType"; - (id) revertToSaved: (id)sender { - NSLog(@"Revert to save not yet implemented"); + id doc = [(id)[self activeDocument] revertDocument: sender]; + + if (doc != nil) + { + [documents addObject: doc]; + [[doc window] makeKeyAndOrderFront: self]; + } return nil; } @@ -653,7 +659,8 @@ NSString *GormLinkPboardType = @"GormLinkPboardType"; if (sel_eq(action, @selector(revertToSaved:))) { - if (active == nil) + if (active == nil || [active documentPath] == nil + || [[active window] isDocumentEdited] == NO) return NO; } diff --git a/GormDocument.h b/GormDocument.h index a791b5a7..4b2a8b7b 100644 --- a/GormDocument.h +++ b/GormDocument.h @@ -65,12 +65,14 @@ - (id) objectForName: (NSString*)aString; - (BOOL) objectIsVisibleAtLaunch: (id)anObject; - (NSArray*) objects; +- (id) loadDocument: (NSString*)path; - (id) openDocument: (id)sender; - (id) parentOfObject: (id)anObject; - (NSArray*) pasteType: (NSString*)aType fromPasteboard: (NSPasteboard*)aPasteboard parent: (id)parent; - (void) removeConnector: (id)aConnector; +- (id) revertDocument: (id)sender; - (id) saveAsDocument: (id)sender; - (id) saveDocument: (id)sender; - (void) setDocumentActive: (BOOL)flag; diff --git a/GormDocument.m b/GormDocument.m index 43362d27..c49a5e89 100644 --- a/GormDocument.m +++ b/GormDocument.m @@ -862,6 +862,108 @@ static NSImage *classesImage = nil; return [nameTable allValues]; } +/* + * NB. This assumes we have an empty document to start with - the loaded + * document is merged in to it. + */ +- (id) loadDocument: (NSString*)aFile +{ + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; + NSMutableDictionary *nt; + NSData *data; + NSUnarchiver *u; + GSNibContainer *c; + NSEnumerator *enumerator; + id con; + NSString *name; + + data = [NSData dataWithContentsOfFile: aFile]; + if (data == nil) + { + NSRunAlertPanel(NULL, + [NSString stringWithFormat: @"Could not read '%@' data", aFile], + @"OK", NULL, NULL); + return nil; + } + + /* + * Create an unarchiver, and use it to unarchive the nib file while + * handling class replacement so that standard objects understood + * by the gui library are converted to their Gorm internal equivalents. + */ + u = AUTORELEASE([[NSUnarchiver alloc] initForReadingWithData: data]); + [u decodeClassName: @"GSNibContainer" + asClassName: @"GormDocument"]; + c = [u decodeObject]; + if (c == nil || [c isKindOfClass: [GSNibContainer class]] == NO) + { + NSRunAlertPanel(NULL, @"Could not unarchive document data", + @"OK", NULL, NULL); + return nil; + } + + /* + * In the newly loaded nib container, we change all the connectors + * to hold the objects rather than their names (using our own dummy + * object as the 'NSOwner'. + */ + [[c nameTable] setObject: filesOwner forKey: @"NSOwner"]; + [[c nameTable] setObject: firstResponder forKey: @"NSFirst"]; + nt = [c nameTable]; + enumerator = [[c connections] objectEnumerator]; + while ((con = [enumerator nextObject]) != nil) + { + NSString *name; + id obj; + + name = (NSString*)[con source]; + obj = [nt objectForKey: name]; + [con setSource: obj]; + name = (NSString*)[con destination]; + obj = [nt objectForKey: name]; + [con setDestination: obj]; + } + + /* + * Now we merge the objects from the nib container into our own data + * structures, taking care not to overwrite our NSOwner and NSFirst. + */ + [nt removeObjectForKey: @"NSOwner"]; + [nt removeObjectForKey: @"NSFirst"]; + [connections addObjectsFromArray: [c connections]]; + [nameTable addEntriesFromDictionary: nt]; + + /* + * Now we build our reverse mapping information and other initialisation + */ + NSResetMapTable(objToName); + NSMapInsert(objToName, (void*)filesOwner, (void*)@"NSOwner"); + NSMapInsert(objToName, (void*)firstResponder, (void*)@"NSFirst"); + enumerator = [nameTable keyEnumerator]; + while ((name = [enumerator nextObject]) != nil) + { + id obj = [nameTable objectForKey: name]; + + NSMapInsert(objToName, (void*)obj, (void*)name); + + if ([obj isKindOfClass: [NSWindow class]] == YES + || [obj isKindOfClass: [NSMenu class]] == YES) + { + [objectsView addObject: obj]; + [[self openEditorForObject: obj] activate]; + } + } + + /* + * Finally, we set our new file name + */ + ASSIGN(documentPath, aFile); + [window setTitleWithRepresentedFilename: documentPath]; + [nc postNotificationName: IBDidOpenDocumentNotification + object: self]; + return self; +} + /* * NB. This assumes we have an empty document to start with - the loaded * document is merged in to it. @@ -888,101 +990,7 @@ static NSImage *classesImage = nil; types: fileTypes]; if (result == NSOKButton) { - NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; - NSMutableDictionary *nt; - NSString *aFile = [oPanel filename]; - NSData *data; - NSUnarchiver *u; - GSNibContainer *c; - NSEnumerator *enumerator; - id con; - NSString *name; - - data = [NSData dataWithContentsOfFile: aFile]; - if (data == nil) - { - NSRunAlertPanel(NULL, - [NSString stringWithFormat: @"Could not read '%@' data", aFile], - @"OK", NULL, NULL); - return nil; - } - - /* - * Create an unarchiver, and use it to unarchive the nib file while - * handling class replacement so that standard objects understood - * by the gui library are converted to their Gorm internal equivalents. - */ - u = AUTORELEASE([[NSUnarchiver alloc] initForReadingWithData: data]); - [u decodeClassName: @"GSNibContainer" - asClassName: @"GormDocument"]; - c = [u decodeObject]; - if (c == nil || [c isKindOfClass: [GSNibContainer class]] == NO) - { - NSRunAlertPanel(NULL, @"Could not unarchive document data", - @"OK", NULL, NULL); - return nil; - } - - /* - * In the newly loaded nib container, we change all the connectors - * to hold the objects rather than their names (using our own dummy - * object as the 'NSOwner'. - */ - [[c nameTable] setObject: filesOwner forKey: @"NSOwner"]; - [[c nameTable] setObject: firstResponder forKey: @"NSFirst"]; - nt = [c nameTable]; - enumerator = [[c connections] objectEnumerator]; - while ((con = [enumerator nextObject]) != nil) - { - NSString *name; - id obj; - - name = (NSString*)[con source]; - obj = [nt objectForKey: name]; - [con setSource: obj]; - name = (NSString*)[con destination]; - obj = [nt objectForKey: name]; - [con setDestination: obj]; - } - - /* - * Now we merge the objects from the nib container into our own data - * structures, taking care not to overwrite our NSOwner and NSFirst. - */ - [nt removeObjectForKey: @"NSOwner"]; - [nt removeObjectForKey: @"NSFirst"]; - [connections addObjectsFromArray: [c connections]]; - [nameTable addEntriesFromDictionary: nt]; - - /* - * Now we build our reverse mapping information and other initialisation - */ - NSResetMapTable(objToName); - NSMapInsert(objToName, (void*)filesOwner, (void*)@"NSOwner"); - NSMapInsert(objToName, (void*)firstResponder, (void*)@"NSFirst"); - enumerator = [nameTable keyEnumerator]; - while ((name = [enumerator nextObject]) != nil) - { - id obj = [nameTable objectForKey: name]; - - NSMapInsert(objToName, (void*)obj, (void*)name); - - if ([obj isKindOfClass: [NSWindow class]] == YES - || [obj isKindOfClass: [NSMenu class]] == YES) - { - [objectsView addObject: obj]; - [[self openEditorForObject: obj] activate]; - } - } - - /* - * Finally, we set our new file name - */ - ASSIGN(documentPath, aFile); - [window setTitleWithRepresentedFilename: documentPath]; - [nc postNotificationName: IBDidOpenDocumentNotification - object: self]; - return self; + return [self loadDocument: [oPanel filename]]; } return nil; /* Failed */ } @@ -1191,6 +1199,26 @@ static NSImage *classesImage = nil; } } +/* + * To revert to a saved version, we actually load a new document and + * close the original document, returning the id of the new document. + */ +- (id) revertDocument: (id)sender +{ + GormDocument *reverted = AUTORELEASE([GormDocument new]); + + if ([reverted loadDocument: documentPath] != nil) + { + NSRect frame = [window frame]; + + [window setReleasedWhenClosed: YES]; + [window close]; + [[reverted window] setFrame: frame display: YES]; + return reverted; + } + return nil; +} + - (id) saveAsDocument: (id)sender { NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; @@ -1255,7 +1283,7 @@ static NSImage *classesImage = nil; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; BOOL archiveResult; - if (documentPath == nil || [documentPath isEqualToString: @""]) + if (documentPath == nil) { return [self saveAsDocument: sender]; } @@ -1376,7 +1404,7 @@ static NSImage *classesImage = nil; NSString *msg; int result; - if (documentPath == nil || [documentPath isEqualToString: @""]) + if (documentPath == nil) { msg = @"Document 'UNTITLED' has been modified"; }