Fixed some memory leaks, and took the address-related properties out of Player.
5 // Created by Jens Alfke on 3/7/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
10 #import "QuartzUtils.h"
14 NSString* const GGBLayerStyleChangedNotification = @"GGBLayerStyleChanged";
17 @implementation GGBLayer
20 - (NSString*) description
22 return [NSString stringWithFormat: @"%@[(%g,%g)]", self.class,self.position.x,self.position.y];
28 [self setNeedsDisplay];
29 for( CALayer *layer in self.sublayers )
30 if( [layer isKindOfClass: [GGBLayer class]] )
31 ((GGBLayer*)layer).redisplayAll;
33 [layer setNeedsDisplay];
38 - (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key
40 NSLog(@"%@[%p] addAnimation: %p forKey: %@",[self class],self,anim,key);
41 [super addAnimation: anim forKey: key];
46 - (void) animateAndBlock: (NSString*)keyPath from: (id)from to: (id)to duration: (NSTimeInterval)duration
48 //WARNING: This code works, but is a mess. I hope to find a better way to do this. --Jens 3/16/08
49 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath: keyPath];
50 anim.duration= duration;
51 anim.fromValue = from;
53 anim.isRemovedOnCompletion = YES;
55 [self addAnimation:anim forKey: @"animateAndBlock:"];
56 _curAnimation = (id)[self animationForKey: @"animateAndBlock:"];
57 [self setValue: to forKeyPath: keyPath]; // animation doesn't update the property value
59 // Now wait for it to finish:
60 while( _curAnimation ) {
61 [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode//NSEventTrackingRunLoopMode
62 beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]];
66 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
68 if( anim==_curAnimation ) {
74 - (void) setStyle: (NSDictionary*)style
76 if( style != _styleDict ) {
78 [[NSNotificationCenter defaultCenter] removeObserver: self
79 name: GGBLayerStyleChangedNotification
82 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_styleChanged)
83 name: GGBLayerStyleChangedNotification
85 setObj(&_styleDict,style);
87 [super setStyle: style];
90 - (void) _styleChanged
92 // Reapply the style, so any changes in the dict will take effect.
93 [super setStyle: _styleDict];
99 [[NSNotificationCenter defaultCenter] removeObserver: self
100 name: GGBLayerStyleChangedNotification
106 - (void) setValue: (id)value ofStyleProperty: (NSString*)prop
109 id oldValue = [_styleDict objectForKey: prop];
110 if( oldValue != value ) {
112 [_styleDict setObject: value forKey: prop];
114 [_styleDict removeObjectForKey: prop];
115 [[NSNotificationCenter defaultCenter] postNotificationName: GGBLayerStyleChangedNotification
119 [self setValue: value forKey: prop];
127 #pragma mark IPHONE VERSION:
130 - (id) copyWithZone: (NSZone*)zone
132 GGBLayer *clone = [[[self class] alloc] init];
133 clone.bounds = self.bounds;
134 clone.position = self.position;
135 clone.zPosition = self.zPosition;
136 clone.anchorPoint = self.anchorPoint;
137 clone.transform = self.transform;
138 clone.hidden = self.hidden;
139 clone.doubleSided = self.doubleSided;
140 clone.sublayerTransform = self.sublayerTransform;
141 clone.masksToBounds = self.masksToBounds;
142 clone.contents = self.contents; // doesn't copy contents (shallow-copy)
143 clone.contentsRect = self.contentsRect;
144 clone.contentsGravity = self.contentsGravity;
145 clone.minificationFilter = self.minificationFilter;
146 clone.magnificationFilter = self.magnificationFilter;
147 clone.opaque = self.opaque;
148 clone.needsDisplayOnBoundsChange = self.needsDisplayOnBoundsChange;
149 clone.edgeAntialiasingMask = self.edgeAntialiasingMask;
150 clone.backgroundColor = self.backgroundColor;
151 clone.opacity = self.opacity;
152 clone.compositingFilter = self.compositingFilter;
153 clone.filters = self.filters;
154 clone.backgroundFilters = self.backgroundFilters;
155 clone.actions = self.actions;
156 clone.name = self.name;
157 clone.style = self.style;
159 clone.cornerRadius = self.cornerRadius;
160 clone.borderWidth = self.borderWidth;
161 clone.borderColor = self.borderColor;
163 for( GGBLayer *sublayer in self.sublayers ) {
164 sublayer = [sublayer copyWithZone: zone];
165 [clone addSublayer: sublayer];
171 - (CGFloat) cornerRadius {return _cornerRadius;}
172 - (CGFloat) borderWidth {return _borderWidth;}
173 - (CGColorRef) backgroundColor {return _realBGColor;}
174 - (CGColorRef) borderColor {return _borderColor;}
176 - (void) setCornerRadius: (CGFloat)r
178 if( r != _cornerRadius ) {
180 [self setNeedsDisplay];
185 - (void) setBorderWidth: (CGFloat)w
187 if( w != _borderWidth ) {
189 self.needsDisplayOnBoundsChange = (_borderWidth>0.0 && _borderColor!=NULL);
190 [self setNeedsDisplay];
195 - (void) setBackgroundColor: (CGColorRef)color
197 if( color != _realBGColor ) {
198 CGColorRelease(_realBGColor);
199 _realBGColor = CGColorRetain(color);
200 [self setNeedsDisplay];
205 - (void) setBorderColor: (CGColorRef)color
207 if( color != _borderColor ) {
208 CGColorRelease(_borderColor);
209 _borderColor = CGColorRetain(color);
210 self.needsDisplayOnBoundsChange = (_borderWidth>0.0 && _borderColor!=NULL);
211 [self setNeedsDisplay];
216 - (void)drawInContext:(CGContextRef)ctx
218 [super drawInContext: ctx];
220 CGContextSaveGState(ctx);
223 CGRect interior = CGRectInset(self.bounds, _borderWidth,_borderWidth);
224 CGContextSetFillColorWithColor(ctx, _realBGColor);
225 if( _cornerRadius <= 0.0 ) {
226 CGContextFillRect(ctx,interior);
228 CGContextBeginPath(ctx);
229 AddRoundRect(ctx,interior,_cornerRadius);
230 CGContextFillPath(ctx);
234 if( _borderWidth > 0.0 && _borderColor!=NULL ) {
235 CGRect border = CGRectInset(self.bounds, _borderWidth/2.0, _borderWidth/2.0);
236 CGContextSetStrokeColorWithColor(ctx, _borderColor);
237 CGContextSetLineWidth(ctx, _borderWidth);
239 if( _cornerRadius <= 0.0 ) {
240 CGContextStrokeRect(ctx,border);
242 CGContextBeginPath(ctx);
243 AddRoundRect(ctx,border,_cornerRadius);
244 CGContextStrokePath(ctx);
248 CGContextRestoreGState(ctx);
255 #pragma mark MAC OS VERSION:
258 - (id) copyWithZone: (NSZone*)zone
260 // NSLayer isn't copyable, but it is archivable. So create a copy by archiving to
261 // a temporary data block, then unarchiving a new layer from that block.
263 // One complication is that, due to a bug in Core Animation, CALayer can't archive
264 // a pattern-based CGColor. So as a workaround, clear the background before archiving,
265 // then restore it afterwards.
267 // Also, archiving a CALayer with an image in it leaks memory. (Filed as rdar://5786865 )
268 // As a workaround, clear the contents before archiving, then restore.
270 CGColorRef bg = CGColorRetain(self.backgroundColor);
271 self.backgroundColor = NULL;
272 id contents = [self.contents retain];
275 NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self];
277 self.backgroundColor = bg;
278 self.contents = contents;
280 GGBLayer *clone = [NSKeyedUnarchiver unarchiveObjectWithData: data];
281 clone.backgroundColor = bg;
282 clone.contents = contents;
286 return [clone retain];
298 #pragma mark UTILITIES:
301 void BeginDisableAnimations(void)
303 [CATransaction begin];
304 [CATransaction setValue:(id)kCFBooleanTrue
305 forKey:kCATransactionDisableActions];
308 void EndDisableAnimations(void)
310 [CATransaction commit];
314 void ChangeSuperlayer( CALayer *layer, CALayer *newSuperlayer, int index )
316 // Disable actions, else the layer will move to the wrong place and then back!
317 [CATransaction flush];
318 BeginDisableAnimations();
320 CGPoint pos = layer.position;
321 if( layer.superlayer )
322 pos = [newSuperlayer convertPoint: pos fromLayer: layer.superlayer];
324 [layer removeFromSuperlayer];
325 layer.position = pos;
327 [newSuperlayer insertSublayer: layer atIndex: index];
329 [newSuperlayer addSublayer: layer];
332 EndDisableAnimations();
336 void RemoveImmediately( CALayer *layer )
338 [CATransaction flush];
339 BeginDisableAnimations();
340 [layer removeFromSuperlayer];
341 EndDisableAnimations();
345 CGColorRef GetEffectiveBackground( CALayer *layer )
347 for( ; layer; layer=layer.superlayer ) {
348 CGColorRef bg = layer.backgroundColor;