Committing implementation of NSNib.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@18557 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
gcasa 2004-02-08 06:38:37 +00:00
parent eec3c1f7e0
commit bef0d1bcb2
6 changed files with 448 additions and 138 deletions

View file

@ -1,3 +1,14 @@
2004-02-08 01:36 Gregory John Casamento <greg_casamento@yahoo.com>
* Source/NSNib.m: Added implmentation for this class.
* Headers/AppKit/NSNib.h: Added header for this class.
* Source/GSNibTemplates.m: Modified awakeWithContext:topLevelItems:
to use the NSNibOwner/NSNibTopLevelObjects entries.
* Source/NSBundleAdditions.m: Added code to use NSNib to load
and instantiate the .gorm file. NSNib now contains all of the
.gorm loading code. NSNib currently does not support .gmodel
loading.
2004-02-07 03:53 Alexander Malmberg <alexander@malmberg.org>
* Source/NSTableView.m (-initWithCoder:): Remove an erroneous 'else'

61
Headers/AppKit/NSNib.h Normal file
View file

@ -0,0 +1,61 @@
/*
NSNib.h
Holds an image to use as a cursor
Copyright (C) 2004 Free Software Foundation, Inc.
Author: Gregory John Casamento <greg_casamento@yahoo.com>
Date: 2004
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _GNUstep_H_NSNib
#define _GNUstep_H_NSNib
#include <Foundation/NSObject.h>
#include <Foundation/NSZone.h>
@class NSData;
@class NSDictionary;
@class NSString;
@class NSBundle;
@class NSURL;
@class NSArray;
@interface NSNib : NSObject <NSCoding>
{
NSData *_nibData;
}
// reading the data...
- (id)initWithContentsOfURL: (NSURL *)nibFileURL;
- (id)initWithNibNamed: (NSString *)nibNamed bundle: (NSBundle *)bundle;
// instantiating the nib.
- (BOOL)instantiateNibWithExternalNameTable: (NSDictionary *)externalNameTable;
- (BOOL)instantiateNibWithOwner: (id)owner topLevelObjects: (NSArray **)topLevelObjects;
#ifndef NO_GNUSTEP
- (BOOL)instantiateNibWithExternalNameTable: (NSDictionary *)externalNameTable withZone: (NSZone *)zone;
#endif
@end
#endif /* _GNUstep_H_NSNib */

View file

@ -103,6 +103,7 @@ NSMenu.m \
NSMenuView.m \
NSMenuItem.m \
NSMenuItemCell.m \
NSNib.m \
NSOpenGLContext.m \
NSOpenGLPixelFormat.m \
NSOpenGLView.m \
@ -252,6 +253,7 @@ NSMenu.h \
NSMenuItem.h \
NSMenuItemCell.h \
NSMenuView.h \
NSNib.h \
NSOpenPanel.h \
NSOpenGL.h \
NSOpenGLView.h \

View file

@ -161,13 +161,13 @@ static const int currentVersion = 1; // GSNibItem version number...
while ((key = [enumerator nextObject]) != nil)
{
if ([context objectForKey: key] == nil ||
[key isEqualToString: @"NSOwner"]) // we want to send the message to the owner
[key isEqualToString: @"NSNibOwner"]) // we want to send the message to the owner
{
if([key isEqualToString: @"NSWindowsMenu"] == NO && // we don't want to send a message to these menus twice,
[key isEqualToString: @"NSServicesMenu"] == NO && // if they're custom classes.
[key isEqualToString: @"NSVisible"] == NO && // also exclude any other special parts of the nameTable.
[key isEqualToString: @"NSDeferred"] == NO &&
[key isEqualToString: @"NSTopLevelObjects"] == NO &&
[key isEqualToString: @"NSNibTopLevelObjects"] == NO &&
[key isEqualToString: @"GSCustomClassMap"] == NO)
{
id o = [nameTable objectForKey: key];
@ -180,12 +180,12 @@ static const int currentVersion = 1; // GSNibItem version number...
}
/*
* See if the user has passed in the NSTopLevelObjects key.
* See if the user has passed in the NSNibTopLevelObjects key.
* This is an implementation of an undocumented, but commonly used feature
* of nib files to allow the release of the top level objects in the nib
* file.
*/
obj = [context objectForKey: @"NSTopLevelObjects"];
obj = [context objectForKey: @"NSNibTopLevelObjects"];
if([obj isKindOfClass: [NSMutableArray class]])
{
topLevelObjects = obj;
@ -208,7 +208,7 @@ static const int currentVersion = 1; // GSNibItem version number...
[key isEqualToString: @"NSServicesMenu"] == NO &&
[key isEqualToString: @"NSVisible"] == NO &&
[key isEqualToString: @"NSDeferred"] == NO &&
[key isEqualToString: @"NSTopLevelObjects"] == NO &&
[key isEqualToString: @"NSNibTopLevelObjects"] == NO &&
[key isEqualToString: @"GSCustomClassMap"] == NO)
{
id o = [nameTable objectForKey: key];
@ -220,7 +220,7 @@ static const int currentVersion = 1; // GSNibItem version number...
{
if(topLevelObjects == nil)
{
// It is expected, if the NSTopLevelObjects key is not passed in,
// It is expected, if the NSNibTopLevelObjects key is not passed in,
// that the user has opted to either allow these objects to leak or
// to release them explicitly.
RETAIN(o);

View file

@ -45,8 +45,10 @@
#include <Foundation/NSUserDefaults.h>
#include <Foundation/NSKeyValueCoding.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSURL.h>
#include "AppKit/NSNibConnector.h"
#include "AppKit/NSNibLoading.h"
#include "AppKit/NSNib.h"
#include "GNUstepGUI/GSNibTemplates.h"
#include "GNUstepGUI/IMLoading.h"
@ -176,66 +178,11 @@
}
@end
/*
* This private class is used to collect the nib items while the
* .gorm file is being unarchived. This is done to allow only
* the top level items to be retained in a clean way. The reason it's
* being done this way is because old .gorm files don't have any
* array within the nameTable which indicates the objects which are
* considered top level, so there is no clean and generic way to determine
* this. Basically the top level items are any instances of or instances
* of subclasses of NSMenu, NSWindow, or any controller class.
* It's the last one that's hairy. Controller classes are
* represented in .gorm files by the GSNibItem class, but once they transform
* into the actual class instance it's not easy to tell if it should be
* retained or not since there are a lot of other things stored in the nameTable
* as well. GJC
*/
@interface _GSNibItemCollector : NSObject
{
NSMutableArray *items;
}
- (void) handleNotification: (NSNotification *)notification;
- (NSMutableArray *)items;
// declare this here to avoid a compiler warning...
@interface NSNib (GNUstepPrivate)
+ (NSString *) _nibFilename: (NSString *)fileName;
@end
@implementation _GSNibItemCollector
- (void) handleNotification: (NSNotification *)notification;
{
id obj = [notification object];
[items addObject: obj];
}
- init
{
if((self = [super init]) != nil)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// add myself as an observer and initialize the items array.
[nc addObserver: self
selector: @selector(handleNotification:)
name: @"__GSInternalNibItemAddedNotification"
object: nil];
items = [[NSMutableArray alloc] init];
}
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
RELEASE(items);
[super dealloc];
}
- (NSMutableArray *)items
{
return items;
}
@end
@implementation NSBundle (NSBundleAdditions)
static
@ -274,11 +221,15 @@ Class gmodel_class(void)
externalNameTable: (NSDictionary*)context
withZone: (NSZone*)zone
{
BOOL loaded = NO;
NSUnarchiver *unarchiver = nil;
NSString *ext = [fileName pathExtension];
id nibitems = nil;
NSString *ext = [fileName pathExtension];
NSNib *nib = nil;
BOOL result = NO;
NSMutableDictionary *ctx = nil;
/*
* Determine if we're loading a .gorm or a .gmodel
* if the extension is .nib.
*/
if ([ext isEqual: @"nib"])
{
NSFileManager *mgr = [NSFileManager defaultManager];
@ -304,78 +255,42 @@ Class gmodel_class(void)
if ([ext isEqualToString: @"gmodel"])
{
return [gmodel_class() loadIMFile: fileName
owner: [context objectForKey: @"NSOwner"]];
owner: [context objectForKey: @"NSOwner"]];
}
NSDebugLog(@"Loading Nib `%@'...\n", fileName);
NS_DURING
// Create a temporary context dictionary, containing all of the entries of the
// one passed in, but replacing the NSOwner and NSTopLevelObjects entries with
// the ones used by the NSNib class. We want *all* nib handling to happen in
// one unified place within NSNib. GJC
if(context != nil)
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDir = NO;
id obj = nil;
if([mgr fileExistsAtPath: fileName isDirectory: &isDir])
ctx = [NSMutableDictionary dictionaryWithDictionary: context];
// remove and set the owner...
obj = [ctx objectForKey: @"NSOwner"];
if(obj != nil)
{
NSData *data = nil;
// if the data is in a directory, then load from objects.gorm in the directory
if(isDir == NO)
{
data = [NSData dataWithContentsOfFile: fileName];
NSDebugLog(@"Loaded data from file...");
}
else
{
NSString *newFileName = [fileName stringByAppendingPathComponent: @"objects.gorm"];
data = [NSData dataWithContentsOfFile: newFileName];
NSDebugLog(@"Loaded data from %@...",newFileName);
}
[ctx removeObjectForKey: @"NSOwner"];
[ctx setObject: obj forKey: @"NSNibOwner"];
}
if (data != nil)
{
unarchiver = [[NSUnarchiver alloc] initForReadingWithData: data];
if (unarchiver != nil)
{
id obj;
nibitems = [[_GSNibItemCollector alloc] init];
NSDebugLog(@"Invoking unarchiver");
[unarchiver setObjectZone: zone];
obj = [unarchiver decodeObject];
if (obj != nil)
{
NSArray *items = [nibitems items];
if ([obj isKindOfClass: [GSNibContainer class]])
{
NSDebugLog(@"Calling awakeWithContext");
[obj awakeWithContext: context
topLevelItems: items];
loaded = YES;
}
else
{
NSLog(@"Nib '%@' without container object!", fileName);
}
}
RELEASE(nibitems);
RELEASE(unarchiver);
}
}
// Remove and set the top level objects...
obj = [ctx objectForKey: @"NSTopLevelObjects"];
if(obj != nil)
{
[ctx removeObjectForKey: @"NSTopLevelObjects"];
[ctx setObject: obj forKey: @"NSNibTopLevelObjects"];
}
}
NS_HANDLER
{
NSLog(@"Exception occured while loading model: %@",[localException reason]);
TEST_RELEASE(nibitems);
TEST_RELEASE(unarchiver);
}
NS_ENDHANDLER
if (loaded == NO)
{
NSLog(@"Failed to load Nib\n");
}
return loaded;
// Load and instantiate the nib... release the NSNib object.
nib = [[NSNib alloc] initWithContentsOfURL: [NSURL fileURLWithPath: [NSNib _nibFilename: fileName]]];
result = [nib instantiateNibWithExternalNameTable: ctx withZone: zone];
RELEASE(nib);
return result;
}
+ (BOOL) loadNibNamed: (NSString *)aNibName
@ -481,17 +396,13 @@ Class gmodel_class(void)
withZone: (NSZone*)zone
{
NSString *path = [self pathForNibResource: fileName];
if (path != nil)
if (fileName != nil)
{
return [NSBundle loadNibFile: path
externalNameTable: context
withZone: (NSZone*)zone];
}
else
{
return NO;
}
return NO;
}
@end
// end of NSBundleAdditions

325
Source/NSNib.m Normal file
View file

@ -0,0 +1,325 @@
/** <title>NSNib</title>
<abstract>
This class serves as a container for a nib file. It's possible
to load a nib file from a URL or from a bundle. Using this
class the nib file can now be "preloaded" and instantiated
multiple times when/if needed. Also, since it's possible to
initialize this class using a NSURL it's possible to load
nib files from remote locations.
<b/>
This class uses: NSNibOwner and NSNibTopLevelObjects to allow
the caller to specify the owner of the nib during instantiation
and receive an array containing the top level objects of the nib
file.
</abstract>
Copyright (C) 2004 Free Software Foundation, Inc.
Author: Gregory John Casamento <greg_casamento@yahoo.com>
Date: 2004
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <AppKit/NSNib.h>
#include <AppKit/NSNibLoading.h>
#include <Foundation/NSData.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSString.h>
#include <Foundation/NSBundle.h>
#include <Foundation/NSURL.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSArchiver.h>
#include <Foundation/NSFileManager.h>
#include <Foundation/NSDebug.h>
#include <Foundation/NSException.h>
#include "GNUstepGUI/GSNibTemplates.h"
#include "GNUstepGUI/IMLoading.h"
/*
* This private class is used to collect the nib items while the
* .gorm file is being unarchived. This is done to allow only
* the top level items to be retained in a clean way. The reason it's
* being done this way is because old .gorm files don't have any
* array within the nameTable which indicates the objects which are
* considered top level, so there is no clean and generic way to determine
* this. Basically the top level items are any instances of or instances
* of subclasses of NSMenu, NSWindow, or any controller class.
* It's the last one that's hairy. Controller classes are
* represented in .gorm files by the GSNibItem class, but once they transform
* into the actual class instance it's not easy to tell if it should be
* retained or not since there are a lot of other things stored in the nameTable
* as well. GJC
*/
@interface _GSNibItemCollector : NSObject
{
NSMutableArray *items;
}
- (void) handleNotification: (NSNotification *)notification;
- (NSMutableArray *)items;
@end
@implementation _GSNibItemCollector
- (void) handleNotification: (NSNotification *)notification;
{
id obj = [notification object];
[items addObject: obj];
}
- init
{
if((self = [super init]) != nil)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// add myself as an observer and initialize the items array.
[nc addObserver: self
selector: @selector(handleNotification:)
name: @"__GSInternalNibItemAddedNotification"
object: nil];
items = [[NSMutableArray alloc] init];
}
return self;
}
- (NSMutableArray *)items
{
return items;
}
@end
@implementation NSNib
+ (NSString *) _nibFilename: (NSString *)fileName
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDir = NO;
NSString *newFileName = nil;
// assign the filename...
ASSIGN(newFileName, fileName);
// detect if it's a directory or not...
if([mgr fileExistsAtPath: fileName isDirectory: &isDir])
{
// if the data is in a directory, then load from objects.gorm in the directory
if(isDir == YES)
{
newFileName = [fileName stringByAppendingPathComponent: @"objects.gorm"];
}
}
return newFileName;
}
// private method to read in the data...
- (void) _readNibData: (NSString *)fileName
{
NSString *ext = [fileName pathExtension];
if ([ext isEqual: @"nib"])
{
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *base = [fileName stringByDeletingPathExtension];
/* We can't read nibs, look for an equivalent gorm or gmodel file */
fileName = [base stringByAppendingPathExtension: @"gorm"];
if ([mgr isReadableFileAtPath: fileName])
{
ext = @"gorm";
}
}
NSDebugLog(@"Loading Nib `%@'...\n", fileName);
NS_DURING
{
NSString *newFileName = [NSNib _nibFilename: fileName];
_nibData = [NSData dataWithContentsOfFile: newFileName];
NSDebugLog(@"Loaded data from %@...",newFileName);
}
NS_HANDLER
{
NSLog(@"Exception occured while loading model: %@",[localException reason]);
}
NS_ENDHANDLER
}
// reading the data...
/**
* Load the NSNib object from the specified URL. This location can be
* any type of resource capable of being pointed to by the NSURL object.
* A file in the local file system or a file on an ftp site.
*/
- (id)initWithContentsOfURL: (NSURL *)nibFileURL
{
if((self = [super init]) != nil)
{
// load the nib data into memory...
_nibData = [NSData dataWithContentsOfURL: nibFileURL];
}
return self;
}
/**
* Load the nib indicated by <code>nibNamed</code>. If the <code>bundle</code>
* argument is <code>nil</code>, then the main bundle is used to resolve the path,
* otherwise the bundle which is supplied will be used.
*/
- (id)initWithNibNamed: (NSString *)nibNamed bundle: (NSBundle *)bundle
{
if((self = [super init]) != nil)
{
NSString *bundlePath = nil;
NSString *fileName = nil;
if(bundle == nil)
{
bundle = [NSBundle mainBundle];
}
// initialize the bundle...
bundlePath = [bundle pathForNibResource: nibNamed];
fileName = [bundlePath stringByAppendingPathComponent: nibNamed];
// load the nib data into memory...
[self _readNibData: fileName];
}
return self;
}
/**
* This is a GNUstep specific method. This method is used when the caller wants the
* objects instantiated in the nib to be stored in the given <code>zone</code>.
*/
- (BOOL)instantiateNibWithExternalNameTable: (NSDictionary *)externalNameTable
withZone: (NSZone *)zone
{
BOOL loaded = NO;
id nibitems = nil;
NSUnarchiver *unarchiver = nil;
NS_DURING
{
if (_nibData != nil)
{
unarchiver = [[NSUnarchiver alloc] initForReadingWithData: _nibData];
if (unarchiver != nil)
{
id obj;
nibitems = [[_GSNibItemCollector alloc] init];
NSDebugLog(@"Invoking unarchiver");
[unarchiver setObjectZone: zone];
obj = [unarchiver decodeObject];
if (obj != nil)
{
NSArray *items = [nibitems items];
if ([obj isKindOfClass: [GSNibContainer class]])
{
NSDebugLog(@"Calling awakeWithContext");
[obj awakeWithContext: externalNameTable
topLevelItems: items];
loaded = YES;
}
else
{
NSLog(@"Nib '%@' without container object!");
}
}
RELEASE(nibitems);
RELEASE(unarchiver);
}
}
}
NS_HANDLER
{
NSLog(@"Exception occured while loading model: %@",[localException reason]);
TEST_RELEASE(nibitems);
TEST_RELEASE(unarchiver);
}
NS_ENDHANDLER
if (loaded == NO)
{
NSLog(@"Failed to load Nib\n");
}
return loaded;
}
/**
* This method instantiates the nib file. The externalNameTable dictionary
* accepts the NSNibOwner and NSNibTopLevelObjects entries described earlier.
* It is recommended, for subclasses whose purpose is to change the behaviour
* of nib loading, to override this method.
*/
- (BOOL)instantiateNibWithExternalNameTable: (NSDictionary *)externalNameTable
{
return [self instantiateNibWithExternalNameTable: externalNameTable
withZone: NSDefaultMallocZone()];
}
/**
* This method instantiates the nib file. It utilizes the
* instantiateNibWithExternalNameTable: method to, in a convenient way,
* allow the user to specify both keys accepted by the
* nib loading process.
*/
- (BOOL)instantiateNibWithOwner: (id)owner topLevelObjects: (NSArray **)topLevelObjects
{
NSMutableDictionary *externalNameTable = [NSMutableDictionary dictionary];
// add the necessary things to the table...
[externalNameTable setObject: owner forKey: @"NSNibOwner"];
if(topLevelObjects != 0)
{
*topLevelObjects = [NSMutableArray array];
[externalNameTable setObject: *topLevelObjects forKey: @"NSNibTopLevelObjects"];
}
return [self instantiateNibWithExternalNameTable: externalNameTable];
}
- (id) initWithCoder: (NSCoder *)coder
{
if((self = [super init]) != nil)
{
[coder decodeValueOfObjCType: @encode(id)
at: &_nibData];
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeValueOfObjCType: @encode(id)
at: &_nibData];
}
- (void) dealloc
{
RELEASE(_nibData);
[super dealloc];
}
@end