X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/81765f5e2d31f1dbc34e7cf05b8084106ceb1343..e29aad19b9c3a0e0686354e3896eda9254d0e06c:/src/vikviewport.c?ds=sidebyside diff --git a/src/vikviewport.c b/src/vikviewport.c index 3bc35005..eaff83a9 100644 --- a/src/vikviewport.c +++ b/src/vikviewport.c @@ -2,6 +2,7 @@ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager * * Copyright (C) 2003-2007, Evan Battaglia + * Copyright (C) 2013, Rob Norris * * Lat/Lon plotting functions calcxy* are from GPSDrive * GPSDrive Copyright (C) 2001-2004 Fritz Ganter @@ -43,11 +44,14 @@ #include "vikcoord.h" #include "vikwindow.h" #include "vikviewport.h" - #include "mapcoord.h" /* for ALTI_TO_MPP */ #include "globals.h" +#include "settings.h" +#include "dialog.h" + +#define MERCATOR_FACTOR(x) ( (65536.0 / 180 / (x)) * 256.0 ) static gdouble EASTING_OFFSET = 500000.0; @@ -55,6 +59,8 @@ static gint PAD = 10; static void viewport_finalize ( GObject *gob ); static void viewport_utm_zone_check ( VikViewport *vvp ); +static void update_centers ( VikViewport *vvp ); +static void free_centers ( VikViewport *vvp, guint start ); static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 ); static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 ); @@ -65,14 +71,19 @@ static void viewport_init_ra(); static GObjectClass *parent_class; - struct _VikViewport { GtkDrawingArea drawing_area; GdkPixmap *scr_buffer; gint width, height; + gint width_2, height_2; // Half of the normal width and height VikCoord center; VikCoordMode coord_mode; gdouble xmpp, ympp; + gdouble xmfactor, ymfactor; + GList *centers; // The history of requested positions (of VikCoord type) + guint centers_index; // current position within the history list + guint centers_max; // configurable maximum size of the history list + guint centers_radius; // Metres GdkPixbuf *alpha_pixbuf; guint8 alpha_pixbuf_width; @@ -123,6 +134,12 @@ viewport_utm_zone_width ( VikViewport *vvp ) return 0.0; } +enum { + VW_UPDATED_CENTER_SIGNAL = 0, + VW_LAST_SIGNAL, +}; +static guint viewport_signals[VW_LAST_SIGNAL] = { 0 }; + G_DEFINE_TYPE (VikViewport, vik_viewport, GTK_TYPE_DRAWING_AREA) static void @@ -136,6 +153,10 @@ vik_viewport_class_init ( VikViewportClass *klass ) object_class->finalize = viewport_finalize; parent_class = g_type_class_peek_parent (klass); + + viewport_signals[VW_UPDATED_CENTER_SIGNAL] = g_signal_new ( "updated_center", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikViewportClass, updated_center), NULL, NULL, + g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); } VikViewport *vik_viewport_new () @@ -144,6 +165,13 @@ VikViewport *vik_viewport_new () return vv; } +#define VIK_SETTINGS_VIEW_LAST_LATITUDE "viewport_last_latitude" +#define VIK_SETTINGS_VIEW_LAST_LONGITUDE "viewport_last_longitude" +#define VIK_SETTINGS_VIEW_LAST_ZOOM_X "viewport_last_zoom_xpp" +#define VIK_SETTINGS_VIEW_LAST_ZOOM_Y "viewport_last_zoom_ypp" +#define VIK_SETTINGS_VIEW_HISTORY_SIZE "viewport_history_size" +#define VIK_SETTINGS_VIEW_HISTORY_DIFF_DIST "viewport_history_diff_dist" + static void vik_viewport_init ( VikViewport *vvp ) { @@ -153,11 +181,27 @@ vik_viewport_init ( VikViewport *vvp ) struct LatLon ll; ll.lat = a_vik_get_default_lat(); ll.lon = a_vik_get_default_long(); + gdouble zoom_x = 4.0; + gdouble zoom_y = 4.0; + + if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) { + gdouble lat, lon, dzoom; + if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, &lat ) ) + ll.lat = lat; + if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, &lon ) ) + ll.lon = lon; + if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, &dzoom ) ) + zoom_x = dzoom; + if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, &dzoom ) ) + zoom_y = dzoom; + } + a_coords_latlon_to_utm ( &ll, &utm ); - /* TODO: not static */ - vvp->xmpp = 4.0; - vvp->ympp = 4.0; + vvp->xmpp = zoom_x; + vvp->ympp = zoom_y; + vvp->xmfactor = MERCATOR_FACTOR (vvp->xmpp); + vvp->ymfactor = MERCATOR_FACTOR (vvp->ympp); vvp->coord_mode = VIK_COORD_LATLON; vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR; vvp->center.mode = VIK_COORD_LATLON; @@ -174,6 +218,15 @@ vik_viewport_init ( VikViewport *vvp ) vvp->scale_bg_gc = NULL; vvp->copyrights = NULL; + vvp->centers = NULL; + vvp->centers_index = 0; + vvp->centers_max = 20; + gint tmp = vvp->centers_max; + if ( a_settings_get_integer ( VIK_SETTINGS_VIEW_HISTORY_SIZE, &tmp ) ) + vvp->centers_max = tmp; + vvp->centers_radius = 500; + if ( a_settings_get_integer ( VIK_SETTINGS_VIEW_HISTORY_DIFF_DIST, &tmp ) ) + vvp->centers_radius = tmp; vvp->draw_scale = TRUE; vvp->draw_centermark = TRUE; @@ -183,9 +236,16 @@ vik_viewport_init ( VikViewport *vvp ) vvp->snapshot_buffer = NULL; vvp->half_drawn = FALSE; + // Initiate center history + update_centers ( vvp ); + g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL); +#if GTK_CHECK_VERSION (2,18,0) + gtk_widget_set_can_focus ( GTK_WIDGET(vvp), TRUE ); +#else GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */ +#endif } GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp ) @@ -264,7 +324,7 @@ GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thic GdkGC *rv = NULL; GdkColor color; - rv = gdk_gc_new ( GTK_WIDGET(vvp)->window ); + rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) ); if ( gdk_color_parse ( colorname, &color ) ) gdk_gc_set_rgb_fg_color ( rv, &color ); else @@ -277,7 +337,7 @@ GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint { GdkGC *rv; - rv = gdk_gc_new ( GTK_WIDGET(vvp)->window ); + rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) ); gdk_gc_set_rgb_fg_color ( rv, color ); gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND ); return rv; @@ -287,14 +347,18 @@ void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint heigh { vvp->width = width; vvp->height = height; + + vvp->width_2 = vvp->width/2; + vvp->height_2 = vvp->height/2; + if ( vvp->scr_buffer ) g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); - vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 ); + vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); /* TODO trigger: only if this is enabled !!! */ if ( vvp->snapshot_buffer ) g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) ); - vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 ); + vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); } @@ -307,19 +371,24 @@ gboolean vik_viewport_configure ( VikViewport *vvp ) { g_return_val_if_fail ( vvp != NULL, TRUE ); - vvp->width = GTK_WIDGET(vvp)->allocation.width; - vvp->height = GTK_WIDGET(vvp)->allocation.height; + GtkAllocation allocation; + gtk_widget_get_allocation ( GTK_WIDGET(vvp), &allocation ); + vvp->width = allocation.width; + vvp->height = allocation.height; + + vvp->width_2 = vvp->width/2; + vvp->height_2 = vvp->height/2; if ( vvp->scr_buffer ) g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); - vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 ); + vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); /* TODO trigger: only if enabled! */ if ( vvp->snapshot_buffer ) g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) ); - vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 ); + vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); /* TODO trigger */ /* this is down here so it can get a GC (necessary?) */ @@ -346,6 +415,18 @@ static void viewport_finalize ( GObject *gob ) g_return_if_fail ( vvp != NULL ); + if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) { + struct LatLon ll; + vik_coord_to_latlon ( &(vvp->center), &ll ); + a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, ll.lat ); + a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, ll.lon ); + a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, vvp->xmpp ); + a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, vvp->ympp ); + } + + if ( vvp->centers ) + free_centers ( vvp, 0 ); + if ( vvp->scr_buffer ) g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); @@ -423,6 +504,10 @@ void vik_viewport_draw_scale ( VikViewport *vvp ) // in 0.1 miles (copes better when zoomed in as 1 mile can be too big) base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0; break; + case VIK_UNITS_DISTANCE_NAUTICAL_MILES: + // in 0.1 NM (copes better when zoomed in as 1 NM can be too big) + base = VIK_METERS_TO_NAUTICAL_MILES(vik_coord_diff ( &left, &right )) * 10.0; + break; default: base = 1; // Keep the compiler happy g_critical("Houston, we've had a problem. distance=%d", dist_units); @@ -452,18 +537,18 @@ void vik_viewport_draw_scale ( VikViewport *vvp ) vik_viewport_draw_line(vvp, vvp->scale_bg_gc, PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT); /* black scale */ - vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, + vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, PAD, vvp->height-PAD, PAD + len, vvp->height-PAD); - vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, + vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT); - vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, + vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT); if (odd%2) { int i; for (i=1; i<5; i++) { vik_viewport_draw_line(vvp, vvp->scale_bg_gc, PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); - vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, + vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); } } else { @@ -471,38 +556,50 @@ void vik_viewport_draw_scale ( VikViewport *vvp ) for (i=1; i<10; i++) { vik_viewport_draw_line(vvp, vvp->scale_bg_gc, PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); - vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, + vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); } } pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); - pango_layout_set_font_description (pl, GTK_WIDGET(&vvp->drawing_area)->style->font_desc); + pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc); switch (dist_units) { case VIK_UNITS_DISTANCE_KILOMETRES: if (unit >= 1000) { - sprintf(s, "%d km", (int)unit/1000); + sprintf(s, "%d km", (int)unit/1000); } else { - sprintf(s, "%d m", (int)unit); + sprintf(s, "%d m", (int)unit); } break; case VIK_UNITS_DISTANCE_MILES: // Handle units in 0.1 miles if (unit < 10.0) { - sprintf(s, "%0.1f miles", unit/10.0); + sprintf(s, "%0.1f miles", unit/10.0); + } + else if ((int)unit == 10.0) { + sprintf(s, "1 mile"); + } + else { + sprintf(s, "%d miles", (int)(unit/10.0)); + } + break; + case VIK_UNITS_DISTANCE_NAUTICAL_MILES: + // Handle units in 0.1 NM + if (unit < 10.0) { + sprintf(s, "%0.1f NM", unit/10.0); } else if ((int)unit == 10.0) { - sprintf(s, "1 mile"); + sprintf(s, "1 NM"); } else { - sprintf(s, "%d miles", (int)(unit/10.0)); + sprintf(s, "%d NMs", (int)(unit/10.0)); } break; default: g_critical("Houston, we've had a problem. distance=%d", dist_units); } pango_layout_set_text(pl, s, -1); - vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, + vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, PAD + len + PAD, vvp->height - PAD - 10, pl); g_object_unref(pl); pl = NULL; @@ -543,7 +640,7 @@ void vik_viewport_draw_copyright ( VikViewport *vvp ) /* create pango layout */ pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); - pango_layout_set_font_description (pl, GTK_WIDGET(&vvp->drawing_area)->style->font_desc); + pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc); pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT ); /* Set the text */ @@ -552,7 +649,7 @@ void vik_viewport_draw_copyright ( VikViewport *vvp ) /* Use maximum of half the viewport width */ pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE ); pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect); - vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, + vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, vvp->width / 2, vvp->height - logical_rect.height, pl); /* Free memory */ @@ -588,7 +685,7 @@ void vik_viewport_draw_centermark ( VikViewport *vvp ) const int gap = 4; int center_x = vvp->width/2; int center_y = vvp->height/2; - GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc; + GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc; /* white back ground */ vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y); @@ -634,7 +731,7 @@ gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp ) void vik_viewport_sync ( VikViewport *vvp ) { g_return_if_fail ( vvp != NULL ); - gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height); + gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height); } void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off ) @@ -642,7 +739,7 @@ void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off ) gint x, y, wid, hei; g_return_if_fail ( vvp != NULL ); - gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height); + gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height); if (x_off >= 0) { x = 0; @@ -665,8 +762,11 @@ void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off ) void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp ) { g_return_if_fail ( vvp != NULL ); - if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) + if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) { vvp->xmpp = vvp->ympp = xympp; + // Since xmpp & ympp are the same it doesn't matter which one is used here + vvp->xmfactor = vvp->ymfactor = MERCATOR_FACTOR(vvp->xmpp); + } if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) viewport_utm_zone_check(vvp); @@ -681,6 +781,9 @@ void vik_viewport_zoom_in ( VikViewport *vvp ) vvp->xmpp /= 2; vvp->ympp /= 2; + vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp); + vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); + viewport_utm_zone_check(vvp); } } @@ -693,6 +796,9 @@ void vik_viewport_zoom_out ( VikViewport *vvp ) vvp->xmpp *= 2; vvp->ympp *= 2; + vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp); + vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); + viewport_utm_zone_check(vvp); } } @@ -718,6 +824,7 @@ void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp ) { if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) { vvp->xmpp = xmpp; + vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) viewport_utm_zone_check(vvp); } @@ -727,6 +834,7 @@ void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp ) { if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) { vvp->ympp = ympp; + vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) viewport_utm_zone_check(vvp); } @@ -757,23 +865,241 @@ static void viewport_utm_zone_check ( VikViewport *vvp ) } } -void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll ) +/** + * Free an individual center position in the history list + */ +static void free_center ( VikViewport *vvp, guint index ) +{ + VikCoord *coord = g_list_nth_data ( vvp->centers, index ); + if ( coord ) + g_free ( coord ); + GList *gl = g_list_nth ( vvp->centers, index ); + if ( gl ) + vvp->centers = g_list_delete_link ( vvp->centers, gl ); +} + +/** + * Free a set of center positions in the history list, + * from the indicated start index to the end of the list + */ +static void free_centers ( VikViewport *vvp, guint start ) +{ + // Have to work backward since we delete items referenced by the '_nth()' values, + // otherwise if processed forward - removing the lower nth index entries would change the subsequent indexing + for ( guint i = g_list_length(vvp->centers)-1; i > start; i-- ) + free_center ( vvp, i ); +} + +/** + * Store the current center position into the history list + * and emit a signal to notify clients the list has been updated + */ +static void update_centers ( VikViewport *vvp ) +{ + VikCoord *new_center = g_malloc(sizeof (VikCoord)); + *new_center = vvp->center; + + if ( vvp->centers_index ) { + + if ( vvp->centers_index == vvp->centers_max-1 ) { + // List is full, so drop the oldest value to make room for the new one + free_center ( vvp, 0 ); + vvp->centers_index--; + } + else { + // Reset the now unused section of the list + // Free from the index to the end + free_centers ( vvp, vvp->centers_index+1 ); + } + + } + + // Store new position + // NB ATM this can be the same location as the last one in the list + vvp->centers = g_list_append ( vvp->centers, new_center ); + + // Reset to the end (NB should be same as centers_index++) + vvp->centers_index = g_list_length ( vvp->centers ) - 1; + + // Inform interested subscribers that this change has occurred + g_signal_emit ( G_OBJECT(vvp), viewport_signals[VW_UPDATED_CENTER_SIGNAL], 0 ); +} + +/** + * Show the list of forward/backward positions + * ATM only for debug usage + */ +void vik_viewport_show_centers ( VikViewport *vvp, GtkWindow *parent ) +{ + GList* node = NULL; + GList* texts = NULL; + gint index = 0; + for (node = vvp->centers; node != NULL; node = g_list_next(node)) { + gchar *lat = NULL, *lon = NULL; + struct LatLon ll; + vik_coord_to_latlon (node->data, &ll); + a_coords_latlon_to_string ( &ll, &lat, &lon ); + gchar *extra = NULL; + if ( index == vvp->centers_index-1 ) + extra = g_strdup ( " [Back]" ); + else if ( index == vvp->centers_index+1 ) + extra = g_strdup ( " [Forward]" ); + else + extra = g_strdup ( "" ); + texts = g_list_prepend ( texts , g_strdup_printf ( "%s %s%s", lat, lon, extra ) ); + g_free ( lat ); + g_free ( lon ); + g_free ( extra ); + index++; + } + + // NB: No i18n as this is just for debug + // Using this function the dialog allows sorting of the list which isn't appropriate here + // but this doesn't matter much for debug purposes of showing stuff... + GList *ans = a_dialog_select_from_list(parent, + texts, + FALSE, + "Back/Forward Locations", + "Back/Forward Locations"); + for (node = ans; node != NULL; node = g_list_next(node)) + g_free(node->data); + g_list_free(ans); + for (node = texts; node != NULL; node = g_list_next(node)) + g_free(node->data); + g_list_free(texts); +} + +/** + * vik_viewport_go_back: + * + * Move back in the position history + * + * Returns: %TRUE one success + */ +gboolean vik_viewport_go_back ( VikViewport *vvp ) +{ + // see if the current position is different from the last saved center position within a certain radius + VikCoord *center = g_list_nth_data ( vvp->centers, vvp->centers_index ); + if ( center ) { + // Consider an exclusion size (should it zoom level dependent, rather than a fixed value?) + // When still near to the last saved position we'll jump over it to the one before + if ( vik_coord_diff ( center, &vvp->center ) > vvp->centers_radius ) { + + if ( vvp->centers_index == g_list_length(vvp->centers)-1 ) { + // Only when we haven't already moved back in the list + // Remember where this request came from + // (alternatively we could insert in the list on every back attempt) + update_centers ( vvp ); + } + + } + // 'Go back' if possible + // NB if we inserted a position above, then this will then move to the last saved position + // otherwise this will skip to the previous saved position, as it's probably somewhere else. + if ( vvp->centers_index > 0 ) + vvp->centers_index--; + } + else { + return FALSE; + } + + VikCoord *new_center = g_list_nth_data ( vvp->centers, vvp->centers_index ); + if ( new_center ) { + vik_viewport_set_center_coord ( vvp, new_center, FALSE ); + return TRUE; + } + return FALSE; +} + +/** + * vik_viewport_go_forward: + * + * Move forward in the position history + * + * Returns: %TRUE one success + */ +gboolean vik_viewport_go_forward ( VikViewport *vvp ) +{ + if ( vvp->centers_index == vvp->centers_max-1 ) + return FALSE; + + vvp->centers_index++; + VikCoord *new_center = g_list_nth_data ( vvp->centers, vvp->centers_index ); + if ( new_center ) { + vik_viewport_set_center_coord ( vvp, new_center, FALSE ); + return TRUE; + } + else + // Set to end of list + vvp->centers_index = g_list_length(vvp->centers) - 1; + + return FALSE; +} + +/** + * vik_viewport_back_available: + * + * Returns: %TRUE when a previous position in the history is available + */ +gboolean vik_viewport_back_available ( const VikViewport *vvp ) +{ + return ( vvp->centers_index > 0 ); +} + +/** + * vik_viewport_forward_available: + * + * Returns: %TRUE when a next position in the history is available + */ +gboolean vik_viewport_forward_available ( const VikViewport *vvp ) +{ + return ( vvp->centers_index < g_list_length(vvp->centers)-1 ); +} + +/** + * vik_viewport_set_center_latlon: + * @vvp: The viewport to reposition. + * @ll: The new center position in Lat/Lon format + * @save_position: Whether this new position should be saved into the history of positions + * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions) + */ +void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll, gboolean save_position ) { vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll ); + if ( save_position ) + update_centers ( vvp ); if ( vvp->coord_mode == VIK_COORD_UTM ) viewport_utm_zone_check ( vvp ); } -void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm ) +/** + * vik_viewport_set_center_utm: + * @vvp: The viewport to reposition. + * @utm: The new center position in UTM format + * @save_position: Whether this new position should be saved into the history of positions + * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions) + */ +void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm, gboolean save_position ) { vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm ); + if ( save_position ) + update_centers ( vvp ); if ( vvp->coord_mode == VIK_COORD_UTM ) viewport_utm_zone_check ( vvp ); } -void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord ) +/** + * vik_viewport_set_center_coord: + * @vvp: The viewport to reposition. + * @coord: The new center position in a VikCoord type + * @save_position: Whether this new position should be saved into the history of positions + * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions) + */ +void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord, gboolean save_position ) { vvp->center = *coord; + if ( save_position ) + update_centers ( vvp ); if ( vvp->coord_mode == VIK_COORD_UTM ) viewport_utm_zone_check ( vvp ); } @@ -836,7 +1162,7 @@ void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y ) } else { VikCoord tmp; vik_viewport_screen_to_coord ( vvp, x, y, &tmp ); - vik_viewport_set_center_coord ( vvp, &tmp ); + vik_viewport_set_center_coord ( vvp, &tmp, FALSE ); } } @@ -863,34 +1189,32 @@ void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *co utm->zone = vvp->center.utm_zone; utm->letter = vvp->center.utm_letter; - utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west; + utm->easting = ( ( x - ( vvp->width_2) ) * vvp->xmpp ) + vvp->center.east_west; zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 ); utm->zone += zone_delta; utm->easting -= zone_delta * vvp->utm_zone_width; - utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south; + utm->northing = ( ( ( vvp->height_2) - y ) * vvp->ympp ) + vvp->center.north_south; } else if ( vvp->coord_mode == VIK_COORD_LATLON ) { coord->mode = VIK_COORD_LATLON; if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) { - coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2)); - coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)); + coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2)); + coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)); } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) - calcxy_rev(&(coord->east_west), &(coord->north_south), x, y, vvp->center.east_west, vvp->center.north_south, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width/2, vvp->height/2); + calcxy_rev(&(coord->east_west), &(coord->north_south), x, y, vvp->center.east_west, vvp->center.north_south, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width_2, vvp->height_2); else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) { - /* FIXMERCATOR */ - coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2)); - coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) ); - -#if 0 ---> THIS IS JUNK HERE. - *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0; - - (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat); - DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll -#endif + /* This isn't called with a high frequently so less need to optimize */ + coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2)); + coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)) ); } } } +/* + * Since this function is used for every drawn trackpoint - it can get called alot + * Thus x & y position factors are calculated once on zoom changes, + * avoiding the need to do it here all the time. + * For good measure the half width and height values are also pre calculated too. + */ void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y ) { static VikCoord tmp; @@ -912,49 +1236,76 @@ void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int return; } - *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) - + *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width_2) - (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp; - *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp ); + *y = (vvp->height_2) - ( (utm->northing - center->northing) / vvp->ympp ); } else if ( vvp->coord_mode == VIK_COORD_LATLON ) { struct LatLon *center = (struct LatLon *) &(vvp->center); struct LatLon *ll = (struct LatLon *) coord; double xx,yy; if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) { - /* FIXMERCATOR: Optimize */ - *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0; - *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (center->lat - ll->lat))*256.0; + *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) ); + *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * (center->lat - ll->lat) ); } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) { - calcxy ( &xx, &yy, center->lon, center->lat, ll->lon, ll->lat, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width / 2, vvp->height / 2 ); + calcxy ( &xx, &yy, center->lon, center->lat, ll->lon, ll->lat, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width_2, vvp->height_2 ); *x = xx; *y = yy; } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) { - /* FIXMERCATOR: Optimize */ - *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0; - *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0; + *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) ); + *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * ( MERCLAT(center->lat) - MERCLAT(ll->lat) ) ); } } } -void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 ) +// Clip functions continually reduce the value by a factor until it is in the acceptable range +// whilst also scaling the other coordinate value. +static void clip_x ( gint *x1, gint *y1, gint *x2, gint *y2 ) { - if ( *x1 > 20000 || *x1 < -20000 ) { - gdouble shrinkfactor = ABS(20000.0 / *x1); - *x1 = *x2 + (shrinkfactor * (*x1-*x2)); - *y1 = *y2 + (shrinkfactor * (*y1-*y2)); - } else if ( *y1 > 20000 || *y1 < -20000 ) { - gdouble shrinkfactor = ABS(20000.0 / *x1); - *x1 = *x2 + (shrinkfactor * (*x1-*x2)); - *y1 = *y2 + (shrinkfactor * (*y1-*y2)); - } else if ( *x2 > 20000 || *x2 < -20000 ) { - gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2); - *x2 = *x1 + (shrinkfactor * (*x2-*x1)); - *y2 = *y1 + (shrinkfactor * (*y2-*y1)); - } else if ( *y2 > 20000 || *y2 < -20000 ) { - gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2); - *x2 = *x1 + (shrinkfactor * (*x2-*x1)); - *y2 = *y1 + (shrinkfactor * (*y2-*y1)); + while ( ABS(*x1) > 32768 ) { + *x1 = *x2 + (0.5 * (*x1-*x2)); + *y1 = *y2 + (0.5 * (*y1-*y2)); } } +static void clip_y ( gint *x1, gint *y1, gint *x2, gint *y2 ) +{ + while ( ABS(*y1) > 32767 ) { + *x1 = *x2 + (0.5 * (*x1-*x2)); + *y1 = *y2 + (0.5 * (*y1-*y2)); + } +} + +/** + * a_viewport_clip_line: + * @x1: screen coord + * @y1: screen coord + * @x2: screen coord + * @y2: screen coord + * + * Due to the seemingly undocumented behaviour of gdk_draw_line(), we need to restrict the range of values passed in. + * So despite it accepting gints, the effective range seems to be the actually the minimum C int range (2^16). + * This seems to be limitations coming from the X Window System. + * + * See http://www.rahul.net/kenton/40errs.html + * ERROR 7. Boundary conditions. + * "The X coordinate space is not infinite. + * Most drawing functions limit position, width, and height to 16 bit integers (sometimes signed, sometimes unsigned) of accuracy. + * Because most C compilers use 32 bit integers, Xlib will not complain if you exceed the 16 bit limit, but your results will usually not be what you expected. + * You should be especially careful of this if you are implementing higher level scalable graphics packages." + * + * This function should be called before calling gdk_draw_line(). + */ +void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 ) +{ + if ( *x1 > 32768 || *x1 < -32767 ) + clip_x ( x1, y1, x2, y2 ); + if ( *y1 > 32768 || *y1 < -32767 ) + clip_y ( x1, y1, x2, y2 ); + if ( *x2 > 32768 || *x2 < -32767 ) + clip_x ( x2, y2, x1, y1 ); + if ( *y2 > 32768 || *y2 < -32767 ) + clip_y ( x2, y2, x1, y1 ); +} + void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 ) { if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) || @@ -1020,9 +1371,8 @@ void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint dest_x, gint dest_y, gint w, gint h ) { gdk_draw_pixbuf ( vvp->scr_buffer, -// GTK_WIDGET(vvp)->style->black_gc, -NULL, - pixbuf, + NULL, + pixbuf, src_x, src_y, dest_x, dest_y, w, h, GDK_RGB_DITHER_NONE, 0, 0 ); }