00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPView.j"
00024
00025 #include "CoreGraphics/CGGeometry.h"
00026
00027
00033 @implementation CPClipView : CPView
00034 {
00035 CPView _documentView;
00036 }
00037
00042 - (void)setDocumentView:(CPView)aView
00043 {
00044 if (_documentView == aView)
00045 return;
00046
00047 var defaultCenter = [CPNotificationCenter defaultCenter];
00048
00049 if (_documentView)
00050 {
00051 [defaultCenter
00052 removeObserver:self
00053 name:CPViewFrameDidChangeNotification
00054 object:_documentView];
00055
00056 [defaultCenter
00057 removeObserver:self
00058 name:CPViewBoundsDidChangeNotification
00059 object:_documentView];
00060
00061 [_documentView removeFromSuperview];
00062 }
00063
00064 _documentView = aView;
00065
00066 if (_documentView)
00067 {
00068
00069 [_documentView setFrameOrigin:CGPointMake(0.0, 0.0)];
00070
00071 [self addSubview:_documentView];
00072
00073 [_documentView setPostsFrameChangedNotifications:YES];
00074 [_documentView setPostsBoundsChangedNotifications:YES];
00075
00076 [defaultCenter
00077 addObserver:self
00078 selector:@selector(viewFrameChanged:)
00079 name:CPViewFrameDidChangeNotification
00080 object:_documentView];
00081
00082 [defaultCenter
00083 addObserver:self
00084 selector:@selector(viewBoundsChanged:)
00085 name:CPViewBoundsDidChangeNotification
00086 object:_documentView];
00087 }
00088 }
00089
00093 - (id)documentView
00094 {
00095 return _documentView;
00096 }
00097
00104 - (CGPoint)constrainScrollPoint:(CGPoint)aPoint
00105 {
00106 var documentFrame = [_documentView frame];
00107
00108 aPoint.x = MAX(0.0, MIN(aPoint.x, MAX(_CGRectGetWidth(documentFrame) - _CGRectGetWidth(_bounds), 0.0)));
00109 aPoint.y = MAX(0.0, MIN(aPoint.y, MAX(_CGRectGetHeight(documentFrame) - _CGRectGetHeight(_bounds), 0.0)));
00110
00111 return aPoint;
00112 }
00113
00114 - (void)setBoundsOrigin:(CGPoint)aPoint
00115 {
00116 if (_CGPointEqualToPoint(_bounds.origin, aPoint))
00117 return;
00118
00119 [super setBoundsOrigin:aPoint];
00120
00121 var superview = [self superview];
00122
00123 if([superview isKindOfClass:[CPScrollView class]])
00124 [superview reflectScrolledClipView:self];
00125 }
00126
00131 - (void)scrollToPoint:(CGPoint)aPoint
00132 {
00133 [self setBoundsOrigin:[self constrainScrollPoint:aPoint]];
00134 }
00135
00140 - (void)viewBoundsChanged:(CPNotification)aNotification
00141 {
00142 [self viewFrameChanged:aNotification];
00143 }
00144
00149 - (void)viewFrameChanged:(CPNotification)aNotification
00150 {
00151 var oldScrollPoint = [self bounds].origin;
00152
00153
00154
00155 [self scrollToPoint:oldScrollPoint];
00156
00157
00158
00159 if (!CGPointEqualToPoint(oldScrollPoint, [self bounds].origin))
00160 return;
00161
00162
00163 var superview = [self superview];
00164
00165 if ([superview isKindOfClass:[CPScrollView class]])
00166 [superview reflectScrolledClipView:self];
00167 }
00168
00169 @end