Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superview as drag destination if subview rejects it

Consider a view (parent) with a subview (child). Both have registered for the dragged type NSFilenamesPboardType.

parent is only interested in image files and child in audio files. If they detect a file of the corresponding type in draggingEntered: they return NSDragOperationCopy. If not, they return NSDragOperationNone.

child overlaps parent and when a drag reaches child, parent receives a draggingExited: message, no matter if child is interested in the drag or not.

How can I make parent receive the drag if child does not want it?

like image 239
hpique Avatar asked Jun 10 '12 16:06

hpique


3 Answers

If I get this right you should call:

[self.superview yourmethod];

in the childs function.

If the child inherited from the parent you can call:

[super yourmethod];
like image 119
Thomas Avatar answered Oct 20 '22 20:10

Thomas


There are a couple of things you could do here:

  1. As @Thomas mentioned, you could implement logic in the child view to determine whether or not the drag events should be forwarded to the parent view and forward the events when appropriate.

  2. You could unregister the child view from receiving drag messages ([childView unregisterDraggedTypes]) and then you could implement all of the logic for drags in the parentView. Essentially, the idea would be to calculate the type and position of the drag and whether or not it intersects with the child view. If it does intersect with the child view, apply the drag logic for the child view. Otherwise, apply the drag logic for the parent view. You will probably need to implement draggingUpdated: to accomplish this.

like image 2
Michael Frederick Avatar answered Oct 20 '22 19:10

Michael Frederick


You can consider make the parent as the only one which register for NSFilenamesPboardType.

And like the UIResponderChain, create a category for each subviews

- (NSView*) dragTest:(id< NSDraggingInfo >)sender;

if the returned value is nil, it means that the subview (and its subivews) doesn't wish to handle the drag event.

Now back to parentview, when draggingEntered: triggered, calculate the location and find out which view it is on, call dragTest: to check if the corresponding subview wish to handle it. Let the returned non-null view to handle the event, otherwise handle it in the parent.

This method will be helpful if you have many level of subviews.

like image 1
xingzhi.sg Avatar answered Oct 20 '22 20:10

xingzhi.sg