From 067d25b65520bc84295e391381faacb2500ed8a5 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Mon, 5 May 2008 15:25:29 +0300 Subject: [PATCH] Compass widget cleanups and optimizations: - Remove timer if the widget is hidden - Re-add update timer on expose --- src/gtkcompass.c | 112 ++++++++++++++++++++++------------------------- src/gtkcompass.h | 1 - 2 files changed, 52 insertions(+), 61 deletions(-) diff --git a/src/gtkcompass.c b/src/gtkcompass.c index a959bea..1e6e985 100644 --- a/src/gtkcompass.c +++ b/src/gtkcompass.c @@ -117,12 +117,8 @@ gtk_compass_init(GtkCompass *compass) { GtkCompassPriv *priv=GTK_COMPASS_GET_PRIVATE(compass); -compass->width=300; -compass->height=300; +compass->heading=0; -priv->gc_h=NULL; -priv->gc_d=NULL; -priv->gc_w=NULL; priv->esid=0; priv->dest_valid=FALSE; priv->way_valid=FALSE; @@ -148,7 +144,6 @@ GtkWidget *widget; compass=g_object_new(GTK_COMPASS_TYPE, NULL); widget=GTK_WIDGET(compass); compass->data=data; -compass->heading=0; return widget; } @@ -182,10 +177,8 @@ g_return_if_fail(requisition != NULL); compass=GTK_COMPASS(widget); -requisition->width=400; -requisition->height=300; -compass->width=400; -compass->height=300; +requisition->width=64; +requisition->height=64; } static void @@ -206,16 +199,16 @@ if (GTK_WIDGET_REALIZED (widget)) { allocation->width, allocation->height); } -compass->width=allocation->width; -compass->height=allocation->height; - compass->size = MIN(widget->allocation.width, widget->allocation.height); if (widget->allocation.width > widget->allocation.height) { compass->xoffset = (widget->allocation.width - widget->allocation.height) / 2; compass->yoffset = 0; -} else { +} else if (widget->allocation.width < widget->allocation.height) { compass->xoffset = 0; compass->yoffset = (widget->allocation.height - widget->allocation.width) / 2; +} else { + compass->xoffset = 0; + compass->yoffset = 0; } } @@ -260,34 +253,26 @@ pango_font_description_set_size(priv->fontdesc, 12*PANGO_SCALE); pango_layout_set_font_description(priv->layout, priv->fontdesc); pango_layout_set_alignment(priv->layout, PANGO_ALIGN_CENTER); -if (!priv->gc_h) { - color.red=0x0000; - color.green=0x0000; - color.blue=0x0000; - priv->gc_h=gdk_gc_new(widget->window); - gdk_gc_set_rgb_fg_color(priv->gc_h, &color); - gdk_gc_set_line_attributes(priv->gc_h, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); -} - -if (!priv->gc_d) { - color.red=0xffff; - color.green=0x0000; - color.blue=0xffff; - priv->gc_d=gdk_gc_new(widget->window); - gdk_gc_set_rgb_fg_color(priv->gc_d, &color); - gdk_gc_set_line_attributes(priv->gc_d, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); -} - -if (!priv->gc_w) { - color.red=0x0000; - color.green=0xffff; - color.blue=0x0000; - priv->gc_w=gdk_gc_new(widget->window); - gdk_gc_set_rgb_fg_color(priv->gc_w, &color); - gdk_gc_set_line_attributes(priv->gc_w, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); -} - -priv->esid=g_timeout_add(300,(GSourceFunc)gtk_compass_refresh_cb, compass); +color.red=0x0000; +color.green=0x0000; +color.blue=0x0000; +priv->gc_h=gdk_gc_new(widget->window); +gdk_gc_set_rgb_fg_color(priv->gc_h, &color); +gdk_gc_set_line_attributes(priv->gc_h, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); + +color.red=0xffff; +color.green=0x0000; +color.blue=0xffff; +priv->gc_d=gdk_gc_new(widget->window); +gdk_gc_set_rgb_fg_color(priv->gc_d, &color); +gdk_gc_set_line_attributes(priv->gc_d, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); + +color.red=0x0000; +color.green=0xffff; +color.blue=0x0000; +priv->gc_w=gdk_gc_new(widget->window); +gdk_gc_set_rgb_fg_color(priv->gc_w, &color); +gdk_gc_set_line_attributes(priv->gc_w, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); } static void @@ -452,11 +437,16 @@ static gboolean gtk_compass_expose(GtkWidget *widget, GdkEventExpose *event) { GtkCompass *compass; +GtkCompassPriv *priv; g_return_val_if_fail(GTK_IS_COMPASS(widget), FALSE); g_return_val_if_fail(event != NULL, FALSE); compass=GTK_COMPASS(widget); +priv=GTK_COMPASS_GET_PRIVATE(compass); +if (priv->esid==0) { + priv->esid=g_timeout_add(300,(GSourceFunc)gtk_compass_refresh_cb, compass); +} gtk_compass_paint(compass); return TRUE; } @@ -465,45 +455,49 @@ static gboolean gtk_compass_refresh_cb(GtkWidget *widget) { GtkCompass *compass; +GtkCompassPriv *priv; gfloat tmp; g_return_val_if_fail(GTK_IS_COMPASS(widget), FALSE); compass=GTK_COMPASS(widget); +priv=GTK_COMPASS_GET_PRIVATE(compass); if ((GTK_WIDGET_MAPPED(widget)==FALSE) || (GTK_WIDGET_VISIBLE(widget)==FALSE)) { compass->heading=compass->data->heading; + if (priv->esid!=0) { + g_source_remove(priv->esid); + priv->esid=0; + } return TRUE; } +if (priv->esid==0) + priv->esid=g_timeout_add(300,(GSourceFunc)gtk_compass_refresh_cb, compass); + if (compass->heading==compass->data->heading) return TRUE; tmp=fabsf(compass->heading-compass->data->heading); -if (tmp>5) +if (tmp>5) { tmp=tmp/2.2; -else + if (compass->headingdata->heading) + compass->heading+=tmp; + else if (compass->heading>compass->data->heading) + compass->heading-=tmp; +} else { compass->heading=compass->data->heading; +} -if (compass->headingdata->heading) - compass->heading+=tmp; - -if (compass->heading>compass->data->heading) - compass->heading-=tmp; - -gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height); +gtk_widget_queue_draw_area(widget, 0, 0, widget->allocation.width, widget->allocation.height); return TRUE; } void gtk_compass_refresh(GtkWidget *widget) { -GtkCompass *compass; - g_return_if_fail(GTK_IS_COMPASS(widget)); - -compass=GTK_COMPASS(widget); -gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height); +gtk_widget_queue_draw_area(widget, 0, 0, widget->allocation.width, widget->allocation.height); } void @@ -520,7 +514,7 @@ priv=GTK_COMPASS_GET_PRIVATE(compass); priv->way_valid=valid; compass->way_heading=heading; -gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height); +gtk_widget_queue_draw_area(widget, 0, 0, widget->allocation.width, widget->allocation.height); } void @@ -537,7 +531,5 @@ priv=GTK_COMPASS_GET_PRIVATE(compass); priv->dest_valid=valid; compass->dest_heading=heading; -compass=GTK_COMPASS(widget); -gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height); +gtk_widget_queue_draw_area(widget, 0, 0, widget->allocation.width, widget->allocation.height); } - diff --git a/src/gtkcompass.h b/src/gtkcompass.h index 8106eaf..6d5c136 100644 --- a/src/gtkcompass.h +++ b/src/gtkcompass.h @@ -35,7 +35,6 @@ typedef struct _GtkCompassClass GtkCompassClass; struct _GtkCompass { GtkDrawingArea widget; - guint width, height; guint size; guint xoffset, yoffset; -- 2.39.5