2006-06-14 05:47:54 +00:00
|
|
|
/* GormNibWrapperLoader
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* Author: Gregory John Casamento <greg_casamento@yahoo.com>
|
|
|
|
* Date: 2006
|
|
|
|
*
|
|
|
|
* 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
|
2007-11-05 23:44:36 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2006-06-14 05:47:54 +00:00
|
|
|
* (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., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
#include <AppKit/AppKit.h>
|
|
|
|
#include <GormCore/GormPalettesManager.h>
|
|
|
|
#include <GormCore/GormClassManager.h>
|
|
|
|
#include <GormCore/GormImage.h>
|
|
|
|
#include <GormCore/GormSound.h>
|
|
|
|
#include <GormCore/GormPrivate.h>
|
|
|
|
#include <GormCore/NSView+GormExtensions.h>
|
|
|
|
#include <GormCore/GormFunctions.h>
|
|
|
|
#include <GormCore/GormCustomView.h>
|
2008-05-17 00:43:33 +00:00
|
|
|
#include "GormNibWrapperLoader.h"
|
2009-12-27 06:16:54 +00:00
|
|
|
#include "GormWindowTemplate.h"
|
2006-06-14 05:47:54 +00:00
|
|
|
|
2009-12-31 04:39:18 +00:00
|
|
|
@class GormNSWindow;
|
|
|
|
|
2006-06-14 05:47:54 +00:00
|
|
|
@implementation GormNibWrapperLoader
|
2008-06-24 23:18:16 +00:00
|
|
|
+ (NSString *) fileType
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
|
|
|
return @"GSNibFileType";
|
|
|
|
}
|
|
|
|
|
2006-06-25 00:50:34 +00:00
|
|
|
- (BOOL) isTopLevelObject: (id)obj
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
2006-06-25 00:45:18 +00:00
|
|
|
NSMapTable *objects = [container objects];
|
|
|
|
id val = NSMapGet(objects,obj);
|
|
|
|
BOOL result = NO;
|
|
|
|
|
|
|
|
if(val == nibFilesOwner || val == nil)
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
2006-06-25 00:45:18 +00:00
|
|
|
result = YES;
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 00:45:18 +00:00
|
|
|
return result;
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) loadFileWrapper: (NSFileWrapper *)wrapper withDocument: (GormDocument *) doc
|
|
|
|
{
|
2009-03-17 05:06:37 +00:00
|
|
|
BOOL result = NO;
|
|
|
|
|
2006-06-14 05:47:54 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
NSData *data = nil;
|
|
|
|
NSData *classes = nil;
|
|
|
|
NSKeyedUnarchiver *u = nil;
|
|
|
|
NSString *key = nil;
|
|
|
|
GormPalettesManager *palettesManager = [(id<Gorm>)NSApp palettesManager];
|
|
|
|
NSDictionary *substituteClasses = [palettesManager substituteClasses];
|
|
|
|
NSString *subClassName = nil;
|
|
|
|
NSDictionary *fileWrappers = nil;
|
|
|
|
|
2009-03-20 03:40:53 +00:00
|
|
|
if ([super loadFileWrapper: wrapper withDocument: doc] &&
|
|
|
|
[wrapper isDirectory])
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
|
|
|
GormClassManager *classManager = [document classManager];
|
2008-01-06 00:18:42 +00:00
|
|
|
id docFilesOwner;
|
2009-03-17 00:18:56 +00:00
|
|
|
NSMapTable *objects;
|
2008-01-06 00:18:42 +00:00
|
|
|
NSArray *objs;
|
|
|
|
NSEnumerator *en;
|
|
|
|
id o;
|
2008-01-07 23:39:25 +00:00
|
|
|
NSMapTable *classesTable;
|
2008-01-06 00:18:42 +00:00
|
|
|
NSArray *classKeys;
|
2006-06-14 05:47:54 +00:00
|
|
|
|
|
|
|
key = nil;
|
|
|
|
fileWrappers = [wrapper fileWrappers];
|
|
|
|
|
|
|
|
// turn off custom classes...
|
|
|
|
[NSClassSwapper setIsInInterfaceBuilder: YES];
|
2008-01-07 23:39:25 +00:00
|
|
|
en = [fileWrappers keyEnumerator];
|
|
|
|
while((key = [en nextObject]) != nil)
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
|
|
|
NSFileWrapper *fw = [fileWrappers objectForKey: key];
|
|
|
|
if([fw isRegularFile])
|
|
|
|
{
|
|
|
|
NSData *fileData = [fw regularFileContents];
|
|
|
|
if([key isEqual: @"keyedobjects.nib"])
|
|
|
|
{
|
|
|
|
data = fileData;
|
|
|
|
}
|
|
|
|
else if([key isEqual: @"classes.nib"])
|
|
|
|
{
|
|
|
|
classes = fileData;
|
|
|
|
|
|
|
|
// load the custom classes...
|
|
|
|
if (![classManager loadNibFormatCustomClassesWithData: classes])
|
|
|
|
{
|
|
|
|
NSRunAlertPanel(_(@"Problem Loading"),
|
|
|
|
_(@"Could not open the associated classes file.\n"
|
|
|
|
@"You won't be able to edit connections on custom classes"),
|
|
|
|
_(@"OK"), nil, nil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the data...
|
|
|
|
if (data == nil || classes == nil)
|
|
|
|
{
|
2009-03-17 05:06:37 +00:00
|
|
|
result = NO;
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
2009-03-17 05:06:37 +00:00
|
|
|
else
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
2009-03-17 05:06:37 +00:00
|
|
|
/*
|
|
|
|
* Create an unarchiver, and use it to unarchive the gorm file while
|
|
|
|
* handling class replacement so that standard objects understood
|
|
|
|
* by the gui library are converted to their Gorm internal equivalents.
|
|
|
|
*/
|
2009-12-31 04:39:18 +00:00
|
|
|
|
2009-03-17 05:06:37 +00:00
|
|
|
u = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
|
|
|
|
[u setDelegate: self];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special internal classes
|
|
|
|
*/
|
|
|
|
[u setClass: [GormObjectProxy class]
|
|
|
|
forClassName: @"NSCustomObject"];
|
|
|
|
[u setClass: [GormCustomView class]
|
|
|
|
forClassName: @"NSCustomView"];
|
2009-12-27 06:16:54 +00:00
|
|
|
[u setClass: [GormWindowTemplate class]
|
|
|
|
forClassName: @"NSWindowTemplate"];
|
2009-12-31 04:39:18 +00:00
|
|
|
[u setClass: [GormNSWindow class]
|
|
|
|
forClassName: @"NSWindow"];
|
2006-06-14 05:47:54 +00:00
|
|
|
|
2009-03-17 05:06:37 +00:00
|
|
|
/*
|
|
|
|
* Substitute any classes specified by the palettes...
|
|
|
|
*/
|
|
|
|
en = [substituteClasses keyEnumerator];
|
|
|
|
while((subClassName = [en nextObject]) != nil)
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
2009-03-17 05:06:37 +00:00
|
|
|
NSString *realClassName = [substituteClasses objectForKey: subClassName];
|
|
|
|
Class substituteClass = NSClassFromString(subClassName);
|
|
|
|
[u setClass: substituteClass
|
|
|
|
forClassName: realClassName];
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
2009-03-17 05:06:37 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// decode
|
|
|
|
//
|
|
|
|
container = [u decodeObjectForKey: @"IB.objectdata"];
|
|
|
|
if (container == nil || [container isKindOfClass: [NSIBObjectData class]] == NO)
|
2006-07-05 04:03:58 +00:00
|
|
|
{
|
2009-03-17 05:06:37 +00:00
|
|
|
result = NO;
|
2006-07-05 04:03:58 +00:00
|
|
|
}
|
2006-07-09 17:05:45 +00:00
|
|
|
else
|
|
|
|
{
|
2009-03-17 05:06:37 +00:00
|
|
|
nibFilesOwner = [container objectForName: @"File's Owner"];
|
|
|
|
|
|
|
|
docFilesOwner = [document filesOwner];
|
|
|
|
objects = [container names];
|
|
|
|
objs = NSAllMapTableKeys(objects);
|
|
|
|
en = [objs objectEnumerator];
|
|
|
|
o = nil;
|
2006-06-17 10:55:14 +00:00
|
|
|
|
2009-03-17 05:06:37 +00:00
|
|
|
//
|
|
|
|
// set the current class on the File's owner...
|
|
|
|
//
|
|
|
|
if([nibFilesOwner isKindOfClass: [GormObjectProxy class]])
|
2006-06-17 10:55:14 +00:00
|
|
|
{
|
2009-03-17 05:06:37 +00:00
|
|
|
[docFilesOwner setClassName: [nibFilesOwner className]];
|
2006-06-17 10:55:14 +00:00
|
|
|
}
|
2009-03-17 05:06:37 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// add objects...
|
|
|
|
//
|
|
|
|
while((o = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
id obj = o;
|
|
|
|
NSString *customClassName = nil;
|
|
|
|
NSString *objName = nil;
|
|
|
|
|
|
|
|
// skip the file's owner, it is handled above...
|
|
|
|
if(o == nibFilesOwner)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//
|
|
|
|
// if it's a window template, then replace it with an actual window.
|
|
|
|
//
|
|
|
|
if([o isKindOfClass: [NSWindowTemplate class]])
|
|
|
|
{
|
|
|
|
NSString *className = [o className];
|
|
|
|
BOOL isDeferred = [o isDeferred];
|
|
|
|
BOOL isVisible = [[container visibleWindows] containsObject: o];
|
|
|
|
|
|
|
|
// make the object deferred/visible...
|
|
|
|
obj = [o nibInstantiate];
|
|
|
|
|
|
|
|
[document setObject: obj isDeferred: isDeferred];
|
|
|
|
[document setObject: obj isVisibleAtLaunch: isVisible];
|
|
|
|
// record the custom class...
|
|
|
|
if([classManager isCustomClass: className])
|
|
|
|
{
|
|
|
|
customClassName = className;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if([self isTopLevelObject: obj])
|
|
|
|
{
|
|
|
|
[document attachObject: obj toParent: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(customClassName != nil)
|
|
|
|
{
|
|
|
|
objName = [document nameForObject: obj];
|
|
|
|
[classManager setCustomClass: customClassName forName: objName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Add custom classes...
|
|
|
|
//
|
|
|
|
classesTable = [container classes];
|
|
|
|
classKeys = NSAllMapTableKeys(classesTable);
|
|
|
|
en = [classKeys objectEnumerator];
|
|
|
|
while((o = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
NSString *name = [document nameForObject: o];
|
|
|
|
NSString *customClass = NSMapGet(classesTable, o);
|
|
|
|
if(name != nil && customClass != nil)
|
|
|
|
{
|
|
|
|
[classManager setCustomClass: customClass forName: name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Name %@ or class %@ for object %@ is nil.", name, customClass, o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// add connections...
|
|
|
|
//
|
|
|
|
en = [[container connections] objectEnumerator];
|
|
|
|
o = nil;
|
|
|
|
while((o = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
id dest = [o destination];
|
|
|
|
id src = [o source];
|
|
|
|
|
|
|
|
// NSLog(@"Connector: %@",o);
|
|
|
|
|
|
|
|
if([o isKindOfClass: [NSNibControlConnector class]])
|
|
|
|
{
|
|
|
|
NSString *tag = [o label];
|
|
|
|
NSRange colonRange = [tag rangeOfString: @":"];
|
|
|
|
unsigned int location = colonRange.location;
|
|
|
|
|
|
|
|
if(location == NSNotFound)
|
|
|
|
{
|
|
|
|
NSString *newTag = [NSString stringWithFormat: @"%@:",tag];
|
|
|
|
[o setLabel: (id)newTag];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dest == nibFilesOwner)
|
|
|
|
{
|
|
|
|
[o setDestination: [document filesOwner]];
|
|
|
|
}
|
|
|
|
else if(dest == nil)
|
|
|
|
{
|
|
|
|
[o setDestination: [document firstResponder]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(src == nibFilesOwner)
|
|
|
|
{
|
|
|
|
[o setSource: [document filesOwner]];
|
|
|
|
}
|
|
|
|
else if(src == nil)
|
|
|
|
{
|
|
|
|
[o setSource: [document firstResponder]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// check src/dest for window template...
|
|
|
|
if([src isKindOfClass: [NSWindowTemplate class]])
|
|
|
|
{
|
|
|
|
id win = [src realObject];
|
|
|
|
[o setSource: win];
|
|
|
|
}
|
|
|
|
|
|
|
|
if([dest isKindOfClass: [NSWindowTemplate class]])
|
|
|
|
{
|
|
|
|
id win = [dest realObject];
|
|
|
|
[o setDestination: win];
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip any help connectors...
|
|
|
|
if([o isKindOfClass: [NSIBHelpConnector class]])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[document addConnector: o];
|
|
|
|
}
|
|
|
|
|
|
|
|
// turn on custom classes.
|
|
|
|
[NSClassSwapper setIsInInterfaceBuilder: NO];
|
|
|
|
|
|
|
|
// clear the changes, since we just loaded the document.
|
|
|
|
[document updateChangeCount: NSChangeCleared];
|
|
|
|
|
|
|
|
result = YES;
|
2006-06-17 10:26:20 +00:00
|
|
|
}
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
2009-12-31 04:39:18 +00:00
|
|
|
[NSClassSwapper setIsInInterfaceBuilder: NO];
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSRunAlertPanel(_(@"Problem Loading"),
|
|
|
|
[NSString stringWithFormat: @"Failed to load file. Exception: %@",[localException reason]],
|
|
|
|
_(@"OK"), nil, nil);
|
2009-03-17 05:06:37 +00:00
|
|
|
result = NO;
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
|
|
|
|
2009-03-17 05:06:37 +00:00
|
|
|
// return the result.
|
|
|
|
return result;
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
2008-05-17 00:43:33 +00:00
|
|
|
- (void) unarchiver: (NSKeyedUnarchiver *)unarchiver
|
|
|
|
willReplaceObject: (id)obj
|
|
|
|
withObject: (id)newObj
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
2006-06-24 14:26:32 +00:00
|
|
|
// Nothing for now...
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) unarchiver: (NSKeyedUnarchiver *)unarchiver didDecodeObject: (id)obj
|
|
|
|
{
|
|
|
|
if([obj isKindOfClass: [NSWindowTemplate class]])
|
|
|
|
{
|
|
|
|
GormClassManager *classManager = [document classManager];
|
2008-01-06 00:18:42 +00:00
|
|
|
Class clz ;
|
2006-06-14 05:47:54 +00:00
|
|
|
NSString *className = [obj className];
|
2008-01-06 00:18:42 +00:00
|
|
|
|
2006-06-14 05:47:54 +00:00
|
|
|
if([classManager isCustomClass: className])
|
|
|
|
{
|
|
|
|
className = [classManager nonCustomSuperClassOf: className];
|
|
|
|
}
|
2008-01-06 00:18:42 +00:00
|
|
|
clz = [unarchiver classForClassName: className];
|
2006-06-14 05:47:54 +00:00
|
|
|
[obj setBaseWindowClass: clz];
|
|
|
|
}
|
2006-06-17 10:26:20 +00:00
|
|
|
else if([obj respondsToSelector: @selector(setTarget:)] &&
|
|
|
|
[obj respondsToSelector: @selector(setAction:)] &&
|
|
|
|
[obj isKindOfClass: [NSCell class]] == NO)
|
2006-06-14 05:47:54 +00:00
|
|
|
{
|
2006-06-17 10:26:20 +00:00
|
|
|
// blank the target/action for all objects.
|
|
|
|
[obj setTarget: nil];
|
|
|
|
[obj setAction: NULL];
|
2006-06-14 05:47:54 +00:00
|
|
|
}
|
2006-06-17 10:26:20 +00:00
|
|
|
|
2006-06-14 05:47:54 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
@end
|