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

Parent Directory Parent Directory | Revision Log Revision Log


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