Surjeet Singh
Surjeet Singh

Reputation: 11949

How to enable trash to accept drop from cocoa application?

Is their any way to enable application to drop items into trash. Right now i m drag and drop item from NSTableView to NSTableView and NSOutlineView.

I got only single question at here How to enable drop something on the Trash in objective-c? but dropping to trash is not happen. When i drag item outline app, i get0 for NSDragOperation. Thanks for help in advance.

Upvotes: 1

Views: 595

Answers (2)

Chris Walken
Chris Walken

Reputation: 408

While trying to solve a different problem - dragging custom items to the trash and using namesOfPromisedFilesDroppedAtDestination I found that even though the dragged item(s) disappear like the drag worked namesOfPromisedFilesDroppedAtDestination was never called. To get around this you can implement:

-(void)draggedImage:(NSImage *)anImage
             endedAt:(NSPoint)aPoint
           operation:(NSDragOperation)operation

and check the operation for NSDragOperationDelete

Upvotes: 0

Hussain Shabbir
Hussain Shabbir

Reputation: 15035

    For dropping outside the location on your machine use the below DataSource methods:-
    - (BOOL)tableView:(NSTableView *)tv writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
    {
        NSArray* entityArray=[yourArrayController selectedObjects];
        NSString* filename=[[[entityArray objectAtIndex:0]valueForKey:@"fileName"]stringValue];
        [pboard setPropertyList:[NSArray arrayWithObject:filename] forType:(NSString*)kPasteboardTypeFileURLPromise];
        NSPoint dragPosition;
        NSRect imageLocation;

        NSEvent *theEvent   = [NSApp currentEvent];

        dragPosition = [tv convertPoint:[theEvent locationInWindow] fromView: nil];
        dragPosition.x -= 16;
        dragPosition.y -= 16;
        imageLocation.origin = dragPosition;
        imageLocation.size = NSMakeSize(32,32);
        [tv dragPromisedFilesOfTypes:[NSArray arrayWithObject:@"*"]
                            fromRect:imageLocation
                              source:self
                           slideBack:YES
                               event:theEvent];
        return YES;
    }

    //For Enabled the Dropping and Extracting the File Path. This method will give you exact path
    - (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
    {
        NSString *str=[dropDestination path];
        NSLog(@"%@",str);
        NSMutableArray  *rootDraggedItemsNames = nil;
        return rootDraggedItemsNames;
    }

    //When Drag Files accepted to the Destination, then it Start Download the Files. This will accept the file drops on the destination
    - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
    {
        [[NSPasteboard pasteboardWithName:NSDragPboard] declareTypes:[NSArray arrayWithObject:(NSString*)kPasteboardTypeFileURLPromise] owner:self];
    }

    The below method is for registering the file promises

   -(void)setAcceptDrops:(BOOL)acceptDrops
{
    if (acceptDrops)
    {
        [tableView registerForDraggedTypes: [NSArray arrayWithObjects:(NSString*)kPasteboardTypeFileURLPromise, nil]];
        [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];

    }
    else
    {
        [tableView registerForDraggedTypes: [NSArray arrayWithObjects:nil]];
        [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
    }
}

-(void)awakeFromNib
{
    [self setAcceptDrops:YES];
}

Hope it helps:)

Upvotes: 1

Related Questions