Parent Directory | Revision Log
GPS position on map
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 "gpxview.h" |
21 | #include <math.h> // for isnan |
22 | |
23 | #ifdef ENABLE_OSM_GPS_MAP |
24 | #include "osm-gps-map.h" |
25 | #endif |
26 | |
27 | #define GPS_DEFAULT_ZOOM 13 |
28 | |
29 | /* equatorial radius in meters */ |
30 | #define EQ_RADIUS (6378137.0) |
31 | |
32 | #define RAD2DEG(a) (((a)*180.0)/M_PI) |
33 | #define DEG2RAD(a) (((a)*M_PI)/180.0) |
34 | |
35 | typedef struct { |
36 | appdata_t *appdata; |
37 | GtkWidget *widget; |
38 | GtkWidget *zoomin, *zoomout, *gps; |
39 | gint handler_id; |
40 | cache_t *press_on; |
41 | #if MAEMO_VERSION_MAJOR == 5 |
42 | GtkWidget *old_view; |
43 | #endif |
44 | } map_context_t; |
45 | |
46 | #define PROXY_KEY "/system/http_proxy/" |
47 | |
48 | static const char *get_proxy_uri(appdata_t *appdata) { |
49 | static char proxy_buffer[64] = ""; |
50 | |
51 | /* use environment settings if preset */ |
52 | const char *proxy = g_getenv("http_proxy"); |
53 | if(proxy) { |
54 | printf("http_proxy: %s\n", proxy); |
55 | return proxy; |
56 | } |
57 | |
58 | /* ------------- get proxy settings -------------------- */ |
59 | if(gconf_client_get_bool(appdata->gconf_client, |
60 | PROXY_KEY "use_http_proxy", NULL)) { |
61 | |
62 | /* we can savely ignore things like "ignore_hosts" since we */ |
63 | /* are pretty sure not inside the net of one of our map renderers */ |
64 | /* (unless the user works at google :-) */ |
65 | |
66 | /* get basic settings */ |
67 | char *host = |
68 | gconf_client_get_string(appdata->gconf_client, PROXY_KEY "host", NULL); |
69 | if(host) { |
70 | int port = |
71 | gconf_client_get_int(appdata->gconf_client, PROXY_KEY "port", NULL); |
72 | |
73 | snprintf(proxy_buffer, sizeof(proxy_buffer), |
74 | "http://%s:%u", host, port); |
75 | |
76 | g_free(host); |
77 | } |
78 | return proxy_buffer; |
79 | } |
80 | |
81 | return NULL; |
82 | } |
83 | |
84 | static void map_zoom(map_context_t *context, int step) { |
85 | gint zoom; |
86 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
87 | g_object_get(map, "zoom", &zoom, NULL); |
88 | zoom = osm_gps_map_set_zoom(map, zoom+step); |
89 | |
90 | /* enable/disable zoom buttons as required */ |
91 | gtk_widget_set_sensitive(context->zoomin, zoom<17); |
92 | gtk_widget_set_sensitive(context->zoomout, zoom>1); |
93 | |
94 | /* save new zoom */ |
95 | context->appdata->map.zoom = zoom; |
96 | } |
97 | |
98 | static gboolean |
99 | cb_map_zoomin(GtkButton *button, map_context_t *context) { |
100 | map_zoom(context, +1); |
101 | return FALSE; |
102 | } |
103 | |
104 | static gboolean |
105 | cb_map_zoomout(GtkButton *button, map_context_t *context) { |
106 | map_zoom(context, -1); |
107 | return FALSE; |
108 | } |
109 | |
110 | static gboolean |
111 | cb_map_gps(GtkButton *button, map_context_t *context) { |
112 | pos_t *refpos = get_pos(context->appdata); |
113 | if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { |
114 | osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
115 | refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM); |
116 | } else { |
117 | /* no coordinates given: display the entire world */ |
118 | osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
119 | 0.0, 0.0, 1); |
120 | } |
121 | |
122 | return FALSE; |
123 | } |
124 | |
125 | static GtkWidget |
126 | *map_add_button(int icon, GCallback cb, gpointer data, |
127 | char *tooltip) { |
128 | GtkWidget *button = gtk_button_new(); |
129 | gtk_button_set_image(GTK_BUTTON(button), icon_get_widget(ICON_MISC, icon)); |
130 | g_signal_connect(button, "clicked", cb, data); |
131 | #ifndef USE_MAEMO |
132 | gtk_widget_set_tooltip_text(button, tooltip); |
133 | #endif |
134 | return button; |
135 | } |
136 | |
137 | static gboolean map_gps_update(gpointer data) { |
138 | map_context_t *context = (map_context_t*)data; |
139 | |
140 | /* get reference position ... */ |
141 | pos_t *refpos = get_pos(context->appdata); |
142 | gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon); |
143 | |
144 | /* ... and enable "goto" button if it's valid */ |
145 | gtk_widget_set_sensitive(context->gps, ok); |
146 | |
147 | if(ok) { |
148 | float heading = context->appdata->use_gps? |
149 | gps_get_heading(context->appdata):NAN; |
150 | |
151 | osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget), |
152 | refpos->lat, refpos->lon, heading); |
153 | } else |
154 | osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget)); |
155 | |
156 | return TRUE; |
157 | } |
158 | |
159 | static gboolean on_map_configure(GtkWidget *widget, |
160 | GdkEventConfigure *event, |
161 | map_context_t *context) { |
162 | |
163 | /* set default values if they are invalid */ |
164 | if(!context->appdata->map.zoom || |
165 | isnan(context->appdata->map.pos.lat) || |
166 | isnan(context->appdata->map.pos.lon)) { |
167 | printf("no valid map position found\n"); |
168 | |
169 | pos_t *refpos = get_pos(context->appdata); |
170 | if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { |
171 | /* use gps position if present */ |
172 | context->appdata->map.pos = *refpos; |
173 | context->appdata->map.zoom = GPS_DEFAULT_ZOOM; |
174 | } else { |
175 | /* use world map otherwise */ |
176 | context->appdata->map.pos.lat = 0.0; |
177 | context->appdata->map.pos.lon = 0.0; |
178 | context->appdata->map.zoom = 1; |
179 | } |
180 | } |
181 | |
182 | /* jump to initial position */ |
183 | osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
184 | context->appdata->map.pos.lat, |
185 | context->appdata->map.pos.lon, |
186 | context->appdata->map.zoom); |
187 | |
188 | return FALSE; |
189 | } |
190 | |
191 | static void map_draw_cachelist(GtkWidget *map, cache_t *cache) { |
192 | while(cache) { |
193 | GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); |
194 | |
195 | osm_gps_map_add_image(OSM_GPS_MAP(map), |
196 | cache->pos.lat, cache->pos.lon, icon); |
197 | |
198 | cache = cache->next; |
199 | } |
200 | } |
201 | |
202 | /* draw a nice popup */ |
203 | typedef struct { |
204 | appdata_t *appdata; |
205 | GtkWidget *window; |
206 | GMainLoop *loop; |
207 | } popup_context_t; |
208 | |
209 | /* draw shape */ |
210 | #define ARROW_BORDER 20 |
211 | #define CORNER_RADIUS 10 |
212 | |
213 | #ifndef USE_MAEMO |
214 | #define POPUP_WIDTH 300 |
215 | #define POPUP_HEIGHT 100 |
216 | #else |
217 | #define POPUP_WIDTH 350 |
218 | #define POPUP_HEIGHT 120 |
219 | #endif |
220 | |
221 | static gboolean |
222 | pointer_in_window(GtkWidget *widget, gint x_root, gint y_root) { |
223 | if(GTK_WIDGET_MAPPED(gtk_widget_get_toplevel(widget))) { |
224 | gint window_x, window_y; |
225 | |
226 | gdk_window_get_position(gtk_widget_get_toplevel(widget)->window, |
227 | &window_x, &window_y); |
228 | |
229 | if(x_root >= window_x && x_root < window_x + widget->allocation.width && |
230 | y_root >= window_y && y_root < window_y + widget->allocation.height) |
231 | return TRUE; |
232 | } |
233 | |
234 | return FALSE; |
235 | } |
236 | |
237 | static gboolean |
238 | on_button_press_event(GtkWidget *widget, |
239 | GdkEventButton *event, popup_context_t *context) { |
240 | gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
241 | |
242 | printf("overlay button press (in = %d)\n", in); |
243 | return !in; |
244 | } |
245 | |
246 | static gboolean |
247 | on_button_release_event(GtkWidget *widget, |
248 | GdkEventButton *event, popup_context_t *context) { |
249 | gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
250 | |
251 | printf("overlay button release (in = %d)\n", in); |
252 | |
253 | if(!in) { |
254 | printf("destroying popup\n"); |
255 | gtk_widget_destroy(gtk_widget_get_toplevel(widget)); |
256 | } |
257 | |
258 | return !in; |
259 | } |
260 | |
261 | static void |
262 | shutdown_loop(popup_context_t *context) { |
263 | if(g_main_loop_is_running(context->loop)) |
264 | g_main_loop_quit(context->loop); |
265 | } |
266 | |
267 | static gint |
268 | run_delete_handler(GtkWindow *window, GdkEventAny *event, |
269 | popup_context_t *context) { |
270 | shutdown_loop(context); |
271 | return TRUE; /* Do not destroy */ |
272 | } |
273 | |
274 | static void |
275 | run_destroy_handler(GtkWindow *window, popup_context_t *context) { |
276 | /* shutdown_loop will be called by run_unmap_handler */ |
277 | printf("popup destroyed\n"); |
278 | } |
279 | |
280 | static void |
281 | run_unmap_handler(GtkWindow *window, popup_context_t *context) { |
282 | shutdown_loop(context); |
283 | } |
284 | |
285 | static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) { |
286 | GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1); |
287 | |
288 | GdkGC *gc = gdk_gc_new(mask); |
289 | GdkColormap *colormap; |
290 | GdkColor black; |
291 | GdkColor white; |
292 | |
293 | /* get black/white color values */ |
294 | colormap = gdk_colormap_get_system(); |
295 | gdk_color_black(colormap, &black); |
296 | gdk_color_white(colormap, &white); |
297 | |
298 | /* erase */ |
299 | gdk_gc_set_foreground(gc, &black); |
300 | gdk_gc_set_background(gc, &white); |
301 | |
302 | /* erase background */ |
303 | gdk_draw_rectangle(mask, gc, TRUE, 0, 0, POPUP_WIDTH, POPUP_HEIGHT); |
304 | |
305 | gdk_gc_set_foreground(gc, &white); |
306 | gdk_gc_set_background(gc, &black); |
307 | |
308 | /* the tip is always above or below the "bubble" but never at its side */ |
309 | guint tip_offset = (tip_y == 0)?ARROW_BORDER:0; |
310 | |
311 | gdk_draw_rectangle(mask, gc, TRUE, |
312 | 0, tip_offset + CORNER_RADIUS, |
313 | POPUP_WIDTH, |
314 | POPUP_HEIGHT - 2*CORNER_RADIUS - ARROW_BORDER); |
315 | |
316 | gdk_draw_rectangle(mask, gc, TRUE, |
317 | CORNER_RADIUS, tip_offset, |
318 | POPUP_WIDTH - 2*CORNER_RADIUS, |
319 | POPUP_HEIGHT - ARROW_BORDER); |
320 | |
321 | int off[][2] = { |
322 | { CORNER_RADIUS, tip_offset + CORNER_RADIUS }, |
323 | { POPUP_WIDTH - CORNER_RADIUS, tip_offset + CORNER_RADIUS }, |
324 | { POPUP_WIDTH - CORNER_RADIUS, |
325 | POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset}, |
326 | { CORNER_RADIUS, |
327 | POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset}}; |
328 | |
329 | int i; |
330 | for(i=0;i<4;i++) { |
331 | gdk_draw_arc(mask, gc, TRUE, |
332 | off[i][0]-CORNER_RADIUS, off[i][1]-CORNER_RADIUS, |
333 | 2*CORNER_RADIUS, 2*CORNER_RADIUS, |
334 | 0, 360*64); |
335 | } |
336 | |
337 | GdkPoint points[3] = { {POPUP_WIDTH*1/3, POPUP_HEIGHT/2}, |
338 | {POPUP_WIDTH*2/3, POPUP_HEIGHT/2}, |
339 | {tip_x,tip_y} }; |
340 | gdk_draw_polygon(mask, gc, TRUE, points, 3); |
341 | |
342 | |
343 | gdk_window_shape_combine_mask(window->window, mask, 0, 0); |
344 | } |
345 | |
346 | /* create a left aligned label (normal ones are centered) */ |
347 | static GtkWidget *gtk_label_left_new(char *str) { |
348 | GtkWidget *label = gtk_label_new(str); |
349 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); |
350 | return label; |
351 | } |
352 | |
353 | /* the small labels are actually only on maemo small */ |
354 | #ifdef USE_MAEMO |
355 | #define MARKUP_SMALL "<span size='small'>%s</span>" |
356 | GtkWidget *gtk_label_small_left_new(char *str) { |
357 | GtkWidget *label = gtk_label_new(""); |
358 | char *markup = g_markup_printf_escaped(MARKUP_SMALL, str); |
359 | gtk_label_set_markup(GTK_LABEL(label), markup); |
360 | g_free(markup); |
361 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); |
362 | return label; |
363 | } |
364 | #define gtk_label_big_left_new(a) gtk_label_left_new(a) |
365 | #else |
366 | #define gtk_label_small_left_new(a) gtk_label_left_new(a) |
367 | #define MARKUP_BIG "<span size='x-large'>%s</span>" |
368 | GtkWidget *gtk_label_big_left_new(char *str) { |
369 | GtkWidget *label = gtk_label_new(""); |
370 | char *markup = g_markup_printf_escaped(MARKUP_BIG, str); |
371 | gtk_label_set_markup(GTK_LABEL(label), markup); |
372 | g_free(markup); |
373 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); |
374 | return label; |
375 | } |
376 | #endif |
377 | |
378 | void cache_popup(map_context_t *mcontext, cache_t *cache) { |
379 | popup_context_t pcontext; |
380 | pcontext.appdata = mcontext->appdata; |
381 | |
382 | pcontext.window = gtk_window_new(GTK_WINDOW_POPUP); |
383 | gtk_widget_realize(pcontext.window); |
384 | gtk_window_set_default_size(GTK_WINDOW(pcontext.window), |
385 | POPUP_WIDTH, POPUP_HEIGHT); |
386 | gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT); |
387 | // gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE); |
388 | gtk_window_set_transient_for(GTK_WINDOW(pcontext.window), |
389 | GTK_WINDOW(mcontext->appdata->window)); |
390 | gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE); |
391 | gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE); |
392 | gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC); |
393 | gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE); |
394 | |
395 | /* connect events */ |
396 | g_signal_connect(G_OBJECT(pcontext.window), "button-press-event", |
397 | G_CALLBACK(on_button_press_event), &pcontext); |
398 | g_signal_connect(G_OBJECT(pcontext.window), "button-release-event", |
399 | G_CALLBACK(on_button_release_event), &pcontext); |
400 | g_signal_connect(G_OBJECT(pcontext.window), "delete-event", |
401 | G_CALLBACK(run_delete_handler), &pcontext); |
402 | g_signal_connect(G_OBJECT(pcontext.window), "destroy", |
403 | G_CALLBACK(run_destroy_handler), &pcontext); |
404 | g_signal_connect(G_OBJECT(pcontext.window), "unmap", |
405 | G_CALLBACK(run_unmap_handler), &pcontext); |
406 | |
407 | gdk_pointer_grab(pcontext.window->window, TRUE, |
408 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, |
409 | NULL, NULL, GDK_CURRENT_TIME); |
410 | gtk_grab_add(pcontext.window); |
411 | |
412 | /* check whether cache is in upper or lower half of window */ |
413 | gint x, y, sx, sy; |
414 | osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget), |
415 | cache->pos.lat, cache->pos.lon, |
416 | &sx, &sy); |
417 | |
418 | gdk_window_get_origin(mcontext->widget->window, &x, &y); |
419 | |
420 | gint ax = 0, ay = 0; |
421 | if(sx > mcontext->widget->allocation.width/2) |
422 | ax = POPUP_WIDTH; |
423 | |
424 | if(sy > mcontext->widget->allocation.height/2) |
425 | ay = POPUP_HEIGHT; |
426 | |
427 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
428 | GdkColor color; |
429 | gdk_color_parse("darkgray", &color); |
430 | gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color); |
431 | #endif |
432 | |
433 | gtk_window_move(GTK_WINDOW(pcontext.window), |
434 | x + mcontext->widget->allocation.x + sx - ax, |
435 | y + mcontext->widget->allocation.y + sy - ay); |
436 | |
437 | |
438 | GtkWidget *alignment = gtk_alignment_new(0.5, 0.5, 1.0, 1.0); |
439 | gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), |
440 | CORNER_RADIUS/2 + (ay?0:ARROW_BORDER), |
441 | CORNER_RADIUS/2 + (ay?ARROW_BORDER:0), |
442 | CORNER_RADIUS, CORNER_RADIUS); |
443 | |
444 | /* --- actual content ---- */ |
445 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); |
446 | |
447 | if(cache->id) { |
448 | GtkWidget *ihbox = gtk_hbox_new(FALSE, 0); |
449 | |
450 | gtk_box_pack_start(GTK_BOX(ihbox), |
451 | icon_get_widget(ICON_CACHE_TYPE, cache->type), |
452 | FALSE, FALSE, 5); |
453 | |
454 | gtk_box_pack_start_defaults(GTK_BOX(ihbox), |
455 | gtk_label_big_left_new(cache->id)); |
456 | |
457 | gtk_box_pack_start_defaults(GTK_BOX(vbox), ihbox); |
458 | } |
459 | |
460 | if(cache->name) { |
461 | GtkWidget *label = gtk_label_small_left_new(cache->name); |
462 | gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); |
463 | gtk_box_pack_start_defaults(GTK_BOX(vbox), label); |
464 | } |
465 | |
466 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
467 | if(cache->terrain) { |
468 | GtkWidget *ihbox = gtk_hbox_new(FALSE, 0); |
469 | gtk_box_pack_start(GTK_BOX(ihbox), |
470 | gtk_label_small_left_new(_("Terrain:")), FALSE, FALSE, 0); |
471 | gtk_box_pack_start(GTK_BOX(ihbox), |
472 | icon_get_widget(ICON_STARS, (int)(cache->terrain*2-2)), |
473 | FALSE, FALSE, 5); |
474 | gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox); |
475 | } |
476 | |
477 | if(cache->difficulty) { |
478 | GtkWidget *ihbox = gtk_hbox_new(FALSE, 0); |
479 | gtk_box_pack_start(GTK_BOX(ihbox), |
480 | gtk_label_small_left_new(_("Difficulty:")), FALSE, FALSE, 0); |
481 | gtk_box_pack_start(GTK_BOX(ihbox), |
482 | icon_get_widget(ICON_STARS, (int)(cache->difficulty*2-2)), |
483 | FALSE, FALSE, 5); |
484 | gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox); |
485 | } |
486 | |
487 | gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
488 | |
489 | gtk_container_add(GTK_CONTAINER(alignment), vbox); |
490 | /* ----------------------- */ |
491 | |
492 | |
493 | gtk_container_add(GTK_CONTAINER(pcontext.window), alignment); |
494 | |
495 | /* give window its shape */ |
496 | popup_window_shape(pcontext.window, ax, ay); |
497 | |
498 | gtk_widget_show_all(pcontext.window); |
499 | |
500 | /* handle this popup until it's gone */ |
501 | |
502 | pcontext.loop = g_main_loop_new(NULL, FALSE); |
503 | |
504 | GDK_THREADS_LEAVE(); |
505 | g_main_loop_run(pcontext.loop); |
506 | GDK_THREADS_ENTER(); |
507 | |
508 | g_main_loop_unref(pcontext.loop); |
509 | |
510 | printf("cache popup removed\n"); |
511 | } |
512 | |
513 | static void |
514 | map_cachelist_nearest(cache_t *cache, pos_t *pos, |
515 | cache_t **result, float *distance) { |
516 | while(cache) { |
517 | float dist = |
518 | pow(cache->pos.lat - pos->lat, 2) + |
519 | pow(cache->pos.lon - pos->lon, 2); |
520 | |
521 | if(!(dist > *distance)) { |
522 | *result = cache; |
523 | *distance = dist; |
524 | } |
525 | |
526 | cache = cache->next; |
527 | } |
528 | } |
529 | |
530 | static cache_t *map_closest(map_context_t *context, pos_t *pos) { |
531 | cache_t *result = NULL; |
532 | float distance = NAN; |
533 | |
534 | #ifdef USE_MAEMO |
535 | if(!context->appdata->cur_gpx) { |
536 | #endif |
537 | /* search all geocaches */ |
538 | gpx_t *gpx = context->appdata->gpx; |
539 | while(gpx) { |
540 | map_cachelist_nearest(gpx->cache, pos, &result, &distance); |
541 | gpx = gpx->next; |
542 | } |
543 | #ifdef USE_MAEMO |
544 | } else { |
545 | map_cachelist_nearest(context->appdata->cur_gpx->cache, |
546 | pos, &result, &distance); |
547 | } |
548 | #endif |
549 | |
550 | return result; |
551 | } |
552 | |
553 | /* translate between osm-gps-map positions and gpxview ones */ |
554 | pos_t coord2pos(coord_t coo) { |
555 | pos_t pos; |
556 | pos.lat = RAD2DEG(coo.rlat); |
557 | pos.lon = RAD2DEG(coo.rlon); |
558 | return pos; |
559 | } |
560 | |
561 | static int dist2pixel(map_context_t *context, float km, float lat) { |
562 | gint zoom; |
563 | g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL); |
564 | |
565 | /* world at zoom 1 == 512 pixels */ |
566 | float m_per_pix = |
567 | cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom)); |
568 | |
569 | return 1000.0*km/m_per_pix; |
570 | } |
571 | |
572 | #define CLICK_FUZZ (24) |
573 | |
574 | static gboolean |
575 | on_map_button_press_event(GtkWidget *widget, |
576 | GdkEventButton *event, map_context_t *context) { |
577 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
578 | |
579 | /* got a press event without release event? eat it! */ |
580 | if(context->press_on != NULL) { |
581 | printf("PRESS: already\n"); |
582 | return TRUE; |
583 | } |
584 | |
585 | pos_t pos = |
586 | coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
587 | |
588 | cache_t *nearest = map_closest(context, &pos); |
589 | if(nearest) { |
590 | float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
591 | if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
592 | context->press_on = nearest; |
593 | } |
594 | |
595 | return FALSE; |
596 | } |
597 | |
598 | static gboolean |
599 | on_map_button_release_event(GtkWidget *widget, |
600 | GdkEventButton *event, map_context_t *context) { |
601 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
602 | |
603 | if(context->press_on) { |
604 | pos_t pos = |
605 | coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
606 | |
607 | cache_t *nearest = map_closest(context, &pos); |
608 | if(nearest && nearest == context->press_on) { |
609 | float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
610 | if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
611 | cache_popup(context, nearest); |
612 | } |
613 | context->press_on = NULL; |
614 | } else { |
615 | /* save new map position */ |
616 | gfloat lat, lon; |
617 | g_object_get(map, "latitude", &lat, "longitude", &lon, NULL); |
618 | context->appdata->map.pos.lat = lat; |
619 | context->appdata->map.pos.lon = lon; |
620 | } |
621 | |
622 | return FALSE; |
623 | } |
624 | |
625 | static void save_map_state(map_context_t *context) { |
626 | /* save map parameters */ |
627 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
628 | gint zoom; |
629 | g_object_get(map, "zoom", &zoom, NULL); |
630 | context->appdata->map.zoom = zoom; |
631 | |
632 | gfloat lat, lon; |
633 | g_object_get(map, "latitude", &lat, "longitude", &lon, NULL); |
634 | context->appdata->map.pos.lat = lat; |
635 | context->appdata->map.pos.lon = lon; |
636 | } |
637 | |
638 | #if MAEMO_VERSION_MAJOR == 5 |
639 | static void on_window_destroy(GtkWidget *widget, map_context_t *context) { |
640 | printf("destroy map view\n"); |
641 | |
642 | save_map_state(context); |
643 | |
644 | /* restore cur_view */ |
645 | context->appdata->cur_view = context->old_view; |
646 | |
647 | gtk_timeout_remove(context->handler_id); |
648 | g_free(context); |
649 | } |
650 | #endif |
651 | |
652 | void map(appdata_t *appdata) { |
653 | map_context_t *context = g_new0(map_context_t, 1); |
654 | context->appdata = appdata; |
655 | |
656 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
657 | |
658 | char *path = g_strdup_printf("%s/map/", appdata->image_path); |
659 | const char *proxy = get_proxy_uri(appdata); |
660 | |
661 | context->widget = g_object_new(OSM_TYPE_GPS_MAP, |
662 | "repo-uri", MAP_SOURCE_OPENSTREETMAP, |
663 | "tile-cache", path, |
664 | "auto-center", FALSE, |
665 | "record-trip-history", FALSE, |
666 | "show-trip-history", FALSE, |
667 | NULL); |
668 | |
669 | if(proxy) |
670 | g_object_set(OSM_GPS_MAP(context->widget), |
671 | "proxy-uri", proxy, NULL); |
672 | |
673 | g_free(path); |
674 | |
675 | char *name = NULL; |
676 | #ifdef USE_MAEMO |
677 | if(!appdata->cur_gpx) { |
678 | #endif |
679 | /* draw all geocaches */ |
680 | gpx_t *gpx = appdata->gpx; |
681 | while(gpx) { |
682 | map_draw_cachelist(context->widget, gpx->cache); |
683 | gpx = gpx->next; |
684 | } |
685 | name = g_strdup(_("all geocaches")); |
686 | #ifdef USE_MAEMO |
687 | } else { |
688 | map_draw_cachelist(context->widget, appdata->cur_gpx->cache); |
689 | name = g_strdup(appdata->cur_gpx->name); |
690 | } |
691 | #endif |
692 | |
693 | char *title = g_strdup_printf(_("Map - %s"), name); |
694 | g_free(name); |
695 | |
696 | #if MAEMO_VERSION_MAJOR == 5 |
697 | GtkWidget *window = hildon_stackable_window_new(); |
698 | gtk_window_set_title(GTK_WINDOW(window), title); |
699 | #else |
700 | GtkWidget *dialog = gtk_dialog_new_with_buttons(title, |
701 | GTK_WINDOW(appdata->window), |
702 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
703 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
704 | NULL); |
705 | |
706 | #ifndef USE_MAEMO |
707 | gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480); |
708 | #else |
709 | gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480); |
710 | #endif |
711 | #endif |
712 | |
713 | g_free(title); |
714 | |
715 | g_signal_connect(G_OBJECT(context->widget), "configure-event", |
716 | G_CALLBACK(on_map_configure), context); |
717 | |
718 | g_signal_connect(G_OBJECT(context->widget), "button-press-event", |
719 | G_CALLBACK(on_map_button_press_event), context); |
720 | |
721 | g_signal_connect(G_OBJECT(context->widget), "button-release-event", |
722 | G_CALLBACK(on_map_button_release_event), context); |
723 | |
724 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget); |
725 | /* zoom button box */ |
726 | GtkWidget *vbox = gtk_vbox_new(FALSE,0); |
727 | |
728 | context->zoomin = |
729 | map_add_button(10, G_CALLBACK(cb_map_zoomin), |
730 | context, _("Zoom in")); |
731 | gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0); |
732 | |
733 | context->zoomout = |
734 | map_add_button(11, G_CALLBACK(cb_map_zoomout), |
735 | context, _("Zoom out")); |
736 | gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0); |
737 | |
738 | context->gps = |
739 | map_add_button(9, G_CALLBACK(cb_map_gps), |
740 | context, _("Jump to GPS position")); |
741 | gtk_widget_set_sensitive(context->gps, FALSE); |
742 | /* install handler for timed updates of the gps button */ |
743 | context->handler_id = gtk_timeout_add(1000, map_gps_update, context); |
744 | gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0); |
745 | |
746 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
747 | |
748 | #if MAEMO_VERSION_MAJOR == 5 |
749 | /* prevent some of the main screen things */ |
750 | context->old_view = appdata->cur_view; |
751 | appdata->cur_view = NULL; |
752 | |
753 | g_signal_connect(G_OBJECT(window), "destroy", |
754 | G_CALLBACK(on_window_destroy), context); |
755 | |
756 | gtk_container_add(GTK_CONTAINER(window), hbox); |
757 | gtk_widget_show_all(GTK_WIDGET(window)); |
758 | |
759 | #else |
760 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
761 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); |
762 | gtk_widget_show_all(dialog); |
763 | gtk_dialog_run(GTK_DIALOG(dialog)); |
764 | save_map_state(context); |
765 | gtk_timeout_remove(context->handler_id); |
766 | gtk_widget_destroy(dialog); |
767 | g_free(context); |
768 | #endif |
769 | } |