]> err.no Git - mapper/commitdiff
Use private struct for compass internal variables
authorKaj-Michael Lang <milang@tal.org>
Mon, 28 Apr 2008 14:29:29 +0000 (17:29 +0300)
committerKaj-Michael Lang <milang@tal.org>
Mon, 28 Apr 2008 14:29:29 +0000 (17:29 +0300)
src/gtkcompass.c
src/gtkcompass.h

index 1f7da5b49675c6d094860f6c0728eb82d7014da7..22c8f0903113bc2e46d3df41842b0ac9f63eab73 100644 (file)
 #include <glib-object.h>
 #include "gtkcompass.h"
 
-#if 0
-#define DEBUG
-#endif
-
 static void gtk_compass_finalize (GObject *object);
 static void gtk_compass_size_request (GtkWidget *widget, GtkRequisition *requisition);
 static void gtk_compass_size_allocate (GtkWidget *widget, GtkAllocation *allocate);
@@ -59,9 +55,15 @@ struct _GtkCompassPriv
 PangoContext *context;
 PangoLayout *layout;
 PangoFontDescription *fontdesc;
+gint esid;
+gboolean dest_valid;
+gboolean way_valid;
+GdkGC *gc_h;
+GdkGC *gc_w;
+GdkGC *gc_d;
 };
 
-#define GTK_COMPASS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_COMPASS, GtkCompassPriv))
+#define GTK_COMPASS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_COMPASS_TYPE, GtkCompassPriv))
 
 static void
 gtk_compass_class_init (GtkCompassClass *class)
@@ -113,14 +115,18 @@ switch (prop_id) {
 static void
 gtk_compass_init(GtkCompass *compass)
 {
-compass->gc_h=NULL;
-compass->gc_d=NULL;
-compass->gc_w=NULL;
-compass->dest_valid=FALSE;
-compass->way_valid=FALSE;
+GtkCompassPriv *priv=GTK_COMPASS_GET_PRIVATE(compass);
+
 compass->width=300;
 compass->height=300;
-compass->esid=0;
+
+priv->gc_h=NULL;
+priv->gc_d=NULL;
+priv->gc_w=NULL;
+priv->esid=0;
+priv->dest_valid=FALSE;
+priv->way_valid=FALSE;
+
 g_signal_connect(G_OBJECT(compass), "button_press_event", G_CALLBACK(gtk_compass_cb_button_press), NULL);
 }
 
@@ -151,10 +157,11 @@ static void
 gtk_compass_finalize(GObject *object)
 {
 GtkCompass *compass;
+GtkCompassPriv *priv=GTK_COMPASS_GET_PRIVATE(compass);
        
 g_return_if_fail(GTK_IS_COMPASS(object));
 compass=GTK_COMPASS(object);
-g_source_remove(compass->esid);
+g_source_remove(priv->esid);
 
 if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) {
        gtk_widget_unmap(GTK_WIDGET(object));
@@ -212,16 +219,18 @@ if (widget->allocation.width > widget->allocation.height) {
 }
 
 static void 
-gtk_compass_realize (GtkWidget *widget)
+gtk_compass_realize(GtkWidget *widget)
 {
 GtkCompass *compass;
 GdkColor color;
 GdkWindowAttr attributes;
 gint attributes_mask;
+GtkCompassPriv *priv;
 
 g_return_if_fail (GTK_IS_COMPASS(widget));
 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
 compass=GTK_COMPASS(widget);
+priv=GTK_COMPASS_GET_PRIVATE(compass);
 
 attributes.x=widget->allocation.x;
 attributes.y=widget->allocation.y;
@@ -241,42 +250,42 @@ widget->style=gtk_style_attach(widget->style, widget->window);
 gdk_window_set_user_data(widget->window, widget);
 gtk_style_set_background(widget->style, widget->window, GTK_STATE_ACTIVE);
 
-compass->context=gtk_widget_get_pango_context(widget);
-compass->layout=pango_layout_new(compass->context);
-compass->fontdesc=pango_font_description_new();
-pango_font_description_set_family(compass->fontdesc, "Sans Serif");
-pango_font_description_set_size(compass->fontdesc, 12*PANGO_SCALE);
-pango_layout_set_font_description(compass->layout, compass->fontdesc);
-pango_layout_set_alignment(compass->layout, PANGO_ALIGN_CENTER);
+priv->context=gtk_widget_get_pango_context(widget);
+priv->layout=pango_layout_new(priv->context);
+priv->fontdesc=pango_font_description_new();
+pango_font_description_set_family(priv->fontdesc, "Sans Serif");
+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 (!compass->gc_h) {
+if (!priv->gc_h) {
        color.red=0x0000;
        color.green=0x0000;
        color.blue=0x0000;
-       compass->gc_h=gdk_gc_new(widget->window);
-       gdk_gc_set_rgb_fg_color(compass->gc_h, &color);
-       gdk_gc_set_line_attributes(compass->gc_h, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
+       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 (!compass->gc_d) {
+if (!priv->gc_d) {
        color.red=0xffff;
        color.green=0x0000;
        color.blue=0xffff;
-       compass->gc_d=gdk_gc_new(widget->window);
-       gdk_gc_set_rgb_fg_color(compass->gc_d, &color);
-       gdk_gc_set_line_attributes(compass->gc_d, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
+       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 (!compass->gc_w) {
+if (!priv->gc_w) {
        color.red=0x0000;
        color.green=0xffff;
        color.blue=0x0000;
-       compass->gc_w=gdk_gc_new(widget->window);
-       gdk_gc_set_rgb_fg_color(compass->gc_w, &color);
-       gdk_gc_set_line_attributes(compass->gc_w, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
+       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);
 }
 
-compass->esid=g_timeout_add(300,(GSourceFunc)gtk_compass_refresh_cb, compass);
+priv->esid=g_timeout_add(300,(GSourceFunc)gtk_compass_refresh_cb, compass);
 }
 
 static void
@@ -307,9 +316,13 @@ gchar *text;
 gchar htext[8];
 gint angle[5] = { -90, -45, 0, 45, 90 };
 gint fsize[5] = { 0, 4, 10, 4, 0 };
+GtkCompassPriv *priv;
 
+g_return_if_fail(GTK_IS_COMPASS(compass));
 widget=GTK_WIDGET(compass);
+priv=GTK_COMPASS_GET_PRIVATE(compass);
 size=compass->size;
+
 #if 1
 hsize=size/2;
 #else
@@ -319,45 +332,45 @@ hsize=0;
 fs=size/41;
 BOUND(fs, 1, 16);
 
-pango_context_set_matrix (compass->context, NULL);
+pango_context_set_matrix (priv->context, NULL);
 
 g_snprintf(htext, 8, "%3.0f°", compass->data->heading);
-pango_font_description_set_size(compass->fontdesc, (10+fs) * PANGO_SCALE);
-pango_layout_set_font_description(compass->layout, compass->fontdesc);
-pango_layout_set_text(compass->layout, htext, -1);
-pango_layout_get_pixel_size(compass->layout, &x, &y);
+pango_font_description_set_size(priv->fontdesc, (10+fs) * PANGO_SCALE);
+pango_layout_set_font_description(priv->layout, priv->fontdesc);
+pango_layout_set_text(priv->layout, htext, -1);
+pango_layout_get_pixel_size(priv->layout, &x, &y);
 
 gdk_draw_layout(widget->window,
-       compass->gc_h,
+       priv->gc_h,
        compass->xoffset+hsize-x/2,
-       compass->yoffset+size-y-2, compass->layout);
+       compass->yoffset+size-y-2, priv->layout);
 
 gdk_draw_arc(widget->window,
-       compass->gc_h,
+       priv->gc_h,
        FALSE,
        compass->xoffset, compass->yoffset+hsize, size - fs, size - fs, 0, 64 * 360);
 
 /* Simple arrow for heading */
 gdk_draw_line(widget->window,
-       compass->gc_h,
+       priv->gc_h,
        compass->xoffset + hsize + 3,
        compass->yoffset + size - y - 5,
        compass->xoffset + hsize, compass->yoffset + hsize + 5);
 
 gdk_draw_line(widget->window,
-       compass->gc_h,
+       priv->gc_h,
        compass->xoffset + hsize - 3,
        compass->yoffset + size - y - 5,
        compass->xoffset + hsize, compass->yoffset + hsize + 5);
 
 gdk_draw_line(widget->window,
-       compass->gc_h,
+       priv->gc_h,
        compass->xoffset + hsize - 3,
        compass->yoffset + size - y - 5,
        compass->xoffset + hsize, compass->yoffset + size - y - 8);
 
 gdk_draw_line(widget->window,
-       compass->gc_h,
+       priv->gc_h,
        compass->xoffset + hsize + 3,
        compass->yoffset + size - y - 5,
        compass->xoffset + hsize, compass->yoffset + size - y - 8);
@@ -402,32 +415,32 @@ for (i = 0; i < 5; i++) {
 
        tmp = ((dir - compass->heading) * (1.f / 180.f)) * G_PI;
 
-       gtk_compass_draw_mark(compass, compass->gc_h, tmp, 6);
+       gtk_compass_draw_mark(compass, priv->gc_h, tmp, 6);
 
        x = fsize[i];
        if (abs((guint) (compass->heading / 45) * 45 - compass->heading)
            > abs((guint) (compass->heading / 45) * 45 + 45 - compass->heading) && (i > 0))
                        x = fsize[i - 1];
 
-       pango_font_description_set_size(compass->fontdesc, (10 + x + fs) * PANGO_SCALE);
-       pango_layout_set_font_description(compass->layout, compass->fontdesc);
-       pango_layout_set_text(compass->layout, text, -1);
+       pango_font_description_set_size(priv->fontdesc, (10 + x + fs) * PANGO_SCALE);
+       pango_layout_set_font_description(priv->layout, priv->fontdesc);
+       pango_layout_set_text(priv->layout, text, -1);
        pango_matrix_rotate (&matrix, -(dir-compass->heading));
-       pango_context_set_matrix (compass->context, &matrix);
-       pango_layout_get_pixel_size(compass->layout, &x, &y);
+       pango_context_set_matrix (priv->context, &matrix);
+       pango_layout_get_pixel_size(priv->layout, &x, &y);
        x = compass->xoffset + hsize + ((hsize + 15 + fs) * sinf(tmp)) - x / 2,
     y = compass->yoffset + size - ((hsize + 15 + fs) * cosf(tmp)) - y / 2,
-    gdk_draw_layout(widget->window, compass->gc_h, x, y, compass->layout);
+    gdk_draw_layout(widget->window, priv->gc_h, x, y, priv->layout);
 }
 
-if (compass->dest_valid) {
+if (priv->dest_valid) {
        tmp=((compass->dest_heading-compass->heading) * (1.f / 180.f)) * G_PI;
-       gtk_compass_draw_mark(compass, compass->gc_d, tmp, 10);
+       gtk_compass_draw_mark(compass, priv->gc_d, tmp, 10);
 }
 
-if (compass->way_valid) {
+if (priv->way_valid) {
        tmp=((compass->way_heading-compass->heading) * (1.f / 180.f)) * G_PI;
-       gtk_compass_draw_mark(compass, compass->gc_w, tmp, 10);
+       gtk_compass_draw_mark(compass, priv->gc_w, tmp, 10);
 }
 
 return;
@@ -476,10 +489,6 @@ if (compass->heading<compass->data->heading)
 if (compass->heading>compass->data->heading)
        compass->heading-=tmp;
 
-#ifdef DEBUG
-g_printf("%.2f %.2f\n", compass->heading, compass->data->heading);
-#endif
-
 gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height);
 return TRUE;
 }
@@ -499,11 +508,14 @@ void
 gtk_compass_set_way_heading(GtkWidget *widget, gboolean valid, gfloat heading)
 {
 GtkCompass *compass;
+GtkCompassPriv *priv;
+
 g_return_if_fail(GTK_IS_COMPASS(widget));
 
 compass=GTK_COMPASS(widget);
+priv=GTK_COMPASS_GET_PRIVATE(compass);
 
-compass->way_valid=valid;
+priv->way_valid=valid;
 compass->way_heading=heading;
 
 gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height);
@@ -513,11 +525,14 @@ void
 gtk_compass_set_dest_heading(GtkWidget *widget, gboolean valid, gfloat heading)
 {
 GtkCompass *compass;
+GtkCompassPriv *priv;
+
 g_return_if_fail(GTK_IS_COMPASS(widget));
 
 compass=GTK_COMPASS(widget);
+priv=GTK_COMPASS_GET_PRIVATE(compass);
 
-compass->dest_valid=valid;
+priv->dest_valid=valid;
 compass->dest_heading=heading;
 
 compass=GTK_COMPASS(widget);
index e760350620ffa0686c0b22b5e24a47268cbe0683..8106eaf4947734dac4b193bd8ce5051adeecc912 100644 (file)
@@ -35,21 +35,12 @@ typedef struct _GtkCompassClass             GtkCompassClass;
 
 struct _GtkCompass {
        GtkDrawingArea widget;
-       GdkGC *gc_h;
-       GdkGC *gc_w;
-       GdkGC *gc_d;
        guint width, height;
        guint size;
        guint xoffset, yoffset;
 
-       PangoContext *context;
-       PangoLayout *layout;
-       PangoFontDescription *fontdesc;
-
        GpsData *data;
        gfloat heading;
-       gboolean dest_valid;
-       gboolean way_valid;
        gfloat dest_heading;
        gfloat way_heading;
        gint esid;