[gtk+/wip/window-scales: 79/84] icon helper: support specifying pixbuf scale



commit 112f8da78c144ebf05343c6ab57d945938b53d9b
Author: Alexander Larsson <alexl redhat com>
Date:   Wed Jun 26 12:15:13 2013 +0200

    icon helper: support specifying pixbuf scale

 gtk/gtkiconhelper.c        |   71 +++++++++++++++++++++++++++++++++----------
 gtk/gtkiconhelperprivate.h |    5 ++-
 2 files changed, 58 insertions(+), 18 deletions(-)
---
diff --git a/gtk/gtkiconhelper.c b/gtk/gtkiconhelper.c
index c307b40..40ab0a5 100644
--- a/gtk/gtkiconhelper.c
+++ b/gtk/gtkiconhelper.c
@@ -30,6 +30,7 @@ struct _GtkIconHelperPrivate {
   GdkWindow *window;
 
   GdkPixbuf *orig_pixbuf;
+  int orig_pixbuf_scale;
   GdkPixbufAnimation *animation;
   GIcon *gicon;
   GtkIconSet *icon_set;
@@ -324,17 +325,34 @@ ensure_pixbuf_at_size (GtkIconHelper   *self,
   if (self->priv->rendered_pixbuf)
     return;
 
-  if (self->priv->pixel_size != -1 ||
-      self->priv->icon_size != GTK_ICON_SIZE_INVALID)
+  if (self->priv->force_scale_pixbuf &&
+      (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) ||
+      if (self->priv->orig_pixbuf_scale > 1 ||
+         /* These should divide the orig_pixbuf size by scale, but need not 
+            due to the above scale > 1 check */
+         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);
+       {
+         width = MIN (width, gdk_pixbuf_get_width (self->priv->orig_pixbuf) / self->priv->orig_pixbuf_scale);
+         height = MIN (height, gdk_pixbuf_get_height (self->priv->orig_pixbuf) / 
self->priv->orig_pixbuf_scale);
+         self->priv->rendered_pixbuf =
+           gdk_pixbuf_scale_simple (self->priv->orig_pixbuf,
+                                    width, height,
+                                    GDK_INTERP_BILINEAR);
+       }
+    }
+  else if (self->priv->orig_pixbuf_scale > 1)
+    {
+         width = gdk_pixbuf_get_width (self->priv->orig_pixbuf) / self->priv->orig_pixbuf_scale;
+         height =gdk_pixbuf_get_height (self->priv->orig_pixbuf) / self->priv->orig_pixbuf_scale;
+         self->priv->rendered_pixbuf =
+           gdk_pixbuf_scale_simple (self->priv->orig_pixbuf,
+                                    width, height,
+                                    GDK_INTERP_BILINEAR);
     }
 
   if (!self->priv->rendered_pixbuf)
@@ -351,10 +369,7 @@ _gtk_icon_helper_ensure_pixbuf (GtkIconHelper *self,
   switch (self->priv->storage_type)
     {
     case GTK_IMAGE_PIXBUF:
-      if (self->priv->force_scale_pixbuf)
-        ensure_pixbuf_at_size (self, context);
-      else
-        pixbuf = g_object_ref (self->priv->orig_pixbuf);
+      ensure_pixbuf_at_size (self, context);
       break;
 
     case GTK_IMAGE_STOCK:
@@ -454,23 +469,28 @@ ensure_pattern_at_size (GtkIconHelper   *self,
        self->priv->icon_size != GTK_ICON_SIZE_INVALID))
     {
       ensure_icon_size (self, context, &width, &height);
-      width *= scale;
-      height *= scale;
 
-      if (width < gdk_pixbuf_get_width (self->priv->orig_pixbuf) ||
-          height < gdk_pixbuf_get_height (self->priv->orig_pixbuf))
+      if (scale != self->priv->orig_pixbuf_scale ||
+         width < gdk_pixbuf_get_width (self->priv->orig_pixbuf) / self->priv->orig_pixbuf_scale ||
+          height < gdk_pixbuf_get_height (self->priv->orig_pixbuf) / self->priv->orig_pixbuf_scale)
        {
+         width = MIN (width * scale, gdk_pixbuf_get_width (self->priv->orig_pixbuf) * scale / 
self->priv->orig_pixbuf_scale);
+         height = MIN (height * scale, gdk_pixbuf_get_height (self->priv->orig_pixbuf) * scale / 
self->priv->orig_pixbuf_scale);
+
          pixbuf = gdk_pixbuf_scale_simple (self->priv->orig_pixbuf,
                                            width, height,
                                            GDK_INTERP_BILINEAR);
        }
       else
-       pixbuf = g_object_ref (self->priv->orig_pixbuf);
+       {
+         pixbuf = g_object_ref (self->priv->orig_pixbuf); 
+         scale = self->priv->orig_pixbuf_scale;
+       }
     }
   else
     {
       pixbuf = g_object_ref (self->priv->orig_pixbuf);
-      scale = 1;
+      scale = self->priv->orig_pixbuf_scale;
     }
 
   self->priv->rendered_pattern_width = (gdk_pixbuf_get_width (pixbuf) + scale - 1) / scale;
@@ -932,3 +952,20 @@ _gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
       _gtk_icon_helper_invalidate (self);
     }
 }
+
+void 
+_gtk_icon_helper_set_pixbuf_scale (GtkIconHelper *self,
+                                  int scale)
+{
+  if (self->priv->orig_pixbuf_scale != scale)
+    {
+      self->priv->orig_pixbuf_scale = scale;
+      _gtk_icon_helper_invalidate (self);
+    }
+}
+
+int
+_gtk_icon_helper_get_pixbuf_scale (GtkIconHelper *self)
+{
+  return self->priv->orig_pixbuf_scale;
+}
diff --git a/gtk/gtkiconhelperprivate.h b/gtk/gtkiconhelperprivate.h
index 10467be..9011d84 100644
--- a/gtk/gtkiconhelperprivate.h
+++ b/gtk/gtkiconhelperprivate.h
@@ -79,7 +79,10 @@ void _gtk_icon_helper_set_gicon (GtkIconHelper *self,
                                  GIcon *gicon,
                                  GtkIconSize icon_size);
 void _gtk_icon_helper_set_pixbuf (GtkIconHelper *self,
-                                  GdkPixbuf *pixbuf);
+                                 GdkPixbuf *pixbuf);
+void _gtk_icon_helper_set_pixbuf_scale (GtkIconHelper *self,
+                                       int scale);
+int  _gtk_icon_helper_get_pixbuf_scale (GtkIconHelper *self);
 void _gtk_icon_helper_set_animation (GtkIconHelper *self,
                                      GdkPixbufAnimation *animation);
 void _gtk_icon_helper_set_icon_set (GtkIconHelper *self,


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