#import "IconGrabView.h"

@implementation IconGrabView

-initWithFrame:(NSRect)rect
{
    [super initWithFrame:rect];
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,nil]];
    return self;
}

-(void)drawRect:(NSRect)rect
{
    NSPoint offset={ 0,0 };
    NSColor *white=[NSColor whiteColor];
    NSRectFillListWithColors( &rect, &white, 1 );
    if ( !icon && filename ) {
        icon = [[[NSWorkspace sharedWorkspace] iconForFile:filename] retain];
        if (!icon ) {
            filename=nil;
        }
    }
    if ( icon ) {
        offset.x = ([self frame].size.width - [icon size].width) / 2;
        offset.y = ([self frame].size.height - [icon size].height) / 2;
        [icon compositeToPoint:offset operation:NSCompositeSourceOver];
    }
    
}

-(BOOL)acceptsFirstResponder
{
    return YES;
}

-(unsigned int)draggingEntered:(id <NSDraggingInfo>)sender
{
    return NSDragOperationCopy;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    if ( filename ) {
        [filename release];
        filename=nil;
    }
    if ( icon ) {
        [icon release];
        icon=nil;
    }
    filename = [[[[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType] objectAtIndex:0] retain];
    [self  setNeedsDisplay:YES];
    return YES;
}

-(void)copy:sender
{
    id pasteboard = [NSPasteboard generalPasteboard];
    [pasteboard declareTypes:[NSArray arrayWithObjects:NSTIFFPboardType,nil] owner:self];
    [pasteboard setData:[icon TIFFRepresentation] forType:NSTIFFPboardType];
}

@end
