[cairo-commit] goocanvas/src goocanvasgroupview.c, 1.18,
1.19 goocanvasitemviewsimple.c, 1.10, 1.11 goocanvasview.c,
1.27, 1.28 goocanvasview.h, 1.9, 1.10
Damon Chaplin
commit at pdx.freedesktop.org
Wed Aug 23 04:56:08 PDT 2006
Committed by: damon
Update of /cvs/cairo/goocanvas/src
In directory kemper:/tmp/cvs-serv32537/src
Modified Files:
goocanvasgroupview.c goocanvasitemviewsimple.c goocanvasview.c
goocanvasview.h
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: goocanvasgroupview.c
===================================================================
RCS file: /cvs/cairo/goocanvas/src/goocanvasgroupview.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- goocanvasgroupview.c 8 Aug 2006 21:58:13 -0000 1.18
+++ goocanvasgroupview.c 23 Aug 2006 11:56:05 -0000 1.19
@@ -228,6 +228,10 @@
g_ptr_array_free (group_view->item_views, TRUE);
+ /* Remove the view from the GooCanvasView hash table. */
+ goo_canvas_view_unregister_item_view (group_view->canvas_view,
+ group_view->group);
+
/* Unref the group. */
g_object_unref (group_view->group);
group_view->group = NULL;
@@ -491,10 +495,6 @@
{
GooCanvasGroupView *group_view = (GooCanvasGroupView*) view;
group_view->parent_view = parent_view;
- if (parent_view)
- group_view->canvas_view = goo_canvas_item_view_get_canvas_view (parent_view);
- else
- group_view->canvas_view = NULL;
}
Index: goocanvasitemviewsimple.c
===================================================================
RCS file: /cvs/cairo/goocanvas/src/goocanvasitemviewsimple.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- goocanvasitemviewsimple.c 8 Aug 2006 21:58:13 -0000 1.10
+++ goocanvasitemviewsimple.c 23 Aug 2006 11:56:05 -0000 1.11
@@ -83,6 +83,10 @@
{
GooCanvasItemViewSimple *simple_view = (GooCanvasItemViewSimple*) object;
+ /* Remove the view from the GooCanvasView hash table. */
+ goo_canvas_view_unregister_item_view (simple_view->canvas_view,
+ (GooCanvasItem*) simple_view->item);
+
/* Unref the item. */
g_object_unref (simple_view->item);
simple_view->item = NULL;
Index: goocanvasview.c
===================================================================
RCS file: /cvs/cairo/goocanvas/src/goocanvasview.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- goocanvasview.c 22 Aug 2006 12:39:09 -0000 1.27
+++ goocanvasview.c 23 Aug 2006 11:56:05 -0000 1.28
@@ -18,14 +18,21 @@
* <informalexample><programlisting>
* #include <goocanvas.h>
*
+ * static gboolean on_rect_button_press (GooCanvasItemView *view,
+ * GooCanvasItemView *target,
+ * GdkEventButton *event,
+ * gpointer data);
+ *
* int
* main (int argc, char *argv[])
* {
* GtkWidget *window, *scrolled_win, *canvas;
* GooCanvasModelSimple *canvas_model;
+ * GooCanvasItemView *item_view;
+ * GooCanvasItem *root, *rect_item, *text_item;
*
* /* Initialize GTK+. */
- * gtk_set_locale ();
+ * gtk_set_locale ();
* gtk_init (&argc, &argv);
*
* /* Create the window and widgets. */
@@ -45,64 +52,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);
- *
- * 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);
*
+ * /* Pass control to the GTK+ main event loop. */
+ * gtk_main ();
*
- * /* 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);
+ * return 0;
* }
*
*
@@ -117,6 +102,7 @@
* g_print ("rect item received button press event\n");
* return TRUE;
* }
+ *
* </programlisting></informalexample>
*
*/
@@ -367,6 +353,8 @@
g_object_ref_sink (canvas_view->hadjustment);
g_object_ref_sink (canvas_view->vadjustment);
+
+ canvas_view->item_to_view = g_hash_table_new (g_direct_hash, g_direct_equal);
}
@@ -408,6 +396,8 @@
g_object_unref (view->hadjustment);
g_object_unref (view->vadjustment);
+ g_hash_table_destroy (view->item_to_view);
+
G_OBJECT_CLASS (goo_canvas_view_parent_class)->finalize (object);
}
@@ -590,6 +580,32 @@
/**
+ * goo_canvas_view_get_item_view:
+ * @view: a #GooCanvasView.
+ * @item: a #GooCanvasItem.
+ *
+ * Gets the view for the given #GooCanvasItem.
+ *
+ * Returns: the view for the given #GooCanvasItem, or %NULL if no view has been
+ * created for it yet.
+ **/
+GooCanvasItemView *
+goo_canvas_view_get_item_view (GooCanvasView *view,
+ GooCanvasItem *item)
+{
+ GooCanvasItemView *item_view;
+
+ item_view = g_hash_table_lookup (view->item_to_view, item);
+
+ /* If the item has a view check it is valid. */
+ g_return_val_if_fail (!item_view || GOO_IS_CANVAS_ITEM_VIEW (item_view),
+ NULL);
+
+ return item_view;
+}
+
+
+/**
* goo_canvas_view_get_item_view_at:
* @view: a #GooCanvasView.
* @x: the x coordinate of the point.
@@ -1282,6 +1298,22 @@
/**
+ * goo_canvas_view_unregister_item_view:
+ * @view: a #GooCanvasView.
+ * @item: the item whose view is being finalized.
+ *
+ * This function should be called in the finalize method of #GooCanvasItemView
+ * objects, to remove the item view from the #GooCanvasView's hash table.
+ **/
+void
+goo_canvas_view_unregister_item_view (GooCanvasView *view,
+ GooCanvasItem *item)
+{
+ g_hash_table_remove (view->item_to_view, item);
+}
+
+
+/**
* goo_canvas_view_create_item_view:
* @view: a #GooCanvasView.
* @item: the item to create a view for.
@@ -1309,11 +1341,13 @@
if (GOO_CANVAS_VIEW_GET_CLASS (view)->create_item_view)
item_view = GOO_CANVAS_VIEW_GET_CLASS (view)->create_item_view (view, item, parent_view);
- /* The virtial method can return NULL to use the default view for an item. */
+ /* The virtual method can return NULL to use the default view for an item. */
if (!item_view)
item_view = GOO_CANVAS_ITEM_GET_IFACE (item)->create_view (item, view,
parent_view);
+ g_hash_table_insert (view->item_to_view, item, item_view);
+
/* Emit a signal so apps can hook up signal handlers if they want. */
g_signal_emit (view, canvas_view_signals[ITEM_VIEW_CREATED], 0,
item_view, item);
Index: goocanvasview.h
===================================================================
RCS file: /cvs/cairo/goocanvas/src/goocanvasview.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- goocanvasview.h 21 Jul 2006 10:30:30 -0000 1.9
+++ goocanvasview.h 23 Aug 2006 11:56:05 -0000 1.10
@@ -98,6 +98,9 @@
/* A window temporarily mapped above the canvas to stop X from scrolling
the contents unnecessarily (i.e. when we zoom in/out). */
GdkWindow *tmp_window;
+
+ /* A hash table mapping canvas items to canvas views. */
+ GHashTable *item_to_view;
};
struct _GooCanvasViewClass
@@ -129,6 +132,8 @@
GooCanvasItemView* goo_canvas_view_get_root_view (GooCanvasView *view);
+GooCanvasItemView* goo_canvas_view_get_item_view (GooCanvasView *view,
+ GooCanvasItem *item);
GooCanvasItemView* goo_canvas_view_get_item_view_at (GooCanvasView *view,
gdouble x,
gdouble y,
@@ -207,6 +212,8 @@
GooCanvasItemView* goo_canvas_view_create_item_view (GooCanvasView *view,
GooCanvasItem *item,
GooCanvasItemView *parent_view);
+void goo_canvas_view_unregister_item_view (GooCanvasView *view,
+ GooCanvasItem *item);
void goo_canvas_view_update (GooCanvasView *view);
void goo_canvas_view_request_update (GooCanvasView *view);
void goo_canvas_view_request_redraw (GooCanvasView *view,
More information about the cairo-commit
mailing list