First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
authorSam Lantinga <slouken@libsdl.org>
Sun, 20 Nov 2011 19:38:18 -0500
changeset 6091 61fa4f3238c5
parent 6090 687400b595ab
child 6092 9637b8814116
First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
include/SDL_events.h
src/events/SDL_dropevents.c
src/events/SDL_dropevents_c.h
src/events/SDL_events.c
src/events/SDL_events_c.h
src/video/cocoa/SDL_cocoaevents.m
--- a/include/SDL_events.h	Sat Nov 19 19:23:33 2011 -0500
+++ b/include/SDL_events.h	Sun Nov 20 19:38:18 2011 -0500
@@ -104,8 +104,10 @@
     SDL_MULTIGESTURE,
 
     /* Clipboard events */
+    SDL_CLIPBOARDUPDATE = 0x900, /**< The clipboard changed */
 
-    SDL_CLIPBOARDUPDATE = 0x900, /**< The clipboard changed */
+    /* Drag and drop events */
+    SDL_DROPFILE        = 0x1000, /**< The system requests a file open */
 
     /* Obsolete events */
     SDL_EVENT_COMPAT1 = 0x7000, /**< SDL 1.2 events for compatibility */
@@ -350,6 +352,18 @@
 
 
 /**
+ *  \brief An event used to request a file open by the system (event.drop.*)
+ *         This event is disabled by default, you can enable it with SDL_EventState()
+ *  \note If you enable this event, you must free the filename in the event.
+ */
+typedef struct SDL_DropEvent
+{
+    Uint32 type;        /**< ::SDL_DROPFILE */
+    char *file;         /**< The file name, which should be freed with SDL_free() */
+} SDL_DropEvent;
+
+
+/**
  *  \brief The "quit requested" event
  */
 typedef struct SDL_QuitEvent
@@ -376,7 +390,8 @@
 
 /**
  *  \brief A video driver dependent system event (event.syswm.*)
- *  
+ *         This event is disabled by default, you can enable it with SDL_EventState()
+ *
  *  \note If you want to use this event, you should include SDL_syswm.h.
  */
 typedef struct SDL_SysWMEvent
@@ -437,6 +452,7 @@
     SDL_TouchButtonEvent tbutton;   /**< Touch button event data */
     SDL_MultiGestureEvent mgesture; /**< Multi Finger Gesture data */
     SDL_DollarGestureEvent dgesture; /**< Multi Finger Gesture data */
+    SDL_DropEvent drop;             /**< Drag and drop event data */
 
     /** Temporarily here for backwards compatibility */
     /*@{*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/events/SDL_dropevents.c	Sun Nov 20 19:38:18 2011 -0500
@@ -0,0 +1,47 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_config.h"
+
+/* Drag and drop event handling code for SDL */
+
+#include "SDL_events.h"
+#include "SDL_events_c.h"
+#include "SDL_dropevents_c.h"
+
+
+int
+SDL_SendDropFile(const char *file)
+{
+    int posted;
+
+    /* Post the event, if desired */
+    posted = 0;
+    if (SDL_GetEventState(SDL_DROPFILE) == SDL_ENABLE) {
+        SDL_Event event;
+        event.type = SDL_DROPFILE;
+	event.drop.file = SDL_strdup(file);
+
+        posted = (SDL_PushEvent(&event) > 0);
+    }
+    return (posted);
+}
+
+/* vi: set ts=4 sw=4 expandtab: */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/events/SDL_dropevents_c.h	Sun Nov 20 19:38:18 2011 -0500
@@ -0,0 +1,30 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_config.h"
+
+#ifndef _SDL_dropevents_c_h
+#define _SDL_dropevents_c_h
+
+extern int SDL_SendDropFile(const char *file);
+
+#endif /* _SDL_dropevents_c_h */
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/events/SDL_events.c	Sat Nov 19 19:23:33 2011 -0500
+++ b/src/events/SDL_events.c	Sun Nov 20 19:38:18 2011 -0500
@@ -121,6 +121,7 @@
 
     /* No filter to start with, process most event types */
     SDL_EventOK = NULL;
+    SDL_EventState(SDL_DROPFILE, SDL_DISABLE);
     SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
 
     /* Create the lock and set ourselves active */
--- a/src/events/SDL_events_c.h	Sat Nov 19 19:23:33 2011 -0500
+++ b/src/events/SDL_events_c.h	Sun Nov 20 19:38:18 2011 -0500
@@ -23,11 +23,13 @@
 /* Useful functions and variables from SDL_events.c */
 #include "SDL_events.h"
 #include "SDL_thread.h"
+#include "SDL_clipboardevents_c.h"
+#include "SDL_dropevents_c.h"
+#include "SDL_gesture_c.h"
+#include "SDL_keyboard_c.h"
 #include "SDL_mouse_c.h"
-#include "SDL_keyboard_c.h"
 #include "SDL_touch_c.h"
 #include "SDL_windowevents_c.h"
-#include "SDL_gesture_c.h"
 
 /* Start and stop the event processing loop */
 extern int SDL_StartEventLoop(void);
--- a/src/video/cocoa/SDL_cocoaevents.m	Sat Nov 19 19:23:33 2011 -0500
+++ b/src/video/cocoa/SDL_cocoaevents.m	Sun Nov 20 19:38:18 2011 -0500
@@ -51,6 +51,11 @@
     SDL_SendQuit();
     return NSTerminateCancel;
 }
+
+- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
+{
+    return (BOOL)SDL_SendDropFile([filename UTF8String]);
+}
 @end
 
 static NSString *