Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
First pass at a simple drag and drop API, allowing you to accept file…
Browse files Browse the repository at this point in the history
…s dropped into your application.
  • Loading branch information
slouken committed Nov 21, 2011
1 parent 692d723 commit 3fa9a0b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
20 changes: 18 additions & 2 deletions include/SDL_events.h
Expand Up @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
/*@{*/
Expand Down
47 changes: 47 additions & 0 deletions src/events/SDL_dropevents.c
@@ -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: */
30 changes: 30 additions & 0 deletions src/events/SDL_dropevents_c.h
@@ -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: */
1 change: 1 addition & 0 deletions src/events/SDL_events.c
Expand Up @@ -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 */
Expand Down
6 changes: 4 additions & 2 deletions src/events/SDL_events_c.h
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/video/cocoa/SDL_cocoaevents.m
Expand Up @@ -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 *
Expand Down

0 comments on commit 3fa9a0b

Please sign in to comment.