filechooser: drag several selected files at once

This commit is contained in:
Daemonratte 2026-10-16 06:40:51 +02:00
commit d342e73380
No known key found for this signature in database
2 changed files with 185 additions and 0 deletions

View file

@ -894,6 +894,9 @@ gtk_file_chooser_default_finalize (GObject *object)
icon_view_scale_remove_timeout (impl);
if (impl->drag_press_row)
gtk_tree_row_reference_free (impl->drag_press_row);
/* requests hold a reference: none are left */
if (impl->thumbnail_requests)
{
@ -5454,6 +5457,182 @@ list_button_press_event_cb (GtkWidget *widget,
return TRUE;
}
/* Dragging several files, as in file managers: a press on one of the
* selected files keeps them all selected, so that they all go; a click
* without a drag then selects just that one. The views themselves
* would drop the others on the press already. */
static gboolean
file_list_button_press_drag_cb (GtkWidget *widget,
GdkEventButton *event,
GtkFileChooserDefault *impl)
{
GtkTreePath *path = NULL;
GList *selected, *l;
gpointer selection;
/* Command, which selects on the Mac, is not among the default ones */
if (event->type != GDK_BUTTON_PRESS || event->button != 1 ||
(event->state & (gtk_accelerator_get_default_mod_mask () |
GTK_MODIFY_SELECTION_MOD_MASK | GTK_EXTEND_SELECTION_MOD_MASK)) ||
current_selection_count_selected_rows (impl) < 2)
return FALSE;
if (GTK_IS_TREE_VIEW (widget))
{
GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
selection = gtk_tree_view_get_selection (tree_view);
if (event->window != gtk_tree_view_get_bin_window (tree_view) ||
!gtk_tree_view_get_path_at_pos (tree_view, event->x, event->y, &path, NULL, NULL, NULL) ||
!gtk_tree_selection_path_is_selected (selection, path))
goto out;
selected = gtk_tree_selection_get_selected_rows (selection, NULL);
}
else
{
GtkIconView *icon_view = GTK_ICON_VIEW (widget);
selection = icon_view;
if (event->window == widget->window ||
!(path = gtk_icon_view_get_path_at_pos (icon_view, event->x, event->y)) ||
!gtk_icon_view_path_is_selected (icon_view, path))
goto out;
selected = gtk_icon_view_get_selected_items (icon_view);
}
/* the view does its part, the press for a drag, the cursor, focus;
* then the selection is as it was, and nobody hears of the change */
g_signal_handlers_block_by_func (selection, list_selection_changed, impl);
GTK_WIDGET_GET_CLASS (widget)->button_press_event (widget, event);
for (l = selected; l; l = l->next)
{
if (GTK_IS_TREE_VIEW (widget))
gtk_tree_selection_select_path (selection, l->data);
else
gtk_icon_view_select_path (selection, l->data);
}
g_signal_handlers_unblock_by_func (selection, list_selection_changed, impl);
g_list_free_full (selected, (GDestroyNotify) gtk_tree_path_free);
check_preview_change (impl);
/* rows may come meanwhile, while the folder loads */
if (impl->drag_press_row)
gtk_tree_row_reference_free (impl->drag_press_row);
impl->drag_press_row = gtk_tree_row_reference_new (impl->current_model, path);
gtk_tree_path_free (path);
return TRUE;
out:
if (path)
gtk_tree_path_free (path);
return FALSE;
}
static gboolean
file_list_button_release_drag_cb (GtkWidget *widget,
GdkEventButton *event,
GtkFileChooserDefault *impl)
{
GtkTreePath *path;
if (impl->drag_press_row == NULL || event->button != 1)
return FALSE;
path = gtk_tree_row_reference_get_model (impl->drag_press_row) == impl->current_model ?
gtk_tree_row_reference_get_path (impl->drag_press_row) : NULL;
gtk_tree_row_reference_free (impl->drag_press_row);
impl->drag_press_row = NULL;
if (path == NULL)
return FALSE;
current_selection_unselect_all (impl);
if (GTK_IS_TREE_VIEW (widget))
gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)), path);
else
gtk_icon_view_select_path (GTK_ICON_VIEW (widget), path);
gtk_tree_path_free (path);
return FALSE;
}
static void
file_list_drag_begin_cb (GtkWidget *widget,
GdkDragContext *context,
GtkFileChooserDefault *impl)
{
if (impl->drag_press_row)
{
gtk_tree_row_reference_free (impl->drag_press_row);
impl->drag_press_row = NULL;
impl->drag_several = TRUE;
gtk_drag_set_icon_stock (context, GTK_STOCK_DND_MULTIPLE, -2, -2);
}
}
static void
file_list_drag_end_cb (GtkWidget *widget,
GdkDragContext *context,
GtkFileChooserDefault *impl)
{
impl->drag_several = FALSE;
}
static void
drag_uris_foreach (GtkTreeModel *model,
GtkTreePath *path,
GtkTreeIter *iter,
gpointer data)
{
GPtrArray *uris = data;
GFile *file;
gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
if (file)
{
g_ptr_array_add (uris, g_file_get_uri (file));
g_object_unref (file);
}
}
/* after the view, which gives the one file under the pointer */
static void
file_list_drag_data_get_cb (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint time_,
GtkFileChooserDefault *impl)
{
GPtrArray *uris;
if (!impl->drag_several)
return;
uris = g_ptr_array_new_with_free_func (g_free);
current_selection_selected_foreach (impl, drag_uris_foreach, uris);
g_ptr_array_add (uris, NULL);
gtk_selection_data_set_uris (selection_data, (gchar **) uris->pdata);
g_ptr_array_free (uris, TRUE);
}
static void
file_list_drag_source_connect (GtkFileChooserDefault *impl,
GtkWidget *view)
{
g_signal_connect (view, "button-press-event",
G_CALLBACK (file_list_button_press_drag_cb), impl);
g_signal_connect (view, "button-release-event",
G_CALLBACK (file_list_button_release_drag_cb), impl);
g_signal_connect_after (view, "drag-begin",
G_CALLBACK (file_list_drag_begin_cb), impl);
g_signal_connect (view, "drag-end",
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);
}
/* Sets the sort column IDs for the file list; needs to be done whenever we
* change the model on the treeview.
*/
@ -5946,6 +6125,7 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl)
NULL, 0,
GDK_ACTION_COPY | GDK_ACTION_MOVE);
gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
file_list_drag_source_connect (impl, impl->browse_files_tree_view);
g_signal_connect (selection, "changed",
G_CALLBACK (list_selection_changed), impl);
@ -6206,6 +6386,7 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl)
NULL, 0,
GDK_ACTION_COPY | GDK_ACTION_MOVE);
gtk_drag_source_add_uri_targets (impl->browse_files_icon_view);
file_list_drag_source_connect (impl, impl->browse_files_icon_view);
impl->list_icon_renderer = gtk_cell_renderer_pixbuf_new ();
g_object_set (G_OBJECT (impl->list_icon_renderer),
@ -13396,6 +13577,8 @@ get_selected_tree_iter_from_icon_view (GtkFileChooserDefault *impl,
return TRUE;
}
/* In the order shown, as in the list view; the list is taken first,
* as @func may run a dialog, meanwhile the folder may change */
static void
icon_view_selection_selected_foreach (GtkFileChooserDefault *impl,
GtkTreeSelectionForeachFunc func,

View file

@ -267,6 +267,7 @@ struct _GtkFileChooserDefault
GSList *loading_shortcuts;
GSList *reload_icon_cancellables;
GCancellable *file_list_drag_data_received_cancellable;
GtkTreeRowReference *drag_press_row; /* pressed among several selected files */
GCancellable *update_current_folder_cancellable;
GCancellable *should_respond_get_info_cancellable;
GCancellable *file_exists_get_info_cancellable;
@ -366,6 +367,7 @@ struct _GtkFileChooserDefault
guint show_thumbnails : 1;
guint compact_view : 1; /* the icon view with small icons beside the names */
guint show_preview_panel : 1;
guint drag_several : 1; /* a drag of all selected files */
guint create_folders : 1;
#if 0