1999-12-08 15:04:57 +00:00
|
|
|
|
/* GormDocument.m
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 1999 Free Software Foundation, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Author: Richard Frith-Macdonald <richard@brainstrom.co.uk>
|
|
|
|
|
* Date: 1999
|
|
|
|
|
*
|
|
|
|
|
* This file is part of GNUstep.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "GormPrivate.h"
|
|
|
|
|
|
1999-12-08 15:19:11 +00:00
|
|
|
|
NSString *IBDidOpenDocumentNotification = @"IBDidOpenDocumentNotification";
|
|
|
|
|
NSString *IBWillSaveDocumentNotification = @"IBWillSaveDocumentNotification";
|
|
|
|
|
NSString *IBDidSaveDocumentNotification = @"IBDidSaveDocumentNotification";
|
|
|
|
|
NSString *IBWillCloseDocumentNotification = @"IBWillCloseDocumentNotification";
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
/*
|
1999-12-13 17:01:35 +00:00
|
|
|
|
* Each document has a GormFilesOwner object that is used as a placeholder
|
|
|
|
|
* for the owner of the document.
|
1999-12-08 15:04:57 +00:00
|
|
|
|
*/
|
1999-12-13 17:01:35 +00:00
|
|
|
|
@interface GormFilesOwner : NSObject
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation GormFilesOwner
|
|
|
|
|
- (NSImage*) imageForViewer
|
|
|
|
|
{
|
|
|
|
|
static NSImage *image = nil;
|
|
|
|
|
|
|
|
|
|
if (image == nil)
|
|
|
|
|
{
|
|
|
|
|
NSBundle *bundle = [NSBundle mainBundle];
|
|
|
|
|
NSString *path = [bundle pathForImageResource: @"GormFilesOwner"];
|
|
|
|
|
|
|
|
|
|
image = [[NSImage alloc] initWithContentsOfFile: path];
|
|
|
|
|
}
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Each document has a GormFirstResponder object that is used as a placeholder
|
|
|
|
|
* for the first responder at any instant.
|
|
|
|
|
*/
|
|
|
|
|
@interface GormFirstResponder : NSObject
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation GormFirstResponder
|
|
|
|
|
- (NSImage*) imageForViewer
|
|
|
|
|
{
|
|
|
|
|
static NSImage *image = nil;
|
|
|
|
|
|
|
|
|
|
if (image == nil)
|
|
|
|
|
{
|
|
|
|
|
NSBundle *bundle = [NSBundle mainBundle];
|
|
|
|
|
NSString *path = [bundle pathForImageResource: @"GormFirstResponder"];
|
|
|
|
|
|
|
|
|
|
image = [[NSImage alloc] initWithContentsOfFile: path];
|
|
|
|
|
}
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Each document may have a GormFontManager object that is used as a
|
|
|
|
|
* placeholder for the current fornt manager.
|
|
|
|
|
*/
|
|
|
|
|
@interface GormFontManager : NSObject
|
|
|
|
|
{
|
|
|
|
|
}
|
1999-12-08 15:04:57 +00:00
|
|
|
|
@end
|
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
@implementation GormFontManager
|
|
|
|
|
- (NSImage*) imageForViewer
|
|
|
|
|
{
|
|
|
|
|
static NSImage *image = nil;
|
|
|
|
|
|
|
|
|
|
if (image == nil)
|
|
|
|
|
{
|
|
|
|
|
NSBundle *bundle = [NSBundle mainBundle];
|
|
|
|
|
NSString *path = [bundle pathForImageResource: @"GormFontManager"];
|
|
|
|
|
|
|
|
|
|
image = [[NSImage alloc] initWithContentsOfFile: path];
|
|
|
|
|
}
|
|
|
|
|
return image;
|
|
|
|
|
}
|
1999-12-08 15:04:57 +00:00
|
|
|
|
@end
|
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
|
|
|
|
|
@implementation GormDocument
|
|
|
|
|
|
|
|
|
|
- (void) addConnector: (id<IBConnectors>)aConnector
|
|
|
|
|
{
|
|
|
|
|
if ([connections indexOfObjectIdenticalTo: aConnector] == NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
[connections addObject: aConnector];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) allConnectors
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([connections copy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) attachObject: (id)anObject toParent: (id)aParent
|
|
|
|
|
{
|
|
|
|
|
NSArray *old;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create a connector that links this object to its parent.
|
|
|
|
|
* A nil parent is the root of the hierarchy so we use a dummy object for it.
|
|
|
|
|
*/
|
|
|
|
|
if (aParent == nil)
|
|
|
|
|
{
|
1999-12-13 17:01:35 +00:00
|
|
|
|
aParent = filesOwner;
|
1999-12-08 15:04:57 +00:00
|
|
|
|
}
|
1999-12-13 17:01:35 +00:00
|
|
|
|
old = [self connectorsForSource: anObject ofClass: [NSNibConnector class]];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
if ([old count] > 0)
|
|
|
|
|
{
|
|
|
|
|
[[old objectAtIndex: 0] setDestination: aParent];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
1999-12-13 17:01:35 +00:00
|
|
|
|
NSNibConnector *con = [NSNibConnector new];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
|
|
|
|
|
[con setSource: anObject];
|
|
|
|
|
[con setDestination: aParent];
|
|
|
|
|
[self addConnector: (id<IBConnectors>)con];
|
|
|
|
|
RELEASE(con);
|
|
|
|
|
}
|
|
|
|
|
[self setName: nil forObject: anObject];
|
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
if ([anObject isKindOfClass: [NSWindow class]] == YES
|
|
|
|
|
|| [anObject isKindOfClass: [NSMenu class]] == YES)
|
1999-12-08 15:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
[resourcesManager addObject: anObject];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) attachObjects: (NSArray*)anArray toParent: (id)aParent
|
|
|
|
|
{
|
|
|
|
|
NSEnumerator *enumerator = [anArray objectEnumerator];
|
|
|
|
|
NSObject *obj;
|
|
|
|
|
|
|
|
|
|
while ((obj = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
[self attachObject: obj toParent: aParent];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
/*
|
|
|
|
|
* A Gorm document is encoded in the archive as a GSNibContainer ...
|
|
|
|
|
* A class that the gnustep gui library knbows about and can unarchive.
|
|
|
|
|
*/
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
|
|
|
|
return [GSNibContainer class];
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
- (NSArray*) connectorsForDestination: (id)destination
|
|
|
|
|
{
|
|
|
|
|
return [self connectorsForDestination: destination ofClass: 0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) connectorsForDestination: (id)destination
|
|
|
|
|
ofClass: (Class)aConnectorClass
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 16];
|
|
|
|
|
NSEnumerator *enumerator = [connections objectEnumerator];
|
|
|
|
|
id<IBConnectors> c;
|
|
|
|
|
|
|
|
|
|
while ((c = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([c destination] == destination
|
|
|
|
|
&& (aConnectorClass == 0 || aConnectorClass == [c class]))
|
|
|
|
|
{
|
|
|
|
|
[array addObject: c];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) connectorsForSource: (id)source
|
|
|
|
|
{
|
|
|
|
|
return [self connectorsForSource: source ofClass: 0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) connectorsForSource: (id)source
|
|
|
|
|
ofClass: (Class)aConnectorClass
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 16];
|
|
|
|
|
NSEnumerator *enumerator = [connections objectEnumerator];
|
|
|
|
|
id<IBConnectors> c;
|
|
|
|
|
|
|
|
|
|
while ((c = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([c source] == source
|
|
|
|
|
&& (aConnectorClass == 0 || aConnectorClass == [c class]))
|
|
|
|
|
{
|
|
|
|
|
[array addObject: c];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) containsObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
if ([self nameForObject: anObject] == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) containsObjectWithName: (NSString*)aName forParent: (id)parent
|
|
|
|
|
{
|
|
|
|
|
id obj = [nameTable objectForKey: aName];
|
|
|
|
|
|
|
|
|
|
if (obj == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
/* FIXME */
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) copyObject: (id)anObject
|
|
|
|
|
type: (NSString*)aType
|
|
|
|
|
toPasteboard: (NSPasteboard*)aPasteboard
|
|
|
|
|
{
|
|
|
|
|
return [self copyObjects: [NSArray arrayWithObject: anObject]
|
|
|
|
|
type: aType
|
|
|
|
|
toPasteboard: aPasteboard];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) copyObjects: (NSArray*)anArray
|
|
|
|
|
type: (NSString*)aType
|
|
|
|
|
toPasteboard: (NSPasteboard*)aPasteboard
|
|
|
|
|
{
|
|
|
|
|
NSData *obj = [NSArchiver archivedDataWithRootObject: anArray];
|
|
|
|
|
|
|
|
|
|
[aPasteboard declareTypes: [NSArray arrayWithObject: aType]
|
|
|
|
|
owner: self];
|
|
|
|
|
return [aPasteboard setData: obj forType: aType];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
[[resourcesManager window] performClose: self];
|
|
|
|
|
RELEASE(resourcesManager);
|
1999-12-13 17:01:35 +00:00
|
|
|
|
RELEASE(filesOwner);
|
|
|
|
|
RELEASE(firstResponder);
|
|
|
|
|
RELEASE(fontManager);
|
1999-12-08 15:04:57 +00:00
|
|
|
|
NSFreeMapTable(objToName);
|
|
|
|
|
RELEASE(documentPath);
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) detachObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
NSString *name = [self nameForObject: anObject];
|
|
|
|
|
unsigned count = [connections count];
|
|
|
|
|
|
|
|
|
|
while (count-- > 0)
|
|
|
|
|
{
|
|
|
|
|
id<IBConnectors> con = [connections objectAtIndex: count];
|
|
|
|
|
|
|
|
|
|
if ([con destination] == anObject || [con source] == anObject)
|
|
|
|
|
{
|
|
|
|
|
[connections removeObjectAtIndex: count];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NSMapRemove(objToName, (void*)anObject);
|
1999-12-13 17:01:35 +00:00
|
|
|
|
if ([anObject isKindOfClass: [NSWindow class]] == YES
|
|
|
|
|
|| [anObject isKindOfClass: [NSMenu class]] == YES)
|
1999-12-08 15:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
[resourcesManager removeObject: anObject];
|
|
|
|
|
}
|
|
|
|
|
[nameTable removeObjectForKey: name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) detachObjects: (NSArray*)anArray
|
|
|
|
|
{
|
|
|
|
|
NSEnumerator *enumerator = [anArray objectEnumerator];
|
|
|
|
|
NSObject *obj;
|
|
|
|
|
|
|
|
|
|
while ((obj = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
[self detachObject: obj];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) documentPath
|
|
|
|
|
{
|
|
|
|
|
return documentPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) documentShouldClose
|
|
|
|
|
{
|
|
|
|
|
if ([[resourcesManager window] isDocumentEdited] == YES)
|
|
|
|
|
{
|
|
|
|
|
NSString *msg;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
if (documentPath == nil || [documentPath isEqualToString: @""])
|
|
|
|
|
{
|
|
|
|
|
msg = @"Document 'UNTITLED' has been modified";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
msg = [NSString stringWithFormat: @"Document '%@' has been modified",
|
|
|
|
|
[documentPath lastPathComponent]];
|
|
|
|
|
}
|
|
|
|
|
result = NSRunAlertPanel(NULL, msg, @"Save", @"Cancel", @"Don't Save");
|
|
|
|
|
if (result == NSAlertAlternateReturn)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
else if (result != NSAlertOtherReturn)
|
|
|
|
|
{
|
|
|
|
|
[self saveDocument: self];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) documentWillClose
|
|
|
|
|
{
|
1999-12-08 15:19:11 +00:00
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
Class winClass = [NSWindow class];
|
1999-12-08 15:19:11 +00:00
|
|
|
|
NSEnumerator *enumerator;
|
1999-12-08 15:04:57 +00:00
|
|
|
|
id obj;
|
|
|
|
|
|
1999-12-08 15:19:11 +00:00
|
|
|
|
[nc postNotificationName: IBWillCloseDocumentNotification
|
|
|
|
|
object: self];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
/*
|
|
|
|
|
* Close all open windows in this document befoew we go away.
|
|
|
|
|
*/
|
1999-12-08 15:19:11 +00:00
|
|
|
|
enumerator = [nameTable objectEnumerator];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
while ((obj = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([obj isKindOfClass: winClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
[obj setReleasedWhenClosed: YES];
|
|
|
|
|
[obj close];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[self setDocumentActive: NO];
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:19:11 +00:00
|
|
|
|
- (void) editor: (id<IBEditors>)anEditor didCloseForObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
/* FIXME */
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id<IBEditors>) editorForObject: (id)anObject
|
|
|
|
|
create: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
return [self editorForObject: anObject inEditor: nil create: flag];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id<IBEditors>) editorForObject: (id)anObject
|
|
|
|
|
inEditor: (id<IBEditors>)anEditor
|
|
|
|
|
create: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
/* FIXME */
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
NSEnumerator *enumerator;
|
|
|
|
|
id<IBConnectors> con;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Map all connector sources and destinations to their name strings.
|
|
|
|
|
*/
|
|
|
|
|
enumerator = [connections objectEnumerator];
|
|
|
|
|
while ((con = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
NSString *name;
|
|
|
|
|
id obj;
|
|
|
|
|
|
|
|
|
|
obj = [con source];
|
|
|
|
|
name = [self nameForObject: obj];
|
|
|
|
|
[con setSource: name];
|
|
|
|
|
obj = [con destination];
|
|
|
|
|
name = [self nameForObject: obj];
|
|
|
|
|
[con setDestination: name];
|
|
|
|
|
}
|
1999-12-13 17:01:35 +00:00
|
|
|
|
/*
|
|
|
|
|
* Remove objects that shouldn't be archived.
|
|
|
|
|
*/
|
|
|
|
|
[nameTable removeObjectForKey: @"NSOwner"];
|
|
|
|
|
[nameTable removeObjectForKey: @"NSFirst"];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
/*
|
|
|
|
|
* Archive self into file
|
|
|
|
|
*/
|
1999-12-08 15:04:57 +00:00
|
|
|
|
[super encodeWithCoder: aCoder];
|
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
/*
|
|
|
|
|
* Restore removed objects.
|
|
|
|
|
*/
|
|
|
|
|
[nameTable setObject: filesOwner forKey: @"NSOwner"];
|
|
|
|
|
[nameTable setObject: firstResponder forKey: @"NSFirst"];
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
/*
|
|
|
|
|
* Map all connector source and destination names to their objects.
|
|
|
|
|
*/
|
|
|
|
|
enumerator = [connections objectEnumerator];
|
|
|
|
|
while ((con = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
NSString *name;
|
|
|
|
|
id obj;
|
|
|
|
|
|
|
|
|
|
name = (NSString*)[con source];
|
|
|
|
|
obj = [self objectForName: name];
|
|
|
|
|
[con setSource: obj];
|
|
|
|
|
name = (NSString*)[con destination];
|
|
|
|
|
obj = [self objectForName: name];
|
|
|
|
|
[con setDestination: obj];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self != nil)
|
|
|
|
|
{
|
|
|
|
|
objToName = NSCreateMapTableWithZone(NSNonRetainedObjectMapKeyCallBacks,
|
|
|
|
|
NSNonRetainedObjectMapValueCallBacks, 128, [self zone]);
|
1999-12-13 17:01:35 +00:00
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
resourcesManager = [GormResourcesManager newManagerForDocument: self];
|
1999-12-13 17:01:35 +00:00
|
|
|
|
/*
|
|
|
|
|
* Set up special-case dummy objects and add them to the resources mgr.
|
|
|
|
|
*/
|
|
|
|
|
filesOwner = [GormFilesOwner new];
|
|
|
|
|
[self setName: @"NSOwner" forObject: filesOwner];
|
|
|
|
|
[resourcesManager addObject: filesOwner];
|
|
|
|
|
firstResponder = [GormFirstResponder new];
|
|
|
|
|
[self setName: @"NSFirst" forObject: firstResponder];
|
|
|
|
|
[resourcesManager addObject: firstResponder];
|
|
|
|
|
fontManager = [GormFontManager new];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) nameForObject: (id)anObject
|
|
|
|
|
{
|
1999-12-13 17:01:35 +00:00
|
|
|
|
return (NSString*)NSMapGet(objToName, (void*)anObject);
|
1999-12-08 15:04:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) objectForName: (NSString*)name
|
|
|
|
|
{
|
1999-12-13 17:01:35 +00:00
|
|
|
|
return [nameTable objectForKey: name];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) objects
|
|
|
|
|
{
|
|
|
|
|
return [nameTable allValues];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* NB. This assumes we have an empty document to start with - the loaded
|
|
|
|
|
* document is merged in to it.
|
|
|
|
|
*/
|
|
|
|
|
- (id) openDocument: (id)sender
|
|
|
|
|
{
|
|
|
|
|
NSArray *fileTypes = [NSArray arrayWithObject: @"nib"];
|
|
|
|
|
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
[oPanel setAllowsMultipleSelection: NO];
|
|
|
|
|
[oPanel setCanChooseFiles: YES];
|
|
|
|
|
[oPanel setCanChooseDirectories: NO];
|
|
|
|
|
result = [oPanel runModalForDirectory: NSHomeDirectory()
|
|
|
|
|
file: nil
|
|
|
|
|
types: fileTypes];
|
|
|
|
|
if (result == NSOKButton)
|
|
|
|
|
{
|
1999-12-08 15:19:11 +00:00
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
1999-12-13 17:01:35 +00:00
|
|
|
|
NSMutableDictionary *nt;
|
1999-12-08 15:04:57 +00:00
|
|
|
|
NSString *aFile = [oPanel filename];
|
|
|
|
|
NSData *data;
|
|
|
|
|
NSUnarchiver *u;
|
|
|
|
|
GSNibContainer *c;
|
|
|
|
|
NSEnumerator *enumerator;
|
|
|
|
|
id <IBConnectors> con;
|
|
|
|
|
NSString *name;
|
|
|
|
|
|
|
|
|
|
data = [NSData dataWithContentsOfFile: aFile];
|
|
|
|
|
if (data == nil)
|
|
|
|
|
{
|
|
|
|
|
NSRunAlertPanel(NULL,
|
|
|
|
|
[NSString stringWithFormat: @"Could not read '%@' data", aFile],
|
|
|
|
|
@"OK", NULL, NULL);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1999-12-13 17:01:35 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
1999-12-08 15:04:57 +00:00
|
|
|
|
u = AUTORELEASE([[NSUnarchiver alloc] initForReadingWithData: data]);
|
1999-12-13 17:01:35 +00:00
|
|
|
|
[u decodeClassName: @"GSNibContainer"
|
|
|
|
|
asClassName: @"GormDocument"];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
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'.
|
|
|
|
|
*/
|
1999-12-13 17:01:35 +00:00
|
|
|
|
[[c nameTable] setObject: filesOwner forKey: @"NSOwner"];
|
|
|
|
|
[[c nameTable] setObject: firstResponder forKey: @"NSFirst"];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
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];
|
|
|
|
|
}
|
1999-12-13 17:01:35 +00:00
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
/*
|
1999-12-13 17:01:35 +00:00
|
|
|
|
* Now we merge the objects from the nib container into our own data
|
|
|
|
|
* structures, taking care not to overwrite our NSOwner and NSFirst.
|
1999-12-08 15:04:57 +00:00
|
|
|
|
*/
|
1999-12-13 17:01:35 +00:00
|
|
|
|
[nt removeObjectForKey: @"NSOwner"];
|
|
|
|
|
[nt removeObjectForKey: @"NSFirst"];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
[connections addObjectsFromArray: [c connections]];
|
1999-12-13 17:01:35 +00:00
|
|
|
|
[nameTable addEntriesFromDictionary: nt];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Now we build our reverse mapping information and other initialisation
|
|
|
|
|
*/
|
|
|
|
|
NSResetMapTable(objToName);
|
1999-12-13 17:01:35 +00:00
|
|
|
|
NSMapInsert(objToName, (void*)filesOwner, (void*)@"NSOwner");
|
|
|
|
|
NSMapInsert(objToName, (void*)firstResponder, (void*)@"NSFirst");
|
1999-12-08 15:04:57 +00:00
|
|
|
|
enumerator = [nameTable keyEnumerator];
|
|
|
|
|
while ((name = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
id obj = [nameTable objectForKey: name];
|
|
|
|
|
|
|
|
|
|
NSMapInsert(objToName, (void*)obj, (void*)name);
|
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
if ([obj isKindOfClass: [NSWindow class]] == YES
|
|
|
|
|
|| [obj isKindOfClass: [NSMenu class]] == YES)
|
1999-12-08 15:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
[resourcesManager addObject: obj];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Finally, we set our new file name
|
|
|
|
|
*/
|
|
|
|
|
ASSIGN(documentPath, aFile);
|
|
|
|
|
[[resourcesManager window] setTitleWithRepresentedFilename: documentPath];
|
1999-12-08 15:19:11 +00:00
|
|
|
|
[nc postNotificationName: IBDidOpenDocumentNotification
|
|
|
|
|
object: self];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
return nil; /* Failed */
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:19:11 +00:00
|
|
|
|
- (id<IBEditors>) openEditorForObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
/* FIXME */
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id<IBEditors>) parentEditorForEditor: (id<IBEditors>)anEditor
|
|
|
|
|
{
|
|
|
|
|
/* FIXME */
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
- (id) parentOfObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
NSArray *old;
|
|
|
|
|
id<IBConnectors> con;
|
|
|
|
|
|
1999-12-13 17:01:35 +00:00
|
|
|
|
old = [self connectorsForSource: anObject ofClass: [NSNibConnector class]];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
con = [old lastObject];
|
1999-12-13 17:01:35 +00:00
|
|
|
|
if ([con destination] != filesOwner && [con destination] != firstResponder)
|
1999-12-08 15:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
return [con destination];
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) pasteType: (NSString*)aType
|
|
|
|
|
fromPasteboard: (NSPasteboard*)aPasteboard
|
|
|
|
|
parent: (id)parent
|
|
|
|
|
{
|
|
|
|
|
NSData *data = [aPasteboard dataForType: aType];
|
|
|
|
|
NSArray *objects = [NSUnarchiver unarchiveObjectWithData: data];
|
|
|
|
|
NSEnumerator *enumerator = [objects objectEnumerator];
|
|
|
|
|
NSPoint filePoint;
|
|
|
|
|
NSPoint screenPoint;
|
|
|
|
|
|
|
|
|
|
filePoint = [[resourcesManager window] mouseLocationOutsideOfEventStream];
|
|
|
|
|
screenPoint = [[resourcesManager window] convertBaseToScreen: filePoint];
|
|
|
|
|
|
|
|
|
|
if ([aType isEqualToString: IBWindowPboardType] == YES)
|
|
|
|
|
{
|
|
|
|
|
NSWindow *win;
|
|
|
|
|
|
|
|
|
|
while ((win = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
[win setFrameTopLeftPoint: screenPoint];
|
|
|
|
|
[win orderFront: self];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Pasting %@ not implemented", aType);
|
|
|
|
|
objects = nil;
|
|
|
|
|
/* FIXME */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self attachObjects: objects toParent: parent];
|
|
|
|
|
[self touch];
|
|
|
|
|
return objects;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeConnector: (id<IBConnectors>)aConnector
|
|
|
|
|
{
|
|
|
|
|
[connections removeObjectIdenticalTo: aConnector];
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:19:11 +00:00
|
|
|
|
- (void) resignSelectionForEditor: (id<IBEditors>)editor
|
|
|
|
|
{
|
|
|
|
|
/* FIXME */
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
- (GormResourcesManager*) resourcesManager
|
|
|
|
|
{
|
|
|
|
|
return resourcesManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setName: (NSString*)aName forObject: (id)object
|
|
|
|
|
{
|
|
|
|
|
id oldObject;
|
|
|
|
|
NSString *oldName;
|
|
|
|
|
|
|
|
|
|
if (object == nil)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Attempt to set name for nil object");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (aName == nil)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* No name given - so we must generate one unless we already have one.
|
|
|
|
|
*/
|
|
|
|
|
oldName = [self nameForObject: object];
|
|
|
|
|
if (oldName == nil)
|
|
|
|
|
{
|
|
|
|
|
NSString *base;
|
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generate a sensible name for the object based on its class.
|
|
|
|
|
*/
|
|
|
|
|
base = NSStringFromClass([object class]);
|
|
|
|
|
if ([base hasPrefix: @"NS"] || [base hasPrefix: @"GS"])
|
|
|
|
|
{
|
|
|
|
|
base = [base substringFromIndex: 2];
|
|
|
|
|
}
|
|
|
|
|
aName = base;
|
|
|
|
|
while ([nameTable objectForKey: aName] != nil)
|
|
|
|
|
{
|
|
|
|
|
aName = [base stringByAppendingFormat: @"%u", ++i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return; /* Already named ... nothing to do */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
oldObject = [nameTable objectForKey: aName];
|
|
|
|
|
if (oldObject != nil)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Attempt to re-use name '%@'", aName);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
oldName = [self nameForObject: object];
|
|
|
|
|
if (oldName != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([oldName isEqual: aName] == YES)
|
|
|
|
|
{
|
|
|
|
|
return; /* Already have this namre ... nothing to do */
|
|
|
|
|
}
|
|
|
|
|
NSMapRemove(objToName, (void*)object);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[nameTable setObject: object forKey: aName];
|
|
|
|
|
NSMapInsert(objToName, (void*)object, (void*)aName);
|
|
|
|
|
if (oldName != nil)
|
|
|
|
|
{
|
|
|
|
|
[nameTable removeObjectForKey: oldName];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) saveAsDocument: (id)sender
|
|
|
|
|
{
|
|
|
|
|
NSSavePanel *sp;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
sp = [NSSavePanel savePanel];
|
|
|
|
|
|
|
|
|
|
[sp setRequiredFileType: @"nib"];
|
|
|
|
|
result = [sp runModalForDirectory: NSHomeDirectory() file: @""];
|
|
|
|
|
|
|
|
|
|
if (result == NSOKButton)
|
|
|
|
|
{
|
|
|
|
|
NSFileManager *mgr = [NSFileManager defaultManager];
|
|
|
|
|
NSString *path = [sp filename];
|
|
|
|
|
NSString *old = documentPath;
|
|
|
|
|
id retval;
|
|
|
|
|
|
|
|
|
|
if ([path isEqual: documentPath] == NO
|
|
|
|
|
&& [mgr fileExistsAtPath: path] == YES)
|
|
|
|
|
{
|
|
|
|
|
if (NSRunAlertPanel(NULL, @"A document with that name exists",
|
|
|
|
|
@"Replace", @"Cancel", NULL) != NSAlertDefaultReturn)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSString *bPath = [path stringByAppendingString: @"~"];
|
|
|
|
|
|
|
|
|
|
[mgr removeFileAtPath: bPath handler: nil];
|
|
|
|
|
[mgr movePath: path toPath: bPath handler: nil];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
documentPath = RETAIN(path);
|
|
|
|
|
retval = [self saveDocument: sender];
|
|
|
|
|
if (retval == nil)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(documentPath);
|
|
|
|
|
documentPath = old;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RELEASE(old);
|
|
|
|
|
/* FIXME - need to update files window title etc */
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) saveDocument: (id)sender
|
|
|
|
|
{
|
1999-12-08 15:19:11 +00:00
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
if (documentPath == nil || [documentPath isEqualToString: @""])
|
|
|
|
|
{
|
|
|
|
|
return [self saveAsDocument: sender];
|
|
|
|
|
}
|
1999-12-08 15:19:11 +00:00
|
|
|
|
|
|
|
|
|
[nc postNotificationName: IBWillSaveDocumentNotification
|
|
|
|
|
object: self];
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
if ([NSArchiver archiveRootObject: self toFile: documentPath] == NO)
|
|
|
|
|
{
|
|
|
|
|
NSRunAlertPanel(NULL, @"Could not save document",
|
|
|
|
|
@"OK", NULL, NULL);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
[[resourcesManager window] setDocumentEdited: NO];
|
|
|
|
|
[[resourcesManager window] setTitleWithRepresentedFilename: documentPath];
|
1999-12-08 15:19:11 +00:00
|
|
|
|
|
|
|
|
|
[nc postNotificationName: IBWillSaveDocumentNotification
|
|
|
|
|
object: self];
|
1999-12-08 15:04:57 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setDocumentActive: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
NSEnumerator *enumerator = [nameTable objectEnumerator];
|
|
|
|
|
Class winClass = [NSWindow class];
|
|
|
|
|
id obj;
|
|
|
|
|
|
|
|
|
|
if (flag == YES)
|
|
|
|
|
{
|
|
|
|
|
while ((obj = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([obj isKindOfClass: winClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
[obj orderFront: self];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[[resourcesManager window] orderFront: self];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
while ((obj = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([obj isKindOfClass: winClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
[obj orderOut: self];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[[resourcesManager window] orderOut: self];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:19:11 +00:00
|
|
|
|
- (void) setSelectionFromEditor: (id<IBEditors>)anEditor
|
|
|
|
|
{
|
|
|
|
|
/* FIXME */
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-08 15:04:57 +00:00
|
|
|
|
- (void) touch
|
|
|
|
|
{
|
|
|
|
|
[[resourcesManager window] setDocumentEdited: YES];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|