Re: Drag & Drop in an NSTableView
Re: Drag & Drop in an NSTableView
- Subject: Re: Drag & Drop in an NSTableView
- From: "Sven A. Schmidt" <email@hidden>
- Date: Fri, 9 Nov 2001 15:27:02 +0100
On Freitag, November 9, 2001, at 01:44 Uhr, Lieven Dekeyser wrote:
Hi!
How can I implement Drag & drop in a NSTableView? I'd like to be able to
drag files to it (which would result in adding the path string).
Do I have to subclass NSTableView and let it implement some protocols?
No, just make your controller the delegate for the table view and
implement the following two delegate messages. There's a bit of code
from my project in there which might help to illustrate what you have to
do.
Sven
- (BOOL)tableView: (NSTableView *)table
acceptDrop: (id <NSDraggingInfo>)info
row: (int)row
dropOperation: (NSTableViewDropOperation)operation {
NSPasteboard *pboard = [info draggingPasteboard];
NSString *type = [ pboard availableTypeFromArray: [ NSArray
arrayWithObjects: NSStringPboardType, NSFilenamesPboardType, nil
] ];
NSMutableString *cmd;
if ( ! type ) return NO;
if ( [ type isEqualToString: NSStringPboardType ] ) {
NSData *data = [ pboard dataForType: NSStringPboardType ];
cmd = [ [ NSString alloc ] initWithData: data encoding:
[ NSString defaultCStringEncoding ] ];
if ( row == -1 ) { // new line
[ self newLineWithCommand: cmd ];
} else { // modify existing line
[ [ crArray objectAtIndex: row ] setObject: cmd forKey:
@"Command" ];
[ self setDirty: YES ];
}
[ cmd release ];
}
if ( [ type isEqualToString: NSFilenamesPboardType ] ) {
NSArray *filenames = [pboard
propertyListForType:NSFilenamesPboardType];
NSEnumerator *iter = [ filenames objectEnumerator ];
NSString *fname;
while ( fname = [ iter nextObject ] )
[ self insertProgramWithString: fname ];
}
return YES;
}
- (unsigned int)tableView:(NSTableView*)tableView validateDrop:(id
<NSDraggingInfo>)info proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)operation {
NSPasteboard *pboard = [info draggingPasteboard];
NSString *type = [ pboard availableTypeFromArray: [ NSArray
arrayWithObjects: NSStringPboardType, NSFilenamesPboardType, nil
] ];
if ( type ) {
//NSLog( @"type: %s", [ type cString ] );
if ( [ type isEqualToString: NSStringPboardType ] ||
[ type isEqualToString: NSFilenamesPboardType ] ) {
return NSDragOperationGeneric;
}
}
//NSLog( @"validateDrop in row %i", row );
return NSDragOperationNone;
}