From e00a21dc6207b4f8406f0e63d728755573c438b1 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Sun, 18 Oct 2026 13:41:38 +0200 Subject: [PATCH] file chooser: Copy in the menu of the file list and Ctrl+C put the selected files on the clipboard as file managers take them (and their locations as text), and the menu counts the selection once (from warning-cleanups) --- gtk/gtkfilechooserdefault.c | 177 +++++++++++++++++++++++++++++++++++- gtk/gtkfilechooserprivate.h | 1 + 2 files changed, 174 insertions(+), 4 deletions(-) diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 9c107149f..a235ffcbe 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -3217,6 +3217,145 @@ rename_selected_file (GtkFileChooserDefault *impl) add_idle_to_edit_icon_view (impl, path); } +/* "Copy" (Ctrl+C) puts the selected files on the clipboard as file + * managers do, so that they can be pasted into a folder there; programs + * that take text get their locations, as from "Copy Location". Pasting + * files is left to the file managers, which show the progress of it. */ +enum +{ + COPIED_FILES_GNOME, /* x-special/gnome-copied-files: Nautilus, Thunar, Caja, PCManFM */ + COPIED_FILES_URIS, /* text/uri-list: Dolphin, Konqueror and most others */ + COPIED_FILES_TEXT +}; + +typedef struct +{ + gchar **uris; + gchar *locations; /* one per line */ +} CopiedFiles; + +static void +copied_files_free (CopiedFiles *files) +{ + g_strfreev (files->uris); + g_free (files->locations); + g_slice_free (CopiedFiles, files); +} + +static void +copied_files_get_cb (GtkClipboard *clipboard, + GtkSelectionData *selection_data, + guint info, + gpointer data) +{ + CopiedFiles *files = data; + + switch (info) + { + case COPIED_FILES_GNOME: + { + gchar *list = g_strjoinv ("\n", files->uris); + gchar *text = g_strconcat ("copy\n", list, NULL); + + gtk_selection_data_set (selection_data, + gtk_selection_data_get_target (selection_data), + 8, (const guchar *) text, strlen (text)); + g_free (text); + g_free (list); + } + break; + + case COPIED_FILES_URIS: + gtk_selection_data_set_uris (selection_data, files->uris); + break; + + default: + gtk_selection_data_set_text (selection_data, files->locations, -1); + break; + } +} + +static void +copied_files_clear_cb (GtkClipboard *clipboard, + gpointer data) +{ + copied_files_free (data); +} + +static void +copy_files_foreach_cb (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data) +{ + GPtrArray *uris = ((gpointer *) data)[0]; + GString *locations = ((gpointer *) data)[1]; + GFile *file; + gchar *location; + + gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1); + if (file == NULL) + return; + + g_ptr_array_add (uris, g_file_get_uri (file)); + + location = g_file_get_path (file); + if (location == NULL) + location = g_file_get_uri (file); + if (locations->len != 0) + g_string_append_c (locations, '\n'); + g_string_append (locations, location); + g_free (location); + + g_object_unref (file); +} + +static void +copy_files (GtkFileChooserDefault *impl) +{ + GPtrArray *uris = g_ptr_array_new (); + GString *locations = g_string_new (NULL); + gpointer data[2]; + CopiedFiles *files; + GtkTargetList *list; + GtkTargetEntry *targets; + gint n_targets; + GtkClipboard *clipboard; + + data[0] = uris; + data[1] = locations; + current_selection_selected_foreach (impl, copy_files_foreach_cb, data); + + if (uris->len == 0) + { + g_ptr_array_free (uris, TRUE); + g_string_free (locations, TRUE); + return; + } + + g_ptr_array_add (uris, NULL); + files = g_slice_new (CopiedFiles); + files->uris = (gchar **) g_ptr_array_free (uris, FALSE); + files->locations = g_string_free (locations, FALSE); + + list = gtk_target_list_new (NULL, 0); + gtk_target_list_add (list, gdk_atom_intern_static_string ("x-special/gnome-copied-files"), + 0, COPIED_FILES_GNOME); + gtk_target_list_add_uri_targets (list, COPIED_FILES_URIS); + gtk_target_list_add_text_targets (list, COPIED_FILES_TEXT); + targets = gtk_target_table_new_from_list (list, &n_targets); + gtk_target_list_unref (list); + + clipboard = gtk_widget_get_clipboard (impl->browse_files_current_view, GDK_SELECTION_CLIPBOARD); + if (gtk_clipboard_set_with_data (clipboard, targets, n_targets, + copied_files_get_cb, copied_files_clear_cb, files)) + gtk_clipboard_set_can_store (clipboard, NULL, 0); + else + copied_files_free (files); + + gtk_target_table_free (targets, n_targets); +} + /* Idle handler for creating a new folder after editing its name cell, or for * canceling the editing. */ @@ -4729,6 +4868,19 @@ browse_files_key_press_event_cb (GtkWidget *widget, return TRUE; } + /* Ctrl+C, on the Mac Command+C */ + if ((event->keyval == GDK_KEY_c || event->keyval == GDK_KEY_C) && +#ifdef GDK_WINDOWING_QUARTZ + (event->state & (gtk_accelerator_get_default_mod_mask () | GDK_MOD2_MASK)) == GDK_MOD2_MASK +#else + (event->state & gtk_accelerator_get_default_mod_mask ()) == GDK_CONTROL_MASK +#endif + ) + { + copy_files (impl); + return TRUE; + } + if ((((event->keyval == GDK_KEY_Delete || event->keyval == GDK_KEY_KP_Delete) && !(event->state & gtk_accelerator_get_default_mod_mask ())) #ifdef GDK_WINDOWING_QUARTZ @@ -4786,6 +4938,7 @@ popup_menu_detach_cb (GtkWidget *attach_widget, impl->browse_files_popup_menu = NULL; impl->browse_files_popup_menu_add_shortcut_item = NULL; + impl->browse_files_popup_menu_copy_item = NULL; impl->browse_files_popup_menu_copy_location_item = NULL; impl->browse_files_popup_menu_rename_item = NULL; impl->browse_files_popup_menu_trash_item = NULL; @@ -5471,6 +5624,13 @@ file_list_build_popup_menu (GtkFileChooserDefault *impl) gtk_widget_show (item); gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item); + item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL); + impl->browse_files_popup_menu_copy_item = item; + g_signal_connect_swapped (item, "activate", + G_CALLBACK (copy_files), impl); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item); + item = gtk_image_menu_item_new_with_mnemonic (_("_Copy Location")); impl->browse_files_popup_menu_copy_location_item = item; gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), @@ -5584,8 +5744,13 @@ file_list_build_popup_menu (GtkFileChooserDefault *impl) static void file_list_update_popup_menu (GtkFileChooserDefault *impl) { + /* counted once: the icon view walks all its items for it */ + gint selected; + file_list_build_popup_menu (impl); + selected = current_selection_count_selected_rows (impl); + /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */ /* The sensitivity of the Add to Bookmarks item is set in @@ -5595,15 +5760,15 @@ file_list_update_popup_menu (GtkFileChooserDefault *impl) gtk_widget_set_visible (impl->browse_files_popup_menu_visit_item, impl->operation_mode != OPERATION_MODE_BROWSE); gtk_widget_set_sensitive (impl->browse_files_popup_menu_visit_item, - current_selection_count_selected_rows (impl) == 1); + selected == 1); gtk_widget_set_sensitive (impl->browse_files_popup_menu_rename_item, impl->operation_mode == OPERATION_MODE_BROWSE && - current_selection_count_selected_rows (impl) == 1); + selected == 1); gtk_widget_set_sensitive (impl->browse_files_popup_menu_trash_item, impl->operation_mode == OPERATION_MODE_BROWSE && - current_selection_count_selected_rows (impl) > 0); + selected > 0); gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_location_item, - current_selection_count_selected_rows (impl) > 0); + selected > 0); /* 'Show Hidden Files' */ g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item, @@ -5621,6 +5786,10 @@ file_list_update_popup_menu (GtkFileChooserDefault *impl) g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_thumbnails_item, G_CALLBACK (show_thumbnails_toggled_cb), impl); + /* 'Copy' */ + gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_item, + gtk_widget_get_sensitive (impl->browse_files_popup_menu_copy_location_item)); + /* 'Show Preview', not beside the preview of the program */ gtk_widget_set_visible (impl->browse_files_popup_menu_preview_item, impl->preview_widget == NULL); diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index 45fb129ac..afc0f5bc8 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -183,6 +183,7 @@ struct _GtkFileChooserDefault GtkWidget *browse_files_icon_view; GtkWidget *browse_files_popup_menu; GtkWidget *browse_files_popup_menu_add_shortcut_item; + GtkWidget *browse_files_popup_menu_copy_item; GtkWidget *browse_files_popup_menu_copy_location_item; GtkWidget *browse_files_popup_menu_rename_item; GtkWidget *browse_files_popup_menu_trash_item;