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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 46 - (show annotations)
Wed Aug 5 14:11:00 2009 UTC (14 years, 9 months ago) by harbaum
File MIME type: text/plain
File size: 18088 byte(s)
Popup shape
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 /* draw shape */
246 #define ARROW_BORDER 20
247 #define CORNER_RADIUS 10
248
249 static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) {
250 GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1);
251
252 GdkGC *gc = gdk_gc_new(mask);
253 GdkColormap *colormap;
254 GdkColor black;
255 GdkColor white;
256
257 /* get black/white color values */
258 colormap = gdk_colormap_get_system();
259 gdk_color_black(colormap, &black);
260 gdk_color_white(colormap, &white);
261
262 /* erase */
263 gdk_gc_set_foreground(gc, &black);
264 gdk_gc_set_background(gc, &white);
265
266 /* erase background */
267 gdk_draw_rectangle(mask, gc, TRUE, 0, 0, POPUP_WIDTH, POPUP_HEIGHT);
268
269 gdk_gc_set_foreground(gc, &white);
270 gdk_gc_set_background(gc, &black);
271
272 gdk_draw_rectangle(mask, gc, TRUE,
273 0, ARROW_BORDER + CORNER_RADIUS,
274 POPUP_WIDTH,
275 POPUP_HEIGHT - 2*CORNER_RADIUS - 2*ARROW_BORDER);
276
277 gdk_draw_rectangle(mask, gc, TRUE,
278 CORNER_RADIUS, ARROW_BORDER,
279 POPUP_WIDTH - 2*CORNER_RADIUS,
280 POPUP_HEIGHT - 2*ARROW_BORDER);
281
282 int off[][2] = {
283 { CORNER_RADIUS, ARROW_BORDER + CORNER_RADIUS },
284 { POPUP_WIDTH - CORNER_RADIUS,
285 ARROW_BORDER + CORNER_RADIUS },
286 { POPUP_WIDTH - CORNER_RADIUS,
287 POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER },
288 { CORNER_RADIUS,
289 POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER}};
290
291 int i;
292 for(i=0;i<4;i++) {
293 gdk_draw_arc(mask, gc, TRUE,
294 off[i][0]-CORNER_RADIUS, off[i][1]-CORNER_RADIUS,
295 2*CORNER_RADIUS, 2*CORNER_RADIUS,
296 0, 360*64);
297 }
298
299 GdkPoint points[3] = { {POPUP_WIDTH*1/3, POPUP_HEIGHT/2},
300 {POPUP_WIDTH*2/3, POPUP_HEIGHT/2},
301 {tip_x,tip_y} };
302 gdk_draw_polygon(mask, gc, TRUE, points, 3);
303
304
305 gdk_window_shape_combine_mask(window->window, mask, 0, 0);
306 }
307
308 void cache_popup(map_context_t *mcontext, cache_t *cache) {
309 popup_context_t pcontext;
310 pcontext.appdata = mcontext->appdata;
311
312 pcontext.window = gtk_window_new(GTK_WINDOW_POPUP);
313 gtk_widget_realize(pcontext.window);
314 gtk_window_set_default_size(GTK_WINDOW(pcontext.window),
315 POPUP_WIDTH, POPUP_HEIGHT);
316 gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT);
317 // gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE);
318 gtk_window_set_transient_for(GTK_WINDOW(pcontext.window),
319 GTK_WINDOW(mcontext->appdata->window));
320 gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE);
321 gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE);
322 gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC);
323 gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE);
324
325 /* connect events */
326 g_signal_connect(G_OBJECT(pcontext.window), "button-press-event",
327 G_CALLBACK(on_button_press_event), &pcontext);
328 g_signal_connect(G_OBJECT(pcontext.window), "button-release-event",
329 G_CALLBACK(on_button_release_event), &pcontext);
330 g_signal_connect(G_OBJECT(pcontext.window), "delete-event",
331 G_CALLBACK(run_delete_handler), &pcontext);
332 g_signal_connect(G_OBJECT(pcontext.window), "destroy",
333 G_CALLBACK(run_destroy_handler), &pcontext);
334 g_signal_connect(G_OBJECT(pcontext.window), "unmap",
335 G_CALLBACK(run_unmap_handler), &pcontext);
336
337 gdk_pointer_grab(pcontext.window->window, TRUE,
338 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
339 NULL, NULL, GDK_CURRENT_TIME);
340 gtk_grab_add(pcontext.window);
341
342 /* check whether cache is in upper or lower half of window */
343 gint x, y, sx, sy;
344 osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget),
345 cache->pos.lat, cache->pos.lon,
346 &sx, &sy);
347
348 printf("screen pos %d/%d\n", sx, sy);
349
350 gdk_window_get_origin(mcontext->widget->window, &x, &y);
351 printf("window = %d/%d +%d+%d %d*%d\n", x, y,
352 mcontext->widget->allocation.x,
353 mcontext->widget->allocation.y,
354 mcontext->widget->allocation.width,
355 mcontext->widget->allocation.height
356 );
357
358 gint ax = 0, ay = 0;
359 if(sx > mcontext->widget->allocation.width/2)
360 ax = POPUP_WIDTH;
361
362 if(sy > mcontext->widget->allocation.height/2)
363 ay = POPUP_HEIGHT;
364
365 #if !defined(USE_HILDON) || (MAEMO_VERSION_MAJOR < 5)
366 GdkColor color;
367 gdk_color_parse("darkgray", &color);
368 gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color);
369 #endif
370
371 gtk_window_move(GTK_WINDOW(pcontext.window),
372 x + mcontext->widget->allocation.x + sx - ax,
373 y + mcontext->widget->allocation.y + sy - ay);
374
375
376 GtkWidget *frame = gtk_frame_new(NULL);
377 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
378 gtk_container_set_border_width(GTK_CONTAINER(frame),
379 ARROW_BORDER+CORNER_RADIUS);
380 gtk_container_add(GTK_CONTAINER(frame),
381 gtk_button_new_with_label(cache->name));
382
383 gtk_container_add(GTK_CONTAINER(pcontext.window), frame);
384
385 /* --------- give the window its shape ----------- */
386 popup_window_shape(pcontext.window, ax, ay);
387
388 gtk_widget_show_all(pcontext.window);
389
390 /* handle this popup until it's gone */
391
392 pcontext.loop = g_main_loop_new(NULL, FALSE);
393
394 GDK_THREADS_LEAVE();
395 g_main_loop_run(pcontext.loop);
396 GDK_THREADS_ENTER();
397
398 g_main_loop_unref(pcontext.loop);
399
400 printf("cache popup removed\n");
401 }
402
403 static void
404 map_cachelist_nearest(cache_t *cache, pos_t *pos,
405 cache_t **result, float *distance) {
406 while(cache) {
407 float dist =
408 pow(cache->pos.lat - pos->lat, 2) +
409 pow(cache->pos.lon - pos->lon, 2);
410
411 if(!(dist > *distance)) {
412 *result = cache;
413 *distance = dist;
414 }
415
416 cache = cache->next;
417 }
418 }
419
420 static cache_t *map_closest(map_context_t *context, pos_t *pos) {
421 cache_t *result = NULL;
422 float distance = NAN;
423
424 #ifdef USE_MAEMO
425 if(!context->appdata->cur_gpx) {
426 #endif
427 /* search all geocaches */
428 gpx_t *gpx = context->appdata->gpx;
429 while(gpx) {
430 map_cachelist_nearest(gpx->cache, pos, &result, &distance);
431 gpx = gpx->next;
432 }
433 #ifdef USE_MAEMO
434 } else {
435 map_cachelist_nearest(context->appdata->cur_gpx->cache,
436 pos, &result, &distance);
437 }
438 #endif
439
440 return result;
441 }
442
443 /* translate between osm-gps-map positions and gpxview ones */
444 pos_t coord2pos(coord_t coo) {
445 pos_t pos;
446 pos.lat = RAD2DEG(coo.rlat);
447 pos.lon = RAD2DEG(coo.rlon);
448 return pos;
449 }
450
451 static int dist2pixel(map_context_t *context, float km, float lat) {
452 int zoom;
453 g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
454
455 /* world at zoom 1 == 512 pixels */
456 float m_per_pix =
457 cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom));
458
459 return 1000.0*km/m_per_pix;
460 }
461
462 #define CLICK_FUZZ (16)
463
464 static gboolean
465 on_map_button_press_event(GtkWidget *widget,
466 GdkEventButton *event, map_context_t *context) {
467 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
468
469 /* got a press event without release event? eat it! */
470 if(context->press_on != NULL) {
471 printf("PRESS: already\n");
472 return TRUE;
473 }
474
475 pos_t pos =
476 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
477
478 cache_t *nearest = map_closest(context, &pos);
479 if(nearest) {
480 float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
481 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
482 context->press_on = nearest;
483 }
484
485 return FALSE;
486 }
487
488 static gboolean
489 on_map_button_release_event(GtkWidget *widget,
490 GdkEventButton *event, map_context_t *context) {
491 if(context->press_on) {
492 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
493
494 pos_t pos =
495 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
496
497 cache_t *nearest = map_closest(context, &pos);
498 if(nearest && nearest == context->press_on) {
499 float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
500 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
501 cache_popup(context, nearest);
502 }
503 context->press_on = NULL;
504 }
505
506 return FALSE;
507 }
508
509
510 #if MAEMO_VERSION_MAJOR == 5
511 static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
512 printf("destroy map view\n");
513
514 /* restore cur_view */
515 context->appdata->cur_view = context->old_view;
516
517 gtk_timeout_remove(context->handler_id);
518 g_free(context);
519 }
520 #endif
521
522 void map(appdata_t *appdata) {
523 map_context_t *context = g_new0(map_context_t, 1);
524 context->appdata = appdata;
525
526 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
527
528 char *path = g_strdup_printf("%s/map/", appdata->image_path);
529 const char *proxy = get_proxy_uri(appdata);
530
531 context->widget = g_object_new(OSM_TYPE_GPS_MAP,
532 "repo-uri", MAP_SOURCE_OPENSTREETMAP,
533 "tile-cache", path,
534 proxy?"proxy-uri":NULL, proxy,
535 NULL);
536
537 g_free(path);
538
539 char *name = NULL;
540 #ifdef USE_MAEMO
541 if(!appdata->cur_gpx) {
542 #endif
543 /* draw all geocaches */
544 gpx_t *gpx = appdata->gpx;
545 while(gpx) {
546 map_draw_cachelist(context->widget, gpx->cache);
547 gpx = gpx->next;
548 }
549 name = g_strdup(_("all geocaches"));
550 #ifdef USE_MAEMO
551 } else {
552 map_draw_cachelist(context->widget, appdata->cur_gpx->cache);
553 name = g_strdup(appdata->cur_gpx->name);
554 }
555 #endif
556
557 char *title = g_strdup_printf(_("Map - %s"), name);
558 g_free(name);
559
560 #if MAEMO_VERSION_MAJOR == 5
561 GtkWidget *window = hildon_stackable_window_new();
562 gtk_window_set_title(GTK_WINDOW(window), title);
563 #else
564 GtkWidget *dialog = gtk_dialog_new_with_buttons(title,
565 GTK_WINDOW(appdata->window),
566 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
567 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
568 NULL);
569
570 #ifndef USE_MAEMO
571 gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480);
572 #else
573 gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480);
574 #endif
575 #endif
576
577 g_free(title);
578
579 g_signal_connect(G_OBJECT(context->widget), "configure-event",
580 G_CALLBACK(on_map_configure), context);
581
582 g_signal_connect(G_OBJECT(context->widget), "button-press-event",
583 G_CALLBACK(on_map_button_press_event), context);
584
585 g_signal_connect(G_OBJECT(context->widget), "button-release-event",
586 G_CALLBACK(on_map_button_release_event), context);
587
588 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget);
589 /* zoom button box */
590 GtkWidget *vbox = gtk_vbox_new(FALSE,0);
591
592 context->zoomin =
593 map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin),
594 context, _("Zoom in"));
595 gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0);
596
597 context->zoomout =
598 map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout),
599 context, _("Zoom out"));
600 gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0);
601
602 context->gps =
603 map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps),
604 context, _("Jump to GPS position"));
605 gtk_widget_set_sensitive(context->gps, FALSE);
606 /* install handler for timed updates of the gps button */
607 context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
608 gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0);
609
610 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
611
612 #if MAEMO_VERSION_MAJOR == 5
613 /* prevent some of the main screen things */
614 context->old_view = appdata->cur_view;
615 appdata->cur_view = NULL;
616
617 g_signal_connect(G_OBJECT(window), "destroy",
618 G_CALLBACK(on_window_destroy), context);
619
620 gtk_container_add(GTK_CONTAINER(window), hbox);
621 gtk_widget_show_all(GTK_WIDGET(window));
622
623 #else
624 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
625 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
626 gtk_widget_show_all(dialog);
627 gtk_dialog_run(GTK_DIALOG(dialog));
628 gtk_timeout_remove(context->handler_id);
629 gtk_widget_destroy(dialog);
630 g_free(context);
631 #endif
632 }