forked from sononum/bwtoolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBWCustomView.m
186 lines (158 loc) · 7.95 KB
/
BWCustomView.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
//
// BWGradientSplitViewSubview.m
// BWToolkit
//
// Created by Brandon Walkin (www.brandonwalkin.com)
// All code is provided under the New BSD license.
//
#import "BWCustomView.h"
#import "NSColor+BWAdditions.h"
#import "NSWindow+BWAdditions.h"
#import "NSApplication+BWAdditions.h"
#import "IBColor.h"
@interface BWCustomView (BWCVPrivate)
- (void)drawTextInRect:(NSRect)rect;
- (NSColor *)containerCustomViewBackgroundColor;
- (NSColor *)childlessCustomViewBackgroundColor;
- (NSColor *)customViewDarkTexturedBorderColor;
- (NSColor *)customViewDarkBorderColor;
- (NSColor *)customViewLightBorderColor;
@end
@implementation BWCustomView
- (void)drawRect:(NSRect)rect
{
rect = self.bounds;
NSColor *insetColor = [self customViewLightBorderColor];
NSColor *borderColor;
if ([[self window] bwIsTextured])
borderColor = [self customViewDarkTexturedBorderColor];
else
borderColor = [self customViewDarkBorderColor];
// Note: These two colors are reversed in IBColor in 10.5
if (self.subviews.count == 0)
{
if ([NSApplication bwIsOnLeopard])
[[self containerCustomViewBackgroundColor] set];
else
[[self childlessCustomViewBackgroundColor] set];
NSRectFillUsingOperation(rect,NSCompositeSourceOver);
}
else
{
if ([NSApplication bwIsOnLeopard])
[[self childlessCustomViewBackgroundColor] set];
else
[[self containerCustomViewBackgroundColor] set];
NSRectFillUsingOperation(rect,NSCompositeSourceOver);
}
if ([[self superview] isKindOfClass:NSClassFromString(@"BWSplitView")] && [[self superview] subviews].count > 1)
{
isOnItsOwn = NO;
NSArray *subviews = [[self superview] subviews];
if ([subviews objectAtIndex:0] == self)
{
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
if ([(NSSplitView *)[self superview] isVertical])
{
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
}
else
{
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
}
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
}
else if ([subviews lastObject] == self)
{
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
if ([(NSSplitView *)[self superview] isVertical])
{
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
}
else
{
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
}
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
}
else
{
if ([(NSSplitView *)[self superview] isVertical])
{
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
}
else
{
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
[insetColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
}
}
}
else
{
isOnItsOwn = YES;
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
[insetColor bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:NO];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:NO flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
[borderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
}
if (rect.size.height > 16)
[self drawTextInRect:rect];
}
- (void)drawTextInRect:(NSRect)rect
{
NSString *text;
if (isOnItsOwn)
text = [NSString stringWithFormat:@"%d x %d pt",(int)rect.size.width,(int)rect.size.height];
else if ([(NSSplitView *)[self superview] isVertical])
text = [NSString stringWithFormat:@"%d pt",(int)rect.size.width];
else
text = [NSString stringWithFormat:@"%d pt",(int)rect.size.height];
if (![self.className isEqualToString:@"NSView"])
text = self.className;
NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
[attributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
[attributes setObject:[NSFont boldSystemFontOfSize:12] forKey:NSFontAttributeName];
NSShadow *shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:0.4]];
[attributes setObject:shadow forKey:NSShadowAttributeName];
NSMutableAttributedString *string = [[[NSMutableAttributedString alloc] initWithString:text attributes:attributes] autorelease];
NSRect boundingRect = [string boundingRectWithSize:rect.size options:0];
NSPoint rectCenter;
rectCenter.x = rect.size.width / 2;
rectCenter.y = rect.size.height / 2;
NSPoint drawPoint = rectCenter;
drawPoint.x -= boundingRect.size.width / 2;
drawPoint.y -= boundingRect.size.height / 2;
drawPoint.x = roundf(drawPoint.x);
drawPoint.y = roundf(drawPoint.y);
[string drawAtPoint:drawPoint];
}
@end