--- trunk/src/map-tool.c 2009/09/07 13:20:37 98 +++ trunk/src/map-tool.c 2009/10/26 10:40:06 142 @@ -175,13 +175,33 @@ return FALSE; } -static void map_draw_cachelist(GtkWidget *map, cache_t *cache) { - while(cache) { - GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); +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; } } @@ -190,9 +210,11 @@ map_cachelist_nearest(cache_t *cache, pos_t *pos, cache_t **result, float *distance) { while(cache) { + pos_t cpos = gpx_cache_pos(cache); + float dist = - pow(cache->pos.lat - pos->lat, 2) + - pow(cache->pos.lon - pos->lon, 2); + pow(cpos.lat - pos->lat, 2) + + pow(cpos.lon - pos->lon, 2); if(!(dist > *distance)) { *result = cache; @@ -207,21 +229,18 @@ cache_t *result = NULL; float distance = NAN; -#ifdef USE_MAEMO - if(!context->appdata->cur_gpx) { -#endif + if(!context->appdata->cur_gpx && !context->appdata->cur_cache) { /* search all geocaches */ gpx_t *gpx = context->appdata->gpx; while(gpx) { map_cachelist_nearest(gpx->cache, pos, &result, &distance); gpx = gpx->next; } -#ifdef USE_MAEMO - } else { + } else if(context->appdata->cur_gpx) { map_cachelist_nearest(context->appdata->cur_gpx->cache, pos, &result, &distance); - } -#endif + } else + result = context->appdata->cur_gpx->cache; return result; } @@ -256,8 +275,10 @@ cache_t *nearest = map_closest(context, &pos); if(nearest) { - float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); - if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) + pos_t cpos = gpx_cache_pos(nearest); + + float dist = gpx_pos_get_distance(pos, cpos, FALSE); + if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ) context->press_on = nearest; } @@ -294,116 +315,182 @@ #endif static void -balloon_draw_cb(cairo_t *cr, OsmGpsMapRect_t *rect, gpointer data) { - cache_t *cache = (cache_t*)data; - -#if 0 - /* draw pink background to check clipping */ - cairo_rectangle (cr, rect->x-20, rect->y-20, rect->w+40, rect->h+40); - cairo_set_source_rgba (cr, 1, 0, 0, 0.3); - cairo_fill_preserve (cr); - cairo_set_line_width (cr, 0); - cairo_stroke (cr); -#endif - - /* leave a little border top and left */ - gint x = rect->x, y = rect->y; +balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) { + map_context_t *context = (map_context_t*)data; + cache_t *cache = context->balloon; - /* draw the cache type icon ... */ - GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); - cairo_draw_pixbuf(cr, icon, x, y); + printf("balloon event: "); - /* ... and right of it the waypoint id */ - cairo_text_extents_t extents; + if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW) { + printf("draw\n"); - if(cache->id) { - cairo_select_font_face (cr, "Sans", - CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_BOLD); - +#if 0 + /* draw pink background to check clipping */ + cairo_rectangle (event->data.draw.cr, + event->data.draw.rect->x-20, event->data.draw.rect->y-20, + event->data.draw.rect->w+40, event->data.draw.rect->h+40); + cairo_set_source_rgba (event->data.draw.cr, 1, 0, 0, 0.3); + cairo_fill_preserve (event->data.draw.cr); + cairo_set_line_width (event->data.draw.cr, 0); + cairo_stroke (event->data.draw.cr); +#endif + + /* leave a little border top and left */ + gint x = event->data.draw.rect->x, y = event->data.draw.rect->y; + + /* draw the cache type icon ... */ + GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); + cairo_draw_pixbuf(event->data.draw.cr, icon, x, y); + + if(cache->notes && cache->notes->override) { + GdkPixbuf *over = icon_get(ICON_MISC, 1); + cairo_draw_pixbuf(event->data.draw.cr, over, x, y); + } + + /* ... and right of it the waypoint id */ + cairo_text_extents_t extents; + + if(cache->id) { + cairo_select_font_face (event->data.draw.cr, "Sans", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_BOLD); + #ifndef BIG_BALLOONS - cairo_set_font_size (cr, 20.0); + cairo_set_font_size (event->data.draw.cr, 20.0); #else - cairo_set_font_size (cr, 36.0); + cairo_set_font_size (event->data.draw.cr, 36.0); #endif - - cairo_text_extents (cr, cache->id, &extents); - - /* display id right of icon vertically centered */ - x += gdk_pixbuf_get_width(icon) + 5; - y += (gdk_pixbuf_get_height(icon) + extents.height)/2; - cairo_move_to (cr, x, y); - cairo_set_source_rgba (cr, 0, 0, 0, 1); - cairo_show_text (cr, cache->id); - cairo_stroke (cr); - - y += (gdk_pixbuf_get_height(icon) - extents.height)/2 + LINE_SKIP; - } else - y += gdk_pixbuf_get_height(icon); - - /* return to the left border and below icon/text */ - x = rect->x; - - /* everything from here uses the same font */ - cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_NORMAL); + + cairo_text_extents (event->data.draw.cr, cache->id, &extents); + + /* display id right of icon vertically centered */ + x += gdk_pixbuf_get_width(icon) + 5; + y += (gdk_pixbuf_get_height(icon) + extents.height)/2; + cairo_move_to (event->data.draw.cr, x, y); + cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); + cairo_show_text (event->data.draw.cr, cache->id); + cairo_stroke (event->data.draw.cr); + + y += (gdk_pixbuf_get_height(icon) - extents.height)/2 + LINE_SKIP; + } else + y += gdk_pixbuf_get_height(icon); + + /* return to the left border and below icon/text */ + x = event->data.draw.rect->x; + + /* everything from here uses the same font */ + cairo_select_font_face (event->data.draw.cr, "Sans", CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); #ifndef BIG_BALLOONS - cairo_set_font_size (cr, 14.0); + cairo_set_font_size (event->data.draw.cr, 14.0); #else - cairo_set_font_size (cr, 22.0); + cairo_set_font_size (event->data.draw.cr, 22.0); #endif - - if(cache->name) { - /* draw cache name */ - cairo_text_extents (cr, cache->name, &extents); - y += extents.height; - cairo_move_to (cr, x, y); - cairo_set_source_rgba (cr, 0, 0, 0, 1); - cairo_show_text (cr, cache->name); - cairo_stroke (cr); - - /* return to the left border and below text */ - y += LINE_SKIP; - x = rect->x; - } - - if(cache->terrain) { - /* draw cache rating */ - const char *terrain = "Terrain:"; - icon = icon_get(ICON_STARS, (int)(cache->terrain*2-2)); - cairo_text_extents (cr, _(terrain), &extents); - y += (gdk_pixbuf_get_height(icon) + extents.height)/2; - - /* draw "Terrain:" string */ - cairo_move_to (cr, x, y); - cairo_set_source_rgba (cr, 0, 0, 0, 1); - cairo_show_text (cr, _(terrain)); - cairo_stroke (cr); - x += extents.width + 2; - - /* draw terrain stars */ - cairo_draw_pixbuf(cr, icon, x, y - - (gdk_pixbuf_get_height(icon) + extents.height)/2); - - x += gdk_pixbuf_get_width(icon) + LINE_SKIP; + + if(cache->name) { + /* draw cache name */ + cairo_text_extents (event->data.draw.cr, cache->name, &extents); + y += extents.height; + cairo_move_to (event->data.draw.cr, x, y); + cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); + cairo_show_text (event->data.draw.cr, cache->name); + cairo_stroke (event->data.draw.cr); + + /* return to the left border and below text */ + y += LINE_SKIP; + x = event->data.draw.rect->x; + } + + if(cache->terrain) { + /* draw cache rating */ + const char *terrain = "Terrain:"; + icon = icon_get(ICON_STARS, (int)(cache->terrain*2-2)); + cairo_text_extents (event->data.draw.cr, _(terrain), &extents); + y += (gdk_pixbuf_get_height(icon) + extents.height)/2; + + /* draw "Terrain:" string */ + cairo_move_to (event->data.draw.cr, x, y); + cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); + cairo_show_text (event->data.draw.cr, _(terrain)); + cairo_stroke (event->data.draw.cr); + x += extents.width + 2; + + /* draw terrain stars */ + cairo_draw_pixbuf(event->data.draw.cr, icon, x, y - + (gdk_pixbuf_get_height(icon) + extents.height)/2); + + x += gdk_pixbuf_get_width(icon) + LINE_SKIP; y -= (gdk_pixbuf_get_height(icon) + extents.height)/2; - } - - if(cache->difficulty) { - const char *difficulty = "Difficulty:"; - cairo_text_extents (cr, _(difficulty), &extents); - y += (gdk_pixbuf_get_height(icon) + extents.height)/2; - - /* draw "Difficulty:" string */ - cairo_move_to (cr, x, y); - cairo_set_source_rgba (cr, 0, 0, 0, 1); - cairo_show_text (cr, _(difficulty)); - cairo_stroke (cr); - x += extents.width + 2; + } - icon = icon_get(ICON_STARS, (int)(cache->difficulty*2-2)); - cairo_draw_pixbuf(cr, icon, x, y - + if(cache->difficulty) { + const char *difficulty = "Difficulty:"; + cairo_text_extents (event->data.draw.cr, _(difficulty), &extents); + y += (gdk_pixbuf_get_height(icon) + extents.height)/2; + + /* draw "Difficulty:" string */ + cairo_move_to (event->data.draw.cr, x, y); + cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); + cairo_show_text (event->data.draw.cr, _(difficulty)); + cairo_stroke (event->data.draw.cr); + x += extents.width + 2; + + icon = icon_get(ICON_STARS, (int)(cache->difficulty*2-2)); + cairo_draw_pixbuf(event->data.draw.cr, icon, x, y - (gdk_pixbuf_get_height(icon) + extents.height)/2); + } + } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_CLICK) { + printf("click %s event at %d %d\n", + event->data.click.down?"down":"up", + event->data.click.x, event->data.click.y); + + /* make the main screen jump to that cache */ + if(!event->data.click.down) { + if(context->appdata->cur_cache) { + printf("ERROR: no current cache should be visible!\n"); + } else { + gpx_t *is_in = NULL; + + if(!context->appdata->cur_gpx) { + printf("click while in \"all\" view\n"); + + /* we first need to figure out which gpx file this cache */ + /* is in so we can open it first */ + gpx_t *gpx = context->appdata->gpx; + while(gpx && !is_in) { + cache_t *cur = gpx->cache; + while(cur && !is_in) { + if(cur == cache) + is_in = gpx; + cur = cur->next; + } + gpx = gpx->next; + } + + if(is_in) + gpxlist_goto_cachelist(context->appdata, is_in); + + } else + /* the simple case: there already is an open gpx file and */ + /* we just jump into the "cache" view */ + is_in = context->appdata->cur_gpx; + + if(is_in) { + printf("selecting %s in %s\n", + cache->id, + context->appdata->cur_gpx->name); + + cachelist_goto_cache(context->appdata, cache); + + /* give focus to main screen (important for maemo) */ + printf("raising main window\n"); + gtk_window_present(GTK_WINDOW(context->appdata->window)); + } + } + } + } else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED) { + printf("removed\n"); + context->balloon = NULL; } } @@ -412,7 +499,11 @@ GdkEventButton *event, map_context_t *context) { OsmGpsMap *map = OSM_GPS_MAP(context->widget); - if(context->press_on) { + /* in "MAP_CACHE" state only one cache is visible */ + /* and the map is in navigation mode. the balloon is */ + /* pretty useless there */ + if(context->press_on && (context->state != MAP_CACHE)) { + coord_t coo; coo = osm_gps_map_get_co_ordinates(map, event->x, event->y); @@ -421,11 +512,14 @@ cache_t *nearest = map_closest(context, &pos); if(nearest && nearest == context->press_on) { - float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); - if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) { + pos_t cpos = gpx_cache_pos(nearest); + + float dist = gpx_pos_get_distance(pos, cpos, FALSE); + if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ) { - osm_gps_map_draw_balloon(map, nearest->pos.lat, nearest->pos.lon, - balloon_draw_cb, nearest); + context->balloon = nearest; + osm_gps_map_osd_draw_balloon(map, cpos.lat, cpos.lon, + balloon_cb, context); } } context->press_on = NULL; @@ -490,18 +584,110 @@ } #endif +/* draw geocaches and set window title */ +static void map_setup(map_context_t *context) { + char *name = NULL; + + 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)); + + /* 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("%s - %s", _("Map"), 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 */ +static gboolean on_focus_in(GtkWidget *widget, GdkEventFocus *event, + gpointer data) { + printf("map got focus\n"); + map_setup((map_context_t*)data); + return FALSE; +} + +void map_update(appdata_t *appdata) { +#ifndef USE_MAEMO + if(appdata->map.context) + map_setup(appdata->map.context); +#endif +} + void map(appdata_t *appdata) { map_context_t *context = NULL; /* if the map window already exists, just raise it */ if(appdata->map.context) { + printf("using existing map!\n"); gtk_window_present(GTK_WINDOW(appdata->map.context->window)); + map_setup(appdata->map.context); return; } context = appdata->map.context = g_new0(map_context_t, 1); context->appdata = appdata; context->map_complete = FALSE; + context->state = MAP_NONE; /* cleanup old (pre 0.8.7) path if it exists */ char *old_path = g_strdup_printf("%s/map/", appdata->image_path); @@ -544,51 +730,30 @@ g_free(path); osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget)); - - char *name = NULL; -#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); #ifdef USE_MAEMO -#ifdef USE_STACKABLE_WINDOW - context->window = hildon_stackable_window_new(); - /* try to enable the zoom buttons. don't do this on x86 as it breaks */ - /* at runtime with cygwin x */ -#ifndef __i386__ + /* we don't use a stackable window here on fremantle, since */ + /* this leaves the main window independent from the map and */ + /* the user can e.g. still navigate the main menu */ + context->window = hildon_window_new(); + +#if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__) g_signal_connect(G_OBJECT(context->window), "realize", G_CALLBACK(on_window_realize), NULL); #endif // MAEMO_VERSION #else - context->window = hildon_window_new(); -#endif -#else context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); #endif - gtk_window_set_title(GTK_WINDOW(context->window), title); + /* setup cache state */ + map_setup(context); #ifndef USE_MAEMO gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480); #endif - g_free(title); + g_signal_connect(G_OBJECT(context->widget), "focus-in-event", + G_CALLBACK(on_focus_in), context); g_signal_connect(G_OBJECT(context->widget), "configure-event", G_CALLBACK(on_map_configure), context);