Dragging operations

I want to be able to drag the contents from my subclass of NSView to any location in finder. I read through the drag and drop documentation, but there were still a few things I did not understand. I tried using the example code given. Using mouseDragged: instead of mouseDown:

- (void)mouseDragged:(NSEvent *)event
{
	NSPoint dragPosition;
	NSRect imageLocation;
    
	dragPosition = [self convertPoint:[event locationInWindow]
	                    fromView:nil];
	dragPosition.x -= 16;
	dragPosition.y -= 16;
	imageLocation.origin = dragPosition;
	imageLocation.size = NSMakeSize(32,32);
	
	[self dragPromisedFilesOfTypes:[NSArray arrayWithObject:@"pdf"]
            fromRect:imageLocation
            source:self
            slideBack:YES
            event:event];
}

but the documentaion says that I have to implement the namesOfPromisedFilesDroppedAtDestination: method in the dragging source too. How do I set the destination to whatever location I drag it to in Finder.
Am I doing it right? Because It seems that I haven't put any data on the pasteboard yet. Do I have to write the pdf data onto the pasteboard?
I would appreciate any help. Thanks.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

update

I tried implementing this. Seems like dropDestination shows up correctly when I drag to a location in Finder. However, I get a CFURLGetFSRef failed error.

- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
{
	NSLog(@"dropDestination = %@", dropDestination);
	return [NSArray arrayWithObject:@"test"] ;
}

namesOfPromisedFilesDroppedAtDestination

I also tried this. But is this right?

- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)destination
{
NSLog(@"destination = %@", destination);

NSRect r = [self bounds] ;
NSData *data = [self dataWithPDFInsideRect:r] ;

NSMutableString *temp = [NSMutableString stringWithCapacity:0] ;
[temp appendString:[destination path]] ;
[temp appendString:@"/test.pdf"] ; // filename

[data writeToFile:temp atomically:YES] ;
return [NSArray arrayWithObject:@"test"] ;
}

and why can't I use code format with this? it always says terminated because of suspicious data input when i try to post the comment.

Re: dragging operations

>> why can't I use code format with this? it always says terminated because of suspicious data input when i try to post the comment.

I think that's a protection measure for either security reasons or spam control.

>> However, I get a CFURLGetFSRef failed error.
CFURLGetFSRef is a behind-the-scenes Core Foundation function that tries to convert an CFURL to a valid file or directory object. CFURL is "toll-free bridged" to NSURL, which means they are interchangeable as long as the proper type cast is used(in this case the runtime is doing any necessary typecasts for you). If you are getting an error, it is probably because the runtime can't convert the NSURL(CFURL) to a valid file object. Maybe in your namesOfPromisedFilesDroppedAtDestination: method, instead of test, try test.pdf(just my guess).

If that doesn't work, could you give more information on the error message if it is available?--usually there is a remark on the type of error in the runtime log.

dragging operations

Thanks. Just tried your guess. Made it return test.pdf instead. The error went away. I have to writeToFile:atomically: in this method if i want to create the file at the end of the drop?

- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)destination
{
NSLog(@"destination = %@", destination);

NSRect r = [self bounds] ;
NSData *data = [self dataWithPDFInsideRect:r] ;

NSMutableString *temp = [NSMutableString stringWithCapacity:0] ;
[temp appendString:[destination path]] ;
[temp appendString:@"/test.pdf"] ; // filename

[data writeToFile:temp atomically:YES] ;
return [NSArray arrayWithObject:@"test.pdf"] ;
}

NSBezierPath and pdfs

I tried drawing some NSBezierPath paths, but even though they show up on screen, they aren't in the saved PDF files. I'm stumped...

I think it has something to do with the order of drawing to the screen? or the amount of things draw to the screen?

I tried drawing LESS things onto the screen and saving it to pdf.

NSData *pdfData = [self dataWithPDFInsideRect:r] ;
[pdfData writeToFile:[sheet filename] atomically:YES] ;

Everything worked out fine...

re: NSBezierPath and pdfs (NSAffineTransform)

I'm sorry, i think my previous plea of help didn't make much sense. I found my problem. In order to draw some strings vertically in NSView, I used

	[self rotateByAngle:90] ;

        // draw my stuff

	[self rotateByAngle:-90] ;

I don't know why but, the drawings after the rotateByAngle:-90 line do not show up in the pdf file that I saved. Some help please?

...

Nevermind. I found a little something called NSAffineTransform. A wrong way of doing it is rotating NSView by rotateByAngle before drawing. The proper way is to apply a NSAffineTransform instead. I found it all out reading the adding transforms in your code section within the cocoa drawing guide.

	NSAffineTransform* xform = [NSAffineTransform transform];
	[xform rotateByDegrees:90.0]; // counterclockwise rotation
	[xform concat] ; // applies transform

        // draw content

	[xform invert] ;
	[xform concat] ;