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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (hide annotations)
Tue Aug 4 14:07:18 2009 UTC (14 years, 9 months ago) by harbaum
File MIME type: text/plain
File size: 15736 byte(s)
Fixed map scroll/select handling
1 harbaum 33 /*
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 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 42 /* 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 harbaum 33 typedef struct {
34 harbaum 34 appdata_t *appdata;
35 harbaum 33 GtkWidget *widget;
36     GtkWidget *zoomin, *zoomout, *gps;
37     gint handler_id;
38 harbaum 42 cache_t *press_on;
39 harbaum 40 #if MAEMO_VERSION_MAJOR == 5
40     GtkWidget *old_view;
41     #endif
42 harbaum 33 } map_context_t;
43    
44 harbaum 34 #define PROXY_KEY "/system/http_proxy/"
45    
46     static const char *get_proxy_uri(appdata_t *appdata) {
47     static char proxy_buffer[64] = "";
48 harbaum 33
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 harbaum 34 /* ------------- get proxy settings -------------------- */
57     if(gconf_client_get_bool(appdata->gconf_client,
58     PROXY_KEY "use_http_proxy", NULL)) {
59 harbaum 33
60 harbaum 34 /* 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 harbaum 33
71 harbaum 34 snprintf(proxy_buffer, sizeof(proxy_buffer),
72     "http://%s:%u", host, port);
73 harbaum 33
74 harbaum 34 g_free(host);
75     }
76 harbaum 35 return proxy_buffer;
77 harbaum 34 }
78    
79 harbaum 35 return NULL;
80 harbaum 33 }
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 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     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 harbaum 34 }
116 harbaum 33
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_MENU));
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 harbaum 34 pos_t *refpos = get_pos(context->appdata);
137     gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
138 harbaum 33
139 harbaum 34 /* get reference position and go there */
140     gtk_widget_set_sensitive(context->gps, ok);
141    
142 harbaum 33 return TRUE;
143     }
144    
145 harbaum 35 static gboolean on_map_configure(GtkWidget *widget,
146     GdkEventConfigure *event,
147     map_context_t *context) {
148 harbaum 33
149 harbaum 35 cb_map_gps(NULL, context);
150    
151     return FALSE;
152     }
153    
154 harbaum 38 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 harbaum 41 /* 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 harbaum 42 void cache_popup(map_context_t *mcontext, cache_t *cache) {
246     popup_context_t pcontext;
247     pcontext.appdata = mcontext->appdata;
248 harbaum 41
249 harbaum 42 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 harbaum 41 POPUP_WIDTH, POPUP_HEIGHT);
253 harbaum 42 gtk_window_resize(GTK_WINDOW(pcontext.window),
254 harbaum 41 POPUP_WIDTH, POPUP_HEIGHT);
255 harbaum 42 // gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE);
256     gtk_window_set_transient_for(GTK_WINDOW(pcontext.window),
257     GTK_WINDOW(mcontext->appdata->window));
258     gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE);
259     gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE);
260     gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC);
261     gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE);
262 harbaum 41
263     /* connect events */
264 harbaum 42 g_signal_connect(G_OBJECT(pcontext.window), "button-press-event",
265     G_CALLBACK(on_button_press_event), &pcontext);
266     g_signal_connect(G_OBJECT(pcontext.window), "button-release-event",
267     G_CALLBACK(on_button_release_event), &pcontext);
268     g_signal_connect(G_OBJECT(pcontext.window), "delete-event",
269     G_CALLBACK(run_delete_handler), &pcontext);
270     g_signal_connect(G_OBJECT(pcontext.window), "destroy",
271     G_CALLBACK(run_destroy_handler), &pcontext);
272     g_signal_connect(G_OBJECT(pcontext.window), "unmap",
273     G_CALLBACK(run_unmap_handler), &pcontext);
274 harbaum 41
275 harbaum 42 gdk_pointer_grab(pcontext.window->window, TRUE,
276 harbaum 41 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
277     NULL, NULL, GDK_CURRENT_TIME);
278 harbaum 42 gtk_grab_add(pcontext.window);
279 harbaum 41
280 harbaum 42 /* check whether cache is in upper or lower half of window */
281     gint x, y;
282     osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget),
283     cache->pos.lat, cache->pos.lon,
284     &x, &y);
285    
286     printf("screen pos %d/%d\n", x, y);
287    
288     gdk_window_get_origin(mcontext->widget->window, &x, &y);
289     printf("window = %d/%d %d/%d\n", x, y,
290     mcontext->widget->allocation.x,
291     mcontext->widget->allocation.y);
292    
293     // gtk_window_move(GTK_WINDOW(pcontext.window),
294 harbaum 41 // x + button->allocation.x,
295     // y + button->allocation.y - HEIGHT);
296    
297    
298 harbaum 42 gtk_window_move(GTK_WINDOW(pcontext.window),
299 harbaum 41 100,
300     100);
301    
302     /* a frame with a vscale inside */
303     GtkWidget *frame = gtk_frame_new(NULL);
304     gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
305    
306     gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name));
307 harbaum 42 gtk_container_add(GTK_CONTAINER(pcontext.window), frame);
308 harbaum 41
309 harbaum 42 gtk_widget_show_all(pcontext.window);
310 harbaum 41
311     /* handle this popup until it's gone */
312    
313 harbaum 42 pcontext.loop = g_main_loop_new(NULL, FALSE);
314 harbaum 41
315     GDK_THREADS_LEAVE();
316 harbaum 42 g_main_loop_run(pcontext.loop);
317 harbaum 41 GDK_THREADS_ENTER();
318    
319 harbaum 42 g_main_loop_unref(pcontext.loop);
320 harbaum 41
321     printf("cache popup removed\n");
322     }
323    
324     static void
325     map_cachelist_nearest(cache_t *cache, pos_t *pos,
326     cache_t **result, float *distance) {
327     while(cache) {
328     float dist =
329     pow(cache->pos.lat - pos->lat, 2) +
330     pow(cache->pos.lon - pos->lon, 2);
331    
332     if(!(dist > *distance)) {
333     *result = cache;
334     *distance = dist;
335     }
336    
337     cache = cache->next;
338     }
339     }
340    
341     static cache_t *map_closest(map_context_t *context, pos_t *pos) {
342     cache_t *result = NULL;
343     float distance = NAN;
344    
345     #ifdef USE_MAEMO
346     if(!context->appdata->cur_gpx) {
347     #endif
348     /* search all geocaches */
349     gpx_t *gpx = context->appdata->gpx;
350     while(gpx) {
351     map_cachelist_nearest(gpx->cache, pos, &result, &distance);
352     gpx = gpx->next;
353     }
354     #ifdef USE_MAEMO
355     } else {
356     map_cachelist_nearest(context->appdata->cur_gpx->cache,
357     pos, &result, &distance);
358     }
359     #endif
360    
361     return result;
362     }
363    
364     /* translate between osm-gps-map positions and gpxview ones */
365     pos_t coord2pos(coord_t coo) {
366     pos_t pos;
367     pos.lat = RAD2DEG(coo.rlat);
368     pos.lon = RAD2DEG(coo.rlon);
369     return pos;
370     }
371    
372 harbaum 42 static int dist2pixel(map_context_t *context, float km, float lat) {
373     int zoom;
374     g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
375    
376     /* world at zoom 1 == 512 pixels */
377     float m_per_pix =
378     cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom));
379    
380     return 1000.0*km/m_per_pix;
381     }
382    
383 harbaum 44 #define CLICK_FUZZ (16)
384 harbaum 42
385 harbaum 41 static gboolean
386     on_map_button_press_event(GtkWidget *widget,
387     GdkEventButton *event, map_context_t *context) {
388     OsmGpsMap *map = OSM_GPS_MAP(context->widget);
389    
390 harbaum 44 /* got a press event without release event? eat it! */
391     if(context->press_on != NULL) {
392     printf("PRESS: already\n");
393     return TRUE;
394     }
395    
396 harbaum 41 pos_t pos =
397 harbaum 42 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
398 harbaum 41
399 harbaum 42 cache_t *nearest = map_closest(context, &pos);
400     if(nearest) {
401     float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
402 harbaum 44 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
403 harbaum 42 context->press_on = nearest;
404     }
405 harbaum 44
406 harbaum 41 return FALSE;
407     }
408    
409     static gboolean
410     on_map_button_release_event(GtkWidget *widget,
411     GdkEventButton *event, map_context_t *context) {
412 harbaum 42 if(context->press_on) {
413     OsmGpsMap *map = OSM_GPS_MAP(context->widget);
414 harbaum 41
415 harbaum 42 pos_t pos =
416     coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
417 harbaum 41
418 harbaum 42 cache_t *nearest = map_closest(context, &pos);
419     if(nearest && nearest == context->press_on) {
420     float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
421 harbaum 44 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
422 harbaum 42 cache_popup(context, nearest);
423     }
424 harbaum 44 context->press_on = NULL;
425 harbaum 41 }
426    
427     return FALSE;
428     }
429    
430 harbaum 44
431 harbaum 40 #if MAEMO_VERSION_MAJOR == 5
432     static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
433     printf("destroy map view\n");
434    
435     /* restore cur_view */
436     context->appdata->cur_view = context->old_view;
437    
438     gtk_timeout_remove(context->handler_id);
439     g_free(context);
440     }
441     #endif
442    
443 harbaum 33 void map(appdata_t *appdata) {
444 harbaum 40 map_context_t *context = g_new0(map_context_t, 1);
445     context->appdata = appdata;
446 harbaum 33
447 harbaum 41 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
448    
449     char *path = g_strdup_printf("%s/map/", appdata->image_path);
450     const char *proxy = get_proxy_uri(appdata);
451    
452     context->widget = g_object_new(OSM_TYPE_GPS_MAP,
453     "repo-uri", MAP_SOURCE_OPENSTREETMAP,
454     "tile-cache", path,
455     proxy?"proxy-uri":NULL, proxy,
456     NULL);
457    
458     g_free(path);
459    
460     char *name = NULL;
461     #ifdef USE_MAEMO
462     if(!appdata->cur_gpx) {
463     #endif
464     /* draw all geocaches */
465     gpx_t *gpx = appdata->gpx;
466     while(gpx) {
467     map_draw_cachelist(context->widget, gpx->cache);
468     gpx = gpx->next;
469     }
470     name = g_strdup(_("all geocaches"));
471     #ifdef USE_MAEMO
472     } else {
473     map_draw_cachelist(context->widget, appdata->cur_gpx->cache);
474 harbaum 44 name = g_strdup(appdata->cur_gpx->name);
475 harbaum 41 }
476     #endif
477    
478     char *title = g_strdup_printf(_("Map - %s"), name);
479     g_free(name);
480    
481 harbaum 40 #if MAEMO_VERSION_MAJOR == 5
482     GtkWidget *window = hildon_stackable_window_new();
483 harbaum 41 gtk_window_set_title(GTK_WINDOW(window), title);
484 harbaum 40 #else
485 harbaum 41 GtkWidget *dialog = gtk_dialog_new_with_buttons(title,
486 harbaum 33 GTK_WINDOW(appdata->window),
487     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
488     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
489     NULL);
490    
491     #ifndef USE_MAEMO
492     gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 350);
493     #else
494     gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480);
495     #endif
496 harbaum 40 #endif
497 harbaum 33
498 harbaum 41 g_free(title);
499 harbaum 33
500 harbaum 41 g_signal_connect(G_OBJECT(context->widget), "configure-event",
501     G_CALLBACK(on_map_configure), context);
502 harbaum 33
503 harbaum 41 g_signal_connect(G_OBJECT(context->widget), "button-press-event",
504     G_CALLBACK(on_map_button_press_event), context);
505 harbaum 33
506 harbaum 40 g_signal_connect(G_OBJECT(context->widget), "button-release-event",
507     G_CALLBACK(on_map_button_release_event), context);
508 harbaum 33
509 harbaum 40 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget);
510 harbaum 33 /* zoom button box */
511     GtkWidget *vbox = gtk_vbox_new(FALSE,0);
512    
513 harbaum 40 context->zoomin =
514 harbaum 33 map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin),
515 harbaum 40 context, _("Zoom in"));
516     gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0);
517 harbaum 33
518 harbaum 40 context->zoomout =
519 harbaum 33 map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout),
520 harbaum 40 context, _("Zoom out"));
521     gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0);
522 harbaum 33
523 harbaum 40 context->gps =
524 harbaum 33 map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps),
525 harbaum 40 context, _("Jump to GPS position"));
526     gtk_widget_set_sensitive(context->gps, FALSE);
527 harbaum 33 /* install handler for timed updates of the gps button */
528 harbaum 40 context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
529     gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0);
530 harbaum 33
531     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
532    
533 harbaum 40 #if MAEMO_VERSION_MAJOR == 5
534     /* prevent some of the main screen things */
535     context->old_view = appdata->cur_view;
536     appdata->cur_view = NULL;
537    
538     g_signal_connect(G_OBJECT(window), "destroy",
539     G_CALLBACK(on_window_destroy), context);
540    
541     gtk_container_add(GTK_CONTAINER(window), hbox);
542     gtk_widget_show_all(GTK_WIDGET(window));
543    
544     #else
545 harbaum 33 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
546     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
547     gtk_widget_show_all(dialog);
548     gtk_dialog_run(GTK_DIALOG(dialog));
549 harbaum 40 gtk_timeout_remove(context->handler_id);
550 harbaum 33 gtk_widget_destroy(dialog);
551 harbaum 40 g_free(context);
552     #endif
553 harbaum 33 }