//
// DotColorView.j
//
// Created by Philippe Laval for DotView tutorial.
// Copyright 2009, Philippe Laval. All rights reserved.
//

@import <Foundation/CPObject.j>

@import <AppKit/CPView.j>
@import <AppKit/CPSlider.j>
@import <AppKit/CPTextField.j>

@implementation DotColorView : CPView
{
}

- (id)initWithFrame:(CGRect)aFrame
{
    self = [super initWithFrame:aFrame];
    if (self != nil)
    {
		var colorWell = [[CPColorWell alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(aFrame), CGRectGetHeight(aFrame) - 20)];
		[colorWell setColor:[CPColor redColor]];
		[colorWell setBordered:YES];
		[self addSubview:colorWell];

		// Associate the action sent by the colorWell to the DotColorView instance (self)
		[colorWell setTarget:self];
		[colorWell setAction:@selector(colorChangedValue:)];
    }
    
    return self;
}

// This action is called when the slider is used by the user (sender is the slider that we have created in initWithFrame:)
- (void)colorChangedValue:(id)sender
{
	// This is the trick. 
	// We ask the application (CPApp) to trigger itself (nil) the action (setColor:) with 
	// the colorWell as the sender. This will allow us to call [sender color] to get the colorWell current value (as a CPColor).
    [CPApp sendAction:@selector(setColor:) to:nil from:sender];
}

@end
