From 3fa9a0bdde0ad928fb998a932c6e1a6bc068bc31 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 20 Nov 2011 19:38:18 -0500 Subject: [PATCH] First pass at a simple drag and drop API, allowing you to accept files dropped into your application. --- include/SDL_events.h | 20 +++++++++++-- src/events/SDL_dropevents.c | 47 +++++++++++++++++++++++++++++++ src/events/SDL_dropevents_c.h | 30 ++++++++++++++++++++ src/events/SDL_events.c | 1 + src/events/SDL_events_c.h | 6 ++-- src/video/cocoa/SDL_cocoaevents.m | 5 ++++ 6 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/events/SDL_dropevents.c create mode 100644 src/events/SDL_dropevents_c.h diff --git a/include/SDL_events.h b/include/SDL_events.h index 4ba6c0dde..13559a883 100644 --- a/include/SDL_events.h +++ b/include/SDL_events.h @@ -104,9 +104,11 @@ typedef enum SDL_MULTIGESTURE, /* Clipboard events */ - 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 */ SDL_EVENT_COMPAT2, @@ -349,6 +351,18 @@ typedef struct SDL_DollarGestureEvent } SDL_DollarGestureEvent; +/** + * \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 */ @@ -376,7 +390,8 @@ typedef struct SDL_SysWMmsg SDL_SysWMmsg; /** * \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 @@ typedef union SDL_Event 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 */ /*@{*/ diff --git a/src/events/SDL_dropevents.c b/src/events/SDL_dropevents.c new file mode 100644 index 000000000..a84672b44 --- /dev/null +++ b/src/events/SDL_dropevents.c @@ -0,0 +1,47 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2011 Sam Lantinga + + 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: */ diff --git a/src/events/SDL_dropevents_c.h b/src/events/SDL_dropevents_c.h new file mode 100644 index 000000000..2bbe78dbf --- /dev/null +++ b/src/events/SDL_dropevents_c.h @@ -0,0 +1,30 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2011 Sam Lantinga + + 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: */ diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c index 5babb0db6..921c3d9e2 100644 --- a/src/events/SDL_events.c +++ b/src/events/SDL_events.c @@ -121,6 +121,7 @@ SDL_StartEventLoop(void) /* 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 */ diff --git a/src/events/SDL_events_c.h b/src/events/SDL_events_c.h index 8c31de2d7..dc936fe01 100644 --- a/src/events/SDL_events_c.h +++ b/src/events/SDL_events_c.h @@ -23,11 +23,13 @@ /* Useful functions and variables from SDL_events.c */ #include "SDL_events.h" #include "SDL_thread.h" -#include "SDL_mouse_c.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_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); diff --git a/src/video/cocoa/SDL_cocoaevents.m b/src/video/cocoa/SDL_cocoaevents.m index 321ea76ad..8098f5464 100644 --- a/src/video/cocoa/SDL_cocoaevents.m +++ b/src/video/cocoa/SDL_cocoaevents.m @@ -51,6 +51,11 @@ - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sende SDL_SendQuit(); return NSTerminateCancel; } + +- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename +{ + return (BOOL)SDL_SendDropFile([filename UTF8String]); +} @end static NSString *