API 0.9.5
AppKit/CPShadowView.j
Go to the documentation of this file.
00001 /*
00002  * CPShadowView.j
00003  * AppKit
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 
00026 CPLightShadow   = 0;
00027 CPHeavyShadow   = 1;
00028 
00029 var CPShadowViewLightBackgroundColor    = nil,
00030     CPShadowViewHeavyBackgroundColor    = nil;
00031 
00032 var LIGHT_LEFT_INSET    = 3.0,
00033     LIGHT_RIGHT_INSET   = 3.0,
00034     LIGHT_TOP_INSET     = 3.0,
00035     LIGHT_BOTTOM_INSET  = 5.0,
00036 
00037     HEAVY_LEFT_INSET    = 7.0,
00038     HEAVY_RIGHT_INSET   = 7.0,
00039     HEAVY_TOP_INSET     = 5.0,
00040     HEAVY_BOTTOM_INSET  = 5.0;
00041 
00046 @implementation CPShadowView : CPView
00047 {
00048     CPShadowWeight  _weight;
00049 }
00050 
00051 + (void)initialize
00052 {
00053     if (self != [CPShadowView class])
00054         return;
00055 
00056     var bundle = [CPBundle bundleForClass:[self class]];
00057 
00058     CPShadowViewLightBackgroundColor = [CPColor colorWithPatternImage:[[CPNinePartImage alloc] initWithImageSlices:
00059         [
00060             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightTopLeft.png"] size:CGSizeMake(9.0, 9.0)],
00061             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightTop.png"] size:CGSizeMake(1.0, 9.0)],
00062             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightTopRight.png"] size:CGSizeMake(9.0, 9.0)],
00063 
00064             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightLeft.png"] size:CGSizeMake(9.0, 1.0)],
00065             nil,
00066             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightRight.png"] size:CGSizeMake(9.0, 1.0)],
00067 
00068             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightBottomLeft.png"] size:CGSizeMake(9.0, 9.0)],
00069             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightBottom.png"] size:CGSizeMake(1.0, 9.0)],
00070             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewLightBottomRight.png"] size:CGSizeMake(9.0, 9.0)]
00071         ]]];
00072 
00073     CPShadowViewHeavyBackgroundColor = [CPColor colorWithPatternImage:[[CPNinePartImage alloc] initWithImageSlices:
00074         [
00075             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyTopLeft.png"] size:CGSizeMake(17.0, 17.0)],
00076             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyTop.png"] size:CGSizeMake(1.0, 17.0)],
00077             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyTopRight.png"] size:CGSizeMake(17.0, 17.0)],
00078 
00079             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyLeft.png"] size:CGSizeMake(17.0, 1.0)],
00080             nil,
00081             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyRight.png"] size:CGSizeMake(17.0, 1.0)],
00082 
00083             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyBottomLeft.png"] size:CGSizeMake(17.0, 17.0)],
00084             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyBottom.png"] size:CGSizeMake(1.0, 17.0)],
00085             [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPShadowView/CPShadowViewHeavyBottomRight.png"] size:CGSizeMake(17.0, 17.0)]
00086         ]]];
00087 }
00088 
00089 + (id)shadowViewEnclosingView:(CPView)aView
00090 {
00091     return [self shadowViewEnclosingView:aView withWeight:CPLightShadow];
00092 }
00093 
00094 + (id)shadowViewEnclosingView:(CPView)aView withWeight:(CPShadowWeight)aWeight
00095 {
00096     var shadowView = [[CPShadowView alloc] initWithFrame:[aView frame]];
00097     [shadowView setWeight:aWeight];
00098 
00099     var size = [shadowView frame].size,
00100         width = size.width - [shadowView leftInset] - [shadowView rightInset],
00101         height = size.height - [shadowView topInset] - [shadowView bottomInset],
00102         enclosingView = [aView superview];
00103 
00104     [shadowView setHitTests:[aView hitTests]];
00105     [shadowView setAutoresizingMask:[aView autoresizingMask]];
00106     [aView removeFromSuperview];
00107     [shadowView addSubview:aView];
00108     [aView setFrame:CGRectMake([shadowView leftInset], [shadowView topInset], width, height)]
00109     [enclosingView addSubview:shadowView];
00110 
00111     return shadowView;
00112 }
00113 
00114 - (id)initWithFrame:(CGRect)aFrame
00115 {
00116     self = [super initWithFrame:aFrame];
00117 
00118     if (self)
00119     {
00120         _weight = CPLightShadow;
00121 
00122         [self setBackgroundColor:CPShadowViewLightBackgroundColor];
00123 
00124         [self setHitTests:NO];
00125     }
00126 
00127     return self;
00128 }
00129 
00130 - (void)setWeight:(CPShadowWeight)aWeight
00131 {
00132     if (_weight == aWeight)
00133         return;
00134 
00135     _weight = aWeight;
00136 
00137     if (_weight == CPLightShadow)
00138         [self setBackgroundColor:CPShadowViewLightBackgroundColor];
00139 
00140     else
00141         [self setBackgroundColor:CPShadowViewHeavyBackgroundColor];
00142 }
00143 
00144 - (float)leftInset
00145 {
00146     return _weight == CPLightShadow ? LIGHT_LEFT_INSET : HEAVY_LEFT_INSET;
00147 }
00148 
00149 - (float)rightInset
00150 {
00151     return _weight == CPLightShadow ? LIGHT_RIGHT_INSET : HEAVY_RIGHT_INSET;
00152 }
00153 
00154 - (float)topInset
00155 {
00156     return _weight == CPLightShadow ? LIGHT_TOP_INSET : HEAVY_TOP_INSET;
00157 }
00158 
00159 - (float)bottomInset
00160 {
00161     return _weight == CPLightShadow ? LIGHT_BOTTOM_INSET : HEAVY_BOTTOM_INSET;
00162 }
00163 
00164 - (float)horizontalInset
00165 {
00166     if (_weight == CPLightShadow)
00167         return LIGHT_LEFT_INSET + LIGHT_RIGHT_INSET;
00168 
00169     return HEAVY_LEFT_INSET + HEAVY_RIGHT_INSET;
00170 }
00171 
00172 - (float)verticalInset
00173 {
00174     if (_weight == CPLightShadow)
00175         return LIGHT_TOP_INSET + LIGHT_BOTTOM_INSET;
00176 
00177     return HEAVY_TOP_INSET + HEAVY_BOTTOM_INSET;
00178 }
00179 
00180 + (CGRect)frameForContentFrame:(CGRect)aFrame withWeight:(CPShadowWeight)aWeight
00181 {
00182     if (aWeight == CPLightShadow)
00183         return CGRectMake(_CGRectGetMinX(aFrame) - LIGHT_LEFT_INSET, _CGRectGetMinY(aFrame) - LIGHT_TOP_INSET, _CGRectGetWidth(aFrame) + LIGHT_LEFT_INSET + LIGHT_RIGHT_INSET, _CGRectGetHeight(aFrame) + LIGHT_TOP_INSET + LIGHT_BOTTOM_INSET);
00184     else
00185         return CGRectMake(_CGRectGetMinX(aFrame) - HEAVY_LEFT_INSET, _CGRectGetMinY(aFrame) - HEAVY_TOP_INSET, _CGRectGetWidth(aFrame) + HEAVY_LEFT_INSET + HEAVY_RIGHT_INSET, _CGRectGetHeight(aFrame) + HEAVY_TOP_INSET + HEAVY_BOTTOM_INSET);
00186 }
00187 
00188 - (CGRect)frameForContentFrame:(CGRect)aFrame
00189 {
00190     return [[self class] frameForContentFrame:aFrame withWeight:_weight];
00191 }
00192 
00193 - (void)setFrameForContentFrame:(CGRect)aFrame
00194 {
00195     [self setFrame:[self frameForContentFrame:aFrame]];
00196 }
00197 
00198 @end
 All Classes Files Functions Variables Defines