Support SDL_EVENT_DROP_TEXT in Cocoa
src/video/cocoa/SDL_cocoawindow.m
Support Copy in addition to Generic as Drag and Drop operation,
Register and Support public.utf8-plain-text for SDL_EVENT_DROP_TEXT.
This commit is contained in:
committed by
Sam Lantinga
parent
c040a02d6c
commit
68cabc2837
@@ -161,6 +161,8 @@
|
|||||||
{
|
{
|
||||||
if (([sender draggingSourceOperationMask] & NSDragOperationGeneric) == NSDragOperationGeneric) {
|
if (([sender draggingSourceOperationMask] & NSDragOperationGeneric) == NSDragOperationGeneric) {
|
||||||
return NSDragOperationGeneric;
|
return NSDragOperationGeneric;
|
||||||
|
} else if (([sender draggingSourceOperationMask] & NSDragOperationCopy) == NSDragOperationCopy) {
|
||||||
|
return NSDragOperationCopy;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NSDragOperationNone; /* no idea what to do with this, reject it. */
|
return NSDragOperationNone; /* no idea what to do with this, reject it. */
|
||||||
@@ -176,6 +178,14 @@
|
|||||||
y = (sdlwindow->h - point.y);
|
y = (sdlwindow->h - point.y);
|
||||||
SDL_SendDropPosition(sdlwindow, x, y);
|
SDL_SendDropPosition(sdlwindow, x, y);
|
||||||
return NSDragOperationGeneric;
|
return NSDragOperationGeneric;
|
||||||
|
} else if (([sender draggingSourceOperationMask] & NSDragOperationCopy) == NSDragOperationCopy) {
|
||||||
|
SDL_Window *sdlwindow = [self findSDLWindow];
|
||||||
|
NSPoint point = [sender draggingLocation];
|
||||||
|
float x, y;
|
||||||
|
x = point.x;
|
||||||
|
y = (sdlwindow->h - point.y);
|
||||||
|
SDL_SendDropPosition(sdlwindow, x, y);
|
||||||
|
return NSDragOperationCopy;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NSDragOperationNone; /* no idea what to do with this, reject it. */
|
return NSDragOperationNone; /* no idea what to do with this, reject it. */
|
||||||
@@ -183,63 +193,115 @@
|
|||||||
|
|
||||||
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
|
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
|
||||||
{
|
{
|
||||||
|
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||||
|
". [SDL] In performDragOperation, draggingSourceOperationMask %lx, "
|
||||||
|
"expected Generic %lx, others Copy %lx, Link %lx, Private %lx, Move %lx, Delete %lx\n",
|
||||||
|
(unsigned long)[sender draggingSourceOperationMask],
|
||||||
|
(unsigned long)NSDragOperationGeneric,
|
||||||
|
(unsigned long)NSDragOperationCopy,
|
||||||
|
(unsigned long)NSDragOperationLink,
|
||||||
|
(unsigned long)NSDragOperationPrivate,
|
||||||
|
(unsigned long)NSDragOperationMove,
|
||||||
|
(unsigned long)NSDragOperationDelete);
|
||||||
|
if ([sender draggingPasteboard]) {
|
||||||
|
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||||
|
". [SDL] In performDragOperation, valid draggingPasteboard, "
|
||||||
|
"name [%s] '%s', changeCount %ld\n",
|
||||||
|
[[[[sender draggingPasteboard] name] className] UTF8String],
|
||||||
|
[[[[sender draggingPasteboard] name] description] UTF8String],
|
||||||
|
(long)[[sender draggingPasteboard] changeCount]);
|
||||||
|
}
|
||||||
@autoreleasepool {
|
@autoreleasepool {
|
||||||
NSPasteboard *pasteboard = [sender draggingPasteboard];
|
NSPasteboard *pasteboard = [sender draggingPasteboard];
|
||||||
NSArray *types = [NSArray arrayWithObject:NSFilenamesPboardType];
|
NSString *desiredType = [pasteboard availableTypeFromArray:@[ NSFilenamesPboardType, NSPasteboardTypeString ]];
|
||||||
NSString *desiredType = [pasteboard availableTypeFromArray:types];
|
|
||||||
SDL_Window *sdlwindow = [self findSDLWindow];
|
SDL_Window *sdlwindow = [self findSDLWindow];
|
||||||
NSData *data;
|
NSData *pboardData;
|
||||||
NSArray *array;
|
id pboardPlist;
|
||||||
|
NSString *pboardString;
|
||||||
NSPoint point;
|
NSPoint point;
|
||||||
float x, y;
|
float x, y;
|
||||||
|
|
||||||
|
for (NSString *supportedType in [pasteboard types]) {
|
||||||
|
NSString *typeString = [pasteboard stringForType:supportedType];
|
||||||
|
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||||
|
". [SDL] In performDragOperation, Pasteboard type '%s', stringForType (%lu) '%s'\n",
|
||||||
|
[[supportedType description] UTF8String],
|
||||||
|
(unsigned long)[[typeString description] length],
|
||||||
|
[[typeString description] UTF8String]);
|
||||||
|
}
|
||||||
|
|
||||||
if (desiredType == nil) {
|
if (desiredType == nil) {
|
||||||
return NO; /* can't accept anything that's being dropped here. */
|
return NO; /* can't accept anything that's being dropped here. */
|
||||||
}
|
}
|
||||||
|
pboardData = [pasteboard dataForType:desiredType];
|
||||||
data = [pasteboard dataForType:desiredType];
|
if (pboardData == nil) {
|
||||||
if (data == nil) {
|
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
SDL_assert([desiredType isEqualToString:NSFilenamesPboardType] ||
|
||||||
|
[desiredType isEqualToString:NSPasteboardTypeString]);
|
||||||
|
|
||||||
SDL_assert([desiredType isEqualToString:NSFilenamesPboardType]);
|
pboardString = [pasteboard stringForType:desiredType];
|
||||||
array = [pasteboard propertyListForType:@"NSFilenamesPboardType"];
|
pboardPlist = [pasteboard propertyListForType:desiredType];
|
||||||
|
|
||||||
/* Code addon to update the mouse location */
|
/* Use SendDropPosition to update the mouse location */
|
||||||
point = [sender draggingLocation];
|
point = [sender draggingLocation];
|
||||||
x = point.x;
|
x = point.x;
|
||||||
y = (sdlwindow->h - point.y);
|
y = (sdlwindow->h - point.y);
|
||||||
if (x >= 0.0f && x < (float)sdlwindow->w && y >= 0.0f && y < (float)sdlwindow->h) {
|
if (x >= 0.0f && x < (float)sdlwindow->w && y >= 0.0f && y < (float)sdlwindow->h) {
|
||||||
SDL_SendMouseMotion(0, sdlwindow, SDL_GLOBAL_MOUSE_ID, SDL_FALSE, x, y);
|
SDL_SendDropPosition(sdlwindow, x, y);
|
||||||
}
|
}
|
||||||
/* Code addon to update the mouse location */
|
/* Use SendDropPosition to update the mouse location */
|
||||||
|
|
||||||
for (NSString *path in array) {
|
if ([desiredType isEqualToString:NSFilenamesPboardType]) {
|
||||||
NSURL *fileURL = [NSURL fileURLWithPath:path];
|
for (NSString *path in (NSArray *)pboardPlist) {
|
||||||
NSNumber *isAlias = nil;
|
NSURL *fileURL = [NSURL fileURLWithPath:path];
|
||||||
|
NSNumber *isAlias = nil;
|
||||||
|
|
||||||
[fileURL getResourceValue:&isAlias forKey:NSURLIsAliasFileKey error:nil];
|
[fileURL getResourceValue:&isAlias forKey:NSURLIsAliasFileKey error:nil];
|
||||||
|
|
||||||
/* If the URL is an alias, resolve it. */
|
/* If the URL is an alias, resolve it. */
|
||||||
if ([isAlias boolValue]) {
|
if ([isAlias boolValue]) {
|
||||||
NSURLBookmarkResolutionOptions opts = NSURLBookmarkResolutionWithoutMounting | NSURLBookmarkResolutionWithoutUI;
|
NSURLBookmarkResolutionOptions opts = NSURLBookmarkResolutionWithoutMounting |
|
||||||
NSData *bookmark = [NSURL bookmarkDataWithContentsOfURL:fileURL error:nil];
|
NSURLBookmarkResolutionWithoutUI;
|
||||||
if (bookmark != nil) {
|
NSData *bookmark = [NSURL bookmarkDataWithContentsOfURL:fileURL error:nil];
|
||||||
NSURL *resolvedURL = [NSURL URLByResolvingBookmarkData:bookmark
|
if (bookmark != nil) {
|
||||||
options:opts
|
NSURL *resolvedURL = [NSURL URLByResolvingBookmarkData:bookmark
|
||||||
relativeToURL:nil
|
options:opts
|
||||||
bookmarkDataIsStale:nil
|
relativeToURL:nil
|
||||||
error:nil];
|
bookmarkDataIsStale:nil
|
||||||
|
error:nil];
|
||||||
if (resolvedURL != nil) {
|
if (resolvedURL != nil) {
|
||||||
fileURL = resolvedURL;
|
fileURL = resolvedURL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||||
|
". [SDL] In performDragOperation, desiredType '%s', "
|
||||||
|
"Submitting DropFile as (%lu) '%s'\n",
|
||||||
|
[[desiredType description] UTF8String],
|
||||||
|
(unsigned long)[[[fileURL path] description] length],
|
||||||
|
[[[fileURL path] description] UTF8String]);
|
||||||
|
if (!SDL_SendDropFile(sdlwindow, NULL, [[[fileURL path] description] UTF8String])) {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else if ([desiredType isEqualToString:NSPasteboardTypeString]) {
|
||||||
if (!SDL_SendDropFile(sdlwindow, NULL, [[fileURL path] UTF8String])) {
|
char *buffer = SDL_strdup([[pboardString description] UTF8String]);
|
||||||
return NO;
|
char *saveptr = NULL;
|
||||||
|
char *token = SDL_strtok_r(buffer, "\r\n", &saveptr);
|
||||||
|
while (token) {
|
||||||
|
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||||
|
". [SDL] In performDragOperation, desiredType '%s', "
|
||||||
|
"Submitting DropText as (%lu) '%s'\n",
|
||||||
|
[[desiredType description] UTF8String],
|
||||||
|
strlen(token), token);
|
||||||
|
if (!SDL_SendDropText(sdlwindow, token)) {
|
||||||
|
SDL_free(buffer);
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
token = SDL_strtok_r(NULL, "\r\n", &saveptr);
|
||||||
}
|
}
|
||||||
|
SDL_free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_SendDropComplete(sdlwindow);
|
SDL_SendDropComplete(sdlwindow);
|
||||||
@@ -2954,7 +3016,8 @@ void Cocoa_AcceptDragAndDrop(SDL_Window *window, SDL_bool accept)
|
|||||||
@autoreleasepool {
|
@autoreleasepool {
|
||||||
SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
|
SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
|
||||||
if (accept) {
|
if (accept) {
|
||||||
[data.nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
|
[data.nswindow registerForDraggedTypes:@[ (NSString *)kUTTypeFileURL,
|
||||||
|
(NSString *)kUTTypeUTF8PlainText ]];
|
||||||
} else {
|
} else {
|
||||||
[data.nswindow unregisterDraggedTypes];
|
[data.nswindow unregisterDraggedTypes];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user