src/macosx/native/sun/awt/CDragSource.m

Print this page




  67     sIsJavaDragging = NO;
  68 }
  69 @end
  70 
  71 JNF_CLASS_CACHE(DataTransfererClass, "sun/awt/datatransfer/DataTransferer");
  72 JNF_CLASS_CACHE(CDragSourceContextPeerClass, "sun/lwawt/macosx/CDragSourceContextPeer");
  73 JNF_CLASS_CACHE(CImageClass, "sun/lwawt/macosx/CImage");
  74 
  75 static NSDragOperation    sDragOperation;
  76 static NSPoint            sDraggingLocation;
  77 
  78 static CDragSource*        sCurrentDragSource;
  79 static BOOL                sNeedsEnter;
  80 
  81 @implementation CDragSource
  82 
  83 + (CDragSource *) currentDragSource {
  84     return sCurrentDragSource;
  85 }
  86 
  87 - (id)init:(jobject)jdragsourcecontextpeer component:(jobject)jcomponent peer:(jobject)jpeer control:(id)control
  88     transferable:(jobject)jtransferable triggerEvent:(jobject)jtrigger
  89     dragPosX:(jint)dragPosX dragPosY:(jint)dragPosY modifiers:(jint)extModifiers clickCount:(jint)clickCount
  90     timeStamp:(jlong)timeStamp cursor:(jobject)jcursor
  91     dragImage:(jobject)jnsdragimage dragImageOffsetX:(jint)jdragimageoffsetx dragImageOffsetY:(jint)jdragimageoffsety
  92     sourceActions:(jint)jsourceactions formats:(jlongArray)jformats formatMap:(jobject)jformatmap
  93 {
  94     self = [super init];
  95     DLog2(@"[CDragSource init]: %@\n", self);
  96 
  97     fView = nil;
  98     fComponent = nil;
  99 
 100     // Construct the object if we have a valid model for it:
 101     if (control != nil) {
 102         JNIEnv *env = [ThreadUtilities getJNIEnv];
 103         fComponent = JNFNewGlobalRef(env, jcomponent);
 104         fComponentPeer = JNFNewGlobalRef(env, jpeer);
 105         fDragSourceContextPeer = JNFNewGlobalRef(env, jdragsourcecontextpeer);
 106 
 107         fTransferable = JNFNewGlobalRef(env, jtransferable);
 108         fTriggerEvent = JNFNewGlobalRef(env, jtrigger);
 109         fCursor = JNFNewGlobalRef(env, jcursor);
 110 
 111         if (jnsdragimage) {
 112             JNF_MEMBER_CACHE(nsImagePtr, CImageClass, "ptr", "J");
 113             jlong imgPtr = JNFGetLongField(env, jnsdragimage, nsImagePtr);
 114             fDragImage = (NSImage*) jlong_to_ptr(imgPtr); // Double-casting prevents compiler 'd$|//
 115 
 116             [fDragImage retain];
 117         }
 118 
 119         fDragImageOffset = NSMakePoint(jdragimageoffsetx, jdragimageoffsety);
 120 
 121         fSourceActions = jsourceactions;
 122         fFormats = JNFNewGlobalRef(env, jformats);
 123         fFormatMap = JNFNewGlobalRef(env, jformatmap);
 124 
 125         fTriggerEventTimeStamp = timeStamp;
 126         fDragPos = NSMakePoint(dragPosX, dragPosY);
 127         fClickCount = clickCount;
 128         fModifiers = extModifiers;
 129 
 130         // Set this object as a dragging source:
 131 
 132         AWTView *awtView = [((NSWindow *) control) contentView];
 133         fView = [awtView retain];
 134         [awtView setDragSource:self];
 135 
 136         // Let AWTEvent know Java drag is getting underway:
 137         [NSEvent javaDraggingBegin];
 138     }
 139 
 140     else {
 141         [self release];
 142         self = nil;
 143     }
 144 
 145     return self;
 146 }
 147 
 148 - (void)removeFromView:(JNIEnv *)env
 149 {
 150     DLog2(@"[CDragSource removeFromView]: %@\n", self);
 151 
 152     // Remove this dragging source from the view:
 153     [((AWTView *) fView) setDragSource:nil];
 154 
 155     // Clean up JNI refs
 156     if (fComponent != NULL) {
 157         JNFDeleteGlobalRef(env, fComponent);
 158         fComponent = NULL;
 159     }
 160 
 161     if (fComponentPeer != NULL) {
 162         JNFDeleteGlobalRef(env, fComponentPeer);
 163         fComponentPeer = NULL;
 164     }
 165 
 166     if (fDragSourceContextPeer != NULL) {
 167         JNFDeleteGlobalRef(env, fDragSourceContextPeer);
 168         fDragSourceContextPeer = NULL;
 169     }
 170 
 171     if (fTransferable != NULL) {
 172         JNFDeleteGlobalRef(env, fTransferable);
 173         fTransferable = NULL;
 174     }
 175 
 176     if (fTriggerEvent != NULL) {
 177         JNFDeleteGlobalRef(env, fTriggerEvent);
 178         fTriggerEvent = NULL;
 179     }
 180 
 181     if (fCursor != NULL) {
 182         JNFDeleteGlobalRef(env, fCursor);
 183         fCursor = NULL;
 184     }
 185 
 186     if (fFormats != NULL) {
 187         JNFDeleteGlobalRef(env, fFormats);
 188         fFormats = NULL;
 189     }
 190 
 191     if (fFormatMap != NULL) {
 192         JNFDeleteGlobalRef(env, fFormatMap);
 193         fFormatMap = NULL;
 194     }
 195 
 196     CFRelease(self); // GC
 197 }
 198 
 199 - (void)dealloc
 200 {
 201     DLog2(@"[CDragSource dealloc]: %@\n", self);
 202 
 203     // Delete or release local data:
 204     [fView release];
 205     fView = nil;


 569                 JNF_MEMBER_CACHE(resetHoveringMethod, CDragSourceContextPeerClass, "resetHovering", "()V");
 570         JNFCallVoidMethod(env, fDragSourceContextPeer, resetHoveringMethod); // Hust reset static variable
 571     } @finally {
 572         // Clear the current DragSource
 573         sCurrentDragSource = nil;
 574         sNeedsEnter = NO;
 575     }
 576 
 577     // We have to do this, otherwise AppKit doesn't know we're finished dragging. Yup, it's that bad.
 578     if ([[[NSRunLoop currentRunLoop] currentMode] isEqualTo:NSEventTrackingRunLoopMode]) {
 579         [NSApp postEvent:[self nsDragEvent:NO] atStart:YES];
 580     }
 581 
 582     DLog2(@"[CDragSource doDrag] end: %@\n", self);
 583 }
 584 
 585 - (void)drag
 586 {
 587     AWT_ASSERT_NOT_APPKIT_THREAD;
 588 
 589     // Set the drag cursor (or not 3839999)
 590     //JNIEnv *env = [ThreadUtilities getJNIEnv];
 591     //jobject gCursor = JNFNewGlobalRef(env, fCursor);
 592     //[EventFactory setJavaCursor:gCursor withEnv:env];
 593 
 594     [self performSelectorOnMainThread:@selector(doDrag) withObject:nil waitUntilDone:YES]; // AWT_THREADING Safe (called from unique asynchronous thread)
 595 }
 596 
 597 /********************************  BEGIN NSDraggingSource Interface  ********************************/
 598 
 599 - (void)draggingOperationChanged:(NSDragOperation)dragOp {
 600     //DLog2(@"[CDragSource draggingOperationChanged]: %@\n", self);
 601 
 602     JNIEnv* env = [ThreadUtilities getJNIEnv];
 603 
 604     jint targetActions = fSourceActions;
 605     if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];
 606 
 607     NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
 608     DLog3(@"  -> posting operationChanged, point %f, %f", point.x, point.y);
 609     jint modifiedModifiers = fDragKeyModifiers | fDragMouseModifiers | [DnDUtilities javaKeyModifiersForNSDragOperation:dragOp];
 610 
 611     JNF_MEMBER_CACHE(operationChangedMethod, CDragSourceContextPeerClass, "operationChanged", "(IIII)V");
 612     JNFCallVoidMethod(env, fDragSourceContextPeer, operationChangedMethod, targetActions, modifiedModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
 613 }




  67     sIsJavaDragging = NO;
  68 }
  69 @end
  70 
  71 JNF_CLASS_CACHE(DataTransfererClass, "sun/awt/datatransfer/DataTransferer");
  72 JNF_CLASS_CACHE(CDragSourceContextPeerClass, "sun/lwawt/macosx/CDragSourceContextPeer");
  73 JNF_CLASS_CACHE(CImageClass, "sun/lwawt/macosx/CImage");
  74 
  75 static NSDragOperation    sDragOperation;
  76 static NSPoint            sDraggingLocation;
  77 
  78 static CDragSource*        sCurrentDragSource;
  79 static BOOL                sNeedsEnter;
  80 
  81 @implementation CDragSource
  82 
  83 + (CDragSource *) currentDragSource {
  84     return sCurrentDragSource;
  85 }
  86 
  87 - (id)init:(jobject)jdragsourcecontextpeer component:(jobject)jcomponent control:(id)control
  88     transferable:(jobject)jtransferable triggerEvent:(jobject)jtrigger
  89     dragPosX:(jint)dragPosX dragPosY:(jint)dragPosY modifiers:(jint)extModifiers clickCount:(jint)clickCount
  90     timeStamp:(jlong)timeStamp
  91     dragImage:(jobject)jnsdragimage dragImageOffsetX:(jint)jdragimageoffsetx dragImageOffsetY:(jint)jdragimageoffsety
  92     sourceActions:(jint)jsourceactions formats:(jlongArray)jformats formatMap:(jobject)jformatmap
  93 {
  94     self = [super init];
  95     DLog2(@"[CDragSource init]: %@\n", self);
  96 
  97     fView = nil;
  98     fComponent = nil;
  99 
 100     // Construct the object if we have a valid model for it:
 101     if (control != nil) {
 102         JNIEnv *env = [ThreadUtilities getJNIEnv];
 103         fComponent = JNFNewGlobalRef(env, jcomponent);

 104         fDragSourceContextPeer = JNFNewGlobalRef(env, jdragsourcecontextpeer);
 105 
 106         fTransferable = JNFNewGlobalRef(env, jtransferable);
 107         fTriggerEvent = JNFNewGlobalRef(env, jtrigger);

 108 
 109         if (jnsdragimage) {
 110             JNF_MEMBER_CACHE(nsImagePtr, CImageClass, "ptr", "J");
 111             jlong imgPtr = JNFGetLongField(env, jnsdragimage, nsImagePtr);
 112             fDragImage = (NSImage*) jlong_to_ptr(imgPtr); // Double-casting prevents compiler 'd$|//
 113 
 114             [fDragImage retain];
 115         }
 116 
 117         fDragImageOffset = NSMakePoint(jdragimageoffsetx, jdragimageoffsety);
 118 
 119         fSourceActions = jsourceactions;
 120         fFormats = JNFNewGlobalRef(env, jformats);
 121         fFormatMap = JNFNewGlobalRef(env, jformatmap);
 122 
 123         fTriggerEventTimeStamp = timeStamp;
 124         fDragPos = NSMakePoint(dragPosX, dragPosY);
 125         fClickCount = clickCount;
 126         fModifiers = extModifiers;
 127 
 128         // Set this object as a dragging source:
 129 
 130         fView = [(AWTView *) control retain];
 131         [fView setDragSource:self];

 132 
 133         // Let AWTEvent know Java drag is getting underway:
 134         [NSEvent javaDraggingBegin];
 135     }
 136 
 137     else {
 138         [self release];
 139         self = nil;
 140     }
 141 
 142     return self;
 143 }
 144 
 145 - (void)removeFromView:(JNIEnv *)env
 146 {
 147     DLog2(@"[CDragSource removeFromView]: %@\n", self);
 148 
 149     // Remove this dragging source from the view:
 150     [((AWTView *) fView) setDragSource:nil];
 151 
 152     // Clean up JNI refs
 153     if (fComponent != NULL) {
 154         JNFDeleteGlobalRef(env, fComponent);
 155         fComponent = NULL;
 156     }
 157 





 158     if (fDragSourceContextPeer != NULL) {
 159         JNFDeleteGlobalRef(env, fDragSourceContextPeer);
 160         fDragSourceContextPeer = NULL;
 161     }
 162 
 163     if (fTransferable != NULL) {
 164         JNFDeleteGlobalRef(env, fTransferable);
 165         fTransferable = NULL;
 166     }
 167 
 168     if (fTriggerEvent != NULL) {
 169         JNFDeleteGlobalRef(env, fTriggerEvent);
 170         fTriggerEvent = NULL;
 171     }
 172 





 173     if (fFormats != NULL) {
 174         JNFDeleteGlobalRef(env, fFormats);
 175         fFormats = NULL;
 176     }
 177 
 178     if (fFormatMap != NULL) {
 179         JNFDeleteGlobalRef(env, fFormatMap);
 180         fFormatMap = NULL;
 181     }
 182 
 183     CFRelease(self); // GC
 184 }
 185 
 186 - (void)dealloc
 187 {
 188     DLog2(@"[CDragSource dealloc]: %@\n", self);
 189 
 190     // Delete or release local data:
 191     [fView release];
 192     fView = nil;


 556                 JNF_MEMBER_CACHE(resetHoveringMethod, CDragSourceContextPeerClass, "resetHovering", "()V");
 557         JNFCallVoidMethod(env, fDragSourceContextPeer, resetHoveringMethod); // Hust reset static variable
 558     } @finally {
 559         // Clear the current DragSource
 560         sCurrentDragSource = nil;
 561         sNeedsEnter = NO;
 562     }
 563 
 564     // We have to do this, otherwise AppKit doesn't know we're finished dragging. Yup, it's that bad.
 565     if ([[[NSRunLoop currentRunLoop] currentMode] isEqualTo:NSEventTrackingRunLoopMode]) {
 566         [NSApp postEvent:[self nsDragEvent:NO] atStart:YES];
 567     }
 568 
 569     DLog2(@"[CDragSource doDrag] end: %@\n", self);
 570 }
 571 
 572 - (void)drag
 573 {
 574     AWT_ASSERT_NOT_APPKIT_THREAD;
 575 





 576     [self performSelectorOnMainThread:@selector(doDrag) withObject:nil waitUntilDone:YES]; // AWT_THREADING Safe (called from unique asynchronous thread)
 577 }
 578 
 579 /********************************  BEGIN NSDraggingSource Interface  ********************************/
 580 
 581 - (void)draggingOperationChanged:(NSDragOperation)dragOp {
 582     //DLog2(@"[CDragSource draggingOperationChanged]: %@\n", self);
 583 
 584     JNIEnv* env = [ThreadUtilities getJNIEnv];
 585 
 586     jint targetActions = fSourceActions;
 587     if ([CDropTarget currentDropTarget]) targetActions = [[CDropTarget currentDropTarget] currentJavaActions];
 588 
 589     NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
 590     DLog3(@"  -> posting operationChanged, point %f, %f", point.x, point.y);
 591     jint modifiedModifiers = fDragKeyModifiers | fDragMouseModifiers | [DnDUtilities javaKeyModifiersForNSDragOperation:dragOp];
 592 
 593     JNF_MEMBER_CACHE(operationChangedMethod, CDragSourceContextPeerClass, "operationChanged", "(IIII)V");
 594     JNFCallVoidMethod(env, fDragSourceContextPeer, operationChangedMethod, targetActions, modifiedModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
 595 }