--- trunk/src/map-tool.c 2009/08/16 19:29:01 57 +++ trunk/src/map-tool.c 2009/09/01 11:16:30 89 @@ -18,20 +18,23 @@ */ #include "gpxview.h" +#include "converter.h" #include // for isnan #ifdef ENABLE_OSM_GPS_MAP #include "osm-gps-map.h" +#include "osm-gps-map-osd-classic.h" #endif -#define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENSTREETMAP -#define GPS_DEFAULT_ZOOM 13 - -/* equatorial radius in meters */ -#define EQ_RADIUS (6378137.0) +#if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5) +#include +#include +#endif -#define RAD2DEG(a) (((a)*180.0)/M_PI) -#define DEG2RAD(a) (((a)*M_PI)/180.0) +// #define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENSTREETMAP +#define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENCYCLEMAP +// #define MAP_SOURCE OSM_GPS_MAP_SOURCE_GOOGLE_STREET +#define GPS_DEFAULT_ZOOM 13 #define PROXY_KEY "/system/http_proxy/" @@ -71,62 +74,28 @@ return NULL; } -static void map_zoom(map_context_t *context, int step) { - gint zoom; - 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 < osm_gps_map_source_get_max_zoom(MAP_SOURCE)); - gtk_widget_set_sensitive(context->zoomout, - zoom > osm_gps_map_source_get_min_zoom(MAP_SOURCE)); - - /* hmm ... this doesn't really work */ - osm_gps_map_osd_speed(map, zoom); - - /* save new zoom */ - context->appdata->map.zoom = zoom; -} - -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; -} +static void +cb_map_gps(osd_button_t but, map_context_t *context) { + if(but == OSD_GPS) { + pos_t *refpos = get_pos(context->appdata); + if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { + gint zoom; + g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL); + if(zoom < 10) + osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), + refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM); + else + osm_gps_map_set_center(OSM_GPS_MAP(context->widget), + refpos->lat, refpos->lon); -static gboolean -cb_map_gps(GtkButton *button, map_context_t *context) { - pos_t *refpos = get_pos(context->appdata); - if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { - osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), - refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM); - } else { - /* no coordinates given: display the entire world */ - osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), - 0.0, 0.0, 1); + /* re-enable centering */ + g_object_set(context->widget, "auto-center", TRUE, NULL); + } else { + /* no coordinates given: display the entire world */ + osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), + 0.0, 0.0, 1); + } } - - return FALSE; -} - -static GtkWidget -*map_add_button(int icon, GCallback cb, gpointer data, - char *tooltip) { - GtkWidget *button = gtk_button_new(); - gtk_button_set_image(GTK_BUTTON(button), icon_get_widget(ICON_MISC, icon)); - g_signal_connect(button, "clicked", cb, data); -#ifndef USE_MAEMO - gtk_widget_set_tooltip_text(button, tooltip); -#endif - return button; } static int dist2pixel(map_context_t *context, float km, float lat) { @@ -141,7 +110,8 @@ gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon); /* ... and enable "goto" button if it's valid */ - gtk_widget_set_sensitive(context->gps, ok); + osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget), + OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context); if(ok) { float heading = NAN; @@ -169,31 +139,35 @@ GdkEventConfigure *event, map_context_t *context) { - /* set default values if they are invalid */ - if(!context->appdata->map.zoom || - isnan(context->appdata->map.pos.lat) || - isnan(context->appdata->map.pos.lon)) { - printf("no valid map position found\n"); - - pos_t *refpos = get_pos(context->appdata); - if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { - /* use gps position if present */ - context->appdata->map.pos = *refpos; - context->appdata->map.zoom = GPS_DEFAULT_ZOOM; - } else { - /* use world map otherwise */ - context->appdata->map.pos.lat = 0.0; - context->appdata->map.pos.lon = 0.0; - context->appdata->map.zoom = 1; + if(!context->map_complete) { + + /* set default values if they are invalid */ + if(!context->appdata->map.zoom || + isnan(context->appdata->map.pos.lat) || + isnan(context->appdata->map.pos.lon)) { + printf("no valid map position found\n"); + + pos_t *refpos = get_pos(context->appdata); + if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { + /* use gps position if present */ + context->appdata->map.pos = *refpos; + context->appdata->map.zoom = GPS_DEFAULT_ZOOM; + } else { + /* use world map otherwise */ + context->appdata->map.pos.lat = 0.0; + context->appdata->map.pos.lon = 0.0; + context->appdata->map.zoom = 1; + } } + + /* jump to initial position */ + osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), + context->appdata->map.pos.lat, + context->appdata->map.pos.lon, + context->appdata->map.zoom); + context->map_complete = TRUE; } - /* jump to initial position */ - osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), - context->appdata->map.pos.lat, - context->appdata->map.pos.lon, - context->appdata->map.zoom); - return FALSE; } @@ -251,8 +225,8 @@ /* translate between osm-gps-map positions and gpxview ones */ pos_t coord2pos(coord_t coo) { pos_t pos; - pos.lat = RAD2DEG(coo.rlat); - pos.lon = RAD2DEG(coo.rlon); + pos.lat = rad2deg(coo.rlat); + pos.lon = rad2deg(coo.rlon); return pos; } @@ -263,10 +237,14 @@ GdkEventButton *event, map_context_t *context) { OsmGpsMap *map = OSM_GPS_MAP(context->widget); + /* check if we actually clicked parts of the OSD */ + if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE) + return FALSE; + /* got a press event without release event? eat it! */ if(context->press_on != NULL) { printf("PRESS: already\n"); - return TRUE; + return FALSE; } pos_t pos = @@ -282,6 +260,149 @@ return FALSE; } +static void +cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) { + /* convert the pixbuf into something cairo can handle */ + + // Create a new ImageSurface + cairo_surface_t *image_surface = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, + gdk_pixbuf_get_width(buf), + gdk_pixbuf_get_height(buf)); + + // Create the new Context for the ImageSurface + cairo_t *context = cairo_create(image_surface); + + // Draw the image on the new Context + gdk_cairo_set_source_pixbuf(context, buf, 0.0, 0.0); + cairo_paint(context); + + // now draw this onto the original context + cairo_set_source_surface(cr, image_surface, x, y); + + cairo_paint(cr); +} + +#ifndef BIG_BALLOONS +#define LINE_SKIP 7 +#else +#define LINE_SKIP 12 +#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; + + /* draw the cache type icon ... */ + GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); + cairo_draw_pixbuf(cr, icon, x, y); + + /* ... and right of it the waypoint id */ + cairo_text_extents_t extents; + + if(cache->id) { + cairo_select_font_face (cr, "Sans", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_BOLD); + +#ifndef BIG_BALLOONS + cairo_set_font_size (cr, 20.0); +#else + cairo_set_font_size (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); +#ifndef BIG_BALLOONS + cairo_set_font_size (cr, 14.0); +#else + cairo_set_font_size (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; + 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 - + (gdk_pixbuf_get_height(icon) + extents.height)/2); + } +} + static gboolean on_map_button_release_event(GtkWidget *widget, GdkEventButton *event, map_context_t *context) { @@ -299,7 +420,8 @@ float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) { - osm_gps_map_draw_balloon(map, nearest->pos.lat, nearest->pos.lon); + osm_gps_map_draw_balloon(map, nearest->pos.lat, nearest->pos.lon, + balloon_draw_cb, nearest); } } context->press_on = NULL; @@ -330,6 +452,10 @@ context->appdata->map.pos.lat = lat; context->appdata->map.pos.lon = lon; + gint source; + g_object_get(map, "map-source", &source, NULL); + context->appdata->map.source = source; + #if MAEMO_VERSION_MAJOR == 5 /* restore cur_view */ context->appdata->cur_view = context->old_view; @@ -341,6 +467,25 @@ appdata->map.context = NULL; } +#if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__) +/* get access to zoom buttons */ +static void +on_window_realize(GtkWidget *widget, gpointer data) { + if (widget->window) { + unsigned char value = 1; + Atom hildon_zoom_key_atom = + gdk_x11_get_xatom_by_name("_HILDON_ZOOM_KEY_ATOM"), + integer_atom = gdk_x11_get_xatom_by_name("INTEGER"); + Display *dpy = + GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(widget->window)); + Window w = GDK_WINDOW_XID(widget->window); + + XChangeProperty(dpy, w, hildon_zoom_key_atom, + integer_atom, 8, PropModeReplace, &value, 1); + } +} +#endif + void map(appdata_t *appdata) { map_context_t *context = NULL; @@ -352,14 +497,39 @@ context = appdata->map.context = g_new0(map_context_t, 1); context->appdata = appdata; + context->map_complete = FALSE; - GtkWidget *hbox = gtk_hbox_new(FALSE, 0); + /* cleanup old (pre 0.8.7) path if it exists */ + char *old_path = g_strdup_printf("%s/map/", appdata->image_path); + if(g_file_test(old_path, G_FILE_TEST_IS_DIR)) { + printf("old file path %s exists\n", old_path); + rmdir_recursive(old_path); + } + + /* It is recommanded that all applications share these same */ + /* map path, so data is only cached once. The path should be: */ + /* ~/.osm-gps-map on standard PC (users home) */ + /* /home/user/.osm-gps-map on Maemo5 (ext3 on internal card) */ + /* /media/mmc2/osm-gps-map on Maemo4 (vfat on internal card) */ +#if !defined(USE_MAEMO) + char *p = getenv("HOME"); + if(!p) p = "/tmp"; + char *path = g_strdup_printf("%s/.osm-gps-map", p); +#else +#if MAEMO_VERSION_MAJOR == 5 + char *path = g_strdup("/home/user/.osm-gps-map"); +#else + char *path = g_strdup("/media/mmc2/osm-gps-map"); +#endif +#endif - char *path = g_strdup_printf("%s/map/", appdata->image_path); const char *proxy = get_proxy_uri(appdata); + gint source = context->appdata->map.source; + if(!source) source = MAP_SOURCE; + context->widget = g_object_new(OSM_TYPE_GPS_MAP, - "map-source", MAP_SOURCE, + "map-source", source, "tile-cache", path, "auto-center", FALSE, "record-trip-history", FALSE, @@ -369,6 +539,8 @@ g_free(path); + osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget)); + char *name = NULL; #ifdef USE_MAEMO if(!appdata->cur_gpx) { @@ -393,6 +565,12 @@ #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__ + g_signal_connect(G_OBJECT(context->window), "realize", + G_CALLBACK(on_window_realize), NULL); +#endif // MAEMO_VERSION #else context->window = hildon_window_new(); #endif @@ -417,30 +595,8 @@ g_signal_connect(G_OBJECT(context->widget), "button-release-event", G_CALLBACK(on_map_button_release_event), context); - 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(10, 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(11, 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(9, G_CALLBACK(cb_map_gps), - context, _("Jump to GPS position")); - gtk_widget_set_sensitive(context->gps, FALSE); - /* install handler for timed updates of the gps button */ context->handler_id = gtk_timeout_add(1000, map_gps_update, context); - gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0); - - gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); #if MAEMO_VERSION_MAJOR == 5 /* prevent some of the main screen things */ @@ -451,6 +607,6 @@ g_signal_connect(G_OBJECT(context->window), "destroy", G_CALLBACK(on_window_destroy), context); - gtk_container_add(GTK_CONTAINER(context->window), hbox); + gtk_container_add(GTK_CONTAINER(context->window), context->widget); gtk_widget_show_all(GTK_WIDGET(context->window)); }