4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A//#define DND_DEBUG TRUE
4632N/A
4632N/A#import "java_awt_dnd_DnDConstants.h"
4632N/A
4632N/A#import <Cocoa/Cocoa.h>
4632N/A#import <JavaNativeFoundation/JavaNativeFoundation.h>
4632N/A
4632N/A#import "AWTEvent.h"
4632N/A#import "AWTView.h"
4632N/A#import "CDataTransferer.h"
4632N/A#import "CDropTarget.h"
4632N/A#import "CDragSource.h"
4632N/A#import "DnDUtilities.h"
4632N/A#import "ThreadUtilities.h"
4632N/A
4632N/A
4632N/A// When sIsJavaDragging is true Java drag gesture has been recognized and a drag is/has been initialized.
4632N/A// We must stop posting MouseEvent.MOUSE_DRAGGED events for the duration of the drag or all hell will break
4632N/A// loose in shared code - tracking state going haywire.
4632N/Astatic BOOL sIsJavaDragging;
4632N/A
4632N/A
4632N/A@interface NSEvent(AWTAdditions)
4632N/A
4632N/A+ (void)javaDraggingBegin;
4632N/A+ (void)javaDraggingEnd;
4632N/A
4632N/A@end
4632N/A
4632N/A
4632N/A@implementation NSEvent(AWTAdditions)
4632N/A
4632N/A
4632N/A+ (void)javaDraggingBegin
4632N/A{
4632N/A sIsJavaDragging = YES;
4632N/A}
4632N/A
4632N/A+ (void)javaDraggingEnd
4632N/A{
4632N/A // sIsJavaDragging is reset on mouseDown as well.
4632N/A sIsJavaDragging = NO;
4632N/A}
4632N/A@end
4632N/A
4632N/AJNF_CLASS_CACHE(DataTransfererClass, "sun/awt/datatransfer/DataTransferer");
4632N/AJNF_CLASS_CACHE(CDragSourceContextPeerClass, "sun/lwawt/macosx/CDragSourceContextPeer");
5037N/AJNF_CLASS_CACHE(CImageClass, "sun/lwawt/macosx/CImage");
4632N/A
4632N/Astatic NSDragOperation sDragOperation;
4632N/Astatic NSPoint sDraggingLocation;
4632N/A
4632N/Astatic CDragSource* sCurrentDragSource;
4632N/Astatic BOOL sNeedsEnter;
4632N/A
4632N/A@implementation CDragSource
4632N/A
4632N/A+ (CDragSource *) currentDragSource {
4632N/A return sCurrentDragSource;
4632N/A}
4632N/A
4632N/A- (id)init:(jobject)jdragsourcecontextpeer component:(jobject)jcomponent peer:(jobject)jpeer control:(id)control
4632N/A transferable:(jobject)jtransferable triggerEvent:(jobject)jtrigger
4632N/A dragPosX:(jint)dragPosX dragPosY:(jint)dragPosY modifiers:(jint)extModifiers clickCount:(jint)clickCount
4632N/A timeStamp:(jlong)timeStamp cursor:(jobject)jcursor
5037N/A dragImage:(jobject)jnsdragimage dragImageOffsetX:(jint)jdragimageoffsetx dragImageOffsetY:(jint)jdragimageoffsety
4632N/A sourceActions:(jint)jsourceactions formats:(jlongArray)jformats formatMap:(jobject)jformatmap
4632N/A{
4632N/A self = [super init];
4632N/A DLog2(@"[CDragSource init]: %@\n", self);
4632N/A
4632N/A fView = nil;
4632N/A fComponent = nil;
4632N/A
4632N/A // Construct the object if we have a valid model for it:
4632N/A if (control != nil) {
4632N/A JNIEnv *env = [ThreadUtilities getJNIEnv];
4632N/A fComponent = JNFNewGlobalRef(env, jcomponent);
4632N/A fComponentPeer = JNFNewGlobalRef(env, jpeer);
4632N/A fDragSourceContextPeer = JNFNewGlobalRef(env, jdragsourcecontextpeer);
4632N/A
4632N/A fTransferable = JNFNewGlobalRef(env, jtransferable);
4632N/A fTriggerEvent = JNFNewGlobalRef(env, jtrigger);
4632N/A fCursor = JNFNewGlobalRef(env, jcursor);
4632N/A
5037N/A if (jnsdragimage) {
5037N/A JNF_MEMBER_CACHE(nsImagePtr, CImageClass, "ptr", "J");
5037N/A jlong imgPtr = JNFGetLongField(env, jnsdragimage, nsImagePtr);
5037N/A fDragImage = (NSImage*) jlong_to_ptr(imgPtr); // Double-casting prevents compiler 'd$|//
5037N/A
5037N/A [fDragImage retain];
5037N/A }
5037N/A
4632N/A fDragImageOffset = NSMakePoint(jdragimageoffsetx, jdragimageoffsety);
4632N/A
4632N/A fSourceActions = jsourceactions;
4632N/A fFormats = JNFNewGlobalRef(env, jformats);
4632N/A fFormatMap = JNFNewGlobalRef(env, jformatmap);
4632N/A
4632N/A fTriggerEventTimeStamp = timeStamp;
4632N/A fDragPos = NSMakePoint(dragPosX, dragPosY);
4632N/A fClickCount = clickCount;
4632N/A fModifiers = extModifiers;
4632N/A
4632N/A // Set this object as a dragging source:
4632N/A
4632N/A AWTView *awtView = [((NSWindow *) control) contentView];
4632N/A fView = [awtView retain];
4632N/A [awtView setDragSource:self];
4632N/A
4632N/A // Let AWTEvent know Java drag is getting underway:
4632N/A [NSEvent javaDraggingBegin];
4632N/A }
4632N/A
4632N/A else {
4632N/A [self release];
4632N/A self = nil;
4632N/A }
4632N/A
4632N/A return self;
4632N/A}
4632N/A
4632N/A- (void)removeFromView:(JNIEnv *)env
4632N/A{
4632N/A DLog2(@"[CDragSource removeFromView]: %@\n", self);
4632N/A
4632N/A // Remove this dragging source from the view:
4632N/A [((AWTView *) fView) setDragSource:nil];
4632N/A
4632N/A // Clean up JNI refs
4632N/A if (fComponent != NULL) {
4632N/A JNFDeleteGlobalRef(env, fComponent);
4632N/A fComponent = NULL;
4632N/A }
4632N/A
4632N/A if (fComponentPeer != NULL) {
4632N/A JNFDeleteGlobalRef(env, fComponentPeer);
4632N/A fComponentPeer = NULL;
4632N/A }
4632N/A
4632N/A if (fDragSourceContextPeer != NULL) {
4632N/A JNFDeleteGlobalRef(env, fDragSourceContextPeer);
4632N/A fDragSourceContextPeer = NULL;
4632N/A }
4632N/A
4632N/A if (fTransferable != NULL) {
4632N/A JNFDeleteGlobalRef(env, fTransferable);
4632N/A fTransferable = NULL;
4632N/A }
4632N/A
4632N/A if (fTriggerEvent != NULL) {
4632N/A JNFDeleteGlobalRef(env, fTriggerEvent);
4632N/A fTriggerEvent = NULL;
4632N/A }
4632N/A
4632N/A if (fCursor != NULL) {
4632N/A JNFDeleteGlobalRef(env, fCursor);
4632N/A fCursor = NULL;
4632N/A }
4632N/A
4632N/A if (fFormats != NULL) {
4632N/A JNFDeleteGlobalRef(env, fFormats);
4632N/A fFormats = NULL;
4632N/A }
4632N/A
4632N/A if (fFormatMap != NULL) {
4632N/A JNFDeleteGlobalRef(env, fFormatMap);
4632N/A fFormatMap = NULL;
4632N/A }
4632N/A
4632N/A CFRelease(self); // GC
4632N/A}
4632N/A
4632N/A- (void)dealloc
4632N/A{
4632N/A DLog2(@"[CDragSource dealloc]: %@\n", self);
4632N/A
4632N/A // Delete or release local data:
4632N/A [fView release];
4632N/A fView = nil;
4632N/A
4632N/A [fDragImage release];
4632N/A fDragImage = nil;
4632N/A
4632N/A [super dealloc];
4632N/A}
4632N/A//- (void)finalize { [super finalize]; }
4632N/A
4632N/A
4632N/A// Appropriated from Windows' awt_DataTransferer.cpp:
4632N/A//
4632N/A// * NOTE: This returns a JNI Local Ref. Any code that calls must call DeleteLocalRef with the return value.
4632N/A//
4632N/A- (jobject)dataTransferer:(JNIEnv*)env
4632N/A{
4632N/A JNF_STATIC_MEMBER_CACHE(getInstanceMethod, DataTransfererClass, "getInstance", "()Lsun/awt/datatransfer/DataTransferer;");
4632N/A return JNFCallStaticObjectMethod(env, getInstanceMethod);
4632N/A}
4632N/A
4632N/A// Appropriated from Windows' awt_DataTransferer.cpp:
4632N/A//
4632N/A// * NOTE: This returns a JNI Local Ref. Any code that calls must call DeleteLocalRef with the return value.
4632N/A//
4632N/A- (jbyteArray)convertData:(jlong)format
4632N/A{
4632N/A JNIEnv* env = [ThreadUtilities getJNIEnv];
4632N/A jobject transferer = [self dataTransferer:env];
4632N/A jbyteArray data = nil;
4632N/A
4632N/A if (transferer != NULL) {
4632N/A JNF_MEMBER_CACHE(convertDataMethod, DataTransfererClass, "convertData", "(Ljava/lang/Object;Ljava/awt/datatransfer/Transferable;JLjava/util/Map;Z)[B");
4632N/A data = JNFCallObjectMethod(env, transferer, convertDataMethod, fComponent, fTransferable, format, fFormatMap, (jboolean) TRUE);
4632N/A }
4632N/A
4632N/A return data;
4632N/A}
4632N/A
4632N/A
4632N/A// Encodes a byte array of zero-terminated filenames into an NSArray of NSStrings representing them.
4632N/A// Borrowed and adapted from awt_DataTransferer.c, convertFileType().
4632N/A- (id)getFileList:(jbyte *)jbytes dataLength:(jsize)jbytesLength
4632N/A{
4632N/A jsize strings = 0;
4632N/A jsize i;
4632N/A
4632N/A // Get number of filenames while making sure to skip over empty strings.
4632N/A for (i = 1; i < jbytesLength; i++) {
4632N/A if (jbytes[i] == '\0' && jbytes[i - 1] != '\0')
4632N/A strings++;
4632N/A }
4632N/A
4632N/A // Create the file list to return:
4632N/A NSMutableArray* fileList = [NSMutableArray arrayWithCapacity:strings];
4632N/A
4632N/A for (i = 0; i < jbytesLength; i++) {
4632N/A char* start = (char *) &jbytes[i];
4632N/A
4632N/A // Skip over empty strings:
4632N/A if (start[0] == '\0') {
4632N/A continue;
4632N/A }
4632N/A
4632N/A // Update the position marker:
4632N/A i += strlen(start);
4632N/A
4632N/A // Add this filename to the file list:
4632N/A NSMutableString* fileName = [NSMutableString stringWithUTF8String:start];
4632N/A // Decompose the filename
4632N/A CFStringNormalize((CFMutableStringRef)fileName, kCFStringNormalizationFormD);
4632N/A [fileList addObject:fileName];
4632N/A }
4632N/A
4632N/A // 03-01-09 Note: keep this around for debugging.
4632N/A // return [NSArray arrayWithObjects:@"/tmp/foo1", @"/tmp/foo2", nil];
4632N/A
4632N/A return ([fileList count] > 0 ? fileList : nil);
4632N/A}
4632N/A
4632N/A
4632N/A// Set up the pasteboard for dragging:
4632N/A- (BOOL)declareTypesToPasteboard:(NSPasteboard *)pb withEnv:(JNIEnv *) env {
4632N/A // 9-20-02 Note: leave this here for debugging:
4632N/A //[pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: self];
4632N/A //return TRUE;
4632N/A
4632N/A // Get byte array elements:
4632N/A jboolean isCopy;
4632N/A jlong* jformats = (*env)->GetLongArrayElements(env, fFormats, &isCopy);
4632N/A if (jformats == nil)
4632N/A return FALSE;
4632N/A
4632N/A // Allocate storage arrays for dragging types to register with the pasteboard:
4632N/A jsize formatsLength = (*env)->GetArrayLength(env, fFormats);
4632N/A NSMutableArray* pbTypes = [[NSMutableArray alloc] initWithCapacity:formatsLength];
4632N/A
4632N/A // And assume there are no NS-type data: [Radar 3065621]
4632N/A // This is to be able to drop transferables containing only a serialized object flavor, e.g.:
4632N/A // "JAVA_DATAFLAVOR:application/x-java-serialized-object; class=java.awt.Label".
4632N/A BOOL hasNSTypeData = false;
4632N/A
4632N/A // Collect all supported types in a pasteboard format into the storage arrays:
4632N/A jsize i;
4632N/A for (i = 0; i < formatsLength; i++) {
4632N/A jlong jformat = jformats[i];
4632N/A
4632N/A if (jformat >= 0) {
4632N/A NSString* type = formatForIndex(jformat);
4632N/A
4632N/A // Add element type to the storage array.
4632N/A if (type != nil) {
4632N/A if ([type hasPrefix:@"JAVA_DATAFLAVOR:application/x-java-jvm-local-objectref;"] == false) {
4632N/A [pbTypes addObject:type];
4632N/A
4632N/A // This is a good approximation if not perfect. A conclusive search would
4632N/A // have to be done matching all defined strings in AppKit's commonStrings.h.
4632N/A if ([type hasPrefix:@"NS"] || [type hasPrefix:@"NeXT"])
4632N/A hasNSTypeData = true;
4632N/A }
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A // 1-16-03 Note: [Radar 3065621]
4632N/A // When TransferHandler is used with Swing components it puts only a type like this on the pasteboard:
4632N/A // "JAVA_DATAFLAVOR:application/x-java-jvm-local-objectref; class=java.lang.String"
4632N/A // And there's similar type for serialized object only transferables.
4632N/A // Since our drop targets aren't trained for arbitrary data types yet we need to fake an empty string
4632N/A // which will cause drop target handlers to fire.
4632N/A // KCH - 3550405 If the drag is between Swing components, formatsLength == 0, so expand the check.
4632N/A // Also, use a custom format rather than NSString, since that will prevent random views from accepting the drag
4632N/A if (hasNSTypeData == false && formatsLength >= 0) {
4632N/A [pbTypes addObject:[DnDUtilities javaPboardType]];
4632N/A }
4632N/A
4632N/A (*env)->ReleaseLongArrayElements(env, fFormats, jformats, JNI_ABORT);
4632N/A
4632N/A // Declare pasteboard types. If the types array is empty we still want to declare them
4632N/A // as otherwise an old set of types/data would remain on the pasteboard.
4632N/A NSUInteger typesCount = [pbTypes count];
4632N/A [pb declareTypes:pbTypes owner: self];
4632N/A
4632N/A // KCH - Lame conversion bug between Cocoa and Carbon drag types
4632N/A // If I provide the filenames _right now_, NSFilenamesPboardType is properly converted to CoreDrag flavors
4632N/A // If I try to wait until pasteboard:provideDataForType:, the conversion won't happen
4632N/A // and pasteboard:provideDataForType: won't even get called! (unless I go over a Cocoa app)
4632N/A if ([pbTypes containsObject:NSFilenamesPboardType]) {
4632N/A [self pasteboard:pb provideDataForType:NSFilenamesPboardType];
4632N/A }
4632N/A
4632N/A [pbTypes release];
4632N/A
4632N/A return typesCount > 0 ? TRUE : FALSE;
4632N/A}
4632N/A
4632N/A// This is an NSPasteboard callback. In declareTypesToPasteboard:withEnv:, we only declared the types
4632N/A// When the AppKit DnD system actually needs the data, this method will be invoked.
4632N/A// Note that if the transfer is handled entirely from Swing (as in a local-vm drag), this method may never be called.
4632N/A- (void)pasteboard:(NSPasteboard *)pb provideDataForType:(NSString *)type {
4632N/A AWT_ASSERT_APPKIT_THREAD;
4632N/A
4632N/A // 9-20-02 Note: leave this here for debugging:
4632N/A //[pb setString: @"Hello, World!" forType: NSStringPboardType];
4632N/A // return;
4632N/A
4632N/A // Set up Java environment:
4632N/A JNIEnv* env = [ThreadUtilities getJNIEnv];
4632N/A
4632N/A id pbData = nil;
4632N/A
4632N/A // Collect data in a pasteboard format:
4632N/A jlong jformat = indexForFormat(type);
4632N/A if (jformat >= 0) {
4632N/A // Convert DataTransfer data to a Java byte array:
4632N/A // Note that this will eventually call getTransferData()
4632N/A jbyteArray jdata = [self convertData:jformat];
4632N/A
4632N/A if (jdata != nil) {
4632N/A jboolean isCopy;
4632N/A jsize jdataLength = (*env)->GetArrayLength(env, jdata);
4632N/A jbyte* jbytedata = (*env)->GetByteArrayElements(env, jdata, &isCopy);
4632N/A
4632N/A if (jdataLength > 0 && jbytedata != nil) {
4632N/A // Get element data to the storage array. For NSFilenamesPboardType type we use
4632N/A // an NSArray-type data - NSData-type data would cause a crash.
4632N/A if (type != nil) {
4632N/A pbData = ([type isEqualTo:NSFilenamesPboardType]) ?
4632N/A [self getFileList:jbytedata dataLength:jdataLength] :
4632N/A [NSData dataWithBytes:jbytedata length:jdataLength];
4632N/A }
4632N/A }
4632N/A
4632N/A (*env)->ReleaseByteArrayElements(env, jdata, jbytedata, JNI_ABORT);
4632N/A
4632N/A (*env)->DeleteLocalRef(env, jdata);
4632N/A }
4632N/A }
4632N/A
4632N/A // If we are the custom type that matches local-vm drags, set an empty NSData
4632N/A if ( (pbData == nil) && ([type isEqualTo:[DnDUtilities javaPboardType]]) ) {
4632N/A pbData = [NSData dataWithBytes:"" length:1];
4632N/A }
4632N/A
4632N/A // Add pasteboard data for the type:
4632N/A // Remember, NSFilenamesPboardType's data is NSArray (property list), not NSData!
4632N/A // We must use proper pb accessor depending on the data type.
4632N/A if ([pbData isKindOfClass:[NSArray class]])
4632N/A [pb setPropertyList:pbData forType:type];
4632N/A else
4632N/A [pb setData:pbData forType:type];
4632N/A}
4632N/A
4632N/A
4632N/A- (void)validateDragImage
4632N/A{
4632N/A // Make a small blank image if we don't have a drag image:
4632N/A if (fDragImage == nil) {
4632N/A // 9-30-02 Note: keep this around for debugging:
4632N/A fDragImage = [[NSImage alloc] initWithSize:NSMakeSize(21, 21)];
4632N/A NSSize imageSize = [fDragImage size];
4632N/A
4632N/A NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
4632N/A pixelsWide:imageSize.width pixelsHigh:imageSize.height bitsPerSample:8 samplesPerPixel:4
4632N/A hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:32];
4632N/A
4632N/A [fDragImage addRepresentation:imageRep];
4632N/A fDragImageOffset = NSMakePoint(0, 0);
4632N/A
4632N/A [imageRep release];
4632N/A }
4632N/A}
4632N/A
4632N/A- (NSEvent*)nsDragEvent:(BOOL)isDrag
4632N/A{
4632N/A // Get NSView for the drag source:
4632N/A NSWindow* window = [fView window];
4632N/A
4632N/A NSInteger windowNumber = [window windowNumber];
4632N/A NSGraphicsContext* graphicsContext = [NSGraphicsContext graphicsContextWithWindow:window];
4632N/A
4632N/A // Convert mouse coordinates to NS:
6099N/A NSPoint eventLocation = [fView convertPoint:NSMakePoint(fDragPos.x, fDragPos.y) toView:nil];
6099N/A eventLocation.y = [[fView window] frame].size.height - eventLocation.y;
6099N/A
4632N/A // Convert fTriggerEventTimeStamp to NS - AWTEvent.h defines UTC(nsEvent) as ((jlong)[event timestamp] * 1000):
4632N/A NSTimeInterval timeStamp = fTriggerEventTimeStamp / 1000;
4632N/A
4632N/A // Convert fModifiers (extModifiers) to NS:
4632N/A NSEventType mouseButtons = 0;
4632N/A float pressure = 0.0;
4632N/A if (isDrag) {
4632N/A mouseButtons = (NSEventType) [DnDUtilities mapJavaExtModifiersToNSMouseDownButtons:fModifiers];
4632N/A pressure = 1.0;
4632N/A } else {
4632N/A mouseButtons = (NSEventType) [DnDUtilities mapJavaExtModifiersToNSMouseUpButtons:fModifiers];
4632N/A }
4632N/A
4632N/A // Convert fModifiers (extModifiers) to NS:
5337N/A NSUInteger modifiers = JavaModifiersToNsKeyModifiers(fModifiers, TRUE);
4632N/A
4632N/A // Just a dummy value ...
4632N/A NSInteger eventNumber = 0;
4632N/A
4632N/A // Make a native autoreleased dragging event:
4632N/A NSEvent* dragEvent = [NSEvent mouseEventWithType:mouseButtons location:eventLocation
4632N/A modifierFlags:modifiers timestamp:timeStamp windowNumber:windowNumber context:graphicsContext
4632N/A eventNumber:eventNumber clickCount:fClickCount pressure:pressure];
4632N/A
4632N/A return dragEvent;
4632N/A}
4632N/A
4632N/A- (void)doDrag
4632N/A{
4632N/A AWT_ASSERT_APPKIT_THREAD;
4632N/A
4632N/A DLog2(@"[CDragSource doDrag]: %@\n", self);
4632N/A
4632N/A // Set up Java environment:
4632N/A JNIEnv *env = [ThreadUtilities getJNIEnv];
4632N/A
4632N/A // Set up the pasteboard:
4632N/A NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard];
4632N/A [self declareTypesToPasteboard:pb withEnv:env];
4632N/A
4632N/A // Make a native autoreleased NS dragging event:
4632N/A NSEvent *dragEvent = [self nsDragEvent:YES];
4632N/A
4632N/A // Get NSView for the drag source:
4632N/A NSView *view = fView;
4632N/A
4632N/A // Make sure we have a valid drag image:
4632N/A [self validateDragImage];
4632N/A NSImage* dragImage = fDragImage;
4632N/A
4632N/A // Get drag origin and offset:
6099N/A NSPoint dragOrigin = [dragEvent locationInWindow];
4632N/A dragOrigin.x += fDragImageOffset.x;
6099N/A dragOrigin.y -= fDragImageOffset.y + [dragImage size].height;
4632N/A
4632N/A // Drag offset values don't seem to matter:
4632N/A NSSize dragOffset = NSMakeSize(0, 0);
4632N/A
4632N/A // These variables should be set based on the transferable:
4632N/A BOOL isFileDrag = FALSE;
4632N/A BOOL fileDragPromises = FALSE;
4632N/A
4632N/A DLog(@"[CDragSource drag]: calling dragImage/File:");
4632N/A DLog3(@" - drag origin: %f, %f", fDragPos.x, fDragPos.y);
4632N/A DLog5(@" - drag image: %f, %f (%f x %f)", fDragImageOffset.x, fDragImageOffset.y, [dragImage size].width, [dragImage size].height);
4632N/A DLog3(@" - event point (window) %f, %f", [dragEvent locationInWindow].x, [dragEvent locationInWindow].y);
4632N/A DLog3(@" - drag point (view) %f, %f", dragOrigin.x, dragOrigin.y);
4632N/A // Set up the fDragKeyModifier, so we know if the operation has changed
4632N/A // Set up the fDragMouseModifier, so we can |= it later (since CoreDrag doesn't tell us mouse states during a drag)
4632N/A fDragKeyModifiers = [DnDUtilities extractJavaExtKeyModifiersFromJavaExtModifiers:fModifiers];
4632N/A fDragMouseModifiers = [DnDUtilities extractJavaExtMouseModifiersFromJavaExtModifiers:fModifiers];
4632N/A
4632N/A // Set the current DragSource
4632N/A sCurrentDragSource = self;
4632N/A sNeedsEnter = YES;
4632N/A
4632N/A @try {
4632N/A // Data dragging:
4632N/A if (isFileDrag == FALSE) {
4632N/A [view dragImage:dragImage at:dragOrigin offset:dragOffset event:dragEvent pasteboard:pb source:view slideBack:YES];
4632N/A } else if (fileDragPromises == FALSE) {
4632N/A // File dragging:
4632N/A NSLog(@"[CDragSource drag]: file dragging is unsupported.");
4632N/A NSString* fileName = nil; // This should be set based on the transferable.
4632N/A NSRect fileLocationRect = NSMakeRect(0, 0, 0, 0); // This should be set based on the filename.
4632N/A
4632N/A BOOL success = [view dragFile:fileName fromRect:fileLocationRect slideBack:YES event:dragEvent];
4632N/A if (success == TRUE) { // One would erase dragged file if this was a move operation.
4632N/A }
4632N/A } else {
4632N/A // Promised file dragging:
4632N/A NSLog(@"[CDragSource drag]: file dragging promises are unsupported.");
4632N/A NSArray* fileTypesArray = nil; // This should be set based on the transferable.
4632N/A NSRect fileLocationRect = NSMakeRect(0, 0, 0, 0); // This should be set based on all filenames.
4632N/A
4632N/A BOOL success = [view dragPromisedFilesOfTypes:fileTypesArray fromRect:fileLocationRect source:view slideBack:YES event:dragEvent];
4632N/A if (success == TRUE) { // One would write out the promised files here.
4632N/A }
4632N/A }
4632N/A
4632N/A NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
4632N/A
4632N/A // Convert drag operation to Java:
4632N/A jint dragOp = [DnDUtilities mapNSDragOperationToJava:sDragOperation];
4632N/A
4632N/A // Drag success must acount for DragOperationNone:
4632N/A jboolean success = (dragOp != java_awt_dnd_DnDConstants_ACTION_NONE);
4632N/A
4632N/A // We have a problem here... we don't send DragSource dragEnter/Exit messages outside of our own process
4632N/A // because we don't get anything from AppKit/CoreDrag
4632N/A // This means that if you drag outside of the app and drop, even if it's valid, a dragDropFinished is posted without dragEnter
4632N/A // I'm worried that this might confuse Java, so we're going to send a "bogus" dragEnter if necessary (only if the drag succeeded)
4632N/A if (success && sNeedsEnter) {
4632N/A [self postDragEnter];
4632N/A }
4632N/A
4632N/A // DragSourceContextPeer.dragDropFinished() should be called even if there was an error:
4632N/A JNF_MEMBER_CACHE(dragDropFinishedMethod, CDragSourceContextPeerClass, "dragDropFinished", "(ZIII)V");
4632N/A DLog3(@" -> posting dragDropFinished, point %f, %f", point.x, point.y);
4632N/A JNFCallVoidMethod(env, fDragSourceContextPeer, dragDropFinishedMethod, success, dragOp, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
4730N/A JNF_MEMBER_CACHE(resetHoveringMethod, CDragSourceContextPeerClass, "resetHovering", "()V");
4639N/A JNFCallVoidMethod(env, fDragSourceContextPeer, resetHoveringMethod); // Hust reset static variable
4632N/A } @finally {
4632N/A // Clear the current DragSource
4632N/A sCurrentDragSource = nil;
4632N/A sNeedsEnter = NO;
4632N/A }
4632N/A
4632N/A // We have to do this, otherwise AppKit doesn't know we're finished dragging. Yup, it's that bad.
4632N/A if ([[[NSRunLoop currentRunLoop] currentMode] isEqualTo:NSEventTrackingRunLoopMode]) {
4632N/A [NSApp postEvent:[self nsDragEvent:NO] atStart:YES];
4632N/A }
4632N/A
4632N/A DLog2(@"[CDragSource doDrag] end: %@\n", self);
4632N/A}
4632N/A
4632N/A- (void)drag
4632N/A{
4632N/A AWT_ASSERT_NOT_APPKIT_THREAD;
4632N/A
4632N/A // Set the drag cursor (or not 3839999)
4632N/A //JNIEnv *env = [ThreadUtilities getJNIEnv];
4632N/A //jobject gCursor = JNFNewGlobalRef(env, fCursor);
4632N/A //[EventFactory setJavaCursor:gCursor withEnv:env];
4632N/A
4632N/A [self performSelectorOnMainThread:@selector(doDrag) withObject:nil waitUntilDone:YES]; // AWT_THREADING Safe (called from unique asynchronous thread)
4632N/A}
4632N/A
4632N/A/******************************** BEGIN NSDraggingSource Interface ********************************/
4632N/A
4632N/A- (void)draggingOperationChanged:(NSDragOperation)dragOp {
4632N/A //DLog2(@"[CDragSource draggingOperationChanged]: %@\n", self);
4632N/A
4632N/A JNIEnv* env = [ThreadUtilities getJNIEnv];
4632N/A
4632N/A jint targetActions = fSourceActions;
4632N/A if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];
4632N/A
4632N/A NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
4632N/A DLog3(@" -> posting operationChanged, point %f, %f", point.x, point.y);
4632N/A jint modifiedModifiers = fDragKeyModifiers | fDragMouseModifiers | [DnDUtilities javaKeyModifiersForNSDragOperation:dragOp];
4632N/A
4632N/A JNF_MEMBER_CACHE(operationChangedMethod, CDragSourceContextPeerClass, "operationChanged", "(IIII)V");
4632N/A JNFCallVoidMethod(env, fDragSourceContextPeer, operationChangedMethod, targetActions, modifiedModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
4632N/A}
4632N/A
4632N/A- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)localDrag {
4632N/A //DLog2(@"[CDragSource draggingSourceOperationMaskForLocal]: %@\n", self);
4632N/A return [DnDUtilities mapJavaDragOperationToNS:fSourceActions];
4632N/A}
4632N/A
4632N/A/* 9-16-02 Note: we don't support promises yet.
4632N/A- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination {
4632N/A}*/
4632N/A
4632N/A- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint {
4632N/A DLog4(@"[CDragSource draggedImage beganAt]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self);
4632N/A
4632N/A // Initialize static variables:
4632N/A sDragOperation = NSDragOperationNone;
4632N/A sDraggingLocation = screenPoint;
4632N/A}
4632N/A
4632N/A- (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation {
4632N/A DLog4(@"[CDragSource draggedImage endedAt:]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self);
4632N/A
4632N/A sDraggingLocation = screenPoint;
4632N/A sDragOperation = operation;
4632N/A}
4632N/A
4632N/A- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint {
4632N/A //DLog4(@"[CDragSource draggedImage moved]: (%d, %d) %@\n", (int) screenPoint.x, (int) screenPoint.y, self);
4632N/A JNIEnv* env = [ThreadUtilities getJNIEnv];
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A // There are two things we would be interested in:
4632N/A // a) mouse pointer has moved
4632N/A // b) drag actions (key modifiers) have changed
4632N/A
4632N/A BOOL notifyJava = FALSE;
4632N/A
4632N/A // a) mouse pointer has moved:
4632N/A if (NSEqualPoints(screenPoint, sDraggingLocation) == FALSE) {
4632N/A //DLog2(@"[CDragSource draggedImage:movedTo]: mouse moved, %@\n", self);
4632N/A notifyJava = TRUE;
4632N/A }
4632N/A
4632N/A // b) drag actions (key modifiers) have changed:
5337N/A jint modifiers = NsKeyModifiersToJavaModifiers([NSEvent modifierFlags], YES);
4632N/A if (fDragKeyModifiers != modifiers) {
4632N/A NSDragOperation currentOp = [DnDUtilities nsDragOperationForModifiers:[NSEvent modifierFlags]];
4632N/A NSDragOperation allowedOp = [DnDUtilities mapJavaDragOperationToNS:fSourceActions] & currentOp;
4632N/A
4632N/A fDragKeyModifiers = modifiers;
4632N/A
4632N/A if (sDragOperation != allowedOp) {
4632N/A sDragOperation = allowedOp;
4632N/A [self draggingOperationChanged:allowedOp];
4632N/A }
4632N/A }
4632N/A
4632N/A // Should we notify Java things have changed?
4632N/A if (notifyJava) {
4632N/A sDraggingLocation = screenPoint;
4632N/A
4632N/A NSPoint point = [self mapNSScreenPointToJavaWithOffset:screenPoint];
4632N/A
4632N/A jint targetActions = fSourceActions;
4632N/A if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];
4632N/A
4632N/A // Motion: dragMotion, dragMouseMoved
4632N/A DLog4(@"[CDragSource draggedImage moved]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self);
4632N/A
4632N/A DLog3(@" -> posting dragMotion, point %f, %f", point.x, point.y);
4632N/A JNF_MEMBER_CACHE(dragMotionMethod, CDragSourceContextPeerClass, "dragMotion", "(IIII)V");
4632N/A JNFCallVoidMethod(env, fDragSourceContextPeer, dragMotionMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
4632N/A
4632N/A DLog3(@" -> posting dragMouseMoved, point %f, %f", point.x, point.y);
4632N/A JNF_MEMBER_CACHE(dragMouseMovedMethod, CDragSourceContextPeerClass, "dragMouseMoved", "(IIII)V");
4632N/A JNFCallVoidMethod(env, fDragSourceContextPeer, dragMouseMovedMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
4632N/A }
4632N/AJNF_COCOA_EXIT(env);
4632N/A}
4632N/A
4632N/A- (BOOL)ignoreModifierKeysWhileDragging {
4632N/A //DLog2(@"[CDragSource ignoreModifierKeysWhileDragging]: %@\n", self);
4632N/A return NO;
4632N/A}
4632N/A
4632N/A/******************************** END NSDraggingSource Interface ********************************/
4632N/A
4632N/A
4632N/A// postDragEnter and postDragExit are called from CDropTarget when possible and appropriate
4632N/A// Currently only possible if source and target are in the same process
4632N/A- (void) postDragEnter {
4632N/A JNIEnv *env = [ThreadUtilities getJNIEnv];
4632N/A sNeedsEnter = NO;
4632N/A
4632N/A jint targetActions = fSourceActions;
4632N/A if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];
4632N/A
4632N/A NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
4632N/A DLog3(@" -> posting dragEnter, point %f, %f", point.x, point.y);
4632N/A JNF_MEMBER_CACHE(dragEnterMethod, CDragSourceContextPeerClass, "dragEnter", "(IIII)V");
4632N/A JNFCallVoidMethod(env, fDragSourceContextPeer, dragEnterMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
4632N/A}
4632N/A
4632N/A- (void) postDragExit {
4632N/A JNIEnv *env = [ThreadUtilities getJNIEnv];
4632N/A sNeedsEnter = YES;
4632N/A
4632N/A NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
4632N/A DLog3(@" -> posting dragExit, point %f, %f", point.x, point.y);
4632N/A JNF_MEMBER_CACHE(dragExitMethod, CDragSourceContextPeerClass, "dragExit", "(II)V");
4632N/A JNFCallVoidMethod(env, fDragSourceContextPeer, dragExitMethod, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
4632N/A}
4632N/A
4632N/A
4632N/A// Java assumes that the origin is the top-left corner of the screen.
4632N/A// Cocoa assumes that the origin is the bottom-left corner of the screen.
4632N/A// Adjust the y coordinate to account for this.
4632N/A// NOTE: Also need to take into account the 0 screen relative screen coords.
4632N/A// This is because all screen coords in Cocoa are relative to the 0 screen.
4632N/A// Also see +[CWindow convertAWTToCocoaScreenRect]
4632N/A// NSScreen-to-JavaScreen mapping:
4632N/A- (NSPoint) mapNSScreenPointToJavaWithOffset:(NSPoint)screenPoint {
4632N/A NSRect mainR = [[[NSScreen screens] objectAtIndex:0] frame];
4632N/A NSPoint point = NSMakePoint(screenPoint.x, mainR.size.height - (screenPoint.y));
4632N/A
4632N/A // Adjust the point with the drag image offset to get the real mouse hotspot:
4632N/A // The point should remain in screen coordinates (as per DragSourceEvent.getLocation() documentation)
4632N/A point.x -= fDragImageOffset.x;
4632N/A point.y -= ([fDragImage size].height + fDragImageOffset.y);
4632N/A
4632N/A return point;
4632N/A}
4632N/A
4632N/A@end