[gnome-applets/rendering-cleanup: 12/12] battstat: Draw with Cairo



commit c7371e955fcd09bcb4827f601687c2fdb4c7d2f3
Author: Benjamin Otte <otte redhat com>
Date:   Fri Aug 20 14:16:26 2010 +0200

    battstat: Draw with Cairo
    
    https://bugzilla.gnome.org/show_bug.cgi?id=627490

 battstat/battstat_applet.c |  173 +++++++++++++++++++-------------------------
 1 files changed, 76 insertions(+), 97 deletions(-)
---
diff --git a/battstat/battstat_applet.c b/battstat/battstat_applet.c
index 16dbc31..f03e851 100644
--- a/battstat/battstat_applet.c
+++ b/battstat/battstat_applet.c
@@ -73,8 +73,7 @@ static const GtkActionEntry battstat_menu_actions [] = {
 #define DC_POWER_STRING _("System is running on battery power")
 
 /* The icons for Battery, Critical, AC and Charging */
-static GdkPixmap *statusimage[STATUS_PIXMAP_NUM];
-static GdkBitmap *statusmask[STATUS_PIXMAP_NUM];
+static GdkPixbuf *statusimage[STATUS_PIXMAP_NUM];
 
 /* Assuming a horizontal battery, the colour is drawn into it one horizontal
    line at a time as a vertical gradient.  The following arrays decide where
@@ -218,29 +217,20 @@ static GdkColor darkred[] = {
 static void
 initialise_global_pixmaps( void )
 {
-  GdkDrawable *defaults;
-
-  defaults = gdk_screen_get_root_window( gdk_screen_get_default() );
-
   statusimage[STATUS_PIXMAP_BATTERY] =
-    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_BATTERY],
-                                  NULL, battery_small_xpm );
+    gdk_pixbuf_new_from_xpm_data ((const char **) battery_small_xpm);
 
   statusimage[STATUS_PIXMAP_METER] =
-    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_METER],
-                                  NULL, battery_small_meter_xpm );
+    gdk_pixbuf_new_from_xpm_data ((const char **) battery_small_meter_xpm);
 
   statusimage[STATUS_PIXMAP_AC] =
-    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_AC],
-                                  NULL, ac_small_xpm );
+    gdk_pixbuf_new_from_xpm_data ((const char **) ac_small_xpm);
    
   statusimage[STATUS_PIXMAP_CHARGE] =
-    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_CHARGE],
-                                  NULL, charge_small_xpm );
+    gdk_pixbuf_new_from_xpm_data ((const char **) charge_small_xpm);
    
   statusimage[STATUS_PIXMAP_WARNING] =
-    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_WARNING],
-                                  NULL, warning_small_xpm );
+    gdk_pixbuf_new_from_xpm_data ((const char **) warning_small_xpm);
 }
 
 /* For non-truecolour displays, each GdkColor has to have a palette entry
@@ -713,14 +703,54 @@ update_tooltip( ProgressData *battstat, BatteryStatus *info )
   g_free (tiptext);
 }
 
+static void
+pixbuf_draw_line( GdkPixbuf *pixbuf, GdkColor *colour, int x1, int y1, int x2, int y2)
+{
+  guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);
+  int stride = gdk_pixbuf_get_rowstride (pixbuf);
+  int channels = gdk_pixbuf_get_n_channels (pixbuf);
+  guchar r, g, b, a;
+  int i, n;
+
+  r = colour->red >> 8;
+  g = colour->green >> 8;
+  b = colour->blue >> 8;
+  a = 0xFF;
+
+  pixels += stride * y1 + 4 * x1;
+
+  if (x1 == x2)
+  {
+    /* stride = gdk_pixbuf_get_rowstride (pixbuf); */
+    n = y2 - y1;
+  }
+  else if (y1 == y2)
+  {
+    stride = gdk_pixbuf_get_n_channels (pixbuf);
+    n = x2 - x1;
+  }
+  else
+    g_assert_not_reached ();
+
+  for (i = 0; i < n; i++)
+  {
+    pixels[0] = r;
+    pixels[1] = g;
+    pixels[2] = b;
+    if (channels == 4)
+      pixels[3] = a;
+
+    pixels += stride;
+  }
+}
+
 /* Redraw the battery meter image.
  */
 static void
 update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
 {
   GdkColor *color, *darkcolor;
-  GdkPixmap *pixmap;
-  GdkBitmap *pixmask;
+  GdkPixbuf *pixbuf;
   guint progress_value;
   gint i, x;
   int batt_life;
@@ -751,25 +781,13 @@ update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
     darkcolor = darkgreen;
   }
 
-  /* We keep this pixgc allocated so we don't have to alloc/free it every
-     time.  A widget has to be realized before it has a valid ->window so
-     we do that here for battstat->applet just in case it's not already done.
-  */
-  if( battstat->pixgc == NULL )
-  {
-    gtk_widget_realize( battstat->applet );
-    battstat->pixgc = gdk_gc_new( gtk_widget_get_window (battstat->applet) );
-  }
-
   /* Depending on if the meter is horizontally oriented start out with the
      appropriate XPM image (from pixmaps.h)
   */
   if (battstat->horizont)
-    pixmap = gdk_pixmap_create_from_xpm_d( gtk_widget_get_window (battstat->applet), &pixmask,
-                                           NULL, battery_gray_xpm );
+    pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) battery_gray_xpm);
   else
-    pixmap = gdk_pixmap_create_from_xpm_d( gtk_widget_get_window (battstat->applet), &pixmask,
-                                           NULL, battery_y_gray_xpm );
+    pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) battery_y_gray_xpm);
 
   /* The core code responsible for painting the battery meter.  For each
      colour in our gradient array, draw a vertical or horizontal line
@@ -780,13 +798,11 @@ update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
 	    
     for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
     {
-      gdk_gc_set_foreground (battstat->pixgc, &color[i]);
-
       if (battstat->horizont)
-        gdk_draw_line (pixmap, battstat->pixgc, pixel_offset_top[i], i + 2,
+        pixbuf_draw_line (pixbuf, &color[i], pixel_offset_top[i], i + 2,
                        pixel_offset_top[i] + progress_value, i + 2);
       else
-        gdk_draw_line (pixmap, battstat->pixgc, i + 2, pixel_offset_top[i],
+        pixbuf_draw_line (pixbuf, &color[i], i + 2, pixel_offset_top[i],
                        i + 2, pixel_offset_top[i] + progress_value);
     }
   }
@@ -796,15 +812,13 @@ update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
 
     for( i = 0; i < G_N_ELEMENTS( orange ); i++)
     {
-      gdk_gc_set_foreground (battstat->pixgc, &color[i]);
-
       if (battstat->horizont)
-        gdk_draw_line (pixmap, battstat->pixgc, pixel_offset_bottom[i], i + 2,
-                       pixel_offset_bottom[i] - progress_value, i + 2);
+        pixbuf_draw_line (pixbuf, &color[i], pixel_offset_bottom[i], i + 2,
+                          pixel_offset_bottom[i] - progress_value, i + 2);
       else
-        gdk_draw_line (pixmap, battstat->pixgc, i + 2,
-                       pixel_offset_bottom[i] - 1, i + 2,
-                       pixel_offset_bottom[i] - progress_value);
+        pixbuf_draw_line (pixbuf, &color[i], i + 2,
+                          pixel_offset_bottom[i] - 1, i + 2,
+                          pixel_offset_bottom[i] - progress_value);
     }
 
     for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
@@ -815,16 +829,14 @@ update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
 
       if (progress_value < 33)
       {
-        gdk_gc_set_foreground (battstat->pixgc, &darkcolor[i]);
-		     
         if (battstat->horizont)
-          gdk_draw_line (pixmap, battstat->pixgc,
-                         pixel_offset_bottom[i] - progress_value - 1,
-                         i + 2, x, i + 2);
+          pixbuf_draw_line (pixbuf, &darkcolor[i],
+                            pixel_offset_bottom[i] - progress_value - 1,
+                            i + 2, x, i + 2);
         else
-          gdk_draw_line (pixmap, battstat->pixgc, i + 2,
-                         pixel_offset_bottom[i] - progress_value - 1,
-                         i + 2, x);
+          pixbuf_draw_line (pixbuf, &darkcolor[i], i + 2,
+                            pixel_offset_bottom[i] - progress_value - 1,
+                            i + 2, x);
       }
     }
   }
@@ -832,15 +844,14 @@ update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
   /* Store our newly created pixmap into the GtkImage.  This results in
      the last reference to the old pixmap/mask being dropped.
   */
-  gtk_image_set_from_pixmap( GTK_IMAGE(battstat->battery),
-                             pixmap, pixmask );
+  gtk_image_set_from_pixbuf( GTK_IMAGE(battstat->battery),
+                             pixbuf);
 
   /* The GtkImage does not assume a reference to the pixmap or mask;
      you still need to unref them if you own references. GtkImage will
      add its own reference rather than adopting yours.
   */
-  g_object_unref( G_OBJECT(pixmap) );
-  g_object_unref( G_OBJECT(pixmask) );
+  g_object_unref( G_OBJECT(pixbuf) );
 }
 
 /* Update the text label that either shows the percentage of time left.
@@ -871,23 +882,6 @@ update_percent_label( ProgressData *battstat, BatteryStatus *info )
   g_free (new_label);
 }
 
-/* Utility function to create a copy of a GdkPixmap */
-static GdkPixmap *
-copy_gdk_pixmap( GdkPixmap *src, GdkGC *gc )
-{
-  gint height, width;
-  GdkPixmap *dest;
-
-  gdk_drawable_get_size( GDK_DRAWABLE( src ), &width, &height );
-
-  dest = gdk_pixmap_new( GDK_DRAWABLE( src ), width, height, -1 );
-
-  gdk_draw_drawable( GDK_DRAWABLE( dest ), gc, GDK_DRAWABLE( src ),
-                     0, 0, 0, 0, width, height );
-
-  return dest;
-}
-
 /* Determine what status icon we ought to be displaying and change the
    status icon to display it if it is different from what we are currently
    showing.
@@ -933,26 +927,14 @@ possibly_update_status_icon( ProgressData *battstat, BatteryStatus *info )
        battstat->last_pixmap_index != STATUS_PIXMAP_METER) )
   {
     GdkColor *colour;
-    GdkPixmap *meter;
+    GdkPixbuf *meter;
     guint progress_value;
     gint i, x;
 
-    /* We keep this pixgc allocated so we don't have to alloc/free it every
-       time.  A widget has to be realized before it has a valid ->window so
-       we do that here for battstat->applet just in case it's not already done.
-    */
-    if( battstat->pixgc == NULL )
-    {
-      gtk_widget_realize( battstat->applet );
-      battstat->pixgc = gdk_gc_new( gtk_widget_get_window (battstat->applet) );
-    }
-
     /* Pull in a clean version of the icons so that we don't paint over
-       top of the same icon over and over.  We neglect to free/update the
-       statusmask here since it will always stay the same.
+       top of the same icon over and over.
      */
-    meter = copy_gdk_pixmap( statusimage[STATUS_PIXMAP_METER],
-                             battstat->pixgc );
+    meter = gdk_pixbuf_copy ( statusimage[STATUS_PIXMAP_METER]);
 
     if (batt_life <= battstat->red_val)
     {
@@ -975,21 +957,19 @@ possibly_update_status_icon( ProgressData *battstat, BatteryStatus *info )
     
     for( i = 0; i < 10; i++ )
     {
-      gdk_gc_set_foreground( battstat->pixgc, &colour[(i * 13 / 10)] );
-
       if( i >= 2 && i <= 7 )
 	x = 17;
       else
 	x = 16;
 
-      gdk_draw_line( meter, battstat->pixgc,
-		     i + 1, x - progress_value,
-		     i + 1, x );
+      pixbuf_draw_line( meter, &colour[(i * 13 / 10)],
+                        i + 1, x - progress_value,
+                        i + 1, x );
     }
 
     /* force a redraw immediately */
-    gtk_image_set_from_pixmap( GTK_IMAGE (battstat->status),
-                               meter, statusmask[STATUS_PIXMAP_METER] );
+    gtk_image_set_from_pixbuf( GTK_IMAGE (battstat->status),
+                               meter);
 
     /* free our private pixmap copy */
     g_object_unref( G_OBJECT( meter ) );
@@ -997,9 +977,8 @@ possibly_update_status_icon( ProgressData *battstat, BatteryStatus *info )
   }
   else if( pixmap_index != battstat->last_pixmap_index )
   {
-    gtk_image_set_from_pixmap (GTK_IMAGE (battstat->status),
-                               statusimage[pixmap_index],
-                               statusmask[pixmap_index]);
+    gtk_image_set_from_pixbuf (GTK_IMAGE (battstat->status),
+                               statusimage[pixmap_index]);
     battstat->last_pixmap_index = pixmap_index;
   }
 }



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