#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);
static void gtk_compass_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void gtk_compass_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static void gtk_compass_paint(GtkCompass *compass);
+static gboolean gtk_compass_refresh_cb(GtkWidget *widget);
G_DEFINE_TYPE(GtkCompass, gtk_compass, GTK_TYPE_WIDGET);
}
static void
-gtk_compass_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+gtk_compass_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
GtkCompass *compass;
g_return_if_fail(GTK_IS_COMPASS(object));
compass=GTK_COMPASS(object);
switch (prop_id) {
default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
-gtk_compass_init (GtkCompass *compass)
+gtk_compass_init(GtkCompass *compass)
{
compass->gc_h=NULL;
compass->gc_d=NULL;
compass->way_valid=FALSE;
compass->width=300;
compass->height=300;
+compass->esid=0;
}
static gboolean
-gtk_compass_cb_button_press(GtkWidget * widget, GdkEventButton * event)
+gtk_compass_cb_button_press(GtkWidget *widget, GdkEventButton *event)
{
GtkCompass *compass;
compass=GTK_COMPASS(widget);
+#ifdef DEBUG
+compass->data->heading=0;
+compass->heading=180;
+#endif
+
return FALSE;
}
compass=gtk_type_new(gtk_compass_get_type ());
widget=GTK_WIDGET(compass);
compass->data=data;
+compass->heading=0;
g_signal_connect(G_OBJECT(widget), "button_press_event", G_CALLBACK(gtk_compass_cb_button_press), NULL);
+
+compass->esid=g_timeout_add(1000,(GSourceFunc)gtk_compass_refresh_cb, compass);
+
return widget;
}
g_return_if_fail(GTK_IS_COMPASS(object));
compass=GTK_COMPASS(object);
+g_source_remove(compass->esid);
if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) {
gtk_widget_unmap(GTK_WIDGET(object));
g_return_if_fail(GTK_IS_COMPASS(widget));
g_return_if_fail(requisition != NULL);
-compass = GTK_COMPASS (widget);
+compass=GTK_COMPASS(widget);
requisition->width=400;
requisition->height=300;
pango_context_set_matrix (compass->context, NULL);
-g_snprintf(htext, 8, "%3.0f°", compass->data->heading);
+g_snprintf(htext, 8, "%3.0f°", compass->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);
for (i = 0; i < 5; i++) {
PangoMatrix matrix = PANGO_MATRIX_INIT;
- dir = (gint) (compass->data->heading / 45) * 45 + angle[i];
+ dir = (gint) (compass->heading / 45) * 45 + angle[i];
switch (dir) {
case 0:
break;
}
- tmp = ((dir - compass->data->heading) * (1.f / 180.f)) * G_PI;
+ tmp = ((dir - compass->heading) * (1.f / 180.f)) * G_PI;
gtk_compass_draw_mark(compass, compass->gc_h, tmp, 6);
x = fsize[i];
- if (abs((guint) (compass->data->heading / 45) * 45 - compass->data->heading)
- > abs((guint) (compass->data->heading / 45) * 45 + 45 - compass->data->heading) && (i > 0))
+ 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_matrix_rotate (&matrix, -(dir-compass->data->heading));
+ pango_matrix_rotate (&matrix, -(dir-compass->heading));
pango_context_set_matrix (compass->context, &matrix);
pango_layout_get_pixel_size(compass->layout, &x, &y);
x = compass->xoffset + hsize + ((hsize + 15 + fs) * sinf(tmp)) - x / 2,
}
if (compass->dest_valid) {
- tmp=((compass->dest_heading-compass->data->heading) * (1.f / 180.f)) * G_PI;
+ tmp=((compass->dest_heading-compass->heading) * (1.f / 180.f)) * G_PI;
gtk_compass_draw_mark(compass, compass->gc_d, tmp, 10);
}
if (compass->way_valid) {
- tmp=((compass->way_heading-compass->data->heading) * (1.f / 180.f)) * G_PI;
+ tmp=((compass->way_heading-compass->heading) * (1.f / 180.f)) * G_PI;
gtk_compass_draw_mark(compass, compass->gc_w, tmp, 10);
}
return TRUE;
}
+static gboolean
+gtk_compass_refresh_cb(GtkWidget *widget)
+{
+GtkCompass *compass;
+gfloat tmp;
+
+g_return_val_if_fail(GTK_IS_COMPASS(widget), FALSE);
+
+compass=GTK_COMPASS(widget);
+
+if ((GTK_WIDGET_MAPPED(widget)==FALSE) || (GTK_WIDGET_VISIBLE(widget)==FALSE)) {
+ compass->heading=compass->data->heading;
+ return TRUE;
+}
+
+if (compass->heading==compass->data->heading)
+ return TRUE;
+
+tmp=fabsf(compass->heading-compass->data->heading);
+if (tmp>5)
+ tmp=tmp/2.2;
+else
+ compass->heading=compass->data->heading;
+
+if (compass->heading<compass->data->heading)
+ compass->heading+=tmp;
+
+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;
+}
+
void
gtk_compass_refresh(GtkWidget *widget)
{