+ if (event->is_hint)
+ gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
+ else
+ mouse_x = event->x;
+
+ GtkAllocation allocation;
+ gtk_widget_get_allocation ( event_box, &allocation );
+
+ gdouble x = mouse_x - allocation.width / 2 + widgets->profile_width / 2 - MARGIN_X / 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 ( widgets->tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
+ if (trackpoint && widgets->w_cur_dist_time) {
+ time_label_update ( widgets->w_cur_dist_time, seconds_from_start );
+ }
+
+ if (trackpoint && widgets->w_cur_dist_time_real) {
+ real_time_label_update ( widgets, widgets->w_cur_dist_time_real, trackpoint );
+ }
+
+ 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];
+ switch ( a_vik_get_units_distance () ) {
+ case VIK_UNITS_DISTANCE_MILES:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", widgets->distances[ix]);
+ break;
+ case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f NM", widgets->distances[ix]);
+ break;
+ default:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", widgets->distances[ix]);
+ break;
+ }
+ 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 ( widgets->tr, widgets->marker_tp );
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ MARGIN_X+x,
+ MARGIN_Y+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);
+}
+
+/**
+ * Update labels and blob marker on mouse moves in the elevation/time graph
+ */
+void track_et_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
+{
+ 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;
+
+ GtkAllocation allocation;
+ gtk_widget_get_allocation ( event_box, &allocation );
+
+ gdouble x = mouse_x - allocation.width / 2 + widgets->profile_width / 2 - MARGIN_X / 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 ( widgets->tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
+ if (trackpoint && widgets->w_cur_elev_time) {
+ time_label_update ( widgets->w_cur_elev_time, seconds_from_start );
+ }
+
+ if (trackpoint && widgets->w_cur_elev_time_real) {
+ real_time_label_update ( widgets, widgets->w_cur_elev_time_real, trackpoint );
+ }
+
+ gint ix = (gint)x;
+ // Ensure ix is inbounds
+ if (ix == widgets->profile_width)
+ ix--;
+
+ if (trackpoint && widgets->w_cur_elev_elev) {
+ 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_elev_elev), tmp_buf);
+ }
+
+ widgets->blob_tp = trackpoint;
+
+ if ( widgets->ats == 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_time (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 ( widgets->tr, widgets->marker_tp );
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ MARGIN_X+x,
+ MARGIN_Y+y_blob,
+ &widgets->elev_time_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+
+ g_list_free(child);
+}
+
+void track_sd_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
+{
+ 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;
+
+ GtkAllocation allocation;
+ gtk_widget_get_allocation ( event_box, &allocation );
+
+ gdouble x = mouse_x - allocation.width / 2 + widgets->profile_width / 2 - MARGIN_X / 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 ( widgets->tr, (gdouble) x / widgets->profile_width, &meters_from_start );
+ if (trackpoint && widgets->w_cur_speed_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;
+ case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f NM", VIK_METERS_TO_NAUTICAL_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_speed_dist), tmp_buf);
+ }
+
+ gint ix = (gint)x;
+ // Ensure ix is inbounds
+ if (ix == widgets->profile_width)
+ ix--;
+
+ if ( widgets->speeds_dist == NULL )
+ return;
+
+ // Show track speed for this position
+ if (widgets->w_cur_speed_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_dist[ix]);
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds_dist[ix]);
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds_dist[ix]);
+ break;
+ default:
+ // VIK_UNITS_SPEED_METRES_PER_SECOND:
+ g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds_dist[ix]);
+ break;
+ }
+ gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed_speed), tmp_buf);
+ }
+
+ widgets->blob_tp = trackpoint;
+
+ 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_dist (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 ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ MARGIN_X+x,
+ MARGIN_Y+y_blob,
+ &widgets->speed_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;
+ gint h2 = height + MARGIN_Y; // Adjust height for x axis labelling offset
+ gint achunk = chunksa[cia]*LINES;
+
+ 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 = h2 - ((height * elev)/achunk );
+ 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 = h2 - (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_grid_y:
+ *
+ * A common way to draw the grid with y axis labels
+ *
+ */
+static void draw_grid_y ( GtkWidget *window, GtkWidget *image, PropWidgets *widgets, GdkPixmap *pix, gchar *ss, gint i )
+{
+ PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
+
+ pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
+ pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
+
+ gchar *label_markup = g_strdup_printf ( "<span size=\"small\">%s</span>", ss );
+ pango_layout_set_markup ( pl, label_markup, -1 );
+ g_free ( label_markup );
+
+ int w, h;
+ pango_layout_get_pixel_size ( pl, &w, &h );
+
+ gdk_draw_layout ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0],
+ MARGIN_X-w-3,
+ CLAMP((int)i*widgets->profile_height/LINES - h/2 + MARGIN_Y, 0, widgets->profile_height-h+MARGIN_Y),
+ pl );
+ g_object_unref ( G_OBJECT ( pl ) );
+
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
+ MARGIN_X, MARGIN_Y + widgets->profile_height/LINES * i,
+ MARGIN_X + widgets->profile_width, MARGIN_Y + widgets->profile_height/LINES * i );
+}
+
+/**
+ * draw_grid_x_time:
+ *
+ * A common way to draw the grid with x axis labels for time graphs
+ *
+ */
+static void draw_grid_x_time ( GtkWidget *window, GtkWidget *image, PropWidgets *widgets, GdkPixmap *pix, guint ii, guint tt, guint xx )
+{
+ gchar *label_markup = NULL;
+ switch (ii) {
+ case 0:
+ case 1:
+ case 2:
+ case 3:
+ // Minutes
+ label_markup = g_strdup_printf ( "<span size=\"small\">%d %s</span>", tt/60, _("mins") );
+ break;
+ case 4:
+ case 5:
+ case 6:
+ case 7:
+ // Hours
+ label_markup = g_strdup_printf ( "<span size=\"small\">%.1f %s</span>", (gdouble)tt/(60*60), _("h") );
+ break;
+ case 8:
+ case 9:
+ case 10:
+ // Days
+ label_markup = g_strdup_printf ( "<span size=\"small\">%.1f %s</span>", (gdouble)tt/(60*60*24), _("d") );
+ break;
+ case 11:
+ case 12:
+ // Weeks
+ label_markup = g_strdup_printf ( "<span size=\"small\">%.1f %s</span>", (gdouble)tt/(60*60*24*7), _("w") );
+ break;
+ case 13:
+ // 'Months'
+ label_markup = g_strdup_printf ( "<span size=\"small\">%.1f %s</span>", (gdouble)tt/(60*60*24*28), _("M") );
+ break;
+ default:
+ break;
+ }
+ if ( label_markup ) {
+
+ PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
+ pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
+
+ pango_layout_set_markup ( pl, label_markup, -1 );
+ g_free ( label_markup );
+ int ww, hh;
+ pango_layout_get_pixel_size ( pl, &ww, &hh );
+
+ gdk_draw_layout ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0],
+ MARGIN_X+xx-ww/2, MARGIN_Y/2-hh/2, pl );
+ g_object_unref ( G_OBJECT ( pl ) );
+ }
+
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
+ MARGIN_X+xx, MARGIN_Y, MARGIN_X+xx, MARGIN_Y+widgets->profile_height );
+}
+
+/**
+ * draw_grid_x_distance:
+ *
+ * A common way to draw the grid with x axis labels for distance graphs
+ *
+ */
+static void draw_grid_x_distance ( GtkWidget *window, GtkWidget *image, PropWidgets *widgets, GdkPixmap *pix, guint ii, gdouble dd, guint xx, vik_units_distance_t dist_units )
+{
+ gchar *label_markup = NULL;
+ switch ( dist_units ) {
+ case VIK_UNITS_DISTANCE_MILES:
+ if ( ii > 4 )
+ label_markup = g_strdup_printf ( "<span size=\"small\">%d %s</span>", (guint)dd, _("miles") );
+ else
+ label_markup = g_strdup_printf ( "<span size=\"small\">%.1f %s</span>", dd, _("miles") );
+ break;
+ case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
+ if ( ii > 4 )
+ label_markup = g_strdup_printf ( "<span size=\"small\">%d %s</span>", (guint)dd, _("NM") );
+ else
+ label_markup = g_strdup_printf ( "<span size=\"small\">%.1f %s</span>", dd, _("NM") );
+ break;
+ default:
+ // VIK_UNITS_DISTANCE_KILOMETRES:
+ if ( ii > 4 )
+ label_markup = g_strdup_printf ( "<span size=\"small\">%d %s</span>", (guint)dd, _("km") );
+ else
+ label_markup = g_strdup_printf ( "<span size=\"small\">%.1f %s</span>", dd, _("km") );
+ break;
+ }
+
+ if ( label_markup ) {
+ PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
+ pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
+
+ pango_layout_set_markup ( pl, label_markup, -1 );
+ g_free ( label_markup );
+ int ww, hh;
+ pango_layout_get_pixel_size ( pl, &ww, &hh );
+
+ gdk_draw_layout ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0],
+ MARGIN_X+xx-ww/2, MARGIN_Y/2-hh/2, pl );
+ g_object_unref ( G_OBJECT ( pl ) );
+ }
+
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
+ MARGIN_X+xx, MARGIN_Y, MARGIN_X+xx, MARGIN_Y+widgets->profile_height );
+}
+
+/**
+ * clear the images (scale texts & actual graph)
+ */
+static void clear_images (GdkPixmap *pix, GtkWidget *window, PropWidgets *widgets)
+{
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->bg_gc[0],
+ TRUE, 0, 0, widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->mid_gc[0],
+ TRUE, 0, 0, widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y);
+}
+
+/**
+ *
+ */
+static void draw_distance_divisions ( GtkWidget *window, GtkWidget *image, GdkPixmap *pix, PropWidgets *widgets, vik_units_distance_t dist_units )
+{
+ // Set to display units from length in metres.
+ gdouble length = widgets->track_length_inc_gaps;
+ switch (dist_units) {
+ case VIK_UNITS_DISTANCE_MILES:
+ length = VIK_METERS_TO_MILES(length);
+ break;
+ case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
+ length = VIK_METERS_TO_NAUTICAL_MILES(length);
+ break;
+ default:
+ // KM
+ length = length/1000.0;
+ break;
+ }
+ guint index = get_distance_chunk_index ( length );
+ gdouble dist_per_pixel = length/widgets->profile_width;
+
+ for (guint i=1; chunksd[index]*i <= length; i++) {
+ draw_grid_x_distance ( window, image, widgets, pix, index, chunksd[index]*i, (guint)(chunksd[index]*i/dist_per_pixel), dist_units );
+ }
+}
+
+/**
+ * Draw just the height profile image
+ */
+static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
+{
+ guint i;
+
+ GdkGC *no_alt_info;
+ 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 (widgets->min_altitude, widgets->max_altitude, chunksa, G_N_ELEMENTS(chunksa), &widgets->draw_min_altitude, &widgets->cia);
+
+ // Assign locally
+ gdouble mina = widgets->draw_min_altitude;
+
+ GtkWidget *window = gtk_widget_get_toplevel (widgets->elev_box);
+ GdkPixmap *pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ no_alt_info = gdk_gc_new ( gtk_widget_get_window(window) );
+ gdk_color_parse ( "yellow", &color );
+ gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
+
+ // Reset before redrawing
+ clear_images (pix, window, widgets);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ gchar s[32];
+
+ 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);
+ }
+
+ draw_grid_y ( window, image, widgets, pix, s, i );
+ }
+
+ draw_distance_divisions ( window, image, pix, widgets, a_vik_get_units_distance() );
+
+ /* draw elevations */
+ guint height = MARGIN_Y+widgets->profile_height;
+ 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_X, MARGIN_Y, i + MARGIN_X, height );
+ else
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
+ i + MARGIN_X, height, i + MARGIN_X, height-widgets->profile_height*(widgets->altitudes[i]-mina)/(chunksa[widgets->cia]*LINES) );
+
+ if ( 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)) ) {
+
+ GdkGC *dem_alt_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+ GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+
+ gdk_color_parse ( "green", &color );
+ gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
+
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
+
+ // 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,
+ widgets->max_altitude - mina,
+ widgets->max_speed,
+ widgets->cia,
+ widgets->profile_width,
+ widgets->profile_height,
+ MARGIN_X,
+ 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)));
+
+ g_object_unref ( G_OBJECT(dem_alt_gc) );
+ g_object_unref ( G_OBJECT(gps_speed_gc) );
+ }
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN_X, MARGIN_Y, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+ g_object_unref ( G_OBJECT(no_alt_info) );
+}
+
+/**
+ * Draws representative speed on the supplied pixmap
+ * (which is the gradients graph)
+ */
+static void draw_speed_dist(VikTrack *tr,
+ GdkDrawable *pix,
+ GdkGC *speed_gc,
+ gdouble max_speed_in,
+ gint width,
+ gint height,
+ gint margin,
+ 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_X;
+ 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 gradient image
+ */
+static void draw_gradients (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
+{
+ guint i;
+
+ // Free previous allocation
+ if ( widgets->gradients )
+ g_free ( widgets->gradients );
+
+ widgets->gradients = vik_track_make_gradient_map ( tr, widgets->profile_width );
+
+ if ( widgets->gradients == NULL )
+ return;
+
+ minmax_array(widgets->gradients, &widgets->min_gradient, &widgets->max_gradient, TRUE, widgets->profile_width);
+
+ get_new_min_and_chunk_index (widgets->min_gradient, widgets->max_gradient, chunksg, G_N_ELEMENTS(chunksg), &widgets->draw_min_gradient, &widgets->cig);
+
+ // Assign locally
+ gdouble mina = widgets->draw_min_gradient;
+
+ GtkWidget *window = gtk_widget_get_toplevel (widgets->gradient_box);
+ GdkPixmap *pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ // Reset before redrawing
+ clear_images (pix, window, widgets);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ gchar s[32];
+
+ sprintf(s, "%8d%%", (int)(mina + (LINES-i)*chunksg[widgets->cig]));
+
+ draw_grid_y ( window, image, widgets, pix, s, i );
+ }
+
+ draw_distance_divisions ( window, image, pix, widgets, a_vik_get_units_distance() );
+
+ /* draw gradients */
+ guint height = widgets->profile_height + MARGIN_Y;
+ for ( i = 0; i < widgets->profile_width; i++ )
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
+ i + MARGIN_X, height, i + MARGIN_X, height - widgets->profile_height*(widgets->gradients[i]-mina)/(chunksg[widgets->cig]*LINES) );
+
+ if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_gradient_gps_speed)) ) {
+ GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+
+ GdkColor color;
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
+
+ // Ensure somekind of max speed when not set
+ if ( widgets->max_speed < 0.01 )
+ widgets->max_speed = vik_track_get_max_speed(tr);
+
+ draw_speed_dist(tr,
+ GDK_DRAWABLE(pix),
+ gps_speed_gc,
+ widgets->max_speed,
+ widgets->profile_width,
+ widgets->profile_height,
+ MARGIN_X,
+ gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)));
+
+ g_object_unref ( G_OBJECT(gps_speed_gc) );
+ }
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN_X, MARGIN_Y, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+}
+
+static void draw_time_lines ( GtkWidget *window, GtkWidget *image, GdkPixmap *pix, PropWidgets *widgets )
+{
+ guint index = get_time_chunk_index ( widgets->duration );
+ gdouble time_per_pixel = (gdouble)(widgets->duration)/widgets->profile_width;
+
+ // If stupidly long track in time - don't bother trying to draw grid lines
+ if ( widgets->duration > chunkst[G_N_ELEMENTS(chunkst)-1]*LINES*LINES )
+ return;
+
+ for (guint i=1; chunkst[index]*i <= widgets->duration; i++) {
+ draw_grid_x_time ( window, image, widgets, pix, index, chunkst[index]*i, (guint)(chunkst[index]*i/time_per_pixel) );
+ }
+}
+
+/**
+ * Draw just the speed (velocity)/time image
+ */
+static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
+{
+ 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;
+
+ widgets->duration = vik_track_get_duration ( tr );
+ // Negative time or other problem
+ if ( widgets->duration <= 0 )
+ 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;
+ }
+
+ GtkWidget *window = gtk_widget_get_toplevel (widgets->speed_box);
+ GdkPixmap *pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ 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, G_N_ELEMENTS(chunkss), &widgets->draw_min_speed, &widgets->cis);
+
+ // Assign locally
+ gdouble mins = widgets->draw_min_speed;
+
+ // Reset before redrawing
+ clear_images (pix, window, widgets);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ gchar s[32];
+
+ // 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);
+ }
+
+ draw_grid_y ( window, image, widgets, pix, s, i );
+ }
+
+ draw_time_lines ( window, image, pix, widgets );
+
+ /* draw speeds */
+ guint height = widgets->profile_height + MARGIN_Y;
+ for ( i = 0; i < widgets->profile_width; i++ )
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
+ i + MARGIN_X, height, i + MARGIN_X, height - widgets->profile_height*(widgets->speeds[i]-mins)/(chunkss[widgets->cis]*LINES) );
+
+ if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_gps_speed)) ) {
+
+ GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+ GdkColor color;
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
+
+ time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
+ time_t dur = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
+
+ GList *iter;
+ for (iter = tr->trackpoints; iter; iter = iter->next) {
+ gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
+ if (isnan(gps_speed))
+ continue;
+ switch (speed_units) {
+ case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
+ gps_speed = VIK_MPS_TO_KPH(gps_speed);
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ gps_speed = VIK_MPS_TO_MPH(gps_speed);
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
+ break;
+ default:
+ // VIK_UNITS_SPEED_METRES_PER_SECOND:
+ // No need to convert as already in m/s
+ break;
+ }
+ int x = MARGIN_X + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
+ int y = height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cis]*LINES);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
+ }
+ g_object_unref ( G_OBJECT(gps_speed_gc) );
+ }
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN_X, MARGIN_Y, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+}
+
+/**
+ * Draw just the distance/time image
+ */
+static void draw_dt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
+{
+ guint i;
+
+ // Free previous allocation
+ if ( widgets->distances )
+ g_free ( widgets->distances );
+
+ widgets->distances = vik_track_make_distance_map ( tr, widgets->profile_width );
+ if ( widgets->distances == NULL )
+ return;
+
+ // Convert into appropriate units
+ vik_units_distance_t dist_units = a_vik_get_units_distance ();
+ switch ( dist_units ) {
+ case VIK_UNITS_DISTANCE_MILES:
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->distances[i] = VIK_METERS_TO_MILES(widgets->distances[i]);
+ }
+ break;
+ case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->distances[i] = VIK_METERS_TO_NAUTICAL_MILES(widgets->distances[i]);
+ }
+ break;
+ default:
+ // Metres - but want in kms
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->distances[i] = widgets->distances[i]/1000.0;
+ }
+ break;
+ }
+
+ widgets->duration = vik_track_get_duration ( widgets->tr );
+ // Negative time or other problem
+ if ( widgets->duration <= 0 )
+ return;
+
+ GtkWidget *window = gtk_widget_get_toplevel (widgets->dist_box);
+ GdkPixmap *pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ // easy to work out min / max of distance!
+ // Assign locally
+ // mind = 0.0; - Thus not used
+ gdouble maxd;
+ switch ( dist_units ) {
+ case VIK_UNITS_DISTANCE_MILES:
+ maxd = VIK_METERS_TO_MILES(vik_track_get_length_including_gaps (tr));
+ break;
+ case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
+ maxd = VIK_METERS_TO_NAUTICAL_MILES(vik_track_get_length_including_gaps (tr));
+ break;
+ default:
+ maxd = vik_track_get_length_including_gaps (tr) / 1000.0;
+ break;
+ }
+
+ /* Find suitable chunk index */
+ gdouble dummy = 0.0; // expect this to remain the same! (not that it's used)
+ get_new_min_and_chunk_index (0, maxd, chunksd, G_N_ELEMENTS(chunksd), &dummy, &widgets->cid);
+
+ // Reset before redrawing
+ clear_images (pix, window, widgets);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ gchar s[32];
+
+ switch ( dist_units ) {
+ case VIK_UNITS_DISTANCE_MILES:
+ sprintf(s, _("%.1f miles"), ((LINES-i)*chunksd[widgets->cid]));
+ break;
+ case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
+ sprintf(s, _("%.1f NM"), ((LINES-i)*chunksd[widgets->cid]));
+ break;
+ default:
+ sprintf(s, _("%.1f km"), ((LINES-i)*chunksd[widgets->cid]));
+ break;
+ }
+
+ draw_grid_y ( window, image, widgets, pix, s, i );
+ }
+
+ draw_time_lines ( window, image, pix, widgets );
+
+ /* draw distance */
+ guint height = widgets->profile_height + MARGIN_Y;
+ for ( i = 0; i < widgets->profile_width; i++ )
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
+ i + MARGIN_X, height, i + MARGIN_X, height - widgets->profile_height*(widgets->distances[i])/(chunksd[widgets->cid]*LINES) );
+
+ // Show speed indicator
+ if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_dist_speed)) ) {
+ GdkGC *dist_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+ GdkColor color;
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( dist_speed_gc, &color);
+
+ gdouble max_speed = 0;
+ max_speed = widgets->max_speed * 110 / 100;
+
+ // This is just an indicator - no actual values can be inferred by user
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), dist_speed_gc, TRUE, i+MARGIN_X-2, y_speed-2, 4, 4);
+ }
+ g_object_unref ( G_OBJECT(dist_speed_gc) );
+ }
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN_X, MARGIN_Y, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+
+}
+
+/**
+ * Draw just the elevation/time image
+ */
+static void draw_et ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
+{
+ guint i;
+
+ // Free previous allocation
+ if ( widgets->ats )
+ g_free ( widgets->ats );
+
+ widgets->ats = vik_track_make_elevation_time_map ( tr, widgets->profile_width );
+
+ if ( widgets->ats == 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->ats[i] = VIK_METERS_TO_FEET(widgets->ats[i]);
+ }
+ }
+ // Otherwise leave in metres
+
+ minmax_array(widgets->ats, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
+
+ get_new_min_and_chunk_index (widgets->min_altitude, widgets->max_altitude, chunksa, G_N_ELEMENTS(chunksa), &widgets->draw_min_altitude_time, &widgets->ciat);
+
+ // Assign locally
+ gdouble mina = widgets->draw_min_altitude_time;
+
+ widgets->duration = vik_track_get_duration ( widgets->tr );
+ // Negative time or other problem
+ if ( widgets->duration <= 0 )
+ return;
+
+ GtkWidget *window = gtk_widget_get_toplevel (widgets->elev_time_box);
+ GdkPixmap *pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ // Reset before redrawing
+ clear_images (pix, window, widgets);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ gchar s[32];
+
+ switch (height_units) {
+ case VIK_UNITS_HEIGHT_METRES:
+ sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
+ break;
+ case VIK_UNITS_HEIGHT_FEET:
+ // NB values already converted into feet
+ sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
+ break;
+ default:
+ sprintf(s, "--");
+ g_critical("Houston, we've had a problem. height=%d", height_units);
+ }
+
+ draw_grid_y ( window, image, widgets, pix, s, i );
+ }
+
+ draw_time_lines ( window, image, pix, widgets );
+
+ /* draw elevations */
+ guint height = widgets->profile_height + MARGIN_Y;
+ for ( i = 0; i < widgets->profile_width; i++ )
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
+ i + MARGIN_X, height, i + MARGIN_X, height-widgets->profile_height*(widgets->ats[i]-mina)/(chunksa[widgets->ciat]*LINES) );
+
+ // Show DEMS
+ if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_elev_dem)) ) {
+ GdkColor color;
+ GdkGC *dem_alt_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+ gdk_color_parse ( "green", &color );
+ gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
+
+ gint h2 = widgets->profile_height + MARGIN_Y; // Adjust height for x axis labelling offset
+ gint achunk = chunksa[widgets->ciat]*LINES;
+
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ // This could be slow doing this each time...
+ VikTrackpoint *tp = vik_track_get_closest_tp_by_percentage_time ( widgets->tr, ((gdouble)i/(gdouble)widgets->profile_width), NULL );
+ if ( tp ) {
+ gint16 elev = a_dems_get_elev_by_coord(&(tp->coord), VIK_DEM_INTERPOL_SIMPLE);
+ elev -= mina; //?
+ 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 = h2 - ((widgets->profile_height * elev)/achunk );
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), dem_alt_gc, TRUE, i+MARGIN_X-2, y_alt-2, 4, 4);
+ }
+ }
+ }
+ g_object_unref ( G_OBJECT(dem_alt_gc) );
+ }
+
+ // Show speeds
+ if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_elev_speed)) ) {
+ GdkColor color;
+ // This is just an indicator - no actual values can be inferred by user
+ GdkGC *elev_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( elev_speed_gc, &color);
+
+ gdouble max_speed = widgets->max_speed * 110 / 100;
+
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), elev_speed_gc, TRUE, i+MARGIN_X-2, y_speed-2, 4, 4);
+ }
+
+ g_object_unref ( G_OBJECT(elev_speed_gc) );
+ }
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN_X, MARGIN_Y, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+}
+
+/**
+ * Draw just the speed/distance image
+ */
+static void draw_sd ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
+{
+ gdouble mins;
+ guint i;
+
+ // Free previous allocation
+ if ( widgets->speeds_dist )
+ g_free ( widgets->speeds_dist );
+
+ widgets->speeds_dist = vik_track_make_speed_dist_map ( tr, widgets->profile_width );
+ if ( widgets->speeds_dist == 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_dist[i] = VIK_MPS_TO_KPH(widgets->speeds_dist[i]);
+ }
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->speeds_dist[i] = VIK_MPS_TO_MPH(widgets->speeds_dist[i]);
+ }
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ for ( i = 0; i < widgets->profile_width; i++ ) {
+ widgets->speeds_dist[i] = VIK_MPS_TO_KNOTS(widgets->speeds_dist[i]);
+ }
+ break;
+ default:
+ // VIK_UNITS_SPEED_METRES_PER_SECOND:
+ // No need to convert as already in m/s
+ break;
+ }
+
+ GtkWidget *window = gtk_widget_get_toplevel (widgets->speed_dist_box);
+ GdkPixmap *pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width+MARGIN_X, widgets->profile_height+MARGIN_Y, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ // OK to resuse min_speed here
+ minmax_array(widgets->speeds_dist, &widgets->min_speed, &widgets->max_speed_dist, 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_dist, chunkss, G_N_ELEMENTS(chunkss), &widgets->draw_min_speed, &widgets->cisd);
+
+ // Assign locally
+ mins = widgets->draw_min_speed;
+
+ // Reset before redrawing
+ clear_images (pix, window, widgets);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ gchar s[32];
+
+ // 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->cisd]));
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
+ break;
+ case VIK_UNITS_SPEED_METRES_PER_SECOND:
+ sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
+ break;
+ default:
+ sprintf(s, "--");
+ g_critical("Houston, we've had a problem. speed=%d", speed_units);
+ }
+
+ draw_grid_y ( window, image, widgets, pix, s, i );
+ }
+
+ draw_distance_divisions ( window, image, pix, widgets, a_vik_get_units_distance() );
+
+ /* draw speeds */
+ guint height = widgets->profile_height + MARGIN_Y;
+ for ( i = 0; i < widgets->profile_width; i++ )
+ gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
+ i + MARGIN_X, height, i + MARGIN_X, height - widgets->profile_height*(widgets->speeds_dist[i]-mins)/(chunkss[widgets->cisd]*LINES) );
+
+
+ if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_sd_gps_speed)) ) {
+
+ GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
+ GdkColor color;
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
+
+ gdouble dist = vik_track_get_length_including_gaps(tr);
+ gdouble dist_tp = 0.0;
+
+ GList *iter = tr->trackpoints;
+ for (iter = iter->next; iter; iter = iter->next) {
+ gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
+ if (isnan(gps_speed))
+ continue;
+ switch (speed_units) {
+ case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
+ gps_speed = VIK_MPS_TO_KPH(gps_speed);
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ gps_speed = VIK_MPS_TO_MPH(gps_speed);
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
+ break;
+ default:
+ // VIK_UNITS_SPEED_METRES_PER_SECOND:
+ // No need to convert as already in m/s
+ break;
+ }
+ dist_tp += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
+ int x = MARGIN_X + (widgets->profile_width * dist_tp / dist);
+ int y = height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cisd]*LINES);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
+ }
+ g_object_unref ( G_OBJECT(gps_speed_gc) );
+ }
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN_X, MARGIN_Y, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+}
+#undef LINES
+
+/**
+ * Draw all graphs
+ */
+static void draw_all_graphs ( GtkWidget *widget, PropWidgets *widgets, gboolean resized )
+{
+ // Draw graphs even if they are not visible
+
+ GList *child = NULL;
+ GtkWidget *image = NULL;
+ GtkWidget *window = gtk_widget_get_toplevel(widget);
+ gdouble pc = NAN;
+ gdouble pc_blob = NAN;
+
+ // Draw elevations
+ if (widgets->elev_box != NULL) {
+
+ // Saved image no longer any good as we've resized, so we remove it here
+ if (resized && widgets->elev_graph_saved_img.img) {
+ g_object_unref(widgets->elev_graph_saved_img.img);
+ widgets->elev_graph_saved_img.img = NULL;
+ widgets->elev_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box));
+ draw_elevations (GTK_WIDGET(child->data), widgets->tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker or blob are redrawn if necessary
+ if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
+
+ pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
+ gdouble x_blob = -MARGIN_X - 1.0; // i.e. Don't draw unless we get a valid value
+ gint y_blob = 0;
+ if (widgets->is_blob_drawn) {
+ pc_blob = tp_percentage_by_distance ( widgets->tr, widgets->blob_tp, widgets->track_length_inc_gaps );
+ if (!isnan(pc_blob)) {
+ x_blob = (pc_blob * widgets->profile_width);
+ }
+ y_blob = blobby_altitude (x_blob, widgets);
+ }
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ x_blob+MARGIN_X,
+ y_blob+MARGIN_Y,
+ &widgets->elev_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+ }
+ }
+
+ // Draw gradients
+ if (widgets->gradient_box != NULL) {
+
+ // Saved image no longer any good as we've resized, so we remove it here
+ if (resized && widgets->gradient_graph_saved_img.img) {
+ g_object_unref(widgets->gradient_graph_saved_img.img);
+ widgets->gradient_graph_saved_img.img = NULL;
+ widgets->gradient_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->gradient_box));
+ draw_gradients (GTK_WIDGET(child->data), widgets->tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker or blob are redrawn if necessary
+ if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
+
+ pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
+ gdouble x_blob = -MARGIN_X - 1.0; // i.e. Don't draw unless we get a valid value
+ gint y_blob = 0;
+ if (widgets->is_blob_drawn) {
+ pc_blob = tp_percentage_by_distance ( widgets->tr, widgets->blob_tp, widgets->track_length_inc_gaps );
+ if (!isnan(pc_blob)) {
+ x_blob = (pc_blob * widgets->profile_width);
+ }
+ y_blob = blobby_gradient (x_blob, widgets);
+ }
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ x_blob+MARGIN_X,
+ y_blob+MARGIN_Y,
+ &widgets->gradient_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+ }
+ }
+
+ // Draw speeds
+ if (widgets->speed_box != NULL) {
+
+ // Saved image no longer any good as we've resized
+ if (resized && widgets->speed_graph_saved_img.img) {
+ g_object_unref(widgets->speed_graph_saved_img.img);
+ widgets->speed_graph_saved_img.img = NULL;
+ widgets->speed_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box));
+ draw_vt (GTK_WIDGET(child->data), widgets->tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker or blob are redrawn if necessary
+ if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
+
+ pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
+
+ gdouble x_blob = -MARGIN_X - 1.0; // i.e. Don't draw unless we get a valid value
+ gint y_blob = 0;
+ if (widgets->is_blob_drawn) {
+ pc_blob = tp_percentage_by_time ( widgets->tr, widgets->blob_tp );
+ if (!isnan(pc_blob)) {
+ x_blob = (pc_blob * widgets->profile_width);
+ }
+
+ y_blob = blobby_speed (x_blob, widgets);
+ }
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ x_blob+MARGIN_X,
+ y_blob+MARGIN_Y,
+ &widgets->speed_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+ }
+ }
+
+ // Draw Distances
+ if (widgets->dist_box != NULL) {
+
+ // Saved image no longer any good as we've resized
+ if (resized && widgets->dist_graph_saved_img.img) {
+ g_object_unref(widgets->dist_graph_saved_img.img);
+ widgets->dist_graph_saved_img.img = NULL;
+ widgets->dist_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->dist_box));
+ draw_dt (GTK_WIDGET(child->data), widgets->tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker or blob are redrawn if necessary
+ if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
+
+ pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
+
+ gdouble x_blob = -MARGIN_X - 1.0; // i.e. Don't draw unless we get a valid value
+ gint y_blob = 0;
+ if (widgets->is_blob_drawn) {
+ pc_blob = tp_percentage_by_time ( widgets->tr, widgets->blob_tp );
+ if (!isnan(pc_blob)) {
+ x_blob = (pc_blob * widgets->profile_width);
+ }
+
+ y_blob = blobby_distance (x_blob, widgets);
+ }
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ x_blob+MARGIN_X,
+ y_blob+MARGIN_Y,
+ &widgets->dist_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+ }
+ }
+
+ // Draw Elevations in timely manner
+ if (widgets->elev_time_box != NULL) {
+
+ // Saved image no longer any good as we've resized
+ if (resized && widgets->elev_time_graph_saved_img.img) {
+ g_object_unref(widgets->elev_time_graph_saved_img.img);
+ widgets->elev_time_graph_saved_img.img = NULL;
+ widgets->elev_time_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_time_box));
+ draw_et (GTK_WIDGET(child->data), widgets->tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker or blob are redrawn if necessary
+ if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
+
+ pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
+
+ gdouble x_blob = -MARGIN_X - 1.0; // i.e. Don't draw unless we get a valid value
+ gint y_blob = 0;
+ if (widgets->is_blob_drawn) {
+ pc_blob = tp_percentage_by_time ( widgets->tr, widgets->blob_tp );
+ if (!isnan(pc_blob)) {
+ x_blob = (pc_blob * widgets->profile_width);
+ }
+ y_blob = blobby_altitude_time (x_blob, widgets);
+ }
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ x_blob+MARGIN_X,
+ y_blob+MARGIN_Y,
+ &widgets->elev_time_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+ }
+ }
+
+ // Draw speed distances
+ if (widgets->speed_dist_box != NULL) {
+
+ // Saved image no longer any good as we've resized, so we remove it here
+ if (resized && widgets->speed_dist_graph_saved_img.img) {
+ g_object_unref(widgets->speed_dist_graph_saved_img.img);
+ widgets->speed_dist_graph_saved_img.img = NULL;
+ widgets->speed_dist_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_dist_box));
+ draw_sd (GTK_WIDGET(child->data), widgets->tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker or blob are redrawn if necessary
+ if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
+
+ pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
+ gdouble x_blob = -MARGIN_X - 1.0; // i.e. Don't draw unless we get a valid value
+ gint y_blob = 0;
+ if (widgets->is_blob_drawn) {
+ pc_blob = tp_percentage_by_distance ( widgets->tr, widgets->blob_tp, widgets->track_length_inc_gaps );
+ if (!isnan(pc_blob)) {
+ x_blob = (pc_blob * widgets->profile_width);
+ }
+ y_blob = blobby_speed_dist (x_blob, widgets);
+ }
+
+ gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN_X;
+ }
+
+ save_image_and_draw_graph_marks (image,
+ marker_x,
+ gtk_widget_get_style(window)->black_gc,
+ x_blob+MARGIN_X,
+ y_blob+MARGIN_Y,
+ &widgets->speed_dist_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn,
+ &widgets->is_blob_drawn);
+ }
+ }
+
+}
+
+/**
+ * Configure/Resize the profile & speed/time images
+ */
+static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, PropWidgets *widgets )
+{
+ if (widgets->configure_dialog) {
+ // Determine size offsets between dialog size and size for images
+ // Only on the initialisation of the dialog
+ widgets->profile_width_offset = event->width - widgets->profile_width;
+ widgets->profile_height_offset = event->height - widgets->profile_height;
+ widgets->configure_dialog = FALSE;
+
+ // Without this the settting, the dialog will only grow in vertical size - one can not then make it smaller!
+ gtk_widget_set_size_request ( widget, widgets->profile_width+widgets->profile_width_offset, widgets->profile_height+widgets->profile_height_offset );
+
+ // Allow resizing back down to a minimal size (especially useful if the initial size has been made bigger after restoring from the saved settings)
+ GdkGeometry geom = { 600+widgets->profile_width_offset, 300+widgets->profile_height_offset, 0, 0, 0, 0, 0, 0, 0, 0, GDK_GRAVITY_STATIC };
+ gdk_window_set_geometry_hints ( gtk_widget_get_window(widget), &geom, GDK_HINT_MIN_SIZE );
+ }
+ else {
+ widgets->profile_width_old = widgets->profile_width;
+ widgets->profile_height_old = widgets->profile_height;