//
//  NJRVendingImageView.m
//  DockCam
//
//  Created by Nicholas Riley on Fri Jun 28 2002.
//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
//

#import "NJRVendingImageView.h"

@implementation NJRVendingImageView

NSCursor *openHandCursor = nil, *closedHandCursor = nil;

+ (void)initialize;
{
    openHandCursor = [[NSCursor alloc] initWithImage: [NSImage imageNamed: @"Open hand"] hotSpot: NSMakePoint(7, 7)];
    closedHandCursor = [[NSCursor alloc] initWithImage: [NSImage imageNamed: @"Closed hand"] hotSpot: NSMakePoint(7, 7)];
}

- (void)putImageOnPasteboard:(NSPasteboard *)pboard;
{
    [pboard declareTypes: [NSArray arrayWithObject: NSTIFFPboardType] owner: self];
    [pboard setData: [[self image] TIFFRepresentation] forType: NSTIFFPboardType];
}

- (void)mouseDown:(NSEvent *)event;
{
    NSPasteboard *pboard = [NSPasteboard pasteboardWithName: NSDragPboard];
    NSImage *image = [self image];
    NSRect viewRect = { NSZeroPoint, [self bounds].size };
    NSRect imageRect = { NSZeroPoint, [image size] };
    NSImage *draggingImage = [[NSImage alloc] initWithSize: viewRect.size];

    [draggingImage lockFocus];
    [image drawInRect: viewRect fromRect: imageRect operation: NSCompositeCopy fraction: 0.6];
    [draggingImage unlockFocus];

    [self putImageOnPasteboard: pboard];

    [NSApp currentEvent];

    [closedHandCursor push]; // XXX doesn't always work, bug 2976315

    [self dragImage: draggingImage at: NSZeroPoint offset: NSZeroSize event: event pasteboard: pboard source: self slideBack: YES];

    [closedHandCursor pop];
}

// XXX guess at size of resize corner
#define RESIZE_CORNER_EDGE 15

// XXX assumes that the view takes up the entire window, fine for this app but not in general
- (void)resetCursorRects;
{
    NSRect rect = [self bounds];

    rect.origin.y += RESIZE_CORNER_EDGE; 
    rect.size.height -= RESIZE_CORNER_EDGE;
    [self addCursorRect: rect cursor: openHandCursor];
    
    rect.origin.y -= RESIZE_CORNER_EDGE;
    rect.size.height = RESIZE_CORNER_EDGE;
    rect.size.width -= RESIZE_CORNER_EDGE;
    [self addCursorRect: rect cursor: openHandCursor];
}

@end
