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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 45 by harbaum, Tue Aug 4 19:27:39 2009 UTC revision 152 by harbaum, Mon Nov 2 10:53:01 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.   * Copyright (C) 2008-2009 Till Harbaum <till@harbaum.org>.
3   *   *
4   * This file is part of GPXView.   * This file is part of GPXView.
5   *   *
# Line 17  Line 17 
17   * along with GPXView.  If not, see <http://www.gnu.org/licenses/>.   * along with GPXView.  If not, see <http://www.gnu.org/licenses/>.
18   */   */
19    
20    /*
21     * http://topo.geofabrik.de/relief/${z}/${x}/${y}.png  8-15
22     * http://topo.geofabrik.de/trail/${z}/${x}/${y}.png   8-15
23     */
24    
25    /*
26     * TODO:
27     * - draw caches only once
28     * - prefer duplicates from selected files
29     * - make semi-transparent caches selectable
30     */
31    
32  #include "gpxview.h"  #include "gpxview.h"
33    #include "converter.h"
34  #include <math.h>    // for isnan  #include <math.h>    // for isnan
35    
36  #ifdef ENABLE_OSM_GPS_MAP  #ifdef ENABLE_OSM_GPS_MAP
37  #include "osm-gps-map.h"  #include "osm-gps-map.h"
38    #include "osm-gps-map-osd-classic.h"
39  #endif  #endif
40    
41  /* equatorial radius in meters */  #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
42  #define EQ_RADIUS     (6378137.0)  #include <gdk/gdkx.h>
43    #include <X11/Xatom.h>
 #define RAD2DEG(a)  (((a)*180.0)/M_PI)  
 #define DEG2RAD(a)  (((a)*M_PI)/180.0)  
   
 typedef struct {  
   appdata_t *appdata;  
   GtkWidget *widget;  
   GtkWidget *zoomin, *zoomout, *gps;  
   gint handler_id;  
   cache_t *press_on;  
 #if MAEMO_VERSION_MAJOR == 5  
   GtkWidget *old_view;  
44  #endif  #endif
45  } map_context_t;  
46    /* default values */
47    #define MAP_SOURCE  OSM_GPS_MAP_SOURCE_OPENCYCLEMAP
48    #define GPS_DEFAULT_ZOOM 13
49    
50  #define PROXY_KEY  "/system/http_proxy/"  #define PROXY_KEY  "/system/http_proxy/"
51    
# Line 79  static const char *get_proxy_uri(appdata Line 85  static const char *get_proxy_uri(appdata
85    return NULL;    return NULL;
86  }  }
87    
88  static void map_zoom(map_context_t *context, int step) {  static void
89    int zoom;  cb_map_gps(osd_button_t but, map_context_t *context) {
   OsmGpsMap *map = OSM_GPS_MAP(context->widget);  
   g_object_get(map, "zoom", &zoom, NULL);  
   zoom = osm_gps_map_set_zoom(map, zoom+step);  
   
   /* enable/disable zoom buttons as required */  
   gtk_widget_set_sensitive(context->zoomin, zoom<17);  
   gtk_widget_set_sensitive(context->zoomout, zoom>1);  
 }  
   
 static gboolean  
 cb_map_zoomin(GtkButton *button, map_context_t *context) {  
   map_zoom(context, +1);  
   return FALSE;  
 }  
   
 static gboolean  
 cb_map_zoomout(GtkButton *button, map_context_t *context) {  
   map_zoom(context, -1);  
   return FALSE;  
 }  
90    
91  static gboolean    if(but == OSD_GPS) {
92  cb_map_gps(GtkButton *button, map_context_t *context) {      pos_t *refpos = get_pos(context->appdata);
93    pos_t *refpos = get_pos(context->appdata);      if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
94    if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {        gint zoom;
95      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),        g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
96                                refpos->lat, refpos->lon, 14);        if(zoom < 10)
97    } else {          osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
98      /* no coordinates given: display the entire world */                                    refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
99      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),        else
100                                0.0, 0.0, 1);          osm_gps_map_set_center(OSM_GPS_MAP(context->widget),
101                                   refpos->lat, refpos->lon);
102    
103          /* re-enable centering */
104          g_object_set(context->widget, "auto-center", TRUE, NULL);
105        } else {
106          /* no coordinates given: display the entire world */
107          osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
108                                    0.0, 0.0, 1);
109        }
110    }    }
   
   return FALSE;  
111  }  }
112    
113  static GtkWidget  static int dist2pixel(map_context_t *context, float km, float lat) {
114  *map_add_button(const gchar *icon, GCallback cb, gpointer data,    return 1000.0*km/osm_gps_map_get_scale(OSM_GPS_MAP(context->widget));
                 char *tooltip) {  
   GtkWidget *button = gtk_button_new();  
   gtk_button_set_image(GTK_BUTTON(button),  
                        gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON));  
   g_signal_connect(button, "clicked", cb, data);  
 #ifndef USE_MAEMO  
   gtk_widget_set_tooltip_text(button, tooltip);  
 #endif  
   return button;  
115  }  }
116    
117  static gboolean map_gps_update(gpointer data) {  static gboolean map_gps_update(gpointer data) {
118    map_context_t *context = (map_context_t*)data;    map_context_t *context = (map_context_t*)data;
119      static gboolean goto_is_enabled = FALSE;
120    
121      /* get reference position ... */
122    pos_t *refpos = get_pos(context->appdata);    pos_t *refpos = get_pos(context->appdata);
123    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
124    
125    /* get reference position and go there */    /* ... and enable "goto" button if it's valid */
126    gtk_widget_set_sensitive(context->gps, ok);    if(ok != goto_is_enabled) {
127        osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget),
128                    OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context);
129        goto_is_enabled = ok;
130      }
131    
132      if(ok) {
133        float heading = NAN;
134        int radius = 0;
135    
136        if(context->appdata->use_gps) {
137          heading = gps_get_heading(context->appdata);
138    
139          /* get error */
140          float eph = gps_get_eph(context->appdata);
141          if(!isnan(eph))
142            radius = dist2pixel(context, eph/1000, refpos->lat);
143        }
144    
145        /* TODO: in order to save energy: only draw if state actually changed */
146    
147        g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);
148        osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),
149                             refpos->lat, refpos->lon, heading);
150      } else
151        osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));
152    
153    return TRUE;    return TRUE;
154  }  }
155    
156  static gboolean on_map_configure(GtkWidget *widget,  static void map_draw_cache(GtkWidget *map, cache_t *cache, gboolean semi) {
157                                   GdkEventConfigure *event,    GdkPixbuf *icon = icon_get(semi?ICON_CACHE_TYPE_SEMI:ICON_CACHE_TYPE,
158                                   map_context_t *context) {                               cache->type);
159    
160      /* check if there's also an overwritten coordinate */
161      if(cache->notes && cache->notes->override) {
162        GdkPixbuf *over = icon_get(ICON_MISC, 1);
163    
164    cb_map_gps(NULL, context);      osm_gps_map_add_image(OSM_GPS_MAP(map),
165              cache->notes->pos.lat, cache->notes->pos.lon, icon);
166    
167    return FALSE;      osm_gps_map_add_image(OSM_GPS_MAP(map),
168              cache->notes->pos.lat, cache->notes->pos.lon, over);
169      } else {
170        if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon))
171          osm_gps_map_add_image(OSM_GPS_MAP(map),
172                                cache->pos.lat, cache->pos.lon, icon);
173      }
174  }  }
175    
176  static void map_draw_cachelist(GtkWidget *map, cache_t *cache) {  static void map_draw_gpx(appdata_t *appdata,
177    while(cache) {                           GtkWidget *map, gpx_t *gpx,
178      GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);                           cache_t *nav, gboolean semi) {
179      if(!gpx->notes_loaded) {
180      osm_gps_map_add_image(OSM_GPS_MAP(map),      notes_load_all(appdata, gpx);
181                            cache->pos.lat, cache->pos.lon, icon);      gpx->notes_loaded = TRUE;
182      }
183    
184      cache_t *cache = gpx->cache;
185      while(cache) {
186        /* if nav is given draw all other caches semitransparent. */
187        /* if nav is not given do what semi sais */
188        map_draw_cache(map, cache, nav?(cache != nav):semi);
189      cache = cache->next;      cache = cache->next;
190    }    }
191  }  }
192    
193  /* draw a nice popup */  /* draw geocaches and set window title */
194  typedef struct {  static void map_setup(map_context_t *context) {
195    appdata_t *appdata;    char *name = NULL;
   GtkWidget *window;  
   GMainLoop *loop;  
 } popup_context_t;  
   
196    
197  #ifndef USE_HILDON    int cache_num = gpx_total_caches_global(context->appdata->gpx);
 #define POPUP_WIDTH  300  
 #define POPUP_HEIGHT 100  
 #else  
 #define POPUP_WIDTH  600  
 #define POPUP_HEIGHT 200  
 #endif  
198    
199  static gboolean    if(context->caches_displayed && (cache_num != context->cache_list_len)) {
200  pointer_in_window(GtkWidget *widget, gint x_root, gint y_root) {      g_free(context->caches_displayed);
201    if(GTK_WIDGET_MAPPED(gtk_widget_get_toplevel(widget))) {      context->caches_displayed = NULL;
202      gint window_x, window_y;      context->cache_list_len = 0;
   
     gdk_window_get_position(gtk_widget_get_toplevel(widget)->window,  
                             &window_x, &window_y);  
   
     if(x_root >= window_x && x_root < window_x + widget->allocation.width &&  
         y_root >= window_y && y_root < window_y + widget->allocation.height)  
       return TRUE;  
203    }    }
   
   return FALSE;  
 }  
204    
205  static gboolean    /* allocate buffer */
206  on_button_press_event(GtkWidget *widget,    if(cache_num) {
207                            GdkEventButton *event, popup_context_t *context) {      context->cache_list_len = cache_num;
208    gboolean in = pointer_in_window(widget, event->x_root, event->y_root);      context->caches_displayed = g_new0(cache_display_t, cache_num);
209        printf("allocated space to handle %d map icons\n", cache_num);
210      }
211    
212    printf("overlay button press (in = %d)\n", in);    if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {
213    return !in;      if(context->state != MAP_ALL) {
214  }        printf("map_setup(ALL)\n");
215    
216  static gboolean  #ifdef OSD_NAV
217  on_button_release_event(GtkWidget *widget,        /* no navigation in this mode */
218                            GdkEventButton *event, popup_context_t *context) {        osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));
219    gboolean in = pointer_in_window(widget, event->x_root, event->y_root);  #endif
220    
221    printf("overlay button release (in = %d)\n", in);        /* clear all existing cache images */
222          osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
223    
224    if(!in) {        /* draw all geocaches and none is semi-transparent */
225      printf("destroying popup\n");        gpx_t *gpx = context->appdata->gpx;
226      gtk_widget_destroy(gtk_widget_get_toplevel(widget));        while(gpx) {
227    }          map_draw_gpx(context->appdata, context->widget, gpx, NULL, FALSE);
228            gpx = gpx->next;
229          }
230          name = g_strdup(_("all"));
231          context->state = MAP_ALL;
232        }
233      } else if(!context->appdata->cur_cache) {
234        if(context->state != MAP_GPX) {
235          printf("map_setup(GPX)\n");
236    
237    #ifdef OSD_NAV
238          /* no navigation in this mode */
239          osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));
240    #endif
241    
242          /* clear all existing ccahe images */
243          osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
244    
245          /* draw all geocaches and all other gpx files are semi-transparent */
246          gpx_t *gpx = context->appdata->gpx;
247          while(gpx) {
248            map_draw_gpx(context->appdata, context->widget, gpx, NULL,
249                         gpx != context->appdata->cur_gpx);
250            gpx = gpx->next;
251          }
252    
253    return !in;        name = g_strdup(context->appdata->cur_gpx->name);
254  }        context->state = MAP_GPX;
255        }
256      } else {
257        cache_t *cache = context->appdata->cur_cache;
258    
259        printf("map_setp(CACHE)\n");
260    
261        /* no balloons in this mode */
262        context->balloon = NULL;
263        osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(context->widget));
264    
265        /* clear all existing ccahe images */
266        osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
267    
268        /* draw all geocaches and all but selected one are semi-transparent */
269        gpx_t *gpx = context->appdata->gpx;
270        while(gpx) {
271          map_draw_gpx(context->appdata, context->widget, gpx, cache, FALSE);
272          gpx = gpx->next;
273        }
274    
275  static void      name = g_strdup(cache->name);
276  shutdown_loop(popup_context_t *context) {      context->state = MAP_CACHE;
277    if(g_main_loop_is_running(context->loop))  
278      g_main_loop_quit(context->loop);      /* navigation in this mode! */
279  }      pos_t cpos = gpx_cache_pos(cache);
280    
281  static gint  #ifdef OSD_NAV
282  run_delete_handler(GtkWindow *window, GdkEventAny *event,      osm_gps_map_osd_draw_nav (OSM_GPS_MAP(context->widget),
283                     popup_context_t *context) {                                context->appdata->imperial,
284    shutdown_loop(context);                                cpos.lat, cpos.lon, cache->name);
285    return TRUE; /* Do not destroy */  #else
286  }  #warning OSD_NAV not defined!
287    #endif
288      }
289    
290  static void    if(name) {
291  run_destroy_handler(GtkWindow *window, popup_context_t *context) {      char *title = g_strdup_printf(_("Map - %s"), name);
292    /* shutdown_loop will be called by run_unmap_handler */      g_free(name);
   printf("popup destroyed\n");  
 }  
293    
294  static void      gtk_window_set_title(GTK_WINDOW(context->window), title);
295  run_unmap_handler(GtkWindow *window, popup_context_t *context) {  
296    shutdown_loop(context);      g_free(title);
297      } else
298        printf("map_setup(keep)\n");
299  }  }
300    
301  void cache_popup(map_context_t *mcontext, cache_t *cache) {  static gboolean on_map_configure(GtkWidget *widget,
302    popup_context_t pcontext;                                   GdkEventConfigure *event,
303    pcontext.appdata = mcontext->appdata;                                   map_context_t *context) {
   
   pcontext.window = gtk_window_new(GTK_WINDOW_POPUP);  
   gtk_widget_realize(pcontext.window);  
   gtk_window_set_default_size(GTK_WINDOW(pcontext.window),  
                               POPUP_WIDTH, POPUP_HEIGHT);  
   gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT);  
   //  gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE);  
   gtk_window_set_transient_for(GTK_WINDOW(pcontext.window),  
                                GTK_WINDOW(mcontext->appdata->window));  
   gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE);  
   gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE);  
   gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC);  
   gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE);  
   
   /* connect events */  
   g_signal_connect(G_OBJECT(pcontext.window), "button-press-event",  
                    G_CALLBACK(on_button_press_event), &pcontext);  
   g_signal_connect(G_OBJECT(pcontext.window), "button-release-event",  
                    G_CALLBACK(on_button_release_event), &pcontext);  
   g_signal_connect(G_OBJECT(pcontext.window), "delete-event",  
                    G_CALLBACK(run_delete_handler), &pcontext);  
   g_signal_connect(G_OBJECT(pcontext.window), "destroy",  
                    G_CALLBACK(run_destroy_handler), &pcontext);  
   g_signal_connect(G_OBJECT(pcontext.window), "unmap",  
                    G_CALLBACK(run_unmap_handler), &pcontext);  
   
   gdk_pointer_grab(pcontext.window->window, TRUE,  
      GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,  
                    NULL, NULL, GDK_CURRENT_TIME);  
   gtk_grab_add(pcontext.window);  
   
   /* check whether cache is in upper or lower half of window */  
   gint x, y, sx, sy;  
   osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget),  
                                    cache->pos.lat, cache->pos.lon,  
                                    &sx, &sy);  
   
   printf("screen pos %d/%d\n", sx, sy);  
   
   gdk_window_get_origin(mcontext->widget->window, &x, &y);  
   printf("window = %d/%d +%d+%d %d*%d\n", x, y,  
          mcontext->widget->allocation.x,  
          mcontext->widget->allocation.y,  
          mcontext->widget->allocation.width,  
          mcontext->widget->allocation.height  
          );  
   
   gint ax = 0, ay = 0;  
   if(sx > mcontext->widget->allocation.width/2)  
     ax = POPUP_WIDTH;  
   
   if(sy > mcontext->widget->allocation.height/2)  
     ay = POPUP_HEIGHT;  
   
   gtk_window_move(GTK_WINDOW(pcontext.window),  
                   x + mcontext->widget->allocation.x + sx - ax,  
                   y + mcontext->widget->allocation.y + sy - ay);  
   
   /* a frame with a vscale inside */  
   GtkWidget *frame = gtk_frame_new(NULL);  
   gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);  
   
   gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name));  
   gtk_container_add(GTK_CONTAINER(pcontext.window), frame);  
   
   gtk_widget_show_all(pcontext.window);  
   
   /* handle this popup until it's gone */  
   
   pcontext.loop = g_main_loop_new(NULL, FALSE);  
304    
305    GDK_THREADS_LEAVE();    /* for some reason there's a configure event with 1/1 */
306    g_main_loop_run(pcontext.loop);    /* on diablo. We just ignore this! */
   GDK_THREADS_ENTER();  
307    
308    g_main_loop_unref(pcontext.loop);    printf("on_map_configure %d %d\n",
309             widget->allocation.width,
310             widget->allocation.height);
311    
312      if(!context->map_complete &&
313         (widget->allocation.width > 100) &&
314         (widget->allocation.height > 100)) {
315    
316        /* setup cache state */
317        map_setup(context);
318    
319        /* set default values if they are invalid */
320        if(!context->appdata->map.zoom ||
321           isnan(context->appdata->map.pos.lat) ||
322           isnan(context->appdata->map.pos.lon)) {
323          printf("no valid map position found\n");
324    
325          pos_t *refpos = get_pos(context->appdata);
326          if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
327            printf("use refpos\n");
328    
329            /* use gps position if present */
330            context->appdata->map.pos = *refpos;
331            context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
332          } else {
333            printf("use zero pos\n");
334    
335            /* use world map otherwise */
336            context->appdata->map.pos.lat = 0.0;
337            context->appdata->map.pos.lon = 0.0;
338            context->appdata->map.zoom = 1;
339          }
340        }
341    
342        /* jump to initial position */
343        printf("osm_gps_map_set_mapcenter(%f,%f,%d)\n",
344               context->appdata->map.pos.lat,
345               context->appdata->map.pos.lon,
346               context->appdata->map.zoom);
347    
348        osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
349                                  context->appdata->map.pos.lat,
350                                  context->appdata->map.pos.lon,
351                                  context->appdata->map.zoom);
352        context->map_complete = TRUE;
353      }
354    
355    printf("cache popup removed\n");    return FALSE;
356  }  }
357    
358  static void  static void
359  map_cachelist_nearest(cache_t *cache, pos_t *pos,  map_cachelist_nearest(cache_t *cache, pos_t *pos,
360                        cache_t **result, float *distance) {                        cache_t **result, float *distance) {
361    
362    while(cache) {    while(cache) {
363        pos_t cpos = gpx_cache_pos(cache);
364    
365      float dist =      float dist =
366        pow(cache->pos.lat - pos->lat, 2) +        pow(cpos.lat - pos->lat, 2) +
367        pow(cache->pos.lon - pos->lon, 2);        pow(cpos.lon - pos->lon, 2);
368    
369      if(!(dist > *distance)) {      if(!(dist > *distance)) {
370        *result = cache;        *result = cache;
# Line 346  static cache_t *map_closest(map_context_ Line 379  static cache_t *map_closest(map_context_
379    cache_t *result = NULL;    cache_t *result = NULL;
380    float distance = NAN;    float distance = NAN;
381    
382  #ifdef USE_MAEMO    if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {
   if(!context->appdata->cur_gpx) {  
 #endif  
383      /* search all geocaches */      /* search all geocaches */
384      gpx_t *gpx = context->appdata->gpx;      gpx_t *gpx = context->appdata->gpx;
385      while(gpx) {      while(gpx) {
386        map_cachelist_nearest(gpx->cache, pos, &result, &distance);        map_cachelist_nearest(gpx->cache, pos, &result, &distance);
387        gpx = gpx->next;        gpx = gpx->next;
388      }      }
389  #ifdef USE_MAEMO    } else if(context->appdata->cur_gpx) {
   } else {  
390      map_cachelist_nearest(context->appdata->cur_gpx->cache,      map_cachelist_nearest(context->appdata->cur_gpx->cache,
391                            pos, &result, &distance);                            pos, &result, &distance);
392    }    } else
393  #endif      result = context->appdata->cur_gpx->cache;
394    
395    return result;    return result;
396  }  }
# Line 368  static cache_t *map_closest(map_context_ Line 398  static cache_t *map_closest(map_context_
398  /* translate between osm-gps-map positions and gpxview ones */  /* translate between osm-gps-map positions and gpxview ones */
399  pos_t coord2pos(coord_t coo) {  pos_t coord2pos(coord_t coo) {
400    pos_t pos;    pos_t pos;
401    pos.lat = RAD2DEG(coo.rlat);    pos.lat = rad2deg(coo.rlat);
402    pos.lon = RAD2DEG(coo.rlon);    pos.lon = rad2deg(coo.rlon);
403    return pos;    return pos;
404  }  }
405    
406  static int dist2pixel(map_context_t *context, float km, float lat) {  #define CLICK_FUZZ (24)
   int zoom;  
   g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);  
   
   /* world at zoom 1 == 512 pixels */  
   float m_per_pix =  
     cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom));  
   
   return 1000.0*km/m_per_pix;  
 }  
   
 #define CLICK_FUZZ (16)  
407    
408  static gboolean  static gboolean
409  on_map_button_press_event(GtkWidget *widget,  on_map_button_press_event(GtkWidget *widget,
410                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
411    
412    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
413    
414      /* check if we actually clicked parts of the OSD */
415      if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE)
416        return FALSE;
417    
418    /* got a press event without release event? eat it! */    /* got a press event without release event? eat it! */
419    if(context->press_on != NULL) {    if(context->press_on != NULL) {
420      printf("PRESS: already\n");      printf("PRESS: already\n");
421      return TRUE;      return FALSE;
422    }    }
423    
424    pos_t pos =    pos_t pos =
# Line 402  on_map_button_press_event(GtkWidget *wid Line 426  on_map_button_press_event(GtkWidget *wid
426    
427    cache_t *nearest = map_closest(context, &pos);    cache_t *nearest = map_closest(context, &pos);
428    if(nearest) {    if(nearest) {
429      float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);      pos_t cpos = gpx_cache_pos(nearest);
430      if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)  
431        float dist = gpx_pos_get_distance(pos, cpos, FALSE);
432        if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ)
433        context->press_on = nearest;        context->press_on = nearest;
434    }    }
435    
436    return FALSE;    return FALSE;
437  }  }
438    
439    static void
440    cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) {
441      /* convert the pixbuf into something cairo can handle */
442    
443      // Create a new ImageSurface
444      cairo_surface_t *image_surface =
445        cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
446                                   gdk_pixbuf_get_width(buf),
447                                   gdk_pixbuf_get_height(buf));
448    
449      // Create the new Context for the ImageSurface
450      g_assert(image_surface);
451      cairo_t *context = cairo_create(image_surface);
452    
453      // Draw the image on the new Context
454      gdk_cairo_set_source_pixbuf(context, buf, 0.0, 0.0);
455      cairo_paint(context);
456    
457      // now draw this onto the original context
458      cairo_set_source_surface(cr, image_surface, x, y);
459    
460      cairo_paint(cr);
461    }
462    
463    #ifndef BIG_BALLOONS
464    #define LINE_SKIP  7
465    #else
466    #define LINE_SKIP  12
467    #endif
468    
469    static void
470    balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) {
471      printf("balloon event: ");
472    
473      map_context_t *context = (map_context_t*)data;
474      cache_t *cache = context->balloon;
475    
476      if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW) {
477        printf("draw\n");
478    
479    #if 0
480        /* draw pink background to check clipping */
481        cairo_rectangle (event->data.draw.cr,
482                         event->data.draw.rect->x-20, event->data.draw.rect->y-20,
483                         event->data.draw.rect->w+40, event->data.draw.rect->h+40);
484        cairo_set_source_rgba (event->data.draw.cr, 1, 0, 0, 0.3);
485        cairo_fill_preserve (event->data.draw.cr);
486        cairo_set_line_width (event->data.draw.cr, 0);
487        cairo_stroke (event->data.draw.cr);
488    #endif
489    
490        /* leave a little border top and left */
491        gint x = event->data.draw.rect->x, y = event->data.draw.rect->y;
492    
493        /* draw the cache type icon ... */
494        GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);
495        cairo_draw_pixbuf(event->data.draw.cr, icon, x, y);
496    
497        if(cache->notes && cache->notes->override) {
498          GdkPixbuf *over = icon_get(ICON_MISC, 1);
499          cairo_draw_pixbuf(event->data.draw.cr, over, x, y);
500        }
501    
502        /* ... and right of it the waypoint id */
503        cairo_text_extents_t extents;
504    
505        if(cache->id) {
506          cairo_select_font_face (event->data.draw.cr, "Sans",
507                                  CAIRO_FONT_SLANT_NORMAL,
508                                  CAIRO_FONT_WEIGHT_BOLD);
509    
510    #ifndef BIG_BALLOONS
511          cairo_set_font_size (event->data.draw.cr, 20.0);
512    #else
513          cairo_set_font_size (event->data.draw.cr, 36.0);
514    #endif
515    
516          cairo_text_extents (event->data.draw.cr, cache->id, &extents);
517    
518          /* display id right of icon vertically centered */
519          x += gdk_pixbuf_get_width(icon) + 5;
520          y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
521          cairo_move_to (event->data.draw.cr, x, y);
522          cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
523          cairo_show_text (event->data.draw.cr, cache->id);
524          cairo_stroke (event->data.draw.cr);
525    
526          y += (gdk_pixbuf_get_height(icon) - extents.height)/2 + LINE_SKIP;
527        } else
528          y += gdk_pixbuf_get_height(icon);
529    
530        /* return to the left border and below icon/text */
531        x = event->data.draw.rect->x;
532    
533        /* everything from here uses the same font */
534        cairo_select_font_face (event->data.draw.cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
535                                CAIRO_FONT_WEIGHT_NORMAL);
536    #ifndef BIG_BALLOONS
537        cairo_set_font_size (event->data.draw.cr, 14.0);
538    #else
539        cairo_set_font_size (event->data.draw.cr, 22.0);
540    #endif
541    
542        if(cache->name) {
543          /* draw cache name */
544          cairo_text_extents (event->data.draw.cr, cache->name, &extents);
545          y += extents.height;
546          cairo_move_to (event->data.draw.cr, x, y);
547          cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
548          cairo_show_text (event->data.draw.cr, cache->name);
549          cairo_stroke (event->data.draw.cr);
550    
551          /* return to the left border and below text */
552          y += LINE_SKIP;
553          x = event->data.draw.rect->x;
554        }
555    
556        if(cache->terrain) {
557          /* draw cache rating */
558          const char *terrain = "Terrain:";
559          icon = icon_get(ICON_STARS, (int)(cache->terrain*2-2));
560          cairo_text_extents (event->data.draw.cr, _(terrain), &extents);
561          y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
562    
563          /* draw "Terrain:" string */
564          cairo_move_to (event->data.draw.cr, x, y);
565          cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
566          cairo_show_text (event->data.draw.cr, _(terrain));
567          cairo_stroke (event->data.draw.cr);
568          x += extents.width + 2;
569    
570          /* draw terrain stars */
571          cairo_draw_pixbuf(event->data.draw.cr, icon, x, y -
572                            (gdk_pixbuf_get_height(icon) + extents.height)/2);
573    
574          x += gdk_pixbuf_get_width(icon) + LINE_SKIP;
575        y -= (gdk_pixbuf_get_height(icon) + extents.height)/2;
576        }
577    
578        if(cache->difficulty) {
579          const char *difficulty = "Difficulty:";
580          cairo_text_extents (event->data.draw.cr, _(difficulty), &extents);
581          y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
582    
583          /* draw "Difficulty:" string */
584          cairo_move_to (event->data.draw.cr, x, y);
585          cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1);
586          cairo_show_text (event->data.draw.cr, _(difficulty));
587          cairo_stroke (event->data.draw.cr);
588          x += extents.width + 2;
589    
590          icon = icon_get(ICON_STARS, (int)(cache->difficulty*2-2));
591          cairo_draw_pixbuf(event->data.draw.cr, icon, x, y -
592                          (gdk_pixbuf_get_height(icon) + extents.height)/2);
593        }
594      } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_CLICK) {
595        printf("click %s event at %d %d\n",
596               event->data.click.down?"down":"up",
597               event->data.click.x, event->data.click.y);
598    
599        /* make the main screen jump to that cache */
600        if(!event->data.click.down) {
601          if(context->appdata->cur_cache) {
602            printf("ERROR: no current cache should be visible!\n");
603          } else {
604            gpx_t *is_in = NULL;
605    
606            if(!context->appdata->cur_gpx) {
607              printf("click while in \"all\" view\n");
608    
609              /* we first need to figure out which gpx file this cache */
610              /* is in so we can open it first */
611              gpx_t *gpx = context->appdata->gpx;
612              while(gpx && !is_in) {
613                cache_t *cur = gpx->cache;
614                while(cur && !is_in) {
615                  if(cur == cache)
616                    is_in = gpx;
617                  cur = cur->next;
618                }
619                gpx = gpx->next;
620              }
621    
622              if(is_in)
623                gpxlist_goto_cachelist(context->appdata, is_in);
624    
625            } else
626              /* the simple case: there already is an open gpx file and */
627              /* we just jump into the "cache" view */
628              is_in = context->appdata->cur_gpx;
629    
630            if(is_in) {
631              printf("selecting %s in %s\n",
632                     cache->id,
633                     context->appdata->cur_gpx->name);
634    
635              cachelist_goto_cache(context->appdata, cache);
636    
637              /* give focus to main screen (important for maemo) */
638              printf("raising main window\n");
639              gtk_window_present(GTK_WINDOW(context->appdata->window));
640            }
641          }
642        }
643      } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED) {
644        printf("removed\n");
645        context->balloon = NULL;
646      }
647    }
648    
649  static gboolean  static gboolean
650  on_map_button_release_event(GtkWidget *widget,  on_map_button_release_event(GtkWidget *widget,
651                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
652    if(context->press_on) {    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
653      OsmGpsMap *map = OSM_GPS_MAP(context->widget);  
654      /* in "MAP_CACHE" state only one cache is visible */
655      /* and the map is in navigation mode. the balloon is */
656      /* pretty useless there */
657      if(context->press_on && (context->state != MAP_CACHE)) {
658    
659        coord_t coo;
660        coo = osm_gps_map_get_co_ordinates(map, event->x, event->y);
661    
662      pos_t pos =      pos_t pos =
663        coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));        coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
664    
665      cache_t *nearest = map_closest(context, &pos);      cache_t *nearest = map_closest(context, &pos);
666      if(nearest && nearest == context->press_on) {      if(nearest && nearest == context->press_on) {
667        float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);        pos_t cpos = gpx_cache_pos(nearest);
668        if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)  
669          cache_popup(context, nearest);        float dist = gpx_pos_get_distance(pos, cpos, FALSE);
670          if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ) {
671    
672            context->balloon = nearest;
673            osm_gps_map_osd_draw_balloon(map, cpos.lat, cpos.lon,
674                                         balloon_cb, context);
675          }
676      }      }
677      context->press_on = NULL;      context->press_on = NULL;
678      } else {
679        /* save new map position */
680        gfloat lat, lon;
681        g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
682        context->appdata->map.pos.lat = lat;
683        context->appdata->map.pos.lon = lon;
684    }    }
685    
686    return FALSE;    return FALSE;
687  }  }
688    
   
 #if MAEMO_VERSION_MAJOR == 5  
689  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
690    printf("destroy map view\n");    appdata_t *appdata = context->appdata;
691    
692      /* save map parameters */
693      OsmGpsMap *map = OSM_GPS_MAP(context->widget);
694      gint zoom;
695      g_object_get(map, "zoom", &zoom, NULL);
696      context->appdata->map.zoom = zoom;
697    
698      gfloat lat, lon;
699      g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
700      context->appdata->map.pos.lat = lat;
701      context->appdata->map.pos.lon = lon;
702    
703      gint source;
704      g_object_get(map, "map-source", &source, NULL);
705      context->appdata->map.source = source;
706    
707    #if MAEMO_VERSION_MAJOR == 5
708    /* restore cur_view */    /* restore cur_view */
709    context->appdata->cur_view = context->old_view;    context->appdata->cur_view = context->old_view;
710    #endif
711    
712    gtk_timeout_remove(context->handler_id);    gtk_timeout_remove(context->handler_id);
713      gps_unregister_callback(appdata, context->cb_id);
714    
715      if(context->caches_displayed) {
716        g_free(context->caches_displayed);
717        context->caches_displayed = NULL;
718      }
719    
720    g_free(context);    g_free(context);
721      appdata->map.context = NULL;
722    }
723    
724    #if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__)
725    /* get access to zoom buttons */
726    static void
727    on_window_realize(GtkWidget *widget, gpointer data) {
728      if (widget->window) {
729        unsigned char value = 1;
730        Atom hildon_zoom_key_atom =
731          gdk_x11_get_xatom_by_name("_HILDON_ZOOM_KEY_ATOM"),
732          integer_atom = gdk_x11_get_xatom_by_name("INTEGER");
733        Display *dpy =
734          GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(widget->window));
735        Window w = GDK_WINDOW_XID(widget->window);
736    
737        XChangeProperty(dpy, w, hildon_zoom_key_atom,
738                        integer_atom, 8, PropModeReplace, &value, 1);
739      }
740  }  }
741  #endif  #endif
742    
743    /* on maemo a window is either on top or completely invisible. this */
744    /* means that we only need to update the map window if its raised.  */
745    /* on ordinary desktops this is different and we always update */
746    
747    static gboolean on_focus_in(GtkWidget *widget, GdkEventFocus *event,
748                             gpointer data) {
749      printf("map got focus\n");
750      map_setup((map_context_t*)data);
751      return FALSE;
752    }
753    
754    void map_update(appdata_t *appdata) {
755      printf("map_update\n");
756    #ifndef USE_MAEMO
757      if(appdata->map.context)
758        map_setup(appdata->map.context);
759    #endif
760    }
761    
762    /* callback called by the gps layer whenever gps state changes */
763    static void
764    gps_callback(struct gps_state *state, gpointer data) {
765      printf("map: gps callback\n");
766    }
767    
768  void map(appdata_t *appdata) {  void map(appdata_t *appdata) {
769    map_context_t *context = g_new0(map_context_t, 1);    map_context_t *context = NULL;
770    
771      /* if the map window already exists, just raise it */
772      if(appdata->map.context) {
773        printf("using existing map!\n");
774        gtk_window_present(GTK_WINDOW(appdata->map.context->window));
775        map_setup(appdata->map.context);
776        return;
777      }
778    
779      context = appdata->map.context = g_new0(map_context_t, 1);
780    context->appdata = appdata;    context->appdata = appdata;
781      context->map_complete = FALSE;
782      context->state = MAP_NONE;
783    
784    GtkWidget *hbox = gtk_hbox_new(FALSE, 0);    /* cleanup old (pre 0.8.7) path if it exists */
785      char *old_path = g_strdup_printf("%s/map/", appdata->image_path);
786      if(g_file_test(old_path, G_FILE_TEST_IS_DIR)) {
787        printf("old file path %s exists\n", old_path);
788        rmdir_recursive(old_path);
789      }
790    
791      /* It is recommanded that all applications share these same */
792      /* map path, so data is only cached once. The path should be: */
793      /* ~/.osm-gps-map on standard PC     (users home) */
794      /* /home/user/.osm-gps-map on Maemo5 (ext3 on internal card) */
795      /* /media/mmc2/osm-gps-map on Maemo4 (vfat on internal card) */
796    #if !defined(USE_MAEMO)
797      char *p = getenv("HOME");
798      if(!p) p = "/tmp";
799      char *path = g_strdup_printf("%s/.osm-gps-map", p);
800    #else
801    #if MAEMO_VERSION_MAJOR == 5
802      char *path = g_strdup("/home/user/.osm-gps-map");
803    #else
804      char *path = g_strdup("/media/mmc2/osm-gps-map");
805    #endif
806    #endif
807    
   char *path = g_strdup_printf("%s/map/", appdata->image_path);  
808    const char *proxy = get_proxy_uri(appdata);    const char *proxy = get_proxy_uri(appdata);
809    
810      gint source = context->appdata->map.source;
811      if(!source) source = MAP_SOURCE;
812    
813    context->widget = g_object_new(OSM_TYPE_GPS_MAP,    context->widget = g_object_new(OSM_TYPE_GPS_MAP,
814                  "repo-uri", MAP_SOURCE_OPENSTREETMAP,                   "map-source",               source,
815                  "tile-cache", path,                   "tile-cache",               path,
816                  proxy?"proxy-uri":NULL, proxy,                   "auto-center",              FALSE,
817                     "record-trip-history",      FALSE,
818                     "show-trip-history",        FALSE,
819                     proxy?"proxy-uri":NULL,     proxy,
820                   NULL);                   NULL);
821    
822    g_free(path);    g_free(path);
823    
824    char *name = NULL;    osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget));
 #ifdef USE_MAEMO  
   if(!appdata->cur_gpx) {  
 #endif  
     /* draw all geocaches */  
     gpx_t *gpx = appdata->gpx;  
     while(gpx) {  
       map_draw_cachelist(context->widget, gpx->cache);  
       gpx = gpx->next;  
     }  
     name = g_strdup(_("all geocaches"));  
 #ifdef USE_MAEMO  
   } else {  
     map_draw_cachelist(context->widget, appdata->cur_gpx->cache);  
     name = g_strdup(appdata->cur_gpx->name);  
   }  
 #endif  
   
   char *title = g_strdup_printf(_("Map - %s"), name);  
   g_free(name);  
825    
826  #if MAEMO_VERSION_MAJOR == 5  #ifdef USE_MAEMO
827    GtkWidget *window = hildon_stackable_window_new();    /* we don't use a stackable window here on fremantle, since */
828    gtk_window_set_title(GTK_WINDOW(window), title);    /* this leaves the main window independent from the map and */
829      /* the user can e.g. still navigate the main menu */
830      context->window = hildon_window_new();
831    
832    #if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__)
833      g_signal_connect(G_OBJECT(context->window), "realize",
834                       G_CALLBACK(on_window_realize), NULL);
835    #endif // MAEMO_VERSION
836  #else  #else
837    GtkWidget *dialog = gtk_dialog_new_with_buttons(title,    context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
838                            GTK_WINDOW(appdata->window),  #endif
                           GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,  
                           GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,  
                           NULL);  
839    
840  #ifndef USE_MAEMO  #ifndef USE_MAEMO
841    gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480);    gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);
 #else  
   gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480);  
 #endif  
842  #endif  #endif
843    
844    g_free(title);    g_signal_connect(G_OBJECT(context->widget), "focus-in-event",
845                       G_CALLBACK(on_focus_in), context);
846    
847    g_signal_connect(G_OBJECT(context->widget), "configure-event",    g_signal_connect(G_OBJECT(context->widget), "configure-event",
848                     G_CALLBACK(on_map_configure), context);                     G_CALLBACK(on_map_configure), context);
# Line 510  void map(appdata_t *appdata) { Line 853  void map(appdata_t *appdata) {
853    g_signal_connect(G_OBJECT(context->widget), "button-release-event",    g_signal_connect(G_OBJECT(context->widget), "button-release-event",
854                     G_CALLBACK(on_map_button_release_event), context);                     G_CALLBACK(on_map_button_release_event), context);
855    
   gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget);  
   /* zoom button box */  
   GtkWidget *vbox = gtk_vbox_new(FALSE,0);  
   
   context->zoomin =  
     map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin),  
                    context, _("Zoom in"));  
   gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0);  
   
   context->zoomout =  
     map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout),  
                    context, _("Zoom out"));  
   gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0);  
   
   context->gps =  
     map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps),  
                    context, _("Jump to GPS position"));  
   gtk_widget_set_sensitive(context->gps, FALSE);  
856    /* install handler for timed updates of the gps button */    /* install handler for timed updates of the gps button */
857    context->handler_id = gtk_timeout_add(1000, map_gps_update, context);    context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
858    gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0);    context->cb_id = gps_register_callback(appdata, gps_callback, context);
   
   gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);  
859    
860  #if MAEMO_VERSION_MAJOR == 5  #if MAEMO_VERSION_MAJOR == 5
861    /* prevent some of the main screen things */    /* prevent some of the main screen things */
862    context->old_view = appdata->cur_view;    context->old_view = appdata->cur_view;
863    appdata->cur_view = NULL;    appdata->cur_view = NULL;
864    #endif
865    
866    g_signal_connect(G_OBJECT(window), "destroy",    g_signal_connect(G_OBJECT(context->window), "destroy",
867                     G_CALLBACK(on_window_destroy), context);                     G_CALLBACK(on_window_destroy), context);
868    
869    gtk_container_add(GTK_CONTAINER(window), hbox);    gtk_container_add(GTK_CONTAINER(context->window), context->widget);
870    gtk_widget_show_all(GTK_WIDGET(window));    gtk_widget_show_all(GTK_WIDGET(context->window));
   
 #else  
   gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);  
   gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);  
   gtk_widget_show_all(dialog);  
   gtk_dialog_run(GTK_DIALOG(dialog));  
   gtk_timeout_remove(context->handler_id);  
   gtk_widget_destroy(dialog);  
   g_free(context);  
 #endif  
871  }  }

Legend:
Removed from v.45  
changed lines
  Added in v.152