3 * GtkGps: Display GPS information
4 * Copyright (C) 2007 Kaj-Michael Lang
5 * Originanl non-widget version Copyright Cezary Jackiewicz
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <glib/gstdio.h>
25 #include <glib-object.h>
28 static void gtk_gps_finalize (GObject *object);
29 static void gtk_gps_size_request (GtkWidget *widget, GtkRequisition *requisition);
30 static void gtk_gps_size_allocate (GtkWidget *widget, GtkAllocation *allocate);
31 static gboolean gtk_gps_expose (GtkWidget *widget, GdkEventExpose *event);
32 static void gtk_gps_realize (GtkWidget *widget);
33 static void gtk_gps_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
34 static void gtk_gps_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
35 static void gtk_gps_paint_by_mode(GtkGps *gps);
37 G_DEFINE_TYPE(GtkGps, gtk_gps, GTK_TYPE_WIDGET);
41 #define BOUND(x, a, b) { \
49 gtk_gps_class_init(GtkGpsClass *class)
51 GObjectClass *object_class;
52 GtkWidgetClass *widget_class;
54 object_class = (GObjectClass*) class;
55 widget_class = (GtkWidgetClass*) class;
57 object_class->finalize = gtk_gps_finalize;
58 object_class->set_property = gtk_gps_set_property;
59 object_class->get_property = gtk_gps_get_property;
61 widget_class->size_request = gtk_gps_size_request;
62 widget_class->expose_event = gtk_gps_expose;
63 widget_class->realize = gtk_gps_realize;
64 widget_class->size_allocate = gtk_gps_size_allocate;
68 gtk_gps_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
72 g_return_if_fail(GTK_IS_GPS(object));
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
83 gtk_gps_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
87 g_return_if_fail(GTK_IS_GPS(object));
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98 gtk_gps_init(GtkGps *gps)
109 gtk_gps_cb_button_press(GtkWidget * widget, GdkEventButton * event)
115 if (event->button==1) {
116 switch (gps->display_mode) {
117 case GTK_GPS_MODE_SKY:
118 gps->display_mode=GTK_GPS_MODE_SIGNAL;
120 case GTK_GPS_MODE_SIGNAL:
122 gps->display_mode=GTK_GPS_MODE_SKY;
126 gtk_widget_queue_draw_area(widget, 0, 0, gps->width, gps->height);
131 gtk_gps_new(GtkGpsMode display_mode, GpsData *data)
136 gps=gtk_type_new(gtk_gps_get_type ());
137 gps->display_mode=display_mode;
139 widget=GTK_WIDGET(gps);
140 g_signal_connect(G_OBJECT(widget), "button_press_event", G_CALLBACK(gtk_gps_cb_button_press), NULL);
142 gps->context=gtk_widget_get_pango_context(widget);
143 gps->layout=pango_layout_new(gps->context);
144 gps->fontdesc=pango_font_description_new();
145 pango_font_description_set_family(gps->fontdesc, "Sans Serif");
146 pango_font_description_set_size(gps->fontdesc, 10*PANGO_SCALE);
147 pango_layout_set_font_description(gps->layout, gps->fontdesc);
148 pango_layout_set_alignment(gps->layout, PANGO_ALIGN_CENTER);
150 return GTK_WIDGET(gps);
154 gtk_gps_finalize(GObject *object)
158 g_return_if_fail(GTK_IS_GPS(object));
161 if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) {
162 gtk_widget_unmap(GTK_WIDGET(object));
165 G_OBJECT_CLASS(gtk_gps_parent_class)->finalize(object);
169 gtk_gps_size_request(GtkWidget *widget, GtkRequisition *requisition)
173 g_return_if_fail(GTK_IS_GPS(widget));
174 g_return_if_fail(requisition!=NULL);
176 gps = GTK_GPS (widget);
178 requisition->width=300;
179 requisition->height=300;
186 gtk_gps_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
190 g_return_if_fail(GTK_IS_GPS(widget));
191 g_return_if_fail(allocation!=NULL);
193 gps=GTK_GPS (widget);
195 widget->allocation = *allocation;
197 if (GTK_WIDGET_REALIZED (widget)) {
198 gdk_window_move_resize (widget->window,
199 allocation->x, allocation->y,
200 allocation->width, allocation->height);
203 gps->width=allocation->width;
204 gps->height=allocation->height;
205 gps->size=MIN(gps->width, gps->height);
207 gps->fs=gps->size/22;
208 BOUND(gps->fs, 10, 24);
210 pango_font_description_set_size(gps->fontdesc, (gps->fs-3)*PANGO_SCALE);
211 pango_layout_set_font_description(gps->layout, gps->fontdesc);
215 gtk_gps_realize (GtkWidget *widget)
219 GdkWindowAttr attributes;
220 gint attributes_mask;
222 g_return_if_fail (GTK_IS_GPS(widget));
223 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
226 attributes.x=widget->allocation.x;
227 attributes.y=widget->allocation.y;
228 attributes.width=widget->allocation.width;
229 attributes.height=widget->allocation.height;
230 attributes.wclass=GDK_INPUT_OUTPUT;
231 attributes.window_type=GDK_WINDOW_CHILD;
232 attributes.event_mask=gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK;
233 attributes.visual=gtk_widget_get_visual(widget);
234 attributes.colormap=gtk_widget_get_colormap(widget);
236 attributes_mask=GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
238 widget->window=gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, attributes_mask);
239 widget->style=gtk_style_attach(widget->style, widget->window);
241 gdk_window_set_user_data(widget->window, widget);
242 gtk_style_set_background(widget->style, widget->window, GTK_STATE_ACTIVE);
248 gps->gc_s=gdk_gc_new(widget->window);
249 gdk_gc_set_rgb_fg_color(gps->gc_s, &color);
256 gps->gc_sf=gdk_gc_new(widget->window);
257 gdk_gc_set_rgb_fg_color(gps->gc_sf, &color);
264 gps->gc_w=gdk_gc_new(widget->window);
265 gdk_gc_set_rgb_fg_color(gps->gc_w, &color);
270 gtk_gps_paint_sky(GtkGps *gps)
274 guint i, x, y, size, halfsize, xoffset, yoffset;
275 guint x1, y1, x0, y0;
278 guint line[12]={0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
280 widget=GTK_WIDGET(gps);
282 g_return_if_fail(gps->data);
284 if (!GTK_WIDGET_MAPPED(widget))
290 size=gps->size-PADDING;
292 if (gps->width>gps->height) {
293 xoffset=x0+(gps->width-gps->height)/2;
297 yoffset=y0+(gps->height-gps->width)/2;
301 gdk_draw_arc(widget->window,
302 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
304 xoffset + 2, yoffset + 2, size - 4, size - 4, 0, 64 * 360);
307 gdk_draw_arc(widget->window,
308 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
310 xoffset + size / 6, yoffset + size / 6,
311 size / 6 * 4, size / 6 * 4, 0, 64 * 360);
314 gdk_draw_arc(widget->window,
315 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
317 xoffset + size / 6 * 2, yoffset + size / 6 * 2,
318 size / 6 * 2, size / 6 * 2, 0, 64 * 360);
320 for (i = 0; i < 12; i++) {
323 tmp = (line[i] * (1.f / 180.f)) * G_PI;
327 gdk_draw_line(widget->window,
328 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
329 xoffset + halfsize + (halfsize - 2) * sint,
330 yoffset + halfsize - (halfsize - 2) * cost,
331 xoffset + halfsize - (halfsize - 2) * sint,
332 yoffset + halfsize + (halfsize - 2) * cost);
334 /* Skip angle text if there is no space */
339 g_snprintf(buffer, 16, "N");
341 g_snprintf(buffer, 16, "%d°", line[i]);
342 pango_layout_set_text(gps->layout, buffer, -1);
343 pango_layout_get_pixel_size(gps->layout, &x, &y);
344 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
345 (xoffset + halfsize + (halfsize - size / 12) * sint) - x / 2,
346 (yoffset + halfsize - (halfsize - size / 12) * cost) - y / 2,
352 tmp = (30 * (1.f / 180.f)) * G_PI;
354 g_snprintf(buffer, 16, "30°");
355 pango_layout_set_text(gps->layout, buffer, -1);
356 pango_layout_get_pixel_size(gps->layout, &x, &y);
357 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
358 (xoffset + halfsize + size / 6 * 2 * sinf(tmp)) - x / 2,
359 (yoffset + halfsize - size / 6 * 2 * cosf(tmp)) - y / 2,
363 g_snprintf(buffer, 16, "60°");
364 pango_layout_set_text(gps->layout, buffer, -1);
365 pango_layout_get_pixel_size(gps->layout, &x, &y);
366 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
367 (xoffset + halfsize + size / 6 * sinf(tmp)) - x / 2,
368 (yoffset + halfsize - size / 6 * cosf(tmp)) - y / 2,
372 for (i=0;i<gps->data->satinview;i++) {
373 /* Sat used or not */
374 gc=(gps->data->sat[i].fix==TRUE) ? gps->gc_sf : gps->gc_s;
376 tmp = (gps->data->sat[i].azimuth * (1.f / 180.f)) * G_PI;
377 x = xoffset + halfsize + (90 - gps->data->sat[i].elevation) * halfsize / 90 * sinf(tmp);
378 y = yoffset + halfsize - (90 - gps->data->sat[i].elevation) * halfsize / 90 * cosf(tmp);
380 gdk_draw_arc(widget->window, gc, TRUE, x-gps->fs, y-gps->fs, gps->fs+8, gps->fs+8, 0, 64 * 360);
382 g_snprintf(buffer, 6, "%02d", gps->data->sat[i].prn);
383 pango_layout_set_text(gps->layout, buffer, -1);
384 pango_layout_get_pixel_size(gps->layout, &x1, &y1);
385 gdk_draw_layout(widget->window, gps->gc_w, x-gps->fs/2-x1/2+2, y-gps->fs/2-y1/2+2, gps->layout);
392 gtk_gps_paint_signals(GtkGps *gps)
396 guint step, i, snr_height, bymargin, xoffset, yoffset;
400 widget=GTK_WIDGET(gps);
402 if (!GTK_WIDGET_MAPPED(widget))
408 /* Bootom margin - 12% */
409 bymargin = gps->height * 0.88f;
412 gdk_draw_line(widget->window,
413 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
414 xoffset + 5, yoffset + bymargin,
415 xoffset + gps->width - 10 - 2, yoffset + bymargin);
416 gdk_draw_line(widget->window,
417 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
418 xoffset + 5, yoffset + bymargin - 1,
419 xoffset + gps->width - 10 - 2, yoffset + bymargin - 1);
421 if (gps->data->satinview > 0) {
422 /* Left margin - 5pix, Right margin - 5pix */
423 step = (gps->width - 10) / gps->data->satinview;
425 for (i = 0; i < gps->data->satinview; i++) {
426 /* Sat used or not */
427 gc=(gps->data->sat[i].fix) ? gps->gc_sf : gps->gc_s;
430 snr_height = gps->data->sat[i].snr * gps->height * 0.78f / 100;
431 y = gps->height * 0.1f + (gps->height * 0.78f - snr_height);
433 /* draw sat rectangle... */
434 gdk_draw_rectangle(widget->window,
437 yoffset + y, step - 2, snr_height);
439 if (gps->data->sat[i].snr > 0) {
441 g_snprintf(tmp, 32, "%02d", gps->data->sat[i].snr);
442 pango_layout_set_text(gps->layout, tmp, 2);
443 pango_layout_get_pixel_size(gps->layout, &x1, &y1);
444 gdk_draw_layout(widget->window,
445 widget->style->fg_gc[GTK_STATE_NORMAL],
446 xoffset + x + ((step - 2) - x1) / 2,
451 /* ...and sat number */
452 g_snprintf(tmp, 32, "%02d", gps->data->sat[i].prn);
453 pango_layout_set_text(gps->layout, tmp, 2);
454 pango_layout_get_pixel_size(gps->layout, &x1, &y1);
455 gdk_draw_layout(widget->window,
456 widget->style->fg_gc[GTK_STATE_NORMAL],
457 xoffset + x + ((step - 2) - x1) / 2,
458 yoffset + bymargin + 1,
467 gtk_gps_paint_by_mode(GtkGps *gps)
469 g_return_if_fail(GTK_IS_GPS(gps));
471 switch (gps->display_mode) {
472 case GTK_GPS_MODE_SIGNAL:
473 gtk_gps_paint_signals(gps);
475 case GTK_GPS_MODE_SKY:
477 gtk_gps_paint_sky(gps);
483 gtk_gps_expose(GtkWidget *widget, GdkEventExpose *event)
487 g_return_val_if_fail(GTK_IS_GPS(widget), FALSE);
488 g_return_val_if_fail(event != NULL, FALSE);
491 gtk_gps_paint_by_mode(gps);
496 gtk_gps_refresh(GtkWidget *widget)
500 g_return_if_fail(GTK_IS_GPS(widget));
503 gtk_widget_queue_draw_area(widget, 0, 0, gps->width, gps->height);