Dragging Destinations
To receive drag operations, you must register the pasteboard types that your window or view will accept by sending the object a registerForDraggedTypes:
message, defined in both NSWindow and NSView, and implement several methods from the NSDraggingDestination protocol. During a dragging session, a candidate destination receives NSDraggingDestination messages only if the destination is registered for a pasteboard type that matches the type of the pasteboard data being dragged. The destination receives these messages as an image enters, moves around inside, and then exits or is released within the destination’s boundaries.
Although NSDraggingDestination is declared as an informal protocol, the NSWindow and NSView subclasses you create to adopt the protocol need only implement those methods that are pertinent. (The NSWindow and NSView classes provide private implementations for all of the methods.) Either a window object or its delegate may implement these methods; however, the delegate’s implementation takes precedence if there are implementations in both places.
The Sender of Destination Messages
Each of the NSDraggingDestination methods sports a single argument: sender, the object that invoked the method. Within its implementations of the NSDraggingDestination methods, the destination can send NSDraggingInfo protocol messages to sender to get more information on the current dragging session, such as querying for the dragging pasteboard or the source’s operations mask. In Java, sender is an NSDragDestination object, which implements the NSDraggingInfo interface.
The Dragging Pasteboard
Although a standard dragging pasteboard (obtained using [NSPasteboard pasteboardWithName:NSDragPboard]
) is provided as a convenience in getting the pasteboard for dragging data, there is NO guarantee that this will be the pasteboard used in a cross-process drag. Thus, to guarantee getting the correct pasteboard, your code should use [sender draggingPasteboard]
.
The Order of Destination Messages
The six NSDraggingDestination methods are invoked in a distinct order:
As the image is dragged into the destination’s boundaries, the destination is sent a
draggingEntered:
message. The method should return a value that indicates which dragging operation the destination will perform.While the image remains within the destination, a series of
draggingUpdated:
messages are sent. The method should return a value that indicates which dragging operation the destination will perform.If the image is dragged out of the destination,
draggingExited:
is sent and the sequence of NSDraggingDestination messages stops. If it re-enters, the sequence begins again (with a newdraggingEntered:
message).When the image is released, it either slides back to its source (and breaks the sequence) or a
prepareForDragOperation:
message is sent to the destination, depending on the value returned by the most recent invocation ofdraggingEntered:
ordraggingUpdated:
.If the
prepareForDragOperation:
message returnedYES
, aperformDragOperation:
message is sent.Finally, if
performDragOperation:
returnedYES
,concludeDragOperation:
is sent.
Copyright © 2012 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2012-01-09