gtk-demo: Port to GdkClipboard

This commit is contained in:
Benjamin Otte 2017-12-02 14:39:37 +01:00
parent 18bf0eb61a
commit c833b47b21
2 changed files with 51 additions and 44 deletions

View File

@ -1,6 +1,6 @@
/* Clipboard /* Clipboard
* *
* GtkClipboard is used for clipboard handling. This demo shows how to * GdkClipboard is used for clipboard handling. This demo shows how to
* copy and paste text to and from the clipboard. * copy and paste text to and from the clipboard.
* *
* It also shows how to transfer images via the clipboard or via * It also shows how to transfer images via the clipboard or via
@ -93,20 +93,24 @@ paste_button_clicked (GtkWidget *button,
gdk_clipboard_read_text_async (clipboard, NULL, paste_received, entry); gdk_clipboard_read_text_async (clipboard, NULL, paste_received, entry);
} }
static cairo_surface_t * static GdkTexture *
get_image_surface (GtkImage *image) get_image_texture (GtkImage *image)
{ {
const gchar *icon_name; const gchar *icon_name;
GtkIconTheme *icon_theme; GtkIconTheme *icon_theme;
GtkIconInfo *icon_info;
switch (gtk_image_get_storage_type (image)) switch (gtk_image_get_storage_type (image))
{ {
case GTK_IMAGE_SURFACE: case GTK_IMAGE_TEXTURE:
return cairo_surface_reference (gtk_image_get_surface (image)); return g_object_ref (gtk_image_get_texture (image));
case GTK_IMAGE_ICON_NAME: case GTK_IMAGE_ICON_NAME:
icon_name = gtk_image_get_icon_name (image); icon_name = gtk_image_get_icon_name (image);
icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (image))); icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (image)));
return gtk_icon_theme_load_surface (icon_theme, icon_name, 48, 1, NULL, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL); icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, 48, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
if (icon_info == NULL)
return NULL;
return gtk_icon_info_load_texture (icon_info);
default: default:
g_warning ("Image storage type %d not handled", g_warning ("Image storage type %d not handled",
gtk_image_get_storage_type (image)); gtk_image_get_storage_type (image));
@ -119,14 +123,11 @@ drag_begin (GtkWidget *widget,
GdkDragContext *context, GdkDragContext *context,
gpointer data) gpointer data)
{ {
cairo_surface_t *surface; GdkTexture *texture;
surface = get_image_surface (GTK_IMAGE (widget)); texture = get_image_texture (GTK_IMAGE (widget));
if (surface) if (texture)
{ gtk_drag_set_icon_texture (context, texture, -2, -2);
cairo_surface_set_device_offset (surface, -2, -2);
gtk_drag_set_icon_surface (context, surface);
}
} }
void void
@ -137,11 +138,11 @@ drag_data_get (GtkWidget *widget,
guint time, guint time,
gpointer data) gpointer data)
{ {
cairo_surface_t *surface; GdkTexture *texture;
surface = get_image_surface (GTK_IMAGE (data)); texture = get_image_texture (GTK_IMAGE (widget));
if (surface) if (texture)
gtk_selection_data_set_surface (selection_data, surface); gtk_selection_data_set_texture (selection_data, texture);
} }
static void static void
@ -155,11 +156,11 @@ drag_data_received (GtkWidget *widget,
{ {
if (gtk_selection_data_get_length (selection_data) > 0) if (gtk_selection_data_get_length (selection_data) > 0)
{ {
cairo_surface_t *surface; GdkTexture *texture;
surface = gtk_selection_data_get_surface (selection_data); texture = gtk_selection_data_get_texture (selection_data);
gtk_image_set_from_surface (GTK_IMAGE (data), surface); gtk_image_set_from_texture (GTK_IMAGE (data), texture);
cairo_surface_destroy (surface); g_object_unref (texture);
} }
} }
@ -167,34 +168,45 @@ static void
copy_image (GtkMenuItem *item, copy_image (GtkMenuItem *item,
gpointer data) gpointer data)
{ {
GtkClipboard *clipboard; GdkClipboard *clipboard;
cairo_surface_t *surface; GdkTexture *texture;
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD); clipboard = gtk_widget_get_clipboard (GTK_WIDGET (data));
surface = get_image_surface (GTK_IMAGE (data)); texture = get_image_texture (GTK_IMAGE (data));
if (surface) if (texture)
{ {
gtk_clipboard_set_surface (clipboard, surface); gdk_clipboard_set_texture (clipboard, texture);
cairo_surface_destroy (surface); g_object_unref (texture);
} }
} }
static void
paste_image_received (GObject *source,
GAsyncResult *result,
gpointer data)
{
GdkTexture *texture;
texture = gdk_clipboard_read_texture_finish (GDK_CLIPBOARD (source), result, NULL);
if (texture == NULL)
return;
gtk_image_set_from_texture (GTK_IMAGE (data), texture);
g_object_unref (texture);
}
static void static void
paste_image (GtkMenuItem *item, paste_image (GtkMenuItem *item,
gpointer data) gpointer data)
{ {
GtkClipboard *clipboard; GdkClipboard *clipboard;
cairo_surface_t *surface;
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD); clipboard = gtk_widget_get_clipboard (GTK_WIDGET (data));
surface = gtk_clipboard_wait_for_surface (clipboard); gdk_clipboard_read_texture_async (clipboard,
NULL,
if (surface) paste_image_received,
{ data);
gtk_image_set_from_surface (GTK_IMAGE (data), surface);
cairo_surface_destroy (surface);
}
} }
static gboolean static gboolean
@ -236,7 +248,6 @@ do_clipboard (GtkWidget *do_widget)
GtkWidget *label; GtkWidget *label;
GtkWidget *entry, *button; GtkWidget *entry, *button;
GtkWidget *image; GtkWidget *image;
GtkClipboard *clipboard;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_display (GTK_WINDOW (window), gtk_window_set_display (GTK_WINDOW (window),
@ -338,10 +349,6 @@ do_clipboard (GtkWidget *do_widget)
/* context menu on image */ /* context menu on image */
g_signal_connect (image, "button-press-event", g_signal_connect (image, "button-press-event",
G_CALLBACK (button_press), image); G_CALLBACK (button_press), image);
/* tell the clipboard manager to make the data persistent */
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
gtk_clipboard_set_can_store (clipboard, NULL);
} }
if (!gtk_widget_get_visible (window)) if (!gtk_widget_get_visible (window))

View File

@ -256,7 +256,7 @@ static gchar *types[] =
"GtkTreeIter ", "GtkTreeIter ",
"GtkTreeViewColumn ", "GtkTreeViewColumn ",
"GdkDisplayManager ", "GdkDisplayManager ",
"GtkClipboard ", "GdkClipboard ",
"GtkIconSize ", "GtkIconSize ",
"GtkImage ", "GtkImage ",
"GdkDragContext ", "GdkDragContext ",