[cairo-commit] goocanvas/demo simple-demo.c,1.1,1.2

Damon Chaplin commit at pdx.freedesktop.org
Wed Aug 23 04:56:08 PDT 2006


Committed by: damon

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

Modified Files:
	simple-demo.c 
Log Message:
2006-08-23  Damon Chaplin  <damon at gnome.org>

	* src/goocanvasview.c: keep an item_to_view hash table so apps can
	get the view for particular items (e.g. as an easy alternative for
	setting up signal handlers).
	(goo_canvas_view_get_item_view): new function to get the item view for
	a given item.
	(goo_canvas_view_unregister_item_view): new function that implementors
	of GooCanvasItemView should call in their finalize method to unregister
	the view (so the GooCanvasView removes it from the hash table.)
	(goo_canvas_view_create_item_view): add the new item view to the
	item_to_view hash table.

	* src/goocanvasitemviewsimple.c (goo_canvas_item_view_simple_finalize):
	* src/goocanvasgroupview.c (goo_canvas_group_view_finalize): unregister
	the item view.
	(goo_canvas_group_view_set_parent_view): don't change the canvas view
	here any more, since we don't support that.

	* demo/simple-demo.c: updated to use simple signal handler setup.



Index: simple-demo.c
===================================================================
RCS file: /cvs/cairo/goocanvas/demo/simple-demo.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- simple-demo.c	16 Apr 2006 21:00:54 -0000	1.1
+++ simple-demo.c	23 Aug 2006 11:56:05 -0000	1.2
@@ -1,20 +1,15 @@
-#include <config.h>
 #include <stdlib.h>
 #include <goocanvas.h>
 
 
-static GooCanvasModelSimple* create_canvas_model (void);
-static void on_item_view_created (GooCanvasView     *view,
-				  GooCanvasItemView *item_view,
-				  GooCanvasItem     *item,
-				  gpointer           data);
 static gboolean on_rect_button_press (GooCanvasItemView *view,
 				      GooCanvasItemView *target,
 				      GdkEventButton    *event,
 				      gpointer           data);
-static gboolean on_delete_event  (GtkWidget         *window,
-				  GdkEvent          *event,
-				  gpointer           unused_data);
+
+static gboolean on_delete_event      (GtkWidget         *window,
+				      GdkEvent          *event,
+				      gpointer           unused_data);
 
 
 int
@@ -22,6 +17,8 @@
 {
   GtkWidget *window, *scrolled_win, *canvas;
   GooCanvasModelSimple *canvas_model;
+  GooCanvasItemView *item_view;
+  GooCanvasItem *root, *rect_item, *text_item;
 
   /* Initialize GTK+. */
   gtk_set_locale ();
@@ -46,66 +43,42 @@
   gtk_widget_show (canvas);
   gtk_container_add (GTK_CONTAINER (scrolled_win), canvas);
 
-  /* Connect to the "item-view-created" signal so we can connect signal
-     handlers to the item views. */
-  g_signal_connect (canvas, "item_view_created",
-		    (GtkSignalFunc) on_item_view_created,
-		    NULL);
-
   /* Create the canvas model */
-  canvas_model = create_canvas_model ();
-  goo_canvas_view_set_model (GOO_CANVAS_VIEW (canvas),
-			     GOO_CANVAS_MODEL (canvas_model));
-  g_object_unref (canvas_model);
-
-  /* Pass control to the GTK+ main event loop. */
-  gtk_main ();
-
-  return 0;
-}
-
-
-/* The creates & returns a simple canvas model with a few simple items. */
-static GooCanvasModelSimple*
-create_canvas_model (void)
-{
-  GooCanvasModelSimple *canvas_model;
-  GooCanvasItem *root, *item;
-
   canvas_model = goo_canvas_model_simple_new ();
 
   root = goo_canvas_model_get_root_item (GOO_CANVAS_MODEL (canvas_model));
 
   /* Add a few simple items. */
-  item = goo_canvas_rect_new (root, 100, 100, 400, 400,
-			      "line-width", 10.0,
-			      "radius-x", 20.0,
-			      "radius-y", 10.0,
-			      "stroke-color", "yellow",
-			      "fill-color", "red",
-			      NULL);
+  rect_item = goo_canvas_rect_new (root, 100, 100, 400, 400,
+				   "line-width", 10.0,
+				   "radius-x", 20.0,
+				   "radius-y", 10.0,
+				   "stroke-color", "yellow",
+				   "fill-color", "red",
+				   NULL);
 
-  item = goo_canvas_text_new (root, "Hello World", 300, 300, -1,
-			      GTK_ANCHOR_CENTER,
-			      "font", "Sans 24",
-			      NULL);
-  goo_canvas_item_rotate (item, 45, 300, 300);
+  text_item = goo_canvas_text_new (root, "Hello World", 300, 300, -1,
+				   GTK_ANCHOR_CENTER,
+				   "font", "Sans 24",
+				   NULL);
+  goo_canvas_item_rotate (text_item, 45, 300, 300);
 
-  return canvas_model;
-}
+  /* Set the model of the canvas view. This will create item views for all the
+     items in the model. */
+  goo_canvas_view_set_model (GOO_CANVAS_VIEW (canvas),
+			     GOO_CANVAS_MODEL (canvas_model));
+  g_object_unref (canvas_model);
 
+  /* Connect a signal handler for the item view of the rectangle item. */
+  item_view = goo_canvas_view_get_item_view (GOO_CANVAS_VIEW (canvas),
+					     rect_item);
+  g_signal_connect (item_view, "button_press_event",
+		    (GtkSignalFunc) on_rect_button_press, NULL);
 
-/* This is our handler for the "item-view-created" signal of the GooCanvasView.
-   We connect to the "button-press-event" signal of new rect views. */
-static void
-on_item_view_created (GooCanvasView     *view,
-		      GooCanvasItemView *item_view,
-		      GooCanvasItem     *item,
-		      gpointer           data)
-{
-  if (GOO_IS_CANVAS_RECT (item))
-    g_signal_connect (item_view, "button_press_event",
-		      (GtkSignalFunc) on_rect_button_press, NULL);
+  /* Pass control to the GTK+ main event loop. */
+  gtk_main ();
+
+  return 0;
 }
 
 



More information about the cairo-commit mailing list