/* Copyright (c) 2004, Noah Sorscher
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 	¥ 	Redistributions of source code must retain the above copyright
 * 	notice, this list of conditions and the following disclaimer.
 * 	¥ 	Redistributions in binary form must reproduce the above copyright
 * 	notice, this list of conditions and the following disclaimer in the
 * 	documentation and/or other materials provided with the distribution.
 * 	¥ 	Neither the name of the Noah Sorscher nor the names of its
 * 	contributors may be used to endorse or promote products derived from
 * 	this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
#import "MHScrubber.h"

@implementation MHScrubber

- (void)setDelegate:(id)newDelegate
{
    [myDelegate release];
    myDelegate = [newDelegate retain];
}

- (void)setMarkers:(NSArray *)markers
{
    [myMarkers release];
    myMarkers = [markers retain];
    [self setNeedsDisplay:YES];
}

- (id)init
{
    myDelegate = nil;
    myMarkers = nil;
    return [super init];
}

- (void)mouseMoved:(NSEvent *)theEvent
{
    NSPoint point = [theEvent locationInWindow];
    NSRect frame = [self frame];
    float value = (double)(point.x - 2 - frame.origin.x) / (double)(frame.size.width - 4);
    if ([myDelegate respondsToSelector:@selector(scrubber:trackMouse:)])
	[myDelegate scrubber:self trackMouse:value];
    [super mouseMoved:theEvent];
}

- (void)mouseDown:(NSEvent *)theEvent
{
    [[self window] makeFirstResponder:self];
    [super mouseDown:theEvent];
}

- (BOOL)acceptsFirstResponder 
{       return YES;     } 

- (BOOL)becomeFirstResponder 
{
    [[self window] setAcceptsMouseMovedEvents: YES];
    return YES;
} 

- (BOOL)resignFirstResponder 
{
    [[self window] setAcceptsMouseMovedEvents: NO];
    return YES;
}


- (void)drawRect:(NSRect)aRect
{
    //[NSBezierPath setDefaultLineWidth:0];
    //[NSBezierPath setDefaultLineJoinStyle:NSMiterLineJoinStyle];
    NSRect frame = [self frame];
    frame.origin.x = frame.origin.y = 0;
    float red = 214.0/256.0, green = 219.0/256.0, blue = 191.0/256.0;
    [[NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1] set];
    [NSBezierPath fillRect:frame];
    NSImage *bg = [NSImage imageNamed:@"bg"];
    NSRect srcRect;
    srcRect.origin.x = srcRect.origin.y = 0;
    srcRect.size = [bg size];
    frame.origin.x += 4;
    //frame.origin.y += 4;
    frame.size.width -= 8;
    frame.size.height = srcRect.size.height;
    [bg drawInRect:frame fromRect:srcRect operation:NSCompositeCopy fraction:1];
    
    NSImage *bgLeft = [NSImage imageNamed:@"bgLeft"];
    NSRect sideFrame;
    sideFrame.origin = frame.origin;
    sideFrame.size = [bgLeft size];
    srcRect.size = sideFrame.size;
    [bgLeft drawInRect:sideFrame fromRect:srcRect operation:NSCompositeCopy fraction:1];
    NSImage *bgRight = [NSImage imageNamed:@"bgRight"];
    sideFrame.origin.x = frame.origin.x + frame.size.width;
    sideFrame.size = [bgRight size];
    srcRect.size = sideFrame.size;
    [bgRight drawInRect:sideFrame fromRect:srcRect operation:NSCompositeCopy fraction:1];
    
    if (myMarkers) {
        [[NSColor grayColor] set];
        [NSBezierPath setDefaultLineWidth:0.5];
        int i, n = [myMarkers count];
        for (i=0;i<n;i++) {
            NSPoint p1, p2;
            p1.y = 0;
            p2.y = frame.size.height;
            p1.x = p2.x = frame.size.width * [[myMarkers objectAtIndex:i] doubleValue] + 4;
            [NSBezierPath strokeLineFromPoint:p1 toPoint:p2];
        }
    }
    
    NSImage *handle = [NSImage imageNamed:@"handle"];
    srcRect.size = [handle size];
    frame.origin.y += 1;
    frame.origin.x = frame.size.width * [self floatValue] + 2;
    frame.size = srcRect.size;
    [handle drawInRect:frame fromRect:srcRect operation:NSCompositeCopy fraction:1];
    /*
    red = 21.0/256.0;
    green = 22.0/256.0;
    blue = 19.0/256.0;
    [[NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1] set];
    [NSBezierPath strokeRect:frame];
    */
}


@end
