Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CDragSource.m
38829 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425//#define DND_DEBUG TRUE2627#import "java_awt_dnd_DnDConstants.h"2829#import <Cocoa/Cocoa.h>30#import <JavaNativeFoundation/JavaNativeFoundation.h>3132#import "AWTEvent.h"33#import "AWTView.h"34#import "CDataTransferer.h"35#import "CDropTarget.h"36#import "CDragSource.h"37#import "DnDUtilities.h"38#import "ThreadUtilities.h"394041// When sIsJavaDragging is true Java drag gesture has been recognized and a drag is/has been initialized.42// We must stop posting MouseEvent.MOUSE_DRAGGED events for the duration of the drag or all hell will break43// loose in shared code - tracking state going haywire.44static BOOL sIsJavaDragging;454647@interface NSEvent(AWTAdditions)4849+ (void)javaDraggingBegin;50+ (void)javaDraggingEnd;5152@end535455@implementation NSEvent(AWTAdditions)565758+ (void)javaDraggingBegin59{60sIsJavaDragging = YES;61}6263+ (void)javaDraggingEnd64{65// sIsJavaDragging is reset on mouseDown as well.66sIsJavaDragging = NO;67}68@end6970JNF_CLASS_CACHE(DataTransfererClass, "sun/awt/datatransfer/DataTransferer");71JNF_CLASS_CACHE(CDragSourceContextPeerClass, "sun/lwawt/macosx/CDragSourceContextPeer");72JNF_CLASS_CACHE(CImageClass, "sun/lwawt/macosx/CImage");7374static NSDragOperation sDragOperation;75static NSPoint sDraggingLocation;7677static BOOL sNeedsEnter;7879@interface CDragSource ()80// Updates from the destination to the source81- (void) postDragEnter;82- (void) postDragExit;83// Utility84- (NSPoint) mapNSScreenPointToJavaWithOffset:(NSPoint) point;85@end8687@implementation CDragSource8889- (id) init:(jobject)jDragSourceContextPeer90component:(jobject)jComponent91control:(id)control92transferable:(jobject)jTransferable93triggerEvent:(jobject)jTrigger94dragPosX:(jint)dragPosX95dragPosY:(jint)dragPosY96modifiers:(jint)extModifiers97clickCount:(jint)clickCount98timeStamp:(jlong)timeStamp99dragImage:(jobject)jDragImage100dragImageOffsetX:(jint)jDragImageOffsetX101dragImageOffsetY:(jint)jDragImageOffsetY102sourceActions:(jint)jSourceActions103formats:(jlongArray)jFormats104formatMap:(jobject)jFormatMap105{106self = [super init];107DLog2(@"[CDragSource init]: %@\n", self);108109fView = nil;110fComponent = nil;111112// Construct the object if we have a valid model for it:113if (control != nil) {114JNIEnv *env = [ThreadUtilities getJNIEnv];115fComponent = JNFNewGlobalRef(env, jComponent);116fDragSourceContextPeer = JNFNewGlobalRef(env, jDragSourceContextPeer);117118fTransferable = JNFNewGlobalRef(env, jTransferable);119fTriggerEvent = JNFNewGlobalRef(env, jTrigger);120121if (jDragImage) {122JNF_MEMBER_CACHE(nsImagePtr, CImageClass, "ptr", "J");123jlong imgPtr = JNFGetLongField(env, jDragImage, nsImagePtr);124fDragImage = (NSImage*) jlong_to_ptr(imgPtr); // Double-casting prevents compiler 'd$|//125126[fDragImage retain];127}128129fDragImageOffset = NSMakePoint(jDragImageOffsetX, jDragImageOffsetY);130131fSourceActions = jSourceActions;132fFormats = JNFNewGlobalRef(env, jFormats);133fFormatMap = JNFNewGlobalRef(env, jFormatMap);134135fTriggerEventTimeStamp = timeStamp;136fDragPos = NSMakePoint(dragPosX, dragPosY);137fClickCount = clickCount;138fModifiers = extModifiers;139140// Set this object as a dragging source:141142fView = [(AWTView *) control retain];143[fView setDragSource:self];144145// Let AWTEvent know Java drag is getting underway:146[NSEvent javaDraggingBegin];147}148149else {150[self release];151self = nil;152}153154return self;155}156157- (void)removeFromView:(JNIEnv *)env158{159DLog2(@"[CDragSource removeFromView]: %@\n", self);160161// Remove this dragging source from the view:162[((AWTView *) fView) setDragSource:nil];163164// Clean up JNI refs165if (fComponent != NULL) {166JNFDeleteGlobalRef(env, fComponent);167fComponent = NULL;168}169170if (fDragSourceContextPeer != NULL) {171JNFDeleteGlobalRef(env, fDragSourceContextPeer);172fDragSourceContextPeer = NULL;173}174175if (fTransferable != NULL) {176JNFDeleteGlobalRef(env, fTransferable);177fTransferable = NULL;178}179180if (fTriggerEvent != NULL) {181JNFDeleteGlobalRef(env, fTriggerEvent);182fTriggerEvent = NULL;183}184185if (fFormats != NULL) {186JNFDeleteGlobalRef(env, fFormats);187fFormats = NULL;188}189190if (fFormatMap != NULL) {191JNFDeleteGlobalRef(env, fFormatMap);192fFormatMap = NULL;193}194195[self release];196}197198- (void)dealloc199{200DLog2(@"[CDragSource dealloc]: %@\n", self);201202// Delete or release local data:203[fView release];204fView = nil;205206[fDragImage release];207fDragImage = nil;208209[super dealloc];210}211212// Appropriated from Windows' awt_DataTransferer.cpp:213//214// * NOTE: This returns a JNI Local Ref. Any code that calls must call DeleteLocalRef with the return value.215//216- (jobject)dataTransferer:(JNIEnv*)env217{218JNF_STATIC_MEMBER_CACHE(getInstanceMethod, DataTransfererClass, "getInstance", "()Lsun/awt/datatransfer/DataTransferer;");219return JNFCallStaticObjectMethod(env, getInstanceMethod);220}221222// Appropriated from Windows' awt_DataTransferer.cpp:223//224// * NOTE: This returns a JNI Local Ref. Any code that calls must call DeleteLocalRef with the return value.225//226- (jbyteArray)convertData:(jlong)format227{228JNIEnv* env = [ThreadUtilities getJNIEnv];229jobject transferer = [self dataTransferer:env];230jbyteArray data = nil;231232if (transferer != NULL) {233JNF_MEMBER_CACHE(convertDataMethod, DataTransfererClass, "convertData", "(Ljava/lang/Object;Ljava/awt/datatransfer/Transferable;JLjava/util/Map;Z)[B");234data = JNFCallObjectMethod(env, transferer, convertDataMethod, fComponent, fTransferable, format, fFormatMap, (jboolean) TRUE);235}236237return data;238}239240241// Encodes a byte array of zero-terminated filenames into an NSArray of NSStrings representing them.242// Borrowed and adapted from awt_DataTransferer.c, convertFileType().243- (id)getFileList:(jbyte *)jbytes dataLength:(jsize)jbytesLength244{245jsize strings = 0;246jsize i;247248// Get number of filenames while making sure to skip over empty strings.249for (i = 1; i < jbytesLength; i++) {250if (jbytes[i] == '\0' && jbytes[i - 1] != '\0')251strings++;252}253254// Create the file list to return:255NSMutableArray* fileList = [NSMutableArray arrayWithCapacity:strings];256257for (i = 0; i < jbytesLength; i++) {258char* start = (char *) &jbytes[i];259260// Skip over empty strings:261if (start[0] == '\0') {262continue;263}264265// Update the position marker:266i += strlen(start);267268// Add this filename to the file list:269NSMutableString* fileName = [NSMutableString stringWithUTF8String:start];270// Decompose the filename271CFStringNormalize((CFMutableStringRef)fileName, kCFStringNormalizationFormD);272[fileList addObject:fileName];273}274275// 03-01-09 Note: keep this around for debugging.276// return [NSArray arrayWithObjects:@"/tmp/foo1", @"/tmp/foo2", nil];277278return ([fileList count] > 0 ? fileList : nil);279}280281282// Set up the pasteboard for dragging:283- (BOOL)declareTypesToPasteboard:(NSPasteboard *)pb withEnv:(JNIEnv *) env {284// 9-20-02 Note: leave this here for debugging:285//[pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: self];286//return TRUE;287288// Get byte array elements:289jboolean isCopy;290jlong* jformats = (*env)->GetLongArrayElements(env, fFormats, &isCopy);291if (jformats == nil)292return FALSE;293294// Allocate storage arrays for dragging types to register with the pasteboard:295jsize formatsLength = (*env)->GetArrayLength(env, fFormats);296NSMutableArray* pbTypes = [[NSMutableArray alloc] initWithCapacity:formatsLength];297298// And assume there are no NS-type data: [Radar 3065621]299// This is to be able to drop transferables containing only a serialized object flavor, e.g.:300// "JAVA_DATAFLAVOR:application/x-java-serialized-object; class=java.awt.Label".301BOOL hasNSTypeData = false;302303// Collect all supported types in a pasteboard format into the storage arrays:304jsize i;305for (i = 0; i < formatsLength; i++) {306jlong jformat = jformats[i];307308if (jformat >= 0) {309NSString* type = formatForIndex(jformat);310311// Add element type to the storage array.312if (type != nil) {313if ([type hasPrefix:@"JAVA_DATAFLAVOR:application/x-java-jvm-local-objectref;"] == false) {314[pbTypes addObject:type];315316// This is a good approximation if not perfect. A conclusive search would317// have to be done matching all defined strings in AppKit's commonStrings.h.318hasNSTypeData = [type hasPrefix:@"NS"] || [type hasPrefix:@"NeXT"] || [type hasPrefix:@"public."];319}320}321}322}323324// 1-16-03 Note: [Radar 3065621]325// When TransferHandler is used with Swing components it puts only a type like this on the pasteboard:326// "JAVA_DATAFLAVOR:application/x-java-jvm-local-objectref; class=java.lang.String"327// And there's similar type for serialized object only transferables.328// Since our drop targets aren't trained for arbitrary data types yet we need to fake an empty string329// which will cause drop target handlers to fire.330// KCH - 3550405 If the drag is between Swing components, formatsLength == 0, so expand the check.331// Also, use a custom format rather than NSString, since that will prevent random views from accepting the drag332if (hasNSTypeData == false && formatsLength >= 0) {333[pbTypes addObject:[DnDUtilities javaPboardType]];334}335336(*env)->ReleaseLongArrayElements(env, fFormats, jformats, JNI_ABORT);337338// Declare pasteboard types. If the types array is empty we still want to declare them339// as otherwise an old set of types/data would remain on the pasteboard.340NSUInteger typesCount = [pbTypes count];341[pb declareTypes:pbTypes owner: self];342343// KCH - Lame conversion bug between Cocoa and Carbon drag types344// If I provide the filenames _right now_, NSFilenamesPboardType is properly converted to CoreDrag flavors345// If I try to wait until pasteboard:provideDataForType:, the conversion won't happen346// and pasteboard:provideDataForType: won't even get called! (unless I go over a Cocoa app)347if ([pbTypes containsObject:NSFilenamesPboardType]) {348[self pasteboard:pb provideDataForType:NSFilenamesPboardType];349}350351[pbTypes release];352353return typesCount > 0 ? TRUE : FALSE;354}355356// This is an NSPasteboard callback. In declareTypesToPasteboard:withEnv:, we only declared the types357// When the AppKit DnD system actually needs the data, this method will be invoked.358// Note that if the transfer is handled entirely from Swing (as in a local-vm drag), this method may never be called.359- (void)pasteboard:(NSPasteboard *)pb provideDataForType:(NSString *)type {360AWT_ASSERT_APPKIT_THREAD;361362// 9-20-02 Note: leave this here for debugging:363//[pb setString: @"Hello, World!" forType: NSStringPboardType];364// return;365366// Set up Java environment:367JNIEnv* env = [ThreadUtilities getJNIEnv];368369id pbData = nil;370371// Collect data in a pasteboard format:372jlong jformat = indexForFormat(type);373if (jformat >= 0) {374// Convert DataTransfer data to a Java byte array:375// Note that this will eventually call getTransferData()376jbyteArray jdata = [self convertData:jformat];377378if (jdata != nil) {379jboolean isCopy;380jsize jdataLength = (*env)->GetArrayLength(env, jdata);381jbyte* jbytedata = (*env)->GetByteArrayElements(env, jdata, &isCopy);382383if (jdataLength > 0 && jbytedata != nil) {384// Get element data to the storage array. For NSFilenamesPboardType type we use385// an NSArray-type data - NSData-type data would cause a crash.386if (type != nil) {387pbData = ([type isEqualTo:NSFilenamesPboardType]) ?388[self getFileList:jbytedata dataLength:jdataLength] :389[NSData dataWithBytes:jbytedata length:jdataLength];390}391}392393(*env)->ReleaseByteArrayElements(env, jdata, jbytedata, JNI_ABORT);394395(*env)->DeleteLocalRef(env, jdata);396}397}398399// If we are the custom type that matches local-vm drags, set an empty NSData400if ( (pbData == nil) && ([type isEqualTo:[DnDUtilities javaPboardType]]) ) {401pbData = [NSData dataWithBytes:"" length:1];402}403404// Add pasteboard data for the type:405// Remember, NSFilenamesPboardType's data is NSArray (property list), not NSData!406// We must use proper pb accessor depending on the data type.407if ([pbData isKindOfClass:[NSArray class]])408[pb setPropertyList:pbData forType:type];409else410[pb setData:pbData forType:type];411}412413414- (void)validateDragImage415{416// Make a small blank image if we don't have a drag image:417if (fDragImage == nil) {418// 9-30-02 Note: keep this around for debugging:419fDragImage = [[NSImage alloc] initWithSize:NSMakeSize(21, 21)];420NSSize imageSize = [fDragImage size];421422NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL423pixelsWide:imageSize.width pixelsHigh:imageSize.height bitsPerSample:8 samplesPerPixel:4424hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:32];425426[fDragImage addRepresentation:imageRep];427fDragImageOffset = NSMakePoint(0, 0);428429[imageRep release];430}431}432433- (NSEvent*)nsDragEvent:(BOOL)isDrag434{435// Get NSView for the drag source:436NSWindow* window = [fView window];437438NSInteger windowNumber = [window windowNumber];439NSGraphicsContext* graphicsContext = [NSGraphicsContext graphicsContextWithWindow:window];440441// Convert mouse coordinates to NS:442NSPoint eventLocation = [fView convertPoint:NSMakePoint(fDragPos.x, fDragPos.y) toView:nil];443eventLocation.y = [[fView window] frame].size.height - eventLocation.y;444445// Convert fTriggerEventTimeStamp to NS - AWTEvent.h defines UTC(nsEvent) as ((jlong)[event timestamp] * 1000):446NSTimeInterval timeStamp = fTriggerEventTimeStamp / 1000;447448// Convert fModifiers (extModifiers) to NS:449NSEventType mouseButtons = 0;450float pressure = 0.0;451if (isDrag) {452mouseButtons = (NSEventType) [DnDUtilities mapJavaExtModifiersToNSMouseDownButtons:fModifiers];453pressure = 1.0;454} else {455mouseButtons = (NSEventType) [DnDUtilities mapJavaExtModifiersToNSMouseUpButtons:fModifiers];456}457458// Convert fModifiers (extModifiers) to NS:459NSUInteger modifiers = JavaModifiersToNsKeyModifiers(fModifiers, TRUE);460461// Just a dummy value ...462NSInteger eventNumber = 0;463464// Make a native autoreleased dragging event:465NSEvent* dragEvent = [NSEvent mouseEventWithType:mouseButtons location:eventLocation466modifierFlags:modifiers timestamp:timeStamp windowNumber:windowNumber context:graphicsContext467eventNumber:eventNumber clickCount:fClickCount pressure:pressure];468469return dragEvent;470}471472- (void)doDrag473{474AWT_ASSERT_APPKIT_THREAD;475476DLog2(@"[CDragSource doDrag]: %@\n", self);477478// Set up Java environment:479JNIEnv *env = [ThreadUtilities getJNIEnv];480481// Set up the pasteboard:482NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard];483[self declareTypesToPasteboard:pb withEnv:env];484485// Make a native autoreleased NS dragging event:486NSEvent *dragEvent = [self nsDragEvent:YES];487488// Get NSView for the drag source:489NSView *view = fView;490491// Make sure we have a valid drag image:492[self validateDragImage];493NSImage* dragImage = fDragImage;494495// Get drag origin and offset:496NSPoint dragOrigin = [dragEvent locationInWindow];497dragOrigin.x += fDragImageOffset.x;498dragOrigin.y -= fDragImageOffset.y + [dragImage size].height;499500// Drag offset values don't seem to matter:501NSSize dragOffset = NSMakeSize(0, 0);502503// These variables should be set based on the transferable:504BOOL isFileDrag = FALSE;505BOOL fileDragPromises = FALSE;506507DLog(@"[CDragSource drag]: calling dragImage/File:");508DLog3(@" - drag origin: %f, %f", fDragPos.x, fDragPos.y);509DLog5(@" - drag image: %f, %f (%f x %f)", fDragImageOffset.x, fDragImageOffset.y, [dragImage size].width, [dragImage size].height);510DLog3(@" - event point (window) %f, %f", [dragEvent locationInWindow].x, [dragEvent locationInWindow].y);511DLog3(@" - drag point (view) %f, %f", dragOrigin.x, dragOrigin.y);512// Set up the fDragKeyModifier, so we know if the operation has changed513// Set up the fDragMouseModifier, so we can |= it later (since CoreDrag doesn't tell us mouse states during a drag)514fDragKeyModifiers = [DnDUtilities extractJavaExtKeyModifiersFromJavaExtModifiers:fModifiers];515fDragMouseModifiers = [DnDUtilities extractJavaExtMouseModifiersFromJavaExtModifiers:fModifiers];516517sNeedsEnter = YES;518519@try {520// Data dragging:521if (isFileDrag == FALSE) {522[view dragImage:dragImage at:dragOrigin offset:dragOffset event:dragEvent pasteboard:pb source:view slideBack:YES];523} else if (fileDragPromises == FALSE) {524// File dragging:525NSLog(@"[CDragSource drag]: file dragging is unsupported.");526NSString* fileName = nil; // This should be set based on the transferable.527NSRect fileLocationRect = NSMakeRect(0, 0, 0, 0); // This should be set based on the filename.528529BOOL success = [view dragFile:fileName fromRect:fileLocationRect slideBack:YES event:dragEvent];530if (success == TRUE) { // One would erase dragged file if this was a move operation.531}532} else {533// Promised file dragging:534NSLog(@"[CDragSource drag]: file dragging promises are unsupported.");535NSArray* fileTypesArray = nil; // This should be set based on the transferable.536NSRect fileLocationRect = NSMakeRect(0, 0, 0, 0); // This should be set based on all filenames.537538BOOL success = [view dragPromisedFilesOfTypes:fileTypesArray fromRect:fileLocationRect source:view slideBack:YES event:dragEvent];539if (success == TRUE) { // One would write out the promised files here.540}541}542543NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];544545// Convert drag operation to Java:546jint dragOp = [DnDUtilities mapNSDragOperationToJava:sDragOperation];547548// Drag success must acount for DragOperationNone:549jboolean success = (dragOp != java_awt_dnd_DnDConstants_ACTION_NONE);550551// We have a problem here... we don't send DragSource dragEnter/Exit messages outside of our own process552// because we don't get anything from AppKit/CoreDrag553// This means that if you drag outside of the app and drop, even if it's valid, a dragDropFinished is posted without dragEnter554// I'm worried that this might confuse Java, so we're going to send a "bogus" dragEnter if necessary (only if the drag succeeded)555if (success && sNeedsEnter) {556[self postDragEnter];557}558559// DragSourceContextPeer.dragDropFinished() should be called even if there was an error:560JNF_MEMBER_CACHE(dragDropFinishedMethod, CDragSourceContextPeerClass, "dragDropFinished", "(ZIII)V");561DLog3(@" -> posting dragDropFinished, point %f, %f", point.x, point.y);562JNFCallVoidMethod(env, fDragSourceContextPeer, dragDropFinishedMethod, success, dragOp, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)563JNF_MEMBER_CACHE(resetHoveringMethod, CDragSourceContextPeerClass, "resetHovering", "()V");564JNFCallVoidMethod(env, fDragSourceContextPeer, resetHoveringMethod); // Hust reset static variable565} @finally {566sNeedsEnter = NO;567}568569// We have to do this, otherwise AppKit doesn't know we're finished dragging. Yup, it's that bad.570if ([[[NSRunLoop currentRunLoop] currentMode] isEqualTo:NSEventTrackingRunLoopMode]) {571[NSApp postEvent:[self nsDragEvent:NO] atStart:YES];572}573574DLog2(@"[CDragSource doDrag] end: %@\n", self);575}576577- (void)drag578{579AWT_ASSERT_NOT_APPKIT_THREAD;580581[self performSelectorOnMainThread:@selector(doDrag) withObject:nil waitUntilDone:YES]; // AWT_THREADING Safe (called from unique asynchronous thread)582}583584/******************************** BEGIN NSDraggingSource Interface ********************************/585586- (void)draggingOperationChanged:(NSDragOperation)dragOp {587//DLog2(@"[CDragSource draggingOperationChanged]: %@\n", self);588589JNIEnv* env = [ThreadUtilities getJNIEnv];590591jint targetActions = fSourceActions;592if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];593594NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];595DLog3(@" -> posting operationChanged, point %f, %f", point.x, point.y);596jint modifiedModifiers = fDragKeyModifiers | fDragMouseModifiers | [DnDUtilities javaKeyModifiersForNSDragOperation:dragOp];597598JNF_MEMBER_CACHE(operationChangedMethod, CDragSourceContextPeerClass, "operationChanged", "(IIII)V");599JNFCallVoidMethod(env, fDragSourceContextPeer, operationChangedMethod, targetActions, modifiedModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)600}601602- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)localDrag {603//DLog2(@"[CDragSource draggingSourceOperationMaskForLocal]: %@\n", self);604return [DnDUtilities mapJavaDragOperationToNS:fSourceActions];605}606607/* 9-16-02 Note: we don't support promises yet.608- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination {609}*/610611- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint {612DLog4(@"[CDragSource draggedImage beganAt]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self);613614// Initialize static variables:615sDragOperation = NSDragOperationNone;616sDraggingLocation = screenPoint;617}618619- (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation {620DLog4(@"[CDragSource draggedImage endedAt:]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self);621622sDraggingLocation = screenPoint;623sDragOperation = operation;624}625626- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint {627//DLog4(@"[CDragSource draggedImage moved]: (%d, %d) %@\n", (int) screenPoint.x, (int) screenPoint.y, self);628JNIEnv* env = [ThreadUtilities getJNIEnv];629630JNF_COCOA_ENTER(env);631// There are two things we would be interested in:632// a) mouse pointer has moved633// b) drag actions (key modifiers) have changed634635BOOL notifyJava = FALSE;636637// a) mouse pointer has moved:638if (NSEqualPoints(screenPoint, sDraggingLocation) == FALSE) {639//DLog2(@"[CDragSource draggedImage:movedTo]: mouse moved, %@\n", self);640notifyJava = TRUE;641}642643// b) drag actions (key modifiers) have changed:644jint modifiers = NsKeyModifiersToJavaModifiers([NSEvent modifierFlags], YES);645if (fDragKeyModifiers != modifiers) {646NSDragOperation currentOp = [DnDUtilities nsDragOperationForModifiers:[NSEvent modifierFlags]];647NSDragOperation allowedOp = [DnDUtilities mapJavaDragOperationToNS:fSourceActions] & currentOp;648649fDragKeyModifiers = modifiers;650651if (sDragOperation != allowedOp) {652sDragOperation = allowedOp;653[self draggingOperationChanged:allowedOp];654}655}656657// Should we notify Java things have changed?658if (notifyJava) {659sDraggingLocation = screenPoint;660661NSPoint point = [self mapNSScreenPointToJavaWithOffset:screenPoint];662663jint targetActions = fSourceActions;664if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];665666// Motion: dragMotion, dragMouseMoved667DLog4(@"[CDragSource draggedImage moved]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self);668669DLog3(@" -> posting dragMotion, point %f, %f", point.x, point.y);670JNF_MEMBER_CACHE(dragMotionMethod, CDragSourceContextPeerClass, "dragMotion", "(IIII)V");671JNFCallVoidMethod(env, fDragSourceContextPeer, dragMotionMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)672673DLog3(@" -> posting dragMouseMoved, point %f, %f", point.x, point.y);674JNF_MEMBER_CACHE(dragMouseMovedMethod, CDragSourceContextPeerClass, "dragMouseMoved", "(IIII)V");675JNFCallVoidMethod(env, fDragSourceContextPeer, dragMouseMovedMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)676}677JNF_COCOA_EXIT(env);678}679680- (BOOL)ignoreModifierKeysWhileDragging {681//DLog2(@"[CDragSource ignoreModifierKeysWhileDragging]: %@\n", self);682return NO;683}684685/******************************** END NSDraggingSource Interface ********************************/686687688// postDragEnter and postDragExit are called from CDropTarget when possible and appropriate689// Currently only possible if source and target are in the same process690- (void) postDragEnter {691JNIEnv *env = [ThreadUtilities getJNIEnv];692sNeedsEnter = NO;693694jint targetActions = fSourceActions;695if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];696697NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];698DLog3(@" -> posting dragEnter, point %f, %f", point.x, point.y);699JNF_MEMBER_CACHE(dragEnterMethod, CDragSourceContextPeerClass, "dragEnter", "(IIII)V");700JNFCallVoidMethod(env, fDragSourceContextPeer, dragEnterMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)701}702703- (void) postDragExit {704JNIEnv *env = [ThreadUtilities getJNIEnv];705sNeedsEnter = YES;706707NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];708DLog3(@" -> posting dragExit, point %f, %f", point.x, point.y);709JNF_MEMBER_CACHE(dragExitMethod, CDragSourceContextPeerClass, "dragExit", "(II)V");710JNFCallVoidMethod(env, fDragSourceContextPeer, dragExitMethod, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)711}712713714// Java assumes that the origin is the top-left corner of the screen.715// Cocoa assumes that the origin is the bottom-left corner of the screen.716// Adjust the y coordinate to account for this.717// NOTE: Also need to take into account the 0 screen relative screen coords.718// This is because all screen coords in Cocoa are relative to the 0 screen.719// Also see +[CWindow convertAWTToCocoaScreenRect]720// NSScreen-to-JavaScreen mapping:721- (NSPoint) mapNSScreenPointToJavaWithOffset:(NSPoint)screenPoint {722NSRect mainR = [[[NSScreen screens] objectAtIndex:0] frame];723NSPoint point = NSMakePoint(screenPoint.x, mainR.size.height - (screenPoint.y));724725// Adjust the point with the drag image offset to get the real mouse hotspot:726// The point should remain in screen coordinates (as per DragSourceEvent.getLocation() documentation)727point.x -= fDragImageOffset.x;728point.y -= ([fDragImage size].height + fDragImageOffset.y);729730return point;731}732733@end734735736