Re: drag image for multi-cell selection
Re: drag image for multi-cell selection
- Subject: Re: drag image for multi-cell selection
- From: Ken Tozier <email@hidden>
- Date: Thu, 5 Jul 2007 15:53:23 -0400
Turns out I only needed to change two lines. So for anyone else who
may be tackling multi-select drags...
The "fromRect" line in the selectionImageWithAlpha method
- (NSImage *) selectionImageWithAlpha:(float) inAlpha
selectionFrame:(NSRect) inFrame
{
NSImage *resultImage = [[[NSImage alloc] initWithSize:
inFrame.size] autorelease],
*cellImage;
NSEnumerator *enumerator = [selectedCells objectEnumerator];
id cell;
NSRect cellFrame;
[resultImage lockFocus];
while (cell = [enumerator nextObject])
{
cellFrame = [cell frame];
cellImage = [cell imageWithAlpha: 1.0];
[cellImage drawAtPoint: NSMakePoint(cellFrame.origin.x -
inFrame.origin.x, cellFrame.origin.y - inFrame.origin.y)
fromRect: NSMakeRect(0, 0, cellFrame.size.width,
cellFrame.size.height)
operation: NSCompositeSourceOver
fraction: inAlpha];
}
[resultImage unlockFocus];
return resultImage;
}
and the "at" line in the "dragImage" call in this method
- (void) dragSelection:(NSEvent *) inEvent
{
NSRect selFrame = [self selectionFrame];
NSImage *selectionImage = [self selectionImageWithAlpha: .7
selectionFrame: selFrame];
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:
NSDragPboard];
NSString *classPBoardType = @"KMatrixViewSelection";
NSData *selectionArchive = [NSKeyedArchiver
archivedDataWithRootObject: selectedCells];
[pboard declareTypes: [NSArray arrayWithObject: classPBoardType]
owner: self];
[pboard setData: selectionArchive forType: classPBoardType];
NSLog(@"starting dragwith pboard: %@", pboard);
[self dragImage: selectionImage
at: selFrame.origin
offset: NSZeroSize
event: inEvent
pasteboard: pboard
source: self
slideBack: YES];
}
And, for completeness, the "imageWithAlpha" snapshot method
- (NSImage *) imageWithAlpha:(float) inAlpha
{
NSRect imgFrame = NSMakeRect(0, 0, [self frame].size.width,
[self frame].size.height);
[self lockFocus];
NSBitmapImageRep *tempBitmap = [[[NSBitmapImageRep alloc]
initWithFocusedViewRect: imgFrame] autorelease];
[self unlockFocus];
NSImage *tempImage = [[[NSImage alloc] initWithSize:
imgFrame.size] autorelease],
*resultImage = [[[NSImage alloc] initWithSize: imgFrame.size]
autorelease];
[tempImage addRepresentation: tempBitmap];
[resultImage lockFocus];
[tempImage drawAtPoint: NSZeroPoint
fromRect: NSMakeRect(0, 0, imgFrame.size.width, imgFrame.size.height)
operation: NSCompositeSourceOver
fraction: inAlpha];
[resultImage unlockFocus];
return resultImage;
}
On Jul 5, 2007, at 1:13 PM, Ken Tozier wrote:
Hi
I'm trying to generate a drag image for a multi-cell selection in
my custom matrix class and while single cell drag images work fine,
I'm apparently doing something wrong while compositing the multi-
cell image. Anyone see what I'm doing wrong here?
// create selection drag image
- (NSImage *) selectionImageWithAlpha:(float) inAlpha
{
NSRect imgFrame = [self selectionFrame];
NSImage *resultImage = [[[NSImage alloc] initWithSize:
imgFrame.size] autorelease],
*cellImage;
NSEnumerator *enumerator = [selectedCells objectEnumerator];
id cell;
NSRect cellFrame;
[resultImage lockFocus];
while (cell = [enumerator nextObject])
{
cellFrame = [cell frame];
// single cell selections are handled by the cells themselves
and, in that situation,
// imageWithAlpha works correctly, so I know "cellImage" is a
valid image.
cellImage = [cell imageWithAlpha: 1.0];
// is this the right way to composite images into another image?
[cellImage drawAtPoint: NSMakePoint(cellFrame.origin.x -
imgFrame.origin.x, cellFrame.origin.y - imgFrame.origin.y)
fromRect: cellFrame
operation: NSCompositeSourceOver
fraction: inAlpha];
}
[resultImage unlockFocus];
return resultImage;
}
// Calculate frame for the selection
- (NSRect) selectionFrame
{
NSEnumerator *enumerator = [selectedCells objectEnumerator];
id cell;
NSRect selfFrame = [self frame],
cellFrame;
float minX = selfFrame.size.width,
maxX = 0,
minY = selfFrame.size.height,
maxY = 0,
tempX,
tempY;
while (cell = [enumerator nextObject])
{
cellFrame = [cell frame];
tempX = cellFrame.origin.x + cellFrame.size.width;
tempY = cellFrame.origin.y + cellFrame.size.height;
if (cellFrame.origin.x < minX)
minX = cellFrame.origin.x;
if (tempX > maxX)
maxX = tempX;
if (cellFrame.origin.y < minY)
minY = cellFrame.origin.y;
if (tempY > maxY)
maxY = tempY;
}
return NSMakeRect(minX, minY, maxX - minX, maxY - minY);
}
_______________________________________________
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:
40comcast.net
This email sent to email@hidden
_______________________________________________
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