From b086e1f7758bd2fc5ef5dce014bdefc8707d12a1 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Sun, 18 Oct 2026 16:46:56 +0200 Subject: [PATCH] filechooser: drag and drop like a file manager Files can be dropped onto folders, the path bar and the sidebar, also from other programs. Same file system moves, otherwise it copies; Ctrl copies, Shift moves, both link. Handles XdndDirectSave (Firefox, Chromium, archive managers) and paths as text. Folders open when you hover over them and the list scrolls at the edges. Also adds Paste Into Folder. --- gtk/gtkfilechooserdefault.c | 1479 +++++++++++++++++++++++++++++++++-- gtk/gtkfilechooserprivate.h | 14 + gtk/gtkfileoperation.c | 16 + gtk/gtkfileoperation.h | 4 + gtk/gtkpathbar.c | 46 ++ gtk/gtkpathbar.h | 4 + po/de.po | 28 + 7 files changed, 1511 insertions(+), 80 deletions(-) diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index a34aff985..bbeeaba92 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -3636,17 +3636,15 @@ paste_gnome_received (GtkClipboard *clipboard, } static void -paste_files (GtkFileChooserDefault *impl) +paste_files_into (GtkFileChooserDefault *impl, + GFile *destination) { PasteData *paste; - if (impl->operation_mode != OPERATION_MODE_BROWSE || impl->current_folder == NULL) - return; - paste = g_slice_new0 (PasteData); paste->impl = impl; g_object_add_weak_pointer (G_OBJECT (impl), (gpointer *) &paste->impl); - paste->destination = g_object_ref (impl->current_folder); + paste->destination = g_object_ref (destination); gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD), @@ -3654,6 +3652,28 @@ paste_files (GtkFileChooserDefault *impl) paste_gnome_received, paste); } +static void +paste_files (GtkFileChooserDefault *impl) +{ + if (impl->operation_mode != OPERATION_MODE_BROWSE || impl->current_folder == NULL) + return; + + paste_files_into (impl, impl->current_folder); +} + +/* "Paste Into Folder": into the one folder that is selected */ +static void +paste_into_folder (GtkFileChooserDefault *impl) +{ + GFile *folder = get_selected_file (impl); + + if (folder == NULL) + return; + if (impl->operation_mode == OPERATION_MODE_BROWSE) + paste_files_into (impl, folder); + g_object_unref (folder); +} + /* Paste in the menu is sensitive when the clipboard has files, which is * asked when the menu is built, without waiting */ static void @@ -4313,6 +4333,92 @@ shortcuts_drag_data_delete_cb (GtkWidget *widget, g_signal_stop_emission_by_name (widget, "drag-data-delete"); } +/* dropping files: further below */ +static gboolean dnd_offers_uris (GdkDragContext *context); +static gint dnd_choose_for (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + GFile *folder); +static void dnd_hover (GtkFileChooserDefault *impl, + GFile *folder); +static void dnd_set (GtkFileChooserDefault *impl, + GdkDragContext *context, + GFile *folder, + gint action, + guint time_); +static void dnd_leave (GtkFileChooserDefault *impl); +static void dnd_set_state (GtkFileChooserDefault *impl, + GFile *folder, + gint action); +static void dnd_data_received (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + GtkSelectionData *selection_data, + guint time_); + +/* The folder of the place at @x, @y, for dropping files into it: a + * place that is not a bookmark anywhere on its row, a bookmark in the + * middle of its row (above and below, new bookmarks are put) */ +static GFile * +shortcuts_folder_at (GtkFileChooserDefault *impl, + gint x, + gint y, + GtkTreePath **path_out) +{ + GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view); + GtkTreeModel *model = gtk_tree_view_get_model (tree_view); + GtkTreePath *path, *child_path; + GtkTreeViewColumn *column; + GtkTreeIter iter, child_iter; + GdkRectangle cell; + gpointer col_data; + ShortcutType shortcut_type; + GFile *folder = NULL; + gint cell_y, row, bookmarks_index; + + *path_out = NULL; + /* the coordinates of a drop include the column header */ + gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, x, y, &x, &y); + if (!gtk_tree_view_get_path_at_pos (tree_view, x, y, &path, &column, NULL, &cell_y)) + return NULL; + + gtk_tree_view_get_background_area (tree_view, path, column, &cell); + if (!gtk_tree_model_get_iter (model, &iter, path)) + { + gtk_tree_path_free (path); + return NULL; + } + + gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter); + child_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter); + row = *gtk_tree_path_get_indices (child_path); + gtk_tree_path_free (child_path); + + bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS); + if (row >= bookmarks_index && row < bookmarks_index + impl->num_bookmarks && + (cell_y < cell.height / 4 || cell_y >= cell.height - cell.height / 4)) + { + gtk_tree_path_free (path); + return NULL; + } + + gtk_tree_model_get (model, &iter, + SHORTCUTS_COL_DATA, &col_data, + SHORTCUTS_COL_TYPE, &shortcut_type, + -1); + if (shortcut_type == SHORTCUT_TYPE_FILE && col_data) + folder = g_object_ref (col_data); + else if (shortcut_type == SHORTCUT_TYPE_VOLUME && col_data && + _gtk_file_system_volume_is_mounted (col_data)) + folder = _gtk_file_system_volume_get_root (col_data); + + if (folder) + *path_out = path; + else + gtk_tree_path_free (path); + return folder; +} + /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the * drop position. */ @@ -4332,6 +4438,7 @@ shortcuts_drag_leave_cb (GtkWidget *widget, gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), NULL, GTK_TREE_VIEW_DROP_BEFORE); + dnd_leave (impl); g_signal_stop_emission_by_name (widget, "drag-leave"); } @@ -4414,6 +4521,25 @@ shortcuts_drag_motion_cb (GtkWidget *widget, GtkTreeViewDropPosition pos; GdkDragAction action; + /* files onto a place go into it; the place opens when the drag stays */ + if (gtk_drag_get_source_widget (context) != widget && dnd_offers_uris (context)) + { + GFile *folder = shortcuts_folder_at (impl, x, y, &path); + + if (folder) + { + gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), path, GTK_TREE_VIEW_DROP_INTO_OR_BEFORE); + dnd_hover (impl, folder); + dnd_set (impl, context, folder, dnd_choose_for (impl, widget, context, folder), time_); + gtk_tree_path_free (path); + g_object_unref (folder); + g_signal_stop_emission_by_name (widget, "drag-motion"); + return TRUE; + } + } + dnd_hover (impl, NULL); + dnd_set (impl, context, NULL, 0, time_); + #if 0 if (gtk_drag_get_source_widget (context) == widget) { @@ -4465,11 +4591,33 @@ shortcuts_drag_drop_cb (GtkWidget *widget, guint time_, GtkFileChooserDefault *impl) { + GdkAtom target; + #if 0 shortcuts_cancel_drag_outside_idle (impl); #endif g_signal_stop_emission_by_name (widget, "drag-drop"); + + /* files into a place, or new bookmarks: where it was dropped */ + { + GtkTreePath *path = NULL; + GFile *folder = NULL; + + if (gtk_drag_get_source_widget (context) != widget && dnd_offers_uris (context)) + folder = shortcuts_folder_at (impl, x, y, &path); + dnd_set_state (impl, folder, folder ? dnd_choose_for (impl, widget, context, folder) : 0); + if (path) + gtk_tree_path_free (path); + if (folder) + g_object_unref (folder); + } + + target = gtk_drag_dest_find_target (widget, context, NULL); + if (target == GDK_NONE) + gtk_drag_finish (context, FALSE, FALSE, time_); + else + gtk_drag_get_data (widget, context, target, time_); return TRUE; } @@ -4494,6 +4642,15 @@ shortcuts_drop_uris (GtkFileChooserDefault *impl, uri = uris[i]; file = g_file_new_for_uri (uri); + /* bookmarks are of folders: a file dropped there would be one + * nothing can be done with (as GTK+ 3 has it) */ + if (g_file_is_native (file) && + g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE, NULL) != G_FILE_TYPE_DIRECTORY) + { + g_object_unref (file); + continue; + } + if (shortcuts_add_bookmark_from_file (impl, file, position)) position++; @@ -4598,6 +4755,15 @@ shortcuts_drag_data_received_cb (GtkWidget *widget, int bookmarks_index; impl = GTK_FILE_CHOOSER_DEFAULT (data); + target = gtk_selection_data_get_target (selection_data); + + /* files into a place */ + if (impl->dnd_folder && gtk_targets_include_uri (&target, 1)) + { + g_signal_stop_emission_by_name (widget, "drag-data-received"); + dnd_data_received (impl, widget, context, selection_data, time_); + return; + } /* Compute position */ @@ -4613,13 +4779,12 @@ shortcuts_drag_data_received_cb (GtkWidget *widget, g_assert (position >= bookmarks_index); position -= bookmarks_index; - target = gtk_selection_data_get_target (selection_data); - if (gtk_targets_include_uri (&target, 1)) shortcuts_drop_uris (impl, selection_data, position); else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW")) shortcuts_reorder (impl, selection_data, position); + gtk_drag_finish (context, TRUE, FALSE, time_); g_signal_stop_emission_by_name (widget, "drag-data-received"); } @@ -5003,10 +5168,10 @@ shortcuts_list_create (GtkFileChooserDefault *impl) GDK_ACTION_MOVE); gtk_drag_dest_set (impl->browse_shortcuts_tree_view, - GTK_DEST_DEFAULT_ALL, + GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT, tree_model_row_targets, G_N_ELEMENTS (tree_model_row_targets), - GDK_ACTION_COPY | GDK_ACTION_MOVE); + GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK | GDK_ACTION_ASK); gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view); selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view)); @@ -5276,6 +5441,7 @@ popup_menu_detach_cb (GtkWidget *attach_widget, impl->browse_files_popup_menu_cut_item = NULL; impl->browse_files_popup_menu_copy_item = NULL; impl->browse_files_popup_menu_paste_item = NULL; + impl->browse_files_popup_menu_paste_into_item = NULL; impl->browse_files_popup_menu_copy_location_item = NULL; impl->browse_files_popup_menu_undo_item = NULL; impl->browse_files_popup_menu_redo_item = NULL; @@ -5805,6 +5971,1048 @@ out: g_object_unref (cancellable); } +/* What a drop on the list always did: the files are selected, a folder + * alone is opened. Takes @uris. */ +static void +dnd_select_uris (GtkFileChooserDefault *impl, + gchar **uris) +{ + struct FileListDragData *data; + + if (uris == NULL || uris[0] == NULL) + { + g_strfreev (uris); + return; + } + + data = g_new0 (struct FileListDragData, 1); + data->impl = g_object_ref (impl); + data->uris = uris; + data->file = g_file_new_for_uri (uris[0]); + + if (impl->file_list_drag_data_received_cancellable) + g_cancellable_cancel (impl->file_list_drag_data_received_cancellable); + + impl->file_list_drag_data_received_cancellable = + _gtk_file_system_get_info (impl->file_system, data->file, + "standard::type", + file_list_drag_data_received_get_info_cb, + data); +} + +/* Drag and drop of files, as in file managers. A drop on a folder (a + * row of the list, a place, a button of the path bar) copies or moves + * the files into it: within the file chooser a move on the same file + * system and a copy to another one, from other programs what they + * suggest; Ctrl copies, Shift moves, both make links (Option, Command + * and both on the Mac), and a source that asks gets a menu. A drop on + * the list itself selects the files, as the file chooser always did; + * with the keys they go into its folder. A drag that stays on a folder + * for a second opens it, and the list scrolls at its edges. Programs + * that give a file only once they know where it goes (XdndDirectSave: + * Firefox, Chromium, archive managers) write it into the folder. + * Copies and moves have progress and can be undone + * (gtkfileoperation.c). */ + +#define DND_HOVER_TIMEOUT 1000 +/* what the files of the list can be dragged for: links are no + * symbolic links on Windows */ +#ifdef G_OS_WIN32 +#define DRAG_SOURCE_ACTIONS (GDK_ACTION_COPY | GDK_ACTION_MOVE) +#else +#define DRAG_SOURCE_ACTIONS (GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK) +#endif +#define DND_SCROLL_EDGE 24 +#define XDS_TARGET "XdndDirectSave0" +#define XDS_TEXT_TARGET "text/plain" +#define XDS_DATA_TARGET "application/octet-stream" + +typedef enum +{ + DROP_NONE, + DROP_SELECT, + DROP_COPY, + DROP_MOVE, + DROP_LINK, + DROP_ASK +} DropAction; + +static void +dnd_highlight_button (GtkFileChooserDefault *impl, + GtkWidget *button) +{ + if (impl->dnd_highlighted == button) + return; + if (impl->dnd_highlighted) + { + gtk_drag_unhighlight (impl->dnd_highlighted); + g_object_remove_weak_pointer (G_OBJECT (impl->dnd_highlighted), + (gpointer *) &impl->dnd_highlighted); + } + impl->dnd_highlighted = button; + if (button) + { + gtk_drag_highlight (button); + g_object_add_weak_pointer (G_OBJECT (button), (gpointer *) &impl->dnd_highlighted); + } +} + +static gboolean dnd_hover_timeout (gpointer data); +static gboolean dnd_scroll_timeout (gpointer data); + +/* A folder to open when the drag stays on it; NULL for none */ +static void +dnd_hover (GtkFileChooserDefault *impl, + GFile *folder) +{ + if (folder && impl->dnd_hover_folder && g_file_equal (folder, impl->dnd_hover_folder)) + return; + + if (impl->dnd_hover_id) + { + g_source_remove (impl->dnd_hover_id); + impl->dnd_hover_id = 0; + } + g_clear_object (&impl->dnd_hover_folder); + + if (folder && !(impl->current_folder && g_file_equal (folder, impl->current_folder) && + impl->operation_mode == OPERATION_MODE_BROWSE)) + { + impl->dnd_hover_folder = g_object_ref (folder); + impl->dnd_hover_id = gdk_threads_add_timeout (DND_HOVER_TIMEOUT, dnd_hover_timeout, impl); + } +} + +static gboolean +dnd_hover_timeout (gpointer data) +{ + GtkFileChooserDefault *impl = data; + GFile *folder = impl->dnd_hover_folder; + + impl->dnd_hover_id = 0; + impl->dnd_hover_folder = NULL; + if (folder) + { + change_folder_and_display_error (impl, folder, FALSE); + g_object_unref (folder); + } + return FALSE; +} + +/* Scrolls the list while the drag is at its upper or lower edge */ +static void +dnd_autoscroll (GtkFileChooserDefault *impl, + GtkWidget *view, + gint y) +{ + gint step = 0; + + if (view) + { + gint height = view->allocation.height; + + if (GTK_IS_TREE_VIEW (view) && gtk_tree_view_get_headers_visible (GTK_TREE_VIEW (view))) + y -= TREE_VIEW_HEADER_HEIGHT (GTK_TREE_VIEW (view)); + if (y < DND_SCROLL_EDGE) + step = -1; + else if (y > height - DND_SCROLL_EDGE) + step = 1; + } + + impl->dnd_scroll_step = step; + impl->dnd_scroll_view = view; + if (step == 0 && impl->dnd_scroll_id) + { + g_source_remove (impl->dnd_scroll_id); + impl->dnd_scroll_id = 0; + } + else if (step != 0 && impl->dnd_scroll_id == 0) + impl->dnd_scroll_id = gdk_threads_add_timeout (50, dnd_scroll_timeout, impl); +} + +static gboolean +dnd_scroll_timeout (gpointer data) +{ + GtkFileChooserDefault *impl = data; + GtkAdjustment *adjustment; + gdouble value; + + if (impl->dnd_scroll_step == 0 || impl->browse_files_scrolled_window == NULL) + { + impl->dnd_scroll_id = 0; + return FALSE; + } + + adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (impl->browse_files_scrolled_window)); + value = gtk_adjustment_get_value (adjustment) + + impl->dnd_scroll_step * MAX (gtk_adjustment_get_step_increment (adjustment), 8.0) / 2; + gtk_adjustment_set_value (adjustment, + CLAMP (value, gtk_adjustment_get_lower (adjustment), + gtk_adjustment_get_upper (adjustment) - + gtk_adjustment_get_page_size (adjustment))); + return TRUE; +} + +/* The end of a drag over the file chooser: what shows it goes */ +static void +dnd_leave (GtkFileChooserDefault *impl) +{ + dnd_highlight_button (impl, NULL); + dnd_hover (impl, NULL); + dnd_autoscroll (impl, NULL, 0); + if (impl->browse_files_tree_view) + gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_files_tree_view), + NULL, GTK_TREE_VIEW_DROP_BEFORE); + if (impl->browse_files_icon_view) + gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (impl->browse_files_icon_view), + NULL, GTK_ICON_VIEW_NO_DROP); +} + +/* The state of a drag, once it is over */ +static void +dnd_reset (GtkFileChooserDefault *impl) +{ + dnd_leave (impl); + g_clear_object (&impl->dnd_folder); + impl->dnd_action = DROP_NONE; + if (impl->dnd_filesystems) + { + g_hash_table_destroy (impl->dnd_filesystems); + impl->dnd_filesystems = NULL; + } +} + +static gboolean +dnd_own_drag (GtkFileChooserDefault *impl, + GdkDragContext *context) +{ + GtkWidget *source = gtk_drag_get_source_widget (context); + + return source != NULL && + (source == impl->browse_files_tree_view || source == impl->browse_files_icon_view); +} + +/* Whether the files of our own drag and @folder are on one file + * system, where a move is a rename; asked once per folder and drag, and + * only for local folders, which answer at once */ +static gboolean +dnd_same_filesystem (GtkFileChooserDefault *impl, + GFile *folder) +{ + GFile *files[2]; + const gchar *ids[2]; + gint i; + + if (impl->drag_uris == NULL || impl->drag_uris[0] == NULL) + return FALSE; + + if (impl->dnd_filesystems == NULL) + impl->dnd_filesystems = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + files[0] = g_file_new_for_uri (impl->drag_uris[0]); + files[1] = g_object_ref (folder); + for (i = 0; i < 2; i++) + { + gchar *uri = g_file_get_uri (files[i]); + gpointer id; + + if (!g_hash_table_lookup_extended (impl->dnd_filesystems, uri, NULL, &id)) + { + GFileInfo *info = NULL; + + if (g_file_is_native (files[i])) + info = g_file_query_info (files[i], G_FILE_ATTRIBUTE_ID_FILESYSTEM, + G_FILE_QUERY_INFO_NONE, NULL, NULL); + id = g_strdup (info ? g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM) : NULL); + if (info) + g_object_unref (info); + g_hash_table_insert (impl->dnd_filesystems, g_strdup (uri), id); + } + ids[i] = id; + g_free (uri); + g_object_unref (files[i]); + } + + return ids[0] != NULL && g_strcmp0 (ids[0], ids[1]) == 0; +} + +/* Whether @folder is one of the files of our own drag (or in one) */ +static gboolean +dnd_folder_is_dragged (GtkFileChooserDefault *impl, + GFile *folder) +{ + gint i; + + for (i = 0; impl->drag_uris && impl->drag_uris[i]; i++) + { + GFile *file = g_file_new_for_uri (impl->drag_uris[i]); + gboolean inside = g_file_equal (file, folder) || g_file_has_prefix (folder, file); + + g_object_unref (file); + if (inside) + return TRUE; + } + return FALSE; +} + +/* What a drop on @folder does: @on_folder is FALSE for the list itself */ +static DropAction +dnd_choose (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + GFile *folder, + gboolean on_folder) +{ + GdkDragAction actions = gdk_drag_context_get_actions (context); + GdkDragAction suggested = gdk_drag_context_get_suggested_action (context); + GdkModifierType mods = 0; + gboolean own = dnd_own_drag (impl, context); +#ifdef GDK_WINDOWING_QUARTZ + const GdkModifierType copy_mask = GDK_MOD1_MASK, move_mask = GDK_MOD2_MASK; +#else + const GdkModifierType copy_mask = GDK_CONTROL_MASK, move_mask = GDK_SHIFT_MASK; +#endif +#ifdef G_OS_WIN32 + const gboolean can_link = FALSE; +#else + const gboolean can_link = TRUE; +#endif + + if (folder == NULL) + return DROP_NONE; + + /* the keys as they are now: other programs do not tell */ + gdk_window_get_pointer (gtk_widget_get_root_window (widget), NULL, NULL, &mods); + + if (own) + { + GFile *parent; + gboolean same_folder; + + if (dnd_folder_is_dragged (impl, folder)) + return DROP_NONE; + parent = impl->drag_uris && impl->drag_uris[0] ? g_file_new_for_uri (impl->drag_uris[0]) : NULL; + if (parent) + { + GFile *p = g_file_get_parent (parent); + + g_object_unref (parent); + parent = p; + } + same_folder = parent && g_file_equal (parent, folder); + if (parent) + g_object_unref (parent); + /* a move into the folder it is in does nothing; a copy is a copy */ + if (same_folder && !(mods & copy_mask)) + return DROP_NONE; + } + + if ((mods & copy_mask) && (mods & move_mask)) + return can_link ? DROP_LINK : DROP_COPY; + if (mods & copy_mask) + return DROP_COPY; + if (mods & move_mask) + return own || (actions & GDK_ACTION_MOVE) ? DROP_MOVE : DROP_COPY; + + if (suggested == GDK_ACTION_ASK) + return DROP_ASK; + + if (own) + return dnd_same_filesystem (impl, folder) ? DROP_MOVE : DROP_COPY; + + if (!on_folder) + return DROP_SELECT; + + if (suggested == GDK_ACTION_MOVE) + return DROP_MOVE; + if (suggested == GDK_ACTION_LINK && can_link) + return DROP_LINK; + return DROP_COPY; +} + +static GdkDragAction +dnd_drag_action (GdkDragContext *context, + DropAction action) +{ + GdkDragAction actions = gdk_drag_context_get_actions (context); + + switch (action) + { + case DROP_MOVE: + return GDK_ACTION_MOVE; + case DROP_LINK: + return GDK_ACTION_LINK; + case DROP_ASK: + return GDK_ACTION_ASK; + case DROP_NONE: + return 0; + default: + /* selecting takes nothing away, nor does copying */ + if (actions & GDK_ACTION_COPY) + return GDK_ACTION_COPY; + return gdk_drag_context_get_suggested_action (context); + } +} + +static gboolean +dnd_offers_text (GdkDragContext *context) +{ + GList *l; + + for (l = gdk_drag_context_list_targets (context); l; l = l->next) + if (gtk_targets_include_text ((GdkAtom *) &l->data, 1)) + return TRUE; + return FALSE; +} + +static gboolean +dnd_offers_uris (GdkDragContext *context) +{ + GList *l; + + for (l = gdk_drag_context_list_targets (context); l; l = l->next) + if (gtk_targets_include_uri ((GdkAtom *) &l->data, 1)) + return TRUE; + return FALSE; +} + +static gboolean +dnd_offers (GtkWidget *widget, + GdkDragContext *context, + const gchar *target) +{ + GdkAtom atom = gdk_atom_intern (target, FALSE); + GList *l; + + for (l = gdk_drag_context_list_targets (context); l; l = l->next) + if (GDK_POINTER_TO_ATOM (l->data) == atom) + return TRUE; + return FALSE; +} + +/* Files of some kind: a list of them, or one the source saves itself */ +static GdkAtom +dnd_files_target (GtkWidget *widget, + GdkDragContext *context) +{ + GList *l; + + for (l = gdk_drag_context_list_targets (context); l; l = l->next) + if (gtk_targets_include_uri ((GdkAtom *) &l->data, 1)) + return GDK_POINTER_TO_ATOM (l->data); + if (dnd_offers (widget, context, XDS_TARGET)) + return gdk_atom_intern_static_string (XDS_TARGET); + return GDK_NONE; +} + +/* Copies, moves or links @files into @folder */ +static void +dnd_start (GtkFileChooserDefault *impl, + DropAction action, + GList *files, + GFile *folder) +{ + GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl)); + GtkFileOperation *operation; + + if (files == NULL || folder == NULL) + return; + + operation = _gtk_file_operation_start (action == DROP_MOVE ? GTK_FILE_OPERATION_MOVE + : action == DROP_LINK ? GTK_FILE_OPERATION_LINK + : GTK_FILE_OPERATION_COPY, + files, folder, impl->file_operations_box, + GTK_IS_WINDOW (toplevel) ? GTK_WINDOW (toplevel) : NULL, + file_operation_done, impl); + if (operation) + impl->file_operations = g_slist_prepend (impl->file_operations, operation); +} + +static GList * +dnd_files_from_uris (gchar **uris) +{ + GList *files = NULL; + gint i; + + for (i = 0; uris && uris[i]; i++) + if (*uris[i] != '\0') + files = g_list_prepend (files, g_file_new_for_uri (uris[i])); + return g_list_reverse (files); +} + +/* The menu for a source that asks */ +typedef struct +{ + GtkFileChooserDefault *impl; + gchar **uris; + GFile *folder; +} DndAsk; + +static void +dnd_ask_free (DndAsk *ask) +{ + g_object_unref (ask->impl); + g_strfreev (ask->uris); + g_object_unref (ask->folder); + g_slice_free (DndAsk, ask); +} + +static void +dnd_ask_activate (GtkMenuItem *item, + DndAsk *ask) +{ + DropAction action = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "gtk-drop-action")); + + if (action == DROP_SELECT) + { + dnd_select_uris (ask->impl, ask->uris); + ask->uris = NULL; + } + else + { + GList *files = dnd_files_from_uris (ask->uris); + + dnd_start (ask->impl, action, files, ask->folder); + g_list_free_full (files, g_object_unref); + } +} + +static void +dnd_ask (GtkFileChooserDefault *impl, + gchar **uris, + GFile *folder, + gboolean can_select, + guint32 time_) +{ + static const struct + { + DropAction action; + const gchar *label; + } items[] = { + { DROP_MOVE, N_("_Move Here") }, + { DROP_COPY, N_("_Copy Here") }, +#ifndef G_OS_WIN32 + { DROP_LINK, N_("_Link Here") }, +#endif + { DROP_SELECT, N_("_Select") } + }; + GtkWidget *menu, *item; + DndAsk *ask; + gint i; + + ask = g_slice_new0 (DndAsk); + ask->impl = g_object_ref (impl); + ask->uris = uris; + ask->folder = g_object_ref (folder); + + menu = gtk_menu_new (); + gtk_menu_set_screen (GTK_MENU (menu), gtk_widget_get_screen (GTK_WIDGET (impl))); + for (i = 0; i < G_N_ELEMENTS (items); i++) + { + if (items[i].action == DROP_SELECT) + { + if (!can_select) + continue; + item = gtk_separator_menu_item_new (); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + } + item = gtk_menu_item_new_with_mnemonic (_(items[i].label)); + g_object_set_data (G_OBJECT (item), I_("gtk-drop-action"), GINT_TO_POINTER (items[i].action)); + g_signal_connect (item, "activate", G_CALLBACK (dnd_ask_activate), ask); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + } + + item = gtk_separator_menu_item_new (); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CANCEL, NULL); + gtk_widget_show (item); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + + /* the menu goes when it is closed; its answer comes before that */ + g_object_set_data_full (G_OBJECT (menu), I_("gtk-drop-ask"), ask, (GDestroyNotify) dnd_ask_free); + g_signal_connect (menu, "deactivate", G_CALLBACK (gtk_widget_destroy), NULL); + g_object_ref_sink (menu); + gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 0, time_); + g_object_unref (menu); +} + +/* Directly saved files (XdndDirectSave): the source names the file, we + * say where it goes, the source writes it there (or hands it over) */ +static gboolean +dnd_xds_start (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + GFile *folder, + guint32 time_) +{ + GdkAtom property = gdk_atom_intern_static_string (XDS_TARGET); + GdkAtom text = gdk_atom_intern_static_string (XDS_TEXT_TARGET); + GdkWindow *source = gdk_drag_context_get_source_window (context); + GdkAtom type; + gint format, length; + guchar *data = NULL; + gchar *name, *basename, *filename, *uri; + + if (source == NULL || folder == NULL || + !gdk_property_get (source, property, text, 0, 4096, FALSE, + &type, &format, &length, &data) || + data == NULL || length <= 0 || format != 8) + { + g_free (data); + return FALSE; + } + + name = g_strndup ((const gchar *) data, length); + g_free (data); + /* a name, nothing that goes elsewhere */ + basename = g_path_get_basename (name); + g_free (name); + if (strcmp (basename, ".") == 0 || strcmp (basename, "..") == 0 || + strcmp (basename, G_DIR_SEPARATOR_S) == 0 || strchr (basename, '/') != NULL) + { + g_free (basename); + return FALSE; + } + + filename = g_utf8_validate (basename, -1, NULL) + ? g_filename_from_utf8 (basename, -1, NULL, NULL, NULL) : NULL; + if (filename == NULL) + filename = g_strdup (basename); + g_free (basename); + + g_clear_object (&impl->dnd_xds_file); + impl->dnd_xds_file = _gtk_file_operation_free_name (folder, filename); + g_free (filename); + if (impl->dnd_xds_file == NULL) + return FALSE; + + uri = g_file_get_uri (impl->dnd_xds_file); + gdk_property_change (source, property, text, 8, GDK_PROP_MODE_REPLACE, + (const guchar *) uri, strlen (uri)); + g_free (uri); + + gtk_drag_get_data (widget, context, property, time_); + return TRUE; +} + +/* The file the source saved: shown and selected */ +static void +dnd_xds_done (GtkFileChooserDefault *impl) +{ + GFile *parent = g_file_get_parent (impl->dnd_xds_file); + + if (parent && impl->current_folder && g_file_equal (parent, impl->current_folder) && + impl->browse_files_model) + { + rename_select_clear (impl); + impl->rename_select = g_object_ref (impl->dnd_xds_file); + if (rename_select_now (impl)) + rename_select_clear (impl); + else + { + impl->rename_select_model = g_object_ref (impl->browse_files_model); + impl->rename_select_handler = + g_signal_connect (impl->rename_select_model, "row-inserted", + G_CALLBACK (rename_row_inserted_cb), impl); + _gtk_file_system_model_add_and_query_file (impl->browse_files_model, + impl->dnd_xds_file, MODEL_ATTRIBUTES); + } + } + if (parent) + g_object_unref (parent); + g_clear_object (&impl->dnd_xds_file); +} + +/* After a drop: the data for it. Used by the list and the path bar. */ +static void +dnd_data_received (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + GtkSelectionData *selection_data, + guint time_) +{ + GdkAtom target = gtk_selection_data_get_target (selection_data); + const guchar *data = gtk_selection_data_get_data (selection_data); + gint length = gtk_selection_data_get_length (selection_data); + GFile *folder = impl->dnd_folder; + DropAction action = impl->dnd_action; + gboolean success = FALSE; + + if (target == gdk_atom_intern_static_string (XDS_TARGET)) + { + if (impl->dnd_xds_file && length == 1 && data[0] == 'S') + { + dnd_xds_done (impl); + success = TRUE; + } + else if (impl->dnd_xds_file && length == 1 && data[0] == 'F' && + dnd_offers (widget, context, XDS_DATA_TARGET)) + { + /* the source can not write there: it hands over the data */ + gtk_drag_get_data (widget, context, gdk_atom_intern_static_string (XDS_DATA_TARGET), time_); + return; + } + else if (impl->dnd_xds_file) + { + error_message (impl, _("The file could not be saved"), + _("The program it came from could not write it there.")); + g_clear_object (&impl->dnd_xds_file); + } + goto out; + } + + if (target == gdk_atom_intern_static_string (XDS_DATA_TARGET)) + { + GError *error = NULL; + + if (impl->dnd_xds_file && length >= 0 && + g_file_replace_contents (impl->dnd_xds_file, (const gchar *) data, length, + NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error)) + { + dnd_xds_done (impl); + success = TRUE; + } + else if (impl->dnd_xds_file) + { + error_message (impl, _("The file could not be saved"), + error ? error->message : ""); + g_clear_error (&error); + g_clear_object (&impl->dnd_xds_file); + } + goto out; + } + + /* a list of files */ + { + gchar **uris = gtk_selection_data_get_uris (selection_data); + gboolean local = FALSE; + gint i; + + if (uris == NULL) + { + /* a path or a URI as text, from a program that has nothing else */ + gchar *text = (gchar *) gtk_selection_data_get_text (selection_data); + + if (text) + { + g_strstrip (text); + if (g_path_is_absolute (text) && strchr (text, '\n') == NULL) + { + GFile *file = g_file_new_for_path (text); + + uris = g_new0 (gchar *, 2); + uris[0] = g_file_get_uri (file); + g_object_unref (file); + } + else if (g_uri_parse_scheme (text) && strchr (text, '\n') == NULL) + { + uris = g_new0 (gchar *, 2); + uris[0] = g_strdup (text); + } + g_free (text); + } + } + + if (uris == NULL || uris[0] == NULL || folder == NULL || action == DROP_NONE) + { + g_strfreev (uris); + goto out; + } + + for (i = 0; uris[i]; i++) + { + GFile *file = g_file_new_for_uri (uris[i]); + + local = local || g_file_is_native (file); + g_object_unref (file); + } + + /* a picture of a web page and the like: the source saves it, if it + * can, as GIO might not get it from the web */ + if (!local && dnd_offers (widget, context, XDS_TARGET) && + dnd_xds_start (impl, widget, context, folder, time_)) + { + g_strfreev (uris); + return; + } + + switch (action) + { + case DROP_SELECT: + dnd_select_uris (impl, uris); + break; + case DROP_ASK: + dnd_ask (impl, uris, folder, widget != impl->browse_path_bar, time_); + break; + default: + { + GList *files = dnd_files_from_uris (uris); + + dnd_start (impl, action, files, folder); + g_list_free_full (files, g_object_unref); + g_strfreev (uris); + } + break; + } + success = TRUE; + } + + out: + /* nothing is deleted by the source: what was moved, we moved */ + gtk_drag_finish (context, success, FALSE, time_); + dnd_reset (impl); +} + +/* After a drop: asks for the files, as a list or saved by the source */ +static gboolean +dnd_drop (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + guint time_) +{ + GdkAtom target; + + dnd_leave (impl); + + if (impl->dnd_folder == NULL || impl->dnd_action == DROP_NONE) + { + gtk_drag_finish (context, FALSE, FALSE, time_); + dnd_reset (impl); + return TRUE; + } + + target = dnd_files_target (widget, context); + if (target == gdk_atom_intern_static_string (XDS_TARGET)) + { + if (!dnd_xds_start (impl, widget, context, impl->dnd_folder, time_)) + { + gtk_drag_finish (context, FALSE, FALSE, time_); + dnd_reset (impl); + } + return TRUE; + } + if (target == GDK_NONE && dnd_offers_text (context)) + target = gdk_atom_intern_static_string ("UTF8_STRING"); + if (target == GDK_NONE || !dnd_offers (widget, context, gdk_atom_name (target))) + target = gtk_drag_dest_find_target (widget, context, NULL); + if (target == GDK_NONE) + { + gtk_drag_finish (context, FALSE, FALSE, time_); + dnd_reset (impl); + return TRUE; + } + + gtk_drag_get_data (widget, context, target, time_); + return TRUE; +} + +/* What a drop on the folder of a place or of the path bar does */ +static gint +dnd_choose_for (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + GFile *folder) +{ + return dnd_choose (impl, widget, context, folder, TRUE); +} + +/* Sets what a drop now does */ +static void +dnd_set_state (GtkFileChooserDefault *impl, + GFile *folder, + gint action) +{ + if (folder != impl->dnd_folder) + { + if (folder) + g_object_ref (folder); + if (impl->dnd_folder) + g_object_unref (impl->dnd_folder); + impl->dnd_folder = folder; + } + impl->dnd_action = folder ? action : DROP_NONE; +} + +/* The same, and tells the source */ +static void +dnd_set (GtkFileChooserDefault *impl, + GdkDragContext *context, + GFile *folder, + gint action, + guint time_) +{ + dnd_set_state (impl, folder, action); + gdk_drag_status (context, dnd_drag_action (context, impl->dnd_action), time_); +} + +/* The folder of the row at @x, @y of the list, if that is one */ +static GFile * +file_list_folder_at (GtkFileChooserDefault *impl, + GtkWidget *view, + gint x, + gint y, + GtkTreePath **path) +{ + GtkTreeModel *model = impl->current_model; + GtkTreeIter iter; + GFile *file = NULL; + gboolean is_folder = FALSE; + + *path = NULL; + if (model == NULL) + return NULL; + + if (GTK_IS_TREE_VIEW (view)) + gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view), x, y, path, NULL); + else + gtk_icon_view_get_dest_item_at_pos (GTK_ICON_VIEW (view), x, y, path, NULL); + + if (*path == NULL || !gtk_tree_model_get_iter (model, &iter, *path)) + { + if (*path) + gtk_tree_path_free (*path); + *path = NULL; + return NULL; + } + + gtk_tree_model_get (model, &iter, + MODEL_COL_FILE, &file, + MODEL_COL_IS_FOLDER, &is_folder, + -1); + if (file && is_folder) + return file; + + if (file) + g_object_unref (file); + gtk_tree_path_free (*path); + *path = NULL; + return NULL; +} + +/* Where a drop on the list at @x, @y goes and what it does: the + * folder under it, or the one of the list; FALSE for nothing. @path is + * the row of the folder. */ +static DropAction +file_list_drop_at (GtkFileChooserDefault *impl, + GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + GFile **folder_out, + GtkTreePath **path) +{ + GFile *folder; + gboolean on_folder; + DropAction action; + + folder = file_list_folder_at (impl, widget, x, y, path); + on_folder = folder != NULL; + if (!on_folder && impl->current_folder && impl->operation_mode == OPERATION_MODE_BROWSE) + folder = g_object_ref (impl->current_folder); + + action = dnd_choose (impl, widget, context, folder, on_folder); + /* in Recently Used and Search, the list itself only selects */ + if (!on_folder && impl->operation_mode != OPERATION_MODE_BROWSE) + action = DROP_SELECT; + if (!on_folder && action == DROP_SELECT && impl->current_folder) + { + if (folder == NULL) + folder = g_object_ref (impl->current_folder); + } + + *folder_out = folder; + return action; +} + +static gboolean +file_list_drag_motion_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + guint time_, + GtkFileChooserDefault *impl) +{ + GtkTreePath *path; + GFile *folder; + gboolean on_folder; + DropAction action; + + g_signal_stop_emission_by_name (widget, "drag-motion"); + + if (dnd_files_target (widget, context) == GDK_NONE && !dnd_offers_text (context)) + { + gdk_drag_status (context, 0, time_); + return FALSE; + } + + dnd_autoscroll (impl, widget, y); + + action = file_list_drop_at (impl, widget, context, x, y, &folder, &path); + on_folder = path != NULL; + + if (on_folder && action != DROP_NONE) + { + if (GTK_IS_TREE_VIEW (widget)) + gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER); + else + gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget), path, GTK_ICON_VIEW_DROP_INTO); + } + else if (GTK_IS_TREE_VIEW (widget)) + gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), NULL, GTK_TREE_VIEW_DROP_BEFORE); + else + gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget), NULL, GTK_ICON_VIEW_NO_DROP); + + /* a drag that stays on a folder opens it */ + dnd_hover (impl, on_folder && dnd_files_target (widget, context) != GDK_NONE ? folder : NULL); + + dnd_set (impl, context, folder, action, time_); + + if (path) + gtk_tree_path_free (path); + if (folder) + g_object_unref (folder); + return TRUE; +} + +static void +file_list_drag_leave_cb (GtkWidget *widget, + GdkDragContext *context, + guint time_, + GtkFileChooserDefault *impl) +{ + g_signal_stop_emission_by_name (widget, "drag-leave"); + /* comes before a drop as well, which still wants the folder */ + dnd_leave (impl); +} + +static gboolean +file_list_drag_drop_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + guint time_, + GtkFileChooserDefault *impl) +{ + GtkTreePath *path; + GFile *folder; + DropAction action; + + g_signal_stop_emission_by_name (widget, "drag-drop"); + + /* where it was dropped: files from Explorer come with no motion */ + action = file_list_drop_at (impl, widget, context, x, y, &folder, &path); + dnd_set_state (impl, folder, action); + if (path) + gtk_tree_path_free (path); + if (folder) + g_object_unref (folder); + + return dnd_drop (impl, widget, context, time_); +} + static void file_list_drag_data_received_cb (GtkWidget *widget, GdkDragContext *context, @@ -5815,72 +7023,90 @@ file_list_drag_data_received_cb (GtkWidget *widget, guint time_, gpointer data) { - GtkFileChooserDefault *impl; - gchar **uris; - char *uri; - GFile *file; - - impl = GTK_FILE_CHOOSER_DEFAULT (data); - - /* Allow only drags from other widgets; see bug #533891. */ - if (gtk_drag_get_source_widget (context) == widget) - { - g_signal_stop_emission_by_name (widget, "drag-data-received"); - return; - } - - /* Parse the text/uri-list string, navigate to the first one */ - uris = gtk_selection_data_get_uris (selection_data); - if (uris && uris[0]) - { - struct FileListDragData *data; - - uri = uris[0]; - file = g_file_new_for_uri (uri); - - data = g_new0 (struct FileListDragData, 1); - data->impl = g_object_ref (impl); - data->uris = uris; - data->file = file; - - if (impl->file_list_drag_data_received_cancellable) - g_cancellable_cancel (impl->file_list_drag_data_received_cancellable); - - impl->file_list_drag_data_received_cancellable = - _gtk_file_system_get_info (impl->file_system, file, - "standard::type", - file_list_drag_data_received_get_info_cb, - data); - } - g_signal_stop_emission_by_name (widget, "drag-data-received"); + dnd_data_received (GTK_FILE_CHOOSER_DEFAULT (data), widget, context, selection_data, time_); } -/* Don't do anything with the drag_drop signal */ +/* The folders of the path bar take drops as well */ static gboolean -file_list_drag_drop_cb (GtkWidget *widget, - GdkDragContext *context, - gint x, - gint y, - guint time_, - GtkFileChooserDefault *impl) +path_bar_drag_motion_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + guint time_, + GtkFileChooserDefault *impl) { - g_signal_stop_emission_by_name (widget, "drag-drop"); + GtkWidget *button; + GFile *folder; + DropAction action; + + if (dnd_files_target (widget, context) == GDK_NONE) + { + gdk_drag_status (context, 0, time_); + return FALSE; + } + + folder = _gtk_path_bar_get_file_at (GTK_PATH_BAR (widget), x, y, &button); + action = dnd_choose (impl, widget, context, folder, TRUE); + dnd_highlight_button (impl, action != DROP_NONE ? button : NULL); + dnd_hover (impl, folder); + dnd_set (impl, context, folder, action, time_); return TRUE; } -/* Disable the normal tree drag motion handler, it makes it look like you're - dropping the dragged item onto a tree item */ -static gboolean -file_list_drag_motion_cb (GtkWidget *widget, - GdkDragContext *context, - gint x, - gint y, - guint time_, - GtkFileChooserDefault *impl) +static void +path_bar_drag_leave_cb (GtkWidget *widget, + GdkDragContext *context, + guint time_, + GtkFileChooserDefault *impl) { - g_signal_stop_emission_by_name (widget, "drag-motion"); - return TRUE; + dnd_leave (impl); +} + +static gboolean +path_bar_drag_drop_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + guint time_, + GtkFileChooserDefault *impl) +{ + GtkWidget *button; + GFile *folder = _gtk_path_bar_get_file_at (GTK_PATH_BAR (widget), x, y, &button); + + dnd_set_state (impl, folder, dnd_choose (impl, widget, context, folder, TRUE)); + return dnd_drop (impl, widget, context, time_); +} + +static void +path_bar_drag_data_received_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + GtkSelectionData *selection_data, + guint info, + guint time_, + GtkFileChooserDefault *impl) +{ + dnd_data_received (impl, widget, context, selection_data, time_); +} + +/* The targets of drops of files: lists of files, files saved by the + * source (and their data), paths as text */ +static void +dnd_dest_set (GtkWidget *widget) +{ + GtkTargetList *list = gtk_target_list_new (NULL, 0); + + gtk_target_list_add_uri_targets (list, 0); + gtk_target_list_add (list, gdk_atom_intern_static_string (XDS_TARGET), 0, 0); + gtk_target_list_add_text_targets (list, 0); + /* only asked for after a source of XdndDirectSave says so */ + gtk_target_list_add (list, gdk_atom_intern_static_string (XDS_DATA_TARGET), 0, 0); + gtk_drag_dest_set (widget, 0, NULL, 0, + GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK | GDK_ACTION_ASK); + gtk_drag_dest_set_target_list (widget, list); + gtk_target_list_unref (list); } /* Constructs the popup menu for the file list if needed */ @@ -5955,6 +7181,12 @@ 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_menu_item_new_with_mnemonic (_("Paste _Into Folder")); + impl->browse_files_popup_menu_paste_into_item = item; + g_signal_connect_swapped (item, "activate", + G_CALLBACK (paste_into_folder), impl); + 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), @@ -6164,6 +7396,31 @@ file_list_update_popup_menu (GtkFileChooserDefault *impl) paste_targets_received, g_object_ref (impl->browse_files_popup_menu_paste_item)); + /* 'Paste Into Folder', on one folder */ + { + gboolean one_folder = FALSE; + + if (impl->operation_mode == OPERATION_MODE_BROWSE && selected == 1) + { + GFile *file = get_selected_file (impl); + GtkTreeIter iter; + + if (file && impl->browse_files_model && + _gtk_file_system_model_get_iter_for_file (impl->browse_files_model, &iter, file)) + gtk_tree_model_get (GTK_TREE_MODEL (impl->browse_files_model), &iter, + MODEL_COL_IS_FOLDER, &one_folder, -1); + if (file) + g_object_unref (file); + } + gtk_widget_set_visible (impl->browse_files_popup_menu_paste_into_item, one_folder); + gtk_widget_set_sensitive (impl->browse_files_popup_menu_paste_into_item, FALSE); + if (one_folder) + gtk_clipboard_request_targets (gtk_widget_get_clipboard (GTK_WIDGET (impl), + GDK_SELECTION_CLIPBOARD), + paste_targets_received, + g_object_ref (impl->browse_files_popup_menu_paste_into_item)); + } + /* 'Undo ...', 'Redo ...' */ { gboolean possible; @@ -6374,8 +7631,29 @@ file_list_button_press_drag_cb (GtkWidget *widget, /* 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) + GTK_MODIFY_SELECTION_MOD_MASK | GTK_EXTEND_SELECTION_MOD_MASK))) + return FALSE; + + /* A press on a file that is not selected drags that file, as in file + * managers; the list view would start a rubber band there, which + * still starts below the files. It is selected before the view sees + * the press. */ + if (GTK_IS_TREE_VIEW (widget) && impl->select_multiple && + event->window == gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget)) && + gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget), event->x, event->y, &path, NULL, NULL, NULL)) + { + GtkTreeSelection *tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)); + + if (!gtk_tree_selection_path_is_selected (tree_selection, path)) + { + gtk_tree_selection_unselect_all (tree_selection); + gtk_tree_selection_select_path (tree_selection, path); + } + gtk_tree_path_free (path); + path = NULL; + } + + if (current_selection_count_selected_rows (impl) < 2) return FALSE; if (GTK_IS_TREE_VIEW (widget)) @@ -6457,11 +7735,24 @@ file_list_button_release_drag_cb (GtkWidget *widget, return FALSE; } +static void drag_uris_foreach (GtkTreeModel *model, GtkTreePath *path, + GtkTreeIter *iter, gpointer data); + static void file_list_drag_begin_cb (GtkWidget *widget, GdkDragContext *context, GtkFileChooserDefault *impl) { + GPtrArray *uris; + + /* what is dragged is kept: the drag can go into another folder, where + * nothing of it is selected */ + uris = g_ptr_array_new (); + current_selection_selected_foreach (impl, drag_uris_foreach, uris); + g_ptr_array_add (uris, NULL); + g_strfreev (impl->drag_uris); + impl->drag_uris = (gchar **) g_ptr_array_free (uris, FALSE); + if (impl->drag_press_row) { gtk_tree_row_reference_free (impl->drag_press_row); @@ -6477,6 +7768,8 @@ file_list_drag_end_cb (GtkWidget *widget, GtkFileChooserDefault *impl) { impl->drag_several = FALSE; + g_strfreev (impl->drag_uris); + impl->drag_uris = NULL; } static void @@ -6507,6 +7800,12 @@ file_list_drag_data_get_cb (GtkWidget *widget, { GPtrArray *uris; + if (impl->drag_uris && impl->drag_uris[0]) + { + gtk_selection_data_set_uris (selection_data, impl->drag_uris); + return; + } + if (!impl->drag_several) return; @@ -7004,11 +8303,9 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl) gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE); - gtk_drag_dest_set (impl->browse_files_tree_view, - GTK_DEST_DEFAULT_ALL, - NULL, 0, - GDK_ACTION_COPY | GDK_ACTION_MOVE); - gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view); + dnd_dest_set (impl->browse_files_tree_view); + g_signal_connect (impl->browse_files_tree_view, "drag-leave", + G_CALLBACK (file_list_drag_leave_cb), impl); g_signal_connect (impl->browse_files_tree_view, "row-activated", G_CALLBACK (list_row_activated), impl); @@ -7039,7 +8336,7 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl) gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view), GDK_BUTTON1_MASK, NULL, 0, - GDK_ACTION_COPY | GDK_ACTION_MOVE); + DRAG_SOURCE_ACTIONS); gtk_drag_source_add_uri_targets (impl->browse_files_tree_view); file_list_drag_source_connect (impl, impl->browse_files_tree_view); @@ -7301,11 +8598,9 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl) g_signal_connect (impl->browse_files_icon_view, "scroll-event", G_CALLBACK (browse_files_icon_view_scroll_event_cb), impl); - gtk_drag_dest_set (impl->browse_files_icon_view, - GTK_DEST_DEFAULT_ALL, - NULL, 0, - GDK_ACTION_COPY | GDK_ACTION_MOVE); - gtk_drag_dest_add_uri_targets (impl->browse_files_icon_view); + dnd_dest_set (impl->browse_files_icon_view); + g_signal_connect (impl->browse_files_icon_view, "drag-leave", + G_CALLBACK (file_list_drag_leave_cb), impl); g_signal_connect (impl->browse_files_icon_view, "drag-data-received", G_CALLBACK (file_list_drag_data_received_cb), impl); g_signal_connect (impl->browse_files_icon_view, "drag-drop", @@ -7315,7 +8610,7 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl) gtk_icon_view_enable_model_drag_source (GTK_ICON_VIEW (impl->browse_files_icon_view), GDK_BUTTON1_MASK, NULL, 0, - GDK_ACTION_COPY | GDK_ACTION_MOVE); + DRAG_SOURCE_ACTIONS); gtk_drag_source_add_uri_targets (impl->browse_files_icon_view); file_list_drag_source_connect (impl, impl->browse_files_icon_view); @@ -8515,6 +9810,11 @@ path_bar_widgets_create (GtkFileChooserDefault *impl) impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL); _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system); g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl); + dnd_dest_set (impl->browse_path_bar); + g_signal_connect (impl->browse_path_bar, "drag-motion", G_CALLBACK (path_bar_drag_motion_cb), impl); + g_signal_connect (impl->browse_path_bar, "drag-leave", G_CALLBACK (path_bar_drag_leave_cb), impl); + g_signal_connect (impl->browse_path_bar, "drag-drop", G_CALLBACK (path_bar_drag_drop_cb), impl); + g_signal_connect (impl->browse_path_bar, "drag-data-received", G_CALLBACK (path_bar_drag_data_received_cb), impl); gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar); gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0); @@ -9434,6 +10734,25 @@ gtk_file_chooser_default_dispose (GObject *object) remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl))); + /* the views are gone already: only the state of a drag */ + if (impl->dnd_hover_id) + g_source_remove (impl->dnd_hover_id); + impl->dnd_hover_id = 0; + if (impl->dnd_scroll_id) + g_source_remove (impl->dnd_scroll_id); + impl->dnd_scroll_id = 0; + if (impl->dnd_highlighted) + g_object_remove_weak_pointer (G_OBJECT (impl->dnd_highlighted), (gpointer *) &impl->dnd_highlighted); + impl->dnd_highlighted = NULL; + g_clear_object (&impl->dnd_hover_folder); + g_clear_object (&impl->dnd_folder); + if (impl->dnd_filesystems) + g_hash_table_destroy (impl->dnd_filesystems); + impl->dnd_filesystems = NULL; + g_clear_object (&impl->dnd_xds_file); + g_strfreev (impl->drag_uris); + impl->drag_uris = NULL; + g_list_free_full (impl->history_back, g_object_unref); impl->history_back = NULL; g_list_free_full (impl->history_forward, g_object_unref); diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index 00512b7b6..f0e29d925 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -189,6 +189,7 @@ struct _GtkFileChooserDefault GtkWidget *browse_files_popup_menu_cut_item; GtkWidget *browse_files_popup_menu_copy_item; GtkWidget *browse_files_popup_menu_paste_item; + GtkWidget *browse_files_popup_menu_paste_into_item; GtkWidget *browse_files_popup_menu_copy_location_item; GtkWidget *browse_files_popup_menu_undo_item; GtkWidget *browse_files_popup_menu_redo_item; @@ -261,6 +262,19 @@ struct _GtkFileChooserDefault ViewMode view_mode; GtkWidget *location_button; + /* dragging files over the file chooser */ + GFile *dnd_folder; /* where a drop now would go */ + gint dnd_action; /* what it would do */ + GFile *dnd_hover_folder; /* opened when a drag stays on it */ + guint dnd_hover_id; + guint dnd_scroll_id; + gint dnd_scroll_step; + GtkWidget *dnd_scroll_view; + GtkWidget *dnd_highlighted; /* a button of the path bar */ + GFile *dnd_xds_file; /* where a file saved by the source goes */ + GHashTable *dnd_filesystems; /* folder URI -> id::filesystem, per drag */ + gchar **drag_uris; /* what our own drag carries */ + GtkWidget *history_back_button; GtkWidget *history_forward_button; diff --git a/gtk/gtkfileoperation.c b/gtk/gtkfileoperation.c index 638a31ed6..55d494209 100644 --- a/gtk/gtkfileoperation.c +++ b/gtk/gtkfileoperation.c @@ -452,6 +452,22 @@ free_name_in (GFile *dir, return NULL; } +GFile * +_gtk_file_operation_free_name (GFile *folder, + const gchar *basename) +{ + GFile *file; + + g_return_val_if_fail (G_IS_FILE (folder), NULL); + + file = g_file_get_child (folder, basename); + if (!g_file_query_exists (file, NULL)) + return file; + g_object_unref (file); + + return free_name_in (folder, basename, FALSE, NULL); +} + /* ---------------------------------------------------------------- */ /* progress, main loop side */ diff --git a/gtk/gtkfileoperation.h b/gtk/gtkfileoperation.h index 3e46d7d54..c41a4acad 100644 --- a/gtk/gtkfileoperation.h +++ b/gtk/gtkfileoperation.h @@ -86,6 +86,10 @@ const gchar * _gtk_file_operation_undo_label (gboolean redo, GFile * _gtk_file_operation_undo_removes (gboolean redo, GFile *folder); +/* @basename in @folder, or "name (2).ext" and so on when it is taken */ +GFile * _gtk_file_operation_free_name (GFile *folder, + const gchar *basename); + /* What the file chooser does itself, for Undo */ void _gtk_file_operation_add_rename (GFile *from, GFile *to); diff --git a/gtk/gtkpathbar.c b/gtk/gtkpathbar.c index 5dcaac595..9b6db49a3 100644 --- a/gtk/gtkpathbar.c +++ b/gtk/gtkpathbar.c @@ -1839,5 +1839,51 @@ _gtk_path_bar_down (GtkPathBar *path_bar) } } + +/** + * _gtk_path_bar_get_file_at: + * @path_bar: a #GtkPathBar + * @x: X coordinate, relative to the path bar + * @y: Y coordinate, relative to the path bar + * @button: (out): the button there + * + * For dropping files on the folders of the path bar. + * + * Returns: the folder of the button at @x, @y, or %NULL + **/ +GFile * +_gtk_path_bar_get_file_at (GtkPathBar *path_bar, + gint x, + gint y, + GtkWidget **button) +{ + GtkWidget *widget = GTK_WIDGET (path_bar); + GList *l; + + *button = NULL; + /* the path bar has no window: the buttons are placed in the one of + * its parent, as the coordinates of a drop are not */ + x += widget->allocation.x; + y += widget->allocation.y; + + for (l = path_bar->button_list; l; l = l->next) + { + ButtonData *button_data = l->data; + GtkWidget *b = button_data->button; + + if (!gtk_widget_get_visible (b) || !gtk_widget_get_child_visible (b)) + continue; + + if (x >= b->allocation.x && x < b->allocation.x + b->allocation.width && + y >= b->allocation.y && y < b->allocation.y + b->allocation.height) + { + *button = b; + return button_data->file; + } + } + + return NULL; +} + #define __GTK_PATH_BAR_C__ #include "gtkaliasdef.c" diff --git a/gtk/gtkpathbar.h b/gtk/gtkpathbar.h index 4058794dc..f4cd96f32 100644 --- a/gtk/gtkpathbar.h +++ b/gtk/gtkpathbar.h @@ -90,6 +90,10 @@ gboolean _gtk_path_bar_set_file (GtkPathBar *path_bar, GError **error); void _gtk_path_bar_up (GtkPathBar *path_bar); void _gtk_path_bar_down (GtkPathBar *path_bar); +GFile *_gtk_path_bar_get_file_at (GtkPathBar *path_bar, + gint x, + gint y, + GtkWidget **button); G_END_DECLS diff --git a/po/de.po b/po/de.po index 29c20e694..d10b6d394 100644 --- a/po/de.po +++ b/po/de.po @@ -6419,3 +6419,31 @@ msgstr "_Wiederholen" #: gtk/gtkfileoperation.c msgid "The shell could not put the file back from the Recycle Bin" msgstr "Die Shell konnte die Datei nicht aus dem Papierkorb zurücklegen" + +#: gtk/gtkfilechooserdefault.c +msgid "_Move Here" +msgstr "Hierher _verschieben" + +#: gtk/gtkfilechooserdefault.c +msgid "_Copy Here" +msgstr "Hierher _kopieren" + +#: gtk/gtkfilechooserdefault.c +msgid "_Link Here" +msgstr "Hierher ver_knüpfen" + +#: gtk/gtkfilechooserdefault.c +msgid "_Select" +msgstr "_Auswählen" + +#: gtk/gtkfilechooserdefault.c +msgid "Paste _Into Folder" +msgstr "In den Ordner _einfügen" + +#: gtk/gtkfilechooserdefault.c +msgid "The file could not be saved" +msgstr "Die Datei konnte nicht gespeichert werden" + +#: gtk/gtkfilechooserdefault.c +msgid "The program it came from could not write it there." +msgstr "Das Programm, aus dem sie kommt, konnte sie dort nicht schreiben."