Re: Trouble creating drag image from a partial view
Re: Trouble creating drag image from a partial view
- Subject: Re: Trouble creating drag image from a partial view
- From: Alastair Houghton <email@hidden>
- Date: Fri, 7 Dec 2007 13:07:42 +0000
On 7 Dec 2007, at 11:20, Ken Tozier wrote:
On Dec 7, 2007, at 5:50 AM, Alastair Houghton wrote:
Up to here you were fine, but then:
[resultImage lockFocus];
[resultImage drawAtPoint: NSZeroPoint
fromRect: imgFrame
operation: NSCompositeSourceOver
fraction: inAlpha];
[resultImage unlockFocus];
What is the above code supposed to do?!
That chunk allows me to set the transparency of the drag image to
"inAlpha." There may be another way to do it but this is "first
pass" code and it seemed to work so I used it.
Even if you intended that the above should change the alpha value, and
even if it is permissible to lock focus on an image and then draw that
same image (which I seriously doubt), it's still wrong, because you'd
need to use NSCompositeCopy.
You don't need to make the drag image transparent yourself; the system
will draw it transparently for you. If you were doing that to try to
make the background less visible, it's not really the right solution
(see below).
I think you'll find that if you delete the code between -lockFocus
and -unlockFocus, it will probably work just fine.
I commented it out the "[resultImage lock focus]" block as you
suggested but got the exact same result. (Basically empty image with
a 1 pixel vertical strip)
- (NSImage *) imageWithAlpha:(float) inAlpha
{
NSRect imgFrame = NSMakeRect(0, 0, [fileIcon frame].size.width,
[fileIcon frame].size.height);
// create view bitmap
[fileIcon lockFocus];
NSBitmapImageRep *selfBitmap = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: [fileIcon frame]];
[fileIcon unlockFocus];
OK, the next problem is that in the code above, you're asking for the
wrong rectangle. You want [fileIcon bounds], assuming "fileIcon" is a
view. Otherwise you'll be grabbing the wrong bit of the display.
Note also that -initWithFocusedViewRect: has one or two problems; if
this view can be scrolled such that part of it isn't visible, you
won't get any data for the non-visible part. This happens because -
initWithFocusedViewRect: takes its data directly from the backing
store. Also, you have very little control over any background that
gets drawn by the view; usually backgrounds look wrong if you're
dragging e.g. icons.
Personally when implementing drag images, I prefer to separate out the
drawing code that is shared between the drag image and the view's
display, then I can just do something like this:
- (NSImage *)dragImageFromRect:(NSRect)dragImageRect
{
NSImage *image = [[[NSImage alloc]
initWithSize:dragImageRect.size] autorelease];
[image lockFocus];
[NSGraphicsContext saveGraphicsState];
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:-NSMinX (dragImageRect) yBy:-NSMinY
(dragImageRect)];
[transform concat];
[self drawStuffInRect:dragImageRect];
[NSGraphicsContext restoreGraphicsState];
[image unlockFocus];
return image;
}
to get my drag image, and my -drawRect: will look like this:
- (void)drawRect:(NSRect)rect
{
// Draw the background
[[NSColor whiteColor] set];
NSRectFill (rect);
// Draw some stuff
[self drawStuffInRect:rect];
}
Kind regards,
Alastair.
p.s. I think I may accidentally have hit send on a window that had an
empty reply in it. My apologies to anyone who receives that.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden