libs-gui/Source/NSNib.m

277 lines
7.8 KiB
Mathematica
Raw Permalink Normal View History

/** <title>NSNib</title>
2020-07-03 14:50:20 +00:00
<abstract>
2020-07-03 14:50:20 +00:00
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.
<br/>
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
2020-07-03 14:50:20 +00:00
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 Lesser 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
2020-07-03 14:50:20 +00:00
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
2020-07-03 14:50:20 +00:00
*/
#import "config.h"
#import <Foundation/NSArchiver.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import <Foundation/NSURL.h>
#import "AppKit/NSNib.h"
#import "AppKit/NSNibLoading.h"
#import "GNUstepGUI/GSModelLoaderFactory.h"
@implementation NSObject (NSNibLoading)
- (void) awakeFromNib
{
// empty implementation, so that all objects respond...
}
- (void) prepareForInterfaceBuilder
{
// empty implementation, so that all objects respond...
}
@end
@implementation NSNib
// private method to read in the data...
- (void) _readNibData: (NSString *)fileName
{
NSDebugLog(@"Loading model `%@'...\n", fileName);
NS_DURING
{
NSString *newFileName = [GSModelLoaderFactory supportedModelFileAtPath: fileName];
ASSIGN(_loader, [GSModelLoaderFactory modelLoaderForFileType: [newFileName pathExtension]]);
ASSIGN(_nibData, [_loader dataForFile: newFileName]);
NSDebugLog(@"Loaded data from %@...", newFileName);
}
NS_HANDLER
{
NSLog(@"Exception occurred while loading model: %@", [localException reason]);
}
NS_ENDHANDLER
}
// Public methods...
/**
* 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.
*/
2020-07-03 14:50:20 +00:00
- (id) initWithContentsOfURL: (NSURL *)nibFileURL
{
if ((self = [super init]) != nil)
{
2020-07-03 14:50:20 +00:00
// Currently we need this short cut for GModel files.
// Remove this when the hack there is cleaned up.
if ([nibFileURL isFileURL])
{
[self _readNibData: [nibFileURL path]];
}
else
{
NS_DURING
{
2020-07-03 14:50:20 +00:00
ASSIGN(_loader, [GSModelLoaderFactory modelLoaderForFileType:
[[nibFileURL path] pathExtension]]);
// load the nib data into memory...
2020-07-03 14:50:20 +00:00
ASSIGN(_nibData, [NSData dataWithContentsOfURL: nibFileURL]);
}
NS_HANDLER
{
NSLog(@"Exception occurred while loading model: %@", [localException reason]);
}
NS_ENDHANDLER
}
}
return self;
}
/**
* Load the nib indicated by <code>nibNamed</code>. If the <code>bundle</code>
2020-07-03 14:50:20 +00:00
* argument is <code>nil</code>, then the main bundle is used to resolve
* the path, otherwise the bundle which is supplied will be used.
*/
2020-07-03 14:50:20 +00:00
- (instancetype) initWithNibNamed: (NSNibName)nibNamed
bundle: (NSBundle *)bundle
{
if ((self = [super init]) != nil)
{
NSString *fileName = nil;
// Keep the bundle for resource creation
ASSIGN(_bundle, bundle);
if (bundle == nil)
{
bundle = [NSBundle mainBundle];
}
// initialize the bundle...
fileName = [bundle pathForNibResource: nibNamed];
if (fileName == nil)
{
DESTROY(self);
return nil;
}
// load the nib data into memory...
[self _readNibData: fileName];
}
return self;
}
2020-07-03 14:50:20 +00:00
- (instancetype) initWithNibData: (NSData *)nibData
bundle: (NSBundle *)bundle
{
if ((self = [super init]) != nil)
{
ASSIGN(_bundle, bundle);
ASSIGN(_nibData, nibData);
ASSIGN(_loader, [GSModelLoaderFactory modelLoaderForData: nibData]);
2020-07-03 14:50:20 +00:00
}
return self;
}
/**
2020-07-03 14:50:20 +00:00
* 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>.
*/
2020-07-03 14:50:20 +00:00
- (BOOL) instantiateNibWithExternalNameTable: (NSDictionary *)externalNameTable
withZone: (NSZone *)zone
{
2020-07-03 14:50:20 +00:00
return [_loader loadModelData: _nibData
externalNameTable: externalNameTable
withZone: zone];
}
/**
* This method instantiates the nib file. The externalNameTable dictionary
* accepts the NSNibOwner and NSNibTopLevelObjects entries described earlier.
2020-07-03 14:50:20 +00:00
* It is recommended, for subclasses whose purpose is to change the behaviour
* of nib loading, to override this method.
*/
2020-07-03 14:50:20 +00:00
- (BOOL) instantiateNibWithExternalNameTable: (NSDictionary *)externalNameTable
{
return [self instantiateNibWithExternalNameTable: externalNameTable
withZone: NSDefaultMallocZone()];
}
/**
2020-07-03 14:50:20 +00:00
* 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.
*/
2020-07-03 14:50:20 +00:00
- (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];
}
2020-07-03 14:50:20 +00:00
return [self instantiateNibWithExternalNameTable: externalNameTable];
}
- (BOOL) instantiateWithOwner: (id)owner
topLevelObjects: (NSArray **)topLevelObjects
{
return [self instantiateNibWithOwner: owner
topLevelObjects: topLevelObjects];
}
- (id) initWithCoder: (NSCoder *)coder
{
if ((self = [super init]) != nil)
{
if ([coder allowsKeyedCoding])
{
// TODO_NIB: Need to verify this key...
ASSIGN(_nibData, [coder decodeObjectForKey: @"NSData"]);
}
else
{
// this is sort of a kludge...
[coder decodeValueOfObjCType: @encode(id)
at: &_nibData];
}
if (_nibData != nil)
{
ASSIGN(_loader, [GSModelLoaderFactory modelLoaderForData: _nibData]);
}
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
if ([coder allowsKeyedCoding])
{
// TODO_NIB: Need to verify this key...
2020-07-03 14:50:20 +00:00
[coder encodeObject: _nibData
forKey: @"NSData"];
}
else
{
[coder encodeValueOfObjCType: @encode(id)
at: &_nibData];
}
}
- (void) dealloc
{
RELEASE(_nibData);
RELEASE(_loader);
TEST_RELEASE(_bundle);
[super dealloc];
}
@end