mirror of
https://github.com/encounter/SDL.git
synced 2025-12-18 09:25:29 +00:00
Some drag'and'drop improvements.
First: disable d'n'd events by default; most apps don't need these at all, and if an app doesn't explicitly handle these, each drop on the window will cause a memory leak if the events are enabled. This follows the guidelines we have for SDL_TEXTINPUT events already. Second: when events are enabled or disabled, signal the video layer, as it might be able to inform the OS, causing UI changes or optimizations (for example, dropping a file icon on a Cocoa app that isn't accepting drops will cause macOS to show a rejection animation instead of the drop operation just vanishing into the ether, X11 might show a different cursor when dragging onto an accepting window, etc). Third: fill in the drop event details in the test library and enable the events in testwm.c for making sure this all works as expected.
This commit is contained in:
@@ -304,6 +304,9 @@ struct SDL_VideoDevice
|
||||
/* Hit-testing */
|
||||
int (*SetWindowHitTest)(SDL_Window * window, SDL_bool enabled);
|
||||
|
||||
/* Tell window that app enabled drag'n'drop events */
|
||||
void (*AcceptDragAndDrop)(SDL_Window * window, SDL_bool accept);
|
||||
|
||||
/* * * */
|
||||
/* Data common to all drivers */
|
||||
SDL_bool is_dummy;
|
||||
@@ -454,6 +457,8 @@ extern void SDL_OnApplicationDidEnterBackground(void);
|
||||
extern void SDL_OnApplicationWillEnterForeground(void);
|
||||
extern void SDL_OnApplicationDidBecomeActive(void);
|
||||
|
||||
extern void SDL_ToggleDragAndDropSupport(void);
|
||||
|
||||
#endif /* SDL_sysvideo_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -1331,9 +1331,43 @@ SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool fullscreen)
|
||||
#define CREATE_FLAGS \
|
||||
(SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_SKIP_TASKBAR | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED)
|
||||
|
||||
static SDL_INLINE SDL_bool
|
||||
IsAcceptingDragAndDrop(void)
|
||||
{
|
||||
if ((SDL_GetEventState(SDL_DROPFILE) == SDL_ENABLE) ||
|
||||
(SDL_GetEventState(SDL_DROPTEXT) == SDL_ENABLE)) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
/* prepare a newly-created window */
|
||||
static SDL_INLINE void
|
||||
PrepareDragAndDropSupport(SDL_Window *window)
|
||||
{
|
||||
if (_this->AcceptDragAndDrop) {
|
||||
_this->AcceptDragAndDrop(window, IsAcceptingDragAndDrop());
|
||||
}
|
||||
}
|
||||
|
||||
/* toggle d'n'd for all existing windows. */
|
||||
void
|
||||
SDL_ToggleDragAndDropSupport(void)
|
||||
{
|
||||
if (_this && _this->AcceptDragAndDrop) {
|
||||
const SDL_bool enable = IsAcceptingDragAndDrop();
|
||||
SDL_Window *window;
|
||||
for (window = _this->windows; window; window = window->next) {
|
||||
_this->AcceptDragAndDrop(window, enable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
|
||||
{
|
||||
PrepareDragAndDropSupport(window);
|
||||
|
||||
if (flags & SDL_WINDOW_MAXIMIZED) {
|
||||
SDL_MaximizeWindow(window);
|
||||
}
|
||||
@@ -1552,6 +1586,9 @@ SDL_CreateWindowFrom(const void *data)
|
||||
SDL_DestroyWindow(window);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PrepareDragAndDropSupport(window);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
|
||||
@@ -105,6 +105,7 @@ Cocoa_CreateDevice(int devindex)
|
||||
device->DestroyWindow = Cocoa_DestroyWindow;
|
||||
device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
|
||||
device->SetWindowHitTest = Cocoa_SetWindowHitTest;
|
||||
device->AcceptDragAndDrop = Cocoa_AcceptDragAndDrop;
|
||||
|
||||
device->shape_driver.CreateShaper = Cocoa_CreateShaper;
|
||||
device->shape_driver.SetWindowShape = Cocoa_SetWindowShape;
|
||||
|
||||
@@ -148,6 +148,7 @@ extern void Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
|
||||
extern void Cocoa_DestroyWindow(_THIS, SDL_Window * window);
|
||||
extern SDL_bool Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info);
|
||||
extern int Cocoa_SetWindowHitTest(SDL_Window *window, SDL_bool enabled);
|
||||
extern void Cocoa_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept);
|
||||
|
||||
#endif /* SDL_cocoawindow_h_ */
|
||||
|
||||
|
||||
@@ -1354,9 +1354,6 @@ Cocoa_CreateWindow(_THIS, SDL_Window * window)
|
||||
[nswindow setContentView:contentView];
|
||||
[contentView release];
|
||||
|
||||
/* Allow files and folders to be dragged onto the window by users */
|
||||
[nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
|
||||
|
||||
if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
|
||||
[nswindow release];
|
||||
return -1;
|
||||
@@ -1864,6 +1861,17 @@ Cocoa_SetWindowHitTest(SDL_Window * window, SDL_bool enabled)
|
||||
return 0; /* just succeed, the real work is done elsewhere. */
|
||||
}
|
||||
|
||||
void
|
||||
Cocoa_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept)
|
||||
{
|
||||
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
|
||||
if (accept) {
|
||||
[data->nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
|
||||
} else {
|
||||
[data->nswindow unregisterDraggedTypes];
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
Cocoa_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
|
||||
{
|
||||
|
||||
@@ -164,6 +164,7 @@ WIN_CreateDevice(int devindex)
|
||||
device->DestroyWindowFramebuffer = WIN_DestroyWindowFramebuffer;
|
||||
device->OnWindowEnter = WIN_OnWindowEnter;
|
||||
device->SetWindowHitTest = WIN_SetWindowHitTest;
|
||||
device->AcceptDragAndDrop = WIN_AcceptDragAndDrop;
|
||||
|
||||
device->shape_driver.CreateShaper = Win32_CreateShaper;
|
||||
device->shape_driver.SetWindowShape = Win32_SetWindowShape;
|
||||
|
||||
@@ -289,9 +289,6 @@ SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, HWND parent, SDL_bool cre
|
||||
videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
|
||||
}
|
||||
|
||||
/* Enable dropping files */
|
||||
DragAcceptFiles(hwnd, TRUE);
|
||||
|
||||
data->initializing = SDL_FALSE;
|
||||
|
||||
/* All done! */
|
||||
@@ -979,6 +976,13 @@ WIN_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
WIN_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept)
|
||||
{
|
||||
const SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
|
||||
DragAcceptFiles(data->hwnd, accept ? TRUE : FALSE);
|
||||
}
|
||||
|
||||
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -78,6 +78,7 @@ extern SDL_bool WIN_GetWindowWMInfo(_THIS, SDL_Window * window,
|
||||
extern void WIN_OnWindowEnter(_THIS, SDL_Window * window);
|
||||
extern void WIN_UpdateClipCursor(SDL_Window *window);
|
||||
extern int WIN_SetWindowHitTest(SDL_Window *window, SDL_bool enabled);
|
||||
extern void WIN_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept);
|
||||
|
||||
#endif /* SDL_windowswindow_h_ */
|
||||
|
||||
|
||||
@@ -260,6 +260,7 @@ X11_CreateDevice(int devindex)
|
||||
device->DestroyWindowFramebuffer = X11_DestroyWindowFramebuffer;
|
||||
device->GetWindowWMInfo = X11_GetWindowWMInfo;
|
||||
device->SetWindowHitTest = X11_SetWindowHitTest;
|
||||
device->AcceptDragAndDrop = X11_AcceptDragAndDrop;
|
||||
|
||||
device->shape_driver.CreateShaper = X11_CreateShaper;
|
||||
device->shape_driver.SetWindowShape = X11_SetWindowShape;
|
||||
|
||||
@@ -390,7 +390,6 @@ X11_CreateWindow(_THIS, SDL_Window * window)
|
||||
const char *wintype_name = NULL;
|
||||
long compositor = 1;
|
||||
Atom _NET_WM_PID;
|
||||
Atom XdndAware, xdnd_version = 5;
|
||||
long fevent = 0;
|
||||
|
||||
#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_EGL
|
||||
@@ -651,11 +650,6 @@ X11_CreateWindow(_THIS, SDL_Window * window)
|
||||
PropertyChangeMask | StructureNotifyMask |
|
||||
KeymapStateMask | fevent));
|
||||
|
||||
XdndAware = X11_XInternAtom(display, "XdndAware", False);
|
||||
X11_XChangeProperty(display, w, XdndAware, XA_ATOM, 32,
|
||||
PropModeReplace,
|
||||
(unsigned char*)&xdnd_version, 1);
|
||||
|
||||
X11_XFlush(display);
|
||||
|
||||
return 0;
|
||||
@@ -1604,6 +1598,22 @@ X11_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
|
||||
return 0; /* just succeed, the real work is done elsewhere. */
|
||||
}
|
||||
|
||||
void
|
||||
X11_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept)
|
||||
{
|
||||
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
|
||||
Display *display = data->videodata->display;
|
||||
Atom XdndAware = X11_XInternAtom(display, "XdndAware", False);
|
||||
|
||||
if (accept) {
|
||||
Atom xdnd_version = 5;
|
||||
X11_XChangeProperty(display, data->xwindow, XdndAware, XA_ATOM, 32,
|
||||
PropModeReplace, (unsigned char*)&xdnd_version, 1);
|
||||
} else {
|
||||
X11_XDeleteProperty(display, data->xwindow, XdndAware);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* SDL_VIDEO_DRIVER_X11 */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -104,6 +104,7 @@ extern void X11_DestroyWindow(_THIS, SDL_Window * window);
|
||||
extern SDL_bool X11_GetWindowWMInfo(_THIS, SDL_Window * window,
|
||||
struct SDL_SysWMinfo *info);
|
||||
extern int X11_SetWindowHitTest(SDL_Window *window, SDL_bool enabled);
|
||||
extern void X11_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept);
|
||||
|
||||
#endif /* SDL_x11window_h_ */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user