[notification-daemon] Scale images down to a max icon size of 48px



commit 001de6e0e980de2900899f16cad381341ee852ad
Author: William Jon McCann <jmccann redhat com>
Date:   Fri Jan 29 03:18:24 2010 -0500

    Scale images down to a max icon size of 48px
    
    Otherwise large images can really mess up the display.  At the
    moment there is no limit to the size of the image that may be
    displayed.

 src/themes/slider/theme.c |   67 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 62 insertions(+), 5 deletions(-)
---
diff --git a/src/themes/slider/theme.c b/src/themes/slider/theme.c
index 892397a..a861989 100644
--- a/src/themes/slider/theme.c
+++ b/src/themes/slider/theme.c
@@ -26,6 +26,8 @@
 typedef void (*ActionInvokedCb) (GtkWindow *nw, const char *key);
 typedef void (*UrlClickedCb)    (GtkWindow *nw, const char *url);
 
+#define MAX_ICON_SIZE 48
+
 typedef struct
 {
         GtkWidget *win;
@@ -675,25 +677,80 @@ set_notification_text (GtkWindow  *nw,
                                      -1);
 }
 
+static GdkPixbuf *
+scale_pixbuf (GdkPixbuf *pixbuf,
+              int        max_width,
+              int        max_height,
+              gboolean   no_stretch_hint)
+{
+        int        pw;
+        int        ph;
+        float      scale_factor_x = 1.0;
+        float      scale_factor_y = 1.0;
+        float      scale_factor = 1.0;
+
+        pw = gdk_pixbuf_get_width (pixbuf);
+        ph = gdk_pixbuf_get_height (pixbuf);
+
+        /* Determine which dimension requires the smallest scale. */
+        scale_factor_x = (float) max_width / (float) pw;
+        scale_factor_y = (float) max_height / (float) ph;
+
+        if (scale_factor_x > scale_factor_y) {
+                scale_factor = scale_factor_y;
+        } else {
+                scale_factor = scale_factor_x;
+        }
+
+        /* always scale down, allow to disable scaling up */
+        if (scale_factor < 1.0 || !no_stretch_hint) {
+                int scale_x;
+                int scale_y;
+
+                scale_x = (int) (pw * scale_factor);
+                scale_y = (int) (ph * scale_factor);
+                return gdk_pixbuf_scale_simple (pixbuf,
+                                                scale_x,
+                                                scale_y,
+                                                GDK_INTERP_BILINEAR);
+        } else {
+                return g_object_ref (pixbuf);
+        }
+}
+
 void
 set_notification_icon (GtkWindow *nw,
                        GdkPixbuf *pixbuf)
 {
-        WindowData *windata = g_object_get_data (G_OBJECT (nw), "windata");
+        WindowData *windata;
+        GdkPixbuf  *scaled;
 
-        g_assert (windata != NULL);
+        windata = g_object_get_data (G_OBJECT (nw), "windata");
 
-        gtk_image_set_from_pixbuf (GTK_IMAGE (windata->icon), pixbuf);
+        g_assert (windata != NULL);
 
+        scaled = NULL;
         if (pixbuf != NULL) {
-                int pixbuf_width = gdk_pixbuf_get_width (pixbuf);
+                scaled = scale_pixbuf (pixbuf,
+                                       MAX_ICON_SIZE,
+                                       MAX_ICON_SIZE,
+                                       TRUE);
+        }
+
+        gtk_image_set_from_pixbuf (GTK_IMAGE (windata->icon), scaled);
+
+        if (scaled != NULL) {
+                int pixbuf_width = gdk_pixbuf_get_width (scaled);
 
                 gtk_widget_show (windata->icon);
                 gtk_widget_set_size_request (windata->iconbox,
                                              MAX (BODY_X_OFFSET, pixbuf_width), -1);
+                g_object_unref (scaled);
         } else {
                 gtk_widget_hide (windata->icon);
-                gtk_widget_set_size_request (windata->iconbox, BODY_X_OFFSET, -1);
+                gtk_widget_set_size_request (windata->iconbox,
+                                             BODY_X_OFFSET,
+                                             -1);
         }
 
         update_content_hbox_visibility (windata);



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