[balsa/wip/gtk4: 41/351] gtk_image_get_pixbuf is deprecated



commit 7deda7edfb1ccb834040b044688be0fd4f3eea62
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Tue Oct 24 12:45:21 2017 -0400

    gtk_image_get_pixbuf is deprecated

 src/balsa-print-object-header.c |   25 ++++++++-------
 src/balsa-print-object-header.h |    2 +-
 src/balsa-print-object.c        |   61 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 13 deletions(-)
---
diff --git a/src/balsa-print-object-header.c b/src/balsa-print-object-header.c
index 9c272e1..871fc61 100644
--- a/src/balsa-print-object-header.c
+++ b/src/balsa-print-object-header.c
@@ -106,8 +106,9 @@ balsa_print_object_header_destroy(GObject * self)
     BalsaPrintObjectHeader *po = BALSA_PRINT_OBJECT_HEADER(self);
 
     g_free(po->headers);
-    if (po->face)
-       g_object_unref(po->face);
+    if (po->face) {
+       g_clear_pointer(&po->face, (GDestroyNotify) cairo_surface_destroy);
+    }
 
     G_OBJECT_CLASS(parent_class)->finalize(self);
 }
@@ -137,7 +138,7 @@ balsa_print_object_header_new_real(GList * list,
     gint p_label_width;
     gint p_layout_width;
     gdouble c_face_height;
-    GdkPixbuf *face;
+    cairo_surface_t *face;
 
     g_return_val_if_fail(headers != NULL, NULL);
 
@@ -207,8 +208,8 @@ balsa_print_object_header_new_real(GList * list,
                g_error_free(err);
 
            if (f_widget) {
-               face = gtk_image_get_pixbuf(GTK_IMAGE(f_widget));
-               g_object_ref(G_OBJECT(face));
+               face = gtk_image_get_surface(GTK_IMAGE(f_widget));
+               cairo_surface_reference(face);
                gtk_widget_destroy(f_widget);
            }
        }
@@ -244,8 +245,8 @@ balsa_print_object_header_new_real(GList * list,
     /* check if we have a face */
     c_use_width = psetup->c_width - 2 * psetup->curr_depth * C_LABEL_SEP;
     if (face) {
-       p_layout_width = C_TO_P(c_use_width - gdk_pixbuf_get_width(face) - C_LABEL_SEP);
-       c_face_height = gdk_pixbuf_get_height(face);
+       p_layout_width = C_TO_P(c_use_width - cairo_image_surface_get_width(face) - C_LABEL_SEP);
+       c_face_height = cairo_image_surface_get_height(face);
     } else {
        p_layout_width = C_TO_P(c_use_width);
        c_face_height = 0;
@@ -478,12 +479,12 @@ balsa_print_object_header_draw(BalsaPrintObject * self,
        gdouble c_face_h;
        gdouble c_face_w;
 
-       c_face_h = gdk_pixbuf_get_height(po->face);
-       c_face_w = gdk_pixbuf_get_width(po->face);
+       c_face_h = cairo_image_surface_get_height(po->face);
+       c_face_w = cairo_image_surface_get_width(po->face);
 
-       cairo_print_pixbuf(cairo_ctx, po->face,
-                          self->c_at_x + self->c_width - c_face_w,
-                          self->c_at_y, 1.0);
+       cairo_print_surface(cairo_ctx, po->face,
+                           self->c_at_x + self->c_width - c_face_w,
+                           self->c_at_y, 1.0);
        if (c_face_h > self->c_height)
            self->c_height = c_face_h;
     }
diff --git a/src/balsa-print-object-header.h b/src/balsa-print-object-header.h
index 88eaaec..669fb3e 100644
--- a/src/balsa-print-object-header.h
+++ b/src/balsa-print-object-header.h
@@ -48,7 +48,7 @@ struct _BalsaPrintObjectHeader {
     gint p_label_width;
     gint p_layout_width;
     gchar *headers;
-    GdkPixbuf *face;
+    cairo_surface_t *face;
 };
 
 
diff --git a/src/balsa-print-object.c b/src/balsa-print-object.c
index 4ad4bb2..11ff43e 100644
--- a/src/balsa-print-object.c
+++ b/src/balsa-print-object.c
@@ -436,3 +436,64 @@ split_for_layout(PangoLayout * layout, const gchar * text,
     /* return the list */
     return split_list;
 }
+
+
+/* print a cairo_surface_t to cairo at the specified position and with the
+ * specified scale */
+gboolean
+cairo_print_pixbuf(cairo_t * cairo_ctx, const cairo_surface_t * surface,
+                  gdouble c_at_x, gdouble c_at_y, gdouble scale)
+{
+    gint width;
+    gint height;
+    gint rowstride;
+    guint32 *dest;
+    cairo_format_t format;
+    cairo_surface_t *surface;
+    cairo_pattern_t *pattern;
+    cairo_matrix_t matrix;
+
+    /* paranoia checks */
+    g_return_val_if_fail(cairo_ctx != NULL, FALSE);
+    g_return_val_if_fail(surface   != NULL, FALSE);
+
+    /* must have 3 (no alpha) or 4 (with alpha) channels */
+    format = cairo_image_surface_get_format(surface);
+    g_return_val_if_fail(format == CAIRO_FORMAT_ARGB32 ||
+                         format == CAIRO_FORMAT_RGB24,
+                         FALSE);
+
+    width = cairo_image_surface_get_width(surface);
+    height = cairo_image_surface_get_height(surface);
+
+    /* save current state */
+    cairo_save(cairo_ctx);
+
+    /* set the curface */
+    cairo_set_source_surface(cairo_ctx, surface, c_at_x, c_at_y);
+
+    /* scale */
+    pattern = cairo_get_source(cairo_ctx);
+    cairo_pattern_get_matrix(pattern, &matrix);
+    matrix.xx /= scale;
+    matrix.yy /= scale;
+    matrix.x0 /= scale;
+    matrix.y0 /= scale;
+    cairo_pattern_set_matrix(pattern, &matrix);
+
+    /* clip around the image */
+    cairo_new_path(cairo_ctx);
+    cairo_move_to(cairo_ctx, c_at_x, c_at_y);
+    cairo_line_to(cairo_ctx, c_at_x + width * scale, c_at_y);
+    cairo_line_to(cairo_ctx, c_at_x + width * scale,
+                 c_at_y + height * scale);
+    cairo_line_to(cairo_ctx, c_at_x, c_at_y + height * scale);
+    cairo_close_path(cairo_ctx);
+    cairo_clip(cairo_ctx);
+
+    /* paint, restore and clean up */
+    cairo_paint(cairo_ctx);
+    cairo_restore(cairo_ctx);
+
+    return TRUE;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]