-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYLabelledTextField.m
executable file
·279 lines (233 loc) · 9.27 KB
/
SSYLabelledTextField.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#import "SSYLabelledTextField.h"
#import "NSView+Layout.h"
#import "SSYAlert.h"
#pragma mark * Const Keys for En/Decoding Instance Variables
NSString* const constKeyKeyField = @"KeyField" ;
NSString* const constKeyValueField = @"ValueField" ;
NSString* const constKeyValidationSelector = @"ValidationSelector" ;
NSString* const constKeyValidationObject = @"ValidationObject" ;
NSString* const constKeyWindowController = @"WindowController" ;
NSString* const constKeyErrorMessage = @"ErrorMessage" ;
NSString* const constKeyTextFieldTag = @"tag" ;
@interface NSWindowController (Disableable)
- (void)setEnabled:(BOOL)enabled ;
- (void)setWhyDisabled:(NSString*)why ;
@end
@implementation SSYLabelledTextField
@synthesize keyField ;
@synthesize valueField ;
@synthesize validationSelector ;
@synthesize validationObject ;
@synthesize windowController ;
@synthesize errorMessage ;
@synthesize tag = m_tag ;
- (void)setStringValue:(NSString*)stringValue {
[[self valueField] setStringValue:stringValue] ;
}
- (NSString*)stringValue {
return [[self valueField] stringValue] ;
}
- (void)validate {
SEL validationSelector_ = [self validationSelector] ;
if (validationSelector_ != NULL) {
BOOL ok = NO ;
ok = [[[self stringValue] performSelector:validationSelector
withObject:[self validationObject]] boolValue] ;
NSWindowController* windowController_ = [[self window] windowController] ;
if (!windowController_) {
windowController_ = windowController ;
}
if ([windowController_ respondsToSelector:@selector(setEnabled:)]) {
[windowController_ setEnabled:ok] ;
}
else if ([windowController_ respondsToSelector:@selector(setIsEnabled:)]) {
[(id)windowController_ setIsEnabled:ok] ;
}
if ([windowController_ respondsToSelector:@selector(setWhyDisabled:)]) {
NSString* why = ok ? nil : [self errorMessage] ;
[windowController_ setWhyDisabled:why] ;
}
}
}
#define CONTROL_VERTICAL_SPACING 14.0
- (id)initAsSecure:(BOOL)secure
validationSelector:(SEL)validationSelector_
validationObject:(id)validationObject_
windowController:(NSWindowController*)windowController_
displayedKey:(NSString*)displayedKey
displayedValue:(NSString*)displayedValue
editable:(BOOL)editable_
tag:(NSInteger)tag
errorMessage:(NSString*)errorMessage_ {
self = [super initWithFrame:NSZeroRect];
if (self != nil) {
NSTextField* textField ;
CGFloat y ;
// Create and add the value field
Class fieldClass = secure ? [NSSecureTextField class] : [NSTextField class] ;
textField = [[fieldClass alloc] initWithFrame:NSMakeRect(0, 0, 100, [SSYAlert smallTextHeight] + 4.0)] ;
[textField setFont:[SSYAlert smallTextFont]] ;
[textField setBordered:NO] ;
[textField setEditable:YES] ;
[textField setDrawsBackground:YES] ;
[textField setLeftEdge:0.0] ;
[textField setBottom:0.0] ;
[textField setDelegate:self] ; // for -controlTextDidChange
[self setValueField:textField] ;
[self addSubview:textField] ;
y = [textField height] ;
[textField release] ;
// Create and add the key field
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, [SSYAlert smallTextHeight])] ;
[textField setFont:[SSYAlert smallTextFont]] ;
[textField setBordered:NO] ;
[textField setEditable:NO] ;
[textField setDrawsBackground:NO] ;
y += CONTROL_VERTICAL_SPACING/2 ; // Divide by two so key field is close to value field
[textField setLeftEdge:0.0] ;
[textField setBottom:y] ;
[self setKeyField:textField] ;
[self addSubview:textField] ;
y += [textField height] ;
[textField release] ;
// Start out with self and subviews all at same width,
// then set to track future changes to self's width
[self setWidth:[textField width]] ;
[[self keyField] setAutoresizingMask:NSViewWidthSizable] ;
[[self valueField] setAutoresizingMask:NSViewWidthSizable] ;
// This looks weird. I suppose it makes sense, if there is only
// one field in the keyboard loop at this level??
[[self valueField] setNextKeyView:[self valueField]] ;
[self setHeight:y] ;
[self setValidationSelector:validationSelector_] ;
[self setValidationObject:validationObject_] ;
[self setWindowController:windowController_] ;
if (displayedKey) {
[[self keyField] setStringValue:displayedKey] ;
}
if (displayedValue) {
[[self valueField] setStringValue:displayedValue] ;
}
[[self valueField] setEditable:editable_] ;
[self setErrorMessage:errorMessage_] ;
[self setTag:tag] ;
// The following is needed in the usual case where, upon initialization,
// the initial value of @"" is invalid. We need to send validation
// messages (actually invalidation messages) to the window controller.
[self validate] ;
}
return self;
}
+ (SSYLabelledTextField*)labelledTextFieldSecure:(BOOL)secure
validationSelector:(SEL)validationSelector
validationObject:(id)validationObject
windowController:(id)windowController
displayedKey:(NSString*)displayedKey
displayedValue:(NSString*)displayedValue
editable:(BOOL)editable
tag:(NSInteger)tag
errorMessage:(NSString*)errorMessage {
SSYLabelledTextField* instance = [[SSYLabelledTextField alloc] initAsSecure:secure
validationSelector:validationSelector
validationObject:validationObject
windowController:windowController
displayedKey:displayedKey
displayedValue:displayedValue
editable:editable
tag:tag
errorMessage:errorMessage] ;
return [instance autorelease] ;
}
- (void) dealloc {
[keyField release] ;
[valueField release] ;
[validationObject release] ;
[windowController release] ;
[errorMessage release] ;
[super dealloc];
}
- (NSString*)description {
NSArray* keys = [NSArray arrayWithObjects:
@"keyField",
@"valueField",
@"validationObject",
@"windowController",
@"errorMessage",
nil] ;
NSDictionary* dictionary = [self dictionaryWithValuesForKeys:keys] ;
NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithDictionary:dictionary] ;
[dic setValue:NSStringFromSelector([self validationSelector])
forKey:@"validationSelector"] ;
[dic setValue:[NSNumber numberWithInteger:[self tag]]
forKey:@"tag"] ;
return [NSString stringWithFormat:
@"%@ %@",
[super description],
[dic description]] ;
}
- (void)sizeHeightToFitAllowShrinking:(BOOL)allowShrinking {
[[self keyField] sizeHeightToFitAllowShrinking:allowShrinking] ;
[self setHeight:
[[self valueField] height]
+ CONTROL_VERTICAL_SPACING
+ [[self keyField] height]] ;
}
- (BOOL)acceptsFirstResponder {
return [[self valueField] acceptsFirstResponder] ;
}
- (void)setNextKeyView:(NSView*)view {
[[self valueField] setNextKeyView:view] ;
}
/*- (NSView*)nextKeyView {
NSView* view = [[self editableField] nextKeyView] ;
return view ;
}
- (NSView*)previousKeyView {
NSView* view = [super previousKeyView] ;
if (!view) {
view = [[self editableField] previousKeyView] ;
}
return view ;
}
*/
- (BOOL)becomeFirstResponder {
BOOL x = [[self window] makeFirstResponder:[self valueField]] ;
return x ;
}
- (NSString*)title { // for debugging
return [[self keyField] stringValue] ;
}
// Delegate methods
- (void)controlTextDidChange:(NSNotification *)notification {
[self validate] ;
}
#pragma mark * NSCoding Protocol Conformance
// See http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/Tasks/codingobjects.html
// Although NSResponder::NSControl subclasses can sometimes get away with not implementing these
// two methods, not so if the control is used in SSYAlert, because SSYAlert will encode it when
// adding to its configurations stack.
// @encode(type_spec) is a compiler directive that returns a character string that encodes
// the type structure of type_spec. It can be used as the first argument of can be used as
// the first argument of encodeValueOfObjCType:at:
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder] ;
[coder encodeObject:keyField forKey:constKeyKeyField] ;
[coder encodeObject:valueField forKey:constKeyValueField] ;
[coder encodeObject:NSStringFromSelector(validationSelector) forKey:constKeyValidationSelector] ;
[coder encodeObject:validationObject forKey:constKeyValidationObject] ;
[coder encodeObject:windowController forKey:constKeyWindowController] ;
[coder encodeObject:errorMessage forKey:constKeyErrorMessage] ;
[coder encodeInteger:m_tag forKey:constKeyTextFieldTag] ;
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder] ;
keyField = [[coder decodeObjectForKey:constKeyKeyField] retain] ;
valueField = [[coder decodeObjectForKey:constKeyValueField] retain] ;
validationSelector = NSSelectorFromString([coder decodeObjectForKey:constKeyValidationSelector]) ;
validationObject = [[coder decodeObjectForKey:constKeyValidationObject] retain] ;
windowController = [[coder decodeObjectForKey:constKeyWindowController] retain] ;
errorMessage = [[coder decodeObjectForKey:constKeyErrorMessage] retain] ;
m_tag = [coder decodeIntegerForKey:constKeyTextFieldTag] ;
return self ;
}
@end