//
//  AUWaveformView.m
//  
//
//  Created by Cynthia Bruyns on 5/30/05.
//  Copyright 2005 Apple Computer. All rights reserved.
//

#import "AUWaveformView.h"
#import "WaveformViewDemo.h" // for the overview definition,  can be duplicated here for reuse

#define NSRectToCGRect(r) CGRectMake(r.origin.x, r.origin.y, r.size.width, r.size.height)

@implementation AUWaveformView

#pragma mark ___Initialization___

- (void) Initialize
{
	backgroundColor = [NSColor colorWithCalibratedRed: 95.0/255.0 green: 95.0/255.0 blue: 95.0/255.0 alpha:1.0];
	lineColor = [NSColor colorWithCalibratedRed: 255.0/255.0 green: 241.0/255.0 blue: 117/255.0 alpha:1.0];
	
	[myBackgroundWell setColor:backgroundColor];		
	[myLineWell setColor: lineColor];
	
	if (waveformPoints) 
		free(waveformPoints);	
		
	waveformPoints = (CGPoint*) malloc(2*kMaxLatency*sizeof(CGPoint)); 	 // cbnote only need width of view

}

- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
		
   }
    return self;
}

- (void)awakeFromNib 
{	
	waveformPoints = NULL;
	[self Initialize];
}

- (void) dealloc
{
	[myBackgroundWell release];
	[myLineWell release];
	
	[backgroundColor release];
	[lineColor release];
	
	[myHoldButton release];

	if (waveformPoints) {
		free(waveformPoints);
		waveformPoints = NULL;
	}
	
	[super dealloc];
}

#pragma mark ___IBActions___

- (IBAction) changeBackgroundColor: (id) sender
{
	backgroundColor = [sender color];
	[self setNeedsDisplay: YES];
}

- (IBAction) changeLineColor: (id) sender
{
	lineColor = [sender color];
	[self setNeedsDisplay: YES];
}

#pragma mark ___Drawing___

- (NSRect) getRect
{
	return [self frame];
}

- (void) plotNum: (UInt32) num dataPoints:(Float32*) data
{
	if ([myHoldButton state] == NSOnState) return;

    CGRect r = NSRectToCGRect([self getRect]);	
	CGSize size = r.size;		
	Float32 width	= size.width;
	Float32 height	= size.height;

	if (num < 1) return;
		
	Float32 dy = height / 2. / 1.5;
	UInt32 pixelWidth = (UInt32) width;	
	UInt32 stride = num / pixelWidth;
	
	UInt32 mid = (UInt32) height / 2;

	// make the first point	
	waveformPointsCount = -1;
	Float32 value = mid + data[0] * dy;
	waveformPoints[++waveformPointsCount].x = 0;
    waveformPoints[waveformPointsCount].y = value;
 
	UInt32 sampleIndex;
	for (UInt32 i= 1; i< pixelWidth; i++){
		
		sampleIndex = i*stride;		
		value = mid + data[sampleIndex] * dy;
		
		waveformPoints[++waveformPointsCount].x	= i;
		waveformPoints[waveformPointsCount].y		= value;
		
		waveformPoints[++waveformPointsCount].x = i;
		waveformPoints[waveformPointsCount].y =  value;
		
		//printf("point[%ld]: %lf %lf\n", i, waveformPoints[waveformPointsCount].x, waveformPoints[waveformPointsCount].y);		
	 }
	 
	 [self setNeedsDisplay: YES];
}


- (void) drawRect: (NSRect) rect
{

	NSGraphicsContext *nsctx = [NSGraphicsContext currentContext];
    CGContextRef context = (CGContextRef)[nsctx graphicsPort];
    CGRect r = NSRectToCGRect([self getRect]);

	CGContextSetRGBFillColor(context, 
	[backgroundColor redComponent], [backgroundColor greenComponent], [backgroundColor blueComponent], 
	[backgroundColor alphaComponent]);
    CGContextFillRect(context, r);

    CGContextSetRGBStrokeColor(context,[lineColor redComponent], [lineColor greenComponent], [lineColor blueComponent], 
	[lineColor alphaComponent]);
    CGContextStrokeLineSegments(context, waveformPoints, waveformPointsCount);

    CGContextFlush(context);

}

@end
