gtk-demo: Make clipboard demo paste from clipboard

The old code was just pasting local clipboard data that we put there
ourselves and was causing criticals on remote clipboard data. Now the
code does the proper async paste.
This commit is contained in:
Benjamin Otte 2021-08-19 03:14:59 +02:00
parent 6f165efcdb
commit 6b733d2943

View File

@ -146,24 +146,46 @@ copy_image (GtkWidget *widget,
g_object_unref (paintable); g_object_unref (paintable);
} }
static void
paste_image_cb (GObject *source,
GAsyncResult *result,
gpointer data)
{
GdkClipboard *clipboard = GDK_CLIPBOARD (source);
DemoImage *demo = DEMO_IMAGE (data);
const GValue *value;
value = gdk_clipboard_read_value_finish (clipboard, result, NULL);
if (value == NULL)
{
gtk_widget_error_bell (GTK_WIDGET (demo));
g_object_unref (demo);
return;
}
gtk_image_set_from_paintable (GTK_IMAGE (demo->image), g_value_get_object (value));
g_object_unref (demo);
}
static void static void
paste_image (GtkWidget *widget, paste_image (GtkWidget *widget,
const char *action_name, const char *action_name,
GVariant *parameter) GVariant *parameter)
{ {
GdkClipboard *clipboard = gtk_widget_get_clipboard (widget); GdkClipboard *clipboard = gtk_widget_get_clipboard (widget);
DemoImage *demo = DEMO_IMAGE (widget); GType type;
GdkContentProvider *content = gdk_clipboard_get_content (clipboard);
GValue value = G_VALUE_INIT;
GdkPaintable *paintable;
g_value_init (&value, GDK_TYPE_PAINTABLE); if (gdk_content_formats_contain_gtype (gdk_clipboard_get_formats (clipboard), GDK_TYPE_TEXTURE))
if (!gdk_content_provider_get_value (content, &value, NULL)) type = GDK_TYPE_TEXTURE;
return; else
type = GDK_TYPE_PAINTABLE;
paintable = GDK_PAINTABLE (g_value_get_object (&value)); gdk_clipboard_read_value_async (clipboard,
gtk_image_set_from_paintable (GTK_IMAGE (demo->image), paintable); type,
g_value_unset (&value); G_PRIORITY_DEFAULT,
NULL,
paste_image_cb,
g_object_ref (widget));
} }
static void static void