Parent Directory | Revision Log
Initial import
1 | harbaum | 1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. | ||
3 | * | ||
4 | * This file is part of GPXView. | ||
5 | * | ||
6 | * GPXView is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation, either version 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * GPXView is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with GPXView. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #include <stdio.h> | ||
21 | #include <string.h> | ||
22 | #include <math.h> | ||
23 | |||
24 | #include "gpxview.h" | ||
25 | |||
26 | #ifdef USE_MAEMO | ||
27 | #include "dbus.h" | ||
28 | #endif | ||
29 | |||
30 | #define COMPASS_SIZE 230 | ||
31 | #define LETTER_SPACE 0.2 | ||
32 | #define DIAMOND_WIDTH 0.05 | ||
33 | #define DIAMOND_HEIGHT 0.2 | ||
34 | #define ARROW_WIDTH 0.3 | ||
35 | #define ARROW_LENGTH 0.7 | ||
36 | #define UPDATE_MS 1000 /* gps updates are sent once a second */ | ||
37 | |||
38 | #define SAT_WIDTH 330 | ||
39 | #define SAT_HEIGHT 60 | ||
40 | |||
41 | /* http://www.gtk.org/tutorial1.2/gtk_tut-30.html */ | ||
42 | /* http://developer.gimp.org/api/2.0/gdk/gdk-Drawing-Primitives.html */ | ||
43 | |||
44 | |||
45 | static float rad2deg(float rad) { | ||
46 | return fmodf(360.0 + (180.0/M_PI) * rad, 360.0); | ||
47 | } | ||
48 | |||
49 | static void compass_draw(GtkWidget *widget, cache_context_t *context) { | ||
50 | float f; | ||
51 | int i; | ||
52 | |||
53 | gint width = widget->allocation.width; | ||
54 | gint height = widget->allocation.height; | ||
55 | gint diameter = (height < width)?height:width; | ||
56 | |||
57 | gint xcenter = width/2; | ||
58 | gint ycenter = height/2; | ||
59 | gint radius = diameter/2; | ||
60 | |||
61 | /* erase background */ | ||
62 | gdk_draw_rectangle(context->gotoc.compass_pixmap, | ||
63 | widget->style->bg_gc[GTK_STATE_NORMAL], TRUE, | ||
64 | 0, 0, width, height); | ||
65 | |||
66 | /* use white rosetta background unless the real background */ | ||
67 | /* is very bright. Use bright grey then */ | ||
68 | |||
69 | if(widget->style->bg[GTK_STATE_NORMAL].red + | ||
70 | widget->style->bg[GTK_STATE_NORMAL].green + | ||
71 | widget->style->bg[GTK_STATE_NORMAL].blue < 3*60000) { | ||
72 | /* background is very bright, use white */ | ||
73 | gdk_draw_arc(context->gotoc.compass_pixmap, widget->style->white_gc, TRUE, | ||
74 | (width-diameter)/2, (height-diameter)/2, diameter, diameter, | ||
75 | 0, 360*64); | ||
76 | } else { | ||
77 | GdkGC *lgrey_gc = gdk_gc_new(context->gotoc.compass_pixmap); | ||
78 | gdk_gc_copy(lgrey_gc, widget->style->black_gc); | ||
79 | GdkColor lgrey_color; | ||
80 | gdk_color_parse("#DDDDDD", &lgrey_color); | ||
81 | gdk_gc_set_rgb_fg_color(lgrey_gc, &lgrey_color); | ||
82 | |||
83 | gdk_draw_arc(context->gotoc.compass_pixmap, lgrey_gc, TRUE, | ||
84 | (width-diameter)/2, (height-diameter)/2, diameter, diameter, | ||
85 | 0, 360*64); | ||
86 | } | ||
87 | |||
88 | /* draw the locked/unlocked icon */ | ||
89 | gdk_draw_pixbuf(context->gotoc.compass_pixmap, | ||
90 | widget->style->fg_gc[GTK_STATE_NORMAL], | ||
91 | icon_get(ICON_MISC, context->appdata->compass_locked?2:3), | ||
92 | 0, 0, (width-diameter)/2 + diameter/32, | ||
93 | (height+diameter)/2 - 16 - diameter/32 , 16, 16, | ||
94 | GDK_RGB_DITHER_NONE,0,0); | ||
95 | |||
96 | /* don't update heading if the compass is locked */ | ||
97 | if(!context->appdata->compass_locked) { | ||
98 | int i, valid = 0, cnt=0; | ||
99 | double x_sum = 0.0, y_sum = 0.0; | ||
100 | |||
101 | /* shift heading buffer up one entry and add new value at entry 0 */ | ||
102 | for(i=MAX_AVERAGE-1;i>0;i--) | ||
103 | context->gotoc.head_avg[i] = context->gotoc.head_avg[i-1]; | ||
104 | context->gotoc.head_avg[0] = gps_get_heading(context->appdata)*M_PI/180.0; | ||
105 | |||
106 | // printf("Damping = %d\n", context->appdata->compass_damping); | ||
107 | // printf("add heading %f\n", rad2deg(context->gotoc.head_avg[0])); | ||
108 | |||
109 | /* determine number of valid entries */ | ||
110 | for(i=0;i<MAX_AVERAGE && valid<context->appdata->compass_damping;i++) | ||
111 | if(!isnan(context->gotoc.head_avg[i])) | ||
112 | valid++; | ||
113 | |||
114 | /* map back to angle if at least one value has been added */ | ||
115 | if(valid) { | ||
116 | /* map all valid antries onto a circle */ | ||
117 | for(i=0;i<MAX_AVERAGE && cnt<context->appdata->compass_damping;i++) { | ||
118 | if(!isnan(context->gotoc.head_avg[i])) { | ||
119 | float weight = 1.0 - ((float)(cnt++)/(float)valid); | ||
120 | printf("weight = %f * %f\n", | ||
121 | weight, rad2deg(context->gotoc.head_avg[i])); | ||
122 | x_sum += weight * sin(context->gotoc.head_avg[i]); | ||
123 | y_sum += weight * cos(context->gotoc.head_avg[i]); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | printf("%d valid heading entries\n", valid); | ||
128 | context->gotoc.heading = atan2(x_sum, y_sum); | ||
129 | printf("averaged heading: %f\n", | ||
130 | rad2deg(context->gotoc.heading)); | ||
131 | } else { | ||
132 | // printf("no valid heading, keeping old value heading\n"); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | if(!isnan(context->gotoc.heading)) { | ||
137 | |||
138 | for(i=0,f=0;f<2*M_PI-M_PI/8;f+=M_PI/4,i++) { | ||
139 | float ang = f - context->gotoc.heading; | ||
140 | |||
141 | if(!(i&1)) { | ||
142 | /* draw diamonds */ | ||
143 | GdkPoint diamond[4]; | ||
144 | |||
145 | #define OUT (1.0-LETTER_SPACE) | ||
146 | |||
147 | diamond[0].x = xcenter + radius * OUT * sin(ang); | ||
148 | diamond[0].y = ycenter + radius * OUT * -cos(ang); | ||
149 | |||
150 | diamond[1].x = xcenter + radius * (OUT-DIAMOND_HEIGHT/2) * | ||
151 | sin(ang+DIAMOND_WIDTH); | ||
152 | diamond[1].y = ycenter + radius * (OUT-DIAMOND_HEIGHT/2)* | ||
153 | -cos(ang+DIAMOND_WIDTH); | ||
154 | |||
155 | diamond[2].x = xcenter + radius * (OUT-DIAMOND_HEIGHT) * sin(ang); | ||
156 | diamond[2].y = ycenter + radius * (OUT-DIAMOND_HEIGHT) * -cos(ang); | ||
157 | |||
158 | diamond[3].x = xcenter + radius * (OUT-DIAMOND_HEIGHT/2) * | ||
159 | sin(ang-DIAMOND_WIDTH); | ||
160 | diamond[3].y = ycenter + radius * (OUT-DIAMOND_HEIGHT/2) * | ||
161 | -cos(ang-DIAMOND_WIDTH); | ||
162 | |||
163 | gdk_draw_polygon(context->gotoc.compass_pixmap, | ||
164 | widget->style->black_gc, TRUE, | ||
165 | diamond, sizeof(diamond)/sizeof(GdkPoint)); | ||
166 | |||
167 | const char *str[] = { "N", "E", "S", "W" }; | ||
168 | PangoLayout *layout = gtk_widget_create_pango_layout(widget, _(str[i/2])); | ||
169 | int tw, th; | ||
170 | pango_layout_get_pixel_size(layout, &tw, &th); | ||
171 | |||
172 | gdk_draw_layout(context->gotoc.compass_pixmap, widget->style->black_gc, | ||
173 | xcenter + radius * (1.0-LETTER_SPACE/2)*sin(ang) - tw/2, | ||
174 | ycenter + radius * (1.0-LETTER_SPACE/2)*-cos(ang) - th/2, | ||
175 | layout); | ||
176 | |||
177 | g_object_unref(layout); | ||
178 | } else | ||
179 | gdk_draw_line(context->gotoc.compass_pixmap, widget->style->black_gc, | ||
180 | xcenter + radius * 0.9 * sin(ang), | ||
181 | ycenter + radius * 0.9 * -cos(ang), | ||
182 | xcenter + radius * 1.0 * sin(ang), | ||
183 | ycenter + radius * 1.0 * -cos(ang)); | ||
184 | } | ||
185 | |||
186 | /* draw arrow */ | ||
187 | |||
188 | /* setup required colors */ | ||
189 | GdkGC *arrow_gc = gdk_gc_new(context->gotoc.compass_pixmap); | ||
190 | gdk_gc_copy(arrow_gc, widget->style->black_gc); | ||
191 | GdkColor arrow_color; | ||
192 | gdk_color_parse("#800000", &arrow_color); | ||
193 | gdk_gc_set_rgb_fg_color(arrow_gc, &arrow_color); | ||
194 | |||
195 | GdkPoint arrow[4]; | ||
196 | |||
197 | pos_t *pos = gps_get_pos(context->appdata); | ||
198 | if(pos) { | ||
199 | context->appdata->gps = *pos; /* save position */ | ||
200 | |||
201 | float arot = | ||
202 | gpx_pos_get_bearing(*pos, context->gotoc.pos) * | ||
203 | M_PI/180 - context->gotoc.heading; | ||
204 | |||
205 | arrow[0].x = xcenter + radius * ARROW_LENGTH * sin(arot); | ||
206 | arrow[0].y = ycenter + radius * ARROW_LENGTH * -cos(arot); | ||
207 | |||
208 | arrow[1].x = xcenter + radius * -ARROW_LENGTH * sin(arot+ARROW_WIDTH); | ||
209 | arrow[1].y = ycenter + radius * -ARROW_LENGTH * -cos(arot+ARROW_WIDTH); | ||
210 | |||
211 | arrow[2].x = xcenter + radius * -0.5 * ARROW_LENGTH * sin(arot); | ||
212 | arrow[2].y = ycenter + radius * -0.5 * ARROW_LENGTH * -cos(arot); | ||
213 | |||
214 | arrow[3].x = xcenter + radius * -ARROW_LENGTH * sin(arot-ARROW_WIDTH); | ||
215 | arrow[3].y = ycenter + radius * -ARROW_LENGTH * -cos(arot-ARROW_WIDTH); | ||
216 | |||
217 | gdk_draw_polygon(context->gotoc.compass_pixmap, arrow_gc, TRUE, | ||
218 | arrow, sizeof(arrow)/sizeof(GdkPoint)); | ||
219 | } else { | ||
220 | PangoLayout *layout; | ||
221 | |||
222 | if(context->appdata->use_gps) | ||
223 | layout = gtk_widget_create_pango_layout(widget, _("No fix")); | ||
224 | else | ||
225 | layout = gtk_widget_create_pango_layout(widget, _("GPS disabled")); | ||
226 | |||
227 | int tw, th; | ||
228 | pango_layout_get_pixel_size(layout, &tw, &th); | ||
229 | |||
230 | gdk_draw_layout(context->gotoc.compass_pixmap, widget->style->black_gc, | ||
231 | xcenter - tw/2, ycenter - th/2, layout); | ||
232 | |||
233 | g_object_unref(layout); | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | |||
238 | /* Create a new backing pixmap of the appropriate size */ | ||
239 | static gint compass_configure_event(GtkWidget *widget, | ||
240 | GdkEventConfigure *event, gpointer data) { | ||
241 | cache_context_t *context = (cache_context_t*)data; | ||
242 | |||
243 | if(context->gotoc.compass_pixmap) | ||
244 | gdk_pixmap_unref(context->gotoc.compass_pixmap); | ||
245 | |||
246 | context->gotoc.compass_pixmap = gdk_pixmap_new(widget->window, | ||
247 | widget->allocation.width, | ||
248 | widget->allocation.height, | ||
249 | -1); | ||
250 | compass_draw(widget, context); | ||
251 | // goto_update(context); | ||
252 | |||
253 | return TRUE; | ||
254 | } | ||
255 | |||
256 | /* Redraw the screen from the backing pixmap */ | ||
257 | static gint compass_expose_event(GtkWidget *widget, GdkEventExpose *event, | ||
258 | gpointer data) { | ||
259 | cache_context_t *context = (cache_context_t*)data; | ||
260 | |||
261 | gdk_draw_pixmap(widget->window, | ||
262 | widget->style->fg_gc[GTK_WIDGET_STATE(widget)], | ||
263 | context->gotoc.compass_pixmap, | ||
264 | event->area.x, event->area.y, | ||
265 | event->area.x, event->area.y, | ||
266 | event->area.width, event->area.height); | ||
267 | |||
268 | return FALSE; | ||
269 | } | ||
270 | |||
271 | static void sat_draw(GtkWidget *widget, cache_context_t *context) { | ||
272 | gint width = widget->allocation.width; | ||
273 | gint height = widget->allocation.height; | ||
274 | |||
275 | /* erase background */ | ||
276 | gdk_draw_rectangle(context->gotoc.sat_pixmap, | ||
277 | widget->style->bg_gc[GTK_STATE_NORMAL], TRUE, | ||
278 | 0, 0, width, height); | ||
279 | |||
280 | gps_sat_t *sat = gps_get_sats(context->appdata); | ||
281 | if(sat && sat->num) { | ||
282 | /* setup required colors */ | ||
283 | GdkGC *used_gc = gdk_gc_new(context->gotoc.sat_pixmap); | ||
284 | gdk_gc_copy(used_gc, widget->style->black_gc); | ||
285 | GdkColor used_color; | ||
286 | gdk_color_parse("#008000", &used_color); // green | ||
287 | gdk_gc_set_rgb_fg_color(used_gc, &used_color); | ||
288 | |||
289 | #define SAT_SPACING 3 | ||
290 | int i, x; | ||
291 | int swid = (width-SAT_SPACING*(sat->num-1))/sat->num; | ||
292 | |||
293 | #if 0 | ||
294 | int max_ss = 1; | ||
295 | for(i=0;i<sat->num;i++) | ||
296 | if(sat->ss[i] > max_ss) max_ss = sat->ss[i]; | ||
297 | |||
298 | printf("max_ss = %d dB\n", max_ss); | ||
299 | #else | ||
300 | /* as of xgps, a ss of 40 and more is "plenty" */ | ||
301 | int max_ss = 40; | ||
302 | for(i=0;i<sat->num;i++) | ||
303 | if(sat->ss[i] > max_ss) sat->ss[i] = max_ss; | ||
304 | #endif | ||
305 | |||
306 | if(swid > 40) { | ||
307 | swid = 40; | ||
308 | x = (width-sat->num*swid)/2; | ||
309 | } else | ||
310 | x = 0; | ||
311 | |||
312 | for(i=0;i<sat->num;i++) { | ||
313 | char str[32]; | ||
314 | #ifdef USE_MAEMO | ||
315 | snprintf(str, sizeof(str), "<span size=\"small\">%d</span>", sat->PRN[i]); | ||
316 | PangoLayout *layout = gtk_widget_create_pango_layout(widget, NULL); | ||
317 | pango_layout_set_markup(layout, str, strlen(str)); | ||
318 | #else | ||
319 | snprintf(str, sizeof(str), "%d", sat->PRN[i]); | ||
320 | PangoLayout *layout = gtk_widget_create_pango_layout(widget, str); | ||
321 | #endif | ||
322 | |||
323 | int tw, th; | ||
324 | pango_layout_get_pixel_size(layout, &tw, &th); | ||
325 | gdk_draw_layout(context->gotoc.sat_pixmap, widget->style->black_gc, | ||
326 | x + swid/2 - tw/2, height - th, layout); | ||
327 | |||
328 | g_object_unref(layout); | ||
329 | |||
330 | int h = (height-th) * sat->ss[i] / max_ss; | ||
331 | |||
332 | gdk_draw_rectangle(context->gotoc.sat_pixmap, | ||
333 | sat->used[i]?used_gc:widget->style->black_gc, | ||
334 | TRUE, | ||
335 | x, height-h-th, swid, h); | ||
336 | |||
337 | x += SAT_SPACING+swid; | ||
338 | } | ||
339 | |||
340 | /* free sat infos */ | ||
341 | free(sat->PRN); | ||
342 | free(sat->ss); | ||
343 | free(sat->used); | ||
344 | } else { | ||
345 | PangoLayout *layout = gtk_widget_create_pango_layout(widget, _("No SAT info")); | ||
346 | int tw, th; | ||
347 | pango_layout_get_pixel_size(layout, &tw, &th); | ||
348 | gdk_draw_layout(context->gotoc.sat_pixmap, widget->style->black_gc, | ||
349 | (width - tw)/2, (height - th)/2, layout); | ||
350 | |||
351 | g_object_unref(layout); | ||
352 | } | ||
353 | |||
354 | /* free the sat structure */ | ||
355 | if(sat) | ||
356 | free(sat); | ||
357 | } | ||
358 | |||
359 | /* Create a new backing pixmap of the appropriate size */ | ||
360 | static gint sat_configure_event(GtkWidget *widget, GdkEventConfigure *event, | ||
361 | gpointer data) { | ||
362 | cache_context_t *context = (cache_context_t*)data; | ||
363 | |||
364 | if(context->gotoc.sat_pixmap) | ||
365 | gdk_pixmap_unref(context->gotoc.sat_pixmap); | ||
366 | |||
367 | context->gotoc.sat_pixmap = gdk_pixmap_new(widget->window, | ||
368 | widget->allocation.width, | ||
369 | widget->allocation.height, | ||
370 | -1); | ||
371 | sat_draw(widget, context); | ||
372 | |||
373 | return TRUE; | ||
374 | } | ||
375 | |||
376 | /* Redraw the screen from the backing pixmap */ | ||
377 | static gint sat_expose_event(GtkWidget *widget, GdkEventExpose *event, | ||
378 | gpointer data) { | ||
379 | cache_context_t *context = (cache_context_t*)data; | ||
380 | |||
381 | gdk_draw_pixmap(widget->window, | ||
382 | widget->style->fg_gc[GTK_WIDGET_STATE(widget)], | ||
383 | context->gotoc.sat_pixmap, | ||
384 | event->area.x, event->area.y, | ||
385 | event->area.x, event->area.y, | ||
386 | event->area.width, event->area.height); | ||
387 | |||
388 | return FALSE; | ||
389 | } | ||
390 | |||
391 | gint goto_destroy_event(GtkWidget *widget, gpointer data ) { | ||
392 | cache_context_t *context = (cache_context_t*)data; | ||
393 | |||
394 | printf("destroying goto view\n"); | ||
395 | |||
396 | /* stop timer */ | ||
397 | if(context->gotoc.handler_id) | ||
398 | gtk_timeout_remove(context->gotoc.handler_id); | ||
399 | |||
400 | return FALSE; | ||
401 | } | ||
402 | |||
403 | static gboolean goto_update(gpointer data) { | ||
404 | cache_context_t *context = (cache_context_t*)data; | ||
405 | |||
406 | if(context->gotoc.sat_pixmap) { | ||
407 | static int sub = 0; | ||
408 | |||
409 | if(!sub) { | ||
410 | /* draw sat view */ | ||
411 | sat_draw(context->gotoc.sat_area, context); | ||
412 | gtk_widget_queue_draw_area(context->gotoc.sat_area, 0,0, | ||
413 | context->gotoc.sat_area->allocation.width, | ||
414 | context->gotoc.sat_area->allocation.height); | ||
415 | } | ||
416 | |||
417 | if(sub++ == 5) sub = 0; | ||
418 | } | ||
419 | |||
420 | if(context->gotoc.compass_pixmap) { | ||
421 | /* draw compass */ | ||
422 | compass_draw(context->gotoc.compass_area, context); | ||
423 | gtk_widget_queue_draw_area(context->gotoc.compass_area, 0,0, | ||
424 | context->gotoc.compass_area->allocation.width, | ||
425 | context->gotoc.compass_area->allocation.height); | ||
426 | } | ||
427 | |||
428 | pos_t *pos = gps_get_pos(context->appdata); | ||
429 | if(pos) { | ||
430 | char str[16]; | ||
431 | gpx_pos_get_distance_str(str, sizeof(str), | ||
432 | *pos, context->gotoc.pos, | ||
433 | context->appdata->imperial); | ||
434 | gtk_label_set_text(GTK_LABEL(context->gotoc.distance_label), str); | ||
435 | |||
436 | snprintf(str, sizeof(str), _("%.1f°"), | ||
437 | gpx_pos_get_bearing(*pos, context->gotoc.pos)); | ||
438 | gtk_label_set_text(GTK_LABEL(context->gotoc.bearing_label), str); | ||
439 | } else { | ||
440 | gtk_label_set_text(GTK_LABEL(context->gotoc.distance_label), "-----"); | ||
441 | gtk_label_set_text(GTK_LABEL(context->gotoc.bearing_label), "-----"); | ||
442 | } | ||
443 | |||
444 | float epe = gps_get_epe(context->appdata); | ||
445 | if(isnan(epe)) | ||
446 | gtk_label_set_text(GTK_LABEL(context->gotoc.epe_label), "-----"); | ||
447 | else { | ||
448 | char str[16]; | ||
449 | if(context->appdata->imperial) { | ||
450 | epe *= 3.2808; | ||
451 | snprintf(str, sizeof(str), "%.1f ft", epe); | ||
452 | } else | ||
453 | snprintf(str, sizeof(str), "%.1f m", epe); | ||
454 | |||
455 | gtk_label_set_text(GTK_LABEL(context->gotoc.epe_label), str); | ||
456 | } | ||
457 | |||
458 | return TRUE; // fire again | ||
459 | } | ||
460 | |||
461 | static gint waypoint_changed_event(GtkWidget *widget, gpointer data ) { | ||
462 | cache_context_t *context = (cache_context_t*)data; | ||
463 | int wpt_idx = gtk_combo_box_get_active(GTK_COMBO_BOX(widget)); | ||
464 | |||
465 | /* get position of selected waypoint */ | ||
466 | if(wpt_idx == 0) | ||
467 | context->gotoc.pos = context->appdata->geomath; | ||
468 | else if(wpt_idx == 1) | ||
469 | // context->gotoc.pos = gpx_cache_pos(context->cache); | ||
470 | context->gotoc.pos = notes_get_pos(context); | ||
471 | else { | ||
472 | wpt_t *wpt = context->cache->wpt; | ||
473 | while(wpt_idx > 2) { | ||
474 | g_assert(wpt != NULL); | ||
475 | wpt = wpt->next; | ||
476 | wpt_idx--; | ||
477 | } | ||
478 | context->gotoc.pos = wpt->pos; | ||
479 | } | ||
480 | |||
481 | char str[32]; | ||
482 | pos_lat_str(str, sizeof(str), context->gotoc.pos.lat); | ||
483 | gtk_label_set_text(GTK_LABEL(context->gotoc.lat_lbl), str); | ||
484 | pos_lon_str(str, sizeof(str), context->gotoc.pos.lon); | ||
485 | gtk_label_set_text(GTK_LABEL(context->gotoc.lon_lbl), str); | ||
486 | |||
487 | goto_update(context); | ||
488 | |||
489 | return FALSE; | ||
490 | } | ||
491 | |||
492 | static void manual_pos_update(cache_context_t *context) { | ||
493 | context->gotoc.pos.lat = context->appdata->manual_goto.lat; | ||
494 | context->gotoc.pos.lon = context->appdata->manual_goto.lon; | ||
495 | |||
496 | char str[32]; | ||
497 | pos_lat_str(str, sizeof(str), context->gotoc.pos.lat); | ||
498 | gtk_label_set_text(GTK_LABEL(context->gotoc.lat_lbl), str); | ||
499 | pos_lon_str(str, sizeof(str), context->gotoc.pos.lon); | ||
500 | gtk_label_set_text(GTK_LABEL(context->gotoc.lon_lbl), str); | ||
501 | |||
502 | goto_update(context); | ||
503 | } | ||
504 | |||
505 | static void on_posedit_clicked(GtkWidget *button, gpointer data) { | ||
506 | cache_context_t *context = (cache_context_t*)data; | ||
507 | |||
508 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Edit Position"), | ||
509 | GTK_WINDOW(context->appdata->window), GTK_DIALOG_MODAL, | ||
510 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, | ||
511 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL); | ||
512 | |||
513 | GtkWidget *lat, *lon, *label; | ||
514 | GtkWidget *table = gtk_table_new(2, 2, FALSE); | ||
515 | |||
516 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
517 | label = gtk_label_new(_("Latitude:")), 0, 1, 0, 1); | ||
518 | gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f); | ||
519 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
520 | lat = lat_entry_new(context->appdata->manual_goto.lat), | ||
521 | 1, 2, 0, 1); | ||
522 | |||
523 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
524 | label = gtk_label_new(_("Longitude:")), 0, 1, 1, 2); | ||
525 | gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f); | ||
526 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
527 | lon = lon_entry_new(context->appdata->manual_goto.lon), | ||
528 | 1, 2, 1, 2); | ||
529 | |||
530 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table); | ||
531 | |||
532 | gtk_widget_show_all(dialog); | ||
533 | |||
534 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) { | ||
535 | if(isnan(lat_get(lat)) || isnan(lon_get(lon))) | ||
536 | errorf(_("Ignoring invalid position")); | ||
537 | else { | ||
538 | context->appdata->manual_goto.lat = lat_get(lat); | ||
539 | context->appdata->manual_goto.lon = lon_get(lon); | ||
540 | |||
541 | manual_pos_update(context); | ||
542 | } | ||
543 | } | ||
544 | gtk_widget_destroy(dialog); | ||
545 | } | ||
546 | |||
547 | static gint radio_changed_event(GtkWidget *widget, gpointer data ) { | ||
548 | cache_context_t *context = (cache_context_t*)data; | ||
549 | |||
550 | if(!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) { | ||
551 | manual_pos_update(context); | ||
552 | gtk_widget_set_sensitive(context->gotoc.edit_but, TRUE); | ||
553 | } else { | ||
554 | waypoint_changed_event(context->gotoc.cbox, data); | ||
555 | gtk_widget_set_sensitive(context->gotoc.edit_but, FALSE); | ||
556 | } | ||
557 | |||
558 | return FALSE; | ||
559 | } | ||
560 | |||
561 | #ifdef USE_MAEMO | ||
562 | static void on_mm_button_clicked(GtkButton *button, gpointer data) { | ||
563 | cache_context_t *context = (cache_context_t*)data; | ||
564 | |||
565 | dbus_mm_set_position(context->appdata, &context->gotoc.pos); | ||
566 | } | ||
567 | #endif | ||
568 | |||
569 | static gboolean compass_clicked_event(GtkWidget *widget, GdkEventButton *event, | ||
570 | gpointer user_data) { | ||
571 | cache_context_t *context = (cache_context_t*)user_data; | ||
572 | |||
573 | context->appdata->compass_locked = !context->appdata->compass_locked; | ||
574 | |||
575 | printf("compass is now %slocked\n", | ||
576 | context->appdata->compass_locked?"":"un"); | ||
577 | |||
578 | if(context->gotoc.compass_pixmap) { | ||
579 | /* draw compass */ | ||
580 | compass_draw(context->gotoc.compass_area, context); | ||
581 | gtk_widget_queue_draw_area(context->gotoc.compass_area, 0,0, | ||
582 | context->gotoc.compass_area->allocation.width, | ||
583 | context->gotoc.compass_area->allocation.height); | ||
584 | } | ||
585 | return FALSE; | ||
586 | } | ||
587 | |||
588 | GtkWidget *goto_cache(cache_context_t *context) { | ||
589 | int i; | ||
590 | |||
591 | /* clear list used for averaging */ | ||
592 | for(i=0;i<MAX_AVERAGE;i++) | ||
593 | context->gotoc.head_avg[i] = NAN; | ||
594 | |||
595 | context->gotoc.pos = gpx_cache_pos(context->cache); | ||
596 | |||
597 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); | ||
598 | |||
599 | context->gotoc.compass_area = gtk_drawing_area_new(); | ||
600 | gtk_drawing_area_size(GTK_DRAWING_AREA(context->gotoc.compass_area), | ||
601 | COMPASS_SIZE, COMPASS_SIZE); | ||
602 | |||
603 | gtk_signal_connect(GTK_OBJECT(context->gotoc.compass_area), "expose_event", | ||
604 | (GtkSignalFunc)compass_expose_event, context); | ||
605 | gtk_signal_connect(GTK_OBJECT(context->gotoc.compass_area),"configure_event", | ||
606 | (GtkSignalFunc)compass_configure_event, context); | ||
607 | gtk_signal_connect(GTK_OBJECT(context->gotoc.compass_area), | ||
608 | "button_press_event", | ||
609 | (GtkSignalFunc)compass_clicked_event, context); | ||
610 | |||
611 | gtk_widget_set_events(context->gotoc.compass_area, GDK_EXPOSURE_MASK); | ||
612 | gtk_widget_add_events(context->gotoc.compass_area, GDK_BUTTON_PRESS_MASK); | ||
613 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context->gotoc.compass_area); | ||
614 | |||
615 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); | ||
616 | GtkWidget *table = gtk_table_new(8, 2, FALSE); | ||
617 | |||
618 | /* ---------- combo box containing waypoint names ------- */ | ||
619 | GtkWidget *radio = gtk_radio_button_new_with_label(NULL, _("Waypoint:")); | ||
620 | gtk_table_attach_defaults(GTK_TABLE(table), radio, 0,1,0,1); | ||
621 | |||
622 | context->gotoc.cbox = gtk_combo_box_new_text(); | ||
623 | gtk_combo_box_append_text(GTK_COMBO_BOX(context->gotoc.cbox), _("Geomath")); | ||
624 | gtk_combo_box_append_text(GTK_COMBO_BOX(context->gotoc.cbox), | ||
625 | context->cache->id); | ||
626 | |||
627 | wpt_t *wpt = context->cache->wpt; | ||
628 | while(wpt) { | ||
629 | gtk_combo_box_append_text(GTK_COMBO_BOX(context->gotoc.cbox), wpt->id); | ||
630 | wpt = wpt->next; | ||
631 | } | ||
632 | gtk_combo_box_set_active(GTK_COMBO_BOX(context->gotoc.cbox), 1); | ||
633 | gtk_signal_connect(GTK_OBJECT(context->gotoc.cbox), "changed", | ||
634 | (GtkSignalFunc)waypoint_changed_event, context); | ||
635 | gtk_table_attach_defaults(GTK_TABLE(table), context->gotoc.cbox, 1,2,0,1); | ||
636 | |||
637 | /* -------------- manual entry field ------------------------- */ | ||
638 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
639 | gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio), | ||
640 | _("Manual:")), 0,1,1,2); | ||
641 | gtk_signal_connect(GTK_OBJECT(radio), "clicked", | ||
642 | (GtkSignalFunc)radio_changed_event, context); | ||
643 | |||
644 | context->gotoc.edit_but = gtk_button_new_with_label(_("Edit...")); | ||
645 | gtk_table_attach_defaults(GTK_TABLE(table), context->gotoc.edit_but, 1,2,1,2); | ||
646 | gtk_signal_connect(GTK_OBJECT(context->gotoc.edit_but), "clicked", | ||
647 | (GtkSignalFunc)on_posedit_clicked, context); | ||
648 | gtk_widget_set_sensitive(context->gotoc.edit_but, FALSE); | ||
649 | |||
650 | #if 0 | ||
651 | context->gotoc.man_lat = gtk_entry_new(); | ||
652 | gtk_table_attach_defaults(GTK_TABLE(table), context->gotoc.man_lat, 1,2,1,2); | ||
653 | pos_lat_str(str, sizeof(str), context->appdata->manual_goto.lat); | ||
654 | gtk_entry_set_text(GTK_ENTRY(context->gotoc.man_lat), str); | ||
655 | textbox_disable(context->gotoc.man_lat); | ||
656 | context->gotoc.man_lon = gtk_entry_new(); | ||
657 | gtk_table_attach_defaults(GTK_TABLE(table), context->gotoc.man_lon, 1,2,2,3); | ||
658 | pos_lon_str(str, sizeof(str), context->appdata->manual_goto.lon); | ||
659 | gtk_entry_set_text(GTK_ENTRY(context->gotoc.man_lon), str); | ||
660 | textbox_disable(context->gotoc.man_lon); | ||
661 | |||
662 | gtk_signal_connect(GTK_OBJECT(context->gotoc.man_lat), "changed", | ||
663 | (GtkSignalFunc)entry_changed_event, context); | ||
664 | gtk_signal_connect(GTK_OBJECT(context->gotoc.man_lon), "changed", | ||
665 | (GtkSignalFunc)entry_changed_event, context); | ||
666 | #endif | ||
667 | |||
668 | gtk_table_set_row_spacing(GTK_TABLE(table), 2, 16); | ||
669 | |||
670 | /* -------------- waypoint coordinates ------------------------- */ | ||
671 | /* SIZE_SMALL doesn't work here as setting the label returns to normal */ | ||
672 | context->gotoc.lat_lbl = pos_lat(context->gotoc.pos.lat, SIZE_NORMAL, | ||
673 | STRIKETHROUGH_NONE); | ||
674 | gtk_table_attach_defaults(GTK_TABLE(table), context->gotoc.lat_lbl,0,1,3,4); | ||
675 | context->gotoc.lon_lbl = pos_lon(context->gotoc.pos.lon, SIZE_NORMAL, | ||
676 | STRIKETHROUGH_NONE); | ||
677 | gtk_table_attach_defaults(GTK_TABLE(table), context->gotoc.lon_lbl,1,2,3,4); | ||
678 | |||
679 | /* -------------- distance label ------------------------- */ | ||
680 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
681 | gtk_label_new(_("Distance:")), 0,1,4,5); | ||
682 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
683 | (context->gotoc.distance_label = gtk_label_new("-----")), 1,2,4,5); | ||
684 | |||
685 | /* -------------- bearing label ------------------------- */ | ||
686 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
687 | gtk_label_new(_("Bearing:")), 0,1,5,6); | ||
688 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
689 | (context->gotoc.bearing_label = gtk_label_new("-----")), 1,2,5,6); | ||
690 | |||
691 | /* -------------- error label ------------------------- */ | ||
692 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
693 | gtk_label_new(_("Est. error:")), 0,1,6,7); | ||
694 | gtk_table_attach_defaults(GTK_TABLE(table), | ||
695 | (context->gotoc.epe_label = gtk_label_new("-----")), 1,2,6,7); | ||
696 | |||
697 | gtk_table_set_row_spacing(GTK_TABLE(table), 6, 16); | ||
698 | |||
699 | /* -------------- sat view box ------------------------- */ | ||
700 | GtkWidget *ihbox = gtk_hbox_new(FALSE,0); | ||
701 | |||
702 | context->gotoc.sat_area = gtk_drawing_area_new(); | ||
703 | gtk_drawing_area_size(GTK_DRAWING_AREA(context->gotoc.sat_area), | ||
704 | SAT_WIDTH, SAT_HEIGHT); | ||
705 | |||
706 | gtk_signal_connect(GTK_OBJECT(context->gotoc.sat_area), "expose_event", | ||
707 | (GtkSignalFunc)sat_expose_event, context); | ||
708 | gtk_signal_connect(GTK_OBJECT(context->gotoc.sat_area),"configure_event", | ||
709 | (GtkSignalFunc)sat_configure_event, context); | ||
710 | |||
711 | gtk_widget_set_events(context->gotoc.sat_area, GDK_EXPOSURE_MASK); | ||
712 | |||
713 | gtk_box_pack_start(GTK_BOX(ihbox), context->gotoc.sat_area, 1,0,0); | ||
714 | |||
715 | #ifdef USE_MAEMO | ||
716 | GtkWidget *mm_button = gtk_button_new(); | ||
717 | gtk_button_set_image(GTK_BUTTON(mm_button), icon_get_widget(ICON_MISC, 0)); | ||
718 | gtk_signal_connect(GTK_OBJECT(mm_button), "clicked", | ||
719 | (GtkSignalFunc)on_mm_button_clicked, context); | ||
720 | gtk_box_pack_start(GTK_BOX(ihbox), mm_button, 1,0,0); | ||
721 | #endif | ||
722 | gtk_table_attach_defaults(GTK_TABLE(table), ihbox, 0,2,7,8); | ||
723 | |||
724 | /* ------------------------------------------------------- */ | ||
725 | |||
726 | gtk_box_pack_start(GTK_BOX(vbox), table, 1,0,0); | ||
727 | gtk_box_pack_start(GTK_BOX(hbox), vbox, 1,0,0); | ||
728 | |||
729 | context->gotoc.handler_id = gtk_timeout_add(UPDATE_MS, | ||
730 | goto_update, context); | ||
731 | |||
732 | return hbox; | ||
733 | } | ||
734 | |||
735 | void goto_coordinate_update(cache_context_t *context) { | ||
736 | if(!context->notes.modified) | ||
737 | return; | ||
738 | |||
739 | waypoint_changed_event(context->gotoc.cbox, context); | ||
740 | } |