//
// DotResizeView.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 DotResizeView : CPView
{
}

- (id)initWithFrame:(CGRect)aFrame
{
    self = [super initWithFrame:aFrame];
    if (self != nil)
    {
		// Create a slider centered vertically and 16 pixels height.
		var slider = [[CPSlider alloc] initWithFrame:CGRectMake(38, CGRectGetHeight(aFrame)/2.0 - 8, CGRectGetWidth(aFrame) - 80, 16)];
	
		[slider setMinValue:1.0];
		[slider setMaxValue:400.0];
		// Associate the action sent by the slider to the PhotoResizeView instance (self)
		[slider setTarget:self];
		[slider setAction:@selector(sliderChangedValue:)];
		
		[self addSubview:slider];
	
		[slider setDoubleValue:10.0];
																 
		// Create a label to show the mimimum value offered by the slider.
		var label = [[CPTextField alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(aFrame)/2.0 - 12, 40, 16)];
		
		[label setAlignment:CPCenterTextAlignment];
		[label setFont:[CPFont systemFontOfSize:12.0]];
		[label setStringValue:"1"];
		
		[self addSubview:label];
	
		// Create a label to show the maximum value offered by the slider.
		label = [[CPTextField alloc] initWithFrame:CGRectMake(CGRectGetWidth(aFrame) - 40, CGRectGetHeight(aFrame)/2.0 - 12, 40, 16)];
		
		[label setAlignment:CPCenterTextAlignment];
		[label setFont:[CPFont systemFontOfSize:12.0]];
		[label setStringValue:"400"];
		
		[self addSubview:label];
    }
    
    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)sliderChangedValue:(id)sender
{
	// This is the trick. 
	// We ask the application (CPApp) to trigger itself (nil) the action (setRadius:) with 
	// the slider as the sender. This will allow us to call [sender value] to get the slider current value (as a double).
    [CPApp sendAction:@selector(setRadius:) to:nil from:sender];
}

@end
