1/** <title>NSBundleAdditions</title> 2 3 <abstract>Implementation of NSBundle Additions</abstract> 4 5 Copyright (C) 1997, 1999 Free Software Foundation, Inc. 6 7 Author: Simon Frankau <sgf@frankau.demon.co.uk> 8 Date: 1997 9 Author: Richard Frith-Macdonald <richard@brainstorm.co.uk> 10 Date: 1999 11 Author: Gregory John Casamento <greg_casamento@yahoo.com> 12 Date: 2000 13 14 This file is part of the GNUstep GUI Library. 15 16 This library is free software; you can redistribute it and/or 17 modify it under the terms of the GNU Lesser General Public 18 License as published by the Free Software Foundation; either 19 version 2 of the License, or (at your option) any later version. 20 21 This library is distributed in the hope that it will be useful, 22 but WITHOUT ANY WARRANTY; without even the implied warranty of 23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 Lesser General Public License for more details. 25 26 You should have received a copy of the GNU Lesser General Public 27 License along with this library; see the file COPYING.LIB. 28 If not, see <http://www.gnu.org/licenses/> or write to the 29 Free Software Foundation, 51 Franklin Street, Fifth Floor, 30 Boston, MA 02110-1301, USA. 31*/ 32 33#import "config.h" 34#import <Foundation/NSArray.h> 35#import <Foundation/NSBundle.h> 36#import <Foundation/NSDebug.h> 37#import <Foundation/NSDictionary.h> 38#import <Foundation/NSEnumerator.h> 39#import <Foundation/NSString.h> 40#import <Foundation/NSURL.h> 41#import <Foundation/NSUserDefaults.h> 42#import "AppKit/NSNib.h" 43#import "AppKit/NSNibLoading.h" 44#import "GNUstepGUI/GSModelLoaderFactory.h" 45 46@implementation NSBundle (NSBundleAdditions) 47+ (BOOL) loadNibFile: (NSString*)fileName 48 externalNameTable: (NSDictionary*)context 49 withZone: (NSZone*)zone 50{ 51 NSNib *nib = [[NSNib alloc] initWithContentsOfURL: [NSURL fileURLWithPath: fileName]]; 52 BOOL loaded = [nib instantiateNibWithExternalNameTable: context 53 withZone: zone]; 54 55 RELEASE(nib); 56 return loaded; 57} 58 59+ (BOOL) loadNibNamed: (NSString *)aNibName 60 owner: (id)owner 61{ 62 NSDictionary *table; 63 NSBundle *bundle; 64 65 if (owner == nil || aNibName == nil) 66 { 67 return NO; 68 } 69 table = [NSDictionary dictionaryWithObject: owner forKey: NSNibOwner]; 70 71 /* 72 * First look for the NIB in the bundle corresponding to the owning class, 73 * since the class may have been loaded dynamically and the bundle may 74 * contain class-specific NIB resources as well as code. 75 * If that fails, try to load the NIB from the main application bundle, 76 * which is where most NIB resources are to be found. 77 * Possibly this is the wrong order ... since it's conceivable that an 78 * application may supply an alternative NIB which it would like to have 79 * used in preference to the one in the classes bundle. However I could 80 * not find the behavior documented anywhere and the current order is 81 * most consistent with the the way the code behaved before I changed it. 82 */ 83 bundle = [self bundleForClass: [owner class]]; 84 if (bundle != nil && [bundle loadNibFile: aNibName 85 externalNameTable: table 86 withZone: [owner zone]] == YES) 87 { 88 return YES; 89 } 90 else 91 { 92 bundle = [self mainBundle]; 93 } 94 return [bundle loadNibFile: aNibName 95 externalNameTable: table 96 withZone: [owner zone]]; 97} 98 99- (NSString *) pathForNibResource: (NSString *)fileName 100{ 101 NSEnumerator *enumerator; 102 NSArray *types = [GSModelLoaderFactory supportedTypes]; 103 NSString *ext = [fileName pathExtension]; 104 105 NSDebugLLog(@"NIB", @"Path for NIB file %@", fileName); 106 if ((ext == nil) || [ext isEqualToString:@""]) 107 { 108 NSString *type; 109 110 enumerator = [types objectEnumerator]; 111 while ((type = [enumerator nextObject])) 112 { 113 NSDebugLLog(@"NIB", @"Checking type %@", type); 114 NSString *path = [self pathForResource: fileName 115 ofType: type]; 116 if (path != nil) 117 { 118 return path; 119 } 120 } 121 } 122 else 123 { 124 if ([types containsObject: ext]) 125 { 126 NSString *path = [self pathForResource: 127 [fileName stringByDeletingPathExtension] 128 ofType: ext]; 129 if (path != nil) 130 { 131 return path; 132 } 133 } 134 } 135 136 NSDebugLLog(@"NIB", @"Did not find NIB resource %@", fileName); 137 return nil; 138} 139 140- (BOOL) loadNibFile: (NSString*)fileName 141 externalNameTable: (NSDictionary*)context 142 withZone: (NSZone*)zone 143{ 144 NSString *path = [self pathForNibResource: fileName]; 145 146 if (path != nil) 147 { 148 return [NSBundle loadNibFile: path 149 externalNameTable: context 150 withZone: (NSZone*)zone]; 151 } 152 else 153 { 154 return NO; 155 } 156} 157@end 158// end of NSBundleAdditions 159