+/**
+ * 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;
+ 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 )
+{
+ GtkWidget *window;
+ GdkPixmap *pix;
+ gdouble mina;
+ guint i;
+
+ GdkColor color;
+
+ // 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_gradient (widgets->min_gradient, widgets->max_gradient, &widgets->draw_min_gradient, &widgets->cig);
+
+ // Assign locally
+ mina = widgets->draw_min_gradient;
+
+ window = gtk_widget_get_toplevel (widgets->gradient_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 );
+
+ /* 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);
+ sprintf(s, "%8d%%", (int)(mina + (LINES-i)*chunksg[widgets->cig]));
+ 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 gradients */
+ 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->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 ( window->window );
+
+ 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,
+ 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), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+}
+