+/**
+ * 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 )