clipboard: exchange files with Explorer and the Finder

Uses CF_HDROP and Preferred DropEffect on Windows and
NSFilenamesPboardType on the Mac.
This commit is contained in:
Daemonratte 2026-10-18 17:24:39 +02:00
commit 41200466c0
9 changed files with 308 additions and 32 deletions

View file

@ -3755,6 +3755,21 @@ gdk_event_translate (MSG *msg,
break;
}
}
else if (msg->wParam == CF_HDROP)
{
/* the URIs of the clipboard as files for Explorer */
_delayed_rendering_data =
_gdk_win32_selection_convert_to_hdrop (_delayed_rendering_data);
if (!_delayed_rendering_data)
break;
}
else if (msg->wParam == _cf_preferred_drop_effect)
{
_delayed_rendering_data =
_gdk_win32_selection_convert_to_drop_effect (_delayed_rendering_data);
if (!_delayed_rendering_data)
break;
}
/* The requestor is holding the clipboard, no
* OpenClipboard() is required/possible

View file

@ -70,6 +70,8 @@ UINT _cf_png;
UINT _cf_jfif;
UINT _cf_gif;
UINT _cf_url;
UINT _cf_preferred_drop_effect;
GdkAtom _gnome_copied_files;
UINT _cf_html_format;
UINT _cf_text_html;

View file

@ -190,6 +190,9 @@ _gdk_windowing_init (void)
_cf_gif = RegisterClipboardFormat ("GIF");
_cf_url = RegisterClipboardFormat ("UniformResourceLocatorW");
/* Explorer: whether files on the clipboard were cut or copied */
_cf_preferred_drop_effect = RegisterClipboardFormat ("Preferred DropEffect");
_gnome_copied_files = gdk_atom_intern_static_string ("x-special/gnome-copied-files");
_cf_html_format = RegisterClipboardFormat ("HTML Format");
_cf_text_html = RegisterClipboardFormat ("text/html");

View file

@ -470,6 +470,13 @@ extern UINT _cf_png;
extern UINT _cf_jfif;
extern UINT _cf_gif;
extern UINT _cf_url;
extern UINT _cf_preferred_drop_effect;
gchar *_gdk_win32_hdrop_to_uri_list (HGLOBAL hdata,
const gchar *separator);
HGLOBAL _gdk_win32_selection_convert_to_hdrop (HGLOBAL hdata);
HGLOBAL _gdk_win32_selection_convert_to_drop_effect (HGLOBAL hdata);
extern GdkAtom _gnome_copied_files;
extern UINT _cf_html_format;
extern UINT _cf_text_html;

View file

@ -35,6 +35,8 @@
#include "gdkdisplay.h"
#include "gdkprivate-win32.h"
#include <shlobj.h> /* DROPFILES, DROPEFFECT_* of Explorer */
/* We emulate the GDK_SELECTION window properties of windows (as used
* in the X11 backend) by using a hash table from window handles to
* GdkSelProp structs.
@ -420,11 +422,14 @@ gdk_selection_convert (GdkWindow *requestor,
gboolean has_text = FALSE;
gboolean has_png = FALSE;
gboolean has_bmp = FALSE;
gboolean has_uris = FALSE;
gboolean has_copied_files = FALSE;
if (!API_CALL (OpenClipboard, (GDK_WINDOW_HWND (requestor))))
return;
targets = g_new (GdkAtom, CountClipboardFormats ());
/* CF_HDROP can stand for two */
targets = g_new (GdkAtom, CountClipboardFormats () + 2);
ntargets = 0;
for (fmt = 0; 0 != (fmt = EnumClipboardFormats (fmt)); )
@ -475,9 +480,31 @@ gdk_selection_convert (GdkWindow *requestor,
if (!has_png)
targets[ntargets++] = _image_gif;
}
else if (fmt == CF_HDROP)
{
/* files from Explorer, for programs that take URIs and for
* file managers, as long as they are not there as such */
if (!has_uris)
targets[ntargets++] = _text_uri_list;
if (!has_copied_files)
targets[ntargets++] = _gnome_copied_files;
has_uris = has_copied_files = TRUE;
}
else if (GetClipboardFormatName (fmt, sFormat, 80) > 0)
{
if (strcmp (sFormat, "image/bmp") == 0 ||
if (strcmp (sFormat, "text/uri-list") == 0)
{
if (!has_uris)
targets[ntargets++] = _text_uri_list;
has_uris = TRUE;
}
else if (strcmp (sFormat, "x-special/gnome-copied-files") == 0)
{
if (!has_copied_files)
targets[ntargets++] = _gnome_copied_files;
has_copied_files = TRUE;
}
else if (strcmp (sFormat, "image/bmp") == 0 ||
strcmp (sFormat, "image/x-bmp") == 0 ||
strcmp (sFormat, "image/x-MS-bmp") == 0 ||
strcmp (sFormat, "image/x-icon") == 0 ||
@ -740,6 +767,55 @@ gdk_selection_convert (GdkWindow *requestor,
}
}
API_CALL (CloseClipboard, ());
}
else if (selection == GDK_SELECTION_CLIPBOARD &&
(target == _text_uri_list || target == _gnome_copied_files) &&
!IsClipboardFormatAvailable (RegisterClipboardFormat (target == _text_uri_list
? "text/uri-list"
: "x-special/gnome-copied-files")) &&
IsClipboardFormatAvailable (CF_HDROP))
{
/* files from Explorer */
gchar *text = NULL;
if (!API_CALL (OpenClipboard, (GDK_WINDOW_HWND (requestor))))
return;
if ((hdata = GetClipboardData (CF_HDROP)) != NULL)
{
HGLOBAL effect_data = GetClipboardData (_cf_preferred_drop_effect);
DWORD effect = DROPEFFECT_COPY;
gchar *uris = _gdk_win32_hdrop_to_uri_list (hdata, target == _text_uri_list ? "\r\n" : "\n");
if (effect_data != NULL && GlobalSize (effect_data) >= sizeof (DWORD))
{
DWORD *ptr = GlobalLock (effect_data);
if (ptr)
{
effect = *ptr;
GlobalUnlock (effect_data);
}
}
if (target == _text_uri_list)
text = uris;
else
{
/* as Nautilus: no newline after the last */
if (g_str_has_suffix (uris, "\n"))
uris[strlen (uris) - 1] = '\0';
text = g_strconcat ((effect & DROPEFFECT_MOVE) ? "cut\n" : "copy\n", uris, NULL);
g_free (uris);
}
}
if (text)
selection_property_store (requestor, target, 8, (guchar *) text, strlen (text));
else
property = GDK_NONE;
API_CALL (CloseClipboard, ());
}
else if (selection == GDK_SELECTION_CLIPBOARD)
@ -1244,6 +1320,26 @@ gdk_win32_selection_add_targets (GdkWindow *owner,
targets[i] == text_plain)
continue;
/* files: Explorer takes them as CF_HDROP, and whether they were
* cut from Preferred DropEffect, made from the list of the
* clipboard of file managers when asked for */
if (targets[i] == _text_uri_list)
{
g_hash_table_replace (_format_atom_table,
GINT_TO_POINTER (CF_HDROP),
targets[i]);
GDK_NOTE (DND, g_print ("... SetClipboardData(CF_HDROP,NULL)\n"));
SetClipboardData (CF_HDROP, NULL);
}
else if (targets[i] == _gnome_copied_files)
{
g_hash_table_replace (_format_atom_table,
GINT_TO_POINTER (_cf_preferred_drop_effect),
targets[i]);
GDK_NOTE (DND, g_print ("... SetClipboardData(Preferred DropEffect,NULL)\n"));
SetClipboardData (_cf_preferred_drop_effect, NULL);
}
target_name = gdk_atom_name (targets[i]);
if (g_str_has_prefix (target_name, "text/plain;charset="))
@ -1310,3 +1406,118 @@ _gdk_win32_selection_convert_to_dib (HGLOBAL hdata,
return NULL;
}
/* The files of CF_HDROP (Explorer) as URIs, each followed by @separator */
gchar *
_gdk_win32_hdrop_to_uri_list (HGLOBAL hdata,
const gchar *separator)
{
HDROP hdrop = (HDROP) hdata;
GString *result = g_string_new (NULL);
UINT i, n;
n = DragQueryFileW (hdrop, 0xFFFFFFFF, NULL, 0);
for (i = 0; i < n; i++)
{
UINT length = DragQueryFileW (hdrop, i, NULL, 0);
wchar_t *wpath = g_new0 (wchar_t, length + 1);
gchar *path, *uri;
DragQueryFileW (hdrop, i, wpath, length + 1);
path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
uri = path ? g_filename_to_uri (path, NULL, NULL) : NULL;
if (uri)
{
g_string_append (result, uri);
g_string_append (result, separator);
}
g_free (uri);
g_free (path);
g_free (wpath);
}
return g_string_free (result, FALSE);
}
/* A list of URIs (text/uri-list), as rendered by the owner of the
* clipboard, as CF_HDROP for Explorer: the local files of it. Takes
* @hdata. */
HGLOBAL
_gdk_win32_selection_convert_to_hdrop (HGLOBAL hdata)
{
gchar *text, **uris;
GArray *paths;
HGLOBAL result = NULL;
gint i;
text = g_strndup (GlobalLock (hdata), GlobalSize (hdata));
GlobalUnlock (hdata);
GlobalFree (hdata);
uris = g_uri_list_extract_uris (text);
g_free (text);
paths = g_array_new (FALSE, FALSE, sizeof (wchar_t));
for (i = 0; uris && uris[i]; i++)
{
gchar *path = g_filename_from_uri (uris[i], NULL, NULL);
gunichar2 *wpath;
glong length;
if (path == NULL)
continue;
wpath = g_utf8_to_utf16 (path, -1, NULL, &length, NULL);
if (wpath)
g_array_append_vals (paths, wpath, length + 1);
g_free (wpath);
g_free (path);
}
g_strfreev (uris);
if (paths->len > 0)
{
wchar_t zero = 0;
DROPFILES *drop;
g_array_append_val (paths, zero);
result = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT,
sizeof (DROPFILES) + paths->len * sizeof (wchar_t));
if (result)
{
drop = GlobalLock (result);
drop->pFiles = sizeof (DROPFILES);
drop->fWide = TRUE;
memcpy ((guchar *) drop + sizeof (DROPFILES), paths->data, paths->len * sizeof (wchar_t));
GlobalUnlock (result);
}
}
g_array_free (paths, TRUE);
return result;
}
/* The clipboard of file managers ("cut" or "copy" and URIs) as the
* Preferred DropEffect of Explorer. Takes @hdata. */
HGLOBAL
_gdk_win32_selection_convert_to_drop_effect (HGLOBAL hdata)
{
gchar *text = g_strndup (GlobalLock (hdata), GlobalSize (hdata));
HGLOBAL result;
DWORD effect;
GlobalUnlock (hdata);
GlobalFree (hdata);
effect = g_str_has_prefix (text, "cut") ? DROPEFFECT_MOVE : (DROPEFFECT_COPY | DROPEFFECT_LINK);
g_free (text);
result = GlobalAlloc (GMEM_MOVEABLE, sizeof (DWORD));
if (result)
{
DWORD *ptr = GlobalLock (result);
*ptr = effect;
GlobalUnlock (result);
}
return result;
}

View file

@ -111,7 +111,10 @@ static GtkClipboard *clipboard_peek (GdkDisplay *display,
memset (&selection_data, 0, sizeof (GtkSelectionData));
selection_data.selection = clipboard->selection;
selection_data.target = gdk_quartz_pasteboard_type_to_atom_libgtk_only (type);
/* the Finder takes files of NSFilenamesPboardType */
selection_data.target = [type isEqualToString:NSFilenamesPboardType]
? gdk_atom_intern_static_string ("text/uri-list")
: gdk_quartz_pasteboard_type_to_atom_libgtk_only (type);
selection_data.display = gdk_display_get_default ();
selection_data.length = -1;
@ -122,8 +125,14 @@ static GtkClipboard *clipboard_peek (GdkDisplay *display,
clipboard->user_data);
if (selection_data.length >= 0)
_gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard,
&selection_data);
{
if ([type isEqualToString:NSFilenamesPboardType])
_gtk_quartz_set_filenames_for_pasteboard (clipboard->pasteboard,
&selection_data);
else
_gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard,
&selection_data);
}
g_free (selection_data.data);
}
@ -364,6 +373,10 @@ gtk_clipboard_set_contents (GtkClipboard *clipboard,
pool = [[NSAutoreleasePool alloc] init];
types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets);
/* files for the Finder too, as file managers put them there; the set
* is a mutable one, made for us */
if (_gtk_quartz_targets_are_files (targets, n_targets))
[(NSMutableSet *) types addObject:NSFilenamesPboardType];
/* call declareTypes before setting the clipboard members because
* declareTypes might clear the clipboard

View file

@ -143,32 +143,6 @@ struct _GtkDragFindData
@end
/* The local files of a URI list, for the Finder, which takes only one
* URL from NSURLPboardType */
static void
set_filenames_for_pasteboard (NSPasteboard *pasteboard,
GtkSelectionData *selection_data)
{
gchar **uris = gtk_selection_data_get_uris (selection_data);
NSMutableArray *paths = [NSMutableArray array];
gint i;
for (i = 0; uris && uris[i]; i++)
{
gchar *path = g_filename_from_uri (uris[i], NULL, NULL);
if (path)
{
[paths addObject:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:path
length:strlen (path)]];
g_free (path);
}
}
g_strfreev (uris);
[pasteboard setPropertyList:paths forType:NSFilenamesPboardType];
}
@implementation GtkDragSourceOwner
-(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
{
@ -198,7 +172,7 @@ set_filenames_for_pasteboard (NSPasteboard *pasteboard,
if (selection_data.length >= 0)
{
if (filenames)
set_filenames_for_pasteboard (sender, &selection_data);
_gtk_quartz_set_filenames_for_pasteboard (sender, &selection_data);
else
_gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
}

View file

@ -306,6 +306,53 @@ _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
forType:type];
}
/* The local files of a URI list, for the Finder, which takes only one
* URL from NSURLPboardType: of a drag, and of the clipboard of files */
void
_gtk_quartz_set_filenames_for_pasteboard (NSPasteboard *pasteboard,
GtkSelectionData *selection_data)
{
gchar **uris = gtk_selection_data_get_uris (selection_data);
NSMutableArray *paths = [NSMutableArray array];
gint i;
for (i = 0; uris && uris[i]; i++)
{
gchar *path = g_filename_from_uri (uris[i], NULL, NULL);
if (path)
{
[paths addObject:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:path
length:strlen (path)]];
g_free (path);
}
}
g_strfreev (uris);
[pasteboard setPropertyList:paths forType:NSFilenamesPboardType];
}
/* Whether a clipboard holds files as file managers put them there
* (the URIs and x-special/gnome-copied-files, as the file chooser does),
* so that the Finder can paste them */
gboolean
_gtk_quartz_targets_are_files (const GtkTargetEntry *targets,
guint n_targets)
{
gboolean uris = FALSE, files = FALSE;
guint i;
for (i = 0; i < n_targets; i++)
{
if (strcmp (targets[i].target, "text/uri-list") == 0)
uris = TRUE;
else if (strcmp (targets[i].target, "x-special/gnome-copied-files") == 0)
files = TRUE;
}
return uris && files;
}
/*
* Bundle-based functions for various directories. These almost work
* even when the application isn't in a bundle, becuase mainBundle

View file

@ -38,6 +38,10 @@ GtkSelectionData *_gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *
void _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
GtkSelectionData *selection_data);
void _gtk_quartz_set_filenames_for_pasteboard (NSPasteboard *pasteboard,
GtkSelectionData *selection_data);
gboolean _gtk_quartz_targets_are_files (const GtkTargetEntry *targets,
guint n_targets);
gboolean _gtk_quartz_trash_file (GFile *file,
GFile **trashed,