Since dragImage: is deprecated I am trying to update drag and drop with beginDraggingSessionWithItems:.
Drag work fine but I cannot get drop to work correctly.
In the code below change #ifndef to #ifdef (using dragImage:) make drop work as expected.
draggingEntered, draggingUpdated, draggingExited, draggingEnded are all called but NOT performDragOperation (which do the drop).
Uncommenting the line
[pb_item pasteboard:paste_board provideDataForType:@"LN_PboardType"];
cause an exceptionPreprocess: -[NSPasteboardItem pasteboard:provideDataForType:]: unrecognized selector sent to instance 0x600001e64240.
I also try add NSDraggingDestination, NSItemProviderWriting, NSPasteboardTypeOwner to @interface line without success.
What I am doing wrong ?
Any other method to do Drag and Drop (other than dragImage: and beginDraggingSessionWithItems:) ?
Where can I found an Obj-C project using beginDraggingSessionWithItems: for drag an drop ?
``@interface Enclosure : NSView <NSDraggingSource, NSPasteboardItemDataProvider>
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[super setAutoresizesSubviews:YES];
[self registerForDraggedTypes:[NSArray arrayWithObjects:@"LN_PboardType", NSPasteboardTypeString, nil]];
}
return self;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)event
{
return YES;
}
- (void)mouseDown:(NSEvent *)drag_event
{
NSImage *drag_image;
NSPoint drag_position;
NSRect dragging_rect;
NSPasteboard *paste_board;
NSDraggingItem *drag_item;
NSArray *items_array;
NSPasteboardItem *pb_item;
paste_board = [NSPasteboard pasteboardWithName:@"LN_PboardType"];
[paste_board declareTypes:[NSArray arrayWithObjects:@"LN_PboardType", NSPasteboardTypeString, nil] owner:self];
pb_item = [[NSPasteboardItem alloc] init];
[pb_item setDataProvider:self forTypes:[NSArray arrayWithObjects:@"LN_PboardType", NSPasteboardTypeString, nil]];
// [pb_item pasteboard:paste_board provideDataForType:@"LN_PboardType"];
drag_image = [ [NSImage imageNamed:@"drag.jpg"];
drag_position = [self convertPoint:[drag_event locationInWindow] fromView:nil];
drag_position.x -= drag_image.size.width/2;
drag_position.y -= drag_image.size.height/2;
#ifndef DEPRECATED
[self dragImage:drag_image at:drag_position offset:NSZeroSize event:drag_event pasteboard:paste_board source:self slideBack:YES];
#else
drag_item = [[NSDraggingItem alloc] initWithPasteboardWriter:pb_item];
dragging_rect = NSMakeRect(drag_position.x, drag_position.y, drag_image.size.width, drag_image.size.height);
[drag_item setDraggingFrame:dragging_rect contents:drag_image];
items_array = [NSArray arrayWithObject:drag_item];
[self beginDraggingSessionWithItems:items_array event:drag_event source:(id)self];
#endif
}
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context
{
if (disable_drag_and_drop)
return NSDragOperationNone;
switch (context)
{
case NSDraggingContextWithinApplication:
return NSDragOperationCopy;
case NSDraggingContextOutsideApplication:
return NSDragOperationMove;
}
return NSDragOperationNone;
}
…``