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)
100 g_printf("%s()\n", __PRETTY_FUNCTION__);
111 gtk_gps_cb_button_press(GtkWidget * widget, GdkEventButton * event)
117 if (event->button==1) {
118 switch (gps->display_mode) {
119 case GTK_GPS_MODE_SKY:
120 gps->display_mode=GTK_GPS_MODE_SIGNAL;
122 case GTK_GPS_MODE_SIGNAL:
124 gps->display_mode=GTK_GPS_MODE_SKY;
128 gtk_gps_paint_by_mode(gps);
133 gtk_gps_new(GtkGpsMode display_mode, GpsData *data)
138 gps=gtk_type_new(gtk_gps_get_type ());
139 gps->display_mode=display_mode;
141 widget=GTK_WIDGET(gps);
142 g_signal_connect(G_OBJECT(widget), "button_press_event", G_CALLBACK(gtk_gps_cb_button_press), NULL);
144 gps->context=gtk_widget_get_pango_context(widget);
145 gps->layout=pango_layout_new(gps->context);
146 gps->fontdesc=pango_font_description_new();
147 pango_font_description_set_family(gps->fontdesc, "Sans Serif");
148 pango_font_description_set_size(gps->fontdesc, 10*PANGO_SCALE);
149 pango_layout_set_font_description(gps->layout, gps->fontdesc);
150 pango_layout_set_alignment(gps->layout, PANGO_ALIGN_CENTER);
152 return GTK_WIDGET(gps);
156 gtk_gps_finalize(GObject *object)
160 g_return_if_fail(GTK_IS_GPS(object));
163 if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) {
164 gtk_widget_unmap(GTK_WIDGET(object));
167 G_OBJECT_CLASS(gtk_gps_parent_class)->finalize(object);
171 gtk_gps_size_request(GtkWidget *widget, GtkRequisition *requisition)
175 g_printf("%s()\n", __PRETTY_FUNCTION__);
177 g_return_if_fail(GTK_IS_GPS(widget));
178 g_return_if_fail(requisition!=NULL);
180 gps = GTK_GPS (widget);
182 requisition->width=300;
183 requisition->height=300;
190 gtk_gps_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
194 g_return_if_fail(GTK_IS_GPS(widget));
195 g_return_if_fail(allocation!=NULL);
197 gps=GTK_GPS (widget);
199 widget->allocation = *allocation;
201 if (GTK_WIDGET_REALIZED (widget)) {
202 gdk_window_move_resize (widget->window,
203 allocation->x, allocation->y,
204 allocation->width, allocation->height);
207 gps->width=allocation->width;
208 gps->height=allocation->height;
209 gps->size=MIN(gps->width, gps->height);
211 gps->fs=gps->size/20;
214 pango_font_description_set_size(gps->fontdesc, gps->fs*PANGO_SCALE);
215 pango_layout_set_font_description(gps->layout, gps->fontdesc);
219 gtk_gps_realize (GtkWidget *widget)
223 GdkWindowAttr attributes;
224 gint attributes_mask;
226 g_return_if_fail (GTK_IS_GPS(widget));
227 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
230 attributes.x=widget->allocation.x;
231 attributes.y=widget->allocation.y;
232 attributes.width=widget->allocation.width;
233 attributes.height=widget->allocation.height;
234 attributes.wclass=GDK_INPUT_OUTPUT;
235 attributes.window_type=GDK_WINDOW_CHILD;
236 attributes.event_mask=gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK;
237 attributes.visual=gtk_widget_get_visual(widget);
238 attributes.colormap=gtk_widget_get_colormap(widget);
240 attributes_mask=GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
242 widget->window=gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, attributes_mask);
243 widget->style=gtk_style_attach(widget->style, widget->window);
245 gdk_window_set_user_data(widget->window, widget);
246 gtk_style_set_background(widget->style, widget->window, GTK_STATE_ACTIVE);
252 gps->gc1=gdk_gc_new(widget->window);
253 gdk_gc_set_rgb_fg_color(gps->gc1, &color);
260 gps->gc2=gdk_gc_new(widget->window);
261 gdk_gc_set_rgb_fg_color(gps->gc2, &color);
268 gps->gc3=gdk_gc_new(widget->window);
269 gdk_gc_set_rgb_fg_color(gps->gc3, &color);
274 gtk_gps_paint_sky(GtkGps *gps)
278 guint i, x, y, size, halfsize, xoffset, yoffset;
279 guint x1, y1, x0, y0;
282 guint line[12]={0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
284 g_printf("%s()\n", __PRETTY_FUNCTION__);
286 widget=GTK_WIDGET(gps);
288 g_return_if_fail(gps->data);
290 if (!GTK_WIDGET_MAPPED(widget))
298 if (gps->width>gps->height) {
299 xoffset=x0+(gps->width-gps->height-PADDING)/2;
303 yoffset=y0+(gps->height-gps->width-PADDING)/2;
306 gtk_paint_flat_box (widget->style,
308 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
309 NULL, widget, "trough", 0, 0,
310 gps->width, gps->height);
313 gdk_draw_arc(widget->window,
314 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
316 xoffset + 2, yoffset + 2, size - 4, size - 4, 0, 64 * 360);
319 gdk_draw_arc(widget->window,
320 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
322 xoffset + size / 6, yoffset + size / 6,
323 size / 6 * 4, size / 6 * 4, 0, 64 * 360);
326 gdk_draw_arc(widget->window,
327 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
329 xoffset + size / 6 * 2, yoffset + size / 6 * 2,
330 size / 6 * 2, size / 6 * 2, 0, 64 * 360);
332 for (i = 0; i < 6; i++) {
334 tmp = (line[i] * (1.f / 180.f)) * G_PI;
335 gdk_draw_line(widget->window,
336 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
337 xoffset + halfsize + (halfsize - 2) * sinf(tmp),
338 yoffset + halfsize - (halfsize - 2) * cosf(tmp),
339 xoffset + halfsize - (halfsize - 2) * sinf(tmp),
340 yoffset + halfsize + (halfsize - 2) * cosf(tmp));
344 for (i = 0; i < 12; i++) {
345 tmp = (line[i] * (1.f / 180.f)) * G_PI;
348 g_snprintf(buffer, 16, "N");
350 g_snprintf(buffer, 16, "%d°", line[i]);
351 pango_layout_set_text(gps->layout, buffer, -1);
352 pango_layout_get_pixel_size(gps->layout, &x, &y);
353 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
354 (xoffset + halfsize + (halfsize - size / 12) * sinf(tmp)) - x / 2,
355 (yoffset + halfsize - (halfsize - size / 12) * cosf(tmp)) - y / 2,
360 tmp = (30 * (1.f / 180.f)) * G_PI;
361 g_snprintf(buffer, 16, "30°");
362 pango_layout_set_text(gps->layout, buffer, -1);
363 pango_layout_get_pixel_size(gps->layout, &x, &y);
364 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
365 (xoffset + halfsize + size / 6 * 2 * sinf(tmp)) - x / 2,
366 (yoffset + halfsize - size / 6 * 2 * cosf(tmp)) - y / 2,
370 tmp = (30 * (1.f / 180.f)) * G_PI;
371 g_snprintf(buffer, 16, "60°");
372 pango_layout_set_text(gps->layout, buffer, -1);
373 pango_layout_get_pixel_size(gps->layout, &x, &y);
374 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
375 (xoffset + halfsize + size / 6 * sinf(tmp)) - x / 2,
376 (yoffset + halfsize - size / 6 * cosf(tmp)) - y / 2,
380 for (i=0;i<gps->data->satinview;i++) {
381 /* Sat used or not */
382 gc=(gps->data->sat[i].fix) ? gps->gc2 : gps->gc1;
384 tmp = (gps->data->sat[i].azimuth * (1.f / 180.f)) * G_PI;
385 x = xoffset + halfsize + (90 - gps->data->sat[i].elevation) * halfsize / 90 * sinf(tmp);
386 y = yoffset + halfsize - (90 - gps->data->sat[i].elevation) * halfsize / 90 * cosf(tmp);
388 gdk_draw_arc(widget->window, gc, TRUE, x-gps->fs, y-gps->fs, gps->fs+3, gps->fs+3, 0, 64 * 360);
390 g_snprintf(buffer, 6, "%02d", gps->data->sat[i].prn);
391 pango_layout_set_text(gps->layout, buffer, -1);
392 pango_layout_get_pixel_size(gps->layout, &x1, &y1);
393 gdk_draw_layout(widget->window, gps->gc3, x - x1 / 2, y - y1 / 2, gps->layout);
400 gtk_gps_paint_signals(GtkGps *gps)
404 guint step, i, snr_height, bymargin, xoffset, yoffset;
408 widget=GTK_WIDGET(gps);
410 if (!GTK_WIDGET_MAPPED(widget))
416 /* Bootom margin - 12% */
417 bymargin = gps->height * 0.88f;
420 gdk_draw_line(widget->window,
421 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
422 xoffset + 5, yoffset + bymargin,
423 xoffset + gps->width - 10 - 2, yoffset + bymargin);
424 gdk_draw_line(widget->window,
425 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
426 xoffset + 5, yoffset + bymargin - 1,
427 xoffset + gps->width - 10 - 2, yoffset + bymargin - 1);
429 if (gps->data->satinview > 0) {
430 /* Left margin - 5pix, Right margin - 5pix */
431 step = (gps->width - 10) / gps->data->satinview;
433 for (i = 0; i < gps->data->satinview; i++) {
434 /* Sat used or not */
435 gc=(gps->data->sat[i].fix) ? gps->gc1 : gps->gc2;
438 snr_height = gps->data->sat[i].snr * gps->height * 0.78f / 100;
439 y = gps->height * 0.1f + (gps->height * 0.78f - snr_height);
441 /* draw sat rectangle... */
442 gdk_draw_rectangle(widget->window,
445 yoffset + y, step - 2, snr_height);
447 if (gps->data->sat[i].snr > 0) {
449 g_snprintf(tmp, 32, "%02d", gps->data->sat[i].snr);
450 pango_layout_set_text(gps->layout, tmp, 2);
451 pango_layout_get_pixel_size(gps->layout, &x1, &y1);
452 gdk_draw_layout(widget->window,
453 widget->style->fg_gc[GTK_STATE_NORMAL],
454 xoffset + x + ((step - 2) - x1) / 2,
459 /* ...and sat number */
460 g_snprintf(tmp, 32, "%02d", gps->data->sat[i].prn);
461 pango_layout_set_text(gps->layout, tmp, 2);
462 pango_layout_get_pixel_size(gps->layout, &x1, &y1);
463 gdk_draw_layout(widget->window,
464 widget->style->fg_gc[GTK_STATE_NORMAL],
465 xoffset + x + ((step - 2) - x1) / 2,
466 yoffset + bymargin + 1,
475 gtk_gps_paint_by_mode(GtkGps *gps)
477 g_return_if_fail(GTK_IS_GPS(gps));
479 switch (gps->display_mode) {
480 case GTK_GPS_MODE_SIGNAL:
481 gtk_gps_paint_signals(gps);
483 case GTK_GPS_MODE_SKY:
485 gtk_gps_paint_sky(gps);
491 gtk_gps_expose(GtkWidget *widget, GdkEventExpose *event)
495 g_return_val_if_fail(GTK_IS_GPS(widget), FALSE);
496 g_return_val_if_fail(event != NULL, FALSE);
499 gtk_gps_paint_by_mode(gps);
504 gtk_gps_refresh(GtkWidget *widget)
508 g_return_if_fail(GTK_IS_GPS(widget));
511 gtk_gps_paint_by_mode(gps);