Parent Directory | Revision Log
Map marker
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of GPXView. |
5 | * |
6 | * GPXView is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * GPXView is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with GPXView. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "gpxview.h" |
21 | #include <math.h> // for isnan |
22 | |
23 | #ifdef ENABLE_OSM_GPS_MAP |
24 | #include "osm-gps-map.h" |
25 | #endif |
26 | |
27 | /* equatorial radius in meters */ |
28 | #define EQ_RADIUS (6378137.0) |
29 | |
30 | #define RAD2DEG(a) (((a)*180.0)/M_PI) |
31 | #define DEG2RAD(a) (((a)*M_PI)/180.0) |
32 | |
33 | typedef struct { |
34 | appdata_t *appdata; |
35 | GtkWidget *widget; |
36 | GtkWidget *zoomin, *zoomout, *gps; |
37 | gint handler_id; |
38 | cache_t *press_on; |
39 | #if MAEMO_VERSION_MAJOR == 5 |
40 | GtkWidget *old_view; |
41 | #endif |
42 | } map_context_t; |
43 | |
44 | #define PROXY_KEY "/system/http_proxy/" |
45 | |
46 | static const char *get_proxy_uri(appdata_t *appdata) { |
47 | static char proxy_buffer[64] = ""; |
48 | |
49 | /* use environment settings if preset */ |
50 | const char *proxy = g_getenv("http_proxy"); |
51 | if(proxy) { |
52 | printf("http_proxy: %s\n", proxy); |
53 | return proxy; |
54 | } |
55 | |
56 | /* ------------- get proxy settings -------------------- */ |
57 | if(gconf_client_get_bool(appdata->gconf_client, |
58 | PROXY_KEY "use_http_proxy", NULL)) { |
59 | |
60 | /* we can savely ignore things like "ignore_hosts" since we */ |
61 | /* are pretty sure not inside the net of one of our map renderers */ |
62 | /* (unless the user works at google :-) */ |
63 | |
64 | /* get basic settings */ |
65 | char *host = |
66 | gconf_client_get_string(appdata->gconf_client, PROXY_KEY "host", NULL); |
67 | if(host) { |
68 | int port = |
69 | gconf_client_get_int(appdata->gconf_client, PROXY_KEY "port", NULL); |
70 | |
71 | snprintf(proxy_buffer, sizeof(proxy_buffer), |
72 | "http://%s:%u", host, port); |
73 | |
74 | g_free(host); |
75 | } |
76 | return proxy_buffer; |
77 | } |
78 | |
79 | return NULL; |
80 | } |
81 | |
82 | static void map_zoom(map_context_t *context, int step) { |
83 | int zoom; |
84 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
85 | g_object_get(map, "zoom", &zoom, NULL); |
86 | zoom = osm_gps_map_set_zoom(map, zoom+step); |
87 | |
88 | /* enable/disable zoom buttons as required */ |
89 | gtk_widget_set_sensitive(context->zoomin, zoom<17); |
90 | gtk_widget_set_sensitive(context->zoomout, zoom>1); |
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, 14); |
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(const gchar *icon, GCallback cb, gpointer data, |
122 | char *tooltip) { |
123 | GtkWidget *button = gtk_button_new(); |
124 | gtk_button_set_image(GTK_BUTTON(button), |
125 | gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON)); |
126 | g_signal_connect(button, "clicked", cb, data); |
127 | #ifndef USE_MAEMO |
128 | gtk_widget_set_tooltip_text(button, tooltip); |
129 | #endif |
130 | return button; |
131 | } |
132 | |
133 | static gboolean map_gps_update(gpointer data) { |
134 | map_context_t *context = (map_context_t*)data; |
135 | |
136 | pos_t *refpos = get_pos(context->appdata); |
137 | gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon); |
138 | |
139 | /* get reference position and go there */ |
140 | gtk_widget_set_sensitive(context->gps, ok); |
141 | |
142 | return TRUE; |
143 | } |
144 | |
145 | static gboolean on_map_configure(GtkWidget *widget, |
146 | GdkEventConfigure *event, |
147 | map_context_t *context) { |
148 | |
149 | cb_map_gps(NULL, context); |
150 | |
151 | return FALSE; |
152 | } |
153 | |
154 | static void map_draw_cachelist(GtkWidget *map, cache_t *cache) { |
155 | while(cache) { |
156 | GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); |
157 | |
158 | osm_gps_map_add_image(OSM_GPS_MAP(map), |
159 | cache->pos.lat, cache->pos.lon, icon); |
160 | |
161 | cache = cache->next; |
162 | } |
163 | } |
164 | |
165 | /* draw a nice popup */ |
166 | typedef struct { |
167 | appdata_t *appdata; |
168 | GtkWidget *window; |
169 | GMainLoop *loop; |
170 | } popup_context_t; |
171 | |
172 | |
173 | #ifndef USE_HILDON |
174 | #define POPUP_WIDTH 300 |
175 | #define POPUP_HEIGHT 100 |
176 | #else |
177 | #define POPUP_WIDTH 600 |
178 | #define POPUP_HEIGHT 200 |
179 | #endif |
180 | |
181 | static gboolean |
182 | pointer_in_window(GtkWidget *widget, gint x_root, gint y_root) { |
183 | if(GTK_WIDGET_MAPPED(gtk_widget_get_toplevel(widget))) { |
184 | gint window_x, window_y; |
185 | |
186 | gdk_window_get_position(gtk_widget_get_toplevel(widget)->window, |
187 | &window_x, &window_y); |
188 | |
189 | if(x_root >= window_x && x_root < window_x + widget->allocation.width && |
190 | y_root >= window_y && y_root < window_y + widget->allocation.height) |
191 | return TRUE; |
192 | } |
193 | |
194 | return FALSE; |
195 | } |
196 | |
197 | static gboolean |
198 | on_button_press_event(GtkWidget *widget, |
199 | GdkEventButton *event, popup_context_t *context) { |
200 | gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
201 | |
202 | printf("overlay button press (in = %d)\n", in); |
203 | return !in; |
204 | } |
205 | |
206 | static gboolean |
207 | on_button_release_event(GtkWidget *widget, |
208 | GdkEventButton *event, popup_context_t *context) { |
209 | gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
210 | |
211 | printf("overlay button release (in = %d)\n", in); |
212 | |
213 | if(!in) { |
214 | printf("destroying popup\n"); |
215 | gtk_widget_destroy(gtk_widget_get_toplevel(widget)); |
216 | } |
217 | |
218 | return !in; |
219 | } |
220 | |
221 | static void |
222 | shutdown_loop(popup_context_t *context) { |
223 | if(g_main_loop_is_running(context->loop)) |
224 | g_main_loop_quit(context->loop); |
225 | } |
226 | |
227 | static gint |
228 | run_delete_handler(GtkWindow *window, GdkEventAny *event, |
229 | popup_context_t *context) { |
230 | shutdown_loop(context); |
231 | return TRUE; /* Do not destroy */ |
232 | } |
233 | |
234 | static void |
235 | run_destroy_handler(GtkWindow *window, popup_context_t *context) { |
236 | /* shutdown_loop will be called by run_unmap_handler */ |
237 | printf("popup destroyed\n"); |
238 | } |
239 | |
240 | static void |
241 | run_unmap_handler(GtkWindow *window, popup_context_t *context) { |
242 | shutdown_loop(context); |
243 | } |
244 | |
245 | void cache_popup(map_context_t *mcontext, cache_t *cache) { |
246 | popup_context_t pcontext; |
247 | pcontext.appdata = mcontext->appdata; |
248 | |
249 | pcontext.window = gtk_window_new(GTK_WINDOW_POPUP); |
250 | gtk_widget_realize(pcontext.window); |
251 | gtk_window_set_default_size(GTK_WINDOW(pcontext.window), |
252 | POPUP_WIDTH, POPUP_HEIGHT); |
253 | gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT); |
254 | // gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE); |
255 | gtk_window_set_transient_for(GTK_WINDOW(pcontext.window), |
256 | GTK_WINDOW(mcontext->appdata->window)); |
257 | gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE); |
258 | gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE); |
259 | gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC); |
260 | gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE); |
261 | |
262 | /* connect events */ |
263 | g_signal_connect(G_OBJECT(pcontext.window), "button-press-event", |
264 | G_CALLBACK(on_button_press_event), &pcontext); |
265 | g_signal_connect(G_OBJECT(pcontext.window), "button-release-event", |
266 | G_CALLBACK(on_button_release_event), &pcontext); |
267 | g_signal_connect(G_OBJECT(pcontext.window), "delete-event", |
268 | G_CALLBACK(run_delete_handler), &pcontext); |
269 | g_signal_connect(G_OBJECT(pcontext.window), "destroy", |
270 | G_CALLBACK(run_destroy_handler), &pcontext); |
271 | g_signal_connect(G_OBJECT(pcontext.window), "unmap", |
272 | G_CALLBACK(run_unmap_handler), &pcontext); |
273 | |
274 | gdk_pointer_grab(pcontext.window->window, TRUE, |
275 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, |
276 | NULL, NULL, GDK_CURRENT_TIME); |
277 | gtk_grab_add(pcontext.window); |
278 | |
279 | /* check whether cache is in upper or lower half of window */ |
280 | gint x, y, sx, sy; |
281 | osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget), |
282 | cache->pos.lat, cache->pos.lon, |
283 | &sx, &sy); |
284 | |
285 | printf("screen pos %d/%d\n", sx, sy); |
286 | |
287 | gdk_window_get_origin(mcontext->widget->window, &x, &y); |
288 | printf("window = %d/%d +%d+%d %d*%d\n", x, y, |
289 | mcontext->widget->allocation.x, |
290 | mcontext->widget->allocation.y, |
291 | mcontext->widget->allocation.width, |
292 | mcontext->widget->allocation.height |
293 | ); |
294 | |
295 | gint ax = 0, ay = 0; |
296 | if(sx > mcontext->widget->allocation.width/2) |
297 | ax = POPUP_WIDTH; |
298 | |
299 | if(sy > mcontext->widget->allocation.height/2) |
300 | ay = POPUP_HEIGHT; |
301 | |
302 | gtk_window_move(GTK_WINDOW(pcontext.window), |
303 | x + mcontext->widget->allocation.x + sx - ax, |
304 | y + mcontext->widget->allocation.y + sy - ay); |
305 | |
306 | /* a frame with a vscale inside */ |
307 | GtkWidget *frame = gtk_frame_new(NULL); |
308 | gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); |
309 | |
310 | gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name)); |
311 | gtk_container_add(GTK_CONTAINER(pcontext.window), frame); |
312 | |
313 | gtk_widget_show_all(pcontext.window); |
314 | |
315 | /* handle this popup until it's gone */ |
316 | |
317 | pcontext.loop = g_main_loop_new(NULL, FALSE); |
318 | |
319 | GDK_THREADS_LEAVE(); |
320 | g_main_loop_run(pcontext.loop); |
321 | GDK_THREADS_ENTER(); |
322 | |
323 | g_main_loop_unref(pcontext.loop); |
324 | |
325 | printf("cache popup removed\n"); |
326 | } |
327 | |
328 | static void |
329 | map_cachelist_nearest(cache_t *cache, pos_t *pos, |
330 | cache_t **result, float *distance) { |
331 | while(cache) { |
332 | float dist = |
333 | pow(cache->pos.lat - pos->lat, 2) + |
334 | pow(cache->pos.lon - pos->lon, 2); |
335 | |
336 | if(!(dist > *distance)) { |
337 | *result = cache; |
338 | *distance = dist; |
339 | } |
340 | |
341 | cache = cache->next; |
342 | } |
343 | } |
344 | |
345 | static cache_t *map_closest(map_context_t *context, pos_t *pos) { |
346 | cache_t *result = NULL; |
347 | float distance = NAN; |
348 | |
349 | #ifdef USE_MAEMO |
350 | if(!context->appdata->cur_gpx) { |
351 | #endif |
352 | /* search all geocaches */ |
353 | gpx_t *gpx = context->appdata->gpx; |
354 | while(gpx) { |
355 | map_cachelist_nearest(gpx->cache, pos, &result, &distance); |
356 | gpx = gpx->next; |
357 | } |
358 | #ifdef USE_MAEMO |
359 | } else { |
360 | map_cachelist_nearest(context->appdata->cur_gpx->cache, |
361 | pos, &result, &distance); |
362 | } |
363 | #endif |
364 | |
365 | return result; |
366 | } |
367 | |
368 | /* translate between osm-gps-map positions and gpxview ones */ |
369 | pos_t coord2pos(coord_t coo) { |
370 | pos_t pos; |
371 | pos.lat = RAD2DEG(coo.rlat); |
372 | pos.lon = RAD2DEG(coo.rlon); |
373 | return pos; |
374 | } |
375 | |
376 | static int dist2pixel(map_context_t *context, float km, float lat) { |
377 | int zoom; |
378 | g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL); |
379 | |
380 | /* world at zoom 1 == 512 pixels */ |
381 | float m_per_pix = |
382 | cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom)); |
383 | |
384 | return 1000.0*km/m_per_pix; |
385 | } |
386 | |
387 | #define CLICK_FUZZ (16) |
388 | |
389 | static gboolean |
390 | on_map_button_press_event(GtkWidget *widget, |
391 | GdkEventButton *event, map_context_t *context) { |
392 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
393 | |
394 | /* got a press event without release event? eat it! */ |
395 | if(context->press_on != NULL) { |
396 | printf("PRESS: already\n"); |
397 | return TRUE; |
398 | } |
399 | |
400 | pos_t pos = |
401 | coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
402 | |
403 | cache_t *nearest = map_closest(context, &pos); |
404 | if(nearest) { |
405 | float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
406 | if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
407 | context->press_on = nearest; |
408 | } |
409 | |
410 | return FALSE; |
411 | } |
412 | |
413 | static gboolean |
414 | on_map_button_release_event(GtkWidget *widget, |
415 | GdkEventButton *event, map_context_t *context) { |
416 | if(context->press_on) { |
417 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
418 | |
419 | pos_t pos = |
420 | coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
421 | |
422 | cache_t *nearest = map_closest(context, &pos); |
423 | if(nearest && nearest == context->press_on) { |
424 | float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
425 | if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
426 | cache_popup(context, nearest); |
427 | } |
428 | context->press_on = NULL; |
429 | } |
430 | |
431 | return FALSE; |
432 | } |
433 | |
434 | |
435 | #if MAEMO_VERSION_MAJOR == 5 |
436 | static void on_window_destroy(GtkWidget *widget, map_context_t *context) { |
437 | printf("destroy map view\n"); |
438 | |
439 | /* restore cur_view */ |
440 | context->appdata->cur_view = context->old_view; |
441 | |
442 | gtk_timeout_remove(context->handler_id); |
443 | g_free(context); |
444 | } |
445 | #endif |
446 | |
447 | void map(appdata_t *appdata) { |
448 | map_context_t *context = g_new0(map_context_t, 1); |
449 | context->appdata = appdata; |
450 | |
451 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
452 | |
453 | char *path = g_strdup_printf("%s/map/", appdata->image_path); |
454 | const char *proxy = get_proxy_uri(appdata); |
455 | |
456 | context->widget = g_object_new(OSM_TYPE_GPS_MAP, |
457 | "repo-uri", MAP_SOURCE_OPENSTREETMAP, |
458 | "tile-cache", path, |
459 | proxy?"proxy-uri":NULL, proxy, |
460 | NULL); |
461 | |
462 | g_free(path); |
463 | |
464 | char *name = NULL; |
465 | #ifdef USE_MAEMO |
466 | if(!appdata->cur_gpx) { |
467 | #endif |
468 | /* draw all geocaches */ |
469 | gpx_t *gpx = appdata->gpx; |
470 | while(gpx) { |
471 | map_draw_cachelist(context->widget, gpx->cache); |
472 | gpx = gpx->next; |
473 | } |
474 | name = g_strdup(_("all geocaches")); |
475 | #ifdef USE_MAEMO |
476 | } else { |
477 | map_draw_cachelist(context->widget, appdata->cur_gpx->cache); |
478 | name = g_strdup(appdata->cur_gpx->name); |
479 | } |
480 | #endif |
481 | |
482 | char *title = g_strdup_printf(_("Map - %s"), name); |
483 | g_free(name); |
484 | |
485 | #if MAEMO_VERSION_MAJOR == 5 |
486 | GtkWidget *window = hildon_stackable_window_new(); |
487 | gtk_window_set_title(GTK_WINDOW(window), title); |
488 | #else |
489 | GtkWidget *dialog = gtk_dialog_new_with_buttons(title, |
490 | GTK_WINDOW(appdata->window), |
491 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
492 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
493 | NULL); |
494 | |
495 | #ifndef USE_MAEMO |
496 | gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480); |
497 | #else |
498 | gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480); |
499 | #endif |
500 | #endif |
501 | |
502 | g_free(title); |
503 | |
504 | g_signal_connect(G_OBJECT(context->widget), "configure-event", |
505 | G_CALLBACK(on_map_configure), context); |
506 | |
507 | g_signal_connect(G_OBJECT(context->widget), "button-press-event", |
508 | G_CALLBACK(on_map_button_press_event), context); |
509 | |
510 | g_signal_connect(G_OBJECT(context->widget), "button-release-event", |
511 | G_CALLBACK(on_map_button_release_event), context); |
512 | |
513 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget); |
514 | /* zoom button box */ |
515 | GtkWidget *vbox = gtk_vbox_new(FALSE,0); |
516 | |
517 | context->zoomin = |
518 | map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin), |
519 | context, _("Zoom in")); |
520 | gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0); |
521 | |
522 | context->zoomout = |
523 | map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout), |
524 | context, _("Zoom out")); |
525 | gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0); |
526 | |
527 | context->gps = |
528 | map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps), |
529 | context, _("Jump to GPS position")); |
530 | gtk_widget_set_sensitive(context->gps, FALSE); |
531 | /* install handler for timed updates of the gps button */ |
532 | context->handler_id = gtk_timeout_add(1000, map_gps_update, context); |
533 | gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0); |
534 | |
535 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
536 | |
537 | #if MAEMO_VERSION_MAJOR == 5 |
538 | /* prevent some of the main screen things */ |
539 | context->old_view = appdata->cur_view; |
540 | appdata->cur_view = NULL; |
541 | |
542 | g_signal_connect(G_OBJECT(window), "destroy", |
543 | G_CALLBACK(on_window_destroy), context); |
544 | |
545 | gtk_container_add(GTK_CONTAINER(window), hbox); |
546 | gtk_widget_show_all(GTK_WIDGET(window)); |
547 | |
548 | #else |
549 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
550 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); |
551 | gtk_widget_show_all(dialog); |
552 | gtk_dialog_run(GTK_DIALOG(dialog)); |
553 | gtk_timeout_remove(context->handler_id); |
554 | gtk_widget_destroy(dialog); |
555 | g_free(context); |
556 | #endif |
557 | } |