/*
 * MyViewWithLayerDelegate.j
 *
 * Created by Philippe Laval on 2009/03/24.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import <AppKit/CPView.j>
@import <AppKit/CPColor.j>
@import <AppKit/CPGraphicsContext.j>

@implementation MyViewWithLayerDelegate : CPView 
{ 
	CALayer _rootLayer;
} 

- (id)initWithFrame:(CGRect)aFrame 
{ 
	self = [super initWithFrame:aFrame];
	if (self) 
	{ 
		// Tell that the view will use a layer to draw itself
		[self setWantsLayer:YES];
		// Create the layer and associate the layer with the view
		_rootLayer = [CALayer layer];
		[self setLayer:_rootLayer];
		
		// We will use the builtin behaviour of the layer 
		// (if its background color is set, it will be used to fill its bounds)
		[_rootLayer setBackgroundColor:[CPColor grayColor]];
		[_rootLayer setDelegate:self];
		
		// We need to tell the layer to draw itself
		[_rootLayer setNeedsDisplay];
	} 
	
	return self; 
} 

// This is the method for CALayer delegate
- (void)drawLayer:(CALayer)layer inContext:(CGContext)context 
{ 
	var bounds = [layer bounds]; 

	CGContextSetFillColor(context, [CPColor yellowColor]);
	CGContextSetStrokeColor(context, [CPColor greenColor]);

	CGContextFillEllipseInRect(context, bounds);
	CGContextStrokeEllipseInRect(context, bounds);
} 


@end 