Contents of /trunk/src/map-tool.c

Parent Directory Parent Directory | Revision Log Revision Log


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