Re: NSImageView as a drag source - how?
Re: NSImageView as a drag source - how?
- Subject: Re: NSImageView as a drag source - how?
- From: David Remahl <email@hidden>
- Date: Fri, 07 Jun 2002 10:02:55 +0200
>
On Thursday, June 6, 2002, at 01:09 PM, Jeff LaMarche wrote:
>
>
> I hit exactly the same predicament a few weeks. I don't know why, but
>
> starting your drag from mouseDragged: doesn't work when subclassing
>
> NSImageView. If you start your drag from mouseDown, I bet it will start
>
> to work.
>
>
>
There's a coincidence -- I've been working on a similar thing recently.
>
>
Here's part of my implementation so far -- dragging works reliably from
>
mouseDragged for me...
>
Please excuse the rather nasty category :-)
Initiating the drag works reliably for me too.
>
The thing that's plaguing me at the moment is how to provide the image
>
as a file. Finder in particular seems to want an NSFilenamesPboardType,
>
but writing the image to a temporary file doesn't always work -- Finder
>
complains that it can't find the file, even when it is there...
The trick is to write the file out quickly, and by a name that has not been
used during the same session. If the file is too large, so you cant write it
out quickly enough, then you are in trouble...I know of no solution to that
problem.
/ David
>
mmalc
>
>
>
>
>
#import "DraggableImageView.h"
>
>
>
@implementation NSImageCell (DraggableImageView)
>
- (NSImage *)_scaledImage {
>
return _scaledImage;
>
}
>
@end
>
>
>
@implementation DraggableImageView
>
>
>
- (void)startDrag:(NSEvent *)event
>
{
>
NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard];
>
NSImage *dragImage;
>
NSImage *scaledImage = [[self cell] _scaledImage];
>
NSPoint dragPoint;
>
>
dragPoint = NSMakePoint(
>
([self bounds].size.width - [scaledImage size].width) / 2,
>
([self bounds].size.height - [scaledImage size].height) / 2);
>
>
>
[pb declareTypes: [NSArray arrayWithObject: NSFilenamesPboardType]
>
owner: self];
>
>
>
dragImage = [[[NSImage alloc] initWithSize: [scaledImage size]]
>
autorelease];
>
[dragImage lockFocus];
>
[[[self cell] _scaledImage] dissolveToPoint: NSMakePoint(0,0)
>
fraction: .5];
>
[dragImage unlockFocus];
>
>
[self dragImage: dragImage
>
at: dragPoint
>
offset: NSMakeSize(0,0)
>
event:event
>
pasteboard:pb
>
source: self
>
slideBack: YES];
>
}
>
>
- (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent *)event
>
{
>
// maybe make more discerning?!
>
return YES;
>
}
>
>
- (BOOL)acceptsFirstMouse:(NSEvent *)event
>
{
>
return YES;
>
}
>
>
- (void)mouseDown:(NSEvent *)event
>
{
>
[self setDownEvent:event];
>
}
>
>
- (void)mouseDragged:(NSEvent *)event
>
{
>
if ([self image]) {
>
[self startDrag:downEvent];
>
}
>
[self setDownEvent:nil];
>
}
>
>
>
- (NSEvent *)downEvent
>
{
>
return downEvent;
>
}
>
- (void)setDownEvent:(NSEvent *)event
>
{
>
[downEvent autorelease];
>
downEvent = [event retain];
>
}
>
>
@end
_______________________________________________
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.