filechooser: Convert to GtkDropTarget

This commit is contained in:
Matthias Clasen 2020-01-02 08:46:30 -05:00
parent c7b70b122a
commit b9034015d7
2 changed files with 30 additions and 37 deletions

View File

@ -1982,25 +1982,26 @@ out:
} }
static void static void
file_list_drag_data_received_cb (GtkWidget *widget, file_list_drag_data_received_cb (GObject *source,
GdkDrop *drop, GAsyncResult *result,
GtkSelectionData *selection_data, gpointer user_data)
gpointer user_data)
{ {
GtkFileChooserWidget *impl = GTK_FILE_CHOOSER_WIDGET (user_data); GtkFileChooserWidget *impl = GTK_FILE_CHOOSER_WIDGET (user_data);
GtkFileChooserWidgetPrivate *priv = gtk_file_chooser_widget_get_instance_private (impl); GtkFileChooserWidgetPrivate *priv = gtk_file_chooser_widget_get_instance_private (impl);
GtkDropTarget *dest = GTK_DROP_TARGET (source);
GtkWidget *widget = gtk_drop_target_get_target (dest);
GdkDrop *drop = gtk_drop_target_get_drop (dest);
GdkDrag *drag = gdk_drop_get_drag (drop);
gchar **uris; gchar **uris;
char *uri; char *uri;
GFile *file; GFile *file;
GtkSelectionData *selection_data;
selection_data = gtk_drop_target_read_selection_finish (dest, result, NULL);
/* Allow only drags from other widgets; see bug #533891. */ /* Allow only drags from other widgets; see bug #533891. */
if (gdk_drop_get_drag (drop) && if (drag && gtk_drag_source_get_origin (gtk_drag_get_source (drag)) == widget)
gtk_drag_source_get_origin (gtk_drag_get_source (gdk_drop_get_drag (drop))) == widget) return;
{
g_signal_stop_emission_by_name (widget, "drag-data-received");
return;
}
/* Parse the text/uri-list string, navigate to the first one */ /* Parse the text/uri-list string, navigate to the first one */
uris = gtk_selection_data_get_uris (selection_data); uris = gtk_selection_data_get_uris (selection_data);
@ -2025,19 +2026,19 @@ file_list_drag_data_received_cb (GtkWidget *widget,
file_list_drag_data_received_get_info_cb, file_list_drag_data_received_get_info_cb,
data); data);
} }
g_signal_stop_emission_by_name (widget, "drag-data-received");
} }
/* Don't do anything with the drag_drop signal */ /* Don't do anything with the drag_drop signal */
static gboolean static gboolean
file_list_drag_drop_cb (GtkWidget *widget, file_list_drag_drop_cb (GtkDropTarget *dest,
GdkDrop *drop, int x,
gint x, int y,
gint y,
GtkFileChooserWidget *impl) GtkFileChooserWidget *impl)
{ {
g_signal_stop_emission_by_name (widget, "drag-drop"); const char *target = g_intern_static_string ("text/uri-list");
gtk_drop_target_read_selection (dest, target, NULL, file_list_drag_data_received_cb, impl);
return TRUE; return TRUE;
} }
@ -2054,13 +2055,12 @@ file_list_drag_begin_cb (GtkDragSource *source,
/* Disable the normal tree drag motion handler, it makes it look like you're /* Disable the normal tree drag motion handler, it makes it look like you're
dropping the dragged item onto a tree item */ dropping the dragged item onto a tree item */
static gboolean static gboolean
file_list_drag_motion_cb (GtkWidget *widget, file_list_drag_motion_cb (GtkDropTarget *dest,
GdkDrop *drop, int x,
gint x, int y,
gint y,
GtkFileChooserWidget *impl) GtkFileChooserWidget *impl)
{ {
g_signal_stop_emission_by_name (widget, "drag-motion"); g_signal_stop_emission_by_name (dest, "drag-motion");
return TRUE; return TRUE;
} }
@ -8463,14 +8463,9 @@ gtk_file_chooser_widget_class_init (GtkFileChooserWidgetClass *class)
gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserWidget, box); gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserWidget, box);
/* And a *lot* of callbacks to bind ... */ /* And a *lot* of callbacks to bind ... */
gtk_widget_class_bind_template_callback (widget_class, file_list_drag_drop_cb);
gtk_widget_class_bind_template_callback (widget_class, file_list_drag_data_received_cb);
gtk_widget_class_bind_template_callback (widget_class, list_popup_menu_cb); gtk_widget_class_bind_template_callback (widget_class, list_popup_menu_cb);
gtk_widget_class_bind_template_callback (widget_class, file_list_query_tooltip_cb); gtk_widget_class_bind_template_callback (widget_class, file_list_query_tooltip_cb);
gtk_widget_class_bind_template_callback (widget_class, list_row_activated); gtk_widget_class_bind_template_callback (widget_class, list_row_activated);
gtk_widget_class_bind_template_callback (widget_class, file_list_drag_begin_cb);
gtk_widget_class_bind_template_callback (widget_class, file_list_drag_motion_cb);
gtk_widget_class_bind_template_callback (widget_class, file_list_drag_end_cb);
gtk_widget_class_bind_template_callback (widget_class, list_selection_changed); gtk_widget_class_bind_template_callback (widget_class, list_selection_changed);
gtk_widget_class_bind_template_callback (widget_class, list_cursor_changed); gtk_widget_class_bind_template_callback (widget_class, list_cursor_changed);
gtk_widget_class_bind_template_callback (widget_class, browse_files_tree_view_keynav_failed_cb); gtk_widget_class_bind_template_callback (widget_class, browse_files_tree_view_keynav_failed_cb);
@ -8506,6 +8501,7 @@ post_process_ui (GtkFileChooserWidget *impl)
GList *cells; GList *cells;
GFile *file; GFile *file;
GtkDragSource *source; GtkDragSource *source;
GtkDropTarget *dest;
GdkContentFormats *formats; GdkContentFormats *formats;
/* Setup file list treeview */ /* Setup file list treeview */
@ -8519,15 +8515,15 @@ post_process_ui (GtkFileChooserWidget *impl)
GDK_BUTTON1_MASK, GDK_BUTTON1_MASK,
formats, formats,
GDK_ACTION_COPY | GDK_ACTION_MOVE); GDK_ACTION_COPY | GDK_ACTION_MOVE);
gdk_content_formats_unref (formats);
g_signal_connect (source, "drag-begin", G_CALLBACK (file_list_drag_begin_cb), impl); g_signal_connect (source, "drag-begin", G_CALLBACK (file_list_drag_begin_cb), impl);
g_signal_connect (source, "drag-end", G_CALLBACK (file_list_drag_end_cb), impl); g_signal_connect (source, "drag-end", G_CALLBACK (file_list_drag_end_cb), impl);
gtk_drag_dest_set (priv->browse_files_tree_view,
GTK_DEST_DEFAULT_ALL, dest = gtk_drop_target_new (GTK_DEST_DEFAULT_ALL, formats, GDK_ACTION_COPY | GDK_ACTION_MOVE);
NULL, g_signal_connect (dest, "drag-motion", G_CALLBACK (file_list_drag_motion_cb), impl);
GDK_ACTION_COPY | GDK_ACTION_MOVE); g_signal_connect (dest, "drag-drop", G_CALLBACK (file_list_drag_drop_cb), impl);
gtk_drag_dest_add_uri_targets (priv->browse_files_tree_view); gtk_drop_target_attach (dest, priv->browse_files_tree_view);
gdk_content_formats_unref (formats);
/* File browser treemodel columns are shared between GtkFileChooser implementations, /* File browser treemodel columns are shared between GtkFileChooser implementations,
* so we don't set cell renderer attributes in GtkBuilder, but rather keep that * so we don't set cell renderer attributes in GtkBuilder, but rather keep that

View File

@ -156,9 +156,6 @@
<signal name="key-pressed" handler="treeview_key_press_cb" swapped="no"/> <signal name="key-pressed" handler="treeview_key_press_cb" swapped="no"/>
</object> </object>
</child> </child>
<signal name="drag-data-received" handler="file_list_drag_data_received_cb" swapped="no"/>
<signal name="drag-drop" handler="file_list_drag_drop_cb" swapped="no"/>
<signal name="drag-motion" handler="file_list_drag_motion_cb" swapped="no"/>
<signal name="popup-menu" handler="list_popup_menu_cb" swapped="no"/> <signal name="popup-menu" handler="list_popup_menu_cb" swapped="no"/>
<signal name="query-tooltip" handler="file_list_query_tooltip_cb" swapped="no"/> <signal name="query-tooltip" handler="file_list_query_tooltip_cb" swapped="no"/>
<signal name="row-activated" handler="list_row_activated" swapped="no"/> <signal name="row-activated" handler="list_row_activated" swapped="no"/>