[cairo-commit] goocanvas/demo Makefile.am, 1.4, 1.5 scalability-demo.c, 1.2, 1.3 units-demo.c, NONE, 1.1

Damon Chaplin commit at pdx.freedesktop.org
Thu Aug 31 08:45:11 PDT 2006


Committed by: damon

Update of /cvs/cairo/goocanvas/demo
In directory kemper:/tmp/cvs-serv3560/demo

Modified Files:
	Makefile.am scalability-demo.c 
Added Files:
	units-demo.c 
Log Message:
2006-08-31  Damon Chaplin  <damon at gnome.org>

	* src/goocanvasview.c: added "units", "resolution-x" & "resolution-y"
	properties so we can support points, inches and millimeters being used
	for item units as well as just pixels. Thus you can create a complete
	WYSIWYG printable document. But note that you must use absolute units
	for font sizes, i.e. use "px" after the size in any font description
	strings, e.g. "Sans 10px".  That stops us scaling fonts twice.
	(goo_canvas_view_set_default_line_width): set a reasonable default
	line width according to the current units.

	* demo/units-demo.c: new demo showing different units in use.



Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/goocanvas/demo/Makefile.am,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Makefile.am	27 Aug 2006 12:20:40 -0000	1.4
+++ Makefile.am	31 Aug 2006 15:45:09 -0000	1.5
@@ -7,7 +7,7 @@
 	-I$(top_srcdir)/src \
 	@PACKAGE_CFLAGS@
 
-noinst_PROGRAMS = demo simple-demo scalability-demo
+noinst_PROGRAMS = demo simple-demo scalability-demo units-demo
 
 demo_SOURCES = \
 	demo.c demo-fifteen.c demo-scalability.c demo-grabs.c \
@@ -26,5 +26,10 @@
 
 scalability_demo_LDADD = $(top_builddir)/src/libgoocanvas.la @PACKAGE_LIBS@ $(INTLLIBS)
 
+units_demo_SOURCES = \
+	units-demo.c
+
+units_demo_LDADD = $(top_builddir)/src/libgoocanvas.la @PACKAGE_LIBS@ $(INTLLIBS)
+
 EXTRA_DIST = flower.png toroid.png
 

Index: scalability-demo.c
===================================================================
RCS file: /cvs/cairo/goocanvas/demo/scalability-demo.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- scalability-demo.c	30 Aug 2006 12:18:47 -0000	1.2
+++ scalability-demo.c	31 Aug 2006 15:45:09 -0000	1.3
@@ -1,10 +1,20 @@
 #include <stdlib.h>
 #include <goocanvas.h>
 
+#if 0
+#define N_GROUP_COLS 5
+#define N_GROUP_ROWS 5
+#else
 #define N_GROUP_COLS 25
 #define N_GROUP_ROWS 20
+#endif
 #define N_COLS 10
 #define N_ROWS 10
+#define N_TOTAL_ID_ITEMS (N_GROUP_COLS * N_GROUP_ROWS) * (N_COLS * N_ROWS)
+
+/* The maximum length of a string identifying an item (i.e. its coords). */
+#define MAX_ID_LEN 20
+
 #define PADDING 10
 
 #if 0
@@ -17,7 +27,7 @@
 
 double total_width, total_height;
 double left_offset, top_offset;
-
+char ids[N_TOTAL_ID_ITEMS][MAX_ID_LEN];
 
 static gboolean
 on_motion_notify (GooCanvasItemView *view,
@@ -59,7 +69,9 @@
   double item_width, item_height;
   double cell_width, cell_height;
   double group_width, group_height;
-  int total_items = 0;
+  int total_items = 0, id_item_num = 0;;
+  GooCanvasStyle *style, *style2;
+  GdkColor color = { 0, 0, 0, 0, };
 
   canvas_model = goo_canvas_model_simple_new ();
   root = goo_canvas_model_get_root_item (GOO_CANVAS_MODEL (canvas_model));
@@ -88,6 +100,18 @@
   left_offset = -total_width / 2;
   top_offset = -total_height / 2;
 
+  style = goo_canvas_style_new ();
+  style->mask |= GOO_CANVAS_STYLE_FILL_PATTERN;
+  gdk_color_parse ("mediumseagreen", &color);
+  style->fill_pattern = cairo_pattern_create_rgb
+	(color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
+
+  style2 = goo_canvas_style_new ();
+  style2->mask |= GOO_CANVAS_STYLE_FILL_PATTERN;
+  gdk_color_parse ("steelblue", &color);
+  style2->fill_pattern = cairo_pattern_create_rgb
+	(color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
+
   for (group_i = 0; group_i < N_GROUP_COLS; group_i++)
     {
       for (group_j = 0; group_j < N_GROUP_ROWS; group_j++)
@@ -108,9 +132,8 @@
 		  double rotation = i % 10 * 2;
 		  double rotation_x = item_x + item_width / 2;
 		  double rotation_y = item_y + item_height / 2;
-		  char buffer[256];
 
-		  sprintf (buffer, "%g, %g",
+		  sprintf (ids[id_item_num], "%g, %g",
 			   group_x + item_x, group_y + item_y);
 
 #ifdef USE_PIXMAP
@@ -119,26 +142,27 @@
 					       "width", item_width,
 					       "height", item_height,
 					       NULL);
-		  total_items++;
 #else
 		  item = goo_canvas_rect_new (group, item_x, item_y,
 					      item_width, item_height,
-					      "fill-color", (j % 2) ? "mediumseagreen" : "steelblue",
 					      NULL);
-		  total_items++;
+		  goo_canvas_item_simple_set_style ((GooCanvasItemSimple*) item, (j % 2) ? style : style2);
 		  goo_canvas_item_rotate (item, rotation, rotation_x, rotation_y);
 #endif
-		  g_object_set_data (G_OBJECT (item), "id", g_strdup (buffer));
+		  g_object_set_data (G_OBJECT (item), "id",
+				     ids[id_item_num]);
 
-		  item = goo_canvas_text_new (group, buffer,
+		  item = goo_canvas_text_new (group, ids[id_item_num],
 					      item_x + item_width / 2,
 					      item_y + item_height / 2,
 					      -1, GTK_ANCHOR_CENTER,
 					      "font", "Sans 8",
 					      NULL);
-		  total_items++;
-		  goo_canvas_item_rotate (item, rotation, rotation_x, rotation_y);
+		  goo_canvas_item_rotate (item, rotation, rotation_x,
+					  rotation_y);
 
+		  id_item_num++;
+		  total_items += 2;
 
 #ifdef ONLY_ONE
 		  break;

--- NEW FILE: units-demo.c ---
#include <stdlib.h>
#include <goocanvas.h>


static gboolean
on_motion_notify (GooCanvasItemView *view,
		  GooCanvasItemView *target,
		  GdkEventMotion *event,
		  gpointer data)
{
  GooCanvasItem *item = goo_canvas_item_view_get_item (target);
  gchar *id = g_object_get_data (G_OBJECT (item), "id");

  g_print ("%s item received 'motion-notify' signal\n", id ? id : "Unknown");

  return FALSE;
}


static void
on_item_view_created (GooCanvasView     *view,
		      GooCanvasItemView *item_view,
		      GooCanvasItem     *item,
		      gpointer           data)
{
  if (GOO_IS_CANVAS_RECT (item) || GOO_IS_CANVAS_IMAGE (item))
    {
      g_signal_connect (item_view, "motion_notify_event",
			(GtkSignalFunc) on_motion_notify, NULL);
    }
}


GooCanvasModel*
create_model (GtkUnit    units,
	      gchar     *units_name)
{
  GooCanvasModelSimple *canvas_model;
  GooCanvasItem *root, *item;
  gchar buffer[256], font_desc[64];
  double *d;
  double data[4][8] = {
    /* Pixels */
    { 100, 100, 200, 20, 10,
      200, 310, 24 },

    /* Points */
    { 100, 100, 200, 20, 10,
      200, 310, 24 },

    /* Inches */
    { 1, 1, 3, 0.5, 0.16,
      3, 4, 0.3 },

    /* MM */
    { 30, 30, 100, 10, 5,
      80, 60, 10 }
  };

  d = data[units];

  canvas_model = goo_canvas_model_simple_new ();
  root = goo_canvas_model_get_root_item (GOO_CANVAS_MODEL (canvas_model));

  item = goo_canvas_rect_new (root, d[0], d[1], d[2], d[3],
			      NULL);

  sprintf (buffer, "This box is %gx%g %s", d[2], d[3], units_name);
  sprintf (font_desc, "Sans %gpx", d[4]);
  item = goo_canvas_text_new (root, buffer, d[0] + d[2] / 2, d[1] + d[3] / 2,
			      -1, GTK_ANCHOR_CENTER,
			      "font", font_desc,
			      NULL);


  sprintf (buffer, "This font is %g %s high", d[7], units_name);
  sprintf (font_desc, "Sans %gpx", d[7]);
  item = goo_canvas_text_new (root, buffer, d[5], d[6], -1,
			      GTK_ANCHOR_CENTER,
			      "font", font_desc,
			      NULL);

  return (GooCanvasModel*) canvas_model;
}


static void
zoom_changed (GtkAdjustment *adj, GooCanvasView *canvas_view)
{
  goo_canvas_view_set_scale (canvas_view, adj->value);
}


GtkWidget *
create_canvas (GtkUnit         units,
	       gchar          *units_name)
{
  GtkWidget *vbox, *hbox, *w, *scrolled_win, *canvas;
  GooCanvasModel *canvas_model;
  GtkAdjustment *adj;

  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  gtk_widget_show (vbox);

  hbox = gtk_hbox_new (FALSE, 4);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  canvas = goo_canvas_view_new ();

  w = gtk_label_new ("Zoom:");
  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));
  w = gtk_spin_button_new (adj, 0.0, 2);
  g_signal_connect (adj, "value_changed",
		    (GtkSignalFunc) zoom_changed,
		    canvas);
  gtk_widget_set_size_request (w, 50, -1);
  gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
  gtk_widget_show (w);

  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
  gtk_widget_show (scrolled_win);

  /* Create the canvas. */
  gtk_widget_set_size_request (canvas, 600, 450);
  g_signal_connect (canvas, "item_view_created",
		    (GtkSignalFunc) on_item_view_created,
		    NULL);

  canvas_model = create_model (units, units_name);
  goo_canvas_view_set_model (GOO_CANVAS_VIEW (canvas), canvas_model);
  g_object_unref (canvas_model);

  goo_canvas_view_set_bounds (GOO_CANVAS_VIEW (canvas),
			      0, 0, 1000, 1000);
  g_object_set (canvas,
		"units", units,
		"anchor", GTK_ANCHOR_CENTER,
		NULL);

  gtk_widget_show (canvas);
  gtk_container_add (GTK_CONTAINER (scrolled_win), canvas);

  return vbox;
}


static gboolean
on_delete_event (GtkWidget *window,
		 GdkEvent  *event,
		 gpointer   unused_data)
{
  exit (0);
}


int
main (int argc, char *argv[])
{
  GtkWidget *window, *notebook;

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 600);
  gtk_widget_show (window);
  g_signal_connect (window, "delete_event", (GtkSignalFunc) on_delete_event,
		    NULL);

  notebook = gtk_notebook_new ();
  gtk_widget_show (notebook);
  gtk_container_add (GTK_CONTAINER (window), notebook);

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
			    create_canvas (GTK_UNIT_PIXEL, "pixels"),
			    gtk_label_new ("Pixels"));

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
			    create_canvas (GTK_UNIT_POINTS, "points"),
			    gtk_label_new ("Points"));

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
			    create_canvas (GTK_UNIT_INCH, "inches"),
			    gtk_label_new ("Inches"));

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
			    create_canvas (GTK_UNIT_MM, "millimeters"),
			    gtk_label_new ("Millimeters"));

  gtk_main ();

  return 0;
}





More information about the cairo-commit mailing list