1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
|
/*
GSDragView - Generic Drag and Drop code.
Copyright (C) 2004 Free Software Foundation, Inc.
Author: Fred Kiefer <fredkiefer@gmx.de>
Date: May 2004
Based on X11 specific code from:
Created by: Wim Oudshoorn <woudshoo@xs4all.nl>
Date: Nov 2001
Written by: Adam Fedor <fedor@gnu.org>
Date: Nov 1998
This file is part of the GNU Objective C User Interface Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSThread.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSCell.h"
#import "AppKit/NSCursor.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSView.h"
#import "AppKit/NSWindow.h"
#import "GNUstepGUI/GSDisplayServer.h"
#import "GNUstepGUI/GSDragView.h"
#include <math.h>
/* Size of the dragged window */
#define DWZ 48
#define SLIDE_TIME_STEP .02 /* in seconds */
#define SLIDE_NR_OF_STEPS 20
@interface GSRawWindow : NSWindow
@end
@interface NSApplication (GNUstepPrivate)
- (void) _postAndSendEvent: (NSEvent *)anEvent;
@end
@interface NSCursor (BackendPrivate)
- (void *)_cid;
- (void) _setCid: (void *)val;
@end
@interface GSDragView (Private)
- (void) _setupWindowFor: (NSImage*)anImage
mousePosition: (NSPoint)mPoint
imagePosition: (NSPoint)iPoint;
- (void) _clearupWindow;
- (BOOL) _updateOperationMask: (NSEvent*) theEvent;
- (void) _setCursor;
- (void) _sendLocalEvent: (GSAppKitSubtype)subtype
action: (NSDragOperation)action
position: (NSPoint)eventLocation
timestamp: (NSTimeInterval)time
toWindow: (NSWindow*)dWindow;
- (void) _handleDrag: (NSEvent*)theEvent slidePoint: (NSPoint)slidePoint;
- (void) _handleEventDuringDragging: (NSEvent *)theEvent;
- (void) _updateAndMoveImageToCorrectPosition;
- (void) _moveDraggedImageToNewPosition;
- (void) _slideDraggedImageTo: (NSPoint)screenPoint
numberOfSteps: (int) steps
delay: (float) delay
waitAfterSlide: (BOOL) waitFlag;
@end
@implementation GSRawWindow
- (BOOL) canBecomeMainWindow
{
return NO;
}
- (BOOL) canBecomeKeyWindow
{
return NO;
}
- (void) _initDefaults
{
[super _initDefaults];
[self setReleasedWhenClosed: NO];
[self setExcludedFromWindowsMenu: YES];
}
- (void) orderWindow: (NSWindowOrderingMode)place relativeTo: (NSInteger)otherWin
{
[super orderWindow: place relativeTo: otherWin];
[self setLevel: NSPopUpMenuWindowLevel];
}
@end
@implementation GSDragView
static GSDragView *sharedDragView = nil;
+ (id) sharedDragView
{
if (sharedDragView == nil)
{
sharedDragView = [GSDragView new];
}
return sharedDragView;
}
+ (Class) windowClass
{
return [GSRawWindow class];
}
- (id) init
{
self = [super init];
if (self != nil)
{
NSRect winRect = {{0, 0}, {DWZ, DWZ}};
NSWindow *sharedDragWindow = [[[object_getClass(self) windowClass] alloc]
initWithContentRect: winRect
styleMask: NSBorderlessWindowMask
backing: NSBackingStoreNonretained
defer: NO];
dragCell = [[NSCell alloc] initImageCell: nil];
[dragCell setBordered: NO];
[sharedDragWindow setContentView: self];
[sharedDragWindow setBackgroundColor: [NSColor clearColor]];
// Kept alive by the window
RELEASE(self);
}
return self;
}
- (void) dealloc
{
[super dealloc];
RELEASE(cursors);
}
/* NSDraggingInfo protocol */
- (NSWindow*) draggingDestinationWindow
{
return destWindow;
}
- (NSPoint) draggingLocation
{
return dragPoint;
}
- (NSPasteboard*) draggingPasteboard
{
return dragPasteboard;
}
- (NSInteger) draggingSequenceNumber
{
return dragSequence;
}
- (id) draggingSource
{
return dragSource;
}
- (NSDragOperation) draggingSourceOperationMask
{
// Mix in possible modifiers
return dragMask & operationMask;
}
- (NSImage*) draggedImage
{
if (dragSource)
return [dragCell image];
else
return nil;
}
- (NSPoint) draggedImageLocation
{
NSPoint loc = dragPoint;
if (dragSource)
{
loc.x -= offset.width;
loc.y -= offset.height;
}
return loc;
}
- (NSArray *) namesOfPromisedFilesDroppedAtDestination: (NSURL *)dropDestination
{
if ([dragSource respondsToSelector:
@selector(namesOfPromisedFilesDroppedAtDestination:)])
{
return [dragSource namesOfPromisedFilesDroppedAtDestination:
dropDestination];
}
else
{
return nil;
}
}
- (BOOL) isDragging
{
return isDragging;
}
- (void) drawRect: (NSRect)rect
{
[dragCell drawWithFrame: [self frame] inView: self];
}
- (void) dragImage: (NSImage*)anImage
at: (NSPoint)screenLocation
offset: (NSSize)initialOffset
event: (NSEvent*)event
pasteboard: (NSPasteboard*)pboard
source: (id)sourceObject
slideBack: (BOOL)slideFlag
{
NSPoint eventPoint;
NSPoint imagePoint;
ASSIGN(dragPasteboard, pboard);
ASSIGN(dragSource, sourceObject);
dragSequence = [event timestamp];
slideBack = slideFlag;
// Unset the target window
targetWindowRef = 0;
targetMask = NSDragOperationEvery;
destExternal = NO;
NSDebugLLog(@"NSDragging", @"Start drag with %@", [pboard types]);
/*
* The position of the mouse is the event location plus any offset
* provided. We convert this from window coordinates to screen
* coordinates.
*/
eventPoint = [event locationInWindow];
eventPoint = [[event window] convertBaseToScreen: eventPoint];
eventPoint.x += initialOffset.width;
eventPoint.y += initialOffset.height;
/*
* Adjust image location to match the mose adjustment.
*/
imagePoint = screenLocation;
imagePoint.x += initialOffset.width;
imagePoint.y += initialOffset.height;
[self _setupWindowFor: anImage
mousePosition: eventPoint
imagePosition: imagePoint];
isDragging = YES;
[self _handleDrag: event slidePoint: screenLocation];
isDragging = NO;
DESTROY(dragSource);
DESTROY(dragPasteboard);
}
- (void) slideDraggedImageTo: (NSPoint)point
{
float distx = point.x - dragPosition.x;
float disty = point.y - dragPosition.y;
float dist = sqrt((distx * distx) + (disty * disty));
NSSize imgSize = [[dragCell image] size];
float imgDist = sqrt((imgSize.width * imgSize.width) +
(imgSize.height * imgSize.height));
int steps = (int)(dist/imgDist);
/*
* Convert point from coordinates of image to coordinates of mouse
* cursor for internal use.
*/
point.x += offset.width;
point.y += offset.height;
/* [self _slideDraggedImageTo: point
numberOfSteps: SLIDE_NR_OF_STEPS
delay: SLIDE_TIME_STEP
waitAfterSlide: YES];*/
[self _slideDraggedImageTo: point
numberOfSteps: steps
delay: SLIDE_TIME_STEP
waitAfterSlide: YES];
}
/*
Called by NSWindow. Sends drag events to external sources
*/
- (void) postDragEvent: (NSEvent *)theEvent
{
if ([theEvent subtype] == GSAppKitDraggingStatus)
{
NSDragOperation action = [theEvent data2];
if (destExternal)
{
}
else
{
if (action != targetMask)
{
targetMask = action;
[self _setCursor];
}
}
}
}
- (void) sendExternalEvent: (GSAppKitSubtype)subtype
action: (NSDragOperation)action
position: (NSPoint)eventLocation
timestamp: (NSTimeInterval)time
toWindow: (int)dWindowNumber
{
}
/*
Return the window that lies below the cursor and accepts drag and drop.
In mouseWindowRef the OS reference for this window is returned, this is even
set, if there is a native window, but no GNUstep window at this location.
*/
- (NSWindow*) windowAcceptingDnDunder: (NSPoint)mouseLocation
windowRef: (int*)mouseWindowRef
{
NSInteger win;
*mouseWindowRef = 0;
win = [GSServerForWindow(_window) findWindowAt: mouseLocation
windowRef: mouseWindowRef
excluding: [_window windowNumber]];
return GSWindowWithNumber(win);
}
@end
@implementation GSDragView (Private)
/*
Method to initialize the dragview before it is put on the screen.
It only initializes the instance variables that have to do with
moving the image over the screen and variables that are used
to keep track where we are.
So it is typically used just before the dragview is actually displayed.
Post conditions:
- dragCell is initialized with the image to drag.
- all instance variables pertaining to moving the window are initialized
*/
- (void) _setupWindowFor: (NSImage*)anImage
mousePosition: (NSPoint)mPoint
imagePosition: (NSPoint)iPoint
{
NSSize imageSize;
if (anImage == nil)
{
anImage = [NSImage imageNamed: @"common_Close"];
}
imageSize = [anImage size];
[dragCell setImage: anImage];
/* setup the coordinates, used for moving the view around */
dragPosition = mPoint;
newPosition = mPoint;
offset.width = mPoint.x - iPoint.x;
offset.height = mPoint.y - iPoint.y;
[_window setFrame:
NSMakeRect (iPoint.x, iPoint.y, imageSize.width, imageSize.height)
display: NO];
// Only display the image
[GSServerForWindow(_window) restrictWindow: [_window windowNumber]
toImage: [dragCell image]];
[_window orderFront: nil];
}
- (void) _clearupWindow
{
[_window setFrame: NSZeroRect display: NO];
[_window orderOut: nil];
}
/*
updates the operationMask by examining modifier keys
pressed during -theEvent-.
If the current value of operationMask == NSDragOperationIgnoresModifiers
it will return immediately without updating the operationMask
This method will return YES if the operationMask
is changed, NO if it is still the same.
*/
- (BOOL) _updateOperationMask: (NSEvent*) theEvent
{
NSUInteger mod = [theEvent modifierFlags];
NSDragOperation oldOperationMask = operationMask;
if (operationMask == NSDragOperationIgnoresModifiers)
{
return NO;
}
if (mod & NSControlKeyMask)
{
operationMask = NSDragOperationLink;
}
else if (mod & NSAlternateKeyMask)
{
operationMask = NSDragOperationCopy;
}
else if (mod & NSCommandKeyMask)
{
operationMask = NSDragOperationGeneric;
}
else
{
operationMask = NSDragOperationEvery;
}
return (operationMask != oldOperationMask);
}
/**
_setCursor examines the state of the dragging and update
the cursor accordingly. It will not save the current cursor,
if you want to keep the original you have to save it yourself.
The code recogines 4 cursors:
- NONE - when the source does not allow dragging
- COPY - when the current operation is ONLY Copy
- LINK - when the current operation is ONLY Link
- GENERIC - all other cases
And two colors
- GREEN - when the target accepts the drop
- BLACK - when the target does not accept the drop
Note that the code to figure out which of the 4 cursor to use
depends on the fact that
{NSDragOperationNone, NSDragOperationCopy, NSDragOperationLink} = {0, 1, 2}
*/
- (void) _setCursor
{
NSCursor *newCursor;
NSString *name;
NSString *iname;
NSDragOperation mask;
mask = dragMask & operationMask;
if (targetWindowRef != 0)
mask &= targetMask;
NSDebugLLog (@"NSDragging",
@"drag, operation, target mask = (%x, %x, %x), dnd aware = %d\n",
(unsigned int)dragMask, (unsigned int)operationMask, (unsigned int)targetMask,
(targetWindowRef != 0));
if (cursors == nil)
cursors = RETAIN([NSMutableDictionary dictionary]);
name = nil;
newCursor = nil;
iname = nil;
switch (mask)
{
case NSDragOperationNone:
name = @"NoCursor";
iname = @"common_noCursor";
break;
case NSDragOperationCopy:
name = @"CopyCursor";
iname = @"common_copyCursor";
break;
case NSDragOperationLink:
name = @"LinkCursor";
iname = @"common_linkCursor";
break;
case NSDragOperationGeneric:
break;
default:
// FIXME: Should not happen, add warning?
break;
}
if (name != nil)
{
newCursor = [cursors objectForKey: name];
if (newCursor == nil)
{
NSImage *image = [NSImage imageNamed: iname];
newCursor = [[NSCursor alloc] initWithImage: image];
[cursors setObject: newCursor forKey: name];
RELEASE(newCursor);
}
}
if (newCursor == nil)
{
name = @"ArrowCursor";
newCursor = [cursors objectForKey: name];
if (newCursor == nil)
{
/* Make our own arrow cursor, since we want to color it */
void *c;
newCursor = [[NSCursor alloc] initWithImage: nil];
[GSCurrentServer() standardcursor: GSArrowCursor : &c];
[newCursor _setCid: c];
[cursors setObject: newCursor forKey: name];
RELEASE(newCursor);
}
}
if ((targetWindowRef != 0) && mask != NSDragOperationNone)
{
[GSCurrentServer() recolorcursor: [NSColor greenColor]
: [NSColor blackColor]
: [newCursor _cid]];
}
else
{
[GSCurrentServer() recolorcursor: [NSColor blackColor]
: [NSColor whiteColor]
: [newCursor _cid]];
}
[newCursor set];
}
- (void) _sendLocalEvent: (GSAppKitSubtype)subtype
action: (NSDragOperation)action
position: (NSPoint)eventLocation
timestamp: (NSTimeInterval)time
toWindow: (NSWindow*)dWindow
{
NSEvent *e;
NSGraphicsContext *context = GSCurrentContext();
// FIXME: Should store this once
NSInteger dragWindowRef = (NSInteger)(intptr_t)[GSServerForWindow(_window) windowDevice: [_window windowNumber]];
eventLocation = [dWindow convertScreenToBase: eventLocation];
e = [NSEvent otherEventWithType: NSAppKitDefined
location: eventLocation
modifierFlags: 0
timestamp: time
windowNumber: [dWindow windowNumber]
context: context
subtype: subtype
data1: dragWindowRef
data2: action];
[NSApp _postAndSendEvent: e];
}
/*
The dragging support works by hijacking the NSApp event loop.
- this function loops until the dragging operation is finished
and consumes all NSEvents during the drag operation.
- It sets up periodic events. The drawing and communication
with DraggingSource and DraggingTarget is handled in the
periodic event code. The use of periodic events is purely
a performance improvement. If no periodic events are used
the system can not process them all on time.
At least on a 333Mhz laptop, using fairly simple
DraggingTarget code.
PROBLEMS:
- No autoreleasePools are created. So long drag operations can consume
memory
- It seems that sometimes a periodic event get lost.
*/
- (void) _handleDrag: (NSEvent*)theEvent slidePoint: (NSPoint)slidePoint
{
// Caching some often used values. These values do not
// change in this method.
// Use eWindow for coordination transformation
NSWindow *eWindow = [theEvent window];
NSDate *theDistantFuture = [NSDate distantFuture];
NSUInteger eventMask = NSLeftMouseDownMask | NSLeftMouseUpMask
| NSLeftMouseDraggedMask | NSMouseMovedMask
| NSPeriodicMask | NSAppKitDefinedMask | NSFlagsChangedMask;
NSPoint startPoint;
// Storing values, to restore after we have finished.
NSCursor *cursorBeforeDrag = [NSCursor currentCursor];
BOOL deposited;
startPoint = [eWindow convertBaseToScreen: [theEvent locationInWindow]];
startPoint.x -= offset.width;
startPoint.y -= offset.height;
NSDebugLLog(@"NSDragging", @"Drag window origin %@\n", NSStringFromPoint(startPoint));
// Notify the source that dragging has started
if ([dragSource respondsToSelector:
@selector(draggedImage:beganAt:)])
{
[dragSource draggedImage: [self draggedImage]
beganAt: startPoint];
}
// --- Setup up the masks for the drag operation ---------------------
if ([dragSource respondsToSelector:
@selector(ignoreModifierKeysWhileDragging)]
&& [dragSource ignoreModifierKeysWhileDragging])
{
operationMask = NSDragOperationIgnoresModifiers;
}
else
{
operationMask = 0;
[self _updateOperationMask: theEvent];
}
if ([dragSource respondsToSelector:
@selector(draggingSourceOperationMaskForLocal:)])
{
dragMask = [dragSource draggingSourceOperationMaskForLocal: !destExternal];
}
else
{
dragMask = NSDragOperationCopy | NSDragOperationLink |
NSDragOperationGeneric | NSDragOperationPrivate;
}
// --- Setup the event loop ------------------------------------------
[self _updateAndMoveImageToCorrectPosition];
[NSEvent startPeriodicEventsAfterDelay: 0.02 withPeriod: 0.03];
// --- Loop that handles all events during drag operation -----------
while ([theEvent type] != NSLeftMouseUp)
{
[self _handleEventDuringDragging: theEvent];
theEvent = [NSApp nextEventMatchingMask: eventMask
untilDate: theDistantFuture
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
}
// --- Event loop for drag operation stopped ------------------------
[NSEvent stopPeriodicEvents];
[self _updateAndMoveImageToCorrectPosition];
NSDebugLLog(@"NSDragging", @"dnd ending %d\n", targetWindowRef);
// --- Deposit the drop ----------------------------------------------
if ((targetWindowRef != 0)
&& ((targetMask & dragMask & operationMask) != NSDragOperationNone))
{
/* FIXME:
* We remove the dragged image from the screen before
* sending the dnd drop event to the destination.
* This code should actually be rewritten, because
* the depositing of the drop consist of three steps
* - prepareForDragOperation
* - performDragOperation
* - concludeDragOperation.
* The dragged image should be removed from the screen
* between the prepare and the perform operation.
* The three steps are now executed in the NSWindow class
* and the NSWindow class does not have access to
* the image.
*/
[self _clearupWindow];
[cursorBeforeDrag set];
NSDebugLLog(@"NSDragging", @"sending dnd drop\n");
if (!destExternal)
{
[self _sendLocalEvent: GSAppKitDraggingDrop
action: 0
position: dragPosition
timestamp: [theEvent timestamp]
toWindow: destWindow];
}
else
{
[self sendExternalEvent: GSAppKitDraggingDrop
action: 0
position: dragPosition
timestamp: [theEvent timestamp]
toWindow: targetWindowRef];
}
deposited = YES;
}
else
{
if (slideBack)
{
[self slideDraggedImageTo: slidePoint];
}
[self _clearupWindow];
[cursorBeforeDrag set];
deposited = NO;
}
if ([dragSource respondsToSelector:
@selector(draggedImage:endedAt:operation:)])
{
NSPoint point;
point = [theEvent locationInWindow];
// Convert from mouse cursor coordinate to image coordinate
point.x -= offset.width;
point.y -= offset.height;
point = [[theEvent window] convertBaseToScreen: point];
[dragSource draggedImage: [self draggedImage]
endedAt: point
operation: targetMask & dragMask & operationMask];
}
else if ([dragSource respondsToSelector:
@selector(draggedImage:endedAt:deposited:)])
{
NSPoint point;
point = [theEvent locationInWindow];
// Convert from mouse cursor coordinate to image coordinate
point.x -= offset.width;
point.y -= offset.height;
point = [[theEvent window] convertBaseToScreen: point];
[dragSource draggedImage: [self draggedImage]
endedAt: point
deposited: deposited];
}
}
/*
* Handle the events for the event loop during drag and drop
*/
- (void) _handleEventDuringDragging: (NSEvent *)theEvent
{
switch ([theEvent type])
{
case NSAppKitDefined:
{
GSAppKitSubtype sub = [theEvent subtype];
switch (sub)
{
case GSAppKitWindowMoved:
case GSAppKitWindowResized:
case GSAppKitRegionExposed:
/*
* Keep window up-to-date with its current position.
*/
[NSApp sendEvent: theEvent];
break;
case GSAppKitDraggingStatus:
NSDebugLLog(@"NSDragging", @"got GSAppKitDraggingStatus\n");
if ((int)[theEvent data1] == targetWindowRef)
{
NSDragOperation newTargetMask = (NSDragOperation)[theEvent data2];
if (newTargetMask != targetMask)
{
targetMask = newTargetMask;
[self _setCursor];
}
}
break;
case GSAppKitDraggingFinished:
NSLog(@"Internal: got GSAppKitDraggingFinished out of seq");
break;
case GSAppKitWindowFocusIn:
case GSAppKitWindowFocusOut:
case GSAppKitWindowLeave:
case GSAppKitWindowEnter:
break;
default:
NSDebugLLog(@"NSDragging", @"dropped NSAppKitDefined (%d) event", sub);
break;
}
}
break;
case NSMouseMoved:
case NSLeftMouseDragged:
case NSLeftMouseDown:
case NSLeftMouseUp:
newPosition = [[theEvent window] convertBaseToScreen:
[theEvent locationInWindow]];
break;
case NSFlagsChanged:
if ([self _updateOperationMask: theEvent])
{
// If flags change, send update to allow
// destination to take note.
if (destWindow)
{
[self _sendLocalEvent: GSAppKitDraggingUpdate
action: dragMask & operationMask
position: newPosition
timestamp: [theEvent timestamp]
toWindow: destWindow];
}
else
{
[self sendExternalEvent: GSAppKitDraggingUpdate
action: dragMask & operationMask
position: newPosition
timestamp: [theEvent timestamp]
toWindow: targetWindowRef];
}
[self _setCursor];
}
break;
case NSPeriodic:
newPosition = [NSEvent mouseLocation];
if (newPosition.x != dragPosition.x || newPosition.y != dragPosition.y)
{
[self _updateAndMoveImageToCorrectPosition];
}
else if (destWindow)
{
[self _sendLocalEvent: GSAppKitDraggingUpdate
action: dragMask & operationMask
position: newPosition
timestamp: [theEvent timestamp]
toWindow: destWindow];
}
else
{
[self sendExternalEvent: GSAppKitDraggingUpdate
action: dragMask & operationMask
position: newPosition
timestamp: [theEvent timestamp]
toWindow: targetWindowRef];
}
break;
default:
NSLog(@"Internal: dropped event (%d) during dragging", (int)[theEvent type]);
}
}
/*
* This method will move the drag image and update all associated data
*/
- (void) _updateAndMoveImageToCorrectPosition
{
//--- Store old values -----------------------------------------------------
NSWindow *oldDestWindow = destWindow;
BOOL oldDestExternal = destExternal;
int mouseWindowRef;
BOOL changeCursor = NO;
//--- Move drag image to the new position -----------------------------------
[self _moveDraggedImageToNewPosition];
if ([dragSource respondsToSelector:
@selector(draggedImage:movedTo:)])
{
[dragSource draggedImage: [self draggedImage] movedTo: dragPosition];
}
//--- Determine target window ---------------------------------------------
destWindow = [self windowAcceptingDnDunder: dragPosition
windowRef: &mouseWindowRef];
// If we are not hovering above a window that we own
// we are dragging to an external application.
destExternal = (mouseWindowRef != 0) && (destWindow == nil);
if (destWindow != nil)
{
dragPoint = [destWindow convertScreenToBase: dragPosition];
}
NSDebugLLog(@"NSDragging", @"mouse window %d (%@) at %@\n",
mouseWindowRef, destWindow, NSStringFromPoint(dragPosition));
//--- send exit message if necessary -------------------------------------
if ((mouseWindowRef != targetWindowRef) && targetWindowRef)
{
/* If we change windows and the old window is dnd aware, we send an
dnd exit */
NSDebugLLog(@"NSDragging", @"sending dnd exit\n");
if (oldDestWindow != nil)
{
[self _sendLocalEvent: GSAppKitDraggingExit
action: dragMask & operationMask
position: NSZeroPoint
timestamp: dragSequence
toWindow: oldDestWindow];
}
else
{
[self sendExternalEvent: GSAppKitDraggingExit
action: dragMask & operationMask
position: NSZeroPoint
timestamp: dragSequence
toWindow: targetWindowRef];
}
}
// Reset drag mask when we switch from external to internal or back
if (oldDestExternal != destExternal)
{
NSDragOperation newMask;
if ([dragSource respondsToSelector:
@selector(draggingSourceOperationMaskForLocal:)])
{
newMask = [dragSource draggingSourceOperationMaskForLocal: !destExternal];
}
else
{
newMask = NSDragOperationCopy | NSDragOperationLink |
NSDragOperationGeneric | NSDragOperationPrivate;
}
if (newMask != dragMask)
{
dragMask = newMask;
changeCursor = YES;
}
}
if (mouseWindowRef == targetWindowRef && targetWindowRef)
{
// same window, sending update
NSDebugLLog(@"NSDragging", @"sending dnd pos\n");
// FIXME: We should only send this when the destination wantsPeriodicDraggingUpdates
if (destWindow != nil)
{
[self _sendLocalEvent: GSAppKitDraggingUpdate
action: dragMask & operationMask
position: dragPosition
timestamp: dragSequence
toWindow: destWindow];
}
else
{
[self sendExternalEvent: GSAppKitDraggingUpdate
action: dragMask & operationMask
position: dragPosition
timestamp: dragSequence
toWindow: targetWindowRef];
}
}
else if (mouseWindowRef != 0)
{
// FIXME: We might force the cursor update here, if the
// target wants to change the cursor.
NSDebugLLog(@"NSDragging", @"sending dnd enter/pos\n");
if (destWindow != nil)
{
[self _sendLocalEvent: GSAppKitDraggingEnter
action: dragMask
position: dragPosition
timestamp: dragSequence
toWindow: destWindow];
}
else
{
[self sendExternalEvent: GSAppKitDraggingEnter
action: dragMask
position: dragPosition
timestamp: dragSequence
toWindow: mouseWindowRef];
}
}
if (targetWindowRef != mouseWindowRef)
{
targetWindowRef = mouseWindowRef;
changeCursor = YES;
}
if (changeCursor)
{
[self _setCursor];
}
}
/*
* Move the dragged image immediately to the position indicated by
* the instance variable newPosition.
*
* In doing so it will update the dragPosition instance variables.
*/
- (void) _moveDraggedImageToNewPosition
{
dragPosition = newPosition;
[GSServerForWindow(_window) movewindow:
NSMakePoint(newPosition.x - offset.width, newPosition.y - offset.height)
: [_window windowNumber]];
}
/*
* NB. screenPoint here is the position of the mouse cursor.
*/
- (void) _slideDraggedImageTo: (NSPoint)screenPoint
numberOfSteps: (int)steps
delay: (float)delay
waitAfterSlide: (BOOL)waitFlag
{
/* If we do not need multiple redrawing, just move the image immediately
* to its desired spot.
*/
if (steps < 2)
{
newPosition = screenPoint;
[self _moveDraggedImageToNewPosition];
}
else
{
[NSEvent startPeriodicEventsAfterDelay: delay withPeriod: delay];
// Use the event loop to redraw the image repeatedly.
// Using the event loop to allow the application to process
// expose events.
while (steps)
{
NSEvent *theEvent = [NSApp nextEventMatchingMask: NSPeriodicMask
untilDate: [NSDate distantFuture]
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
if ([theEvent type] != NSPeriodic)
{
NSDebugLLog (@"NSDragging",
@"Unexpected event type: %d during slide",
(int)[theEvent type]);
}
newPosition.x = (screenPoint.x + ((float) steps - 1.0)
* dragPosition.x) / ((float) steps);
newPosition.y = (screenPoint.y + ((float) steps - 1.0)
* dragPosition.y) / ((float) steps);
[self _moveDraggedImageToNewPosition];
steps--;
}
[NSEvent stopPeriodicEvents];
}
if (waitFlag)
{
[NSThread sleepUntilDate:
[NSDate dateWithTimeIntervalSinceNow: delay * 2.0]];
}
}
@end
|