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

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

revision 136 by harbaum, Mon Oct 19 13:02:41 2009 UTC revision 148 by harbaum, Wed Oct 28 11:57:54 2009 UTC
# Line 80  static const char *get_proxy_uri(appdata Line 80  static const char *get_proxy_uri(appdata
80    
81  static void  static void
82  cb_map_gps(osd_button_t but, map_context_t *context) {  cb_map_gps(osd_button_t but, map_context_t *context) {
83    
84    if(but == OSD_GPS) {    if(but == OSD_GPS) {
85      pos_t *refpos = get_pos(context->appdata);      pos_t *refpos = get_pos(context->appdata);
86      if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {      if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
# Line 108  static int dist2pixel(map_context_t *con Line 109  static int dist2pixel(map_context_t *con
109    
110  static gboolean map_gps_update(gpointer data) {  static gboolean map_gps_update(gpointer data) {
111    map_context_t *context = (map_context_t*)data;    map_context_t *context = (map_context_t*)data;
112      static gboolean goto_is_enabled = FALSE;
113    
114    /* get reference position ... */    /* get reference position ... */
115    pos_t *refpos = get_pos(context->appdata);    pos_t *refpos = get_pos(context->appdata);
116    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
117    
118    /* ... and enable "goto" button if it's valid */    /* ... and enable "goto" button if it's valid */
119    osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget),    if(ok != goto_is_enabled) {
120                   OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context);      osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget),
121                    OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context);
122        goto_is_enabled = ok;
123      }
124    
125    if(ok) {    if(ok) {
126      float heading = NAN;      float heading = NAN;
# Line 130  static gboolean map_gps_update(gpointer Line 135  static gboolean map_gps_update(gpointer
135          radius = dist2pixel(context, eph/1000, refpos->lat);          radius = dist2pixel(context, eph/1000, refpos->lat);
136      }      }
137    
138        /* TODO: in order to save energy: only draw if state actually changed */
139    
140      g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);      g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);
141      osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),      osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),
142                           refpos->lat, refpos->lon, heading);                           refpos->lat, refpos->lon, heading);
# Line 139  static gboolean map_gps_update(gpointer Line 146  static gboolean map_gps_update(gpointer
146    return TRUE;    return TRUE;
147  }  }
148    
149    static void map_draw_cache(GtkWidget *map, cache_t *cache) {
150      GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);
151    
152      /* check if there's also an overwritten coordinate */
153      if(cache->notes && cache->notes->override) {
154        GdkPixbuf *over = icon_get(ICON_MISC, 1);
155    
156        osm_gps_map_add_image(OSM_GPS_MAP(map),
157              cache->notes->pos.lat, cache->notes->pos.lon, icon);
158    
159        osm_gps_map_add_image(OSM_GPS_MAP(map),
160              cache->notes->pos.lat, cache->notes->pos.lon, over);
161      } else {
162        if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon))
163          osm_gps_map_add_image(OSM_GPS_MAP(map),
164                                cache->pos.lat, cache->pos.lon, icon);
165      }
166    }
167    
168    static void map_draw_gpx(appdata_t *appdata, GtkWidget *map, gpx_t *gpx) {
169      if(!gpx->notes_loaded) {
170        notes_load_all(appdata, gpx);
171        gpx->notes_loaded = TRUE;
172      }
173    
174      cache_t *cache = gpx->cache;
175      while(cache) {
176        map_draw_cache(map, cache);
177        cache = cache->next;
178      }
179    }
180    
181    /* draw geocaches and set window title */
182    static void map_setup(map_context_t *context) {
183      char *name = NULL;
184    
185      if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {
186        if(context->state != MAP_ALL) {
187          printf("map_setup(ALL)\n");
188    
189    #ifdef OSD_NAV
190          /* no navigation in this mode */
191          osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));
192    #endif
193    
194          /* clear all existing ccahe images */
195          osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
196    
197          /* draw all geocaches */
198          gpx_t *gpx = context->appdata->gpx;
199          while(gpx) {
200            map_draw_gpx(context->appdata, context->widget, gpx);
201            gpx = gpx->next;
202          }
203          name = g_strdup(_("all"));
204          context->state = MAP_ALL;
205        }
206      } else if(!context->appdata->cur_cache) {
207        if(context->state != MAP_GPX) {
208          printf("map_setup(GPX)\n");
209    
210    #ifdef OSD_NAV
211          /* no navigation in this mode */
212          osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));
213    #endif
214    
215          /* clear all existing ccahe images */
216          osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
217    
218          map_draw_gpx(context->appdata, context->widget,
219                       context->appdata->cur_gpx);
220          name = g_strdup(context->appdata->cur_gpx->name);
221          context->state = MAP_GPX;
222        }
223      } else {
224        cache_t *cache = context->appdata->cur_cache;
225    
226        printf("map_setp(CACHE)\n");
227    
228        /* no balloons in this mode */
229        context->balloon = NULL;
230        osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(context->widget));
231    
232        /* clear all existing ccahe images */
233        osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
234    
235        map_draw_cache(context->widget, cache);
236        name = g_strdup(cache->name);
237        context->state = MAP_CACHE;
238    
239        /* navigation in this mode! */
240        pos_t cpos = gpx_cache_pos(cache);
241    
242    #ifdef OSD_NAV
243        osm_gps_map_osd_draw_nav (OSM_GPS_MAP(context->widget),
244                                  context->appdata->imperial,
245                                  cpos.lat, cpos.lon, cache->name);
246    #else
247    #warning OSD_NAV not defined!
248    #endif
249      }
250    
251      if(name) {
252        char *title = g_strdup_printf(_("Map - %s"), name);
253        g_free(name);
254    
255        gtk_window_set_title(GTK_WINDOW(context->window), title);
256    
257        g_free(title);
258      } else
259        printf("map_setup(keep)\n");
260    }
261    
262  static gboolean on_map_configure(GtkWidget *widget,  static gboolean on_map_configure(GtkWidget *widget,
263                                   GdkEventConfigure *event,                                   GdkEventConfigure *event,
264                                   map_context_t *context) {                                   map_context_t *context) {
265    
266    if(!context->map_complete) {    /* for some reason there's a configure event with 1/1 */
267      /* on diablo. We just ignore this! */
268    
269      printf("on_map_configure %d %d\n",
270             widget->allocation.width,
271             widget->allocation.height);
272    
273      if(!context->map_complete &&
274         (widget->allocation.width > 100) &&
275         (widget->allocation.height > 100)) {
276    
277        /* setup cache state */
278        map_setup(context);
279    
280      /* set default values if they are invalid */      /* set default values if they are invalid */
281      if(!context->appdata->map.zoom ||      if(!context->appdata->map.zoom ||
# Line 153  static gboolean on_map_configure(GtkWidg Line 285  static gboolean on_map_configure(GtkWidg
285    
286        pos_t *refpos = get_pos(context->appdata);        pos_t *refpos = get_pos(context->appdata);
287        if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {        if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
288            printf("use refpos\n");
289    
290          /* use gps position if present */          /* use gps position if present */
291          context->appdata->map.pos = *refpos;          context->appdata->map.pos = *refpos;
292          context->appdata->map.zoom = GPS_DEFAULT_ZOOM;          context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
293        } else {        } else {
294            printf("use zero pos\n");
295    
296          /* use world map otherwise */          /* use world map otherwise */
297          context->appdata->map.pos.lat = 0.0;          context->appdata->map.pos.lat = 0.0;
298          context->appdata->map.pos.lon = 0.0;          context->appdata->map.pos.lon = 0.0;
# Line 165  static gboolean on_map_configure(GtkWidg Line 301  static gboolean on_map_configure(GtkWidg
301      }      }
302    
303      /* jump to initial position */      /* jump to initial position */
304        printf("osm_gps_map_set_mapcenter(%f,%f,%d)\n",
305               context->appdata->map.pos.lat,
306               context->appdata->map.pos.lon,
307               context->appdata->map.zoom);
308    
309      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
310                                context->appdata->map.pos.lat,                                context->appdata->map.pos.lat,
311                                context->appdata->map.pos.lon,                                context->appdata->map.pos.lon,
# Line 175  static gboolean on_map_configure(GtkWidg Line 316  static gboolean on_map_configure(GtkWidg
316    return FALSE;    return FALSE;
317  }  }
318    
 static void map_draw_cache(GtkWidget *map, cache_t *cache) {  
   GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);  
   
   /* check if there's also an overwritten coordinate */  
   if(cache->notes && cache->notes->override) {  
     GdkPixbuf *over = icon_get(ICON_MISC, 1);  
   
     osm_gps_map_add_image(OSM_GPS_MAP(map),  
           cache->notes->pos.lat, cache->notes->pos.lon, icon);  
   
     osm_gps_map_add_image(OSM_GPS_MAP(map),  
           cache->notes->pos.lat, cache->notes->pos.lon, over);  
   } else  
     osm_gps_map_add_image(OSM_GPS_MAP(map),  
                           cache->pos.lat, cache->pos.lon, icon);  
   
 }  
   
 static void map_draw_gpx(appdata_t *appdata, GtkWidget *map, gpx_t *gpx) {  
   if(!gpx->notes_loaded) {  
     notes_load_all(appdata, gpx);  
     gpx->notes_loaded = TRUE;  
   }  
   
   cache_t *cache = gpx->cache;  
   while(cache) {  
     map_draw_cache(map, cache);  
     cache = cache->next;  
   }  
 }  
   
319  static void  static void
320  map_cachelist_nearest(cache_t *cache, pos_t *pos,  map_cachelist_nearest(cache_t *cache, pos_t *pos,
321                        cache_t **result, float *distance) {                        cache_t **result, float *distance) {
322    
323    while(cache) {    while(cache) {
324      pos_t cpos = gpx_cache_pos(cache);      pos_t cpos = gpx_cache_pos(cache);
325    
# Line 258  pos_t coord2pos(coord_t coo) { Line 369  pos_t coord2pos(coord_t coo) {
369  static gboolean  static gboolean
370  on_map_button_press_event(GtkWidget *widget,  on_map_button_press_event(GtkWidget *widget,
371                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
372    
373    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
374    
375    /* check if we actually clicked parts of the OSD */    /* check if we actually clicked parts of the OSD */
376    if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE)    if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE)
377      return FALSE;      return FALSE;
378    
379    /* got a press event without release event? eat it! */    /* got a press event without release event? eat it! */
# Line 316  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf Line 428  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf
428    
429  static void  static void
430  balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) {  balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) {
431      printf("balloon event: ");
432    
433    map_context_t *context = (map_context_t*)data;    map_context_t *context = (map_context_t*)data;
434    cache_t *cache = context->balloon;    cache_t *cache = context->balloon;
435    
   printf("balloon event: ");  
   
436    if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW) {    if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW) {
437      printf("draw\n");      printf("draw\n");
438    
# Line 444  balloon_cb(osm_gps_map_balloon_event_t * Line 556  balloon_cb(osm_gps_map_balloon_event_t *
556             event->data.click.down?"down":"up",             event->data.click.down?"down":"up",
557             event->data.click.x, event->data.click.y);             event->data.click.x, event->data.click.y);
558    
559        /* make the main screen jump to that cache */
560        if(!event->data.click.down) {
561          if(context->appdata->cur_cache) {
562            printf("ERROR: no current cache should be visible!\n");
563          } else {
564            gpx_t *is_in = NULL;
565    
566            if(!context->appdata->cur_gpx) {
567              printf("click while in \"all\" view\n");
568    
569              /* we first need to figure out which gpx file this cache */
570              /* is in so we can open it first */
571              gpx_t *gpx = context->appdata->gpx;
572              while(gpx && !is_in) {
573                cache_t *cur = gpx->cache;
574                while(cur && !is_in) {
575                  if(cur == cache)
576                    is_in = gpx;
577                  cur = cur->next;
578                }
579                gpx = gpx->next;
580              }
581    
582              if(is_in)
583                gpxlist_goto_cachelist(context->appdata, is_in);
584    
585            } else
586              /* the simple case: there already is an open gpx file and */
587              /* we just jump into the "cache" view */
588              is_in = context->appdata->cur_gpx;
589    
590            if(is_in) {
591              printf("selecting %s in %s\n",
592                     cache->id,
593                     context->appdata->cur_gpx->name);
594    
595              cachelist_goto_cache(context->appdata, cache);
596    
597              /* give focus to main screen (important for maemo) */
598              printf("raising main window\n");
599              gtk_window_present(GTK_WINDOW(context->appdata->window));
600            }
601          }
602        }
603    } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED) {    } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED) {
604      printf("removed\n");      printf("removed\n");
605      context->balloon = NULL;      context->balloon = NULL;
# Line 493  on_map_button_release_event(GtkWidget *w Line 649  on_map_button_release_event(GtkWidget *w
649  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
650    appdata_t *appdata = context->appdata;    appdata_t *appdata = context->appdata;
651    
   printf("destroy map window\n");  
   
652    /* save map parameters */    /* save map parameters */
653    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
654    gint zoom;    gint zoom;
# Line 540  on_window_realize(GtkWidget *widget, gpo Line 694  on_window_realize(GtkWidget *widget, gpo
694  }  }
695  #endif  #endif
696    
697  /* draw geocaches and set window title */  /* on maemo a window is either on top or completely invisible. this */
698  static void map_setup(map_context_t *context) {  /* means that we only need to update the map window if its raised.  */
699    char *name = NULL;  /* on ordinary desktops this is different and we always update */
   
   if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {  
     if(context->state != MAP_ALL) {  
       printf("map_setp(ALL)\n");  
   
       /* no navigation in this mode */  
       osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));  
   
       /* clear all existing ccahe images */  
       osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));  
700    
       /* draw all geocaches */  
       gpx_t *gpx = context->appdata->gpx;  
       while(gpx) {  
         map_draw_gpx(context->appdata, context->widget, gpx);  
         gpx = gpx->next;  
       }  
       name = g_strdup(_("all"));  
       context->state = MAP_ALL;  
     }  
   } else if(!context->appdata->cur_cache) {  
     if(context->state != MAP_GPX) {  
       printf("map_setp(GPX)\n");  
   
       /* no navigation in this mode */  
       osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));  
   
       /* clear all existing ccahe images */  
       osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));  
   
       map_draw_gpx(context->appdata, context->widget,  
                    context->appdata->cur_gpx);  
       name = g_strdup(context->appdata->cur_gpx->name);  
       context->state = MAP_GPX;  
     }  
   } else {  
     cache_t *cache = context->appdata->cur_cache;  
   
     printf("map_setp(CACHE)\n");  
   
     /* no balloons in this mode */  
     context->balloon = NULL;  
     osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(context->widget));  
   
     /* clear all existing ccahe images */  
     osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));  
   
     map_draw_cache(context->widget, cache);  
     name = g_strdup(cache->name);  
     context->state = MAP_CACHE;  
   
     /* navigation in this mode! */  
     pos_t cpos = gpx_cache_pos(cache);  
     osm_gps_map_osd_draw_nav (OSM_GPS_MAP(context->widget),  
                               context->appdata->imperial,  
                               cpos.lat, cpos.lon, cache->name);  
   }  
   
   if(name) {  
     char *title = g_strdup_printf(_("Map - %s"), name);  
     g_free(name);  
   
     gtk_window_set_title(GTK_WINDOW(context->window), title);  
   
     g_free(title);  
   } else  
     printf("map_setup(keep)\n");  
 }  
   
 /* on maemo a window is either on top or completely invisible. this means that */  
 /* we only need to update the map window if its raised. on ordinary desktops this */  
 /* is different and we always update */  
701  static gboolean on_focus_in(GtkWidget *widget, GdkEventFocus *event,  static gboolean on_focus_in(GtkWidget *widget, GdkEventFocus *event,
702                           gpointer data) {                           gpointer data) {
703    printf("map got focus\n");    printf("map got focus\n");
# Line 623  static gboolean on_focus_in(GtkWidget *w Line 706  static gboolean on_focus_in(GtkWidget *w
706  }  }
707    
708  void map_update(appdata_t *appdata) {  void map_update(appdata_t *appdata) {
709      printf("map_update\n");
710  #ifndef USE_MAEMO  #ifndef USE_MAEMO
711    if(appdata->map.context)    if(appdata->map.context)
712      map_setup(appdata->map.context);      map_setup(appdata->map.context);
# Line 701  void map(appdata_t *appdata) { Line 785  void map(appdata_t *appdata) {
785    context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);    context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
786  #endif  #endif
787    
   /* setup cache state */  
   map_setup(context);  
   
788  #ifndef USE_MAEMO  #ifndef USE_MAEMO
789    gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);    gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);
790  #endif  #endif

Legend:
Removed from v.136  
changed lines
  Added in v.148