[goocanvas/new-api] 2010-07-09 Damon Chaplin <damon gnome org>



commit f89cc7ca243f7bd52d3b40c64ca90ee3c5f749b6
Author: Damon Chaplin <damon gnome org>
Date:   Fri Jul 9 12:44:20 2010 +0100

    2010-07-09  Damon Chaplin  <damon gnome org>
    
    	    * src/goocanvas.c (goo_canvas_set_default_style): new function to set
    	    a default style for the canvas, so you can turn antialiasing off
    	    for all items, for example. Or set the default line width/cap/join.
    	    (reconfigure_canvas): update window_x/y to fix a redrawing problem
    	    of the static items when the bounds are changed. This code is a bit
    	    complex so I'm not 100% sure I've fixed it.
    
    	    * demo/demo.c: test changing the default style.

 ChangeLog       |   11 +++++++++
 demo/demo.c     |   26 +++++++++++++++++++-
 src/goocanvas.c |   68 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/goocanvas.h |    6 +++++
 4 files changed, 105 insertions(+), 6 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 815d1c9..80db886 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2010-07-09  Damon Chaplin  <damon gnome org>
 
+	* src/goocanvas.c (goo_canvas_set_default_style): new function to set
+	a default style for the canvas, so you can turn antialiasing off
+	for all items, for example. Or set the default line width/cap/join.
+	(reconfigure_canvas): update window_x/y to fix a redrawing problem
+	of the static items when the bounds are changed. This code is a bit
+	complex so I'm not 100% sure I've fixed it.
+
+	* demo/demo.c: test changing the default style.
+
+2010-07-09  Damon Chaplin  <damon gnome org>
+
 	* src/goocanvasitemsimple.c (goo_canvas_item_simple_set_stroke_options):
 	keep a single black pattern rather than recreating one when needed.
 
diff --git a/demo/demo.c b/demo/demo.c
index 70879bb..d61f928 100644
--- a/demo/demo.c
+++ b/demo/demo.c
@@ -17,11 +17,13 @@
 #include <goocanvas.h>
 #include "demo-item.h"
 
+static GtkWidget *canvas;
 static GooCanvasItem *ellipse2, *textitem;
 
 static gboolean dragging = FALSE;
 static double drag_x, drag_y;
 
+static GooCanvasStyle *canvas_style;
 static GooCanvasStyle *diamond_style = NULL;
 
 static void setup_canvas (GooCanvas *canvas);
@@ -204,6 +206,12 @@ change_style_clicked (GtkWidget *button, GooCanvas *canvas)
 
   if (last_state == 0)
     {
+      g_object_set (canvas_style,
+		    "line-width", 5.0,
+		    "stroke-color", "red",
+		    NULL);
+      goo_canvas_set_default_style (canvas, canvas_style);
+
       g_object_set (diamond_style,
 		    "line-width", 3.0,
 		    "stroke-color", "orange",
@@ -213,6 +221,12 @@ change_style_clicked (GtkWidget *button, GooCanvas *canvas)
     }
   else if (last_state == 1)
     {
+      g_object_set (canvas_style,
+		    "line-width", 1.0,
+		    "stroke-color", "blue",
+		    NULL);
+      goo_canvas_set_default_style (canvas, canvas_style);
+
       g_object_set (diamond_style,
 		    "line-width", 1.0,
 		    "stroke-color", "red",
@@ -222,6 +236,12 @@ change_style_clicked (GtkWidget *button, GooCanvas *canvas)
     }
   else
     {
+      g_object_set (canvas_style,
+		    "line-width", 2.0,
+		    "stroke-pattern", NULL,
+		    NULL);
+      goo_canvas_set_default_style (canvas, canvas_style);
+
       g_object_set (diamond_style,
 		    "stroke-color", "black",
 		    "line-width", 1.0,
@@ -512,7 +532,7 @@ create_canvas_primitives ()
 	GtkWidget *vbox;
 	GtkWidget *hbox;
 	GtkWidget *w;
-	GtkWidget *scrolled_win, *canvas;
+	GtkWidget *scrolled_win;
 	GtkAdjustment *adj;
 	GSList *group = NULL;
 
@@ -543,6 +563,8 @@ create_canvas_primitives ()
 		      NULL);
 	goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 604, 454);
 
+	canvas_style = g_object_new (GOO_TYPE_CANVAS_STYLE, NULL);
+
 	/* Scale */
 
 	w = gtk_label_new ("Scale:");
@@ -646,7 +668,7 @@ create_canvas_primitives ()
 			  G_CALLBACK (change_bounds_clicked),
 			  canvas);
 
-	w = gtk_button_new_with_label("Change Style");
+	w = gtk_button_new_with_label("Change Styles");
 	gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
 	gtk_widget_show (w);
 	g_signal_connect (w, "clicked",
diff --git a/src/goocanvas.c b/src/goocanvas.c
index 2398e7a..b1e396d 100644
--- a/src/goocanvas.c
+++ b/src/goocanvas.c
@@ -539,6 +539,12 @@ goo_canvas_dispose (GObject *object)
       canvas->vadjustment = NULL;
     }
 
+  if (canvas->style)
+    {
+      g_object_unref (canvas->style);
+      canvas->style = NULL;
+    }
+
   G_OBJECT_CLASS (goo_canvas_parent_class)->dispose (object);
 }
 
@@ -568,6 +574,9 @@ goo_canvas_get_default_line_width (GooCanvas *canvas)
   if (!canvas)
     return 2.0;
 
+  if (canvas->style && canvas->style->line_width >= 0.0)
+    return canvas->style->line_width;
+
   /* We use the same default as cairo when using pixels, i.e. 2 pixels.
      For other units we use 2 points, or thereabouts. */
   switch (canvas->units)
@@ -590,6 +599,33 @@ goo_canvas_get_default_line_width (GooCanvas *canvas)
 }
 
 
+static void
+goo_canvas_apply_style (GooCanvas *canvas,
+			cairo_t   *cr)
+{
+  GooCanvasStyle *style = canvas->style;
+
+  /* Set the default line width based on the current units setting. */
+  cairo_set_line_width (cr, goo_canvas_get_default_line_width (canvas));
+
+  if (!style)
+    return;
+
+  if (style->stroke_pattern)
+    cairo_set_source (cr, style->stroke_pattern);
+
+  cairo_set_operator (cr, style->op);
+  cairo_set_antialias (cr, style->antialias);
+  cairo_set_line_cap (cr, style->line_cap);
+  cairo_set_line_join (cr, style->line_join);
+  cairo_set_miter_limit (cr, style->line_join_miter_limit);
+
+  if (style->dash)
+    cairo_set_dash (cr, style->dash->dashes, style->dash->num_dashes,
+		    style->dash->dash_offset);
+}
+
+
 /**
  * goo_canvas_create_cairo_context:
  * @canvas: a #GooCanvas.
@@ -624,8 +660,7 @@ goo_canvas_create_cairo_context (GooCanvas *canvas)
      what is recommended when using unhinted text. */
   cairo_set_antialias (cr, CAIRO_ANTIALIAS_GRAY);
 
-  /* Set the default line width based on the current units setting. */
-  cairo_set_line_width (cr, goo_canvas_get_default_line_width (canvas));
+  goo_canvas_apply_style (canvas, cr);
 
   return cr;
 }
@@ -1571,6 +1606,10 @@ reconfigure_canvas (GooCanvas *canvas,
       if (redraw_if_needed)
 	gtk_widget_queue_draw (GTK_WIDGET (canvas));
     }
+
+  /* Update the stored window position. FIXME: Check over this. */
+  canvas->window_x = window_x;
+  canvas->window_y = window_y;
 }
 
 
@@ -2394,8 +2433,7 @@ goo_canvas_render (GooCanvas             *canvas,
   if (canvas->need_update)
     goo_canvas_update (canvas);
 
-  /* Set the default line width based on the current units setting. */
-  cairo_set_line_width (cr, goo_canvas_get_default_line_width (canvas));
+  goo_canvas_apply_style (canvas, cr);
 
   if (bounds)
     {
@@ -4098,3 +4136,25 @@ goo_canvas_check_font_size (GooCanvas            *canvas,
 
   pango_font_description_set_absolute_size (font_desc, size);
 }
+
+
+void
+goo_canvas_set_default_style	    (GooCanvas      *canvas,
+				     GooCanvasStyle *style)
+{
+  if (canvas->style)
+    g_object_unref (canvas->style);
+
+  canvas->style = style;
+  if (style)
+    g_object_ref (style);
+
+  if (GOO_IS_CANVAS_ITEM_SIMPLE (canvas->root_item))
+    goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (canvas->root_item),
+				    TRUE);
+  if (GOO_IS_CANVAS_ITEM_SIMPLE (canvas->static_root_item))
+    goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (canvas->static_root_item),
+				    TRUE);
+}
+
+
diff --git a/src/goocanvas.h b/src/goocanvas.h
index dac7ab0..afe4669 100644
--- a/src/goocanvas.h
+++ b/src/goocanvas.h
@@ -155,6 +155,9 @@ struct _GooCanvas
 
   /* The last window position, used for static items. */
   gint window_x, window_y;
+
+  /* The default style for all canvas items. */
+  GooCanvasStyle *style;
 };
 
 /**
@@ -236,6 +239,9 @@ void            goo_canvas_render	    (GooCanvas		   *canvas,
 					     const GooCanvasBounds *bounds,
 					     gdouble                scale);
 
+void            goo_canvas_set_default_style	    (GooCanvas      *canvas,
+						     GooCanvasStyle *style);
+
 void            goo_canvas_update_items_using_style (GooCanvas      *canvas,
 						     GooCanvasStyle *style,
 						     gboolean        recompute_bounds);



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