testdnd3: Use GValue dnd

This commit is contained in:
Benjamin Otte 2020-02-16 14:29:22 +01:00
parent 8d2ed36a1b
commit dbad440468

View File

@ -5,8 +5,6 @@ prepare (GtkDragSource *source, double x, double y)
{
GtkWidget *canvas;
GtkWidget *item;
GdkContentProvider *provider;
GBytes *bytes;
canvas = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (source));
item = gtk_widget_pick (canvas, x, y, GTK_PICK_DEFAULT);
@ -14,13 +12,9 @@ prepare (GtkDragSource *source, double x, double y)
if (!GTK_IS_LABEL (item))
return NULL;
bytes = g_bytes_new (&item, sizeof (gpointer));
provider = gdk_content_provider_new_for_bytes ("CANVAS_ITEM", bytes);
g_bytes_unref (bytes);
g_object_set_data (G_OBJECT (canvas), "dragged-item", item);
return provider;
return gdk_content_provider_new_typed (GTK_TYPE_WIDGET, item);
}
static void
@ -48,6 +42,14 @@ drag_end (GtkDragSource *source, GdkDrag *drag)
gtk_widget_set_opacity (item, 1.0);
}
static void
drag_cancel (GtkDragSource *source,
GdkDrag *drag,
GdkDragCancelReason reason)
{
drag_end (source, drag);
}
typedef struct {
GtkWidget *canvas;
double x;
@ -81,18 +83,20 @@ got_data (GObject *source,
{
GdkDrop *drop = GDK_DROP (source);
DropData *data = user_data;
GInputStream *stream;
GBytes *bytes;
GtkWidget *item;
const char *mime_type;
GError *error = NULL;
const GValue *value;
TransformData *transform_data;
GtkWidget *canvas;
GtkWidget *last_child;
stream = gdk_drop_read_finish (drop, result, &mime_type, &error);
bytes = g_input_stream_read_bytes (stream, sizeof (gpointer), NULL, NULL);
item = (gpointer) *(gpointer *)g_bytes_get_data (bytes, NULL);
value = gdk_drop_read_value_finish (drop, result, NULL);
if (value == NULL)
{
gdk_drop_finish (drop, 0);
return;
}
item = g_value_get_object (value);
transform_data = g_object_get_data (G_OBJECT (item), "transform-data");
@ -108,8 +112,6 @@ got_data (GObject *source,
gdk_drop_finish (drop, GDK_ACTION_MOVE);
g_bytes_unref (bytes);
g_object_unref (stream);
g_free (data);
}
@ -126,7 +128,7 @@ drag_drop (GtkDropTarget *dest,
data->x = x;
data->y = y;
gdk_drop_read_async (drop, (const char *[]){"CANVAS_ITEM", NULL}, G_PRIORITY_DEFAULT, NULL, got_data, data);
gdk_drop_read_value_async (drop, GTK_TYPE_WIDGET, G_PRIORITY_DEFAULT, NULL, got_data, data);
return TRUE;
}
@ -149,9 +151,10 @@ canvas_new (void)
g_signal_connect (source, "prepare", G_CALLBACK (prepare), NULL);
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), NULL);
g_signal_connect (source, "drag-end", G_CALLBACK (drag_end), NULL);
g_signal_connect (source, "drag-cancel", G_CALLBACK (drag_cancel), NULL);
gtk_widget_add_controller (canvas, GTK_EVENT_CONTROLLER (source));
formats = gdk_content_formats_new ((const char *[]){"CANVAS_ITEM", NULL}, 1);
formats = gdk_content_formats_new_for_gtype (GTK_TYPE_WIDGET);
dest = gtk_drop_target_new (formats, GDK_ACTION_MOVE);
g_signal_connect (dest, "drag-drop", G_CALLBACK (drag_drop), NULL);
gtk_widget_add_controller (canvas, GTK_EVENT_CONTROLLER (dest));