Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Drag image of NSView



I use something like this to grab a view as an image for dragging. This is in the - (void)mouseDragged:(NSEvent *)theEvent method for my NSView subclass. This view is a container for a number of other entry views that can be reordered.

if (!startedDrag)
{
/* other code - Sets "entry" which is the subview of this view I want an image for */


[entry lockFocus];
bitmap = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: [entry bounds]] autorelease];;
[entry unlockFocus];


image = [[[NSImage alloc] initWithSize:[entry bounds].size] autorelease];
[image addRepresentation:bitmap];




        /* set up the dragging pasteboard */

startedDrag = YES;
[self dragImage:image at:NSMakePoint(NSMinX([entry frame]), NSMaxY([entry frame])) offset:NSMakeSize(0.0, 0.0) event:lastMouseDown pasteboard:draggingPasteboard source:self slideBack:YES];
}




On Oct 14, 2005, at 8:35 PM, John Pannell wrote:

Hi Ken-

The example code (assuming you pulled this from the NSView docs) only addresses the case where the object being dragged is an actual image (i.e. the contents of an NSImageView or cell). There is no method on NSView that will return an image looking like the view.

Actually, if you are Tiger only, NSView has a method (bitmapImageRepForCachingDisplayInRect:) that would fit the bill - you could pass the rect for the whole view and then construct an NSImage from the NSBitmapImageRep returned.

Note that this would send the message that the user is dragging the entire view somewhere, which is not appropriate for frequently used compound views (NSMatrix, NSTableView, etc.) A more common pattern would have users dragging the data represented by a cell of one of these views, and cells with images have methods to pull the image from them. Not knowing your specific situation, I can't comment much further.

I just got done implementing drag and drop in my app, feel free to mail off-list with more specifics if you would like and I'd be happy to assist if I can.

HTH!

John

John Pannell
Positive Spin Media
http://www.positivespinmedia.com

On Oct 14, 2005, at 8:59 PM, Ken Tozier wrote:


I'm sure this is an easy one but the Apple Drag and Drop info located here "http://developer.apple.com/documentation/Cocoa/ Conceptual/DragandDrop/index.html?http://developer.apple.com/ documentation/Cocoa/Conceptual/DragandDrop/DragandDrop.html" gives no indication of how to actually create a transparent snapshot of an NSView for use as a drag image.

The documentation for NSView's dragImage:at:offset:event:pasteboard:source:slideBack: method shows the following snippet for getting the image

[pboard setData:[[self image] TIFFRepresentation] forType:NSTIFFPboardType];
[self dragImage:[self image] at:[self imageLocation] offset:dragOffset event:theEvent pasteboard:pboard source:self slideBack:YES];


Problem is though, NSView has no "image" method. Nor do it's superclasses "NSResponder" or "NSObject" so where are they getting the image? Is this some deprecated method of NSView?

Anyway. If there is some better documentation around or if someone could provide a simple code snippet of how to get a transparent image of a view for a drag icon, it would be much appreciated

Thanks for any help

Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/john% 40positivespinmedia.com


This email sent to email@hidden



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Drag image of NSView (From: Ken Tozier <email@hidden>)
 >Re: Drag image of NSView (From: John Pannell <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.