+#include "dems.h"
+
+#include "vikviewport.h" /* ugh */
+#include "viktreeview.h" /* ugh */
+#include <gdk-pixbuf/gdk-pixdata.h>
+#include "viklayer.h" /* ugh */
+#include "vikaggregatelayer.h"
+#include "viklayerspanel.h" /* ugh */
+
+#define PROPWIN_PROFILE_WIDTH 600
+#define PROPWIN_PROFILE_HEIGHT 300
+
+#define PROPWIN_LABEL_FONT "Sans 7"
+
+typedef enum {
+ PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE,
+ PROPWIN_GRAPH_TYPE_SPEED_TIME,
+ PROPWIN_GRAPH_TYPE_DISTANCE_TIME,
+ PROPWIN_GRAPH_TYPE_END,
+} VikPropWinGraphType_t;
+
+/* (Hopefully!) Human friendly altitude grid sizes - note no fixed 'ratio' just numbers that look nice...*/
+static const gdouble chunksa[] = {2.0, 5.0, 10.0, 15.0, 20.0,
+ 25.0, 50.0, 75.0, 100.0,
+ 150.0, 200.0, 250.0, 375.0, 500.0,
+ 750.0, 1000.0, 2000.0, 5000.0, 10000.0, 100000.0};
+
+/* (Hopefully!) Human friendly grid sizes - note no fixed 'ratio' just numbers that look nice...*/
+/* As need to cover walking speeds - have many low numbers (but also may go up to airplane speeds!) */
+static const gdouble chunkss[] = {1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
+ 15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
+ 100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
+ 750.0, 1000.0, 10000.0};
+
+/* (Hopefully!) Human friendly distance grid sizes - note no fixed 'ratio' just numbers that look nice...*/
+static const gdouble chunksd[] = {0.1, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
+ 15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
+ 100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
+ 750.0, 1000.0, 10000.0};
+
+typedef struct _propsaved {
+ gboolean saved;
+ GdkImage *img;
+} PropSaved;
+
+typedef struct _propwidgets {
+ gboolean configure_dialog;
+ VikTrwLayer *vtl;
+ VikTrack *tr;
+ gchar *track_name;
+ gint profile_width;
+ gint profile_height;
+ gint profile_width_old;
+ gint profile_height_old;
+ gint profile_width_offset;
+ gint profile_height_offset;
+ GtkWidget *dialog;
+ GtkWidget *w_comment;
+ GtkWidget *w_track_length;
+ GtkWidget *w_tp_count;
+ GtkWidget *w_segment_count;
+ GtkWidget *w_duptp_count;
+ GtkWidget *w_max_speed;
+ GtkWidget *w_avg_speed;
+ GtkWidget *w_avg_dist;
+ GtkWidget *w_elev_range;
+ GtkWidget *w_elev_gain;
+ GtkWidget *w_time_start;
+ GtkWidget *w_time_end;
+ GtkWidget *w_time_dur;
+ GtkWidget *w_cur_dist; /*< Current distance */
+ GtkWidget *w_cur_elevation;
+ GtkWidget *w_cur_time; /*< Current time */
+ GtkWidget *w_cur_speed;
+ GtkWidget *w_cur_dist_dist; /*< Current distance on distance graph */
+ GtkWidget *w_cur_dist_time; /*< Current time on distance graph */
+ GtkWidget *w_show_dem;
+ GtkWidget *w_show_alt_gps_speed;
+ GtkWidget *w_show_gps_speed;
+ GtkWidget *w_show_dist_speed;
+ gdouble track_length;
+ PropSaved elev_graph_saved_img;
+ PropSaved speed_graph_saved_img;
+ PropSaved dist_graph_saved_img;
+ GtkWidget *elev_box;
+ GtkWidget *speed_box;
+ GtkWidget *dist_box;
+ gdouble *altitudes;
+ gdouble min_altitude;
+ gdouble max_altitude;
+ gdouble draw_min_altitude;
+ gint cia; // Chunk size Index into Altitudes
+ gdouble *speeds;
+ gdouble min_speed;
+ gdouble max_speed;
+ gdouble draw_min_speed;
+ gint cis; // Chunk size Index into Speeds
+ gdouble *distances;
+ gint cid; // Chunk size Index into Distance
+ VikTrackpoint *marker_tp;
+ gboolean is_marker_drawn;
+ VikTrackpoint *blob_tp;
+ gboolean is_blob_drawn;
+} PropWidgets;
+
+static PropWidgets *prop_widgets_new()
+{
+ PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
+
+ return widgets;
+}
+
+static void prop_widgets_free(PropWidgets *widgets)
+{
+ if (widgets->elev_graph_saved_img.img)
+ g_object_unref(widgets->elev_graph_saved_img.img);
+ if (widgets->speed_graph_saved_img.img)
+ g_object_unref(widgets->speed_graph_saved_img.img);
+ if (widgets->dist_graph_saved_img.img)
+ g_object_unref(widgets->dist_graph_saved_img.img);
+ if (widgets->altitudes)
+ g_free(widgets->altitudes);
+ if (widgets->speeds)
+ g_free(widgets->speeds);
+ if (widgets->distances)
+ g_free(widgets->distances);
+ g_free(widgets);
+}
+
+static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gboolean NO_ALT_TEST, gint PROFILE_WIDTH)
+{
+ *max = -1000;
+ *min = 20000;
+ guint i;
+ for ( i=0; i < PROFILE_WIDTH; i++ ) {
+ if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) {
+ if ( array[i] > *max )
+ *max = array[i];
+ if ( array[i] < *min )
+ *min = array[i];
+ }
+ }
+}
+
+#define MARGIN 70
+#define LINES 5
+/**
+ * Returns via pointers:
+ * the new minimum value to be used for the altitude graph
+ * the index in to the altitudes chunk sizes array (ci = Chunk Index)
+ */
+static void get_new_min_and_chunk_index_altitude (gdouble mina, gdouble maxa, gdouble *new_min, gint *ci)
+{
+ /* Get unitized chunk */
+ /* Find suitable chunk index */
+ *ci = 0;
+ gdouble diffa_chunk = (maxa - mina)/LINES;
+
+ /* Loop through to find best match */
+ while (diffa_chunk > chunksa[*ci]) {
+ (*ci)++;
+ /* Last Resort Check */
+ if ( *ci == sizeof(chunksa)/sizeof(chunksa[0]) )
+ break;
+ }
+
+ /* Ensure adjusted minimum .. maximum covers mina->maxa */
+
+ // Now work out adjusted minimum point to the nearest lowest chunk divisor value
+ // When negative ensure logic uses lowest value
+ if ( mina < 0 )
+ *new_min = (gdouble) ( ( (gint)(mina - chunksa[*ci]) / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
+ else
+ *new_min = (gdouble) ( ( (gint)mina / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
+
+ // Range not big enough - as new minimum has lowered
+ if ((*new_min + (chunksa[*ci] * LINES) < maxa)) {
+ // Next chunk should cover it
+ if ( *ci < sizeof(chunksa)/sizeof(chunksa[0]) ) {
+ (*ci)++;
+ }
+ }
+}
+
+/**
+ * Returns via pointers:
+ * the new minimum value to be used for the appropriate graph
+ * the index in to that array (ci = Chunk Index)
+ */
+static void get_new_min_and_chunk_index (gdouble mins, gdouble maxs, const gdouble *chunks, size_t chunky, gdouble *new_min, gint *ci)
+{
+ *ci = 0;
+ gdouble diff_chunk = (maxs - mins)/LINES;
+
+ /* Loop through to find best match */
+ while (diff_chunk > chunks[*ci]) {
+ (*ci)++;
+ /* Last Resort Check */
+ if ( *ci == chunky )
+ break;
+ }
+ *new_min = (gdouble) ( (gint)((mins / chunks[*ci] ) * chunks[*ci]) );
+
+ // Speeds are never negative so don't need to worry about a negative new minimum
+}
+
+static VikTrackpoint *set_center_at_graph_position(gdouble event_x,
+ gint img_width,
+ VikTrwLayer *vtl,
+ VikLayersPanel *vlp,
+ VikViewport *vvp,
+ VikTrack *tr,
+ gboolean time_base,
+ gint PROFILE_WIDTH)
+{
+ VikTrackpoint *trackpoint;
+ gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
+ if (x < 0)
+ x = 0;
+ if (x > PROFILE_WIDTH)
+ x = PROFILE_WIDTH;
+
+ if (time_base)
+ trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
+ else
+ trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
+
+ if ( trackpoint ) {
+ VikCoord coord = trackpoint->coord;
+ if ( vlp ) {
+ vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
+ vik_layers_panel_emit_update ( vlp );
+ }
+ else {
+ /* since vlp not set, vvp should be valid instead! */
+ if ( vvp )
+ vik_viewport_set_center_coord ( vvp, &coord );
+ vik_layer_emit_update ( VIK_LAYER(vtl) );
+ }
+ }
+ return trackpoint;
+}
+
+/**
+ * Returns whether the marker was drawn or not and whether the blob was drawn or not
+ */
+static void save_image_and_draw_graph_marks (GtkWidget *image,
+ gdouble marker_x,
+ GdkGC *gc,
+ gint blob_x,
+ gint blob_y,
+ PropSaved *saved_img,
+ gint PROFILE_WIDTH,
+ gint PROFILE_HEIGHT,
+ gboolean *marker_drawn,
+ gboolean *blob_drawn)
+{
+ GdkPixmap *pix = NULL;
+ /* the pixmap = margin + graph area */
+ gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL);
+
+ /* Restore previously saved image */
+ if (saved_img->saved) {
+ gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
+ saved_img->saved = FALSE;
+ }
+
+ // ATM always save whole image - as anywhere could have changed
+ if (saved_img->img)
+ gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
+ else
+ saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
+ saved_img->saved = TRUE;
+
+ if ((marker_x >= MARGIN) && (marker_x < (PROFILE_WIDTH + MARGIN))) {
+ gdk_draw_line (GDK_DRAWABLE(pix), gc, marker_x, 0, marker_x, image->allocation.height);
+ *marker_drawn = TRUE;
+ }
+ else
+ *marker_drawn = FALSE;
+
+ // Draw a square blob to indicate where we are on track for this graph
+ if ( (blob_x >= MARGIN) && (blob_x < (PROFILE_WIDTH + MARGIN)) && (blob_y < PROFILE_HEIGHT) ) {
+ gdk_draw_rectangle (GDK_DRAWABLE(pix), gc, TRUE, blob_x-3, blob_y-3, 6, 6);
+ *blob_drawn = TRUE;
+ }
+ else
+ *blob_drawn = FALSE;
+
+ // Anywhere on image could have changed
+ if (*marker_drawn || *blob_drawn)
+ gtk_widget_queue_draw(image);
+}
+
+/**
+ * Return the percentage of how far a trackpoint is a long a track via the time method
+ */
+static gdouble tp_percentage_by_time ( VikTrack *tr, VikTrackpoint *trackpoint )
+{
+ gdouble pc = NAN;
+ if (trackpoint == NULL)
+ return pc;
+ time_t t_start, t_end, t_total;
+ t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
+ t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
+ t_total = t_end - t_start;
+ pc = (gdouble)(trackpoint->timestamp - t_start)/t_total;
+ return pc;
+}
+
+/**
+ * Return the percentage of how far a trackpoint is a long a track via the distance method
+ */
+static gdouble tp_percentage_by_distance ( VikTrack *tr, VikTrackpoint *trackpoint, gdouble track_length )
+{
+ gdouble pc = NAN;
+ if (trackpoint == NULL)
+ return pc;
+ gdouble dist = 0.0;
+ GList *iter;
+ for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) {
+ dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord),
+ &(VIK_TRACKPOINT(iter->prev->data)->coord));
+ /* Assuming trackpoint is not a copy */
+ if (trackpoint == VIK_TRACKPOINT(iter->data))
+ break;
+ }
+ if (iter != NULL)
+ pc = dist/track_length;
+ return pc;
+}
+
+static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, VikPropWinGraphType_t graph_type )
+{
+ VikTrack *tr = pass_along[0];
+ VikLayersPanel *vlp = pass_along[1];
+ VikViewport *vvp = pass_along[2];
+ PropWidgets *widgets = pass_along[3];
+
+ gboolean is_time_graph = graph_type != PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE;
+
+ VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, widgets->vtl, vlp, vvp, tr, is_time_graph, widgets->profile_width);
+ // Unable to get the point so give up
+ if ( trackpoint == NULL ) {
+ gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
+ return;
+ }
+
+ widgets->marker_tp = trackpoint;
+
+ GList *child;
+ GtkWidget *image;
+ GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box));
+ GtkWidget *graph_box;
+ PropSaved *graph_saved_img;
+ gdouble pc = NAN;
+
+ // Attempt to redraw marker on all graph types
+ gint graphite;
+ for ( graphite = PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE;
+ graphite < PROPWIN_GRAPH_TYPE_END;
+ graphite++ ) {
+
+ // Switch commonal variables to particular graph type
+ switch (graphite) {
+ default:
+ case PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE:
+ graph_box = widgets->elev_box;
+ graph_saved_img = &widgets->elev_graph_saved_img;
+ is_time_graph = FALSE;
+ break;
+ case PROPWIN_GRAPH_TYPE_SPEED_TIME:
+ graph_box = widgets->speed_box;
+ graph_saved_img = &widgets->speed_graph_saved_img;
+ is_time_graph = TRUE;
+ break;
+ case PROPWIN_GRAPH_TYPE_DISTANCE_TIME:
+ graph_box = widgets->dist_box;
+ graph_saved_img = &widgets->dist_graph_saved_img;
+ is_time_graph = TRUE;
+ break;
+ }
+
+ // Commonal method of redrawing marker
+ if ( graph_box ) {
+
+ child = gtk_container_get_children(GTK_CONTAINER(graph_box));
+ image = GTK_WIDGET(child->data);
+
+ if (is_time_graph)
+ pc = tp_percentage_by_time ( tr, trackpoint );
+ else
+ pc = tp_percentage_by_distance ( tr, trackpoint, widgets->track_length );
+
+ if (!isnan(pc)) {
+ gdouble marker_x = (pc * widgets->profile_width) + MARGIN;
+ save_image_and_draw_graph_marks(image,
+ marker_x,
+ window->style->black_gc,
+ -1, // Don't draw blob on clicks
+ 0,
+ graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+ }
+ g_list_free(child);
+ }
+ }
+
+ gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, widgets->is_marker_drawn);
+}
+
+static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
+{
+ track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE);
+ return TRUE; /* don't call other (further) callbacks */
+}
+
+static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
+{
+ track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_SPEED_TIME);
+ return TRUE; /* don't call other (further) callbacks */
+}
+
+static gboolean track_dt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
+{
+ track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_DISTANCE_TIME);
+ return TRUE; /* don't call other (further) callbacks */
+}
+
+/**
+ * Calculate y position for blob on elevation graph
+ */
+static gint blobby_altitude ( gdouble x_blob, PropWidgets *widgets )
+{
+ gint ix = (gint)x_blob;
+ // Ensure ix is inbounds
+ if (ix == widgets->profile_width)
+ ix--;
+
+ gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->altitudes[ix]-widgets->draw_min_altitude)/(chunksa[widgets->cia]*LINES);
+
+ return y_blob;
+}
+
+/**
+ * Calculate y position for blob on speed graph
+ */
+static gint blobby_speed ( gdouble x_blob, PropWidgets *widgets )
+{
+ gint ix = (gint)x_blob;
+ // Ensure ix is inbounds
+ if (ix == widgets->profile_width)
+ ix--;
+
+ gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->speeds[ix]-widgets->draw_min_speed)/(chunkss[widgets->cis]*LINES);
+
+ return y_blob;
+}
+
+/**
+ * Calculate y position for blob on distance graph
+ */
+static gint blobby_distance ( gdouble x_blob, PropWidgets *widgets )
+{
+ gint ix = (gint)x_blob;
+ // Ensure ix is inbounds
+ if (ix == widgets->profile_width)
+ ix--;
+
+ gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->distances[ix])/(chunksd[widgets->cid]*LINES);
+ //NB min distance is always 0, so no need to subtract that from this ______/
+
+ return y_blob;
+}
+
+void track_profile_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
+{
+ VikTrack *tr = pass_along[0];
+ PropWidgets *widgets = pass_along[3];
+ int mouse_x, mouse_y;
+ GdkModifierType state;
+
+ if (event->is_hint)
+ gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
+ else
+ mouse_x = event->x;
+
+ gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
+ if (x < 0)
+ x = 0;
+ if (x > widgets->profile_width)
+ x = widgets->profile_width;
+
+ gdouble meters_from_start;
+ VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
+ if (trackpoint && widgets->w_cur_dist) {
+ static gchar tmp_buf[20];
+ vik_units_distance_t dist_units = a_vik_get_units_distance ();
+ switch (dist_units) {
+ case VIK_UNITS_DISTANCE_KILOMETRES:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
+ break;
+ case VIK_UNITS_DISTANCE_MILES:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
+ break;
+ default:
+ g_critical("Houston, we've had a problem. distance=%d", dist_units);
+ }
+ gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf);
+ }
+
+ // Show track elevation for this position - to the nearest whole number
+ if (trackpoint && widgets->w_cur_elevation) {
+ static gchar tmp_buf[20];
+ if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude));
+ else
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude);
+ gtk_label_set_text(GTK_LABEL(widgets->w_cur_elevation), tmp_buf);
+ }
+
+ widgets->blob_tp = trackpoint;
+
+ if ( widgets->altitudes == NULL )
+ return;
+
+ GtkWidget *window = gtk_widget_get_toplevel (event_box);
+ GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
+ GtkWidget *image = GTK_WIDGET(child->data);
+
+ gint y_blob = blobby_altitude (x, widgets);
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (widgets->is_marker_drawn) {
+ gdouble pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length );
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN;
+ }
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ window->style->black_gc,
+ MARGIN+x,
+ y_blob,
+ &widgets->elev_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+
+ g_list_free(child);
+}
+
+void track_vt_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
+{
+ VikTrack *tr = pass_along[0];
+ PropWidgets *widgets = pass_along[3];
+ int mouse_x, mouse_y;
+ GdkModifierType state;
+
+ if (event->is_hint)
+ gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
+ else
+ mouse_x = event->x;
+
+ gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
+ if (x < 0)
+ x = 0;
+ if (x > widgets->profile_width)
+ x = widgets->profile_width;
+
+ time_t seconds_from_start;
+ VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
+ if (trackpoint && widgets->w_cur_time) {
+ static gchar tmp_buf[20];
+ guint h, m, s;
+ h = seconds_from_start/3600;
+ m = (seconds_from_start - h*3600)/60;
+ s = seconds_from_start - (3600*h) - (60*m);
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
+
+ gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf);
+ }
+
+ gint ix = (gint)x;
+ // Ensure ix is inbounds
+ if (ix == widgets->profile_width)
+ ix--;
+
+ // Show track speed for this position
+ if (trackpoint && widgets->w_cur_speed) {
+ static gchar tmp_buf[20];
+ // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown
+ // No conversions needed as already in appropriate units
+ vik_units_speed_t speed_units = a_vik_get_units_speed ();
+ switch (speed_units) {
+ case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), widgets->speeds[ix]);
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds[ix]);
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds[ix]);
+ break;
+ default:
+ // VIK_UNITS_SPEED_METRES_PER_SECOND:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds[ix]);
+ break;
+ }
+ gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed), tmp_buf);
+ }
+
+ widgets->blob_tp = trackpoint;
+
+ if ( widgets->speeds == NULL )
+ return;
+
+ GtkWidget *window = gtk_widget_get_toplevel (event_box);
+ GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
+ GtkWidget *image = GTK_WIDGET(child->data);
+
+ gint y_blob = blobby_speed (x, widgets);
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (widgets->is_marker_drawn) {
+ gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN;
+ }
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ window->style->black_gc,
+ MARGIN+x,
+ y_blob,
+ &widgets->speed_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+
+ g_list_free(child);
+}
+
+/**
+ * Update labels and blob marker on mouse moves in the distance/time graph
+ */
+void track_dt_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
+{
+ VikTrack *tr = pass_along[0];
+ PropWidgets *widgets = pass_along[3];
+ int mouse_x, mouse_y;
+ GdkModifierType state;
+
+ if (event->is_hint)
+ gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
+ else
+ mouse_x = event->x;
+
+ gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
+ if (x < 0)
+ x = 0;
+ if (x > widgets->profile_width)
+ x = widgets->profile_width;
+
+ time_t seconds_from_start;
+ VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
+ if (trackpoint && widgets->w_cur_dist_time) {
+ static gchar tmp_buf[20];
+ guint h, m, s;
+ h = seconds_from_start/3600;
+ m = (seconds_from_start - h*3600)/60;
+ s = seconds_from_start - (3600*h) - (60*m);
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
+
+ gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_time), tmp_buf);
+ }
+
+ gint ix = (gint)x;
+ // Ensure ix is inbounds
+ if (ix == widgets->profile_width)
+ ix--;
+
+ if (trackpoint && widgets->w_cur_dist_dist) {
+ static gchar tmp_buf[20];
+ if ( a_vik_get_units_distance () == VIK_UNITS_DISTANCE_MILES )
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", widgets->distances[ix]);
+ else
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", widgets->distances[ix]);
+ gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_dist), tmp_buf);
+ }
+
+ widgets->blob_tp = trackpoint;
+
+ if ( widgets->distances == NULL )
+ return;
+
+ GtkWidget *window = gtk_widget_get_toplevel (event_box);
+ GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
+ GtkWidget *image = GTK_WIDGET(child->data);
+
+ gint y_blob = blobby_distance (x, widgets);
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (widgets->is_marker_drawn) {
+ gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN;
+ }
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ window->style->black_gc,
+ MARGIN+x,
+ y_blob,
+ &widgets->dist_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+
+ g_list_free(child);
+}
+
+/**
+ * Draws DEM points and a respresentative speed on the supplied pixmap
+ * (which is the elevations graph)
+ */
+static void draw_dem_alt_speed_dist(VikTrack *tr,
+ GdkDrawable *pix,
+ GdkGC *alt_gc,
+ GdkGC *speed_gc,
+ gdouble alt_offset,
+ gdouble alt_diff,
+ gdouble max_speed_in,
+ gint cia,
+ gint width,
+ gint height,
+ gint margin,
+ gboolean do_dem,
+ gboolean do_speed)
+{
+ GList *iter;
+ gdouble max_speed = 0;
+ gdouble total_length = vik_track_get_length_including_gaps(tr);
+
+ // Calculate the max speed factor
+ if (do_speed)
+ max_speed = max_speed_in * 110 / 100;
+
+ gdouble dist = 0;
+ for (iter = tr->trackpoints->next; iter; iter = iter->next) {
+ int x;
+ dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
+ &(VIK_TRACKPOINT(iter->prev->data)->coord) );
+ x = (width * dist)/total_length + margin;
+ if (do_dem) {
+ gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST);
+ elev -= alt_offset;
+ if ( elev != VIK_DEM_INVALID_ELEVATION ) {
+ // Convert into height units
+ if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
+ elev = VIK_METERS_TO_FEET(elev);
+ // No conversion needed if already in metres
+
+ // consider chunk size
+ int y_alt = height - ((height * (elev-alt_offset))/(chunksa[cia]*LINES) );
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4);
+ }
+ }
+ if (do_speed) {
+ // This is just a speed indicator - no actual values can be inferred by user
+ if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
+ int y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
+ }
+ }
+ }
+}
+
+/**
+ * Draw just the height profile image
+ */
+static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
+{
+ GtkWidget *window;
+ GdkPixmap *pix;
+ gdouble mina, maxa;
+ guint i;
+
+ GdkGC *no_alt_info;
+ GdkGC *dem_alt_gc;
+ GdkGC *gps_speed_gc;
+
+ GdkColor color;
+
+ // Free previous allocation
+ if ( widgets->altitudes )
+ g_free ( widgets->altitudes );
+
+ widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
+
+ if ( widgets->altitudes == NULL )
+ return;
+
+ // Convert into appropriate units
+ vik_units_height_t height_units = a_vik_get_units_height ();
+ if ( height_units == VIK_UNITS_HEIGHT_FEET ) {
+ // Convert altitudes into feet units
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->altitudes[i] = VIK_METERS_TO_FEET(widgets->altitudes[i]);
+ }
+ }
+ // Otherwise leave in metres
+
+ minmax_array(widgets->altitudes, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
+
+ get_new_min_and_chunk_index_altitude (widgets->min_altitude, widgets->max_altitude, &widgets->draw_min_altitude, &widgets->cia);
+
+ // Assign locally
+ mina = widgets->draw_min_altitude;
+ maxa = widgets->max_altitude;
+
+ window = gtk_widget_get_toplevel (widgets->elev_box);
+
+ pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ no_alt_info = gdk_gc_new ( window->window );
+ gdk_color_parse ( "yellow", &color );
+ gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
+
+ dem_alt_gc = gdk_gc_new ( window->window );
+ gdk_color_parse ( "green", &color );
+ gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
+
+ gps_speed_gc = gdk_gc_new ( window->window );
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
+
+ /* clear the image */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
+ TRUE, 0, 0, MARGIN, widgets->profile_height);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
+ TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ PangoFontDescription *pfd;
+ PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
+ gchar s[32];
+ int w, h;
+
+ pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
+ pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
+ pango_layout_set_font_description (pl, pfd);
+ pango_font_description_free (pfd);
+ switch (height_units) {
+ case VIK_UNITS_HEIGHT_METRES:
+ sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
+ break;
+ case VIK_UNITS_HEIGHT_FEET:
+ // NB values already converted into feet
+ sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
+ break;
+ default:
+ sprintf(s, "--");
+ g_critical("Houston, we've had a problem. height=%d", height_units);
+ }
+ pango_layout_set_text(pl, s, -1);
+ pango_layout_get_pixel_size (pl, &w, &h);
+ gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
+ CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
+
+ gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
+ MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
+ g_object_unref ( G_OBJECT ( pl ) );
+ pl = NULL;
+ }
+
+ /* draw elevations */
+ for ( i = 0; i < widgets->profile_width; i++ )
+ if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE )
+ gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
+ i + MARGIN, 0, i + MARGIN, widgets->profile_height );
+ else
+ gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
+ i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->altitudes[i]-mina)/(chunksa[widgets->cia]*LINES) );
+
+ // Ensure somekind of max speed when not set
+ if ( widgets->max_speed < 0.01 )
+ widgets->max_speed = vik_track_get_max_speed(tr);
+
+ draw_dem_alt_speed_dist(tr,
+ GDK_DRAWABLE(pix),
+ dem_alt_gc,
+ gps_speed_gc,
+ mina,
+ maxa - mina,
+ widgets->max_speed,
+ widgets->cia,
+ widgets->profile_width,
+ widgets->profile_height,
+ MARGIN,
+ gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_dem)),
+ gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)));
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+ g_object_unref ( G_OBJECT(no_alt_info) );
+ g_object_unref ( G_OBJECT(dem_alt_gc) );
+ g_object_unref ( G_OBJECT(gps_speed_gc) );
+
+}
+
+/**
+ * Draw just the speed (velocity)/time image
+ */
+static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
+{
+ GtkWidget *window;
+ GdkPixmap *pix;
+ gdouble mins, maxs;
+ guint i;
+
+ // Free previous allocation
+ if ( widgets->speeds )
+ g_free ( widgets->speeds );
+
+ widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
+ if ( widgets->speeds == NULL )
+ return;
+
+ // Convert into appropriate units
+ vik_units_speed_t speed_units = a_vik_get_units_speed ();
+ switch (speed_units) {
+ case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->speeds[i] = VIK_MPS_TO_KPH(widgets->speeds[i]);
+ }
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->speeds[i] = VIK_MPS_TO_MPH(widgets->speeds[i]);
+ }
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->speeds[i] = VIK_MPS_TO_KNOTS(widgets->speeds[i]);
+ }
+ break;
+ default:
+ // VIK_UNITS_SPEED_METRES_PER_SECOND:
+ // No need to convert as already in m/s
+ break;
+ }
+
+ GdkGC *gps_speed_gc;
+ GdkColor color;
+
+ window = gtk_widget_get_toplevel (widgets->speed_box);
+
+ pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ gps_speed_gc = gdk_gc_new ( window->window );
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
+
+ minmax_array(widgets->speeds, &widgets->min_speed, &widgets->max_speed, FALSE, widgets->profile_width);
+ if (widgets->min_speed < 0.0)
+ widgets->min_speed = 0; /* splines sometimes give negative speeds */
+
+ /* Find suitable chunk index */
+ get_new_min_and_chunk_index (widgets->min_speed, widgets->max_speed, chunkss, sizeof(chunkss)/sizeof(chunkss[0]), &widgets->draw_min_speed, &widgets->cis);
+
+ // Assign locally
+ mins = widgets->draw_min_speed;
+ maxs = widgets->max_speed;
+
+ /* clear the image */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
+ TRUE, 0, 0, MARGIN, widgets->profile_height);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
+ TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ PangoFontDescription *pfd;
+ PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
+ gchar s[32];
+ int w, h;
+
+ pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
+ pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
+ pango_layout_set_font_description (pl, pfd);
+ pango_font_description_free (pfd);
+ // NB: No need to convert here anymore as numbers are in the appropriate units
+ switch (speed_units) {
+ case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
+ sprintf(s, "%8dkm/h", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
+ break;
+ case VIK_UNITS_SPEED_METRES_PER_SECOND:
+ sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
+ break;
+ default:
+ sprintf(s, "--");
+ g_critical("Houston, we've had a problem. speed=%d", speed_units);
+ }
+
+ pango_layout_set_text(pl, s, -1);
+ pango_layout_get_pixel_size (pl, &w, &h);
+ gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
+ CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
+
+ gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
+ MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
+ g_object_unref ( G_OBJECT ( pl ) );
+ pl = NULL;
+ }
+
+
+ /* draw speeds */
+ for ( i = 0; i < widgets->profile_width; i++ )
+ gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
+ i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(chunkss[widgets->cis]*LINES) );