News Software Download Buy Now About Us Developers Contact
software TOC | PREV | NEXT



Drag & Drop

GIFfun allows you to drop in Image files or folders of images. I wanted the whole window to be "active" to a drag, so I subclassed Window with "GifWindow", and made the main Window in InterfaceBuilder be a member of this Custom Class. Here's the core code for receiving dragged items into a NSView subclass, go directly to GifWindow.m to see the full source.

@implementation GifWindow

- (void) awakeFromNib
{
    [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}

- (unsigned int) checkFilename:(NSString *)f
{
    [filename autorelease];
    filename = [f retain];

    return NSDragOperationCopy;
}

...
- (BOOL) isGifDirectoryOrFile:(NSString *)n
{
BOOL isDirectory;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL canRead = [fileManager fileExistsAtPath:n isDirectory:&isDirectory];
return (canRead && ( isDirectory || [[self delegate] isGifFile:n]));
}
...
- (unsigned int) draggingEntered:sender
{
NSPasteboard    *pboard;
NSArray        *fnames;
NSString        *fn;
int            i, n;

    last        = NSDragOperationNone;
    pboard    = [sender draggingPasteboard];

    if ([[pboard types] containsObject:NSFilenamesPboardType])
    {
        fnames = [pboard propertyListForType:NSFilenamesPboardType];
        n = [fnames count];
        for (i = 0; i < n && last == NSDragOperationNone; i++)
        {
            fn = [fnames objectAtIndex:i];
            if ([self isGifDirectoryOrFile:fn])
                last = [self checkFilename:fn];
        }
    }
    return last;
}
...
- (BOOL) performDragOperation:(id <NSDraggingInfo>)sender
{
BOOL ret = [[self delegate] tryAndLoadPath:filename];
if (ret)    [NSApp activateIgnoringOtherApps:YES];
return ret;
}


@end



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