1 #import "ITImageCell.h"
2 #import <ApplicationServices/ApplicationServices.h>
3 #import <ITFoundation/ITFoundation.h>
5 @implementation ITImageCell
7 - (id)initImageCell:(NSImage *)image {
8 if ((self = [super initImageCell:image])) {
20 if ((self = [super init])) {
21 _scalesSmoothly = YES;
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.
31 - (void)drawWithFrame:(NSRect)rect inView:(NSView *)controlView {
34 if (_scalesSmoothly || castsShadow) {
35 [NSGraphicsContext saveGraphicsState];
38 if (_scalesSmoothly) {
39 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
40 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
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));
47 shadow = [[NSShadow alloc] init];
48 [shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:(1.0 - shadowAmbient)]];
49 [shadow setShadowOffset:NSMakeSize(width, height)];
50 [shadow setShadowBlurRadius:shadowRadius];
55 [super drawWithFrame:rect inView:controlView];
57 if (_scalesSmoothly || castsShadow) {
58 [NSGraphicsContext restoreGraphicsState];
66 - (BOOL)scalesSmoothly {
67 return _scalesSmoothly;
70 - (void)setScalesSmoothly:(BOOL)flag {
71 _scalesSmoothly = flag;
72 [[self controlView] setNeedsDisplay:YES];
79 - (void)setCastsShadow:(BOOL)newSetting {
80 castsShadow = newSetting;
81 [[self controlView] setNeedsDisplay:YES];
84 - (float)shadowElevation {
88 - (void)setShadowElevation:(float)newElevation {
89 ITDebugLog(@"setShadowElevation: on ITImageCell objects does nothing.");
92 - (float)shadowAzimuth {
96 - (void)setShadowAzimuth:(float)newAzimuth {
97 shadowAzimuth = newAzimuth;
98 [[self controlView] setNeedsDisplay:YES];
101 - (float)shadowAmbient {
102 return shadowAmbient;
105 - (void)setShadowAmbient:(float)newAmbient {
106 shadowAmbient = newAmbient;
107 [[self controlView] setNeedsDisplay:YES];
110 - (float)shadowHeight {
114 - (void)setShadowHeight:(float)newHeight {
115 shadowHeight = newHeight;
116 [[self controlView] setNeedsDisplay:YES];
119 - (float)shadowRadius {
123 - (void)setShadowRadius:(float)newRadius {
124 shadowRadius = newRadius;
125 [[self controlView] setNeedsDisplay:YES];
128 - (float)shadowSaturation {
132 - (void)setShadowSaturation:(float)newSaturation {
133 ITDebugLog(@"setShadowSaturation: on ITImageCell objects does nothing.");