Re: Drag and Drop Oddities
Re: Drag and Drop Oddities
- Subject: Re: Drag and Drop Oddities
- From: email@hidden
- Date: Wed, 12 Jun 2002 11:49:32 -0700
i've had much better luck running my own event loop in mouseDown...
(which is why you sometimes never see mouseDragged get called when you
subclass certain controls...)
[warning, cut pasted into mail from other code, much deleted, ymmv]
- (void)mouseDown:(NSEvent *)e
{
NSPoint p = [self convertPoint:[e locationInWindow] fromView:nil];
NSRect r = [self clickableRect];
if (NSPointInRect(p, r)) {
NSPoint dragStartPoint = p;
do {
float dx, dy, dist;
e = [[self window]
nextEventMatchingMask:NSLeftMouseDraggedMask | NSLeftMouseUpMask];
p = [self convertPoint:[theEvent locationInWindow] fromView:nil];
dx = p.x - dragStartPoint.x;
dy = p.y - dragStartPoint.y;
dist = fabs(dx) + fabs(dy);
if (dist > DRAG_HYSTERESIS) {
NSSize offset = NSMakeSize(p.x - r.origin.x, p.y - r.origin.y);
NSPoint at = NSMakePoint(p.x - offset.width, p.y - offset.height);
NSPasteboard *pboard = [NSPasteboard
pasteboardWithName:NSDragPboard];
// create your dragImage, declare your pboard types
and set your pboard data here.
[self dragImage:dragImage at:at offset:offset event:e
pasteboard:pboard source:self slideBack:YES];
return;
}
} while ([theEvent type] == NSLeftMouseDragged);
}
}
On Wednesday, June 12, 2002, at 08:28 AM, John Anderson wrote:
>
So if I'm using the following code to detect a click or a drag, I'm
>
only using periodic events, right?:
>
>
- (void)mouseDown:(NSEvent *)theEvent
>
{
>
NSPoint localPoint = [self convertPoint:[theEvent locationInWindow]
>
fromView:nil];
>
>
[[[self window] windowController] deselectAll];
>
>
if ([self mouse:localPoint inRect:[self clickableRect]]) {
>
isSelected = YES;
>
[self display];
>
}
>
else
>
isSelected = NO;
>
}
>
>
- (void)mouseDragged:(NSEvent *)theEvent
>
{
>
NSSize dragOffset;
>
NSPoint imageLoc;
>
NSPasteboard *pboard;
>
NSImage *dragImage = [[NSImage alloc] initWithData:[self
>
dataWithPDFInsideRect:[self clickableRect]]];
>
NSPoint localPoint = [self convertPoint:[theEvent locationInWindow]
>
fromView:nil];
>
NSData *ownerData;
>
>
[[[self window] windowController] deselectAll];
>
>
if ([self mouse:localPoint inRect:[self clickableRect]]) {
>
imageLoc = [self clickableRect].origin;
>
imageLoc.y += 8;
>
dragOffset = NSMakeSize(localPoint.x, localPoint.y);
>
>
pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
>
[pboard declareTypes:[NSArray
>
arrayWithObject:@"PresenceContainer"] owner:self];
>
ownerData = [NSArchiver archivedDataWithRootObject:self];
>
[pboard setData:ownerData forType:@"PresenceContainer"];
>
[self dragImage:dragImage at:imageLoc offset:dragOffset
>
event:theEvent pasteboard:pboard source:self slideBack:YES];
>
}
>
}
>
>
What's causing the slowness here? Should I create the NSImage and
>
NSData when I get the mouseDown event instead, and hold onto it until I
>
get a drag? Perhaps that would speed things up... any other
>
suggestions? I don't really want to send dragImage: when I get
>
onMouseDown because I support selection of these objects as well, and
>
the slightest mouse movement causes a snap-back effect, which is
>
disturbing... so i want a delay, just not one this big.
>
>
John Anderson
>
everchanging
>
>
On Wednesday, June 12, 2002, at 12:26 AM, Jason Harris wrote:
>
>
> I've seen this before. It occurs when overriding mouseDown to receive
>
> an
>
> event and then starting periodic events to determine whether to begin
>
> a drag
>
> even if the user hasn't moved the mouse. You send locationInWindow to
>
> the
>
> periodic event and this is what you see, because locationInWindow is
>
> only
>
> defined for mouse events.
>
>
>
> Jason
>
>
>
>
>
> Hsu Tried to Tell Me:
>
>
>
>> I've not seen this in XShelf (which is based directly on code from
>
>> Apple
>
>> source archives and Hillegass' book)... drag/drop seems to work fine
>
>> (and it's thus far all Cocoa, no Carbon)
>
>>
>
>> Karl
>
>>
>
>> On Tuesday, June 11, 2002, at 11:39 AM, John Anderson wrote:
>
>>
>
>>> I've seen the exact same problem. Hate to post a "me too" here but I
>
>>> thought you'd like to know that someone feels your pain :-)
>
>>>
>
>>> John
>
>>>
>
>>>
>
>>> On Monday, June 10, 2002, at 09:02 PM, Lucas Haley wrote:
>
>>>
>
>>>> Heya all --
>
>>>>
>
>>>> I've just delved into drag and drop, and things are going well.
>
>>>> However, whenever I begin my drag, the app stalls a little, and the
>
>>>> image quickly jumps once from the top left of the monitor to the
>
>>>> mouse
>
>>>> position. Has anyone else encountered this? I am using code almost
>
>>>> exclusively lifted from Hillegass and the Apple source code
>
>>>> archives.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.