[goffice] Graph: use a timer for rate-limiting.



commit 7f409143413edf198e9d5f57266a8475804202d6
Author: Morten Welinder <terra gnome org>
Date:   Sun Oct 18 16:45:33 2009 -0400

    Graph: use a timer for rate-limiting.

 ChangeLog                  |    3 +++
 goffice/canvas/goc-graph.c |   41 ++++++++++++++++++++++++++++++++++-------
 goffice/canvas/goc-graph.h |    6 +++++-
 3 files changed, 42 insertions(+), 8 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 7ff4652..ea2c0ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2009-10-18  Morten Welinder  <terra gnome org>
 
+	* goffice/canvas/goc-graph.c (goc_graph_motion): Hook up a timer
+	to handle the rate-limiting.
+
 	* goffice/graph/gog-axis.c (gog_axis_get_effective_format): Rename
 	from get_axis_format and make public.
 	(axis_format_value): Use unicode minuses and proper date_conv.
diff --git a/goffice/canvas/goc-graph.c b/goffice/canvas/goc-graph.c
index 41372db..3560c32 100644
--- a/goffice/canvas/goc-graph.c
+++ b/goffice/canvas/goc-graph.c
@@ -148,6 +148,10 @@ goc_graph_finalize (GObject *obj)
 		g_object_unref (graph->renderer);
 		graph->renderer = NULL;
 	}
+	if (graph->coords.timer_id) {
+		g_source_remove (graph->coords.timer_id);
+		graph->coords.timer_id = 0;
+	}
 	(*parent_klass->finalize) (obj);
 }
 
@@ -241,7 +245,7 @@ format_coordinate (GogAxis *axis, GOFormat *fmt, double x)
 }
 
 static void
-goc_graph_do_tooltip (GocGraph *graph, double x, double y)
+goc_graph_do_tooltip (GocGraph *graph)
 {
 	GogView *view;
 	char *buf = NULL, *s1 = NULL, *s2 = NULL;
@@ -256,6 +260,8 @@ goc_graph_do_tooltip (GocGraph *graph, double x, double y)
 	GSList *l;
 	GOFormat *format;
 	GocItem *item = (GocItem *)graph;
+	double x = graph->coords.x;
+	double y = graph->coords.y;
 
 	/* translate x and y tovalues relative to the graph */
 	xpos = graph->x;
@@ -340,24 +346,45 @@ tooltip:
 }
 
 static gboolean
+goc_graph_timer (GocGraph *graph)
+{
+	goc_graph_do_tooltip (graph);
+	graph->coords.timer_id = 0;
+	return FALSE;
+}
+
+static gboolean
 goc_graph_motion (GocItem *item, double x, double y)
 {
-	GdkEventMotion *event = (GdkEventMotion*) goc_canvas_get_cur_event (item->canvas);
 	GocGraph *graph = GOC_GRAPH (item);
 
-	/* do not allow more than 20 updates per second */
-	if (event->time - graph->last_time >= 50) {
-		graph->last_time = event->time;
-		goc_graph_do_tooltip (graph, x, y);
+	/*
+	 * Do not allow more than 20 updates per second.  We do this by
+	 * scheduling the actual update in a timeout.
+	 */
+	if (graph->coords.timer_id == 0) {
+		graph->coords.timer_id =
+			g_timeout_add (50,
+				       (GSourceFunc)goc_graph_timer,
+				       graph);
 	}
 
+	/* When the timer first, use the last (x,y) we have.  */
+	graph->coords.x = x;
+	graph->coords.y = y;
+
 	return ((GocItemClass*) parent_klass)->motion (item, x, y);
 }
 
 static gboolean
 goc_graph_leave_notify (GocItem *item, double x, double y)
 {
-	GOC_GRAPH (item)->last_time = 0;
+	GocGraph *graph = GOC_GRAPH (item);
+
+	if (graph->coords.timer_id) {
+		g_source_remove (graph->coords.timer_id);
+		graph->coords.timer_id = 0;
+	}
 	gtk_widget_set_tooltip_text (GTK_WIDGET (item->canvas), NULL);
 	return ((GocItemClass*) parent_klass)->leave_notify (item, x, y);
 }
diff --git a/goffice/canvas/goc-graph.h b/goffice/canvas/goc-graph.h
index 3d2e0ee..1d1db62 100644
--- a/goffice/canvas/goc-graph.h
+++ b/goffice/canvas/goc-graph.h
@@ -31,7 +31,11 @@ struct _GocGraph {
 
 	double x, y, w, h;
 	GogRenderer *renderer;
-	guint32 last_time;
+
+	struct {
+		guint timer_id;
+		double x, y;
+	} coords;
 };
 
 typedef GocItemClass GocGraphClass;



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