API  1.0.0
CPShadowView.j
Go to the documentation of this file.
1 /*
2  * CPShadowView.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 @typedef CPShadowWeight
28 
29 CPThemeStateShadowViewLight = CPThemeState("shadowview-style-light");
30 CPThemeStateShadowViewHeavy = CPThemeState("shadowview-style-heavy");
31 
32 
36 @implementation CPShadowView : CPView
37 {
38  CPShadowWeight _weight;
39 }
40 
41 + (CPString)defaultThemeClass
42 {
43  return "shadow-view";
44 }
45 
46 + (CPDictionary)themeAttributes
47 {
48  return @{
49  @"bezel-color": [CPNull null],
50  @"content-inset": CGInsetMakeZero()
51  };
52 }
53 
54 + (CGRect)frameForContentFrame:(CGRect)aFrame withWeight:(CPShadowWeight)aWeight
55 {
56  var shadowView = [CPShadowView new],
57  inset = [shadowView valueForThemeAttribute:@"content-inset" inState:(aWeight == CPLightShadow) ? CPThemeStateShadowViewLight : CPThemeStateShadowViewHeavy];
58 
59  return CGRectMake(CGRectGetMinX(aFrame) - inset.left, CGRectGetMinY(aFrame) - inset.top, CGRectGetWidth(aFrame) + inset.left + inset.right, CGRectGetHeight(aFrame) + inset.top + inset.bottom);
60 }
61 
62 + (id)shadowViewEnclosingView:(CPView)aView
63 {
64  return [self shadowViewEnclosingView:aView withWeight:CPLightShadow];
65 }
66 
67 + (id)shadowViewEnclosingView:(CPView)aView withWeight:(CPShadowWeight)aWeight
68 {
69  var shadowView = [[self alloc] initWithFrame:[aView frame]];
70 
71  if (shadowView)
72  {
73  [shadowView setWeight:aWeight];
74 
75  var size = [shadowView frame].size,
76  inset = [shadowView currentValueForThemeAttribute:@"content-inset"],
77  width = size.width - inset.left - inset.right,
78  height = size.height - inset.top - inset.bottom,
79  enclosingView = [aView superview];
80 
81  [shadowView setHitTests:[aView hitTests]];
82  [shadowView setAutoresizingMask:[aView autoresizingMask]];
83  [aView removeFromSuperview];
84  [shadowView addSubview:aView];
85  [aView setFrame:CGRectMake(inset.left, inset.top, width, height)];
86  [enclosingView addSubview:shadowView];
87  }
88 
89  return shadowView;
90 }
91 
92 - (id)initWithFrame:(CGRect)aFrame
93 {
94  self = [super initWithFrame:aFrame];
95 
96  if (self)
97  {
98  [self setWeight:CPLightShadow];
99 
100  [self setHitTests:NO];
101  }
102 
103  return self;
104 }
105 
106 - (void)setWeight:(CPShadowWeight)aWeight
107 {
108  if (_weight == aWeight)
109  return;
110 
111  _weight = aWeight;
112 
113  if (_weight == CPLightShadow)
114  [self setThemeState:CPThemeStateShadowViewLight];
115  else
116  [self setThemeState:CPThemeStateShadowViewHeavy];
117 
118  [self setNeedsLayout];
119 }
120 
121 - (float)leftInset
122 {
123  return [self currentValueForThemeAttribute:@"content-inset"].left;
124 }
125 
126 - (float)rightInset
127 {
128  return [self currentValueForThemeAttribute:@"content-inset"].right;
129 }
130 
131 - (float)topInset
132 {
133  return [self currentValueForThemeAttribute:@"content-inset"].top;
134 }
135 
136 - (float)bottomInset
137 {
138  return [self currentValueForThemeAttribute:@"content-inset"].bottom;
139 }
140 
141 - (float)horizontalInset
142 {
143  var currentContentInset = [self currentValueForThemeAttribute:@"content-inset"];
144 
145  return currentContentInset.left + currentContentInset.right;
146 }
147 
148 - (float)verticalInset
149 {
150  var currentContentInset = [self currentValueForThemeAttribute:@"content-inset"];
151 
152  return currentContentInset.top + currentContentInset.bottom;
153 }
154 
155 - (CGRect)frameForContentFrame:(CGRect)aFrame
156 {
157  return [[self class] frameForContentFrame:aFrame withWeight:_weight];
158 }
159 
160 - (void)setFrameForContentFrame:(CGRect)aFrame
161 {
162  [self setFrame:[self frameForContentFrame:aFrame]];
163 }
164 
165 - (void)layoutSubviews
166 {
167  [super layoutSubviews];
168 
169  [self setBackgroundColor:[self currentValueForThemeAttribute:@"bezel-color"]];
170 }
171 
172 @end
id initWithFrame:(CGRect aFrame)
Definition: CPView.j:351
void layoutSubviews()
Definition: CPView.j:2806
BOOL setThemeState:(ThemeState aState)
Definition: CPView.j:3255
An object representation of nil.
Definition: CPNull.h:2
BOOL hitTests()
Definition: CPView.j:1838
void setFrame:(CGRect aFrame)
Definition: CPView.j:1020
int width
CPThemeStateShadowViewHeavy
Definition: CPShadowView.j:30
void removeFromSuperview()
Definition: CPView.j:678
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPThemeStateShadowViewLight
Definition: CPShadowView.j:29
An immutable string (collection of characters).
Definition: CPString.h:2
CPNull null()
Definition: CPNull.j:51
CGRect frameForContentFrame:(CGRect aFrame)
Definition: CPShadowView.j:155
void setNeedsLayout()
Definition: CPView.j:2748
CPShadowWeight CPLightShadow
Definition: CPShadowView.j:26
void setBackgroundColor:(CPColor aColor)
Definition: CPView.j:1947
void setHitTests:(BOOL shouldHitTest)
Definition: CPView.j:1847
CPView superview()
Definition: CPView.j:510
unsigned autoresizingMask()
Definition: CPView.j:1519
CGRect frame()
Definition: CPView.j:1046
id shadowViewEnclosingView:withWeight:(CPView aView, [withWeight] CPShadowWeight aWeight)
Definition: CPShadowView.j:67
void setWeight:(CPShadowWeight aWeight)
Definition: CPShadowView.j:106
Definition: CPView.j:137
CPHeavyShadow
Definition: CPShadowView.j:27