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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 56 - (hide annotations)
Fri Aug 14 12:19:45 2009 UTC (14 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 22850 byte(s)
Map balloon started
1 harbaum 33 /*
2 harbaum 55 * Copyright (C) 2008-2009 Till Harbaum <till@harbaum.org>.
3 harbaum 33 *
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 harbaum 34 #include <math.h> // for isnan
22 harbaum 33
23     #ifdef ENABLE_OSM_GPS_MAP
24     #include "osm-gps-map.h"
25     #endif
26    
27 harbaum 55 #define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENSTREETMAP
28 harbaum 48 #define GPS_DEFAULT_ZOOM 13
29    
30 harbaum 42 /* 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 harbaum 34 #define PROXY_KEY "/system/http_proxy/"
37    
38     static const char *get_proxy_uri(appdata_t *appdata) {
39     static char proxy_buffer[64] = "";
40 harbaum 33
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 harbaum 34 /* ------------- get proxy settings -------------------- */
49     if(gconf_client_get_bool(appdata->gconf_client,
50     PROXY_KEY "use_http_proxy", NULL)) {
51 harbaum 33
52 harbaum 34 /* 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 harbaum 33
63 harbaum 34 snprintf(proxy_buffer, sizeof(proxy_buffer),
64     "http://%s:%u", host, port);
65 harbaum 33
66 harbaum 34 g_free(host);
67     }
68 harbaum 35 return proxy_buffer;
69 harbaum 34 }
70    
71 harbaum 35 return NULL;
72 harbaum 33 }
73    
74     static void map_zoom(map_context_t *context, int step) {
75 harbaum 48 gint zoom;
76 harbaum 33 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 harbaum 55 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 harbaum 48
86 harbaum 56 /* hmm ... this doesn't really work */
87     osm_gps_map_osd_speed(map, zoom);
88    
89 harbaum 48 /* save new zoom */
90     context->appdata->map.zoom = zoom;
91 harbaum 33 }
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 harbaum 34 pos_t *refpos = get_pos(context->appdata);
108     if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
109 harbaum 35 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
110 harbaum 48 refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
111 harbaum 35 } 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 harbaum 34 }
116 harbaum 33
117     return FALSE;
118     }
119    
120     static GtkWidget
121 harbaum 51 *map_add_button(int icon, GCallback cb, gpointer data,
122 harbaum 33 char *tooltip) {
123     GtkWidget *button = gtk_button_new();
124 harbaum 51 gtk_button_set_image(GTK_BUTTON(button), icon_get_widget(ICON_MISC, icon));
125 harbaum 33 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 harbaum 55 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 harbaum 33 static gboolean map_gps_update(gpointer data) {
137     map_context_t *context = (map_context_t*)data;
138    
139 harbaum 51 /* get reference position ... */
140 harbaum 34 pos_t *refpos = get_pos(context->appdata);
141     gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
142 harbaum 33
143 harbaum 51 /* ... and enable "goto" button if it's valid */
144 harbaum 34 gtk_widget_set_sensitive(context->gps, ok);
145    
146 harbaum 51 if(ok) {
147 harbaum 53 float heading = NAN;
148 harbaum 54 int radius = 0;
149 harbaum 51
150 harbaum 53 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 harbaum 55 if(!isnan(eph))
156     radius = dist2pixel(context, eph/1000, refpos->lat);
157 harbaum 53 }
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 harbaum 51 refpos->lat, refpos->lon, heading);
162     } else
163     osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));
164    
165 harbaum 33 return TRUE;
166     }
167    
168 harbaum 35 static gboolean on_map_configure(GtkWidget *widget,
169     GdkEventConfigure *event,
170     map_context_t *context) {
171 harbaum 33
172 harbaum 48 /* 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 harbaum 35
191 harbaum 48 /* 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 harbaum 35 return FALSE;
198     }
199    
200 harbaum 38 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 harbaum 41 /* draw a nice popup */
212     typedef struct {
213     appdata_t *appdata;
214     GtkWidget *window;
215     GMainLoop *loop;
216 harbaum 55 cache_t *cache;
217 harbaum 41 } popup_context_t;
218    
219 harbaum 47 /* draw shape */
220     #define ARROW_BORDER 20
221     #define CORNER_RADIUS 10
222 harbaum 41
223 harbaum 47 #ifndef USE_MAEMO
224 harbaum 41 #define POPUP_WIDTH 300
225     #define POPUP_HEIGHT 100
226     #else
227 harbaum 55 #define POPUP_WIDTH 360
228 harbaum 47 #define POPUP_HEIGHT 120
229 harbaum 41 #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 harbaum 45 printf("overlay button press (in = %d)\n", in);
253 harbaum 41 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 harbaum 45 printf("overlay button release (in = %d)\n", in);
262 harbaum 41
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 harbaum 46 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 harbaum 47 /* 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 harbaum 46 gdk_draw_rectangle(mask, gc, TRUE,
322 harbaum 47 0, tip_offset + CORNER_RADIUS,
323 harbaum 46 POPUP_WIDTH,
324 harbaum 47 POPUP_HEIGHT - 2*CORNER_RADIUS - ARROW_BORDER);
325 harbaum 46
326     gdk_draw_rectangle(mask, gc, TRUE,
327 harbaum 47 CORNER_RADIUS, tip_offset,
328 harbaum 46 POPUP_WIDTH - 2*CORNER_RADIUS,
329 harbaum 47 POPUP_HEIGHT - ARROW_BORDER);
330 harbaum 46
331     int off[][2] = {
332 harbaum 47 { CORNER_RADIUS, tip_offset + CORNER_RADIUS },
333     { POPUP_WIDTH - CORNER_RADIUS, tip_offset + CORNER_RADIUS },
334 harbaum 46 { POPUP_WIDTH - CORNER_RADIUS,
335 harbaum 47 POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset},
336 harbaum 46 { CORNER_RADIUS,
337 harbaum 47 POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset}};
338 harbaum 46
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 harbaum 47 /* 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 harbaum 55 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 harbaum 42 void cache_popup(map_context_t *mcontext, cache_t *cache) {
395     popup_context_t pcontext;
396     pcontext.appdata = mcontext->appdata;
397 harbaum 55 pcontext.cache = cache;
398 harbaum 41
399 harbaum 42 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 harbaum 41 POPUP_WIDTH, POPUP_HEIGHT);
403 harbaum 45 gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT);
404 harbaum 42 // 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 harbaum 41
412     /* connect events */
413 harbaum 42 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 harbaum 41
424 harbaum 42 gdk_pointer_grab(pcontext.window->window, TRUE,
425 harbaum 41 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
426     NULL, NULL, GDK_CURRENT_TIME);
427 harbaum 42 gtk_grab_add(pcontext.window);
428 harbaum 41
429 harbaum 42 /* check whether cache is in upper or lower half of window */
430 harbaum 45 gint x, y, sx, sy;
431 harbaum 42 osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget),
432     cache->pos.lat, cache->pos.lon,
433 harbaum 45 &sx, &sy);
434 harbaum 42
435     gdk_window_get_origin(mcontext->widget->window, &x, &y);
436    
437 harbaum 45 gint ax = 0, ay = 0;
438     if(sx > mcontext->widget->allocation.width/2)
439     ax = POPUP_WIDTH;
440 harbaum 41
441 harbaum 45 if(sy > mcontext->widget->allocation.height/2)
442     ay = POPUP_HEIGHT;
443 harbaum 41
444 harbaum 47 #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
445 harbaum 46 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 harbaum 42 gtk_window_move(GTK_WINDOW(pcontext.window),
451 harbaum 45 x + mcontext->widget->allocation.x + sx - ax,
452     y + mcontext->widget->allocation.y + sy - ay);
453 harbaum 41
454 harbaum 46
455 harbaum 47 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 harbaum 41
461 harbaum 47 /* --- actual content ---- */
462     GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
463 harbaum 55 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
464 harbaum 47
465 harbaum 55 gtk_box_pack_start(GTK_BOX(hbox),
466     icon_get_widget(ICON_CACHE_TYPE, cache->type),
467     FALSE, FALSE, 5);
468 harbaum 47
469 harbaum 55 if(cache->id)
470     gtk_box_pack_start_defaults(GTK_BOX(hbox),
471     gtk_label_big_left_new(cache->id));
472 harbaum 47
473 harbaum 56 #if 0
474 harbaum 55 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 harbaum 56 #endif
480 harbaum 47
481 harbaum 55 gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox);
482 harbaum 47
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 harbaum 55 hbox = gtk_hbox_new(FALSE, 0);
490 harbaum 47 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 harbaum 41
518 harbaum 47 /* give window its shape */
519 harbaum 46 popup_window_shape(pcontext.window, ax, ay);
520    
521 harbaum 42 gtk_widget_show_all(pcontext.window);
522 harbaum 41
523     /* handle this popup until it's gone */
524    
525 harbaum 42 pcontext.loop = g_main_loop_new(NULL, FALSE);
526 harbaum 41
527     GDK_THREADS_LEAVE();
528 harbaum 42 g_main_loop_run(pcontext.loop);
529 harbaum 41 GDK_THREADS_ENTER();
530    
531 harbaum 42 g_main_loop_unref(pcontext.loop);
532 harbaum 41
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 harbaum 47 #define CLICK_FUZZ (24)
585 harbaum 42
586 harbaum 41 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 harbaum 44 /* 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 harbaum 41 pos_t pos =
598 harbaum 42 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
599 harbaum 41
600 harbaum 42 cache_t *nearest = map_closest(context, &pos);
601     if(nearest) {
602     float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
603 harbaum 44 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
604 harbaum 42 context->press_on = nearest;
605     }
606 harbaum 44
607 harbaum 41 return FALSE;
608     }
609    
610     static gboolean
611     on_map_button_release_event(GtkWidget *widget,
612     GdkEventButton *event, map_context_t *context) {
613 harbaum 48 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
614    
615 harbaum 42 if(context->press_on) {
616     pos_t pos =
617     coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
618 harbaum 41
619 harbaum 42 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 harbaum 44 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
623 harbaum 42 cache_popup(context, nearest);
624     }
625 harbaum 44 context->press_on = NULL;
626 harbaum 48 } 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 harbaum 41 }
633    
634     return FALSE;
635     }
636    
637 harbaum 56 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 harbaum 48 /* 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 harbaum 44
648 harbaum 48 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 harbaum 40 #if MAEMO_VERSION_MAJOR == 5
654     /* restore cur_view */
655     context->appdata->cur_view = context->old_view;
656 harbaum 56 #endif
657 harbaum 40
658     gtk_timeout_remove(context->handler_id);
659 harbaum 56
660 harbaum 40 g_free(context);
661 harbaum 56 appdata->map.context = NULL;
662 harbaum 40 }
663    
664 harbaum 33 void map(appdata_t *appdata) {
665 harbaum 56 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 harbaum 40 context->appdata = appdata;
675 harbaum 33
676 harbaum 41 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 harbaum 55 "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 harbaum 41 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 harbaum 44 name = g_strdup(appdata->cur_gpx->name);
707 harbaum 41 }
708     #endif
709    
710     char *title = g_strdup_printf(_("Map - %s"), name);
711     g_free(name);
712    
713 harbaum 56 #ifdef USE_MAEMO
714     #ifdef USE_STACKABLE_WINDOW
715     context->window = hildon_stackable_window_new();
716 harbaum 40 #else
717 harbaum 56 context->window = hildon_window_new();
718     #endif
719     #else
720     context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
721     #endif
722 harbaum 33
723 harbaum 56 gtk_window_set_title(GTK_WINDOW(context->window), title);
724    
725 harbaum 33 #ifndef USE_MAEMO
726 harbaum 56 gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);
727 harbaum 33 #endif
728    
729 harbaum 41 g_free(title);
730 harbaum 33
731 harbaum 41 g_signal_connect(G_OBJECT(context->widget), "configure-event",
732     G_CALLBACK(on_map_configure), context);
733 harbaum 33
734 harbaum 41 g_signal_connect(G_OBJECT(context->widget), "button-press-event",
735     G_CALLBACK(on_map_button_press_event), context);
736 harbaum 33
737 harbaum 40 g_signal_connect(G_OBJECT(context->widget), "button-release-event",
738     G_CALLBACK(on_map_button_release_event), context);
739 harbaum 33
740 harbaum 40 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget);
741 harbaum 33 /* zoom button box */
742     GtkWidget *vbox = gtk_vbox_new(FALSE,0);
743    
744 harbaum 40 context->zoomin =
745 harbaum 51 map_add_button(10, G_CALLBACK(cb_map_zoomin),
746 harbaum 40 context, _("Zoom in"));
747     gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0);
748 harbaum 33
749 harbaum 40 context->zoomout =
750 harbaum 51 map_add_button(11, G_CALLBACK(cb_map_zoomout),
751 harbaum 40 context, _("Zoom out"));
752     gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0);
753 harbaum 33
754 harbaum 40 context->gps =
755 harbaum 51 map_add_button(9, G_CALLBACK(cb_map_gps),
756 harbaum 40 context, _("Jump to GPS position"));
757     gtk_widget_set_sensitive(context->gps, FALSE);
758 harbaum 56
759 harbaum 33 /* install handler for timed updates of the gps button */
760 harbaum 40 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 harbaum 33
763     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
764    
765 harbaum 40 #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 harbaum 56 #endif
770 harbaum 40
771 harbaum 56 g_signal_connect(G_OBJECT(context->window), "destroy",
772 harbaum 40 G_CALLBACK(on_window_destroy), context);
773    
774 harbaum 56 gtk_container_add(GTK_CONTAINER(context->window), hbox);
775     gtk_widget_show_all(GTK_WIDGET(context->window));
776 harbaum 33 }