[cairo-commit] goocanvas/src goocanvas.c, 1.20, 1.21 goocanvas.h, 1.12, 1.13

Damon Chaplin commit at pdx.freedesktop.org
Tue May 15 17:02:02 EEST 2007


Committed by: damon

Update of /cvs/cairo/goocanvas/src
In directory kemper:/tmp/cvs-serv28719/src

Modified Files:
	goocanvas.c goocanvas.h 
Log Message:
2007-05-15  Damon Chaplin  <damon at gnome.org>

	* src/goocanvas.c: added "scale-x" and "scale-y" properties to set
	the horizontal and vertical scale independently. Note that if items
	use the visibility threshold setting it will compare it to the minimum
	of scale_x and scale_y.

	* demo/demo.c: added "Scale X" and "Scale Y" spinbuttons.



Index: goocanvas.c
===================================================================
RCS file: /cvs/cairo/goocanvas/src/goocanvas.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- goocanvas.c	15 May 2007 11:07:12 -0000	1.20
+++ goocanvas.c	15 May 2007 14:01:51 -0000	1.21
@@ -110,6 +110,8 @@
   PROP_0,
 
   PROP_SCALE,
+  PROP_SCALE_X,
+  PROP_SCALE_Y,
   PROP_ANCHOR,
   PROP_X1,
   PROP_Y1,
@@ -183,6 +185,10 @@
 					    GtkCallback       callback,
 					    gpointer          callback_data);
 
+static void	goo_canvas_set_scale_internal (GooCanvas     *canvas,
+					       gdouble        scale_x,
+					       gdouble        scale_y);
+
 static void     set_item_pointer           (GooCanvasItem   **item,
 					    GooCanvasItem    *new_item);
 static void     update_pointer_item        (GooCanvas        *canvas,
@@ -248,6 +254,20 @@
 							0.0, G_MAXDOUBLE, 1.0,
 							G_PARAM_READWRITE));
 
+  g_object_class_install_property (gobject_class, PROP_SCALE_X,
+				   g_param_spec_double ("scale-x",
+							_("Scale X"),
+							_("The horizontal magnification factor of the canvas"),
+							0.0, G_MAXDOUBLE, 1.0,
+							G_PARAM_READWRITE));
+
+  g_object_class_install_property (gobject_class, PROP_SCALE_Y,
+				   g_param_spec_double ("scale-y",
+							_("Scale Y"),
+							_("The vertical magnification factor of the canvas"),
+							0.0, G_MAXDOUBLE, 1.0,
+							G_PARAM_READWRITE));
+
   g_object_class_install_property (gobject_class, PROP_ANCHOR,
 				   g_param_spec_enum ("anchor",
 						      _("Anchor"),
@@ -382,6 +402,8 @@
      Though developers can turn this off if not needed for efficiency. */
   GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
 
+  canvas->scale_x = 1.0;
+  canvas->scale_y = 1.0;
   canvas->scale = 1.0;
   canvas->need_update = TRUE;
   canvas->crossing_event.type = GDK_LEAVE_NOTIFY;
@@ -583,6 +605,12 @@
     case PROP_SCALE:
       g_value_set_double (value, canvas->scale);
       break;
+    case PROP_SCALE_X:
+      g_value_set_double (value, canvas->scale_x);
+      break;
+    case PROP_SCALE_Y:
+      g_value_set_double (value, canvas->scale_y);
+      break;
     case PROP_ANCHOR:
       g_value_set_enum (value, canvas->anchor);
       break;
@@ -631,6 +659,14 @@
     case PROP_SCALE:
       goo_canvas_set_scale (canvas, g_value_get_double (value));
       break;
+    case PROP_SCALE_X:
+      goo_canvas_set_scale_internal (canvas, g_value_get_double (value),
+				     canvas->scale_y);
+      break;
+    case PROP_SCALE_Y:
+      goo_canvas_set_scale_internal (canvas, canvas->scale_x,
+				     g_value_get_double (value));
+      break;
     case PROP_ANCHOR:
       canvas->anchor = g_value_get_enum (value);
       need_reconfigure = TRUE;
@@ -1075,8 +1111,8 @@
   gdk_window_set_user_data (widget->window, widget);
 
   /* We want to round the sizes up to the next pixel. */
-  width_pixels = ((canvas->bounds.x2 - canvas->bounds.x1) * canvas->scale_x) + 1;
-  height_pixels = ((canvas->bounds.y2 - canvas->bounds.y1) * canvas->scale_y) + 1;
+  width_pixels = ((canvas->bounds.x2 - canvas->bounds.x1) * canvas->device_to_pixels_x) + 1;
+  height_pixels = ((canvas->bounds.y2 - canvas->bounds.y1) * canvas->device_to_pixels_y) + 1;
 
   attributes.x = canvas->hadjustment ? - canvas->hadjustment->value : 0,
   attributes.y = canvas->vadjustment ? - canvas->vadjustment->value : 0;
@@ -1287,21 +1323,21 @@
   switch (canvas->units)
     {
     case GTK_UNIT_PIXEL:
-      canvas->scale_x = canvas->scale;
-      canvas->scale_y = canvas->scale;
+      canvas->device_to_pixels_x = canvas->scale_x;
+      canvas->device_to_pixels_y = canvas->scale_y;
       break;
     case GTK_UNIT_POINTS:
-      canvas->scale_x = canvas->scale * (canvas->resolution_x / 72.0);
-      canvas->scale_y = canvas->scale * (canvas->resolution_y / 72.0);
+      canvas->device_to_pixels_x = canvas->scale_x * (canvas->resolution_x / 72.0);
+      canvas->device_to_pixels_y = canvas->scale_y * (canvas->resolution_y / 72.0);
       break;
     case GTK_UNIT_INCH:
-      canvas->scale_x = canvas->scale * canvas->resolution_x;
-      canvas->scale_y = canvas->scale * canvas->resolution_y;
+      canvas->device_to_pixels_x = canvas->scale_x * canvas->resolution_x;
+      canvas->device_to_pixels_y = canvas->scale_y * canvas->resolution_y;
       break;
     case GTK_UNIT_MM:
       /* There are 25.4 mm to an inch. */
-      canvas->scale_x = canvas->scale * (canvas->resolution_x / 25.4);
-      canvas->scale_y = canvas->scale * (canvas->resolution_y / 25.4);
+      canvas->device_to_pixels_x = canvas->scale_x * (canvas->resolution_x / 25.4);
+      canvas->device_to_pixels_y = canvas->scale_y * (canvas->resolution_y / 25.4);
       break;
     }
 }
@@ -1327,13 +1363,13 @@
   if (canvas->bounds.y2 < canvas->bounds.y1)
     canvas->bounds.y2 = canvas->bounds.y1;
 
-  /* Recalculate scale_x & scale_y. */
+  /* Recalculate device_to_pixels_x & device_to_pixels_y. */
   recalculate_scales (canvas);
 
   /* This is the natural size of the canvas window in pixels, rounded up to
      the next pixel. */
-  width_pixels = ((canvas->bounds.x2 - canvas->bounds.x1) * canvas->scale_x) + 1;
-  height_pixels = ((canvas->bounds.y2 - canvas->bounds.y1) * canvas->scale_y) + 1;
+  width_pixels = ((canvas->bounds.x2 - canvas->bounds.x1) * canvas->device_to_pixels_x) + 1;
+  height_pixels = ((canvas->bounds.y2 - canvas->bounds.y1) * canvas->device_to_pixels_y) + 1;
 
   /* The actual window size is always at least as big as the widget's window.*/
   window_width = MAX (width_pixels, widget->allocation.width);
@@ -1777,20 +1813,10 @@
 }
 
 
-/**
- * goo_canvas_set_scale:
- * @canvas: a #GooCanvas.
- * @scale: the new scale setting.
- * 
- * Sets the scale of the canvas.
- *
- * The scale specifies the magnification factor of the canvas, e.g. if an item
- * has a width of 2 pixels and the scale is set to 3, it will be displayed with
- * a width of 2 x 3 = 6 pixels.
- **/
-void
-goo_canvas_set_scale	(GooCanvas *canvas,
-			 gdouble    scale)
+static void
+goo_canvas_set_scale_internal	(GooCanvas *canvas,
+				 gdouble    scale_x,
+				 gdouble    scale_y)
 {
   gdouble x, y;
 
@@ -1814,12 +1840,14 @@
 
   canvas->freeze_count++;
 
-  canvas->scale = scale;
+  canvas->scale_x = scale_x;
+  canvas->scale_y = scale_y;
+  canvas->scale = MIN (scale_x, scale_y);
   reconfigure_canvas (canvas, FALSE);
 
   /* Convert from the center point to the new desired top-left posision. */
-  x -= canvas->hadjustment->page_size / canvas->scale_x / 2;
-  y -= canvas->vadjustment->page_size / canvas->scale_y / 2;
+  x -= canvas->hadjustment->page_size / canvas->device_to_pixels_x / 2;
+  y -= canvas->vadjustment->page_size / canvas->device_to_pixels_y / 2;
 
   /* Now try to scroll to it. */
   goo_canvas_scroll_to (canvas, x, y);
@@ -1835,6 +1863,27 @@
 
 
 /**
+ * goo_canvas_set_scale:
+ * @canvas: a #GooCanvas.
+ * @scale: the new scale setting.
+ * 
+ * Sets the scale of the canvas.
+ *
+ * The scale specifies the magnification factor of the canvas, e.g. if an item
+ * has a width of 2 pixels and the scale is set to 3, it will be displayed with
+ * a width of 2 x 3 = 6 pixels.
+ **/
+void
+goo_canvas_set_scale	(GooCanvas *canvas,
+			 gdouble    scale)
+{
+  g_return_if_fail (GOO_IS_CANVAS (canvas));
+
+  goo_canvas_set_scale_internal (canvas, scale, scale);
+}
+
+
+/**
  * goo_canvas_unregister_item:
  * @canvas: a #GooCanvas.
  * @model: the item model whose canvas item is being finalized.
@@ -2008,14 +2057,14 @@
 
   /* We subtract one from the left & top edges, in case anti-aliasing makes
      the drawing use an extra pixel. */
-  rect.x = (double) (bounds->x1 - canvas->bounds.x1) * canvas->scale_x - 1;
-  rect.y = (double) (bounds->y1 - canvas->bounds.y1) * canvas->scale_y - 1;
+  rect.x = (double) (bounds->x1 - canvas->bounds.x1) * canvas->device_to_pixels_x - 1;
+  rect.y = (double) (bounds->y1 - canvas->bounds.y1) * canvas->device_to_pixels_y - 1;
 
   /* We add an extra one here for the same reason. (The other extra one is to
      round up to the next pixel.) And one for luck! */
-  rect.width = (double) (bounds->x2 - canvas->bounds.x1) * canvas->scale_x
+  rect.width = (double) (bounds->x2 - canvas->bounds.x1) * canvas->device_to_pixels_x
     - rect.x + 2 + 1;
-  rect.height = (double) (bounds->y2 - canvas->bounds.y1) * canvas->scale_y
+  rect.height = (double) (bounds->y2 - canvas->bounds.y1) * canvas->device_to_pixels_y
     - rect.y + 2 + 1;
 
   rect.x += canvas->canvas_x_offset;
@@ -2050,19 +2099,19 @@
   if (canvas->need_update)
     goo_canvas_update_internal (canvas, cr);
 
-  bounds.x1 = ((event->area.x - canvas->canvas_x_offset) / canvas->scale_x)
+  bounds.x1 = ((event->area.x - canvas->canvas_x_offset) / canvas->device_to_pixels_x)
     + canvas->bounds.x1;
-  bounds.y1 = ((event->area.y - canvas->canvas_y_offset) / canvas->scale_y)
+  bounds.y1 = ((event->area.y - canvas->canvas_y_offset) / canvas->device_to_pixels_y)
     + canvas->bounds.y1;
-  bounds.x2 = (event->area.width / canvas->scale_x) + bounds.x1;
-  bounds.y2 = (event->area.height / canvas->scale_y) + bounds.y1;
+  bounds.x2 = (event->area.width / canvas->device_to_pixels_x) + bounds.x1;
+  bounds.y2 = (event->area.height / canvas->device_to_pixels_y) + bounds.y1;
 
   /* Translate it to use the canvas pixel offsets (used when the canvas is
      smaller than the window and the anchor isn't set to NORTH_WEST). */
   cairo_translate (cr, canvas->canvas_x_offset, canvas->canvas_y_offset);
 
   /* Scale it so we can use canvas coordinates. */
-  cairo_scale (cr, canvas->scale_x, canvas->scale_y);
+  cairo_scale (cr, canvas->device_to_pixels_x, canvas->device_to_pixels_y);
 
   /* Translate it so the top-left of the canvas becomes (0,0). */
   cairo_translate (cr, -canvas->bounds.x1, -canvas->bounds.y1);
@@ -2885,8 +2934,8 @@
 			      gdouble       *x,
 			      gdouble       *y)
 {
-  *x = ((*x - canvas->bounds.x1) * canvas->scale_x) + canvas->canvas_x_offset;
-  *y = ((*y - canvas->bounds.y1) * canvas->scale_y) + canvas->canvas_y_offset;
+  *x = ((*x - canvas->bounds.x1) * canvas->device_to_pixels_x) + canvas->canvas_x_offset;
+  *y = ((*y - canvas->bounds.y1) * canvas->device_to_pixels_y) + canvas->canvas_y_offset;
 }
 
 
@@ -2911,8 +2960,8 @@
 				gdouble       *x,
 				gdouble       *y)
 {
-  *x = ((*x - canvas->canvas_x_offset) / canvas->scale_x) + canvas->bounds.x1;
-  *y = ((*y - canvas->canvas_y_offset) / canvas->scale_y) + canvas->bounds.y1;
+  *x = ((*x - canvas->canvas_x_offset) / canvas->device_to_pixels_x) + canvas->bounds.x1;
+  *y = ((*y - canvas->canvas_y_offset) / canvas->device_to_pixels_y) + canvas->bounds.y1;
 }
 
 

Index: goocanvas.h
===================================================================
RCS file: /cvs/cairo/goocanvas/src/goocanvas.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- goocanvas.h	30 Mar 2007 11:40:28 -0000	1.12
+++ goocanvas.h	15 May 2007 14:01:51 -0000	1.13
@@ -50,7 +50,11 @@
   /* The bounds of the canvas, in canvas units (not pixels). */
   GooCanvasBounds bounds;
 
-  /* The scale/zoom factor of the canvas, in pixels per device unit. */
+  /* The scale/zoom factors of the canvas. */
+  gdouble scale_x, scale_y;
+
+  /* The minimum of scale_x and scale_y, to compare with items' visibility
+     thresholds. */
   gdouble scale;
 
   /* Where the canvas is anchored (where it is displayed when it is smaller
@@ -120,7 +124,7 @@
 
   /* The multiplers to convert from device units to pixels, taking into account
      the canvas scale, the units setting and the display resolution. */
-  gdouble scale_x, scale_y;
+  gdouble device_to_pixels_x, device_to_pixels_y;
 
   /* The list of child widgets (using GooCanvasWidget items). */
   GList *widget_items;



More information about the cairo-commit mailing list