News Software Download Buy Now About Us Developers Contact
software TOC | PREV | NEXT
Drag & Drop (part 2)

GIFfun allows you to drag out the created GIF animation, using an NSControl subclass, "GifWell" - a simple class which displays the icon of the created file, and loads its path onto the pasteboard when you initiate a drag session by clicking and dragging out the file.

#import <AppKit/AppKit.h>

@interface GifWell:NSControl
{
id image;
NSString *path;
}

- (void)setFile:(NSString *)aPath;

@end

@implementation GifWell


- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {return YES;}
- (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent *)theEvent
{
    return YES;     
}

- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)flag
{
    if (flag) return NSDragOperationCopy;
    return NSDragOperationCopy|NSDragOperationGeneric|NSDragOperationLink;
}

- (BOOL)copyDataTo:pboard
{
    if (path != nil) {
        [pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
        [pboard setPropertyList:[NSArray arrayWithObject:path] forType:NSFilenamesPboardType];
        return YES;
    } else return NO;

}


- (void)setFile:(NSString *)aPath
{
    [path autorelease];
    [image release];
    if (!aPath) {
        path = image = nil;
    } else {
        path = [aPath copy];
        image = [[[NSWorkspace sharedWorkspace] iconForFile:aPath]retain];
    }
    [self display];
}


- (void)drawRect:(NSRect)rects
{
    NSDrawGrayBezel([self bounds] , [self bounds]);
    if (image != nil) {
        NSPoint p;
        NSSize sz = [image size];
        p.x = ([self bounds].size.width - sz.width)/2.;
        p.y = ([self bounds].size.height - sz.height)/2.;
        [image compositeToPoint:p operation:NSCompositeSourceOver];
    }
}


- (void)mouseDown:(NSEvent *)e
{
    NSPoint location;
    NSSize size;
    NSPasteboard *pb = [NSPasteboard pasteboardWithName:(NSString *) NSDragPboard];
            
    if ([self copyDataTo:pb]) {
        size = [image size];
        location.x = ([self bounds].size.width - size.width)/2;
        location.y = ([self bounds].size.height - size.height)/2;

        [self dragImage:image at:location offset:NSZeroSize event:(NSEvent *)e pasteboard:pb source:self slideBack:YES];
    }
}

@end
TOC | PREV | NEXT
Created by Stone Design's Create on 4/30/1998
©2000 Stone Design top