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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 189 - (show annotations)
Tue Nov 17 09:07:17 2009 UTC (14 years, 5 months ago) by harbaum
File MIME type: text/plain
File size: 28253 byte(s)
Various adjustments
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 /*
21 * http://topo.geofabrik.de/relief/${z}/${x}/${y}.png 8-15
22 * http://topo.geofabrik.de/trail/${z}/${x}/${y}.png 8-15
23 */
24
25 /*
26 * TODO:
27 * - draw caches only once
28 * - prefer duplicates from selected files
29 * - make semi-transparent caches selectable
30 */
31
32 #include "gpxview.h"
33 #include "converter.h"
34 #include <math.h> // for isnan
35
36 #ifdef ENABLE_OSM_GPS_MAP
37 #include "osm-gps-map.h"
38 #include "osm-gps-map-osd-classic.h"
39 #endif
40
41 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
42 #include <gdk/gdkx.h>
43 #include <X11/Xatom.h>
44 #endif
45
46 /* default values */
47 #define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENCYCLEMAP
48 #define GPS_DEFAULT_ZOOM 13
49
50 #define PROXY_KEY "/system/http_proxy/"
51
52 static const char *get_proxy_uri(appdata_t *appdata) {
53 static char proxy_buffer[64] = "";
54
55 /* use environment settings if preset */
56 const char *proxy = g_getenv("http_proxy");
57 if(proxy) {
58 printf("map http proxy from env: %s\n", proxy);
59 return proxy;
60 }
61
62 /* ------------- get proxy settings -------------------- */
63 if(gconf_client_get_bool(appdata->gconf_client,
64 PROXY_KEY "use_http_proxy", NULL)) {
65
66 /* we can savely ignore things like "ignore_hosts" since we */
67 /* are pretty sure not inside the net of one of our map renderers */
68 /* (unless the user works at google :-) */
69
70 /* get basic settings */
71 char *host =
72 gconf_client_get_string(appdata->gconf_client, PROXY_KEY "host", NULL);
73 if(host) {
74 int port =
75 gconf_client_get_int(appdata->gconf_client, PROXY_KEY "port", NULL);
76
77 snprintf(proxy_buffer, sizeof(proxy_buffer),
78 "http://%s:%u", host, port);
79 printf("map http proxy from gconf: %s\n ", proxy_buffer);
80
81 g_free(host);
82 }
83 return proxy_buffer;
84 }
85
86 return NULL;
87 }
88
89 static void
90 cb_map_gps(osd_button_t but, map_context_t *context) {
91
92 if(but == OSD_GPS) {
93 pos_t *refpos = get_pos(context->appdata);
94 if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
95 gint zoom;
96 g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
97 if(zoom < 10)
98 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
99 refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
100 else
101 osm_gps_map_set_center(OSM_GPS_MAP(context->widget),
102 refpos->lat, refpos->lon);
103
104 /* re-enable centering */
105 g_object_set(context->widget, "auto-center", TRUE, NULL);
106 } else {
107 /* no coordinates given: display the entire world */
108 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
109 0.0, 0.0, 1);
110 }
111 }
112 }
113
114 static int dist2pixel(map_context_t *context, float km, float lat) {
115 return 1000.0*km/osm_gps_map_get_scale(OSM_GPS_MAP(context->widget));
116 }
117
118 static gboolean map_gps_update(gpointer data) {
119 map_context_t *context = (map_context_t*)data;
120 static gboolean goto_is_enabled = FALSE;
121
122 /* get reference position ... */
123 pos_t *refpos = get_pos(context->appdata);
124 gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
125
126 /* ... and enable "goto" button if it's valid */
127 if(ok != goto_is_enabled) {
128 osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget),
129 OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context);
130 goto_is_enabled = ok;
131 }
132
133 if(ok) {
134 float heading = NAN;
135 int radius = 0;
136
137 if(context->appdata->use_gps) {
138 heading = gps_get_heading(context->appdata);
139
140 /* get error */
141 float eph = gps_get_eph(context->appdata);
142 if(!isnan(eph))
143 radius = dist2pixel(context, eph/1000, refpos->lat);
144 }
145
146 /* TODO: in order to save energy: only draw if state actually changed */
147
148 g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);
149 osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),
150 refpos->lat, refpos->lon, heading);
151 } else
152 osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));
153
154 return TRUE;
155 }
156
157 static void map_draw_cache(GtkWidget *map, cache_t *cache, gboolean semi) {
158 int type = semi?ICON_CACHE_TYPE_SEMI:ICON_CACHE_TYPE;
159
160 GdkPixbuf *icon = icon_get(type, cache->type);
161
162 /* check if there's also an overwritten coordinate */
163 if(cache->notes && cache->notes->override) {
164 GdkPixbuf *over = icon_get(type, 11);
165
166 osm_gps_map_add_image(OSM_GPS_MAP(map),
167 cache->notes->pos.lat, cache->notes->pos.lon, icon);
168
169 osm_gps_map_add_image(OSM_GPS_MAP(map),
170 cache->notes->pos.lat, cache->notes->pos.lon, over);
171 } else {
172 if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon))
173 osm_gps_map_add_image(OSM_GPS_MAP(map),
174 cache->pos.lat, cache->pos.lon, icon);
175 }
176 }
177
178 static void map_draw_gpx(appdata_t *appdata, cache_display_t *caches,
179 GtkWidget *map, gpx_t *gpx,
180 cache_t *nav, gboolean semi) {
181 if(!gpx->notes_loaded) {
182 notes_load_all(appdata, gpx);
183 gpx->notes_loaded = TRUE;
184 }
185
186 cache_t *cache = gpx->cache;
187 while(cache) {
188 /* search if we have that cache already in our list/displayed */
189 int i=0;
190 while(caches[i].id && (strcmp(caches[i].id, cache->id) != 0))
191 i++;
192
193 if(!caches[i].id) {
194 /* if nav is given draw all other caches semitransparent. */
195 /* if nav is not given do what semi sais */
196 map_draw_cache(map, cache, nav?(cache != nav):semi);
197 caches[i].id = cache->id;
198 }
199
200 cache = cache->next;
201 }
202 }
203
204 /* draw geocaches and set window title */
205 static void map_setup(map_context_t *context) {
206 char *name = NULL;
207
208 int cache_num = gpx_total_caches_global(context->appdata->gpx);
209
210 if(context->caches_displayed && (cache_num != context->cache_list_len)) {
211 // printf("re-alloc because %p %d/%d\n", context->caches_displayed,
212 // cache_num, context->cache_list_len);
213 g_free(context->caches_displayed);
214 context->caches_displayed = NULL;
215 context->cache_list_len = 0;
216 }
217
218 /* allocate buffer */
219 if(cache_num && !context->caches_displayed) {
220 context->cache_list_len = cache_num;
221 context->caches_displayed = g_new0(cache_display_t, cache_num+1);
222 printf("allocated space to handle %d map icons\n", cache_num);
223 }
224
225 if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {
226 if(context->state != MAP_ALL) {
227 printf("map_setup(ALL)\n");
228
229 #ifdef OSD_NAV
230 /* no navigation in this mode */
231 osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));
232 #endif
233
234 /* clear all existing cache images */
235 osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
236 memset(context->caches_displayed, 0,
237 (cache_num+1) * sizeof(cache_display_t));
238
239 /* draw all geocaches and none is semi-transparent */
240 gpx_t *gpx = context->appdata->gpx;
241 while(gpx) {
242 map_draw_gpx(context->appdata, context->caches_displayed,
243 context->widget, gpx, NULL, FALSE);
244 gpx = gpx->next;
245 }
246
247 {
248 int i=0;
249 while(context->caches_displayed[i].id) i++;
250 printf("number of caches actually displayed: %d\n", i);
251 }
252
253 name = g_strdup(_("all"));
254 context->state = MAP_ALL;
255 }
256 } else if(!context->appdata->cur_cache) {
257 if(context->state != MAP_GPX) {
258 printf("map_setup(GPX)\n");
259
260 #ifdef OSD_NAV
261 /* no navigation in this mode */
262 osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));
263 #endif
264
265 /* clear all existing ccahe images */
266 osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
267 memset(context->caches_displayed, 0,
268 (cache_num+1) * sizeof(cache_display_t));
269
270 /* draw all geocaches and all other gpx files are semi-transparent */
271 map_draw_gpx(context->appdata, context->caches_displayed,
272 context->widget, context->appdata->cur_gpx, NULL, FALSE);
273
274 gpx_t *gpx = context->appdata->gpx;
275 while(gpx) {
276 if(gpx != context->appdata->cur_gpx)
277 map_draw_gpx(context->appdata, context->caches_displayed,
278 context->widget, gpx, NULL, TRUE);
279
280 gpx = gpx->next;
281 }
282
283 name = g_strdup(context->appdata->cur_gpx->name);
284 context->state = MAP_GPX;
285 }
286 } else {
287 cache_t *cache = context->appdata->cur_cache;
288
289 printf("map_setup(CACHE)\n");
290
291 /* no balloons in this mode */
292 context->balloon = NULL;
293 osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(context->widget));
294
295 /* clear all existing ccahe images */
296 osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
297 memset(context->caches_displayed, 0,
298 (cache_num+1) * sizeof(cache_display_t));
299
300 /* draw all geocaches and all but selected one are semi-transparent */
301 gpx_t *gpx = context->appdata->gpx;
302 while(gpx) {
303 map_draw_gpx(context->appdata, context->caches_displayed,
304 context->widget, gpx, cache, FALSE);
305 gpx = gpx->next;
306 }
307
308 name = g_strdup(cache->name);
309 context->state = MAP_CACHE;
310
311 /* navigation in this mode! */
312 pos_t cpos = gpx_cache_pos(cache);
313
314 #ifdef OSD_NAV
315 osm_gps_map_osd_draw_nav (OSM_GPS_MAP(context->widget),
316 context->appdata->imperial,
317 cpos.lat, cpos.lon, cache->name);
318 #else
319 #warning OSD_NAV not defined!
320 #endif
321 }
322
323 if(name) {
324 char *title = g_strdup_printf(_("Map - %s"), name);
325 g_free(name);
326
327 gtk_window_set_title(GTK_WINDOW(context->window), title);
328
329 g_free(title);
330 } else
331 printf("map_setup(keep)\n");
332 }
333
334 static gboolean on_map_configure(GtkWidget *widget,
335 GdkEventConfigure *event,
336 map_context_t *context) {
337
338 /* for some reason there's a configure event with 1/1 */
339 /* on diablo. We just ignore this! */
340
341 printf("on_map_configure %d %d\n",
342 widget->allocation.width,
343 widget->allocation.height);
344
345 if(!context->map_complete &&
346 (widget->allocation.width > 100) &&
347 (widget->allocation.height > 100)) {
348
349 /* setup cache state */
350 map_setup(context);
351
352 /* set default values if they are invalid */
353 if(!context->appdata->map.zoom ||
354 isnan(context->appdata->map.pos.lat) ||
355 isnan(context->appdata->map.pos.lon)) {
356 printf("no valid map position found\n");
357
358 pos_t *refpos = get_pos(context->appdata);
359 if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
360 printf("use refpos\n");
361
362 /* use gps position if present */
363 context->appdata->map.pos = *refpos;
364 context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
365 } else {
366 printf("use zero pos\n");
367
368 /* use world map otherwise */
369 context->appdata->map.pos.lat = 0.0;
370 context->appdata->map.pos.lon = 0.0;
371 context->appdata->map.zoom = 1;
372 }
373 }
374
375 /* jump to initial position */
376 printf("osm_gps_map_set_mapcenter(%f,%f,%d)\n",
377 context->appdata->map.pos.lat,
378 context->appdata->map.pos.lon,
379 context->appdata->map.zoom);
380
381 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
382 context->appdata->map.pos.lat,
383 context->appdata->map.pos.lon,
384 context->appdata->map.zoom);
385 context->map_complete = TRUE;
386 }
387
388 return FALSE;
389 }
390
391 static void
392 map_cachelist_nearest(cache_t *cache, pos_t *pos,
393 cache_t **result, float *distance) {
394
395 while(cache) {
396 pos_t cpos = gpx_cache_pos(cache);
397
398 float dist =
399 pow(cpos.lat - pos->lat, 2) +
400 pow(cpos.lon - pos->lon, 2);
401
402 if(!(dist > *distance)) {
403 *result = cache;
404 *distance = dist;
405 }
406
407 cache = cache->next;
408 }
409 }
410
411 static cache_t *map_closest(map_context_t *context, pos_t *pos) {
412 cache_t *result = NULL;
413 float distance = NAN;
414
415 if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {
416 /* search all geocaches */
417 gpx_t *gpx = context->appdata->gpx;
418 while(gpx) {
419 map_cachelist_nearest(gpx->cache, pos, &result, &distance);
420 gpx = gpx->next;
421 }
422 } else if(context->appdata->cur_gpx) {
423 map_cachelist_nearest(context->appdata->cur_gpx->cache,
424 pos, &result, &distance);
425 } else
426 result = context->appdata->cur_gpx->cache;
427
428 return result;
429 }
430
431 /* translate between osm-gps-map positions and gpxview ones */
432 pos_t coord2pos(coord_t coo) {
433 pos_t pos;
434 pos.lat = rad2deg(coo.rlat);
435 pos.lon = rad2deg(coo.rlon);
436 return pos;
437 }
438
439 #define CLICK_FUZZ (24)
440
441 static gboolean
442 on_map_button_press_event(GtkWidget *widget,
443 GdkEventButton *event, map_context_t *context) {
444
445 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
446
447 /* check if we actually clicked parts of the OSD */
448 if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE)
449 return FALSE;
450
451 /* got a press event without release event? eat it! */
452 if(context->press_on != NULL) {
453 printf("PRESS: already\n");
454 return FALSE;
455 }
456
457 pos_t pos =
458 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
459
460 cache_t *nearest = map_closest(context, &pos);
461 if(nearest) {
462 pos_t cpos = gpx_cache_pos(nearest);
463
464 float dist = gpx_pos_get_distance(pos, cpos, FALSE);
465 if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ)
466 context->press_on = nearest;
467 }
468
469 return FALSE;
470 }
471
472 static void
473 cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) {
474 /* convert the pixbuf into something cairo can handle */
475
476 // Create a new ImageSurface
477 cairo_surface_t *image_surface =
478 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
479 gdk_pixbuf_get_width(buf),
480 gdk_pixbuf_get_height(buf));
481
482 // Create the new Context for the ImageSurface
483 g_assert(image_surface);
484 cairo_t *context = cairo_create(image_surface);
485
486 // Draw the image on the new Context
487 gdk_cairo_set_source_pixbuf(context, buf, 0.0, 0.0);
488 cairo_paint(context);
489
490 // now draw this onto the original context
491 cairo_set_source_surface(cr, image_surface, x, y);
492
493 cairo_paint(cr);
494 }
495
496 #ifndef BIG_BALLOONS
497 #define ICON_SIZE ICON_CACHE_TYPE
498 #define LINE_SKIP 7
499 #else
500 #define ICON_SIZE ICON_CACHE_TYPE_1_5X
501 #define LINE_SKIP 12
502 #endif
503
504 static void
505 balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) {
506 printf("balloon event: ");
507
508 map_context_t *context = (map_context_t*)data;
509 cache_t *cache = context->balloon;
510
511 if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW) {
512 printf("draw\n");
513
514 #if 0
515 /* draw pink background to check clipping */
516 cairo_rectangle (event->data.draw.cr,
517 event->data.draw.rect->x-20, event->data.draw.rect->y-20,
518 event->data.draw.rect->w+40, event->data.draw.rect->h+40);
519 cairo_set_source_rgba (event->data.draw.cr, 1, 0, 0, 0.3);
520 cairo_fill_preserve (event->data.draw.cr);
521 cairo_set_line_width (event->data.draw.cr, 0);
522 cairo_stroke (event->data.draw.cr);
523 #endif
524
525 /* leave a little border top and left */
526 gint x = event->data.draw.rect->x, y = event->data.draw.rect->y;
527
528 /* draw the cache type icon ... */
529 GdkPixbuf *icon = icon_get(ICON_SIZE, cache->type);
530 cairo_draw_pixbuf(event->data.draw.cr, icon, x, y);
531
532 if(cache->notes && cache->notes->override) {
533 GdkPixbuf *over = icon_get(ICON_SIZE, 11);
534 cairo_draw_pixbuf(event->data.draw.cr, over, x, y);
535 }
536
537 /* ... and right of it the waypoint id */
538 cairo_text_extents_t extents;
539
540 if(cache->id) {
541 cairo_select_font_face (event->data.draw.cr, "Sans",
542 CAIRO_FONT_SLANT_NORMAL,
543 CAIRO_FONT_WEIGHT_BOLD);
544
545 #ifndef BIG_BALLOONS
546 cairo_set_font_size (event->data.draw.cr, 20.0);
547 #else
548 cairo_set_font_size (event->data.draw.cr, 36.0);
549 #endif
550
551 cairo_text_extents (event->data.draw.cr, cache->id, &extents);
552
553 /* display id right of icon vertically centered */
554 x += gdk_pixbuf_get_width(icon) + 5;
555 y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
556 cairo_move_to (event->data.draw.cr, x, y);
557 cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
558 cairo_show_text (event->data.draw.cr, cache->id);
559 cairo_stroke (event->data.draw.cr);
560
561 y += (gdk_pixbuf_get_height(icon) - extents.height)/2 + LINE_SKIP;
562 } else
563 y += gdk_pixbuf_get_height(icon);
564
565 /* return to the left border and below icon/text */
566 x = event->data.draw.rect->x;
567
568 /* everything from here uses the same font */
569 cairo_select_font_face (event->data.draw.cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
570 CAIRO_FONT_WEIGHT_NORMAL);
571 #ifndef BIG_BALLOONS
572 #define FONT_SIZE 14.0
573 #else
574 #define FONT_SIZE 22.0
575 #endif
576
577 cairo_set_font_size (event->data.draw.cr, FONT_SIZE);
578
579 if(cache->name) {
580 /* draw cache name */
581 cairo_text_extents (event->data.draw.cr, cache->name, &extents);
582 y += extents.height;
583 cairo_move_to (event->data.draw.cr, x, y);
584 cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
585 cairo_show_text (event->data.draw.cr, cache->name);
586 cairo_stroke (event->data.draw.cr);
587
588 /* return to the left border and below text */
589 y += LINE_SKIP;
590 x = event->data.draw.rect->x;
591 }
592
593 if(cache->terrain) {
594 int text_y = 0, icon_y = 0;
595
596 /* draw cache rating */
597 const char *terrain = "Terrain:";
598 icon = icon_get(ICON_STARS, (int)(cache->terrain*2-2));
599 cairo_text_extents (event->data.draw.cr, _(terrain), &extents);
600
601 if(gdk_pixbuf_get_height(icon) > FONT_SIZE)
602 text_y = (gdk_pixbuf_get_height(icon) - FONT_SIZE)/2;
603 else
604 icon_y = (FONT_SIZE - gdk_pixbuf_get_height(icon))/2;
605
606 /* draw "Terrain:" string */
607 cairo_move_to (event->data.draw.cr, x, y - extents.y_bearing + text_y);
608 cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
609 cairo_show_text (event->data.draw.cr, _(terrain));
610 cairo_stroke (event->data.draw.cr);
611 x += extents.width + 2;
612
613 /* draw terrain stars */
614 cairo_draw_pixbuf(event->data.draw.cr, icon, x, y + icon_y);
615
616 x += gdk_pixbuf_get_width(icon) + LINE_SKIP - 2;
617 }
618
619 if(cache->difficulty) {
620 int text_y = 0, icon_y = 0;
621
622 const char *difficulty = "Difficulty:";
623 icon = icon_get(ICON_STARS, (int)(cache->difficulty*2-2));
624 cairo_text_extents (event->data.draw.cr, _(difficulty), &extents);
625
626 if(gdk_pixbuf_get_height(icon) > FONT_SIZE)
627 text_y = (gdk_pixbuf_get_height(icon) - FONT_SIZE)/2;
628 else
629 icon_y = (FONT_SIZE - gdk_pixbuf_get_height(icon))/2;
630
631 /* draw "Difficulty:" string */
632 cairo_move_to (event->data.draw.cr, x, y - extents.y_bearing + text_y);
633 cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
634 cairo_show_text (event->data.draw.cr, _(difficulty));
635 cairo_stroke (event->data.draw.cr);
636 x += extents.width + 2;
637
638 cairo_draw_pixbuf(event->data.draw.cr, icon, x, y + icon_y);
639 }
640 } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_CLICK) {
641 printf("click %s event at %d %d\n",
642 event->data.click.down?"down":"up",
643 event->data.click.x, event->data.click.y);
644
645 /* make the main screen jump to that cache */
646 if(!event->data.click.down) {
647 if(context->appdata->cur_cache) {
648 printf("ERROR: no current cache should be visible!\n");
649 } else {
650 gpx_t *is_in = NULL;
651
652 if(!context->appdata->cur_gpx) {
653 printf("click while in \"all\" view\n");
654
655 /* we first need to figure out which gpx file this cache */
656 /* is in so we can open it first */
657 gpx_t *gpx = context->appdata->gpx;
658 while(gpx && !is_in) {
659 cache_t *cur = gpx->cache;
660 while(cur && !is_in) {
661 if(cur == cache)
662 is_in = gpx;
663 cur = cur->next;
664 }
665 gpx = gpx->next;
666 }
667
668 if(is_in)
669 gpxlist_goto_cachelist(context->appdata, is_in);
670
671 } else
672 /* the simple case: there already is an open gpx file and */
673 /* we just jump into the "cache" view */
674 is_in = context->appdata->cur_gpx;
675
676 if(is_in) {
677 printf("selecting %s in %s\n",
678 cache->id,
679 context->appdata->cur_gpx->name);
680
681 cachelist_goto_cache(context->appdata, cache);
682
683 /* give focus to main screen (important for maemo) */
684 printf("raising main window\n");
685 gtk_window_present(GTK_WINDOW(context->appdata->window));
686 }
687 }
688 }
689 } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED) {
690 printf("removed\n");
691 context->balloon = NULL;
692 }
693 }
694
695 static gboolean
696 on_map_button_release_event(GtkWidget *widget,
697 GdkEventButton *event, map_context_t *context) {
698 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
699
700 /* in "MAP_CACHE" state only one cache is visible */
701 /* and the map is in navigation mode. the balloon is */
702 /* pretty useless there */
703 if(context->press_on && (context->state != MAP_CACHE)) {
704
705 coord_t coo;
706 coo = osm_gps_map_get_co_ordinates(map, event->x, event->y);
707
708 pos_t pos =
709 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
710
711 cache_t *nearest = map_closest(context, &pos);
712 if(nearest && nearest == context->press_on) {
713 pos_t cpos = gpx_cache_pos(nearest);
714
715 float dist = gpx_pos_get_distance(pos, cpos, FALSE);
716 if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ) {
717
718 context->balloon = nearest;
719 osm_gps_map_osd_draw_balloon(map, cpos.lat, cpos.lon,
720 balloon_cb, context);
721 }
722 }
723 context->press_on = NULL;
724 } else {
725 /* save new map position */
726 gfloat lat, lon;
727 g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
728 context->appdata->map.pos.lat = lat;
729 context->appdata->map.pos.lon = lon;
730 }
731
732 return FALSE;
733 }
734
735 static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
736 appdata_t *appdata = context->appdata;
737
738 /* save map parameters */
739 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
740 gint zoom;
741 g_object_get(map, "zoom", &zoom, NULL);
742 context->appdata->map.zoom = zoom;
743
744 gfloat lat, lon;
745 g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
746 context->appdata->map.pos.lat = lat;
747 context->appdata->map.pos.lon = lon;
748
749 gint source;
750 g_object_get(map, "map-source", &source, NULL);
751 context->appdata->map.source = source;
752
753 #if MAEMO_VERSION_MAJOR == 5
754 /* restore cur_view */
755 context->appdata->cur_view = context->old_view;
756 #endif
757
758 gtk_timeout_remove(context->handler_id);
759 gps_unregister_callback(appdata, context->cb_id);
760
761 if(context->caches_displayed) {
762 g_free(context->caches_displayed);
763 context->caches_displayed = NULL;
764 }
765
766 g_free(context);
767 appdata->map.context = NULL;
768 }
769
770 #if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__)
771 /* get access to zoom buttons */
772 static void
773 on_window_realize(GtkWidget *widget, gpointer data) {
774 if (widget->window) {
775 unsigned char value = 1;
776 Atom hildon_zoom_key_atom =
777 gdk_x11_get_xatom_by_name("_HILDON_ZOOM_KEY_ATOM"),
778 integer_atom = gdk_x11_get_xatom_by_name("INTEGER");
779 Display *dpy =
780 GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(widget->window));
781 Window w = GDK_WINDOW_XID(widget->window);
782
783 XChangeProperty(dpy, w, hildon_zoom_key_atom,
784 integer_atom, 8, PropModeReplace, &value, 1);
785 }
786 }
787 #endif
788
789 /* on maemo a window is either on top or completely invisible. this */
790 /* means that we only need to update the map window if its raised. */
791 /* on ordinary desktops this is different and we always update */
792
793 static gboolean on_focus_in(GtkWidget *widget, GdkEventFocus *event,
794 gpointer data) {
795 map_context_t *context = (map_context_t*)data;
796
797 printf("map got focus\n");
798
799 #ifdef USE_MAEMO
800 /* re-enable refresh of map */
801 if(!context->handler_id)
802 context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
803 #endif
804
805 map_setup(context);
806 return FALSE;
807 }
808
809 #ifdef USE_MAEMO
810 static gboolean on_focus_out(GtkWidget *widget, GdkEventFocus *event,
811 gpointer data) {
812 map_context_t *context = (map_context_t*)data;
813
814 printf("map lost focus\n");
815 gtk_timeout_remove(context->handler_id);
816 context->handler_id = 0;
817
818 return FALSE;
819 }
820 #endif
821
822 void map_update(appdata_t *appdata) {
823 printf("map_update\n");
824 #ifndef USE_MAEMO
825 if(appdata->map.context)
826 map_setup(appdata->map.context);
827 #endif
828 }
829
830 /* callback called by the gps layer whenever gps state changes */
831 static void
832 gps_callback(struct gps_state *state, gpointer data) {
833 printf("map: gps callback\n");
834 }
835
836 void map(appdata_t *appdata) {
837 map_context_t *context = NULL;
838
839 /* if the map window already exists, just raise it */
840 if(appdata->map.context) {
841 printf("using existing map!\n");
842 gtk_window_present(GTK_WINDOW(appdata->map.context->window));
843 map_setup(appdata->map.context);
844 return;
845 }
846
847 context = appdata->map.context = g_new0(map_context_t, 1);
848 context->appdata = appdata;
849 context->map_complete = FALSE;
850 context->state = MAP_NONE;
851
852 /* cleanup old (pre 0.8.7) path if it exists */
853 char *old_path = g_strdup_printf("%s/map/", appdata->image_path);
854 if(g_file_test(old_path, G_FILE_TEST_IS_DIR)) {
855 printf("old file path %s exists\n", old_path);
856 rmdir_recursive(old_path);
857 }
858
859 /* It is recommanded that all applications share these same */
860 /* map path, so data is only cached once. The path should be: */
861 /* ~/.osm-gps-map on standard PC (users home) */
862 /* /home/user/.osm-gps-map on Maemo5 (ext3 on internal card) */
863 /* /media/mmc2/osm-gps-map on Maemo4 (vfat on internal card) */
864 #if !defined(USE_MAEMO)
865 char *p = getenv("HOME");
866 if(!p) p = "/tmp";
867 char *path = g_strdup_printf("%s/.osm-gps-map", p);
868 #else
869 #if MAEMO_VERSION_MAJOR == 5
870 char *path = g_strdup("/home/user/.osm-gps-map");
871 #else
872 char *path = g_strdup("/media/mmc2/osm-gps-map");
873 #endif
874 #endif
875
876 const char *proxy = get_proxy_uri(appdata);
877
878 gint source = context->appdata->map.source;
879 if(!source) source = MAP_SOURCE;
880
881 context->widget = g_object_new(OSM_TYPE_GPS_MAP,
882 "map-source", source,
883 "tile-cache", path,
884 "auto-center", FALSE,
885 "record-trip-history", FALSE,
886 "show-trip-history", FALSE,
887 proxy?"proxy-uri":NULL, proxy,
888 NULL);
889
890 g_free(path);
891
892 osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget));
893
894 #ifdef USE_MAEMO
895 /* we don't use a stackable window here on fremantle, since */
896 /* this leaves the main window independent from the map and */
897 /* the user can e.g. still navigate the main menu */
898 context->window = hildon_window_new();
899
900 #if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__)
901 g_signal_connect(G_OBJECT(context->window), "realize",
902 G_CALLBACK(on_window_realize), NULL);
903 #endif // MAEMO_VERSION
904 #else
905 context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
906 #endif
907
908 #ifndef USE_MAEMO
909 gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);
910 #endif
911
912 g_signal_connect(G_OBJECT(context->widget), "focus-in-event",
913 G_CALLBACK(on_focus_in), context);
914
915 #ifdef USE_MAEMO
916 g_signal_connect(G_OBJECT(context->widget), "focus-out-event",
917 G_CALLBACK(on_focus_out), context);
918 #endif
919
920 g_signal_connect(G_OBJECT(context->widget), "configure-event",
921 G_CALLBACK(on_map_configure), context);
922
923 g_signal_connect(G_OBJECT(context->widget), "button-press-event",
924 G_CALLBACK(on_map_button_press_event), context);
925
926 g_signal_connect(G_OBJECT(context->widget), "button-release-event",
927 G_CALLBACK(on_map_button_release_event), context);
928
929 /* install handler for timed updates of the gps button */
930 context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
931 context->cb_id = gps_register_callback(appdata, gps_callback, context);
932
933 #if MAEMO_VERSION_MAJOR == 5
934 /* prevent some of the main screen things */
935 context->old_view = appdata->cur_view;
936 appdata->cur_view = NULL;
937 #endif
938
939 g_signal_connect(G_OBJECT(context->window), "destroy",
940 G_CALLBACK(on_window_destroy), context);
941
942 gtk_container_add(GTK_CONTAINER(context->window), context->widget);
943 gtk_widget_show_all(GTK_WIDGET(context->window));
944 }