From 8511f25b97e0843870234ef4d6d834a49c8a9a34 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 19 Jun 2020 15:24:24 -0400 Subject: [PATCH] editablelabel: Allow to drag text If we allow editing, we should allow direct drags too. --- gtk/gtkeditablelabel.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/gtk/gtkeditablelabel.c b/gtk/gtkeditablelabel.c index ad36867a22..7914f9bca7 100644 --- a/gtk/gtkeditablelabel.c +++ b/gtk/gtkeditablelabel.c @@ -17,6 +17,7 @@ #include "config.h" +#include "gtkdragsource.h" #include "gtkdroptarget.h" #include "gtkeditablelabel.h" #include "gtkeditable.h" @@ -163,11 +164,25 @@ gtk_editable_label_drag_drop (GtkDropTarget *dest, return TRUE; } +static GdkContentProvider * +gtk_editable_label_prepare_drag (GtkDragSource *source, + double x, + double y, + GtkEditableLabel *self) +{ + if (!gtk_editable_get_editable (GTK_EDITABLE (self))) + return NULL; + + return gdk_content_provider_new_typed (G_TYPE_STRING, + gtk_label_get_label (GTK_LABEL (self->label))); +} + static void gtk_editable_label_init (GtkEditableLabel *self) { GtkGesture *gesture; GtkDropTarget *target; + GtkDragSource *source; gtk_widget_set_focusable (GTK_WIDGET (self), TRUE); @@ -182,7 +197,7 @@ gtk_editable_label_init (GtkEditableLabel *self) gtk_widget_set_parent (self->stack, GTK_WIDGET (self)); gesture = gtk_gesture_click_new (); - g_signal_connect_swapped (gesture, "pressed", G_CALLBACK (clicked_cb), self); + g_signal_connect_swapped (gesture, "released", G_CALLBACK (clicked_cb), self); gtk_widget_add_controller (self->label, GTK_EVENT_CONTROLLER (gesture)); g_signal_connect_swapped (self->entry, "activate", G_CALLBACK (activate_cb), self); @@ -193,6 +208,10 @@ gtk_editable_label_init (GtkEditableLabel *self) g_signal_connect (target, "drop", G_CALLBACK (gtk_editable_label_drag_drop), self); gtk_widget_add_controller (self->label, GTK_EVENT_CONTROLLER (target)); + source = gtk_drag_source_new (); + g_signal_connect (source, "prepare", G_CALLBACK (gtk_editable_label_prepare_drag), self); + gtk_widget_add_controller (self->label, GTK_EVENT_CONTROLLER (source)); + gtk_editable_init_delegate (GTK_EDITABLE (self)); }