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
00026 CPNoBorder = 0;
00027 CPLineBorder = 1;
00028 CPBezelBorder = 2;
00029 CPGrooveBorder = 3;
00030
00031 @implementation CPBox : CPView
00032 {
00033 CPBorderType _borderType;
00034
00035 CPColor _borderColor;
00036 CPColor _fillColor;
00037
00038 float _cornerRadius;
00039 float _borderWidth;
00040
00041 CPSize _contentMargin;
00042 CPView _contentView;
00043 }
00044
00045 + (id)boxEnclosingView:(CPView)aView
00046 {
00047 var box = [[self alloc] initWithFrame:CGRectMakeZero()],
00048 enclosingView = [aView superview];
00049
00050 [box setFrameFromContentFrame:[aView frame]];
00051
00052 [enclosingView replaceSubview:aView with:box];
00053
00054 [box setContentView:aView];
00055
00056 return box;
00057 }
00058
00059 - (id)initWithFrame:(CPRect)frameRect
00060 {
00061 self = [super initWithFrame:frameRect];
00062
00063 if (self)
00064 {
00065 _fillColor = [CPColor clearColor];
00066 _borderColor = [CPColor blackColor];
00067
00068 _borderWidth = 1.0;
00069 _contentMargin = CGSizeMake(0.0, 0.0);
00070
00071 _contentView = [[CPView alloc] initWithFrame:[self bounds]];
00072
00073 [self addSubview:_contentView];
00074 }
00075
00076 return self;
00077 }
00078
00079
00080
00081 - (CPRect)borderRect
00082 {
00083 return [self bounds];
00084 }
00085
00086 - (CPBorderType)borderType
00087 {
00088 return _borderType;
00089 }
00090
00091 - (void)setBorderType:(CPBorderType)value
00092 {
00093 _borderType = value;
00094 [self setNeedsDisplay:YES];
00095 }
00096
00097 - (CPColor)borderColor
00098 {
00099 return _borderColor;
00100 }
00101
00102 - (void)setBorderColor:(CPColor)color
00103 {
00104 if ([color isEqual:_borderColor])
00105 return;
00106
00107 _borderColor = color;
00108 [self setNeedsDisplay:YES];
00109 }
00110
00111 - (float)borderWidth
00112 {
00113 return _borderWidth;
00114 }
00115
00116 - (void)setBorderWidth:(float)width
00117 {
00118 if (width === _borderWidth)
00119 return;
00120
00121 _borderWidth = width;
00122 [self setNeedsDisplay:YES];
00123 }
00124
00125 - (float)cornerRadius
00126 {
00127 return _cornerRadius;
00128 }
00129
00130 - (void)setCornerRadius:(float)radius
00131 {
00132 if (radius === _cornerRadius)
00133 return;
00134
00135 _cornerRadius = radius;
00136 [self setNeedsDisplay:YES];
00137 }
00138
00139 - (CPColor)fillColor
00140 {
00141 return _fillColor;
00142 }
00143
00144 - (void)setFillColor:(CPColor)color
00145 {
00146 if ([color isEqual:_fillColor])
00147 return;
00148
00149 _fillColor = color;
00150 [self setNeedsDisplay:YES];
00151 }
00152
00153 - (CPView)contentView
00154 {
00155 return _contentView;
00156 }
00157
00158 - (void)setContentView:(CPView)aView
00159 {
00160 if (aView === _contentView)
00161 return;
00162
00163 [aView setFrame:CGRectInset([self bounds], _contentMargin.width + _borderWidth, _contentMargin.height + _borderWidth)];
00164 [self replaceSubview:_contentView with:aView];
00165 [aView setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
00166
00167 _contentView = aView;
00168 }
00169
00170 - (CPSize)contentViewMargins
00171 {
00172 return _contentMargin;
00173 }
00174
00175 - (void)setContentViewMargins:(CPSize)size
00176 {
00177 if(size.width < 0 || size.height < 0)
00178 [CPException raise:CPGenericException reason:@"Margins must be positive"];
00179
00180 _contentMargin = CGSizeMakeCopy(size);
00181 [self setNeedsDisplay:YES];
00182 }
00183
00184 - (void)setFrameFromContentFrame:(CPRect)aRect
00185 {
00186 [self setFrame:CGRectInset(aRect, -(_contentMargin.width + _borderWidth), -(_contentMargin.height + _borderWidth))];
00187 [self setNeedsDisplay:YES];
00188 }
00189
00190 - (void)sizeToFit
00191 {
00192 var contentFrame = [_contentView frame];
00193
00194 [self setFrameSize:CGSizeMake(contentFrame.size.width + _contentMargin.width * 2,
00195 contentFrame.size.height + _contentMargin.height * 2)];
00196
00197 [_contentView setFrameOrigin:CGPointMake(_contentMargin.width, _contentMargin.height)];
00198 }
00199
00200 - (void)drawRect:(CPRect)rect
00201 {
00202 var bounds = [self bounds],
00203 aContext = [[CPGraphicsContext currentContext] graphicsPort],
00204 border2 = _borderWidth/2,
00205
00206 strokeRect = CGRectMake(bounds.origin.x + border2,
00207 bounds.origin.y + border2,
00208 bounds.size.width - _borderWidth,
00209 bounds.size.height - _borderWidth),
00210
00211 fillRect = CGRectMake(bounds.origin.x + border2,
00212 bounds.origin.y + border2,
00213 bounds.size.width - _borderWidth,
00214 bounds.size.height - _borderWidth);
00215
00216 CGContextSetFillColor(aContext, [self fillColor]);
00217 CGContextSetStrokeColor(aContext, [self borderColor]);
00218 CGContextSetLineWidth(aContext, _borderWidth);
00219
00220 switch(_borderType)
00221 {
00222 case CPLineBorder: CGContextFillRoundedRectangleInRect(aContext, fillRect, _cornerRadius, YES, YES, YES, YES);
00223 CGContextStrokeRoundedRectangleInRect(aContext, strokeRect, _cornerRadius, YES, YES, YES, YES);
00224 break;
00225
00226 case CPBezelBorder: CGContextFillRoundedRectangleInRect(aContext, fillRect, _cornerRadius, YES, YES, YES, YES);
00227 CGContextSetStrokeColor(aContext, [CPColor colorWithWhite:190.0/255.0 alpha:1.0]);
00228 CGContextBeginPath(aContext);
00229 CGContextMoveToPoint(aContext, strokeRect.origin.x, strokeRect.origin.y);
00230 CGContextAddLineToPoint(aContext, CGRectGetMinX(strokeRect), CGRectGetMaxY(strokeRect)),
00231 CGContextAddLineToPoint(aContext, CGRectGetMaxX(strokeRect), CGRectGetMaxY(strokeRect)),
00232 CGContextAddLineToPoint(aContext, CGRectGetMaxX(strokeRect), CGRectGetMinY(strokeRect)),
00233 CGContextStrokePath(aContext);
00234 CGContextSetStrokeColor(aContext, [CPColor colorWithWhite:142.0/255.0 alpha:1.0]);
00235 CGContextBeginPath(aContext);
00236 CGContextMoveToPoint(aContext, bounds.origin.x, strokeRect.origin.y);
00237 CGContextAddLineToPoint(aContext, CGRectGetMaxX(bounds), CGRectGetMinY(strokeRect));
00238 CGContextStrokePath(aContext);
00239 break;
00240
00241 default: break;
00242 }
00243 }
00244
00245 @end