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 609161fa4f3238c5
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
     1.1 --- a/include/SDL_events.h	Sat Nov 19 19:23:33 2011 -0500
     1.2 +++ b/include/SDL_events.h	Sun Nov 20 19:38:18 2011 -0500
     1.3 @@ -104,8 +104,10 @@
     1.4      SDL_MULTIGESTURE,
     1.5  
     1.6      /* Clipboard events */
     1.7 +    SDL_CLIPBOARDUPDATE = 0x900, /**< The clipboard changed */
     1.8  
     1.9 -    SDL_CLIPBOARDUPDATE = 0x900, /**< The clipboard changed */
    1.10 +    /* Drag and drop events */
    1.11 +    SDL_DROPFILE        = 0x1000, /**< The system requests a file open */
    1.12  
    1.13      /* Obsolete events */
    1.14      SDL_EVENT_COMPAT1 = 0x7000, /**< SDL 1.2 events for compatibility */
    1.15 @@ -350,6 +352,18 @@
    1.16  
    1.17  
    1.18  /**
    1.19 + *  \brief An event used to request a file open by the system (event.drop.*)
    1.20 + *         This event is disabled by default, you can enable it with SDL_EventState()
    1.21 + *  \note If you enable this event, you must free the filename in the event.
    1.22 + */
    1.23 +typedef struct SDL_DropEvent
    1.24 +{
    1.25 +    Uint32 type;        /**< ::SDL_DROPFILE */
    1.26 +    char *file;         /**< The file name, which should be freed with SDL_free() */
    1.27 +} SDL_DropEvent;
    1.28 +
    1.29 +
    1.30 +/**
    1.31   *  \brief The "quit requested" event
    1.32   */
    1.33  typedef struct SDL_QuitEvent
    1.34 @@ -376,7 +390,8 @@
    1.35  
    1.36  /**
    1.37   *  \brief A video driver dependent system event (event.syswm.*)
    1.38 - *  
    1.39 + *         This event is disabled by default, you can enable it with SDL_EventState()
    1.40 + *
    1.41   *  \note If you want to use this event, you should include SDL_syswm.h.
    1.42   */
    1.43  typedef struct SDL_SysWMEvent
    1.44 @@ -437,6 +452,7 @@
    1.45      SDL_TouchButtonEvent tbutton;   /**< Touch button event data */
    1.46      SDL_MultiGestureEvent mgesture; /**< Multi Finger Gesture data */
    1.47      SDL_DollarGestureEvent dgesture; /**< Multi Finger Gesture data */
    1.48 +    SDL_DropEvent drop;             /**< Drag and drop event data */
    1.49  
    1.50      /** Temporarily here for backwards compatibility */
    1.51      /*@{*/
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/events/SDL_dropevents.c	Sun Nov 20 19:38:18 2011 -0500
     2.3 @@ -0,0 +1,47 @@
     2.4 +/*
     2.5 +  Simple DirectMedia Layer
     2.6 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 +  This software is provided 'as-is', without any express or implied
     2.9 +  warranty.  In no event will the authors be held liable for any damages
    2.10 +  arising from the use of this software.
    2.11 +
    2.12 +  Permission is granted to anyone to use this software for any purpose,
    2.13 +  including commercial applications, and to alter it and redistribute it
    2.14 +  freely, subject to the following restrictions:
    2.15 +
    2.16 +  1. The origin of this software must not be misrepresented; you must not
    2.17 +     claim that you wrote the original software. If you use this software
    2.18 +     in a product, an acknowledgment in the product documentation would be
    2.19 +     appreciated but is not required.
    2.20 +  2. Altered source versions must be plainly marked as such, and must not be
    2.21 +     misrepresented as being the original software.
    2.22 +  3. This notice may not be removed or altered from any source distribution.
    2.23 +*/
    2.24 +#include "SDL_config.h"
    2.25 +
    2.26 +/* Drag and drop event handling code for SDL */
    2.27 +
    2.28 +#include "SDL_events.h"
    2.29 +#include "SDL_events_c.h"
    2.30 +#include "SDL_dropevents_c.h"
    2.31 +
    2.32 +
    2.33 +int
    2.34 +SDL_SendDropFile(const char *file)
    2.35 +{
    2.36 +    int posted;
    2.37 +
    2.38 +    /* Post the event, if desired */
    2.39 +    posted = 0;
    2.40 +    if (SDL_GetEventState(SDL_DROPFILE) == SDL_ENABLE) {
    2.41 +        SDL_Event event;
    2.42 +        event.type = SDL_DROPFILE;
    2.43 +	event.drop.file = SDL_strdup(file);
    2.44 +
    2.45 +        posted = (SDL_PushEvent(&event) > 0);
    2.46 +    }
    2.47 +    return (posted);
    2.48 +}
    2.49 +
    2.50 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/events/SDL_dropevents_c.h	Sun Nov 20 19:38:18 2011 -0500
     3.3 @@ -0,0 +1,30 @@
     3.4 +/*
     3.5 +  Simple DirectMedia Layer
     3.6 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 +  This software is provided 'as-is', without any express or implied
     3.9 +  warranty.  In no event will the authors be held liable for any damages
    3.10 +  arising from the use of this software.
    3.11 +
    3.12 +  Permission is granted to anyone to use this software for any purpose,
    3.13 +  including commercial applications, and to alter it and redistribute it
    3.14 +  freely, subject to the following restrictions:
    3.15 +
    3.16 +  1. The origin of this software must not be misrepresented; you must not
    3.17 +     claim that you wrote the original software. If you use this software
    3.18 +     in a product, an acknowledgment in the product documentation would be
    3.19 +     appreciated but is not required.
    3.20 +  2. Altered source versions must be plainly marked as such, and must not be
    3.21 +     misrepresented as being the original software.
    3.22 +  3. This notice may not be removed or altered from any source distribution.
    3.23 +*/
    3.24 +#include "SDL_config.h"
    3.25 +
    3.26 +#ifndef _SDL_dropevents_c_h
    3.27 +#define _SDL_dropevents_c_h
    3.28 +
    3.29 +extern int SDL_SendDropFile(const char *file);
    3.30 +
    3.31 +#endif /* _SDL_dropevents_c_h */
    3.32 +
    3.33 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/events/SDL_events.c	Sat Nov 19 19:23:33 2011 -0500
     4.2 +++ b/src/events/SDL_events.c	Sun Nov 20 19:38:18 2011 -0500
     4.3 @@ -121,6 +121,7 @@
     4.4  
     4.5      /* No filter to start with, process most event types */
     4.6      SDL_EventOK = NULL;
     4.7 +    SDL_EventState(SDL_DROPFILE, SDL_DISABLE);
     4.8      SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
     4.9  
    4.10      /* Create the lock and set ourselves active */
     5.1 --- a/src/events/SDL_events_c.h	Sat Nov 19 19:23:33 2011 -0500
     5.2 +++ b/src/events/SDL_events_c.h	Sun Nov 20 19:38:18 2011 -0500
     5.3 @@ -23,11 +23,13 @@
     5.4  /* Useful functions and variables from SDL_events.c */
     5.5  #include "SDL_events.h"
     5.6  #include "SDL_thread.h"
     5.7 +#include "SDL_clipboardevents_c.h"
     5.8 +#include "SDL_dropevents_c.h"
     5.9 +#include "SDL_gesture_c.h"
    5.10 +#include "SDL_keyboard_c.h"
    5.11  #include "SDL_mouse_c.h"
    5.12 -#include "SDL_keyboard_c.h"
    5.13  #include "SDL_touch_c.h"
    5.14  #include "SDL_windowevents_c.h"
    5.15 -#include "SDL_gesture_c.h"
    5.16  
    5.17  /* Start and stop the event processing loop */
    5.18  extern int SDL_StartEventLoop(void);
     6.1 --- a/src/video/cocoa/SDL_cocoaevents.m	Sat Nov 19 19:23:33 2011 -0500
     6.2 +++ b/src/video/cocoa/SDL_cocoaevents.m	Sun Nov 20 19:38:18 2011 -0500
     6.3 @@ -51,6 +51,11 @@
     6.4      SDL_SendQuit();
     6.5      return NSTerminateCancel;
     6.6  }
     6.7 +
     6.8 +- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
     6.9 +{
    6.10 +    return (BOOL)SDL_SendDropFile([filename UTF8String]);
    6.11 +}
    6.12  @end
    6.13  
    6.14  static NSString *