editablelabel: Allow to drag text

If we allow editing, we should allow direct drags too.
This commit is contained in:
Matthias Clasen 2020-06-19 15:24:24 -04:00
parent 290611f9ce
commit 8511f25b97

View File

@ -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));
}