3 * GtkCompass: Display heading/compass information
4 * Also bearing to target and next waypoint.
6 * Copyright (C) 2007 Kaj-Michael Lang
7 * Originanl non-widget version Copyright Cezary Jackiewicz
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <glib/gstdio.h>
28 #include <glib-object.h>
29 #include "gtkcompass.h"
31 static void gtk_compass_finalize (GObject *object);
32 static void gtk_compass_size_request (GtkWidget *widget, GtkRequisition *requisition);
33 static void gtk_compass_size_allocate (GtkWidget *widget, GtkAllocation *allocate);
34 static gboolean gtk_compass_expose (GtkWidget *widget, GdkEventExpose *event);
35 static void gtk_compass_realize (GtkWidget *widget);
36 static void gtk_compass_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
37 static void gtk_compass_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
38 static void gtk_compass_paint(GtkCompass *compass);
40 G_DEFINE_TYPE(GtkCompass, gtk_compass, GTK_TYPE_WIDGET);
42 #define BOUND(x, a, b) { \
50 gtk_compass_class_init (GtkCompassClass *class)
52 GObjectClass *object_class;
53 GtkWidgetClass *widget_class;
55 object_class = (GObjectClass*) class;
56 widget_class = (GtkWidgetClass*) class;
58 object_class->finalize = gtk_compass_finalize;
59 object_class->set_property = gtk_compass_set_property;
60 object_class->get_property = gtk_compass_get_property;
62 widget_class->size_request = gtk_compass_size_request;
63 widget_class->expose_event = gtk_compass_expose;
64 widget_class->realize = gtk_compass_realize;
65 widget_class->size_allocate = gtk_compass_size_allocate;
69 gtk_compass_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
72 g_return_if_fail(GTK_IS_COMPASS(object));
73 compass=GTK_COMPASS(object);
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
82 gtk_compass_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
85 g_return_if_fail(GTK_IS_COMPASS(object));
86 compass=GTK_COMPASS(object);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 gtk_compass_init (GtkCompass *compass)
100 compass->dest_valid=FALSE;
101 compass->way_valid=FALSE;
107 gtk_compass_cb_button_press(GtkWidget * widget, GdkEventButton * event)
111 compass=GTK_COMPASS(widget);
117 gtk_compass_new(GpsData *data)
122 compass=gtk_type_new(gtk_compass_get_type ());
123 widget=GTK_WIDGET(compass);
125 g_signal_connect(G_OBJECT(widget), "button_press_event", G_CALLBACK(gtk_compass_cb_button_press), NULL);
130 gtk_compass_finalize(GObject *object)
134 g_return_if_fail(GTK_IS_COMPASS(object));
135 compass=GTK_COMPASS(object);
137 if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) {
138 gtk_widget_unmap(GTK_WIDGET(object));
141 G_OBJECT_CLASS(gtk_compass_parent_class)->finalize(object);
145 gtk_compass_size_request(GtkWidget *widget, GtkRequisition *requisition)
149 g_return_if_fail(GTK_IS_COMPASS(widget));
150 g_return_if_fail(requisition != NULL);
152 compass = GTK_COMPASS (widget);
154 requisition->width=400;
155 requisition->height=300;
161 gtk_compass_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
165 g_return_if_fail(GTK_IS_COMPASS(widget));
166 g_return_if_fail(allocation!=NULL);
168 compass=GTK_COMPASS(widget);
170 widget->allocation=*allocation;
172 if (GTK_WIDGET_REALIZED (widget)) {
173 gdk_window_move_resize (widget->window,
174 allocation->x, allocation->y,
175 allocation->width, allocation->height);
178 compass->width=allocation->width;
179 compass->height=allocation->height;
181 compass->size = MIN(widget->allocation.width, widget->allocation.height);
182 if (widget->allocation.width > widget->allocation.height) {
183 compass->xoffset = (widget->allocation.width - widget->allocation.height) / 2;
184 compass->yoffset = 0;
186 compass->xoffset = 0;
187 compass->yoffset = (widget->allocation.height - widget->allocation.width) / 2;
193 gtk_compass_realize (GtkWidget *widget)
197 GdkWindowAttr attributes;
198 gint attributes_mask;
200 g_return_if_fail (GTK_IS_COMPASS(widget));
201 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
202 compass=GTK_COMPASS(widget);
204 attributes.x=widget->allocation.x;
205 attributes.y=widget->allocation.y;
206 attributes.width=widget->allocation.width;
207 attributes.height=widget->allocation.height;
208 attributes.wclass=GDK_INPUT_OUTPUT;
209 attributes.window_type=GDK_WINDOW_CHILD;
210 attributes.event_mask=gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK;
211 attributes.visual=gtk_widget_get_visual(widget);
212 attributes.colormap=gtk_widget_get_colormap(widget);
214 attributes_mask=GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
216 widget->window=gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, attributes_mask);
217 widget->style=gtk_style_attach(widget->style, widget->window);
219 gdk_window_set_user_data(widget->window, widget);
220 gtk_style_set_background(widget->style, widget->window, GTK_STATE_ACTIVE);
222 compass->context=gtk_widget_get_pango_context(widget);
223 compass->layout=pango_layout_new(compass->context);
224 compass->fontdesc=pango_font_description_new();
225 pango_font_description_set_family(compass->fontdesc, "Sans Serif");
226 pango_font_description_set_size(compass->fontdesc, 12*PANGO_SCALE);
227 pango_layout_set_font_description(compass->layout, compass->fontdesc);
228 pango_layout_set_alignment(compass->layout, PANGO_ALIGN_CENTER);
230 if (!compass->gc_h) {
234 compass->gc_h=gdk_gc_new(widget->window);
235 gdk_gc_set_rgb_fg_color(compass->gc_h, &color);
236 gdk_gc_set_line_attributes(compass->gc_h, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
239 if (!compass->gc_d) {
243 compass->gc_d=gdk_gc_new(widget->window);
244 gdk_gc_set_rgb_fg_color(compass->gc_d, &color);
245 gdk_gc_set_line_attributes(compass->gc_d, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
248 if (!compass->gc_w) {
252 compass->gc_w=gdk_gc_new(widget->window);
253 gdk_gc_set_rgb_fg_color(compass->gc_w, &color);
254 gdk_gc_set_line_attributes(compass->gc_w, 6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
260 gtk_compass_draw_mark(GtkCompass *compass, GdkGC *gc, gdouble angle, gint size)
265 widget=GTK_WIDGET(compass);
269 gdk_draw_line(widget->window,gc,
270 compass->xoffset+hs+((hs-size)*sin(angle)),
271 compass->yoffset+compass->size-((hs-size)*cos(angle)),
272 compass->xoffset+hs+((hs+size)*sin(angle)),
273 compass->yoffset+compass->size-((hs+size)*cos(angle)));
277 gtk_compass_paint(GtkCompass *compass)
280 guint i, x, y, size, hsize, fs;
285 gint angle[5] = { -90, -45, 0, 45, 90 };
286 gint fsize[5] = { 0, 4, 10, 4, 0 };
288 widget=GTK_WIDGET(compass);
295 pango_context_set_matrix (compass->context, NULL);
297 g_snprintf(htext, 8, "%3.0f°", compass->data->heading);
298 pango_font_description_set_size(compass->fontdesc, (10+fs) * PANGO_SCALE);
299 pango_layout_set_font_description(compass->layout, compass->fontdesc);
300 pango_layout_set_text(compass->layout, htext, -1);
301 pango_layout_get_pixel_size(compass->layout, &x, &y);
303 gdk_draw_layout(widget->window,
305 compass->xoffset+size/2-x/2,
306 compass->yoffset+size-y-2, compass->layout);
308 gdk_draw_arc(widget->window,
311 compass->xoffset, compass->yoffset+size/2, size, size, 0, 64 * 180);
313 /* Simple arrow for heading */
314 gdk_draw_line(widget->window,
316 compass->xoffset + size / 2 + 3,
317 compass->yoffset + size - y - 5,
318 compass->xoffset + size / 2, compass->yoffset + size / 2 + 5);
320 gdk_draw_line(widget->window,
322 compass->xoffset + size / 2 - 3,
323 compass->yoffset + size - y - 5,
324 compass->xoffset + size / 2, compass->yoffset + size / 2 + 5);
326 gdk_draw_line(widget->window,
328 compass->xoffset + size / 2 - 3,
329 compass->yoffset + size - y - 5,
330 compass->xoffset + size / 2, compass->yoffset + size - y - 8);
332 gdk_draw_line(widget->window,
334 compass->xoffset + size / 2 + 3,
335 compass->yoffset + size - y - 5,
336 compass->xoffset + size / 2, compass->yoffset + size - y - 8);
338 for (i = 0; i < 5; i++) {
339 PangoMatrix matrix = PANGO_MATRIX_INIT;
340 dir = (gint) (compass->data->heading / 45) * 45 + angle[i];
376 tmp = ((dir - compass->data->heading) * (1.f / 180.f)) * G_PI;
378 gtk_compass_draw_mark(compass, compass->gc_h, tmp, 6);
381 if (abs((guint) (compass->data->heading / 45) * 45 - compass->data->heading)
382 > abs((guint) (compass->data->heading / 45) * 45 + 45 - compass->data->heading) && (i > 0))
385 pango_font_description_set_size(compass->fontdesc, (10 + x + fs) * PANGO_SCALE);
386 pango_layout_set_font_description(compass->layout, compass->fontdesc);
387 pango_layout_set_text(compass->layout, text, -1);
388 pango_matrix_rotate (&matrix, -(dir-compass->data->heading));
389 pango_context_set_matrix (compass->context, &matrix);
390 pango_layout_get_pixel_size(compass->layout, &x, &y);
391 x = compass->xoffset + size / 2 + ((size / 2 + 15 + fs) * sinf(tmp)) - x / 2,
392 y = compass->yoffset + size - ((size / 2 + 15 + fs) * cosf(tmp)) - y / 2,
393 gdk_draw_layout(widget->window, compass->gc_h, x, y, compass->layout);
396 if (compass->dest_valid)
397 gtk_compass_draw_mark(compass, compass->gc_d, compass->dest_heading, 10);
399 if (compass->way_valid)
400 gtk_compass_draw_mark(compass, compass->gc_w, compass->way_heading, 10);
406 gtk_compass_expose(GtkWidget *widget, GdkEventExpose *event)
410 g_return_val_if_fail(GTK_IS_COMPASS(widget), FALSE);
411 g_return_val_if_fail(event != NULL, FALSE);
413 compass=GTK_COMPASS(widget);
414 gtk_compass_paint(compass);
419 gtk_compass_refresh(GtkWidget *widget)
423 g_return_if_fail(GTK_IS_COMPASS(widget));
425 compass=GTK_COMPASS(widget);
426 gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height);
430 gtk_compass_set_way_heading(GtkWidget *widget, gboolean valid, gfloat heading)
433 g_return_if_fail(GTK_IS_COMPASS(widget));
435 compass=GTK_COMPASS(widget);
437 compass->way_valid=valid;
438 compass->way_heading=heading;
440 gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height);
444 gtk_compass_set_dest_heading(GtkWidget *widget, gboolean valid, gfloat heading)
447 g_return_if_fail(GTK_IS_COMPASS(widget));
449 compass=GTK_COMPASS(widget);
451 compass->dest_valid=valid;
452 compass->dest_heading=heading;
454 compass=GTK_COMPASS(widget);
455 gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height);