0
votes

I have NSTableView which i want to have support for drag and drop. I was successfully able to handle drop in nstableview from finder. Also able to drop from nstableview to finder but it create Text Clipping file at destination. But i only want the destination path. Also i found one post about NSFilesPromisPboardType which was also working but not as aspected. (while using NSFilesPromisePboardType method i was not able to reorder rows).

My question is that how can i enable reordering of rows as well as dragging to finder (should only get destination path).

1
Call draggingSession:sourceOperationMaskForDraggingContext: twice with both values of NSDraggingContext.Willeke
But will it return me path where it is dropped? I just want a path of dropped location for further execution on drop completion nothing else like copy,move etc. Sorry for noob question.Tulsi Ojha

1 Answers

0
votes

Finally i solved it.

#define MyPrivateTableViewDataType @"MyPrivateTableViewDataType"


[self.tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
[self.tableView registerForDraggedTypes:[NSArray arrayWithObjects:MyPrivateTableViewDataType,NSFilesPromisePboardType,NSFilenamesPboardType,nil]];



//handling drag and drop

-(BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard{

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObjects:MyPrivateTableViewDataType,NSFilenamesPboardType,nil] owner:self];


NSPoint dragPosition;
NSRect imageLocation;

NSEvent *event =[NSApp currentEvent];
dragPosition = [tv convertPoint:[event locationInWindow] fromView:nil];

dragPosition.x -=16;
dragPosition.y -=16;

imageLocation.origin=dragPosition;
imageLocation.size=NSMakeSize(32, 32);
[tv dragPromisedFilesOfTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil] fromRect:imageLocation source:self slideBack:YES event:event];

return YES;
}


-(NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination{
NSString *str=[dropDestination path];
NSLog(@"%@", str);
NSMutableArray *rootDraggedItems=nil;
return  rootDraggedItems;

}

-(NSDragOperation)tableView:(NSTableView *)tv validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation{
if ([info draggingSource] !=tv) {
    return  NSDragOperationCopy;
}
return NSDragOperationNone;
}

-(BOOL)tableView:(NSTableView *)tv acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation{

NSPasteboard *pboard = [info draggingPasteboard];

NSArray *files=[pboard propertyListForType:NSFilenamesPboardType];
NSInteger i;
for (i=0; i<[files count]; i++) {
    NSString *filePath = [files objectAtIndex:i];
    NSString *path =[filePath stringByStandardizingPath];
    NSLog(@"%@", path);
}


NSLog(@"%ld", (long)row);


return YES;
}

I know this is not perfect but working at least for now.