Removing the use of private CoreGraphics APIs to draw shadows, and replacing with...
[ITKit.git] / ITImageCell.m
1 #import "ITImageCell.h"
2 #import <ApplicationServices/ApplicationServices.h>
3 #import <ITFoundation/ITFoundation.h>
4
5 @implementation ITImageCell
6
7 - (id)initImageCell:(NSImage *)image {
8         if ((self = [super initImageCell:image])) {
9                 _scalesSmoothly = YES;
10                 castsShadow = NO;
11                 shadowAzimuth = 90.0;
12                 shadowAmbient = 0.15;
13                 shadowHeight = 1.00;
14                 shadowRadius = 4.00;
15         }
16         return self;
17 }
18
19 - (id)init {
20         if ((self = [super init])) {
21                 _scalesSmoothly = YES;
22                 castsShadow = NO;
23                 shadowAzimuth = 90.0;
24                 shadowAmbient = 0.15; // In my tests, an alpha component of 0.85 perfectly duplicates the old private API's results, resulting in identical shadows. Therefore, the ambient can remain 0.15.
25                 shadowHeight = 1.00;
26                 shadowRadius = 4.00;
27         }
28         return self;
29 }
30
31 - (void)drawWithFrame:(NSRect)rect inView:(NSView *)controlView {
32         NSShadow *shadow;
33         
34         if (_scalesSmoothly || castsShadow) {
35                 [NSGraphicsContext saveGraphicsState];
36         }
37         
38         if (_scalesSmoothly) {
39                 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
40                 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
41         }
42         
43         if (castsShadow) {
44                 CGFloat height = ((2.0*tan((M_PI/360.0)*(shadowAzimuth-180.0)))*shadowHeight)/(1.0+pow(tan((M_PI/360.0)*(shadowAzimuth-180.0)),2.0));
45                 CGFloat width = sqrt(pow(shadowHeight, 2.0)-pow(height, 2.0));
46                 
47                 shadow = [[NSShadow alloc] init];
48                 [shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:(1.0 - shadowAmbient)]]; 
49                 [shadow setShadowOffset:NSMakeSize(width, height)];
50                 [shadow setShadowBlurRadius:shadowRadius];
51                 
52                 [shadow set];
53         }
54         
55         [super drawWithFrame:rect inView:controlView];
56         
57         if (_scalesSmoothly || castsShadow) {
58                 [NSGraphicsContext restoreGraphicsState];
59         }
60         
61         if (castsShadow) {
62                 [shadow release];
63         }
64 }
65
66 - (BOOL)scalesSmoothly {
67         return _scalesSmoothly;
68 }
69
70 - (void)setScalesSmoothly:(BOOL)flag {
71         _scalesSmoothly = flag;
72         [[self controlView] setNeedsDisplay:YES];
73 }
74
75 - (BOOL)castsShadow {
76         return castsShadow;
77 }
78
79 - (void)setCastsShadow:(BOOL)newSetting {
80         castsShadow = newSetting;
81         [[self controlView] setNeedsDisplay:YES];
82 }
83
84 - (float)shadowElevation {
85         return 45.0;
86 }
87
88 - (void)setShadowElevation:(float)newElevation {
89         ITDebugLog(@"setShadowElevation: on ITImageCell objects does nothing.");
90 }
91
92 - (float)shadowAzimuth {
93         return shadowAzimuth;
94 }
95
96 - (void)setShadowAzimuth:(float)newAzimuth {
97         shadowAzimuth = newAzimuth;
98         [[self controlView] setNeedsDisplay:YES];
99 }
100
101 - (float)shadowAmbient {
102         return shadowAmbient;
103 }
104
105 - (void)setShadowAmbient:(float)newAmbient {
106         shadowAmbient = newAmbient;
107         [[self controlView] setNeedsDisplay:YES];
108 }
109
110 - (float)shadowHeight {
111         return shadowHeight;
112 }
113
114 - (void)setShadowHeight:(float)newHeight {
115         shadowHeight = newHeight;
116         [[self controlView] setNeedsDisplay:YES];
117 }
118
119 - (float)shadowRadius {
120         return shadowRadius;
121 }
122
123 - (void)setShadowRadius:(float)newRadius {
124         shadowRadius = newRadius;
125         [[self controlView] setNeedsDisplay:YES];
126 }
127
128 - (float)shadowSaturation {
129         return 1.0;
130 }
131
132 - (void)setShadowSaturation:(float)newSaturation {
133         ITDebugLog(@"setShadowSaturation: on ITImageCell objects does nothing.");
134 }
135
136 @end