Minor fixes.
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 if( self.presentationLayer ) {
60 // Now wait for it to finish:
61 while( _curAnimation ) {
62 [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode//NSEventTrackingRunLoopMode
63 beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]];
70 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
72 if( anim==_curAnimation ) {
78 - (void) setStyle: (NSDictionary*)style
80 if( style != _styleDict ) {
82 [[NSNotificationCenter defaultCenter] removeObserver: self
83 name: GGBLayerStyleChangedNotification
86 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_styleChanged)
87 name: GGBLayerStyleChangedNotification
89 setObj(&_styleDict,style);
91 [super setStyle: style];
94 - (void) _styleChanged
96 // Reapply the style, so any changes in the dict will take effect.
97 [super setStyle: _styleDict];
103 [[NSNotificationCenter defaultCenter] removeObserver: self
104 name: GGBLayerStyleChangedNotification
110 - (void) setValue: (id)value ofStyleProperty: (NSString*)prop
113 id oldValue = [_styleDict objectForKey: prop];
114 if( oldValue != value ) {
116 [_styleDict setObject: value forKey: prop];
118 [_styleDict removeObjectForKey: prop];
119 [[NSNotificationCenter defaultCenter] postNotificationName: GGBLayerStyleChangedNotification
123 [self setValue: value forKey: prop];
131 #pragma mark IPHONE VERSION:
134 - (id) copyWithZone: (NSZone*)zone
136 GGBLayer *clone = [[[self class] alloc] init];
137 clone.bounds = self.bounds;
138 clone.position = self.position;
139 clone.zPosition = self.zPosition;
140 clone.anchorPoint = self.anchorPoint;
141 clone.transform = self.transform;
142 clone.hidden = self.hidden;
143 clone.doubleSided = self.doubleSided;
144 clone.sublayerTransform = self.sublayerTransform;
145 clone.masksToBounds = self.masksToBounds;
146 clone.contents = self.contents; // doesn't copy contents (shallow-copy)
147 clone.contentsRect = self.contentsRect;
148 clone.contentsGravity = self.contentsGravity;
149 clone.minificationFilter = self.minificationFilter;
150 clone.magnificationFilter = self.magnificationFilter;
151 clone.opaque = self.opaque;
152 clone.needsDisplayOnBoundsChange = self.needsDisplayOnBoundsChange;
153 clone.edgeAntialiasingMask = self.edgeAntialiasingMask;
154 clone.backgroundColor = self.backgroundColor;
155 clone.opacity = self.opacity;
156 clone.compositingFilter = self.compositingFilter;
157 clone.filters = self.filters;
158 clone.backgroundFilters = self.backgroundFilters;
159 clone.actions = self.actions;
160 clone.name = self.name;
161 clone.style = self.style;
163 clone.cornerRadius = self.cornerRadius;
164 clone.borderWidth = self.borderWidth;
165 clone.borderColor = self.borderColor;
167 for( GGBLayer *sublayer in self.sublayers ) {
168 sublayer = [sublayer copyWithZone: zone];
169 [clone addSublayer: sublayer];
175 - (CGFloat) cornerRadius {return _cornerRadius;}
176 - (CGFloat) borderWidth {return _borderWidth;}
177 - (CGColorRef) backgroundColor {return _realBGColor;}
178 - (CGColorRef) borderColor {return _borderColor;}
180 - (void) setCornerRadius: (CGFloat)r
182 if( r != _cornerRadius ) {
184 [self setNeedsDisplay];
189 - (void) setBorderWidth: (CGFloat)w
191 if( w != _borderWidth ) {
193 self.needsDisplayOnBoundsChange = (_borderWidth>0.0 && _borderColor!=NULL);
194 [self setNeedsDisplay];
199 - (void) setBackgroundColor: (CGColorRef)color
201 if( color != _realBGColor ) {
202 CGColorRelease(_realBGColor);
203 _realBGColor = CGColorRetain(color);
204 [self setNeedsDisplay];
209 - (void) setBorderColor: (CGColorRef)color
211 if( color != _borderColor ) {
212 CGColorRelease(_borderColor);
213 _borderColor = CGColorRetain(color);
214 self.needsDisplayOnBoundsChange = (_borderWidth>0.0 && _borderColor!=NULL);
215 [self setNeedsDisplay];
220 - (void)drawInContext:(CGContextRef)ctx
222 [super drawInContext: ctx];
224 CGContextSaveGState(ctx);
227 CGRect interior = CGRectInset(self.bounds, _borderWidth,_borderWidth);
228 CGContextSetFillColorWithColor(ctx, _realBGColor);
229 if( _cornerRadius <= 0.0 ) {
230 CGContextFillRect(ctx,interior);
232 CGContextBeginPath(ctx);
233 AddRoundRect(ctx,interior,_cornerRadius);
234 CGContextFillPath(ctx);
238 if( _borderWidth > 0.0 && _borderColor!=NULL ) {
239 CGRect border = CGRectInset(self.bounds, _borderWidth/2.0, _borderWidth/2.0);
240 CGContextSetStrokeColorWithColor(ctx, _borderColor);
241 CGContextSetLineWidth(ctx, _borderWidth);
243 if( _cornerRadius <= 0.0 ) {
244 CGContextStrokeRect(ctx,border);
246 CGContextBeginPath(ctx);
247 AddRoundRect(ctx,border,_cornerRadius);
248 CGContextStrokePath(ctx);
252 CGContextRestoreGState(ctx);
259 #pragma mark MAC OS VERSION:
262 - (id) copyWithZone: (NSZone*)zone
264 // NSLayer isn't copyable, but it is archivable. So create a copy by archiving to
265 // a temporary data block, then unarchiving a new layer from that block.
267 // One complication is that, due to a bug in Core Animation, CALayer can't archive
268 // a pattern-based CGColor. So as a workaround, clear the background before archiving,
269 // then restore it afterwards.
271 // Also, archiving a CALayer with an image in it leaks memory. (Filed as rdar://5786865 )
272 // As a workaround, clear the contents before archiving, then restore.
274 CGColorRef bg = CGColorRetain(self.backgroundColor);
275 self.backgroundColor = NULL;
276 id contents = [self.contents retain];
279 NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self];
281 self.backgroundColor = bg;
282 self.contents = contents;
284 GGBLayer *clone = [NSKeyedUnarchiver unarchiveObjectWithData: data];
285 clone.backgroundColor = bg;
286 clone.contents = contents;
290 return [clone retain];
302 #pragma mark UTILITIES:
305 void BeginDisableAnimations(void)
307 [CATransaction begin];
308 [CATransaction setValue:(id)kCFBooleanTrue
309 forKey:kCATransactionDisableActions];
312 void EndDisableAnimations(void)
314 [CATransaction commit];
318 void ChangeSuperlayer( CALayer *layer, CALayer *newSuperlayer, int index )
320 // Disable actions, else the layer will move to the wrong place and then back!
321 [CATransaction flush];
322 BeginDisableAnimations();
324 CGPoint pos = layer.position;
325 if( layer.superlayer )
326 pos = [newSuperlayer convertPoint: pos fromLayer: layer.superlayer];
328 [layer removeFromSuperlayer];
329 layer.position = pos;
331 [newSuperlayer insertSublayer: layer atIndex: index];
333 [newSuperlayer addSublayer: layer];
336 EndDisableAnimations();
340 void RemoveImmediately( CALayer *layer )
342 [CATransaction flush];
343 BeginDisableAnimations();
344 [layer removeFromSuperlayer];
345 EndDisableAnimations();
349 CGColorRef GetEffectiveBackground( CALayer *layer )
351 for( ; layer; layer=layer.superlayer ) {
352 CGColorRef bg = layer.backgroundColor;