[gtk+/composite-templates] Add API to force a GtkIconHelper pixbuf rescaling



commit 03340a47e63ea293e9edae07ab8ac58f09ead58a
Author: Carlos Garnacho <carlos lanedo com>
Date:   Fri Jul 13 15:54:29 2012 +0200

    Add API to force a GtkIconHelper pixbuf rescaling
    
    This API is handy in entries and status icons, where the size is
    limited and independent of the pixbuf size

 gtk/gtkiconhelper.c        |   54 ++++++++++++++++++++++++++++++++++++++++++-
 gtk/gtkiconhelperprivate.h |    5 ++++
 2 files changed, 57 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkiconhelper.c b/gtk/gtkiconhelper.c
index 589f1e9..6f23914 100644
--- a/gtk/gtkiconhelper.c
+++ b/gtk/gtkiconhelper.c
@@ -36,7 +36,8 @@ struct _GtkIconHelperPrivate {
   GtkIconSize icon_size;
   gint pixel_size;
 
-  gboolean use_fallback;
+  guint use_fallback : 1;
+  guint force_scale_pixbuf : 1;
 
   GdkPixbuf *rendered_pixbuf;
   GtkStateFlags last_rendered_state;
@@ -277,6 +278,35 @@ ensure_pixbuf_for_icon_set (GtkIconHelper *self,
     gtk_icon_set_render_icon_pixbuf (icon_set, context, self->priv->icon_size);
 }
 
+static void
+ensure_pixbuf_at_size (GtkIconHelper   *self,
+                       GtkStyleContext *context)
+{
+  gint width, height;
+
+  if (!check_invalidate_pixbuf (self, context))
+    return;
+
+  if (self->priv->rendered_pixbuf)
+    return;
+
+  if (self->priv->pixel_size != -1 ||
+      self->priv->icon_size != GTK_ICON_SIZE_INVALID)
+    {
+      ensure_icon_size (self, context, &width, &height);
+
+      if (width < gdk_pixbuf_get_width (self->priv->orig_pixbuf) ||
+          height < gdk_pixbuf_get_height (self->priv->orig_pixbuf))
+        self->priv->rendered_pixbuf =
+          gdk_pixbuf_scale_simple (self->priv->orig_pixbuf,
+                                   width, height,
+                                   GDK_INTERP_BILINEAR);
+    }
+
+  if (!self->priv->rendered_pixbuf)
+    self->priv->rendered_pixbuf = g_object_ref (self->priv->orig_pixbuf);
+}
+
 GdkPixbuf *
 _gtk_icon_helper_ensure_pixbuf (GtkIconHelper *self,
                                 GtkStyleContext *context)
@@ -287,7 +317,10 @@ _gtk_icon_helper_ensure_pixbuf (GtkIconHelper *self,
   switch (self->priv->storage_type)
     {
     case GTK_IMAGE_PIXBUF:
-      pixbuf = g_object_ref (self->priv->orig_pixbuf);
+      if (self->priv->force_scale_pixbuf)
+        ensure_pixbuf_at_size (self, context);
+      else
+        pixbuf = g_object_ref (self->priv->orig_pixbuf);
       break;
 
     case GTK_IMAGE_STOCK:
@@ -563,3 +596,20 @@ _gtk_icon_helper_get_is_empty (GtkIconHelper *self)
 {
   return (self->priv->storage_type == GTK_IMAGE_EMPTY);
 }
+
+gboolean
+_gtk_icon_helper_get_force_scale_pixbuf (GtkIconHelper *self)
+{
+  return self->priv->force_scale_pixbuf;
+}
+
+void
+_gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
+                                         gboolean       force_scale)
+{
+  if (self->priv->force_scale_pixbuf != force_scale)
+    {
+      self->priv->force_scale_pixbuf = force_scale;
+      _gtk_icon_helper_invalidate (self);
+    }
+}
diff --git a/gtk/gtkiconhelperprivate.h b/gtk/gtkiconhelperprivate.h
index 8cc8a47..c2b9f08 100644
--- a/gtk/gtkiconhelperprivate.h
+++ b/gtk/gtkiconhelperprivate.h
@@ -124,6 +124,11 @@ void _gtk_icon_helper_draw (GtkIconHelper *self,
                             gdouble x,
                             gdouble y);
 
+gboolean _gtk_icon_helper_get_force_scale_pixbuf (GtkIconHelper *self);
+void     _gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
+                                                  gboolean       force_scale);
+
+
 G_END_DECLS
 
 #endif /* __GTK_ICON_HELPER_H__ */



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