views:

169

answers:

1

I'm trying to write some simple code to drag-and-drop the contents of a text file onto a window. With some help from an earlier post and an Apple example I've now got the basics of drag-and-drop up and running.

Unfortunately however, Apple's sample code deals only with images. Can anyone please tell me how I'd modify their "pasteboard" method (shown below) to send the contents of a simple 'dot.txt' file?

- (void)pasteboard:(NSPasteboard*)sender provideDataForType:(NSString*)type
{
//------------------------------------------------------
//   method called by pasteboard to support promised drag types.
//--------------------------------------------------------
//sender has accepted the drag and now we need to send the data for the type we promised
if([type compare: NSTIFFPboardType]==NSOrderedSame)
  {
  //set data for TIFF type on the pasteboard as requested
  [sender setData:[[self image] TIFFRepresentation] forType:NSTIFFPboardType];
  }
 else if([type compare: NSPDFPboardType]==NSOrderedSame)
  {
  [sender setData:[self dataWithPDFInsideRect:[self bounds]] forType:NSPDFPboardType];
  }
}

Thanks :-)

A: 

Can anyone please tell me how I'd modify their "pasteboard" method (shown below) to send the contents of a simple 'dot.txt' file?

The caller is asking you to send data of a certain type. If you can provide data of that type, do so by putting it on the pasteboard. If you can't, do nothing.

Peter Hosey
Thanks, Peter. So will this method still work un-modified if I attempt to provide the pasteboard with a text file (data of type 'string') rather than an image file?
Bender
Read it and see. What does it do when asked for text?
Peter Hosey
And you aren't providing the pasteboard with the data before entering this method—that's this method's job. This method fulfills a promise you made earlier; you receive this message when the other end attempts to access the data that you promised but did not provide.
Peter Hosey