Reputation: 539
I have NSButton what accepts drop on to add dragged items to NSMutableArray. How can I make drag from NSButton to drag all items in NSMutableArray?
Upvotes: 0
Views: 621
Reputation: 104082
One way to do that is to use the NSView method, dragImage:at:offset:event:pasteboard:source:slideBack: to start the drag. In your custom button class you would override mouseDown: and call that method. I wrote an example where the image that's dragged is the button's image, and the data I'm dragging is just the string "ARRAY". That should be all you need to do from the source side -- when you drop that on your destination, it could test to see if what was dropped was the string "ARRAY" and then do whatever you want to do with that array you created with your first drop. I used the NSMultipleDocuments image in my button, and I make it disappear when I drag out of the button.
- (void)mouseDown:(NSEvent *)theEvent {
NSImage *pic = self.image;
NSSize dragOffset = NSMakeSize(0.0, 0.0);
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[pboard writeObjects:[NSArray arrayWithObject:@"ARRAY"]];
NSPoint btnMiddle = NSMakePoint(self.frame.size.width/2,self.frame.size.height/2);
NSPoint picOrigin = NSMakePoint(btnMiddle.x - pic.size.width/2, btnMiddle.y + pic.size.height/2);
self.image = nil;
[self dragImage:pic at:picOrigin offset:dragOffset event:theEvent pasteboard:pboard source:self slideBack:YES];
}
Upvotes: 2