First pass at a simple drag and drop API, allowing you to accept files dropped into your application.

This commit is contained in:
Sam Lantinga 2011-11-20 19:38:18 -05:00
parent 269da4cbfb
commit f52f040b3d
6 changed files with 105 additions and 4 deletions

View file

@ -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 */
/*@{*/

View file

@ -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: */

View file

@ -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: */

View file

@ -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 */

View file

@ -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);

View file

@ -51,6 +51,11 @@
SDL_SendQuit();
return NSTerminateCancel;
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
return (BOOL)SDL_SendDropFile([filename UTF8String]);
}
@end
static NSString *