//
//  AUTrackingView.m
//  SDIFCreator
//
//  Created by cynthia bruyns on 4/15/05.
//  Copyright 2005 Apple Computer. All rights reserved.
//

#import "AUTrackingView.h"

@implementation AUTrackingView

- (id)initWithFrame:(NSRect)frameRect
{
   if ((self = [super initWithFrame:frameRect]) != nil) {
		attrs = [[NSMutableDictionary dictionary]retain];
		[attrs setObject: [NSFont fontWithName: @"Arial" size:12] forKey: NSFontAttributeName];
		[attrs setObject: [NSColor whiteColor] forKey: NSForegroundColorAttributeName];
	
		queryPoint= NSMakePoint(0,0);
	}
    return self;
}

- (void)dealloc
{	[queryString release];
	[attrs release];
    [super dealloc];
}    


- (void) drawCrosshairs
{
	// draw crosshairs image or bezier curve	
	
	//printf("%lf %lf point draw", queryPoint.x, queryPoint.y);
	NSSize rect = [[self superview] bounds].size;
	[[NSColor whiteColor] setStroke];

	NSBezierPath* y = [NSBezierPath bezierPath];
	[y moveToPoint: NSMakePoint(queryPoint.x, 0)];
	[y lineToPoint: NSMakePoint(queryPoint.x, rect.height)];
	[y stroke];
	
	NSBezierPath* x = [NSBezierPath bezierPath];
	[x moveToPoint: NSMakePoint(0, queryPoint.y)];
	[x lineToPoint: NSMakePoint(rect.width, queryPoint.y)];
	[x stroke];

	NSRect pointCircle;
	pointCircle.origin.x = queryPoint.x - 2.5 ;
	pointCircle.origin.y = queryPoint.y - 2.5 ;

	NSSize pointSize;
	pointSize.width = 5;
	pointSize.height = 5;		
	pointCircle.size = pointSize;		
	
	[[NSColor whiteColor] setFill];
	[[NSBezierPath bezierPathWithOvalInRect: pointCircle] fill];
}

- (NSPoint) clipToView: (NSPoint) oldPoint
{
	NSPoint newPoint = oldPoint;
	NSSize textSize = [queryString sizeWithAttributes:attrs];
	NSSize boundingRectSize = boundingRect.size;
	
	// if point + textwidth is greater than viewwidth, move back by width
	if ( (newPoint.x + textSize.width) > boundingRectSize.width)
		newPoint.x -= textSize.width;
	
	// if point + textheight is greater than viewheight, move back by height
	if ( (newPoint.y + textSize.height) > boundingRectSize.height)
		newPoint.y -= textSize.height;

	return newPoint;
}

- (void) drawText
{
	NSPoint newPt = NSMakePoint(queryPoint.x + 5, queryPoint.y + 5); 
	NSPoint finalPt = [self clipToView: newPt];	
	[queryString drawAtPoint: finalPt withAttributes: attrs];
}

- (void) drawRect:(NSRect) rect
{
	if (!shouldDraw) return;
	
	[self drawCrosshairs];
	[self drawText];
}

- (void) setBoundingRect: (NSRect) r
{
	boundingRect = r;
}

- (void) setShouldDraw: (bool) d
{
	shouldDraw = d;
}

- (void)setPoint: (NSPoint)value {
	queryPoint = value;
}

- (void)setString: (NSString *)value {
    if ( queryString != value ) {
        [queryString release];
        queryString = [value retain];
    }
}
@end
