src/events/SDL_dropevents.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 05 Jan 2016 01:42:00 -0500
changeset 10022 30807689ca1b
parent 10020 08b9e569e1ce
child 10042 8546f8015052
permissions -rw-r--r--
Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.

This allows an app to know when a set of drops are coming in a grouping of
some sort (for example, a user selected multiple files and dropped them all
on the window with a single drag), and when that set is complete.

This also adds a window ID to the drop events, so the app can determine to
which window a given drop was delivered. For application-level drops (for
example, you launched an app by dropping a file on its icon), the window ID
will be zero.
slouken@6091
     1
/*
slouken@6091
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@6091
     4
slouken@6091
     5
  This software is provided 'as-is', without any express or implied
slouken@6091
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6091
     7
  arising from the use of this software.
slouken@6091
     8
slouken@6091
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6091
    10
  including commercial applications, and to alter it and redistribute it
slouken@6091
    11
  freely, subject to the following restrictions:
slouken@6091
    12
slouken@6091
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6091
    14
     claim that you wrote the original software. If you use this software
slouken@6091
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6091
    16
     appreciated but is not required.
slouken@6091
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6091
    18
     misrepresented as being the original software.
slouken@6091
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6091
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@6091
    22
slouken@6091
    23
/* Drag and drop event handling code for SDL */
slouken@6091
    24
slouken@6091
    25
#include "SDL_events.h"
slouken@6091
    26
#include "SDL_events_c.h"
slouken@6091
    27
#include "SDL_dropevents_c.h"
slouken@6091
    28
icculus@10022
    29
#include "../video/SDL_sysvideo.h"  /* for SDL_Window internals. */
icculus@10022
    30
slouken@6091
    31
icculus@10020
    32
static int
icculus@10022
    33
SDL_SendDrop(SDL_Window *window, const SDL_EventType evtype, const char *data)
slouken@6091
    34
{
icculus@10022
    35
    static SDL_bool app_is_dropping = SDL_FALSE;
icculus@10022
    36
    int posted = 0;
slouken@6091
    37
slouken@6091
    38
    /* Post the event, if desired */
icculus@10020
    39
    if (SDL_GetEventState(evtype) == SDL_ENABLE) {
icculus@10022
    40
        const SDL_bool need_begin = window ? !window->is_dropping : !app_is_dropping;
slouken@6091
    41
        SDL_Event event;
icculus@10022
    42
icculus@10022
    43
        if (need_begin) {
icculus@10022
    44
            SDL_zero(event);
icculus@10022
    45
            event.type = SDL_DROPBEGIN;
icculus@10022
    46
            event.drop.windowID = window->id;
icculus@10022
    47
            posted = (SDL_PushEvent(&event) > 0);
icculus@10022
    48
            if (!posted) {
icculus@10022
    49
                return 0;
icculus@10022
    50
            }
icculus@10022
    51
            if (window) {
icculus@10022
    52
                window->is_dropping = SDL_TRUE;
icculus@10022
    53
            } else {
icculus@10022
    54
                app_is_dropping = SDL_TRUE;
icculus@10022
    55
            }
icculus@10022
    56
        }
icculus@10022
    57
icculus@10020
    58
        SDL_zero(event);
icculus@10020
    59
        event.type = evtype;
icculus@10022
    60
        event.drop.file = data ? SDL_strdup(data) : NULL;
icculus@10022
    61
        event.drop.windowID = window ? window->id : 0;
slouken@6091
    62
        posted = (SDL_PushEvent(&event) > 0);
icculus@10022
    63
icculus@10022
    64
        if (posted && (evtype == SDL_DROPCOMPLETE)) {
icculus@10022
    65
            if (window) {
icculus@10022
    66
                window->is_dropping = SDL_FALSE;
icculus@10022
    67
            } else {
icculus@10022
    68
                app_is_dropping = SDL_FALSE;
icculus@10022
    69
            }
icculus@10022
    70
        }
slouken@6091
    71
    }
icculus@10020
    72
    return posted;
icculus@10020
    73
}
icculus@10020
    74
icculus@10020
    75
int
icculus@10022
    76
SDL_SendDropFile(SDL_Window *window, const char *file)
icculus@10020
    77
{
icculus@10022
    78
    return SDL_SendDrop(window, SDL_DROPFILE, file);
icculus@10020
    79
}
icculus@10020
    80
icculus@10020
    81
int
icculus@10022
    82
SDL_SendDropText(SDL_Window *window, const char *text)
icculus@10020
    83
{
icculus@10022
    84
    return SDL_SendDrop(window, SDL_DROPTEXT, text);
slouken@6091
    85
}
slouken@6091
    86
icculus@10022
    87
int
icculus@10022
    88
SDL_SendDropComplete(SDL_Window *window)
icculus@10022
    89
{
icculus@10022
    90
    return SDL_SendDrop(window, SDL_DROPCOMPLETE, NULL);
icculus@10022
    91
}
icculus@10022
    92
icculus@10022
    93
slouken@6091
    94
/* vi: set ts=4 sw=4 expandtab: */