my attempts to copy file contents to the pasteboard are unsuccessful... i've read the documentation on dragging file contents and promised files and all that...
i have a view (which is just a color), when i drag the mouse over the view, the program makes a .png file from cropping the view and writing that file to the tmp folder. then the program is *suppose* to copy that file to the pasteboard using the writeFileContents method. it doesn't copy to the pasteboard because i can't seem to drop it on my desktop, or anywhere outside of the program that should accept .PNG files for that matter. it will just always slideback.
what am i doing wrong here?
i have a view (which is just a color), when i drag the mouse over the view, the program makes a .png file from cropping the view and writing that file to the tmp folder. then the program is *suppose* to copy that file to the pasteboard using the writeFileContents method. it doesn't copy to the pasteboard because i can't seem to drop it on my desktop, or anywhere outside of the program that should accept .PNG files for that matter. it will just always slideback.
what am i doing wrong here?
Code:
#pragma mark Dragging Source
- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
return NSDragOperationCopy;
}
- (void)mouseDown:(NSEvent *)event
{
[event retain];
[mouseDownEvent release];
mouseDownEvent = event;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)event
{
return YES;
}
- (void)swatchDragWithStroke:(NSRect)rect
{
[fillColor setFill];
NSRectFill(rect);
NSBezierPath *pathThickness = [NSBezierPath bezierPathWithRect:rect];
[pathThickness setLineWidth:4];
NSImage *dottedStroke = [NSImage imageNamed:@"DottedStroke.png"];
NSColor *dottedColor = [NSColor colorWithPatternImage:dottedStroke];
[dottedColor setStroke];
[pathThickness stroke];
}
- (void)mouseDragged:(NSEvent *)event
{
NSPoint down = [mouseDownEvent locationInWindow];
NSPoint drag = [event locationInWindow];
float distance = hypot(down.x - drag.x, down.y - drag.y);
if (distance < 2)
{
return;
}
NSRect swatchDrag;
NSSize size = NSMakeSize(64, 64);
swatchDrag.origin = NSZeroPoint;
swatchDrag.size = size;
NSImage *anImage = [[NSImage alloc] initWithSize:size];
[anImage lockFocus];
[self swatchDragWithStroke:swatchDrag];
[anImage unlockFocus];
NSString *pathName = @"/tmp/Swatch.png";
NSRect croppedRect = NSMakeRect(0, 0, 16, 16);
[self lockFocus];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:croppedRect];
[self unlockFocus];
NSData *data = [rep TIFFRepresentation];
[data writeToFile:pathName atomically:YES];
[NSBitmapImageRep release];
NSPoint point = [self convertPoint:down fromView:nil];
point.x -= size.width / 2.0;
point.y -= size.height / 2.0;
NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSDragPboard];
[pb writeFileContents:pathName];
[self dragImage:anImage at:point offset:NSMakeSize(0,0) event:mouseDownEvent pasteboard:pb source:self slideBack:YES];
[anImage release];
}