Removing the use of private CoreGraphics APIs to draw shadows, and replacing with...
[ITKit.git] / ITTextFieldCell.m
1 #import "ITTextFieldCell.h"
2 #import <ApplicationServices/ApplicationServices.h>
3 #import <ITFoundation/ITFoundation.h>
4
5 @implementation ITTextFieldCell
6
7 - (id)initTextCell:(NSString *)string {
8         if ((self = [super initTextCell:string])) {
9                 castsShadow = NO;
10                 shadowAzimuth = 90.0;
11                 shadowAmbient = 0.0;
12                 shadowHeight = 1.00;
13                 shadowRadius = 4.00;
14         }
15         return self;
16 }
17
18 - (id)initWithCoder:(NSCoder *)coder {
19         if ((self = [super initWithCoder:coder])) {             
20                 castsShadow = NO;
21                 shadowAzimuth = 90.0;
22                 shadowAmbient = 0.0; // Unlike ITImageCell, even an alpha component of 1.0 is ligher than the old private API's results. There's not much we can do about it as we can't go "blacker" than black.
23                 shadowHeight = 1.00;
24                 shadowRadius = 4.00;
25         }
26         return self;
27 }
28
29 - (void)drawWithFrame:(NSRect)rect inView:(NSView *)controlView {
30         NSShadow *shadow;
31         
32         if (castsShadow) {
33                 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));
34                 CGFloat width = sqrt(pow(shadowHeight, 2.0)-pow(height, 2.0));
35                 
36                 shadow = [[NSShadow alloc] init];
37                 [shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:(1.0 - shadowAmbient)]];
38                 [shadow setShadowOffset:NSMakeSize(width, height)];
39                 [shadow setShadowBlurRadius:shadowRadius];
40                 
41                 [NSGraphicsContext saveGraphicsState];
42                 [shadow set];
43         }
44         
45         // Draw the string
46         [super drawWithFrame:rect inView:controlView];
47         
48         if (castsShadow) { 
49                 // Restore the old context
50                 [NSGraphicsContext restoreGraphicsState];
51                 [shadow release];
52         }
53 }
54
55 - (BOOL)castsShadow {
56         return castsShadow;
57 }
58
59 - (void)setCastsShadow:(BOOL)newSetting {
60         castsShadow = newSetting;
61         [[self controlView] setNeedsDisplay:YES];
62 }
63
64 - (float)shadowElevation {
65         return 45.0;
66 }
67
68 - (void)setShadowElevation:(float)newElevation {
69         ITDebugLog(@"setShadowElevation: on ITTextFieldCell objects does nothing.");
70 }
71
72 - (float)shadowAzimuth {
73         return shadowAzimuth;
74 }
75
76 - (void)setShadowAzimuth:(float)newAzimuth {
77         shadowAzimuth = newAzimuth;
78         [[self controlView] setNeedsDisplay:YES];
79 }
80
81 - (float)shadowAmbient {
82         return shadowAmbient;
83 }
84
85 - (void)setShadowAmbient:(float)newAmbient {
86         shadowAmbient = newAmbient;
87         [[self controlView] setNeedsDisplay:YES];
88 }
89
90 - (float)shadowHeight {
91         return shadowHeight;
92 }
93
94 - (void)setShadowHeight:(float)newHeight {
95         shadowHeight = newHeight;
96         [[self controlView] setNeedsDisplay:YES];
97 }
98
99 - (float)shadowRadius {
100         return shadowRadius;
101 }
102
103 - (void)setShadowRadius:(float)newRadius {
104         shadowRadius = newRadius;
105         [[self controlView] setNeedsDisplay:YES];
106 }
107
108 - (float)shadowSaturation {
109         return 1.0;
110 }
111
112 - (void)setShadowSaturation:(float)newSaturation {
113         ITDebugLog(@"setShadowSaturation: on ITTextFieldCell objects does nothing.");
114 }
115
116 @end