[goocanvas/goocanvas-1.0] Added "alpha" property to GooCanvasImage.



commit c8ffb25d17692519c9cbaa1a7f5fe173665127fc
Author: Damon Chaplin <damon gnome org>
Date:   Mon Jan 10 00:17:37 2011 +0000

    Added "alpha" property to GooCanvasImage.
    
    2011-01-10  Damon Chaplin  <damon gnome org>
    
    	    * src/goocanvasimage.c: added "alpha" property. Patch from xapantu
    	    plus a few changes.
    
    	    * demo/mv-demo.c:
    	    * demo/demo.c: added test for the above for the flowers.
    
    	    * demo/*: don't set page size for spin button adjustments since that
    	    seems to be deprecated and outputs warnings.

 ChangeLog               |   11 ++++++++++
 demo/demo-large-items.c |    2 +-
 demo/demo.c             |   18 +++++++++-------
 demo/mv-demo.c          |   14 +++++++-----
 demo/units-demo.c       |    2 +-
 src/goocanvasimage.c    |   52 +++++++++++++++++++++++++++++++++++++++-------
 6 files changed, 75 insertions(+), 24 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9b1c2b0..3ee2515 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2011-01-10  Damon Chaplin  <damon gnome org>
+
+	* src/goocanvasimage.c: added "alpha" property. Patch from xapantu
+	plus a few changes.
+
+	* demo/mv-demo.c:
+	* demo/demo.c: added test for the above for the flowers.
+
+	* demo/*: don't set page size for spin button adjustments since that
+	seems to be deprecated and outputs warnings.
+
 2010-07-27  Damon Chaplin  <damon gnome org>
 
 	* src/goocanvas.c (goo_canvas_adjustment_value_changed): fixed call
diff --git a/demo/demo-large-items.c b/demo/demo-large-items.c
index 7e5c67d..bcee3d4 100644
--- a/demo/demo-large-items.c
+++ b/demo/demo-large-items.c
@@ -143,7 +143,7 @@ create_large_items_page (void)
   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
   gtk_widget_show (w);
 
-  adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 10.00, 0.05, 0.50, 0.50));
+  adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 10.00, 0.05, 0.50, 0.0));
   w = gtk_spin_button_new (adj, 0.0, 2);
   g_signal_connect (adj, "value_changed",
 		    G_CALLBACK (zoom_changed),
diff --git a/demo/demo.c b/demo/demo.c
index 2c44e18..65b8b38 100644
--- a/demo/demo.c
+++ b/demo/demo.c
@@ -504,7 +504,7 @@ create_canvas_primitives ()
 	gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
 	gtk_widget_show (w);
 
-	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.50));
+	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.0));
 	w = gtk_spin_button_new (adj, 0.0, 2);
 	g_signal_connect (adj, "value_changed",
 			  G_CALLBACK (zoom_changed),
@@ -518,7 +518,7 @@ create_canvas_primitives ()
 	gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
 	gtk_widget_show (w);
 
-	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.50));
+	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.0));
 	w = gtk_spin_button_new (adj, 0.0, 2);
 	g_signal_connect (adj, "value_changed",
 			  G_CALLBACK (zoom_x_changed),
@@ -532,7 +532,7 @@ create_canvas_primitives ()
 	gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
 	gtk_widget_show (w);
 
-	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.50));
+	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.0));
 	w = gtk_spin_button_new (adj, 0.0, 2);
 	g_signal_connect (adj, "value_changed",
 			  G_CALLBACK (zoom_y_changed),
@@ -1305,7 +1305,8 @@ setup_invisible_texts (GooCanvasItem *root)
 
 
 static void
-plant_flower (GooCanvasItem *root, double x, double y, GtkAnchorType anchor)
+plant_flower (GooCanvasItem *root, double x, double y, GtkAnchorType anchor,
+	      gdouble opacity)
 {
   cairo_pattern_t *pattern;
   cairo_surface_t *surface;
@@ -1324,6 +1325,7 @@ plant_flower (GooCanvasItem *root, double x, double y, GtkAnchorType anchor)
 			       "width", w * 1.5,
 			       "height", h * 2,
 				"scale-to-fit", TRUE,
+				"alpha", opacity,
 			       NULL);
   cairo_pattern_destroy (pattern);
   setup_item_signals (image);
@@ -1352,10 +1354,10 @@ setup_images (GooCanvasItem *root)
   else
     g_warning ("Could not find the toroid.png sample file");
 
-  plant_flower (root,  20.0, 170.0, GTK_ANCHOR_NW);
-  plant_flower (root, 180.0, 170.0, GTK_ANCHOR_NE);
-  plant_flower (root,  20.0, 280.0, GTK_ANCHOR_SW);
-  plant_flower (root, 180.0, 280.0, GTK_ANCHOR_SE);
+  plant_flower (root,  20.0, 170.0, GTK_ANCHOR_NW, 0.3);
+  plant_flower (root, 180.0, 170.0, GTK_ANCHOR_NE, 0.5);
+  plant_flower (root,  20.0, 280.0, GTK_ANCHOR_SW, 0.7);
+  plant_flower (root, 180.0, 280.0, GTK_ANCHOR_SE, 1.0);
 }
 
 
diff --git a/demo/mv-demo.c b/demo/mv-demo.c
index ddb6472..0981e36 100644
--- a/demo/mv-demo.c
+++ b/demo/mv-demo.c
@@ -431,7 +431,7 @@ create_canvas_primitives (GooCanvasItemModel *model,
 	gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
 	gtk_widget_show (w);
 
-	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.50));
+	adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 50.00, 0.05, 0.50, 0.0));
 	w = gtk_spin_button_new (adj, 0.0, 2);
 	g_signal_connect (adj, "value_changed",
 			  G_CALLBACK (zoom_changed),
@@ -1173,7 +1173,8 @@ setup_invisible_texts (GooCanvasItemModel *root)
 static void
 plant_flower (GooCanvasItemModel *root,
 	      double x, double y,
-	      GtkAnchorType anchor)
+	      GtkAnchorType anchor,
+	      double opacity)
 {
   cairo_pattern_t *pattern;
   cairo_surface_t *surface;
@@ -1192,6 +1193,7 @@ plant_flower (GooCanvasItemModel *root,
 				      "width", w * 1.5,
 				      "height", h * 2,
 				      "scale-to-fit", TRUE,
+				      "alpha", opacity,
 				      NULL);
   cairo_pattern_destroy (pattern);
 }
@@ -1218,10 +1220,10 @@ setup_images (GooCanvasItemModel *root)
   else
     g_warning ("Could not find the toroid.png sample file");
 
-  plant_flower (root,  20.0, 170.0, GTK_ANCHOR_NW);
-  plant_flower (root, 180.0, 170.0, GTK_ANCHOR_NE);
-  plant_flower (root,  20.0, 280.0, GTK_ANCHOR_SW);
-  plant_flower (root, 180.0, 280.0, GTK_ANCHOR_SE);
+  plant_flower (root,  20.0, 170.0, GTK_ANCHOR_NW, 0.3);
+  plant_flower (root, 180.0, 170.0, GTK_ANCHOR_NE, 0.5);
+  plant_flower (root,  20.0, 280.0, GTK_ANCHOR_SW, 0.7);
+  plant_flower (root, 180.0, 280.0, GTK_ANCHOR_SE, 1.0);
 }
 
 
diff --git a/demo/units-demo.c b/demo/units-demo.c
index 4a28e8b..0f2a853 100644
--- a/demo/units-demo.c
+++ b/demo/units-demo.c
@@ -114,7 +114,7 @@ create_canvas (GtkUnit         units,
   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
   gtk_widget_show (w);
 
-  adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 100.00, 0.05, 0.50, 0.50));
+  adj = GTK_ADJUSTMENT (gtk_adjustment_new (1.00, 0.05, 100.00, 0.05, 0.50, 0.0));
   w = gtk_spin_button_new (adj, 0.0, 2);
   g_signal_connect (adj, "value_changed",
 		    G_CALLBACK (zoom_changed),
diff --git a/src/goocanvasimage.c b/src/goocanvasimage.c
index a91ae62..5ecc7db 100644
--- a/src/goocanvasimage.c
+++ b/src/goocanvasimage.c
@@ -36,6 +36,7 @@
 typedef struct _GooCanvasImagePrivate GooCanvasImagePrivate;
 struct _GooCanvasImagePrivate {
   gboolean scale_to_fit;
+  gdouble alpha;
 };
 
 #define GOO_CANVAS_IMAGE_GET_PRIVATE(image)  \
@@ -53,6 +54,7 @@ enum {
   PROP_WIDTH,
   PROP_HEIGHT,
   PROP_SCALE_TO_FIT,
+  PROP_ALPHA,
 
   /* Convenience properties. */
   PROP_PIXBUF
@@ -123,6 +125,13 @@ goo_canvas_image_install_common_properties (GObjectClass *gobject_class)
 							 FALSE,
 							 G_PARAM_READWRITE));
 
+  g_object_class_install_property (gobject_class, PROP_ALPHA,
+				   g_param_spec_double ("alpha",
+							_("Alpha"),
+							_("The opacity of the image, 0.0 is fully transparent, and 1.0 is opaque."),
+							0.0, 1.0, 1.0,
+							G_PARAM_READWRITE));
+
   g_object_class_install_property (gobject_class, PROP_PIXBUF,
 				   g_param_spec_object ("pixbuf",
 							_("Pixbuf"),
@@ -156,7 +165,11 @@ goo_canvas_image_get_private (gpointer object)
 static void
 goo_canvas_image_init (GooCanvasImage *image)
 {
+  GooCanvasImagePrivate *priv = GOO_CANVAS_IMAGE_GET_PRIVATE (image);
+
   image->image_data = g_slice_new0 (GooCanvasImageData);
+
+  priv->alpha = 1.0;
 }
 
 
@@ -288,6 +301,9 @@ goo_canvas_image_get_common_property (GObject              *object,
     case PROP_SCALE_TO_FIT:
       g_value_set_boolean (value, priv->scale_to_fit);
       break;
+    case PROP_ALPHA:
+      g_value_set_double (value, priv->alpha);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -309,7 +325,7 @@ goo_canvas_image_get_property (GObject              *object,
 }
 
 
-static void
+static gboolean
 goo_canvas_image_set_common_property (GObject              *object,
 				      GooCanvasImageData   *image_data,
 				      guint                 prop_id,
@@ -318,6 +334,7 @@ goo_canvas_image_set_common_property (GObject              *object,
 {
   GooCanvasImagePrivate *priv = goo_canvas_image_get_private (object);
   GdkPixbuf *pixbuf;
+  gboolean recompute_bounds = TRUE;
 
   switch (prop_id)
     {
@@ -348,10 +365,16 @@ goo_canvas_image_set_common_property (GObject              *object,
       image_data->width = pixbuf ? gdk_pixbuf_get_width (pixbuf) : 0;
       image_data->height = pixbuf ? gdk_pixbuf_get_height (pixbuf) : 0;
       break;
+    case PROP_ALPHA:
+      priv->alpha = g_value_get_double (value);
+      recompute_bounds = FALSE;
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
   }
+
+  return recompute_bounds;
 }
 
 
@@ -363,6 +386,7 @@ goo_canvas_image_set_property (GObject              *object,
 {
   GooCanvasItemSimple *simple = (GooCanvasItemSimple*) object;
   GooCanvasImage *image = (GooCanvasImage*) object;
+  gboolean recompute_bounds;
 
   if (simple->model)
     {
@@ -370,9 +394,11 @@ goo_canvas_image_set_property (GObject              *object,
       return;
     }
 
-  goo_canvas_image_set_common_property (object, image->image_data, prop_id,
-					value, pspec);
-  goo_canvas_item_simple_changed (simple, TRUE);
+  recompute_bounds = goo_canvas_image_set_common_property (object,
+							   image->image_data,
+							   prop_id,
+							   value, pspec);
+  goo_canvas_item_simple_changed (simple, recompute_bounds);
 }
 
 
@@ -445,7 +471,12 @@ goo_canvas_image_paint (GooCanvasItemSimple   *simple,
   cairo_set_source (cr, image_data->pattern);
   cairo_rectangle (cr, image_data->x, image_data->y,
 		   image_data->width, image_data->height);
-  cairo_fill (cr);
+  /* To have better performance, we don't use cairo_paint_with_alpha if
+   * the image is not transparent at all. */
+  if (priv->alpha != 1.0)
+    cairo_paint_with_alpha (cr, priv->alpha);
+  else
+    cairo_fill (cr);
 #else
   /* Using cairo_paint() used to be much slower than cairo_fill(), though
      they seem similar now. I'm not sure if it matters which we use. */
@@ -571,7 +602,9 @@ goo_canvas_image_model_class_init (GooCanvasImageModelClass *klass)
 static void
 goo_canvas_image_model_init (GooCanvasImageModel *emodel)
 {
+  GooCanvasImagePrivate *priv = GOO_CANVAS_IMAGE_MODEL_GET_PRIVATE (emodel);
 
+  priv->alpha = 1.0;
 }
 
 
@@ -674,10 +707,13 @@ goo_canvas_image_model_set_property (GObject              *object,
 				     GParamSpec           *pspec)
 {
   GooCanvasImageModel *imodel = (GooCanvasImageModel*) object;
+  gboolean recompute_bounds;
 
-  goo_canvas_image_set_common_property (object, &imodel->image_data, prop_id,
-					value, pspec);
-  g_signal_emit_by_name (imodel, "changed", TRUE);
+  recompute_bounds = goo_canvas_image_set_common_property (object,
+							   &imodel->image_data,
+							   prop_id,
+							   value, pspec);
+  g_signal_emit_by_name (imodel, "changed", recompute_bounds);
 }
 
 



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