quartz: pass all dragged files to the Finder

This commit is contained in:
Daemonratte 2026-10-16 09:25:00 +02:00
commit ae9bfb033f
3 changed files with 53 additions and 4 deletions

View file

@ -143,16 +143,46 @@ 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
{
guint target_info;
GtkSelectionData selection_data;
gboolean filenames = [type isEqualToString:NSFilenamesPboardType];
selection_data.selection = GDK_NONE;
selection_data.data = NULL;
selection_data.length = -1;
selection_data.target = gdk_quartz_pasteboard_type_to_atom_libgtk_only (type);
selection_data.type = GDK_NONE;
selection_data.format = 0;
selection_data.target = filenames ? gdk_atom_intern_static_string ("text/uri-list")
: gdk_quartz_pasteboard_type_to_atom_libgtk_only (type);
selection_data.display = gdk_display_get_default ();
if (gtk_target_list_find (info->target_list,
@ -166,7 +196,12 @@ struct _GtkDragFindData
time);
if (selection_data.length >= 0)
_gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
{
if (filenames)
set_filenames_for_pasteboard (sender, &selection_data);
else
_gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
}
g_free (selection_data.data);
}
@ -1084,6 +1119,12 @@ gtk_drag_begin_idle (gpointer arg)
owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
types = _gtk_quartz_target_list_to_pasteboard_types (info->target_list);
/* beside the URL, the names of all files, for the Finder, where the
* widget gives local files only (the file chooser says so); the set
* is a mutable one, made for us */
if ([types containsObject:NSURLPboardType] &&
g_object_get_data (G_OBJECT (info->widget), "gtk-drag-local-files"))
[(NSMutableSet *) types addObject:NSFilenamesPboardType];
[pasteboard declareTypes:[types allObjects] owner:owner];

View file

@ -5631,6 +5631,11 @@ file_list_drag_source_connect (GtkFileChooserDefault *impl,
G_CALLBACK (file_list_drag_end_cb), impl);
g_signal_connect_after (view, "drag-data-get",
G_CALLBACK (file_list_drag_data_get_cb), impl);
#ifdef GDK_WINDOWING_QUARTZ
/* files on the Mac are local ones: the Finder is given the names of
* all of them then (gtkdnd-quartz.c) */
g_object_set_data (G_OBJECT (view), I_("gtk-drag-local-files"), GINT_TO_POINTER (TRUE));
#endif
}
/* Sets the sort column IDs for the file list; needs to be done whenever we

View file

@ -288,8 +288,11 @@ _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
result = g_uri_list_extract_uris (list[0]);
url = [NSURL URLWithString:[NSString stringWithUTF8String:result[0]]];
[url writeToPasteboard:pasteboard];
if (result[0])
{
url = [NSURL URLWithString:[NSString stringWithUTF8String:result[0]]];
[url writeToPasteboard:pasteboard];
}
g_strfreev (result);
}