-
Notifications
You must be signed in to change notification settings - Fork 2
/
UKNibOwner.m
153 lines (126 loc) · 4.05 KB
/
UKNibOwner.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* =============================================================================
FILE: UKNibOwner.m
PROJECT: CocoaTADS
COPYRIGHT: (c) 2004-2006 M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
Guenther Noack - GN
LICENSES: GPL, Modified BSD
REVISIONS:
2004-11-13 UK Created.
2006-08-17 GN Made it load the Nib from the classes bundle instead
of just using the main bundle.
2006-08-18 GN Added loadNib method, modified comments to be doxygen
compatible.
========================================================================== */
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import "UKNibOwner.h"
#import "GNUstep.h"
/**
* On instantiation, an object which inherits from UKNibOwner automatically loads
* the Nib file which is named just like the concrete UKNibOwner subclass it is an
* instance of.
*
* For example, a direct instance of the UKNibOwner subclass "PreferencesPanel"
* would try to load the Nib file named "PreferencesPanel.nib".
*
* @author Uli Kusterer
8 @author Guenther Noack
*/
@implementation UKNibOwner
/**
* Create this object and load NIB file. Note that for subclasses, this
* is called before your subclass has been fully constructed. Thus,
* awakeFromNib can't rely on stuff that's done in the constructor.
* I'll probably change this eventually.
*
* If Nib file loading fails, this method returns nil and deallocates
* the instance.
*
* REVISIONS:
* 2004-12-23 UK Documented.
*
* @return an instance of the class if Nib loading succeeded
*/
-(id) init
{
if( (self = [super init]) )
{
// Mark Nib as not loaded
nibLoaded = NO;
// XXX: If removing loading Nibs from init, also update the
// comment for the UKNibOwner class!
if ([self loadNib] == NO) {
DESTROY(self);
}
}
return self;
}
-(void) dealloc
{
// XXX: How do I deallocate the Nib correctly? -GN
[super dealloc];
}
/**
* Loads the owners Nib file. If it works, it returns YES. If it doesn't, it returns
* NO and prints a log message.
*
* This method may throw an exception if the bundle of the current class could not be
* determined.
*
* REVISIONS
* 2006-08-18 GN Created.
*
* @return YES, if and only if the Nib loading succeeded.
*/
-(BOOL) loadNib
{
// Ensure we don't load the Nib multiple times.
if (nibLoaded == YES)
return YES;
// get my bundle
NSBundle* bundle = [self bundle];
// ensure the bundle has been found
NSAssert1(bundle != nil, @"Failed finding bundle for NibOwner %@", self);
NSDictionary* ent = [NSDictionary dictionaryWithObjectsAndKeys:
self, @"NSOwner", nil];
// load Nib belonging to bundle
nibLoaded = [bundle loadNibFile: [self nibFilename]
externalNameTable: ent
withZone: [self zone]];
// Return NO if the Nib loading failed.
if (nibLoaded == NO) {
NSLog(@"NibOwner %@ couldn't load Nib (Gorm) file %@.nib (~.gorm)", self, [self nibFilename]);
return NO;
}
nibLoaded = YES;
return YES;
}
/**
* Returns the filename (minus ".nib" suffix) for the NIB file to load.
* Note that, if you subclass this, it will use the subclass's name, and
* if you subclass that, the sub-subclass's name. So, you *may* want to
* override this to return a constant string if you don't expect subclasses
* to have their own similar-but-different NIB file.
*
* REVISIONS:
* 2004-12-23 UK Documented.
*
* @return the filename (minus ".nib" suffix) for the Nib file.
*/
-(NSString*) nibFilename
{
return NSStringFromClass([self class]);
}
/**
* Returns the NSBundle to load the Nib from.
*
* REVISIONS:
* 2006-08-17 GN Created
*/
-(NSBundle*) bundle
{
return [NSBundle bundleForClass: [self class]];
}
@end