Cocoa: Use NSURLs for drag and drop

Fixes #1377.
This commit is contained in:
Camilla Löwy 2018-11-19 22:27:51 +01:00
parent 9bfdd218fb
commit 18145a7f3d

View File

@ -51,7 +51,6 @@
#endif #endif
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101300 #if MAC_OS_X_VERSION_MAX_ALLOWED < 101300
#define NSPasteboardTypeFileURL NSFilenamesPboardType
#define NSPasteboardTypeString NSStringPboardType #define NSPasteboardTypeString NSStringPboardType
#endif #endif
@ -442,8 +441,9 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
markedText = [[NSMutableAttributedString alloc] init]; markedText = [[NSMutableAttributedString alloc] init];
[self updateTrackingAreas]; [self updateTrackingAreas];
[self registerForDraggedTypes:[NSArray arrayWithObjects: // NOTE: kUTTypeURL corresponds to NSPasteboardTypeURL but is available
NSPasteboardTypeFileURL, nil]]; // on 10.7 without having been deprecated yet
[self registerForDraggedTypes:@[(__bridge NSString*) kUTTypeURL]];
} }
return self; return self;
@ -713,45 +713,33 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{ {
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) // HACK: We don't know what to say here because we don't know what the
== NSDragOperationGeneric) // application wants to do with the paths
{ return NSDragOperationGeneric;
[self setNeedsDisplay:YES];
return NSDragOperationGeneric;
}
return NSDragOperationNone;
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
[self setNeedsDisplay:YES];
return YES;
} }
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{ {
NSPasteboard* pasteboard = [sender draggingPasteboard];
NSArray* files = [pasteboard propertyListForType:NSPasteboardTypeFileURL];
const NSRect contentRect = [window->ns.view frame]; const NSRect contentRect = [window->ns.view frame];
_glfwInputCursorPos(window, _glfwInputCursorPos(window,
[sender draggingLocation].x, [sender draggingLocation].x,
contentRect.size.height - [sender draggingLocation].y); contentRect.size.height - [sender draggingLocation].y);
const NSUInteger count = [files count]; NSPasteboard* pasteboard = [sender draggingPasteboard];
NSDictionary* options = @{NSPasteboardURLReadingFileURLsOnlyKey:@YES};
NSArray* urls = [pasteboard readObjectsForClasses:@[[NSURL class]]
options:options];
const NSUInteger count = [urls count];
if (count) if (count)
{ {
NSEnumerator* e = [files objectEnumerator];
char** paths = calloc(count, sizeof(char*)); char** paths = calloc(count, sizeof(char*));
NSUInteger i;
for (i = 0; i < count; i++) for (NSUInteger i = 0; i < count; i++)
paths[i] = _glfw_strdup([[e nextObject] UTF8String]); paths[i] = _glfw_strdup([[urls objectAtIndex:i] fileSystemRepresentation]);
_glfwInputDrop(window, (int) count, (const char**) paths); _glfwInputDrop(window, (int) count, (const char**) paths);
for (i = 0; i < count; i++) for (NSUInteger i = 0; i < count; i++)
free(paths[i]); free(paths[i]);
free(paths); free(paths);
} }
@ -759,11 +747,6 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
return YES; return YES;
} }
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
[self setNeedsDisplay:YES];
}
- (BOOL)hasMarkedText - (BOOL)hasMarkedText
{ {
return [markedText length] > 0; return [markedText length] > 0;