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 gint 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 gint gtk_gps_paint(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)
71 g_return_if_fail(GTK_IS_GPS(object));
75 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
81 gtk_gps_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
84 g_return_if_fail(GTK_IS_GPS(object));
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94 gtk_gps_init (GtkGps *gps)
96 g_printf("%s()\n", __PRETTY_FUNCTION__);
111 gps = gtk_type_new(gtk_gps_get_type ());
112 return GTK_WIDGET(gps);
116 gtk_gps_finalize(GObject *object)
120 g_printf("%s()\n", __PRETTY_FUNCTION__);
122 g_return_if_fail(GTK_IS_GPS(object));
125 if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) {
126 gtk_widget_unmap(GTK_WIDGET(object));
129 G_OBJECT_CLASS(gtk_gps_parent_class)->finalize(object);
133 gtk_gps_size_request(GtkWidget *widget, GtkRequisition *requisition)
137 g_printf("%s()\n", __PRETTY_FUNCTION__);
139 g_return_if_fail(GTK_IS_GPS(widget));
140 g_return_if_fail(requisition != NULL);
142 gps = GTK_GPS (widget);
144 requisition->width=400;
145 requisition->height=300;
147 gps->height=300 - 10;
151 gtk_gps_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
155 g_printf("%s()\n", __PRETTY_FUNCTION__);
157 g_return_if_fail(GTK_IS_GPS(widget));
158 g_return_if_fail(allocation!=NULL);
160 gps=GTK_GPS (widget);
162 widget->allocation = *allocation;
164 if (GTK_WIDGET_REALIZED (widget)) {
165 gdk_window_move_resize (widget->window,
166 allocation->x, allocation->y,
167 allocation->width, allocation->height);
170 gps->width=allocation->width-PADDING;
171 gps->height=allocation->height-PADDING;
175 gtk_gps_realize (GtkWidget *widget)
179 GdkWindowAttr attributes;
180 gint attributes_mask;
182 g_printf("%s()\n", __PRETTY_FUNCTION__);
184 g_return_if_fail (GTK_IS_GPS(widget));
185 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
188 attributes.x=widget->allocation.x;
189 attributes.y=widget->allocation.y;
190 attributes.width=widget->allocation.width;
191 attributes.height=widget->allocation.height;
192 attributes.wclass=GDK_INPUT_OUTPUT;
193 attributes.window_type=GDK_WINDOW_CHILD;
194 attributes.event_mask=gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK ;
195 attributes.visual=gtk_widget_get_visual(widget);
196 attributes.colormap=gtk_widget_get_colormap(widget);
198 attributes_mask=GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
200 widget->window=gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, attributes_mask);
201 widget->style=gtk_style_attach(widget->style, widget->window);
203 gdk_window_set_user_data(widget->window, widget);
204 gtk_style_set_background(widget->style, widget->window, GTK_STATE_ACTIVE);
206 gps->sat_details_context=gtk_widget_get_pango_context(widget);
207 gps->sat_details_layout=pango_layout_new(gps->sat_details_context);
208 gps->sat_details_fontdesc=pango_font_description_new();
209 pango_font_description_set_family(gps->sat_details_fontdesc, "Sans Serif");
210 pango_font_description_set_size(gps->sat_details_fontdesc, 12*PANGO_SCALE);
211 pango_layout_set_font_description(gps->sat_details_layout, gps->sat_details_fontdesc);
212 pango_layout_set_alignment(gps->sat_details_layout, PANGO_ALIGN_CENTER);
218 gps->gc1=gdk_gc_new(widget->window);
219 gdk_gc_set_rgb_fg_color(gps->gc1, &color);
226 gps->gc2=gdk_gc_new(widget->window);
227 gdk_gc_set_rgb_fg_color(gps->gc2, &color);
234 gps->gc3=gdk_gc_new(widget->window);
235 gdk_gc_set_rgb_fg_color(gps->gc3, &color);
240 gtk_gps_paint(GtkGps *gps)
244 guint i, j, x, y, size, halfsize, xoffset, yoffset;
245 guint x1, y1, x0, y0;
248 guint line[12]={0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
250 g_printf("%s()\n", __PRETTY_FUNCTION__);
252 widget=GTK_WIDGET(gps);
254 if (!GTK_WIDGET_MAPPED(widget))
260 size=MIN(gps->width, gps->height);
262 if (gps->width>gps->height) {
263 xoffset=x0+(gps->width-gps->height-PADDING)/2;
267 yoffset=y0+(gps->height-gps->width-PADDING)/2;
271 gdk_draw_arc(widget->window,
272 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
274 xoffset + 2, yoffset + 2, size - 4, size - 4, 0, 64 * 360);
277 gdk_draw_arc(widget->window,
278 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
280 xoffset + size / 6, yoffset + size / 6,
281 size / 6 * 4, size / 6 * 4, 0, 64 * 360);
284 gdk_draw_arc(widget->window,
285 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
287 xoffset + size / 6 * 2, yoffset + size / 6 * 2,
288 size / 6 * 2, size / 6 * 2, 0, 64 * 360);
290 for (i = 0; i < 6; i++) {
292 tmp = (line[i] * (1.f / 180.f)) * G_PI;
293 gdk_draw_line(widget->window,
294 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
295 xoffset + halfsize + (halfsize - 2) * sinf(tmp),
296 yoffset + halfsize - (halfsize - 2) * cosf(tmp),
297 xoffset + halfsize - (halfsize - 2) * sinf(tmp),
298 yoffset + halfsize + (halfsize - 2) * cosf(tmp));
301 for (i = 0; i < 12; i++) {
302 tmp = (line[i] * (1.f / 180.f)) * G_PI;
305 g_snprintf(buffer, 16, "N");
307 g_snprintf(buffer, 16, "%d°", line[i]);
308 pango_layout_set_text(gps->sat_details_layout, buffer, -1);
309 pango_layout_get_pixel_size(gps->sat_details_layout, &x, &y);
310 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
311 (xoffset + halfsize + (halfsize - size / 12) * sinf(tmp)) - x / 2,
312 (yoffset + halfsize - (halfsize - size / 12) * cosf(tmp)) - y / 2,
313 gps->sat_details_layout);
317 tmp = (30 * (1.f / 180.f)) * G_PI;
318 g_snprintf(buffer, 16, "30°");
319 pango_layout_set_text(gps->sat_details_layout, buffer, -1);
320 pango_layout_get_pixel_size(gps->sat_details_layout, &x, &y);
321 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
322 (xoffset + halfsize + size / 6 * 2 * sinf(tmp)) - x / 2,
323 (yoffset + halfsize - size / 6 * 2 * cosf(tmp)) - y / 2,
324 gps->sat_details_layout);
327 tmp = (30 * (1.f / 180.f)) * G_PI;
328 g_snprintf(buffer, 16, "60°");
329 pango_layout_set_text(gps->sat_details_layout, buffer, -1);
330 pango_layout_get_pixel_size(gps->sat_details_layout, &x, &y);
331 gdk_draw_layout(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
332 (xoffset + halfsize + size / 6 * sinf(tmp)) - x / 2,
333 (yoffset + halfsize - size / 6 * cosf(tmp)) - y / 2,
334 gps->sat_details_layout);
336 for (i=0;i<gps->satinview;i++) {
337 /* Sat used or not */
339 for (j=0;j<gps->satinuse;j++) {
340 if (gps->satforfix[j]==gps->gps_sat[i].prn) {
346 tmp = (gps->gps_sat[i].azimuth * (1.f / 180.f)) * G_PI;
347 x = xoffset + halfsize + (90 - gps->gps_sat[i].elevation) * halfsize / 90 * sinf(tmp);
348 y = yoffset + halfsize - (90 - gps->gps_sat[i].elevation) * halfsize / 90 * cosf(tmp);
350 gdk_draw_arc(widget->window, gc, TRUE, x - 10, y - 10, 20, 20, 0, 64 * 360);
352 g_snprintf(buffer, 6, "%02d", gps->gps_sat[i].prn);
353 pango_layout_set_text(gps->sat_details_layout, buffer, -1);
354 pango_layout_get_pixel_size(gps->sat_details_layout, &x1, &y1);
355 gdk_draw_layout(widget->window, gps->gc3, x - x1 / 2, y - y1 / 2, gps->sat_details_layout);
358 g_printf("%s(): return\n", __PRETTY_FUNCTION__);
363 gtk_gps_expose(GtkWidget *widget, GdkEventExpose *event)
365 g_return_val_if_fail(GTK_IS_GPS(widget), FALSE);
366 g_return_val_if_fail(event != NULL, FALSE);
368 gtk_gps_paint(GTK_GPS(widget));
373 gtk_gps_set_satellite_data(GtkWidget *widget, guint sat, gboolean fix, guint prn, guint elevation, guint azimuth, guint snr)
377 g_printf("%s()\n", __PRETTY_FUNCTION__);
378 g_return_if_fail(GTK_IS_GPS (widget));
385 BOUND(elevation, 0, 90);
386 BOUND(azimuth, 0, 360);
388 gps->gps_sat[sat].prn=prn;
389 gps->gps_sat[sat].elevation=elevation;
390 gps->gps_sat[sat].azimuth=azimuth;
391 gps->gps_sat[sat].snr=snr;
393 gps->satforfix[sat]=prn;
395 gps->satforfix[sat]=-1;
396 g_printf("%s(): return\n", __PRETTY_FUNCTION__);
400 gtk_gps_set_satellite_in_view(GtkWidget *widget, guint sat)
404 g_printf("%s()\n", __PRETTY_FUNCTION__);
405 g_return_if_fail(GTK_IS_GPS(widget));
415 g_printf("%s(): return\n", __PRETTY_FUNCTION__);