Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa NSView subview blocking drag/drop

I have an NSView subclass which registers for drag files in init method like this:

    [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];

The drag drop works perfectly fine, but if I add a subview to this view with the exact same frame, it doesn't work any more. My guess is that the subview is block the drag event to go to super view. Can can I avoid that? Thanks

Also, I know I am asking two questions, but I don't want to create a new topic just for this: When I am dragging, my cursor doesn't change to the "+" sign like with other drags, how can I do that? Thanks again.

UPDATE: Here's the how I have it set up in my IB: enter image description here

The DrawView is the custom class I was talking about that registered for draggedtypes. And the Image view simply is a subview, I dragged an image from the media section... If it helps, here's my relevant code for DragView:

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
    return NSDragOperationCopy;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
    NSPasteboard *pboard;
    pboard = [sender draggingPasteboard];
    NSArray *list = [pboard propertyListForType:NSFilenamesPboardType];
    if ([list count] == 1) {
        BOOL isDirectory = NO;
        NSString *fileName = [list objectAtIndex:0];
        [[NSFileManager defaultManager] fileExistsAtPath:fileName
                                             isDirectory: &isDirectory];
        if (isDirectory) {
            NSLog(@"AHH YEA");
        } else {
            NSLog(@"NOO");
        }
    }
    return YES;
}
like image 685
user635064 Avatar asked May 05 '11 03:05

user635064


1 Answers

The answer to the second part of your question is this:

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
    return NSDragOperationCopy;
}

if you return NSDragOperationCopy, you get the mouse badge for a copy operation. (You can and should, of course, not just return NSDragOperationCopy unconditionally; check the objects on the pasteboard to see if you can accept them.)

I'm not sure what the answer to the first part of your question is, because I'm unable to recreate the subview blocking effect.

Okay, the answer is unfortunately that you can't. The image you dragged is contained in an NSImageView, and NSImageViews accept drag events themselves, so it's grabbing the drag event and not doing anything with it. If your subview was a custom class, you could either a) not implement drag and drop, in which case the drags would be passed through; b) implement drag and drop to accept drags for the subview. In this case, you're using a class over which you don't have any control. If all you want it to do is display an image, you should be able to make another NSView subclass that does nothing but draw the image in drawRect:

like image 175
jscs Avatar answered Sep 18 '22 14:09

jscs