| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any damages |
| 7 | arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | */ |
| 21 | #include "SDL_internal.h" |
| 22 | |
| 23 | // Drag and drop event handling code for SDL |
| 24 | |
| 25 | #include "SDL_events_c.h" |
| 26 | #include "SDL_dropevents_c.h" |
| 27 | |
| 28 | #include "../video/SDL_sysvideo.h" // for SDL_Window internals. |
| 29 | |
| 30 | static bool SDL_SendDrop(SDL_Window *window, const SDL_EventType evtype, const char *source, const char *data, float x, float y) |
| 31 | { |
| 32 | static bool app_is_dropping = false; |
| 33 | static float last_drop_x = 0; |
| 34 | static float last_drop_y = 0; |
| 35 | bool posted = false; |
| 36 | |
| 37 | // Post the event, if desired |
| 38 | if (SDL_EventEnabled(evtype)) { |
| 39 | const bool need_begin = window ? !window->is_dropping : !app_is_dropping; |
| 40 | SDL_Event event; |
| 41 | |
| 42 | if (need_begin) { |
| 43 | SDL_zero(event); |
| 44 | event.type = SDL_EVENT_DROP_BEGIN; |
| 45 | event.common.timestamp = 0; |
| 46 | event.drop.windowID = window ? window->id : 0; |
| 47 | posted = SDL_PushEvent(&event); |
| 48 | if (!posted) { |
| 49 | return false; |
| 50 | } |
| 51 | if (window) { |
| 52 | window->is_dropping = true; |
| 53 | } else { |
| 54 | app_is_dropping = true; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | SDL_zero(event); |
| 59 | event.type = evtype; |
| 60 | event.common.timestamp = 0; |
| 61 | if (source) { |
| 62 | event.drop.source = SDL_CreateTemporaryString(source); |
| 63 | if (!event.drop.source) { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | if (data) { |
| 68 | event.drop.data = SDL_CreateTemporaryString(data); |
| 69 | if (!event.drop.data) { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | event.drop.windowID = window ? window->id : 0; |
| 74 | |
| 75 | if (evtype == SDL_EVENT_DROP_POSITION) { |
| 76 | last_drop_x = x; |
| 77 | last_drop_y = y; |
| 78 | } |
| 79 | event.drop.x = last_drop_x; |
| 80 | event.drop.y = last_drop_y; |
| 81 | posted = SDL_PushEvent(&event); |
| 82 | |
| 83 | if (posted && (evtype == SDL_EVENT_DROP_COMPLETE)) { |
| 84 | if (window) { |
| 85 | window->is_dropping = false; |
| 86 | } else { |
| 87 | app_is_dropping = false; |
| 88 | } |
| 89 | |
| 90 | last_drop_x = 0; |
| 91 | last_drop_y = 0; |
| 92 | } |
| 93 | } |
| 94 | return posted; |
| 95 | } |
| 96 | |
| 97 | bool SDL_SendDropFile(SDL_Window *window, const char *source, const char *file) |
| 98 | { |
| 99 | return SDL_SendDrop(window, SDL_EVENT_DROP_FILE, source, file, 0, 0); |
| 100 | } |
| 101 | |
| 102 | bool SDL_SendDropPosition(SDL_Window *window, float x, float y) |
| 103 | { |
| 104 | return SDL_SendDrop(window, SDL_EVENT_DROP_POSITION, NULL, NULL, x, y); |
| 105 | } |
| 106 | |
| 107 | bool SDL_SendDropText(SDL_Window *window, const char *text) |
| 108 | { |
| 109 | return SDL_SendDrop(window, SDL_EVENT_DROP_TEXT, NULL, text, 0, 0); |
| 110 | } |
| 111 | |
| 112 | bool SDL_SendDropComplete(SDL_Window *window) |
| 113 | { |
| 114 | return SDL_SendDrop(window, SDL_EVENT_DROP_COMPLETE, NULL, NULL, 0, 0); |
| 115 | } |
| 116 | |