42 - (void)setDocumentView:(
CPView)aView
44 if (_documentView == aView)
53 name:CPViewFrameDidChangeNotification
54 object:_documentView];
58 name:CPViewBoundsDidChangeNotification
59 object:_documentView];
61 [_documentView removeFromSuperview];
64 _documentView = aView;
68 [
self addSubview:_documentView];
69 [
self _observeDocumentView];
73 - (void)_observeDocumentView
77 [_documentView setPostsFrameChangedNotifications:YES];
78 [_documentView setPostsBoundsChangedNotifications:YES];
82 selector:@selector(viewFrameChanged:)
83 name:CPViewFrameDidChangeNotification
84 object:_documentView];
88 selector:@selector(viewBoundsChanged:)
89 name:CPViewBoundsDidChangeNotification
90 object:_documentView];
107 - (CGPoint)constrainScrollPoint:(CGPoint)aPoint
110 return CGPointMakeZero();
112 var documentFrame = [_documentView frame];
114 aPoint.x = MAX(0.0, MIN(aPoint.x, MAX(CGRectGetWidth(documentFrame) - CGRectGetWidth(_bounds), 0.0)));
115 aPoint.y = MAX(0.0, MIN(aPoint.y, MAX(CGRectGetHeight(documentFrame) - CGRectGetHeight(_bounds), 0.0)));
120 - (void)setBoundsOrigin:(CGPoint)aPoint
122 if (CGPointEqualToPoint(_bounds.origin, aPoint))
125 [
super setBoundsOrigin:aPoint];
127 var superview = [
self superview],
131 scrollViewClass = objj_getClass(
"CPScrollView");
133 if ([superview isKindOfClass:scrollViewClass])
134 [superview reflectScrolledClipView:self];
141 - (void)scrollToPoint:(CGPoint)aPoint
143 [
self setBoundsOrigin:[
self constrainScrollPoint:aPoint]];
152 [
self _constrainScrollPoint];
161 [
self _constrainScrollPoint];
164 - (void)resizeSubviewsWithOldSize:(CGSize)aSize
166 [
super resizeSubviewsWithOldSize:aSize];
167 [
self _constrainScrollPoint];
170 - (void)_constrainScrollPoint
172 var oldScrollPoint = [
self bounds].origin;
176 [
self scrollToPoint:oldScrollPoint];
180 if (!CGPointEqualToPoint(oldScrollPoint, [
self bounds].origin))
184 var superview = [
self superview],
188 scrollViewClass = objj_getClass(
"CPScrollView");
190 if ([superview isKindOfClass:scrollViewClass])
191 [superview reflectScrolledClipView:self];
194 - (BOOL)autoscroll:(
CPEvent)anEvent
196 var bounds = [
self bounds],
197 eventLocation = [
self convertPoint:[anEvent locationInWindow] fromView:nil],
198 superview = [
self superview],
202 if (CGRectContainsPoint(bounds, eventLocation))
205 if (![superview isKindOfClass:[
CPScrollView class]] || [superview hasVerticalScroller])
207 if (eventLocation.y < CGRectGetMinY(bounds))
208 deltaY = CGRectGetMinY(bounds) - eventLocation.y;
209 else if (eventLocation.y > CGRectGetMaxY(bounds))
210 deltaY = CGRectGetMaxY(bounds) - eventLocation.y;
211 if (deltaY < -bounds.size.height)
212 deltaY = -bounds.size.height;
213 if (deltaY > bounds.size.height)
214 deltaY = bounds.size.height;
217 if (![superview isKindOfClass:[
CPScrollView class]] || [superview hasHorizontalScroller])
219 if (eventLocation.x < CGRectGetMinX(bounds))
220 deltaX = CGRectGetMinX(bounds) - eventLocation.x;
221 else if (eventLocation.x > CGRectGetMaxX(bounds))
222 deltaX = CGRectGetMaxX(bounds) - eventLocation.x;
223 if (deltaX < -bounds.size.width)
224 deltaX = -bounds.size.
width;
225 if (deltaX > bounds.size.width)
226 deltaX = bounds.size.width;
229 return [
self scrollToPoint:CGPointMake(bounds.origin.x - deltaX, bounds.origin.y - deltaY)];
235 var CPClipViewDocumentViewKey =
@"CPScrollViewDocumentView";
237 @implementation CPClipView (CPCoding)
239 - (id)initWithCoder:(
CPCoder)aCoder
241 if (
self = [super initWithCoder:aCoder])
245 _documentView = [aCoder decodeObjectForKey:CPClipViewDocumentViewKey];
246 [
self _observeDocumentView];
252 - (void)encodeWithCoder:(
CPCoder)aCoder
254 [
super encodeWithCoder:aCoder];
256 [aCoder encodeObject:_documentView forKey:CPClipViewDocumentViewKey];