Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. - (void)mouseDown:(NSEvent*)event
  2. {
  3.  
  4. //create a NSPasteboardItem
  5. NSPasteboardItem *pbItem = [NSPasteboardItem new];
  6. [pbItem setDataProvider:self forTypes:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, nil]];
  7.  
  8. //create a new NSDraggingItem with our pasteboard item.
  9. NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pbItem];
  10. NSPoint dragPosition = [self convertPoint:[event locationInWindow] fromView:nil];
  11. dragPosition.x -= self.image.size.width/5/2;
  12. dragPosition.y -= self.image.size.height/5/2;
  13. NSRect draggingRect = NSMakeRect(dragPosition.x, dragPosition.y, self.image.size.width/5, self.image.size.height/5);
  14. [dragItem setDraggingFrame:draggingRect contents:self.image];
  15.  
  16. //create a dragging session with our drag item and ourself as the source.
  17. NSDraggingSession *draggingSession = [self beginDraggingSessionWithItems:[NSArray arrayWithObject:dragItem] event:event source:self];
  18.  
  19. //causes the dragging item to slide back to the source if the drag fails.
  20. draggingSession.animatesToStartingPositionsOnCancelOrFail = YES;
  21.  
  22. }
  23.  
  24. - (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context
  25. {
  26. if (context == NSDraggingContextOutsideApplication) {
  27. return NSDragOperationCopy;
  28. }
  29.  
  30. return NSDragOperationNone;
  31. }
  32.  
  33.  
  34. -(void) draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint) screenPoint
  35. {
  36. for (NSPasteboardItem *item in [[NSPasteboard pasteboardWithName:NSDragPboard] pasteboardItems]) {
  37. NSLog(@"%@",[item types]);
  38. }
  39. }
  40.  
  41. - (void)draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation
  42. {
  43. NSLog(@"%@",session);
  44. }
  45.  
  46. - (void)pasteboard:(NSPasteboard *)sender item:(NSPasteboardItem *)item provideDataForType:(NSString *)type
  47. {
  48.  
  49. NSLog(@"FIRED ! %@",type);
  50.  
  51. //sender has accepted the drag and now we need to send the data for the type we promised
  52. if ( [type compare: NSPasteboardTypeTIFF] == NSOrderedSame ) {
  53. [sender setData:[[self image] TIFFRepresentation] forType:NSPasteboardTypeTIFF];
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement