[gnome-applets/rendering-cleanup: 10/12] multiload: Render with Cairo



commit 6e79169db16e87af4fe8f0dec3f96ec4deeda09b
Author: Benjamin Otte <otte redhat com>
Date:   Fri Aug 20 14:03:19 2010 +0200

    multiload: Render with Cairo
    
    https://bugzilla.gnome.org/show_bug.cgi?id=627490

 multiload/global.h     |    4 +-
 multiload/load-graph.c |  115 ++++++++++++++++-------------------------------
 multiload/properties.c |    2 -
 3 files changed, 40 insertions(+), 81 deletions(-)
---
diff --git a/multiload/global.h b/multiload/global.h
index 9f96ccf..4830c1a 100644
--- a/multiload/global.h
+++ b/multiload/global.h
@@ -34,11 +34,9 @@ struct _LoadGraph {
     guint data_size;
     guint *pos;
 
-    gint colors_allocated;
     GtkWidget *main_widget;
     GtkWidget *frame, *box, *disp;
-    GdkPixmap *pixmap;
-    GdkGC *gc;
+    cairo_surface_t *surface;
     int timer_index;
 
     gint show_frame;
diff --git a/multiload/load-graph.c b/multiload/load-graph.c
index 2ca1423..d189462 100644
--- a/multiload/load-graph.c
+++ b/multiload/load-graph.c
@@ -49,73 +49,52 @@ load_graph_draw (LoadGraph *g)
 {
     GtkStyle *style;
     guint i, j;
+    cairo_t *cr;
 
     /* we might get called before the configure event so that
      * g->disp->allocation may not have the correct size
      * (after the user resized the applet in the prop dialog). */
 
-    if (!g->pixmap)
-		g->pixmap = gdk_pixmap_new (gtk_widget_get_window (g->disp),
-					    g->draw_width, g->draw_height,
-					    gtk_widget_get_visual (g->disp)->depth);
+    if (!g->surface)
+		g->surface = gdk_window_create_similar_surface (gtk_widget_get_window (g->disp),
+					                        CAIRO_CONTENT_COLOR,
+                                                                g->draw_width, g->draw_height);
 	
     style = gtk_widget_get_style (g->disp);
 
-    /* Create GC if necessary. */
-    if (!g->gc)
-    {
-		g->gc = gdk_gc_new (gtk_widget_get_window (g->disp));
-		gdk_gc_copy (g->gc, style->black_gc);
-    }
-
-    /* Allocate colors. */
-    if (!g->colors_allocated)
-    {
-		GdkColormap *colormap;
-
-		colormap = gdk_drawable_get_colormap (gtk_widget_get_window (g->disp));
-		
-		for (i = 0; i < g->n; i++)
-	 	   gdk_colormap_alloc_color (colormap, &(g->colors [i]),
-					     FALSE, TRUE);
-
-		g->colors_allocated = 1;
-    }
-	
-    /* Erase Rectangle */
-    gdk_draw_rectangle (g->pixmap,
-			style->black_gc,
-			TRUE, 0, 0,
-			g->draw_width,
-			g->draw_height);
+    cr = cairo_create (g->surface);
+    cairo_set_line_width (cr, 1.0);
+    cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
 
     for (i = 0; i < g->draw_width; i++)
 		g->pos [i] = g->draw_height - 1;
 
     for (j = 0; j < g->n; j++)
     {
-		gdk_gc_set_foreground (g->gc, &(g->colors [j]));
+		gdk_cairo_set_source_color (cr, &(g->colors [j]));
 
 		for (i = 0; i < g->draw_width; i++) {
 			if (g->data [i][j] != 0) {
-				gdk_draw_line (g->pixmap, g->gc,
-					       g->draw_width - i - 1,
-					       g->pos[i],
-					       g->draw_width - i - 1,
-					       g->pos[i] - (g->data [i][j] - 1));
+                                cairo_move_to (cr,
+                                               g->draw_width - i - 0.5,
+                                               g->pos[i] + 0.5);
+                                cairo_line_to (cr,
+                                               g->draw_width - i - 0.5,
+					       g->pos[i] - (g->data [i][j] - 0.5));
 
 				g->pos [i] -= g->data [i][j];
 			}
 		}
+
+                cairo_stroke (cr);
     }
 	
-    gdk_draw_drawable (gtk_widget_get_window (g->disp),
-		       style->fg_gc [gtk_widget_get_state (g->disp)],
-		       g->pixmap,
-		       0, 0,
-		       0, 0,
-		       g->draw_width,
-		       g->draw_height);
+    cairo_destroy (cr);
+
+    cr = gdk_cairo_create (gtk_widget_get_window (g->disp));
+    cairo_set_source_surface (cr, g->surface, 0, 0);
+    cairo_paint (cr);
+    cairo_destroy (cr);
 }
 
 /* Updates the load graph when the timeout expires */
@@ -158,14 +137,9 @@ load_graph_unalloc (LoadGraph *g)
     g->size = panel_applet_gconf_get_int(g->multiload->applet, "size", NULL);
     g->size = MAX (g->size, 10);
 
-    if (g->pixmap) {
-		g_object_unref (g->pixmap);
-		g->pixmap = NULL;
-    }
-
-    if (g->gc) {
-		g_object_unref (g->gc);
-		g->gc = NULL;
+    if (g->surface) {
+		cairo_surface_destroy (g->surface);
+		g->surface = NULL;
     }
 
     g->allocated = FALSE;
@@ -209,24 +183,12 @@ load_graph_configure (GtkWidget *widget, GdkEventConfigure *event,
     
     load_graph_alloc (c);
  
-    if (!c->pixmap)
-	c->pixmap = gdk_pixmap_new (gtk_widget_get_window (c->disp),
-				    c->draw_width,
-				    c->draw_height,
-				    gtk_widget_get_visual (c->disp)->depth);
-
-    gdk_draw_rectangle (c->pixmap,
-			(gtk_widget_get_style (widget))->black_gc,
-			TRUE, 0,0,
-			c->draw_width,
-			c->draw_height);
-    gdk_draw_drawable (gtk_widget_get_window (widget),
-		       (gtk_widget_get_style (c->disp))->fg_gc [gtk_widget_get_state (widget)],
-		       c->pixmap,
-		       0, 0,
-		       0, 0,
-		       c->draw_width,
-		       c->draw_height);
+    if (!c->surface)
+	c->surface = gdk_window_create_similar_surface (gtk_widget_get_window (c->disp),
+                                                        CAIRO_CONTENT_COLOR,
+				                        c->draw_width, c->draw_height);
+
+    gtk_widget_queue_draw (widget);
 
     return TRUE;
 }
@@ -236,13 +198,14 @@ load_graph_expose (GtkWidget *widget, GdkEventExpose *event,
 		   gpointer data_ptr)
 {
     LoadGraph *g = (LoadGraph *) data_ptr;
+    cairo_t *cr;
+
+    cr = gdk_cairo_create (event->window);
+
+    cairo_set_source_surface (cr, g->surface, 0, 0);
+    cairo_paint (cr);
 
-    gdk_draw_drawable (gtk_widget_get_window (widget),
-		       (gtk_widget_get_style (widget))->fg_gc [gtk_widget_get_state (widget)],
-		       g->pixmap,
-		       event->area.x, event->area.y,
-		       event->area.x, event->area.y,
-		       event->area.width, event->area.height);
+    cairo_destroy (cr);
 
     return FALSE;
 }
diff --git a/multiload/properties.c b/multiload/properties.c
index 661e44d..522439e 100644
--- a/multiload/properties.c
+++ b/multiload/properties.c
@@ -271,8 +271,6 @@ color_picker_set_cb(GtkColorButton *color_picker, gpointer data)
 	gdk_color_parse(color_string, 
 			&(ma->graphs[prop_type]->colors[g_ascii_digit_value(gconf_path[strlen(gconf_path) - 1]) ]) );
 	
-	ma->graphs[prop_type]->colors_allocated = FALSE;
-	
 	return;
 }
 



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