This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SCBorderView.j
236 lines (190 loc) · 6.89 KB
/
SCBorderView.j
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
/*
* SCBorderView.j
* SCKit
*
* Created by Aparajita Fishman.
* Copyright (c) 2010, Aparajita Fishman.
*
* Released under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPGraphicsContext.j>
@import <AppKit/CPImage.j>
@import <AppKit/CPImageView.j>
@import <AppKit/CPView.j>
/*!
@ingroup sckit
*/
SCBorderViewImageTopLeft = 0;
SCBorderViewImageTop = 1;
SCBorderViewImageTopRight = 2;
SCBorderViewImageLeft = 3;
SCBorderViewImageCenter = 4;
SCBorderViewImageRight = 5;
SCBorderViewImageBottomLeft = 6;
SCBorderViewImageBottom = 7;
SCBorderViewImageBottomRight = 8;
SCBorderViewModeImages = 0;
SCBorderViewModeShadow = 1;
var SCBorderViewPathSuffixes = [
@"TopLeft.png",
@"Top.png",
@"TopRight.png",
@"Left.png",
@"Center.png",
@"Right.png",
@"BottomLeft.png",
@"Bottom.png",
@"BottomRight.png"
];
@implementation SCBorderView : CPView
{
SCBorderViewMode _mode @accessors(readonly, getter=mode)
CPView _contentView @accessors(readonly, getter=contentView);
float _borderWidth @accessors(readonly, getter=borderWidth);
CPColor _borderColor @accessors(readonly, getter=borderColor);
CGSize _shadowOffset @accessors(readonly, getter=shadowOffset);
float _shadowBlur @accessors(readonly, getter=shadowBlur);
CPColor _shadowColor @accessors(readonly, getter=shadowColor);
float _leftInset @accessors(readonly, getter=leftInset);
float _rightInset @accessors(readonly, getter=rightInset);
float _topInset @accessors(readonly, getter=topInset);
float _bottomInset @accessors(readonly, getter=bottomInset);
}
+ (id)borderViewEnclosingView:(CPView)aView
width:(float)aWidth
color:(CPColor)aColor
imagePath:(CPString)anImagePath
sizes:(CPArray)sizes
insets:(CPArray)insets
{
return [[SCBorderView alloc] initWithView:aView width:aWidth color:aColor imagePath:anImagePath sizes:sizes insets:insets];
}
+ (id)borderViewEnclosingView:(CPView)aView
width:(float)aWidth
color:(CPColor)aColor
shadowOffset:(CGSize)anOffset
shadowBlur:(float)aBlur
shadowColor:(CPColor)aShadowColor
{
return [[SCBorderView alloc] initWithView:aView width:aWidth color:aColor shadowOffset:anOffset shadowBlur:aBlur shadowColor:aShadowColor];
}
- (id)initWithView:(CPView)aView
width:(float)aWidth
color:(CPColor)aColor
imagePath:(CPString)anImagePath
sizes:(CPArray)sizes
insets:(CPArray)insets
{
self = [super initWithFrame:[aView frame]];
if (self)
{
_mode = SCBorderViewModeImages;
_borderWidth = aWidth;
_borderColor = aColor == nil ? [CPColor grayColor] : aColor;
_shadowOffset = CGSizeMakeZero();
_shadowBlur = 0;
_shadowColor = nil;
_topInset = insets[0] + _borderWidth;
_rightInset = insets[1] + _borderWidth;
_bottomInset = insets[2] + _borderWidth;
_leftInset = insets[3] + _borderWidth;
var path = [[CPBundle mainBundle] pathForResource:anImagePath],
slices = [CPArray arrayWithCapacity:9];
for (var i = 0; i < 9; ++i)
{
var size = [sizes objectAtIndex:i],
image = nil;
if (size != nil)
image = [[CPImage alloc] initWithContentsOfFile:path + [SCBorderViewPathSuffixes objectAtIndex:i] size:size];
[slices replaceObjectAtIndex:i withObject:image];
}
[self setBackgroundColor:[CPColor colorWithPatternImage:[[CPNinePartImage alloc] initWithImageSlices:slices]]];
[self _initWithView:aView];
}
return self;
}
- (id)initWithView:(CPView)aView
width:(float)aWidth
color:(CPColor)aColor
shadowOffset:(CGSize)anOffset
shadowBlur:(float)aBlur
shadowColor:(CPColor)aShadowColor
{
self = [super initWithFrame:[aView frame]];
if (self)
{
_mode = SCBorderViewModeShadow;
_borderWidth = aWidth;
_borderColor = aColor == nil ? [CPColor colorWithWhite:190.0 / 255.0 alpha:1.0] : aColor;
_shadowOffset = anOffset;
_shadowBlur = aBlur;
_shadowColor = aShadowColor == nil ? [CPColor colorWithWhite:190.0 / 255.0 alpha:1.0] : aShadowColor;
_topInset = _borderWidth + MAX(_shadowBlur - _shadowOffset.height, 0);
_rightInset = _borderWidth + _shadowOffset.width + _shadowBlur;
_bottomInset = _borderWidth + _shadowOffset.height + _shadowBlur;
_leftInset = _borderWidth + MAX(_shadowBlur - _shadowOffset.width, 0);
[self _initWithView:aView];
}
return self;
}
- (void)_initWithView:(CPView)aView
{
_contentView = aView;
var size = [self frame].size,
width = size.width - _leftInset - _rightInset,
height = size.height - _topInset - _bottomInset,
enclosingView = [_contentView superview];
[self setHitTests:[_contentView hitTests]];
[self setAutoresizingMask:[_contentView autoresizingMask]];
[_contentView removeFromSuperview];
[self addSubview:_contentView];
[_contentView setFrame:CGRectMake(_leftInset, _topInset, width, height)]
[enclosingView addSubview:self];
}
- (float)horizontalInset
{
return _leftInset + _rightInset;
}
- (float)verticalInset
{
return _topInset + _bottomInset;
}
- (CGRect)frameForContentFrame:(CGRect)aFrame
{
return CGRectMake(CGRectGetMinX(aFrame) - _leftInset,
CGRectGetMinY(aFrame) - _topInset,
CGRectGetWidth(aFrame) + _leftInset + _rightInset,
CGRectGetHeight(aFrame) + _topInset + _bottomInset);
}
- (void)setFrameForContentFrame:(CGRect)aFrame
{
[self setFrame:[self frameForContentFrame:aFrame]];
}
- (void)drawRect:(CGRect)aRect
{
[super drawRect:aRect];
if (_mode == SCBorderViewModeShadow)
{
var context = [[CPGraphicsContext currentContext] graphicsPort],
frame = [_contentView frame],
fillRect = CGRectInset(frame, -_borderWidth, -_borderWidth),
strokeRect = CGRectInset(frame, -_borderWidth * 0.5, -_borderWidth * 0.5);
if (_shadowBlur > 0)
{
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, _shadowOffset, _shadowBlur, _shadowColor);
CGContextSetFillColor(context, [CPColor whiteColor]);
CGContextFillRect(context, fillRect);
CGContextRestoreGState(context);
}
if (_borderWidth > 0)
{
CGContextSetLineWidth(context, _borderWidth);
CGContextSetStrokeColor(context, _borderColor);
CGContextStrokeRect(context, strokeRect);
}
}
}
@end