Navigation Menu

Skip to content

Commit

Permalink
Mac: Add drag & drop support.
Browse files Browse the repository at this point in the history
Fixes bug https://bugzilla.libsdl.org/show_bug.cgi?id=2757

Thanks to Alex Szpakowski for the patch!
  • Loading branch information
jorgenpt committed Nov 24, 2014
1 parent 084642d commit 9453913
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -34,6 +34,7 @@
#include "../../events/SDL_mouse_c.h"
#include "../../events/SDL_touch_c.h"
#include "../../events/SDL_windowevents_c.h"
#include "../../events/SDL_dropevents_c.h"
#include "SDL_cocoavideo.h"
#include "SDL_cocoashape.h"
#include "SDL_cocoamouse.h"
Expand All @@ -52,15 +53,21 @@
#define FULLSCREEN_MASK (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN)


@interface SDLWindow : NSWindow
@interface SDLWindow : NSWindow <NSDraggingDestination>
/* These are needed for borderless/fullscreen windows */
- (BOOL)canBecomeKeyWindow;
- (BOOL)canBecomeMainWindow;
- (void)sendEvent:(NSEvent *)event;
- (void)doCommandBySelector:(SEL)aSelector;

/* Handle drag-and-drop of files onto the SDL window. */
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
- (BOOL)wantsPeriodicDraggingUpdates;
@end

@implementation SDLWindow

- (BOOL)canBecomeKeyWindow
{
return YES;
Expand Down Expand Up @@ -96,6 +103,51 @@ - (void)doCommandBySelector:(SEL)aSelector
{
/*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
return NSDragOperationGeneric;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSURL *fileURL = [NSURL URLFromPasteboard:[sender draggingPasteboard]];
NSNumber *isAlias = nil;

if (fileURL == nil) {
return NO;
}

/* Functionality for resolving URL aliases was added with OS X 10.6. */
if ([fileURL respondsToSelector:@selector(getResourceValue:forKey:error:)]) {
[fileURL getResourceValue:&isAlias forKey:NSURLIsAliasFileKey error:nil];
}

/* If the URL is an alias, resolve it. */
if ([isAlias boolValue]) {
NSURLBookmarkResolutionOptions opts = NSURLBookmarkResolutionWithoutMounting | NSURLBookmarkResolutionWithoutUI;
NSData *bookmark = [NSURL bookmarkDataWithContentsOfURL:fileURL error:nil];
if (bookmark != nil) {
NSURL *resolvedURL = [NSURL URLByResolvingBookmarkData:bookmark
options:opts
relativeToURL:nil
bookmarkDataIsStale:nil
error:nil];

if (resolvedURL != nil) {
fileURL = resolvedURL;
}
}
}

return (BOOL) SDL_SendDropFile([[fileURL path] UTF8String]);
}

- (BOOL)wantsPeriodicDraggingUpdates
{
return NO;
}

@end


Expand Down Expand Up @@ -1120,6 +1172,9 @@ - (void)resetCursorRects
[nswindow setContentView: contentView];
[contentView release];

/* Allow files and folders to be dragged onto the window by users */
[nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];

[pool release];

if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
Expand Down Expand Up @@ -1351,6 +1406,7 @@ - (void)resetCursorRects
[data->listener close];
data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
[data->nswindow setContentView:[nswindow contentView]];
[data->nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
/* See comment in SetupWindowData. */
[data->nswindow setOneShot:NO];
[data->listener listen:data];
Expand Down

0 comments on commit 9453913

Please sign in to comment.