tests: Port from surface to texture

This commit is contained in:
Benjamin Otte 2018-03-15 02:59:03 +01:00
parent a8608618a9
commit 01f996447e
2 changed files with 48 additions and 36 deletions

View File

@ -1,25 +1,29 @@
#include <gtk/gtk.h>
static cairo_surface_t *
get_image_surface (GtkImage *image,
static GdkTexture *
get_image_texture (GtkImage *image,
int *out_size)
{
GtkIconTheme *icon_theme;
const char *icon_name;
int width = 48;
cairo_surface_t *surface;
GdkTexture *texture;
GtkIconInfo *icon_info;
switch (gtk_image_get_storage_type (image))
{
case GTK_IMAGE_SURFACE:
surface = gtk_image_get_surface (image);
*out_size = cairo_image_surface_get_width (surface);
return cairo_surface_reference (surface);
case GTK_IMAGE_PAINTABLE:
paintable = gtk_image_get_paintable (image);
*out_size = gdk_paintable_get_intrinsic_width (paintable);
return g_object_ref (paintable);
case GTK_IMAGE_ICON_NAME:
icon_name = gtk_image_get_icon_name (image);
icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (image)));
*out_size = width;
return gtk_icon_theme_load_surface (icon_theme, icon_name, width, 1, NULL, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, width, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
texture = gtk_icon_info_load_texture (icon_info);
g_object_unref (icon_info);
return texture;
default:
g_warning ("Image storage type %d not handled",
gtk_image_get_storage_type (image));
@ -38,12 +42,12 @@ image_drag_begin (GtkWidget *widget,
GdkDragContext *context,
gpointer data)
{
cairo_surface_t *surface;
GdkTexture *texture;
gint hotspot;
gint hot_x, hot_y;
gint size;
surface = get_image_surface (GTK_IMAGE (data), &size);
texture = get_image_texture (GTK_IMAGE (data), &size);
hotspot = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (data), "hotspot"));
switch (hotspot)
{
@ -61,9 +65,8 @@ image_drag_begin (GtkWidget *widget,
hot_y = size;
break;
}
cairo_surface_set_device_offset (surface, hot_x, hot_y);
gtk_drag_set_icon_surface (context, surface);
cairo_surface_destroy (surface);
gtk_drag_set_icon_paintable (context, GDK_PAINTABLE (texture), hot_x, hot_y);
g_object_unref (texture);
}
static void
@ -90,7 +93,7 @@ window_drag_begin (GtkWidget *widget,
GdkDragContext *context,
gpointer data)
{
cairo_surface_t *surface;
GdkTexture *texture;
GtkWidget *image;
int hotspot;
int size;
@ -101,9 +104,9 @@ window_drag_begin (GtkWidget *widget,
if (image == NULL)
{
g_print ("creating new drag widget\n");
surface = get_image_surface (GTK_IMAGE (data), &size);
image = gtk_image_new_from_surface (surface);
cairo_surface_destroy (surface);
texture = get_image_texture (GTK_IMAGE (data), &size);
image = gtk_image_new_from_texture (texture);
g_object_unref (texture);
g_object_ref (image);
g_object_set_data (G_OBJECT (widget), "drag widget", image);
g_signal_connect (image, "destroy", G_CALLBACK (drag_widget_destroyed), widget);
@ -155,14 +158,15 @@ image_drag_data_get (GtkWidget *widget,
guint time,
gpointer data)
{
cairo_surface_t *surface;
GdkTexture *texture;
const gchar *name;
int size;
if (gtk_selection_data_targets_include_image (selection_data, TRUE))
{
surface = get_image_surface (GTK_IMAGE (data), &size);
gtk_selection_data_set_surface (selection_data, surface);
texture = get_image_texture (GTK_IMAGE (data), &size);
gtk_selection_data_set_texture (selection_data, texture);
g_object_unref (texture);
}
else if (gtk_selection_data_targets_include_text (selection_data))
{
@ -185,7 +189,6 @@ image_drag_data_received (GtkWidget *widget,
guint32 time,
gpointer data)
{
cairo_surface_t *surface;
gchar *text;
if (gtk_selection_data_get_length (selection_data) == 0)
@ -193,9 +196,12 @@ image_drag_data_received (GtkWidget *widget,
if (gtk_selection_data_targets_include_image (selection_data, FALSE))
{
surface = gtk_selection_data_get_surface (selection_data);
gtk_image_set_from_surface (GTK_IMAGE (data), surface);
cairo_surface_destroy (surface);
GdkTexture *texture;
texture = gtk_selection_data_get_texture (selection_data);
gtk_image_set_from_texture (GTK_IMAGE (data), texture);
g_object_unref (texture);
}
else if (gtk_selection_data_targets_include_text (selection_data))
{

View File

@ -24,10 +24,10 @@ drag_begin (GtkWidget *widget,
gpointer data)
{
GtkWidget *image = GTK_WIDGET (data);
GdkTexture *texture;
cairo_surface_t *surface = gtk_image_get_surface (GTK_IMAGE (image));
cairo_surface_set_device_offset (surface, -2, -2);
gtk_drag_set_icon_surface (context, surface);
texture = gtk_image_get_texture (GTK_IMAGE (image));
gtk_drag_set_icon_paintable (context, GDK_PAINTABLE (texture), -2, -2);
}
void
@ -38,10 +38,11 @@ drag_data_get (GtkWidget *widget,
gpointer data)
{
GtkWidget *image = GTK_WIDGET (data);
GdkTexture *texture;
cairo_surface_t *surface = gtk_image_get_surface (GTK_IMAGE (image));
gtk_selection_data_set_surface (selection_data, surface);
texture = gtk_image_get_texture (GTK_IMAGE (image));
gtk_selection_data_set_texture (selection_data, texture);
g_object_unref (texture);
}
static void
@ -53,14 +54,15 @@ drag_data_received (GtkWidget *widget,
gpointer data)
{
GtkWidget *image = GTK_WIDGET (data);
cairo_surface_t *surface;
GdkTexture *texture;
if (gtk_selection_data_get_length (selection_data) < 0)
return;
surface = gtk_selection_data_get_surface (selection_data);
texture = gtk_selection_data_get_texture (selection_data);
gtk_image_set_from_texture (GTK_IMAGE (image), texture);
gtk_image_set_from_surface (GTK_IMAGE (image), surface);
g_object_unref (texture);
}
static gboolean
@ -77,9 +79,10 @@ main (int argc, char **argv)
GtkWidget *window, *grid;
GtkWidget *label, *image;
GtkIconTheme *theme;
cairo_surface_t *surface;
GdkTexture *texture;
gchar *icon_name = "help-browser";
gchar *anim_filename = NULL;
GtkIconInfo *icon_info;
GIcon *icon;
GFile *file;
@ -107,8 +110,11 @@ main (int argc, char **argv)
gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
theme = gtk_icon_theme_get_default ();
surface = gtk_icon_theme_load_surface (theme, icon_name, 48, gtk_widget_get_scale_factor (window), gtk_widget_get_window (window), 0, NULL);
image = gtk_image_new_from_surface (surface);
icon_info = gtk_icon_theme_lookup_icon_for_scale (theme, icon_name, 48, gtk_widget_get_scale_factor (window), GTK_ICON_LOOKUP_GENERIC_FALLBACK);
texture = gtk_icon_info_load_texture (icon_info);
g_object_unref (icon_info);
image = gtk_image_new_from_texture (texture);
g_object_unref (texture);
gtk_grid_attach (GTK_GRID (grid), image, 2, 1, 1, 1);
gtk_drag_source_set (image, GDK_BUTTON1_MASK,