This document answers commonly asked questions about the drag and drop capabilities of the Application Kit. This includes information on HFS promise drags and special information regarding cross-application dragging from NSTableView objects.
Note: This document obsoletes Developer Technical Q&A’s 1200, 1220, and 1300. Future updates to these questions will be posted to this programming topic.
This document covers the following subjects:
HFS Promise Drag
Cross-Application Drag and Drop
The questions addressed in this section include:
“How Do I Set a Custom Drag Image When Doing an HFS Promise Drag in Cocoa?”
“How Do I Add Other Pasteboard Types to an HFS Promise Drag in Cocoa? ”
NSView’s dragPromisedFilesOfTypes:fromRect:source:slideBack:event:
method doesn't provide a way to set the drag image to be used when dragging an HFS promise file. However, this method calls another NSView method, dragImage:at:offset:event:pastboard:source:slideback:
in its implementation, which does provide a parameter for an NSImage to be used as the drag image. So, to set a custom drag image, simply override dragImage:at:offset:event:pastboard:source:slideback:
, setup your custom image, invoke super's dragImage:at:offset:event:pastboard:source:slideback:
and pass in your custom drag image.
For more information, see documentation on NSView.
NSView's dragPromisedFilesOfTypes:fromRect:source:slideBack:event:
method doesn't provide a means to add other pasteboard types to the HFS Promise data, because it doesn't expose the pasteboard that it uses. However, there is a workaround to add other pasteboard type data. dragPromisedFilesOfTypes:fromRect:source:slideBack:event:
calls NSView's dragImage:at:offset:event:pasteboard:source:slideBack:
method in its implementation. So, you can override dragImage:at:offset:event:pasteboard:source:slideBack:
, add any additional pasteboard types you need, and then invoke super's dragImage:at:offset:event:pasteboard:source:slideBack:
to allow everything to continue as before.
For more information, see documentation on NSView.
The questions addressed in this section include:
A bug in NSTableView in Mac OS X version 10.2 and later causes cross-application drags to not work without additional code from the application developer. Drag-and-Drop within an application still works correctly.
You can work around the bug by subclassing NSTableView and overriding draggingSourceOperationMaskForLocal:
to return the appropriate NSDragOperation
(typically NSDragOperationCopy
, depending upon what drag operation you want the drag-and-drop to perform). Only applications built on Mac OS X version 10.2 and later are affected; applications built on Mac OS X version 10.1.x are not affected.
In Mac OS X v10.4 you can work around this bug without subclassing. By default the NSTableView implementation of draggingSourceOperationMaskForLocal:
disallows dragging to destinations outside of the application while allowing any type of drag within the same application. You can change this behavior by sending the table view a setDraggingSourceOperationMask:forLocal:
message. Passing a value of YES
as the second parameter indicates that specified mask applies when the destination object is in the same application. Passing a value of NO
indicates that the specified mask applies when the destination object in an application outside the receiver's application. The masks are archived with the table view.
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-06-28)