API  0.9.10
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 
42 {
43  return "shadow-view";
44 }
45 
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 
142 {
143  var currentContentInset = [self currentValueForThemeAttribute:@"content-inset"];
144 
145  return currentContentInset.left + currentContentInset.right;
146 }
147 
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 
166 {
167  [super layoutSubviews];
168 
169  [self setBackgroundColor:[self currentValueForThemeAttribute:@"bezel-color"]];
170 }
171 
172 @end
id initWithFrame:(CGRect aFrame)
Definition: CPView.j:334
void layoutSubviews()
Definition: CPView.j:2765
BOOL setThemeState:(ThemeState aState)
Definition: CPView.j:3214
An object representation of nil.
Definition: CPNull.h:2
void layoutSubviews()
Definition: CPShadowView.j:165
BOOL hitTests()
Definition: CPView.j:1814
float topInset()
Definition: CPShadowView.j:131
void setFrame:(CGRect aFrame)
Definition: CPView.j:996
int width
CPThemeStateShadowViewHeavy
Definition: CPShadowView.j:30
void removeFromSuperview()
Definition: CPView.j:654
A mutable key-value pair collection.
Definition: CPDictionary.h:2
float horizontalInset()
Definition: CPShadowView.j:141
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
float verticalInset()
Definition: CPShadowView.j:148
float bottomInset()
Definition: CPShadowView.j:136
CPDictionary themeAttributes()
Definition: CPShadowView.j:46
CPString defaultThemeClass()
Definition: CPShadowView.j:41
void setNeedsLayout()
Definition: CPView.j:2707
CPShadowWeight CPLightShadow
Definition: CPShadowView.j:26
float leftInset()
Definition: CPShadowView.j:121
void setBackgroundColor:(CPColor aColor)
Definition: CPView.j:1923
void setHitTests:(BOOL shouldHitTest)
Definition: CPView.j:1823
CPView superview()
Definition: CPView.j:486
unsigned autoresizingMask()
Definition: CPView.j:1495
CGRect frame()
Definition: CPView.j:1022
float rightInset()
Definition: CPShadowView.j:126
id shadowViewEnclosingView:withWeight:(CPView aView, [withWeight] CPShadowWeight aWeight)
Definition: CPShadowView.j:67
void setWeight:(CPShadowWeight aWeight)
Definition: CPShadowView.j:106
Definition: CPView.j:136
CPHeavyShadow
Definition: CPShadowView.j:27