-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNSAlertCheckbox.m
More file actions
executable file
·179 lines (143 loc) · 4.43 KB
/
NSAlertCheckbox.m
File metadata and controls
executable file
·179 lines (143 loc) · 4.43 KB
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
//
// NSAlertCheckbox.m
// PermanentEraser
//
// Created by Chad Armstrong on 8/13/09.
// Copyright 2009 Edenwaith. All rights reserved.
//
#import "NSAlertCheckbox.h"
@interface NSAlertCheckbox (Private)
- (void)_addCheckboxToWindow;
@end
@implementation NSAlertCheckbox
- (id)init {
if (self = [super init]) {
_checkbox = nil;
}
return self;
}
- (void)dealloc {
[_checkbox release];
[super dealloc];
}
+ (NSAlertCheckbox *)alertWithMessageText:(NSString *)message defaultButton:(NSString *)defaultButton alternateButton:(NSString *)alternateButton otherButton:(NSString *)otherButton informativeText:(NSString *)format
{
NSAlert *alert = [super alertWithMessageText:message
defaultButton:defaultButton
alternateButton:alternateButton
otherButton:otherButton
informativeTextWithFormat:format];
return (NSAlertCheckbox *)alert;
}
#pragma mark -
- (BOOL)showsCheckbox {
return (_checkbox != nil);
}
- (void)setShowsCheckbox:(BOOL)showsCheckbox {
if (showsCheckbox && !_checkbox) {
_checkbox = [[NSButton alloc] initWithFrame:NSZeroRect];
[_checkbox setButtonType:NSSwitchButton];
} else if (!showsCheckbox && _checkbox) {
if ([_checkbox superview]) {
[_checkbox removeFromSuperview];
}
[_checkbox release];
_checkbox = nil;
}
}
- (NSString *)checkboxText {
NSString *text = nil;
if ([self showsCheckbox]) {
text = [_checkbox title];
}
return text;
}
- (void)setCheckboxText:(NSString *)text {
if ([self showsCheckbox]) {
[_checkbox setTitle:text];
}
}
- (int)checkboxState {
int state = -1;
if ([self showsCheckbox]) {
state = [_checkbox state];
}
return state;
}
- (void)setCheckboxState:(int)state {
if ([self showsCheckbox]) {
[_checkbox setState:state];
}
}
#pragma mark -
- (id)buildAlertStyle:(int)fp8 title:(id)fp12 formattedMsg:(id)fp16 first:(id)fp20 second:(id)fp24 third:(id)fp28 oldStyle:(BOOL)fp32 {
id retVal = [super buildAlertStyle:fp8 title:fp12 formattedMsg:fp16 first:fp20 second:fp24 third:fp28 oldStyle:fp32];
[self _addCheckboxToWindow];
return retVal;
}
- (id)buildAlertStyle:(int)fp8 title:(id)fp12 message:(id)fp16 first:(id)fp20 second:(id)fp24 third:(id)fp28 oldStyle:(BOOL)fp32 args:(char *)fp36 {
id retVal = [super buildAlertStyle:fp8 title:fp12 message:fp16 first:fp20 second:fp24 third:fp28 oldStyle:fp32 args:fp36];
[self _addCheckboxToWindow];
return retVal;
}
@end
// Some of the odd bits like -4 and +1 to the padding and adjustment of the placement of buttons is to get the
// alert box to look as close as possible as the one in Mac OS 10.5+. The only difference is the checkbox
// font is a tad smaller, which actually looks a little better than what OS 10.5+'s NSAlert offers.
@implementation NSAlertCheckbox (Private)
- (void)_addCheckboxToWindow {
float checkboxPadding = 12.0f; // according to the apple HIG
if ([self showsCheckbox]) {
NSWindow *window = [self window];
NSView *content = [window contentView];
// find the position of the main text field
NSArray *subviews = [content subviews];
NSEnumerator *en = [subviews objectEnumerator];
NSView *subview = nil;
NSTextField *messageText = nil;
int count = 0;
while (subview = [en nextObject]) {
if ([subview isKindOfClass:[NSTextField class]]) {
count++;
if (count == 2) {
messageText = (NSTextField *)subview;
}
}
}
if (messageText)
{
float windowPadding = 0.0;
float messageTextHeight = [messageText bounds].size.height;
[content addSubview:_checkbox];
// make the checkbox font match the text area above it
[_checkbox setFont:[messageText font]];
[_checkbox sizeToFit];
if (messageTextHeight <= 14.0)
{
windowPadding = -18.0;
}
else if (messageTextHeight <= 28.0)
{
windowPadding = -4.0;
}
else if (messageTextHeight <= 42.0)
{
windowPadding = 6.0;
}
else if (messageTextHeight <= 70.0)
{
windowPadding = 7.0;
}
// expand the window
NSRect windowFrame = [window frame];
NSRect checkboxFrame = [_checkbox frame];
windowFrame.size.height += checkboxFrame.size.height + checkboxPadding + windowPadding;
[window setFrame:windowFrame display:YES];
checkboxFrame.origin.y = [messageText frame].origin.y - checkboxFrame.size.height - checkboxPadding;
checkboxFrame.origin.x = [messageText frame].origin.x + 1;
[_checkbox setFrame:checkboxFrame];
// [window makeFirstResponder:[[self buttons] objectAtIndex:0]];
}
}
}
@end