forked from karelia/iMedia
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathIMBAlertPopoverViewController.m
432 lines (325 loc) · 11.8 KB
/
IMBAlertPopoverViewController.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//**********************************************************************************************************************
//
// IMBAlertPopoverViewController.m
//
// Author: Peter Baumgartner, [email protected]
// Copyright: ©2011 by IMAGINE GbR. All rights reserved.
// Abstract: View conroller for alert popovers
//
//**********************************************************************************************************************
#pragma HEADERS
#import "IMBAlertPopoverViewController.h"
//----------------------------------------------------------------------------------------------------------------------
@implementation IMBAlertPopoverViewController
@synthesize icon = _icon;
@synthesize headerString = _headerString;
@synthesize bodyString = _bodyString;
@synthesize footerString = _footerString;
@synthesize headerTextColor = _headerTextColor;
@synthesize bodyTextColor = _bodyTextColor;
@synthesize footerTextColor = _footerTextColor;
//----------------------------------------------------------------------------------------------------------------------
- (id) initWithNibName:(NSString*)inNibName bundle:(NSBundle*)inBundle
{
self = [super initWithNibName:inNibName bundle:inBundle];
if (self)
{
_buttonInfo = [[NSMutableArray alloc] init];
}
return self;
}
// Cleanup...
- (void) dealloc
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
IMBRelease(_icon);
IMBRelease(_headerString);
IMBRelease(_bodyString);
IMBRelease(_footerString);
IMBRelease(_buttonInfo);
IMBRelease(_headerTextColor);
IMBRelease(_bodyTextColor);
IMBRelease(_footerTextColor);
[super dealloc];
}
//----------------------------------------------------------------------------------------------------------------------
// Calculate the optimum width of the given text field, for its current content and the proposed width...
- (NSSize) _bestSizeForTextField:(NSTextField*)inTextField width:(CGFloat)inProposedWidth
{
// First save the current string contents...
NSString* originalString = inTextField.stringValue;
// Divide the string into several paragraphs at line breaks. Measure each paragraph separately,
// and add up the results...
NSArray* paragraphs = [originalString componentsSeparatedByString:@"\n"];
NSSize totalSize = NSZeroSize;
for (NSString* paragraph in paragraphs)
{
// Within each paragraph measure each line separately, as soft line breaks can occur at
// unpredictable places due to varying word lengths...
NSArray* words = [paragraph componentsSeparatedByString:@" "];
NSUInteger wordCount = words.count;
NSUInteger n = wordCount;
NSSize lineSize = NSZeroSize;
NSUInteger i1 = 0;
for (NSUInteger i2=0; i2<wordCount; i2++)
{
NSArray* words2 = [words subarrayWithRange:NSMakeRange(i1,i2-i1+1)];
NSString* line = [words2 componentsJoinedByString:@" "];
inTextField.stringValue = line;
lineSize = [inTextField intrinsicContentSize];
if ((lineSize.width > inProposedWidth) && ([words2 count] > 1))
{
n -= i2 - i1;
i1 = i2--;
totalSize.height += lineSize.height;
totalSize.width = inProposedWidth;
}
}
if (n > 0)
{
totalSize.height += lineSize.height;
totalSize.width = inProposedWidth;
}
}
// Restore the original string...
inTextField.stringValue = originalString;
return totalSize;
}
- (void) adjustLayout
{
// Get rid of contraints that IB has generated, since we cannot do it in Xcode 4.2 - and changing them
// also doesn't seam possible...
NSView* superview = _headerTextField.superview;
NSDictionary* views = NSDictionaryOfVariableBindings(
_iconView,
_headerTextField,
_bodyTextField,
_footerTextField,
_button0,
_button1,
_button2,
_progressBackground,
_progressWheel);
[superview removeConstraints:superview.constraints];
[_iconView removeConstraints:_iconView.constraints];
[_headerTextField removeConstraints:_headerTextField.constraints];
[_bodyTextField removeConstraints:_bodyTextField.constraints];
[_footerTextField removeConstraints:_footerTextField.constraints];
[_button0 removeConstraints:_button0.constraints];
[_button1 removeConstraints:_button1.constraints];
[_button2 removeConstraints:_button2.constraints];
// Calculate the button sizes...
CGFloat totalButtonWidth = 0.0;
CGFloat totalButtonHeight = 0.0;
NSSize buttonSize0 = NSZeroSize;
NSSize buttonSize1 = NSZeroSize;
NSSize buttonSize2 = NSZeroSize;
NSUInteger buttonCount = _buttonInfo.count;
if (buttonCount > 0)
{
[_button0 setHidden:NO];
buttonSize0 = [_button0 intrinsicContentSize];
totalButtonWidth += buttonSize0.width + 8.0;
totalButtonHeight = MAX(totalButtonHeight,buttonSize0.height);
}
if (buttonCount > 1)
{
[_button1 setHidden:NO];
buttonSize1 = [_button1 intrinsicContentSize];
totalButtonWidth += buttonSize1.width + 8.0;
totalButtonHeight = MAX(totalButtonHeight,buttonSize1.height);
}
if (buttonCount > 2)
{
[_button2 setHidden:NO];
buttonSize2 = [_button2 intrinsicContentSize];
totalButtonWidth += buttonSize2.width + 8.0;
totalButtonHeight = MAX(totalButtonHeight,buttonSize2.height);
}
if (buttonCount > 0) totalButtonWidth -= 8.0;
// Calculate the optimum textfield sizes (depending on their content)...
NSSize headerSize = [_headerTextField intrinsicContentSize];
NSSize bodySize = [_bodyTextField intrinsicContentSize];
NSSize footerSize /*= [_footerTextField intrinsicContentSize]*/;
headerSize.width += 32.0;
CGFloat minWidth = MAX(220.0,totalButtonWidth);
CGFloat maxWidth = MAX(360.0,totalButtonWidth);
if (headerSize.width < 360.0) minWidth = MAX(minWidth,headerSize.width);
CGFloat width = bodySize.width / 3.0;
if (width < minWidth) width = minWidth;
if (width > maxWidth) width = maxWidth;
headerSize = [self _bestSizeForTextField:_headerTextField width:width];
bodySize = [self _bestSizeForTextField:_bodyTextField width:width];
footerSize = [self _bestSizeForTextField:_footerTextField width:width];
if (self.headerString == nil) headerSize.height = 0.0;
if (self.bodyString == nil) bodySize.height = 0.0;
if (self.footerString == nil) footerSize.height = 0.0;
// Horizontal layout...
NSString* horizontalFormat = nil;
if (self.icon)
{
horizontalFormat = [NSString stringWithFormat:
@"|-10-[_iconView(32)]-14-[_headerTextField(%d@800)]-16-|",
(int)headerSize.width];
}
else
{
horizontalFormat = [NSString stringWithFormat:
@"|-16-[_iconView(0)][_headerTextField(%d@800)]-16-|",
(int)headerSize.width];
}
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:horizontalFormat
options:0
metrics:nil
views:views]];
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"[_bodyTextField(==_headerTextField)]"
options:0
metrics:nil
views:views]];
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"[_footerTextField(==_headerTextField)]"
options:0
metrics:nil
views:views]];
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"|[_progressBackground]|"
options:0
metrics:nil
views:views]];
// Button layout...
NSMutableString* buttonFormat = [NSMutableString string];
[buttonFormat appendFormat:@"[_button0(%d)]",(int)buttonSize0.width];
if (buttonCount > 0)
{
[buttonFormat appendString:@"-"];
}
[buttonFormat appendFormat:@"[_button1(%d)]",(int)buttonSize1.width];
if (buttonCount > 0)
{
[buttonFormat appendString:@"-"];
}
[buttonFormat appendFormat:@"[_button2(%d)]->=16@800-|",(int)buttonSize2.width];
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:buttonFormat
options:NSLayoutFormatAlignAllTop
metrics:nil
views:views]];
// Define the Vertical layout...
NSMutableString* verticalFormat = [NSMutableString stringWithString:@"V:|-10-"];
[verticalFormat appendFormat:@"[_headerTextField(%d)]",(int)headerSize.height];
[verticalFormat appendString:@"-"];
[verticalFormat appendFormat:@"[_bodyTextField(%d)]",(int)bodySize.height];
if (buttonCount > 0)
{
if (self.footerString)
{
[verticalFormat appendString:@"-12-"];
[verticalFormat appendFormat:@"[_button0(%d)]",(int)totalButtonHeight];
[verticalFormat appendString:@"-12-"];
[verticalFormat appendFormat:@"[_footerTextField(%d)]",(int)footerSize.height];
[verticalFormat appendString:@"-12-|"];
}
else
{
[verticalFormat appendString:@"-12-"];
[verticalFormat appendFormat:@"[_button0(%d)]",(int)totalButtonHeight];
[verticalFormat appendFormat:@"[_footerTextField(%d)]",0];
[verticalFormat appendString:@"-16-|"];
}
}
else
{
if (self.footerString)
{
[verticalFormat appendString:@"-"];
[verticalFormat appendFormat:@"[_button0(%d)]",0];
[verticalFormat appendFormat:@"[_footerTextField(%d)]",(int)footerSize.height];
[verticalFormat appendString:@"-12-|"];
}
else
{
[verticalFormat appendFormat:@"[_button0(%d)]",0];
[verticalFormat appendFormat:@"[_footerTextField(%d)]",0];
[verticalFormat appendString:@"-12-|"];
}
}
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:verticalFormat
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:views]];
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-12-[_iconView(32)]"
options:0
metrics:nil
views:views]];
[superview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[_progressBackground]|"
options:0
metrics:nil
views:views]];
}
//----------------------------------------------------------------------------------------------------------------------
- (void) addButtonWithTitle:(NSString*)inTitle block:(IMBButtonBlockType)inBlock
{
IMBButtonBlockType block = [inBlock copy];
NSDictionary* buttonInfo = [NSDictionary dictionaryWithObjectsAndKeys:
inTitle,@"title",
block,@"block",
nil];
[block release];
[_buttonInfo addObject:buttonInfo];
}
- (IBAction) buttonAction:(id)inSender
{
NSButton* button = (NSButton*)inSender;
IMBButtonBlockType block = [button.cell representedObject];
block();
}
//----------------------------------------------------------------------------------------------------------------------
- (void) awakeFromNib
{
// Set icon and strings...
if (self.icon) _iconView.image = self.icon;
if (self.headerString) _headerTextField.stringValue = self.headerString; else _headerTextField.stringValue = @"";
if (self.bodyString) _bodyTextField.stringValue = self.bodyString; else _bodyTextField.stringValue = @"";
if (self.footerString) _footerTextField.stringValue = self.footerString; else _footerTextField.stringValue = @"";
// Add optional buttons...
NSUInteger i = 0;
NSButton* button = nil;
for (NSDictionary* buttonInfo in _buttonInfo)
{
if (i == 0) button = _button0;
else if (i == 1) button = _button1;
else button = _button2;
i++;
[button setTitle:[buttonInfo objectForKey:@"title"]];
[button.cell setRepresentedObject:[buttonInfo objectForKey:@"block"]];
[button setTarget:self];
[button setAction:@selector(buttonAction:)];
}
// Set custom colors...
if (_headerTextColor) [_headerTextField setTextColor:_headerTextColor];
if (_bodyTextColor) [_footerTextField setTextColor:_bodyTextColor];
if (_footerTextColor) [_footerTextField setTextColor:_footerTextColor];
// Adjust the layout according to contents...
[self adjustLayout];
}
//----------------------------------------------------------------------------------------------------------------------
// Show or hide the dimming box with the progress wheel...
- (void) showProgressIndicator
{
[_progressBackground setHidden:NO];
[_progressWheel setUsesThreadedAnimation:YES];
[_progressWheel startAnimation:nil];
}
- (void) hideProgressIndicator
{
[_progressBackground setHidden:YES];
[_progressWheel setUsesThreadedAnimation:NO];
[_progressWheel stopAnimation:nil];
}
//----------------------------------------------------------------------------------------------------------------------
@end