]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_propwin.c
Make GPS layer use dynamic list of GPS devices as supported by GPSBabel.
[andy/viking.git] / src / viktrwlayer_propwin.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  * Copyright (C) 2005-2007, Alex Foobarian <foobarian@gmail.com>
6  * Copyright (C) 2007-2008, Quy Tonthat <qtonthat@gmail.com>
7  * Copyright (C) 2012, Rob Norris <rw_norris@hotmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #ifdef HAVE_MATH_H
30 #include <math.h>
31 #endif
32
33 #include <gtk/gtk.h>
34 #include <glib/gi18n.h>
35 #include <time.h>
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #include "coords.h"
40 #include "vikcoord.h"
41 #include "viktrack.h"
42 #include "viktrwlayer.h"
43 #include "viktrwlayer_propwin.h"
44 #include "vikwaypoint.h"
45 #include "dialog.h"
46 #include "globals.h"
47 #include "dems.h"
48
49 #include "vikviewport.h" /* ugh */
50 #include "viktreeview.h" /* ugh */
51 #include <gdk-pixbuf/gdk-pixdata.h>
52 #include "viklayer.h" /* ugh */
53 #include "vikaggregatelayer.h"
54 #include "viklayerspanel.h" /* ugh */
55
56 #define PROPWIN_PROFILE_WIDTH 600
57 #define PROPWIN_PROFILE_HEIGHT 300
58
59 #define PROPWIN_LABEL_FONT "Sans 7"
60
61 typedef enum {
62   PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE,
63   PROPWIN_GRAPH_TYPE_GRADIENT_DISTANCE,
64   PROPWIN_GRAPH_TYPE_SPEED_TIME,
65   PROPWIN_GRAPH_TYPE_DISTANCE_TIME,
66   PROPWIN_GRAPH_TYPE_ELEVATION_TIME,
67   PROPWIN_GRAPH_TYPE_SPEED_DISTANCE,
68   PROPWIN_GRAPH_TYPE_END,
69 } VikPropWinGraphType_t;
70
71 /* (Hopefully!) Human friendly altitude grid sizes - note no fixed 'ratio' just numbers that look nice...*/
72 static const gdouble chunksa[] = {2.0, 5.0, 10.0, 15.0, 20.0,
73                                   25.0, 40.0, 50.0, 75.0, 100.0,
74                                   150.0, 200.0, 250.0, 375.0, 500.0,
75                                   750.0, 1000.0, 2000.0, 5000.0, 10000.0, 100000.0};
76
77 /* (Hopefully!) Human friendly gradient grid sizes - note no fixed 'ratio' just numbers that look nice...*/
78 static const gdouble chunksg[] = {1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
79                                   12.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 75.0,
80                                   100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
81                                   750.0, 1000.0, 10000.0};
82
83 /* (Hopefully!) Human friendly grid sizes - note no fixed 'ratio' just numbers that look nice...*/
84 /* As need to cover walking speeds - have many low numbers (but also may go up to airplane speeds!) */
85 static const gdouble chunkss[] = {1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
86                                   15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
87                                   100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
88                                   750.0, 1000.0, 10000.0};
89
90 /* (Hopefully!) Human friendly distance grid sizes - note no fixed 'ratio' just numbers that look nice...*/
91 static const gdouble chunksd[] = {0.1, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
92                                   15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
93                                   100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
94                                   750.0, 1000.0, 10000.0};
95
96 typedef struct _propsaved {
97   gboolean saved;
98   GdkImage *img;
99 } PropSaved;
100
101 typedef struct _propwidgets {
102   gboolean  configure_dialog;
103   VikTrwLayer *vtl;
104   VikTrack *tr;
105   gint      profile_width;
106   gint      profile_height;
107   gint      profile_width_old;
108   gint      profile_height_old;
109   gint      profile_width_offset;
110   gint      profile_height_offset;
111   GtkWidget *dialog;
112   GtkWidget *w_comment;
113   GtkWidget *w_track_length;
114   GtkWidget *w_tp_count;
115   GtkWidget *w_segment_count;
116   GtkWidget *w_duptp_count;
117   GtkWidget *w_max_speed;
118   GtkWidget *w_avg_speed;
119   GtkWidget *w_mvg_speed;
120   GtkWidget *w_avg_dist;
121   GtkWidget *w_elev_range;
122   GtkWidget *w_elev_gain;
123   GtkWidget *w_time_start;
124   GtkWidget *w_time_end;
125   GtkWidget *w_time_dur;
126   GtkWidget *w_cur_dist; /*< Current distance */
127   GtkWidget *w_cur_elevation;
128   GtkWidget *w_cur_gradient_dist; /*< Current distance on gradient graph */
129   GtkWidget *w_cur_gradient_gradient; /*< Current gradient on gradient graph */
130   GtkWidget *w_cur_time; /*< Current time */
131   GtkWidget *w_cur_speed;
132   GtkWidget *w_cur_dist_dist; /*< Current distance on distance graph */
133   GtkWidget *w_cur_dist_time; /*< Current time on distance graph */
134   GtkWidget *w_cur_elev_elev;
135   GtkWidget *w_cur_elev_time;
136   GtkWidget *w_cur_speed_dist;
137   GtkWidget *w_cur_speed_speed;
138   GtkWidget *w_show_dem;
139   GtkWidget *w_show_alt_gps_speed;
140   GtkWidget *w_show_gps_speed;
141   GtkWidget *w_show_gradient_gps_speed;
142   GtkWidget *w_show_dist_speed;
143   GtkWidget *w_show_elev_speed;
144   GtkWidget *w_show_sd_gps_speed;
145   gdouble   track_length;
146   gdouble   track_length_inc_gaps;
147   PropSaved elev_graph_saved_img;
148   PropSaved gradient_graph_saved_img;
149   PropSaved speed_graph_saved_img;
150   PropSaved dist_graph_saved_img;
151   PropSaved elev_time_graph_saved_img;
152   PropSaved speed_dist_graph_saved_img;
153   GtkWidget *elev_box;
154   GtkWidget *gradient_box;
155   GtkWidget *speed_box;
156   GtkWidget *dist_box;
157   GtkWidget *elev_time_box;
158   GtkWidget *speed_dist_box;
159   gdouble   *altitudes;
160   gdouble   *ats; // altitudes in time
161   gdouble   min_altitude;
162   gdouble   max_altitude;
163   gdouble   draw_min_altitude;
164   gdouble   draw_min_altitude_time;
165   gint      cia; // Chunk size Index into Altitudes
166   gint      ciat; // Chunk size Index into Altitudes / Time
167   // NB cia & ciat are normally same value but sometimes not due to differing methods of altitude array creation
168   //    thus also have draw_min_altitude for each altitude graph type
169   gdouble   *gradients;
170   gdouble   min_gradient;
171   gdouble   max_gradient;
172   gdouble   draw_min_gradient;
173   gint      cig; // Chunk size Index into Gradients
174   gdouble   *speeds;
175   gdouble   *speeds_dist;
176   gdouble   min_speed;
177   gdouble   max_speed;
178   gdouble   draw_min_speed;
179   gdouble   max_speed_dist;
180   gint      cis; // Chunk size Index into Speeds
181   gint      cisd; // Chunk size Index into Speed/Distance
182   gdouble   *distances;
183   gint      cid; // Chunk size Index into Distance
184   VikTrackpoint *marker_tp;
185   gboolean  is_marker_drawn;
186   VikTrackpoint *blob_tp;
187   gboolean  is_blob_drawn;
188 } PropWidgets;
189
190 static PropWidgets *prop_widgets_new()
191 {
192   PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
193
194   return widgets;
195 }
196
197 static void prop_widgets_free(PropWidgets *widgets)
198 {
199   if (widgets->elev_graph_saved_img.img)
200     g_object_unref(widgets->elev_graph_saved_img.img);
201   if (widgets->gradient_graph_saved_img.img)
202     g_object_unref(widgets->gradient_graph_saved_img.img);
203   if (widgets->speed_graph_saved_img.img)
204     g_object_unref(widgets->speed_graph_saved_img.img);
205   if (widgets->dist_graph_saved_img.img)
206     g_object_unref(widgets->dist_graph_saved_img.img);
207   if (widgets->elev_time_graph_saved_img.img)
208     g_object_unref(widgets->elev_time_graph_saved_img.img);
209   if (widgets->speed_dist_graph_saved_img.img)
210     g_object_unref(widgets->speed_dist_graph_saved_img.img);
211   if (widgets->altitudes)
212     g_free(widgets->altitudes);
213   if (widgets->gradients)
214     g_free(widgets->gradients);
215   if (widgets->speeds)
216     g_free(widgets->speeds);
217   if (widgets->distances)
218     g_free(widgets->distances);
219   if (widgets->ats)
220     g_free(widgets->ats);
221   if (widgets->speeds_dist)
222     g_free(widgets->speeds_dist);
223   g_free(widgets);
224 }
225
226 static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gboolean NO_ALT_TEST, gint PROFILE_WIDTH)
227 {
228   *max = -1000;
229   *min = 20000;
230   guint i;
231   for ( i=0; i < PROFILE_WIDTH; i++ ) {
232     if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) {
233       if ( array[i] > *max )
234         *max = array[i];
235       if ( array[i] < *min )
236         *min = array[i];
237     }
238   }
239 }
240
241 #define MARGIN 70
242 #define LINES 5
243 /**
244  * Returns via pointers:
245  *   the new minimum value to be used for the altitude graph
246  *   the index in to the altitudes chunk sizes array (ci = Chunk Index)
247  */
248 static void get_new_min_and_chunk_index_altitude (gdouble mina, gdouble maxa, gdouble *new_min, gint *ci)
249 {
250   /* Get unitized chunk */
251   /* Find suitable chunk index */
252   *ci = 0;
253   gdouble diffa_chunk = (maxa - mina)/LINES;
254
255   /* Loop through to find best match */
256   while (diffa_chunk > chunksa[*ci]) {
257     (*ci)++;
258     /* Last Resort Check */
259     if ( *ci == sizeof(chunksa)/sizeof(chunksa[0]) )
260       break;
261   }
262
263   /* Ensure adjusted minimum .. maximum covers mina->maxa */
264
265   // Now work out adjusted minimum point to the nearest lowest chunk divisor value
266   // When negative ensure logic uses lowest value
267   if ( mina < 0 )
268     *new_min = (gdouble) ( ( (gint)(mina - chunksa[*ci]) / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
269   else
270     *new_min = (gdouble) ( ( (gint)mina / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
271
272   // Range not big enough - as new minimum has lowered
273   if ((*new_min + (chunksa[*ci] * LINES) < maxa)) {
274     // Next chunk should cover it
275     if ( *ci < sizeof(chunksa)/sizeof(chunksa[0]) ) {
276       (*ci)++;
277       // Remember to adjust the minimum too...
278       if ( mina < 0 )
279         *new_min = (gdouble) ( ( (gint)(mina - chunksa[*ci]) / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
280       else
281         *new_min = (gdouble) ( ( (gint)mina / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
282     }
283   }
284 }
285
286 /**
287  * Returns via pointers:
288  *   the new minimum value to be used for the altitude graph
289  *   the index in to the altitudes chunk sizes array (ci = Chunk Index)
290  */
291 static void get_new_min_and_chunk_index_gradient (gdouble mina, gdouble maxa, gdouble *new_min, gint *ci)
292 {
293   /* Get unitized chunk */
294   /* Find suitable chunk index */
295   *ci = 0;
296   gdouble diffa_chunk = (maxa - mina)/LINES;
297
298   /* Loop through to find best match */
299   while (diffa_chunk > chunksg[*ci]) {
300     (*ci)++;
301     /* Last Resort Check */
302     if ( *ci == sizeof(chunksg)/sizeof(chunksg[0]) )
303       break;
304   }
305
306   /* Ensure adjusted minimum .. maximum covers mina->maxa */
307
308   // Now work out adjusted minimum point to the nearest lowest chunk divisor value
309   // When negative ensure logic uses lowest value
310   if ( mina < 0 )
311     *new_min = (gdouble) ( ( (gint)(mina - chunksg[*ci]) / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
312   else
313     *new_min = (gdouble) ( ( (gint)mina / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
314
315   // Range not big enough - as new minimum has lowered
316   if ((*new_min + (chunksg[*ci] * LINES) < maxa)) {
317     // Next chunk should cover it
318     if ( *ci < sizeof(chunksg)/sizeof(chunksg[0]) ) {
319       (*ci)++;
320       // Remember to adjust the minimum too...
321       if ( mina < 0 )
322         *new_min = (gdouble) ( ( (gint)(mina - chunksg[*ci]) / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
323       else
324         *new_min = (gdouble) ( ( (gint)mina / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
325     }
326   }
327 }
328
329 /**
330  * Returns via pointers:
331  *   the new minimum value to be used for the appropriate graph
332  *   the index in to that array (ci = Chunk Index)
333  */
334 static void get_new_min_and_chunk_index (gdouble mins, gdouble maxs, const gdouble *chunks, size_t chunky, gdouble *new_min, gint *ci)
335 {
336   *ci = 0;
337   gdouble diff_chunk = (maxs - mins)/LINES;
338
339   /* Loop through to find best match */
340   while (diff_chunk > chunks[*ci]) {
341     (*ci)++;
342     /* Last Resort Check */
343     if ( *ci == chunky )
344       break;
345   }
346   *new_min = (gdouble) ( (gint)((mins / chunks[*ci] ) * chunks[*ci]) );
347
348   // Speeds are never negative so don't need to worry about a negative new minimum
349 }
350
351 static VikTrackpoint *set_center_at_graph_position(gdouble event_x,
352                                                    gint img_width,
353                                                    VikTrwLayer *vtl,
354                                                    VikLayersPanel *vlp,
355                                                    VikViewport *vvp,
356                                                    VikTrack *tr,
357                                                    gboolean time_base,
358                                                    gint PROFILE_WIDTH)
359 {
360   VikTrackpoint *trackpoint;
361   gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
362   if (x < 0)
363     x = 0;
364   if (x > PROFILE_WIDTH)
365     x = PROFILE_WIDTH;
366
367   if (time_base)
368     trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
369   else
370     trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
371
372   if ( trackpoint ) {
373     VikCoord coord = trackpoint->coord;
374     if ( vlp ) {
375       vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
376       vik_layers_panel_emit_update ( vlp );
377     }
378     else {
379       /* since vlp not set, vvp should be valid instead! */
380       if ( vvp )
381         vik_viewport_set_center_coord ( vvp, &coord );
382       vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
383     }
384   }
385   return trackpoint;
386 }
387
388 /**
389  * Returns whether the marker was drawn or not and whether the blob was drawn or not
390  */
391 static void save_image_and_draw_graph_marks (GtkWidget *image,
392                                              gdouble marker_x,
393                                              GdkGC *gc,
394                                              gint blob_x,
395                                              gint blob_y,
396                                              PropSaved *saved_img,
397                                              gint PROFILE_WIDTH,
398                                              gint PROFILE_HEIGHT,
399                                              gboolean *marker_drawn,
400                                              gboolean *blob_drawn)
401 {
402   GdkPixmap *pix = NULL;
403   /* the pixmap = margin + graph area */
404   gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL);
405
406   /* Restore previously saved image */
407   if (saved_img->saved) {
408     gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
409     saved_img->saved = FALSE;
410   }
411
412   // ATM always save whole image - as anywhere could have changed
413   if (saved_img->img)
414     gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
415   else
416     saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
417   saved_img->saved = TRUE;
418
419   if ((marker_x >= MARGIN) && (marker_x < (PROFILE_WIDTH + MARGIN))) {
420     gdk_draw_line (GDK_DRAWABLE(pix), gc, marker_x, 0, marker_x, image->allocation.height);
421     *marker_drawn = TRUE;
422   }
423   else
424     *marker_drawn = FALSE;
425
426   // Draw a square blob to indicate where we are on track for this graph
427   if ( (blob_x >= MARGIN) && (blob_x < (PROFILE_WIDTH + MARGIN)) && (blob_y < PROFILE_HEIGHT) ) {
428     gdk_draw_rectangle (GDK_DRAWABLE(pix), gc, TRUE, blob_x-3, blob_y-3, 6, 6);
429     *blob_drawn = TRUE;
430   }
431   else
432     *blob_drawn = FALSE;
433   
434   // Anywhere on image could have changed
435   if (*marker_drawn || *blob_drawn)
436     gtk_widget_queue_draw(image);
437 }
438
439 /**
440  * Return the percentage of how far a trackpoint is a long a track via the time method
441  */
442 static gdouble tp_percentage_by_time ( VikTrack *tr, VikTrackpoint *trackpoint )
443 {
444   gdouble pc = NAN;
445   if (trackpoint == NULL)
446     return pc;
447   time_t t_start, t_end, t_total;
448   t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
449   t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
450   t_total = t_end - t_start;
451   pc = (gdouble)(trackpoint->timestamp - t_start)/t_total;
452   return pc;
453 }
454
455 /**
456  * Return the percentage of how far a trackpoint is a long a track via the distance method
457  */
458 static gdouble tp_percentage_by_distance ( VikTrack *tr, VikTrackpoint *trackpoint, gdouble track_length )
459 {
460   gdouble pc = NAN;
461   if (trackpoint == NULL)
462     return pc;
463   gdouble dist = 0.0;
464   GList *iter;
465   for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) {
466     dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord),
467                            &(VIK_TRACKPOINT(iter->prev->data)->coord));
468     /* Assuming trackpoint is not a copy */
469     if (trackpoint == VIK_TRACKPOINT(iter->data))
470       break;
471   }
472   if (iter != NULL)
473     pc = dist/track_length;
474   return pc;
475 }
476
477 static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, VikPropWinGraphType_t graph_type )
478 {
479   VikTrack *tr = pass_along[0];
480   VikLayersPanel *vlp = pass_along[1];
481   VikViewport *vvp = pass_along[2];
482   PropWidgets *widgets = pass_along[3];
483
484   gboolean is_time_graph =
485     ( graph_type == PROPWIN_GRAPH_TYPE_SPEED_TIME ||
486       graph_type == PROPWIN_GRAPH_TYPE_DISTANCE_TIME ||
487       graph_type == PROPWIN_GRAPH_TYPE_ELEVATION_TIME );
488
489   VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, widgets->vtl, vlp, vvp, tr, is_time_graph, widgets->profile_width);
490   // Unable to get the point so give up
491   if ( trackpoint == NULL ) {
492     gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
493     return;
494   }
495
496   widgets->marker_tp = trackpoint;
497
498   GList *child;
499   GtkWidget *image;
500   GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box));
501   GtkWidget *graph_box;
502   PropSaved *graph_saved_img;
503   gdouble pc = NAN;
504
505   // Attempt to redraw marker on all graph types
506   gint graphite;
507   for ( graphite = PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE;
508         graphite < PROPWIN_GRAPH_TYPE_END;
509         graphite++ ) {
510
511     // Switch commonal variables to particular graph type
512     switch (graphite) {
513     default:
514     case PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE:
515       graph_box       = widgets->elev_box;
516       graph_saved_img = &widgets->elev_graph_saved_img;
517       is_time_graph   = FALSE;
518       break;
519     case PROPWIN_GRAPH_TYPE_GRADIENT_DISTANCE:
520       graph_box       = widgets->gradient_box;
521       graph_saved_img = &widgets->gradient_graph_saved_img;
522       is_time_graph   = FALSE;
523       break;
524     case PROPWIN_GRAPH_TYPE_SPEED_TIME:
525       graph_box       = widgets->speed_box;
526       graph_saved_img = &widgets->speed_graph_saved_img;
527       is_time_graph   = TRUE;
528       break;
529     case PROPWIN_GRAPH_TYPE_DISTANCE_TIME:
530       graph_box       = widgets->dist_box;
531       graph_saved_img = &widgets->dist_graph_saved_img;
532       is_time_graph   = TRUE;
533       break;
534     case PROPWIN_GRAPH_TYPE_ELEVATION_TIME:
535       graph_box       = widgets->elev_time_box;
536       graph_saved_img = &widgets->elev_time_graph_saved_img;
537       is_time_graph   = TRUE;
538       break;
539     case PROPWIN_GRAPH_TYPE_SPEED_DISTANCE:
540       graph_box       = widgets->speed_dist_box;
541       graph_saved_img = &widgets->speed_dist_graph_saved_img;
542       is_time_graph   = FALSE;
543       break;
544     }
545
546     // Commonal method of redrawing marker
547     if ( graph_box ) {
548
549       child = gtk_container_get_children(GTK_CONTAINER(graph_box));
550       image = GTK_WIDGET(child->data);
551
552       if (is_time_graph)
553         pc = tp_percentage_by_time ( tr, trackpoint );
554       else
555         pc = tp_percentage_by_distance ( tr, trackpoint, widgets->track_length_inc_gaps );
556
557       if (!isnan(pc)) {
558         gdouble marker_x = (pc * widgets->profile_width) + MARGIN;
559         save_image_and_draw_graph_marks(image,
560                                         marker_x,
561                                         window->style->black_gc,
562                                         -1, // Don't draw blob on clicks
563                                         0,
564                                         graph_saved_img,
565                                         widgets->profile_width,
566                                         widgets->profile_height,
567                                         &widgets->is_marker_drawn,
568                                         &widgets->is_blob_drawn);
569       }
570       g_list_free(child);
571     }
572   }
573
574   gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, widgets->is_marker_drawn);
575 }
576
577 static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
578 {
579   track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE);
580   return TRUE;  /* don't call other (further) callbacks */
581 }
582
583 static gboolean track_gradient_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
584 {
585   track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_GRADIENT_DISTANCE);
586   return TRUE;  /* don't call other (further) callbacks */
587 }
588
589 static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
590 {
591   track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_SPEED_TIME);
592   return TRUE;  /* don't call other (further) callbacks */
593 }
594
595 static gboolean track_dt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
596 {
597   track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_DISTANCE_TIME);
598   return TRUE;  /* don't call other (further) callbacks */
599 }
600
601 static gboolean track_et_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
602 {
603   track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_ELEVATION_TIME);
604   return TRUE;  /* don't call other (further) callbacks */
605 }
606
607 static gboolean track_sd_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
608 {
609   track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_SPEED_DISTANCE);
610   return TRUE;  /* don't call other (further) callbacks */
611 }
612
613 /**
614  * Calculate y position for blob on elevation graph
615  */
616 static gint blobby_altitude ( gdouble x_blob, PropWidgets *widgets )
617 {
618   gint ix = (gint)x_blob;
619   // Ensure ix is inbounds
620   if (ix == widgets->profile_width)
621     ix--;
622
623   gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->altitudes[ix]-widgets->draw_min_altitude)/(chunksa[widgets->cia]*LINES);
624
625   return y_blob;
626 }
627
628 /**
629  * Calculate y position for blob on gradient graph
630  */
631 static gint blobby_gradient ( gdouble x_blob, PropWidgets *widgets )
632 {
633   gint ix = (gint)x_blob;
634   // Ensure ix is inbounds
635   if (ix == widgets->profile_width)
636     ix--;
637
638   gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->gradients[ix]-widgets->draw_min_gradient)/(chunksg[widgets->cig]*LINES);
639
640   return y_blob;
641 }
642
643 /**
644  * Calculate y position for blob on speed graph
645  */
646 static gint blobby_speed ( gdouble x_blob, PropWidgets *widgets )
647 {
648   gint ix = (gint)x_blob;
649   // Ensure ix is inbounds
650   if (ix == widgets->profile_width)
651     ix--;
652
653   gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->speeds[ix]-widgets->draw_min_speed)/(chunkss[widgets->cis]*LINES);
654
655   return y_blob;
656 }
657
658 /**
659  * Calculate y position for blob on distance graph
660  */
661 static gint blobby_distance ( gdouble x_blob, PropWidgets *widgets )
662 {
663   gint ix = (gint)x_blob;
664   // Ensure ix is inbounds
665   if (ix == widgets->profile_width)
666     ix--;
667
668   gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->distances[ix])/(chunksd[widgets->cid]*LINES);
669   //NB min distance is always 0, so no need to subtract that from this  ______/
670
671   return y_blob;
672 }
673
674 /**
675  * Calculate y position for blob on elevation/time graph
676  */
677 static gint blobby_altitude_time ( gdouble x_blob, PropWidgets *widgets )
678 {
679   gint ix = (gint)x_blob;
680   // Ensure ix is inbounds
681   if (ix == widgets->profile_width)
682     ix--;
683
684   gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->ats[ix]-widgets->draw_min_altitude_time)/(chunksa[widgets->ciat]*LINES);
685   return y_blob;
686 }
687
688 /**
689  * Calculate y position for blob on speed/dist graph
690  */
691 static gint blobby_speed_dist ( gdouble x_blob, PropWidgets *widgets )
692 {
693   gint ix = (gint)x_blob;
694   // Ensure ix is inbounds
695   if (ix == widgets->profile_width)
696     ix--;
697
698   gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->speeds_dist[ix]-widgets->draw_min_speed)/(chunkss[widgets->cisd]*LINES);
699
700   return y_blob;
701 }
702
703
704 void track_profile_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
705 {
706   VikTrack *tr = pass_along[0];
707   PropWidgets *widgets = pass_along[3];
708   int mouse_x, mouse_y;
709   GdkModifierType state;
710
711   if (event->is_hint)
712     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
713   else
714     mouse_x = event->x;
715
716   gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
717   if (x < 0)
718     x = 0;
719   if (x > widgets->profile_width)
720     x = widgets->profile_width;
721
722   gdouble meters_from_start;
723   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
724   if (trackpoint && widgets->w_cur_dist) {
725     static gchar tmp_buf[20];
726     vik_units_distance_t dist_units = a_vik_get_units_distance ();
727     switch (dist_units) {
728     case VIK_UNITS_DISTANCE_KILOMETRES:
729       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
730       break;
731     case VIK_UNITS_DISTANCE_MILES:
732       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
733       break;
734     default:
735       g_critical("Houston, we've had a problem. distance=%d", dist_units);
736     }
737     gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf);
738   }
739
740   // Show track elevation for this position - to the nearest whole number
741   if (trackpoint && widgets->w_cur_elevation) {
742     static gchar tmp_buf[20];
743     if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
744       g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude));
745     else
746       g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude);
747     gtk_label_set_text(GTK_LABEL(widgets->w_cur_elevation), tmp_buf);
748   }
749
750   widgets->blob_tp = trackpoint;
751
752   if ( widgets->altitudes == NULL )
753     return;
754
755   GtkWidget *window = gtk_widget_get_toplevel (event_box);
756   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
757   GtkWidget *image = GTK_WIDGET(child->data);
758
759   gint y_blob = blobby_altitude (x, widgets);
760
761   gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
762   if (widgets->is_marker_drawn) {
763     gdouble pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
764     if (!isnan(pc)) {
765       marker_x = (pc * widgets->profile_width) + MARGIN;
766     }
767   }
768
769   save_image_and_draw_graph_marks (image,
770                                    marker_x,
771                                    window->style->black_gc,
772                                    MARGIN+x,
773                                    y_blob,
774                                    &widgets->elev_graph_saved_img,
775                                    widgets->profile_width,
776                                    widgets->profile_height,
777                                    &widgets->is_marker_drawn,
778                                    &widgets->is_blob_drawn);
779
780   g_list_free(child);
781 }
782
783 void track_gradient_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
784 {
785   VikTrack *tr = pass_along[0];
786   PropWidgets *widgets = pass_along[3];
787   int mouse_x, mouse_y;
788   GdkModifierType state;
789
790   if (event->is_hint)
791     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
792   else
793     mouse_x = event->x;
794
795   gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
796   if (x < 0)
797     x = 0;
798   if (x > widgets->profile_width)
799     x = widgets->profile_width;
800
801   gdouble meters_from_start;
802   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
803   if (trackpoint && widgets->w_cur_gradient_dist) {
804     static gchar tmp_buf[20];
805     vik_units_distance_t dist_units = a_vik_get_units_distance ();
806     switch (dist_units) {
807     case VIK_UNITS_DISTANCE_KILOMETRES:
808       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
809       break;
810     case VIK_UNITS_DISTANCE_MILES:
811       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
812       break;
813     default:
814       g_critical("Houston, we've had a problem. distance=%d", dist_units);
815     }
816     gtk_label_set_text(GTK_LABEL(widgets->w_cur_gradient_dist), tmp_buf);
817   }
818
819   // Show track gradient for this position - to the nearest whole number
820   if (trackpoint && widgets->w_cur_gradient_gradient) {
821     static gchar tmp_buf[20];
822     
823     double gradient = widgets->gradients[(int) x];
824
825     g_snprintf(tmp_buf, sizeof(tmp_buf), "%d%%", (int)gradient);
826     gtk_label_set_text(GTK_LABEL(widgets->w_cur_gradient_gradient), tmp_buf);
827   }
828
829   widgets->blob_tp = trackpoint;
830
831   if ( widgets->gradients == NULL )
832     return;
833
834   GtkWidget *window = gtk_widget_get_toplevel (event_box);
835   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
836   GtkWidget *image = GTK_WIDGET(child->data);
837
838   gint y_blob = blobby_gradient (x, widgets);
839
840   gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
841   if (widgets->is_marker_drawn) {
842     gdouble pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
843     if (!isnan(pc)) {
844       marker_x = (pc * widgets->profile_width) + MARGIN;
845     }
846   }
847
848   save_image_and_draw_graph_marks (image,
849                                    marker_x,
850                                    window->style->black_gc,
851                                    MARGIN+x,
852                                    y_blob,
853                                    &widgets->gradient_graph_saved_img,
854                                    widgets->profile_width,
855                                    widgets->profile_height,
856                                    &widgets->is_marker_drawn,
857                                    &widgets->is_blob_drawn);
858
859   g_list_free(child);
860 }
861
862 void track_vt_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
863 {
864   VikTrack *tr = pass_along[0];
865   PropWidgets *widgets = pass_along[3];
866   int mouse_x, mouse_y;
867   GdkModifierType state;
868
869   if (event->is_hint)
870     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
871   else
872     mouse_x = event->x;
873
874   gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
875   if (x < 0)
876     x = 0;
877   if (x > widgets->profile_width)
878     x = widgets->profile_width;
879
880   time_t seconds_from_start;
881   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
882   if (trackpoint && widgets->w_cur_time) {
883     static gchar tmp_buf[20];
884     guint h, m, s;
885     h = seconds_from_start/3600;
886     m = (seconds_from_start - h*3600)/60;
887     s = seconds_from_start - (3600*h) - (60*m);
888     g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
889
890     gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf);
891   }
892
893   gint ix = (gint)x;
894   // Ensure ix is inbounds
895   if (ix == widgets->profile_width)
896     ix--;
897
898   // Show track speed for this position
899   if (trackpoint && widgets->w_cur_speed) {
900     static gchar tmp_buf[20];
901     // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown
902     // No conversions needed as already in appropriate units
903     vik_units_speed_t speed_units = a_vik_get_units_speed ();
904     switch (speed_units) {
905     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
906       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), widgets->speeds[ix]);
907       break;
908     case VIK_UNITS_SPEED_MILES_PER_HOUR:
909       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds[ix]);
910       break;
911     case VIK_UNITS_SPEED_KNOTS:
912       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds[ix]);
913       break;
914     default:
915       // VIK_UNITS_SPEED_METRES_PER_SECOND:
916       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds[ix]);
917       break;
918     }
919     gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed), tmp_buf);
920   }
921
922   widgets->blob_tp = trackpoint;
923
924   if ( widgets->speeds == NULL )
925     return;
926
927   GtkWidget *window = gtk_widget_get_toplevel (event_box);
928   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
929   GtkWidget *image = GTK_WIDGET(child->data);
930
931   gint y_blob = blobby_speed (x, widgets);
932
933   gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
934   if (widgets->is_marker_drawn) {
935     gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
936     if (!isnan(pc)) {
937       marker_x = (pc * widgets->profile_width) + MARGIN;
938     }
939   }
940
941   save_image_and_draw_graph_marks (image,
942                                    marker_x,
943                                    window->style->black_gc,
944                                    MARGIN+x,
945                                    y_blob,
946                                    &widgets->speed_graph_saved_img,
947                                    widgets->profile_width,
948                                    widgets->profile_height,
949                                    &widgets->is_marker_drawn,
950                                    &widgets->is_blob_drawn);
951
952   g_list_free(child);
953 }
954
955 /**
956  * Update labels and blob marker on mouse moves in the distance/time graph
957  */
958 void track_dt_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
959 {
960   VikTrack *tr = pass_along[0];
961   PropWidgets *widgets = pass_along[3];
962   int mouse_x, mouse_y;
963   GdkModifierType state;
964
965   if (event->is_hint)
966     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
967   else
968     mouse_x = event->x;
969
970   gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
971   if (x < 0)
972     x = 0;
973   if (x > widgets->profile_width)
974     x = widgets->profile_width;
975
976   time_t seconds_from_start;
977   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
978   if (trackpoint && widgets->w_cur_dist_time) {
979     static gchar tmp_buf[20];
980     guint h, m, s;
981     h = seconds_from_start/3600;
982     m = (seconds_from_start - h*3600)/60;
983     s = seconds_from_start - (3600*h) - (60*m);
984     g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
985
986     gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_time), tmp_buf);
987   }
988
989   gint ix = (gint)x;
990   // Ensure ix is inbounds
991   if (ix == widgets->profile_width)
992     ix--;
993
994   if (trackpoint && widgets->w_cur_dist_dist) {
995     static gchar tmp_buf[20];
996     if ( a_vik_get_units_distance () == VIK_UNITS_DISTANCE_MILES )
997       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", widgets->distances[ix]);
998     else
999       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", widgets->distances[ix]);
1000     gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_dist), tmp_buf);
1001   }
1002
1003   widgets->blob_tp = trackpoint;
1004
1005   if ( widgets->distances == NULL )
1006     return;
1007
1008   GtkWidget *window = gtk_widget_get_toplevel (event_box);
1009   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
1010   GtkWidget *image = GTK_WIDGET(child->data);
1011
1012   gint y_blob = blobby_distance (x, widgets);
1013
1014   gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1015   if (widgets->is_marker_drawn) {
1016     gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
1017     if (!isnan(pc)) {
1018       marker_x = (pc * widgets->profile_width) + MARGIN;
1019     }
1020   }
1021
1022   save_image_and_draw_graph_marks (image,
1023                                    marker_x,
1024                                    window->style->black_gc,
1025                                    MARGIN+x,
1026                                    y_blob,
1027                                    &widgets->dist_graph_saved_img,
1028                                    widgets->profile_width,
1029                                    widgets->profile_height,
1030                                    &widgets->is_marker_drawn,
1031                                    &widgets->is_blob_drawn);
1032
1033   g_list_free(child);
1034 }
1035
1036 /**
1037  * Update labels and blob marker on mouse moves in the elevation/time graph
1038  */
1039 void track_et_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
1040 {
1041   VikTrack *tr = pass_along[0];
1042   PropWidgets *widgets = pass_along[3];
1043   int mouse_x, mouse_y;
1044   GdkModifierType state;
1045
1046   if (event->is_hint)
1047     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
1048   else
1049     mouse_x = event->x;
1050
1051   gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
1052   if (x < 0)
1053     x = 0;
1054   if (x > widgets->profile_width)
1055     x = widgets->profile_width;
1056
1057   time_t seconds_from_start;
1058   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
1059   if (trackpoint && widgets->w_cur_elev_time) {
1060     static gchar tmp_buf[20];
1061     guint h, m, s;
1062     h = seconds_from_start/3600;
1063     m = (seconds_from_start - h*3600)/60;
1064     s = seconds_from_start - (3600*h) - (60*m);
1065     g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
1066
1067     gtk_label_set_text(GTK_LABEL(widgets->w_cur_elev_time), tmp_buf);
1068   }
1069
1070   gint ix = (gint)x;
1071   // Ensure ix is inbounds
1072   if (ix == widgets->profile_width)
1073     ix--;
1074
1075   if (trackpoint && widgets->w_cur_elev_elev) {
1076     static gchar tmp_buf[20];
1077     if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
1078       g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude));
1079     else
1080       g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude);
1081     gtk_label_set_text(GTK_LABEL(widgets->w_cur_elev_elev), tmp_buf);
1082   }
1083
1084   widgets->blob_tp = trackpoint;
1085
1086   if ( widgets->ats == NULL )
1087     return;
1088
1089   GtkWidget *window = gtk_widget_get_toplevel (event_box);
1090   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
1091   GtkWidget *image = GTK_WIDGET(child->data);
1092
1093   gint y_blob = blobby_altitude_time (x, widgets);
1094
1095   gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1096   if (widgets->is_marker_drawn) {
1097     gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
1098     if (!isnan(pc)) {
1099       marker_x = (pc * widgets->profile_width) + MARGIN;
1100     }
1101   }
1102
1103   save_image_and_draw_graph_marks (image,
1104                                    marker_x,
1105                                    window->style->black_gc,
1106                                    MARGIN+x,
1107                                    y_blob,
1108                                    &widgets->elev_time_graph_saved_img,
1109                                    widgets->profile_width,
1110                                    widgets->profile_height,
1111                                    &widgets->is_marker_drawn,
1112                                    &widgets->is_blob_drawn);
1113
1114   g_list_free(child);
1115 }
1116
1117 void track_sd_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
1118 {
1119   VikTrack *tr = pass_along[0];
1120   PropWidgets *widgets = pass_along[3];
1121   int mouse_x, mouse_y;
1122   GdkModifierType state;
1123
1124   if (event->is_hint)
1125     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
1126   else
1127     mouse_x = event->x;
1128
1129   gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
1130   if (x < 0)
1131     x = 0;
1132   if (x > widgets->profile_width)
1133     x = widgets->profile_width;
1134
1135   gdouble meters_from_start;
1136   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
1137   if (trackpoint && widgets->w_cur_speed_dist) {
1138     static gchar tmp_buf[20];
1139     vik_units_distance_t dist_units = a_vik_get_units_distance ();
1140     switch (dist_units) {
1141     case VIK_UNITS_DISTANCE_KILOMETRES:
1142       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
1143       break;
1144     case VIK_UNITS_DISTANCE_MILES:
1145       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
1146       break;
1147     default:
1148       g_critical("Houston, we've had a problem. distance=%d", dist_units);
1149     }
1150     gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed_dist), tmp_buf);
1151   }
1152
1153   gint ix = (gint)x;
1154   // Ensure ix is inbounds
1155   if (ix == widgets->profile_width)
1156     ix--;
1157
1158   if ( widgets->speeds_dist == NULL )
1159     return;
1160
1161   // Show track speed for this position
1162   if (widgets->w_cur_speed_speed) {
1163     static gchar tmp_buf[20];
1164     // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown
1165     // No conversions needed as already in appropriate units
1166     vik_units_speed_t speed_units = a_vik_get_units_speed ();
1167     switch (speed_units) {
1168     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1169       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), widgets->speeds_dist[ix]);
1170       break;
1171     case VIK_UNITS_SPEED_MILES_PER_HOUR:
1172       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds_dist[ix]);
1173       break;
1174     case VIK_UNITS_SPEED_KNOTS:
1175       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds_dist[ix]);
1176       break;
1177     default:
1178       // VIK_UNITS_SPEED_METRES_PER_SECOND:
1179       g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds_dist[ix]);
1180       break;
1181     }
1182     gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed_speed), tmp_buf);
1183   }
1184
1185   widgets->blob_tp = trackpoint;
1186
1187   GtkWidget *window = gtk_widget_get_toplevel (event_box);
1188   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
1189   GtkWidget *image = GTK_WIDGET(child->data);
1190
1191   gint y_blob = blobby_speed_dist (x, widgets);
1192
1193   gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1194   if (widgets->is_marker_drawn) {
1195     gdouble pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
1196     if (!isnan(pc)) {
1197       marker_x = (pc * widgets->profile_width) + MARGIN;
1198     }
1199   }
1200
1201   save_image_and_draw_graph_marks (image,
1202                                    marker_x,
1203                                    window->style->black_gc,
1204                                    MARGIN+x,
1205                                    y_blob,
1206                                    &widgets->speed_dist_graph_saved_img,
1207                                    widgets->profile_width,
1208                                    widgets->profile_height,
1209                                    &widgets->is_marker_drawn,
1210                                    &widgets->is_blob_drawn);
1211
1212   g_list_free(child);
1213 }
1214
1215 /**
1216  * Draws DEM points and a respresentative speed on the supplied pixmap
1217  *   (which is the elevations graph)
1218  */
1219 static void draw_dem_alt_speed_dist(VikTrack *tr,
1220                                     GdkDrawable *pix,
1221                                     GdkGC *alt_gc,
1222                                     GdkGC *speed_gc,
1223                                     gdouble alt_offset,
1224                                     gdouble alt_diff,
1225                                     gdouble max_speed_in,
1226                                     gint cia,
1227                                     gint width,
1228                                     gint height,
1229                                     gint margin,
1230                                     gboolean do_dem,
1231                                     gboolean do_speed)
1232 {
1233   GList *iter;
1234   gdouble max_speed = 0;
1235   gdouble total_length = vik_track_get_length_including_gaps(tr);
1236
1237   // Calculate the max speed factor
1238   if (do_speed)
1239     max_speed = max_speed_in * 110 / 100;
1240
1241   gdouble dist = 0;
1242   for (iter = tr->trackpoints->next; iter; iter = iter->next) {
1243     int x;
1244     dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1245                              &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1246     x = (width * dist)/total_length + margin;
1247     if (do_dem) {
1248       gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST);
1249       elev -= alt_offset;
1250       if ( elev != VIK_DEM_INVALID_ELEVATION ) {
1251         // Convert into height units
1252         if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
1253           elev =  VIK_METERS_TO_FEET(elev);
1254         // No conversion needed if already in metres
1255
1256         // consider chunk size
1257         int y_alt = height - ((height * elev)/(chunksa[cia]*LINES) );
1258         gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4);
1259       }
1260     }
1261     if (do_speed) {
1262       // This is just a speed indicator - no actual values can be inferred by user
1263       if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
1264         int y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
1265         gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
1266       }
1267     }
1268   }
1269 }
1270
1271 /**
1272  * Draw just the height profile image
1273  */
1274 static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1275 {
1276   GtkWidget *window;
1277   GdkPixmap *pix;
1278   gdouble mina, maxa;
1279   guint i;
1280
1281   GdkGC *no_alt_info;
1282   GdkColor color;
1283
1284   // Free previous allocation
1285   if ( widgets->altitudes )
1286     g_free ( widgets->altitudes );
1287
1288   widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
1289
1290   if ( widgets->altitudes == NULL )
1291     return;
1292
1293   // Convert into appropriate units
1294   vik_units_height_t height_units = a_vik_get_units_height ();
1295   if ( height_units == VIK_UNITS_HEIGHT_FEET ) {
1296     // Convert altitudes into feet units
1297     for ( i = 0; i < widgets->profile_width; i++ ) {
1298       widgets->altitudes[i] = VIK_METERS_TO_FEET(widgets->altitudes[i]);
1299     }
1300   }
1301   // Otherwise leave in metres
1302
1303   minmax_array(widgets->altitudes, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
1304
1305   get_new_min_and_chunk_index_altitude (widgets->min_altitude, widgets->max_altitude, &widgets->draw_min_altitude, &widgets->cia);
1306
1307   // Assign locally
1308   mina = widgets->draw_min_altitude;
1309   maxa = widgets->max_altitude;
1310
1311   window = gtk_widget_get_toplevel (widgets->elev_box);
1312
1313   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1314
1315   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1316
1317   no_alt_info = gdk_gc_new ( window->window );
1318   gdk_color_parse ( "yellow", &color );
1319   gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
1320
1321   /* clear the image */
1322   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], 
1323                      TRUE, 0, 0, MARGIN, widgets->profile_height);
1324   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], 
1325                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1326   
1327   /* draw grid */
1328   for (i=0; i<=LINES; i++) {
1329     PangoFontDescription *pfd;
1330     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1331     gchar s[32];
1332     int w, h;
1333
1334     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1335     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1336     pango_layout_set_font_description (pl, pfd);
1337     pango_font_description_free (pfd);
1338     switch (height_units) {
1339     case VIK_UNITS_HEIGHT_METRES:
1340       sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
1341       break;
1342     case VIK_UNITS_HEIGHT_FEET:
1343       // NB values already converted into feet
1344       sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
1345       break;
1346     default:
1347       sprintf(s, "--");
1348       g_critical("Houston, we've had a problem. height=%d", height_units);
1349     }
1350     pango_layout_set_text(pl, s, -1);
1351     pango_layout_get_pixel_size (pl, &w, &h);
1352     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, 
1353                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1354
1355     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], 
1356                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1357     g_object_unref ( G_OBJECT ( pl ) );
1358     pl = NULL;
1359   }
1360
1361   /* draw elevations */
1362   for ( i = 0; i < widgets->profile_width; i++ )
1363     if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE )
1364       gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info, 
1365                       i + MARGIN, 0, i + MARGIN, widgets->profile_height );
1366     else 
1367       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], 
1368                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->altitudes[i]-mina)/(chunksa[widgets->cia]*LINES) );
1369
1370   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_dem)) ||
1371        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)) ) {
1372
1373     GdkGC *dem_alt_gc = gdk_gc_new ( window->window );
1374     GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
1375
1376     gdk_color_parse ( "green", &color );
1377     gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
1378
1379     gdk_color_parse ( "red", &color );
1380     gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1381
1382     // Ensure somekind of max speed when not set
1383     if ( widgets->max_speed < 0.01 )
1384       widgets->max_speed = vik_track_get_max_speed(tr);
1385
1386     draw_dem_alt_speed_dist(tr,
1387                             GDK_DRAWABLE(pix),
1388                             dem_alt_gc,
1389                             gps_speed_gc,
1390                             mina,
1391                             maxa - mina,
1392                             widgets->max_speed,
1393                             widgets->cia,
1394                             widgets->profile_width,
1395                             widgets->profile_height,
1396                             MARGIN,
1397                             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_dem)),
1398                             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)));
1399     
1400     g_object_unref ( G_OBJECT(dem_alt_gc) );
1401     g_object_unref ( G_OBJECT(gps_speed_gc) );
1402   }
1403
1404   /* draw border */
1405   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1406
1407   g_object_unref ( G_OBJECT(pix) );
1408   g_object_unref ( G_OBJECT(no_alt_info) );
1409 }
1410
1411 /**
1412  * Draws representative speed on the supplied pixmap
1413  *   (which is the gradients graph)
1414  */
1415 static void draw_speed_dist(VikTrack *tr,
1416                                     GdkDrawable *pix,
1417                                     GdkGC *speed_gc,
1418                                     gdouble max_speed_in,
1419                                     gint width,
1420                                     gint height,
1421                                     gint margin,
1422                                     gboolean do_speed)
1423 {
1424   GList *iter;
1425   gdouble max_speed = 0;
1426   gdouble total_length = vik_track_get_length_including_gaps(tr);
1427
1428   // Calculate the max speed factor
1429   if (do_speed)
1430     max_speed = max_speed_in * 110 / 100;
1431
1432   gdouble dist = 0;
1433   for (iter = tr->trackpoints->next; iter; iter = iter->next) {
1434     int x;
1435     dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1436                              &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1437     x = (width * dist)/total_length + margin;
1438     if (do_speed) {
1439       // This is just a speed indicator - no actual values can be inferred by user
1440       if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
1441         int y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
1442         gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
1443       }
1444     }
1445   }
1446 }
1447
1448 /**
1449  * Draw just the gradient image
1450  */
1451 static void draw_gradients (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1452 {
1453   GtkWidget *window;
1454   GdkPixmap *pix;
1455   gdouble mina;
1456   guint i;
1457
1458   GdkColor color;
1459
1460   // Free previous allocation
1461   if ( widgets->gradients )
1462     g_free ( widgets->gradients );
1463
1464   widgets->gradients = vik_track_make_gradient_map ( tr, widgets->profile_width );
1465
1466   if ( widgets->gradients == NULL )
1467     return;
1468
1469   minmax_array(widgets->gradients, &widgets->min_gradient, &widgets->max_gradient, TRUE, widgets->profile_width);
1470
1471   get_new_min_and_chunk_index_gradient (widgets->min_gradient, widgets->max_gradient, &widgets->draw_min_gradient, &widgets->cig);
1472
1473   // Assign locally
1474   mina = widgets->draw_min_gradient;
1475
1476   window = gtk_widget_get_toplevel (widgets->gradient_box);
1477
1478   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1479
1480   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1481
1482   /* clear the image */
1483   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], 
1484                      TRUE, 0, 0, MARGIN, widgets->profile_height);
1485   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], 
1486                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1487   
1488   /* draw grid */
1489   for (i=0; i<=LINES; i++) {
1490     PangoFontDescription *pfd;
1491     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1492     gchar s[32];
1493     int w, h;
1494
1495     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1496     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1497     pango_layout_set_font_description (pl, pfd);
1498     pango_font_description_free (pfd);
1499     sprintf(s, "%8d%%", (int)(mina + (LINES-i)*chunksg[widgets->cig]));
1500     pango_layout_set_text(pl, s, -1);
1501     pango_layout_get_pixel_size (pl, &w, &h);
1502     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, 
1503                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1504
1505     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], 
1506                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1507     g_object_unref ( G_OBJECT ( pl ) );
1508     pl = NULL;
1509   }
1510
1511   /* draw gradients */
1512   for ( i = 0; i < widgets->profile_width; i++ )
1513       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], 
1514                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->gradients[i]-mina)/(chunksg[widgets->cig]*LINES) );
1515
1516   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_gradient_gps_speed)) ) {
1517     GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
1518
1519     gdk_color_parse ( "red", &color );
1520     gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1521
1522     // Ensure somekind of max speed when not set
1523     if ( widgets->max_speed < 0.01 )
1524       widgets->max_speed = vik_track_get_max_speed(tr);
1525
1526     draw_speed_dist(tr,
1527                             GDK_DRAWABLE(pix),
1528                             gps_speed_gc,
1529                             widgets->max_speed,
1530                             widgets->profile_width,
1531                             widgets->profile_height,
1532                             MARGIN,
1533                             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)));
1534     
1535     g_object_unref ( G_OBJECT(gps_speed_gc) );
1536   }
1537
1538   /* draw border */
1539   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1540
1541   g_object_unref ( G_OBJECT(pix) );
1542 }
1543
1544 /**
1545  * Draw just the speed (velocity)/time image
1546  */
1547 static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
1548 {
1549   GtkWidget *window;
1550   GdkPixmap *pix;
1551   gdouble mins;
1552   guint i;
1553
1554   // Free previous allocation
1555   if ( widgets->speeds )
1556     g_free ( widgets->speeds );
1557
1558   widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
1559   if ( widgets->speeds == NULL )
1560     return;
1561
1562   // Convert into appropriate units
1563   vik_units_speed_t speed_units = a_vik_get_units_speed ();
1564   switch (speed_units) {
1565   case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1566     for ( i = 0; i < widgets->profile_width; i++ ) {
1567       widgets->speeds[i] = VIK_MPS_TO_KPH(widgets->speeds[i]);
1568     }
1569     break;
1570   case VIK_UNITS_SPEED_MILES_PER_HOUR:
1571     for ( i = 0; i < widgets->profile_width; i++ ) {
1572       widgets->speeds[i] = VIK_MPS_TO_MPH(widgets->speeds[i]);
1573     }
1574     break;
1575   case VIK_UNITS_SPEED_KNOTS:
1576     for ( i = 0; i < widgets->profile_width; i++ ) {
1577       widgets->speeds[i] = VIK_MPS_TO_KNOTS(widgets->speeds[i]);
1578     }
1579     break;
1580   default:
1581     // VIK_UNITS_SPEED_METRES_PER_SECOND:
1582     // No need to convert as already in m/s
1583     break;
1584   }
1585
1586   window = gtk_widget_get_toplevel (widgets->speed_box);
1587
1588   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1589
1590   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1591
1592   minmax_array(widgets->speeds, &widgets->min_speed, &widgets->max_speed, FALSE, widgets->profile_width);
1593   if (widgets->min_speed < 0.0)
1594     widgets->min_speed = 0; /* splines sometimes give negative speeds */
1595
1596   /* Find suitable chunk index */
1597   get_new_min_and_chunk_index (widgets->min_speed, widgets->max_speed, chunkss, sizeof(chunkss)/sizeof(chunkss[0]), &widgets->draw_min_speed, &widgets->cis);
1598
1599   // Assign locally
1600   mins = widgets->draw_min_speed;
1601   
1602   /* clear the image */
1603   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], 
1604                      TRUE, 0, 0, MARGIN, widgets->profile_height);
1605   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], 
1606                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1607
1608   /* draw grid */
1609   for (i=0; i<=LINES; i++) {
1610     PangoFontDescription *pfd;
1611     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1612     gchar s[32];
1613     int w, h;
1614
1615     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1616     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1617     pango_layout_set_font_description (pl, pfd);
1618     pango_font_description_free (pfd);
1619     // NB: No need to convert here anymore as numbers are in the appropriate units
1620     switch (speed_units) {
1621     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1622       sprintf(s, "%8dkm/h", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1623       break;
1624     case VIK_UNITS_SPEED_MILES_PER_HOUR:
1625       sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1626       break;
1627     case VIK_UNITS_SPEED_METRES_PER_SECOND:
1628       sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1629       break;
1630     case VIK_UNITS_SPEED_KNOTS:
1631       sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1632       break;
1633     default:
1634       sprintf(s, "--");
1635       g_critical("Houston, we've had a problem. speed=%d", speed_units);
1636     }
1637
1638     pango_layout_set_text(pl, s, -1);
1639     pango_layout_get_pixel_size (pl, &w, &h);
1640     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, 
1641                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1642
1643     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], 
1644                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1645     g_object_unref ( G_OBJECT ( pl ) );
1646     pl = NULL;
1647   }
1648   
1649
1650   /* draw speeds */
1651   for ( i = 0; i < widgets->profile_width; i++ )
1652       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], 
1653                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(chunkss[widgets->cis]*LINES) );
1654
1655
1656   if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_gps_speed)) ) {
1657
1658     GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
1659     GdkColor color;
1660     gdk_color_parse ( "red", &color );
1661     gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1662
1663     time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1664     time_t dur =  VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
1665
1666     GList *iter;
1667     for (iter = tr->trackpoints; iter; iter = iter->next) {
1668       gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
1669       if (isnan(gps_speed))
1670         continue;
1671       switch (speed_units) {
1672       case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1673         gps_speed = VIK_MPS_TO_KPH(gps_speed);
1674         break;
1675       case VIK_UNITS_SPEED_MILES_PER_HOUR:
1676         gps_speed = VIK_MPS_TO_MPH(gps_speed);
1677         break;
1678       case VIK_UNITS_SPEED_KNOTS:
1679         gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
1680         break;
1681       default:
1682         // VIK_UNITS_SPEED_METRES_PER_SECOND:
1683         // No need to convert as already in m/s
1684         break;
1685       }
1686       int x = MARGIN + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
1687       int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cis]*LINES);
1688       gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
1689     }
1690     g_object_unref ( G_OBJECT(gps_speed_gc) );
1691   }
1692
1693   /* draw border */
1694   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1695
1696   g_object_unref ( G_OBJECT(pix) );
1697 }
1698
1699 /**
1700  * Draw just the distance/time image
1701  */
1702 static void draw_dt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1703 {
1704   GtkWidget *window;
1705   GdkPixmap *pix;
1706   gdouble maxd;
1707   guint i;
1708
1709   // Free previous allocation
1710   if ( widgets->distances )
1711     g_free ( widgets->distances );
1712
1713   widgets->distances = vik_track_make_distance_map ( tr, widgets->profile_width );
1714   if ( widgets->distances == NULL )
1715     return;
1716
1717   // Convert into appropriate units
1718   vik_units_distance_t dist_units = a_vik_get_units_distance ();
1719   if ( dist_units == VIK_UNITS_DISTANCE_MILES ) {
1720     for ( i = 0; i < widgets->profile_width; i++ ) {
1721       widgets->distances[i] = VIK_METERS_TO_MILES(widgets->distances[i]);
1722     }
1723   }
1724   else {
1725     // Metres - but want in kms
1726     for ( i = 0; i < widgets->profile_width; i++ ) {
1727       widgets->distances[i] = widgets->distances[i]/1000.0;
1728     }
1729   }
1730
1731   window = gtk_widget_get_toplevel (widgets->dist_box);
1732
1733   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1734
1735   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1736
1737   // easy to work out min / max of distance!
1738   // Assign locally
1739   // mind = 0.0; - Thus not used
1740   if ( dist_units == VIK_UNITS_DISTANCE_MILES )
1741     maxd = VIK_METERS_TO_MILES(vik_track_get_length_including_gaps (tr));
1742   else
1743     maxd = vik_track_get_length_including_gaps (tr) / 1000.0;
1744
1745   /* Find suitable chunk index */
1746   gdouble dummy = 0.0; // expect this to remain the same! (not that it's used)
1747   get_new_min_and_chunk_index (0, maxd, chunksd, sizeof(chunksd)/sizeof(chunksd[0]), &dummy, &widgets->cid);
1748
1749   /* clear the image */
1750   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1751                      TRUE, 0, 0, MARGIN, widgets->profile_height);
1752   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1753                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1754
1755   /* draw grid */
1756   for (i=0; i<=LINES; i++) {
1757     PangoFontDescription *pfd;
1758     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1759     gchar s[32];
1760     int w, h;
1761
1762     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1763     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1764     pango_layout_set_font_description (pl, pfd);
1765     pango_font_description_free (pfd);
1766     if ( dist_units == VIK_UNITS_DISTANCE_MILES )
1767       sprintf(s, _("%.1f miles"), ((LINES-i)*chunksd[widgets->cid]));
1768     else
1769       sprintf(s, _("%.1f km"), ((LINES-i)*chunksd[widgets->cid]));
1770
1771     pango_layout_set_text(pl, s, -1);
1772     pango_layout_get_pixel_size (pl, &w, &h);
1773     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
1774                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1775
1776     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
1777                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1778     g_object_unref ( G_OBJECT ( pl ) );
1779     pl = NULL;
1780   }
1781   
1782   /* draw distance */
1783   for ( i = 0; i < widgets->profile_width; i++ )
1784       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
1785                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->distances[i])/(chunksd[widgets->cid]*LINES) );
1786
1787   // Show speed indicator
1788   if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_dist_speed)) ) {
1789     GdkGC *dist_speed_gc = gdk_gc_new ( window->window );
1790     GdkColor color;
1791     gdk_color_parse ( "red", &color );
1792     gdk_gc_set_rgb_fg_color ( dist_speed_gc, &color);
1793
1794     gdouble max_speed = 0;
1795     max_speed = widgets->max_speed * 110 / 100;
1796
1797     // This is just an indicator - no actual values can be inferred by user
1798     gint i;
1799     for ( i = 0; i < widgets->profile_width; i++ ) {
1800       int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
1801       gdk_draw_rectangle(GDK_DRAWABLE(pix), dist_speed_gc, TRUE, i+MARGIN-2, y_speed-2, 4, 4);
1802     }
1803     g_object_unref ( G_OBJECT(dist_speed_gc) );
1804   }
1805
1806   /* draw border */
1807   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1808
1809   g_object_unref ( G_OBJECT(pix) );
1810
1811 }
1812
1813 /**
1814  * Draw just the elevation/time image
1815  */
1816 static void draw_et ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1817 {
1818   GtkWidget *window;
1819   GdkPixmap *pix;
1820   gdouble mina;
1821   guint i;
1822
1823   // Free previous allocation
1824   if ( widgets->ats )
1825     g_free ( widgets->ats );
1826
1827   widgets->ats = vik_track_make_elevation_time_map ( tr, widgets->profile_width );
1828
1829   if ( widgets->ats == NULL )
1830     return;
1831
1832   // Convert into appropriate units
1833   vik_units_height_t height_units = a_vik_get_units_height ();
1834   if ( height_units == VIK_UNITS_HEIGHT_FEET ) {
1835     // Convert altitudes into feet units
1836     for ( i = 0; i < widgets->profile_width; i++ ) {
1837       widgets->ats[i] = VIK_METERS_TO_FEET(widgets->ats[i]);
1838     }
1839   }
1840   // Otherwise leave in metres
1841
1842   minmax_array(widgets->ats, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
1843
1844   get_new_min_and_chunk_index_altitude (widgets->min_altitude, widgets->max_altitude, &widgets->draw_min_altitude_time, &widgets->ciat);
1845
1846   // Assign locally
1847   mina = widgets->draw_min_altitude_time;
1848
1849   window = gtk_widget_get_toplevel (widgets->elev_time_box);
1850
1851   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1852
1853   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1854
1855   /* clear the image */
1856   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1857                      TRUE, 0, 0, MARGIN, widgets->profile_height);
1858   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1859                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1860
1861   /* draw grid */
1862   for (i=0; i<=LINES; i++) {
1863     PangoFontDescription *pfd;
1864     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1865     gchar s[32];
1866     int w, h;
1867
1868     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1869     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1870     pango_layout_set_font_description (pl, pfd);
1871     pango_font_description_free (pfd);
1872     switch (height_units) {
1873     case VIK_UNITS_HEIGHT_METRES:
1874       sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
1875       break;
1876     case VIK_UNITS_HEIGHT_FEET:
1877       // NB values already converted into feet
1878       sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
1879       break;
1880     default:
1881       sprintf(s, "--");
1882       g_critical("Houston, we've had a problem. height=%d", height_units);
1883     }
1884     pango_layout_set_text(pl, s, -1);
1885     pango_layout_get_pixel_size (pl, &w, &h);
1886     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
1887                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1888
1889     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
1890                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1891     g_object_unref ( G_OBJECT ( pl ) );
1892     pl = NULL;
1893   }
1894
1895   /* draw elevations */
1896   for ( i = 0; i < widgets->profile_width; i++ )
1897       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
1898                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->ats[i]-mina)/(chunksa[widgets->ciat]*LINES) );
1899
1900   // Show speed indicator
1901   if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_elev_speed)) ) {
1902     GdkGC *elev_speed_gc = gdk_gc_new ( window->window );
1903     GdkColor color;
1904     gdk_color_parse ( "red", &color );
1905     gdk_gc_set_rgb_fg_color ( elev_speed_gc, &color);
1906
1907     gdouble max_speed = 0;
1908     max_speed = widgets->max_speed * 110 / 100;
1909
1910     // This is just an indicator - no actual values can be inferred by user
1911     gint i;
1912     for ( i = 0; i < widgets->profile_width; i++ ) {
1913       int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
1914       gdk_draw_rectangle(GDK_DRAWABLE(pix), elev_speed_gc, TRUE, i+MARGIN-2, y_speed-2, 4, 4);
1915     }
1916     g_object_unref ( G_OBJECT(elev_speed_gc) );
1917   }
1918
1919   /* draw border */
1920   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1921
1922   g_object_unref ( G_OBJECT(pix) );
1923
1924 }
1925
1926 /**
1927  * Draw just the speed/distance image
1928  */
1929 static void draw_sd ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
1930 {
1931   GtkWidget *window;
1932   GdkPixmap *pix;
1933   gdouble mins;
1934   guint i;
1935
1936   // Free previous allocation
1937   if ( widgets->speeds_dist )
1938     g_free ( widgets->speeds_dist );
1939
1940   widgets->speeds_dist = vik_track_make_speed_dist_map ( tr, widgets->profile_width );
1941   if ( widgets->speeds_dist == NULL )
1942     return;
1943
1944   // Convert into appropriate units
1945   vik_units_speed_t speed_units = a_vik_get_units_speed ();
1946   switch (speed_units) {
1947   case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1948     for ( i = 0; i < widgets->profile_width; i++ ) {
1949       widgets->speeds_dist[i] = VIK_MPS_TO_KPH(widgets->speeds_dist[i]);
1950     }
1951     break;
1952   case VIK_UNITS_SPEED_MILES_PER_HOUR:
1953     for ( i = 0; i < widgets->profile_width; i++ ) {
1954       widgets->speeds_dist[i] = VIK_MPS_TO_MPH(widgets->speeds_dist[i]);
1955     }
1956     break;
1957   case VIK_UNITS_SPEED_KNOTS:
1958     for ( i = 0; i < widgets->profile_width; i++ ) {
1959       widgets->speeds_dist[i] = VIK_MPS_TO_KNOTS(widgets->speeds_dist[i]);
1960     }
1961     break;
1962   default:
1963     // VIK_UNITS_SPEED_METRES_PER_SECOND:
1964     // No need to convert as already in m/s
1965     break;
1966   }
1967
1968   window = gtk_widget_get_toplevel (widgets->speed_dist_box);
1969
1970   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1971
1972   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1973
1974   // OK to resuse min_speed here
1975   minmax_array(widgets->speeds_dist, &widgets->min_speed, &widgets->max_speed_dist, FALSE, widgets->profile_width);
1976   if (widgets->min_speed < 0.0)
1977     widgets->min_speed = 0; /* splines sometimes give negative speeds */
1978
1979   /* Find suitable chunk index */
1980   get_new_min_and_chunk_index (widgets->min_speed, widgets->max_speed_dist, chunkss, sizeof(chunkss)/sizeof(chunkss[0]), &widgets->draw_min_speed, &widgets->cisd);
1981
1982   // Assign locally
1983   mins = widgets->draw_min_speed;
1984   
1985   /* clear the image */
1986   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1987                      TRUE, 0, 0, MARGIN, widgets->profile_height);
1988   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1989                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1990
1991   /* draw grid */
1992   for (i=0; i<=LINES; i++) {
1993     PangoFontDescription *pfd;
1994     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1995     gchar s[32];
1996     int w, h;
1997
1998     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1999     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
2000     pango_layout_set_font_description (pl, pfd);
2001     pango_font_description_free (pfd);
2002     // NB: No need to convert here anymore as numbers are in the appropriate units
2003     switch (speed_units) {
2004     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2005       sprintf(s, "%8dkm/h", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
2006       break;
2007     case VIK_UNITS_SPEED_MILES_PER_HOUR:
2008       sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
2009       break;
2010     case VIK_UNITS_SPEED_METRES_PER_SECOND:
2011       sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
2012       break;
2013     case VIK_UNITS_SPEED_KNOTS:
2014       sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
2015       break;
2016     default:
2017       sprintf(s, "--");
2018       g_critical("Houston, we've had a problem. speed=%d", speed_units);
2019     }
2020
2021     pango_layout_set_text(pl, s, -1);
2022     pango_layout_get_pixel_size (pl, &w, &h);
2023     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
2024                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
2025
2026     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
2027                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
2028     g_object_unref ( G_OBJECT ( pl ) );
2029     pl = NULL;
2030   }
2031   
2032
2033   /* draw speeds */
2034   for ( i = 0; i < widgets->profile_width; i++ )
2035       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
2036                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds_dist[i]-mins)/(chunkss[widgets->cisd]*LINES) );
2037
2038
2039   if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_sd_gps_speed)) ) {
2040
2041     GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
2042     GdkColor color;
2043     gdk_color_parse ( "red", &color );
2044     gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
2045
2046     gdouble dist = vik_track_get_length_including_gaps(tr);
2047     gdouble dist_tp = 0.0;
2048
2049     GList *iter = tr->trackpoints;
2050     for (iter = iter->next; iter; iter = iter->next) {
2051       gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
2052       if (isnan(gps_speed))
2053         continue;
2054       switch (speed_units) {
2055       case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2056         gps_speed = VIK_MPS_TO_KPH(gps_speed);
2057         break;
2058       case VIK_UNITS_SPEED_MILES_PER_HOUR:
2059         gps_speed = VIK_MPS_TO_MPH(gps_speed);
2060         break;
2061       case VIK_UNITS_SPEED_KNOTS:
2062         gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
2063         break;
2064       default:
2065         // VIK_UNITS_SPEED_METRES_PER_SECOND:
2066         // No need to convert as already in m/s
2067         break;
2068       }
2069       dist_tp += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
2070       int x = MARGIN + (widgets->profile_width * dist_tp / dist);
2071       int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cisd]*LINES);
2072       gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
2073     }
2074     g_object_unref ( G_OBJECT(gps_speed_gc) );
2075   }
2076
2077   /* draw border */
2078   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
2079
2080   g_object_unref ( G_OBJECT(pix) );
2081 }
2082 #undef LINES
2083
2084 /**
2085  * Draw all graphs
2086  */
2087 static void draw_all_graphs ( GtkWidget *widget, gpointer *pass_along, gboolean resized )
2088 {
2089   VikTrack *tr = pass_along[0];
2090   PropWidgets *widgets = pass_along[3];
2091
2092   // Draw graphs even if they are not visible
2093
2094   GList *child = NULL;
2095   GtkWidget *image = NULL;
2096   GtkWidget *window = gtk_widget_get_toplevel(widget);
2097   gdouble pc = NAN;
2098   gdouble pc_blob = NAN;
2099
2100   // Draw elevations
2101   if (widgets->elev_box != NULL) {
2102
2103     // Saved image no longer any good as we've resized, so we remove it here
2104     if (resized && widgets->elev_graph_saved_img.img) {
2105       g_object_unref(widgets->elev_graph_saved_img.img);
2106       widgets->elev_graph_saved_img.img = NULL;
2107       widgets->elev_graph_saved_img.saved = FALSE;
2108     }
2109
2110     child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box));
2111     draw_elevations (GTK_WIDGET(child->data), tr, widgets );
2112
2113     image = GTK_WIDGET(child->data);
2114     g_list_free(child);
2115
2116     // Ensure marker or blob are redrawn if necessary
2117     if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2118
2119       pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
2120       gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2121       gint y_blob = 0;
2122       if (widgets->is_blob_drawn) {
2123         pc_blob = tp_percentage_by_distance ( tr, widgets->blob_tp, widgets->track_length_inc_gaps );
2124         if (!isnan(pc_blob)) {
2125           x_blob = (pc_blob * widgets->profile_width);
2126         }
2127         y_blob = blobby_altitude (x_blob, widgets);
2128       }
2129
2130       gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2131       if (!isnan(pc)) {
2132         marker_x = (pc * widgets->profile_width) + MARGIN;
2133       }
2134
2135       save_image_and_draw_graph_marks (image,
2136                                        marker_x,
2137                                        window->style->black_gc,
2138                                        x_blob+MARGIN,
2139                                        y_blob,
2140                                        &widgets->elev_graph_saved_img,
2141                                        widgets->profile_width,
2142                                        widgets->profile_height,
2143                                        &widgets->is_marker_drawn,
2144                                        &widgets->is_blob_drawn);
2145     }
2146   }
2147
2148   // Draw gradients
2149   if (widgets->gradient_box != NULL) {
2150
2151     // Saved image no longer any good as we've resized, so we remove it here
2152     if (resized && widgets->gradient_graph_saved_img.img) {
2153       g_object_unref(widgets->gradient_graph_saved_img.img);
2154       widgets->gradient_graph_saved_img.img = NULL;
2155       widgets->gradient_graph_saved_img.saved = FALSE;
2156     }
2157
2158     child = gtk_container_get_children(GTK_CONTAINER(widgets->gradient_box));
2159     draw_gradients (GTK_WIDGET(child->data), tr, widgets );
2160
2161     image = GTK_WIDGET(child->data);
2162     g_list_free(child);
2163
2164     // Ensure marker or blob are redrawn if necessary
2165     if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2166
2167       pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
2168       gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2169       gint y_blob = 0;
2170       if (widgets->is_blob_drawn) {
2171         pc_blob = tp_percentage_by_distance ( tr, widgets->blob_tp, widgets->track_length_inc_gaps );
2172         if (!isnan(pc_blob)) {
2173           x_blob = (pc_blob * widgets->profile_width);
2174         }
2175         y_blob = blobby_gradient (x_blob, widgets);
2176       }
2177
2178       gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2179       if (!isnan(pc)) {
2180         marker_x = (pc * widgets->profile_width) + MARGIN;
2181       }
2182
2183       save_image_and_draw_graph_marks (image,
2184                                        marker_x,
2185                                        window->style->black_gc,
2186                                        x_blob+MARGIN,
2187                                        y_blob,
2188                                        &widgets->gradient_graph_saved_img,
2189                                        widgets->profile_width,
2190                                        widgets->profile_height,
2191                                        &widgets->is_marker_drawn,
2192                                        &widgets->is_blob_drawn);
2193     }
2194   }
2195
2196   // Draw speeds
2197   if (widgets->speed_box != NULL) {
2198
2199     // Saved image no longer any good as we've resized
2200     if (resized && widgets->speed_graph_saved_img.img) {
2201       g_object_unref(widgets->speed_graph_saved_img.img);
2202       widgets->speed_graph_saved_img.img = NULL;
2203       widgets->speed_graph_saved_img.saved = FALSE;
2204     }
2205
2206     child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box));
2207     draw_vt (GTK_WIDGET(child->data), tr, widgets );
2208
2209     image = GTK_WIDGET(child->data);
2210     g_list_free(child);
2211
2212     // Ensure marker or blob are redrawn if necessary
2213     if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2214
2215       pc = tp_percentage_by_time ( tr, widgets->marker_tp );
2216
2217       gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2218       gint    y_blob = 0;
2219       if (widgets->is_blob_drawn) {
2220         pc_blob = tp_percentage_by_time ( tr, widgets->blob_tp );
2221         if (!isnan(pc_blob)) {
2222           x_blob = (pc_blob * widgets->profile_width);
2223         }
2224          
2225         y_blob = blobby_speed (x_blob, widgets);
2226       }
2227
2228       gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2229       if (!isnan(pc)) {
2230         marker_x = (pc * widgets->profile_width) + MARGIN;
2231       }
2232
2233       save_image_and_draw_graph_marks (image,
2234                                        marker_x,
2235                                        window->style->black_gc,
2236                                        x_blob+MARGIN,
2237                                        y_blob,
2238                                        &widgets->speed_graph_saved_img,
2239                                        widgets->profile_width,
2240                                        widgets->profile_height,
2241                                        &widgets->is_marker_drawn,
2242                                        &widgets->is_blob_drawn);
2243     }
2244   }
2245
2246   // Draw Distances
2247   if (widgets->dist_box != NULL) {
2248
2249     // Saved image no longer any good as we've resized
2250     if (resized && widgets->dist_graph_saved_img.img) {
2251       g_object_unref(widgets->dist_graph_saved_img.img);
2252       widgets->dist_graph_saved_img.img = NULL;
2253       widgets->dist_graph_saved_img.saved = FALSE;
2254     }
2255
2256     child = gtk_container_get_children(GTK_CONTAINER(widgets->dist_box));
2257     draw_dt (GTK_WIDGET(child->data), tr, widgets );
2258
2259     image = GTK_WIDGET(child->data);
2260     g_list_free(child);
2261
2262     // Ensure marker or blob are redrawn if necessary
2263     if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2264
2265       pc = tp_percentage_by_time ( tr, widgets->marker_tp );
2266
2267       gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2268       gint    y_blob = 0;
2269       if (widgets->is_blob_drawn) {
2270         pc_blob = tp_percentage_by_time ( tr, widgets->blob_tp );
2271         if (!isnan(pc_blob)) {
2272           x_blob = (pc_blob * widgets->profile_width);
2273         }
2274          
2275         y_blob = blobby_distance (x_blob, widgets);
2276       }
2277
2278       gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2279       if (!isnan(pc)) {
2280         marker_x = (pc * widgets->profile_width) + MARGIN;
2281       }
2282
2283       save_image_and_draw_graph_marks (image,
2284                                        marker_x,
2285                                        window->style->black_gc,
2286                                        x_blob+MARGIN,
2287                                        y_blob,
2288                                        &widgets->dist_graph_saved_img,
2289                                        widgets->profile_width,
2290                                        widgets->profile_height,
2291                                        &widgets->is_marker_drawn,
2292                                        &widgets->is_blob_drawn);
2293     }
2294   }
2295
2296   // Draw Elevations in timely manner
2297   if (widgets->elev_time_box != NULL) {
2298
2299     // Saved image no longer any good as we've resized
2300     if (resized && widgets->elev_time_graph_saved_img.img) {
2301       g_object_unref(widgets->elev_time_graph_saved_img.img);
2302       widgets->elev_time_graph_saved_img.img = NULL;
2303       widgets->elev_time_graph_saved_img.saved = FALSE;
2304     }
2305
2306     child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_time_box));
2307     draw_et (GTK_WIDGET(child->data), tr, widgets );
2308
2309     image = GTK_WIDGET(child->data);
2310     g_list_free(child);
2311
2312     // Ensure marker or blob are redrawn if necessary
2313     if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2314
2315       pc = tp_percentage_by_time ( tr, widgets->marker_tp );
2316
2317       gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2318       gint    y_blob = 0;
2319       if (widgets->is_blob_drawn) {
2320         pc_blob = tp_percentage_by_time ( tr, widgets->blob_tp );
2321         if (!isnan(pc_blob)) {
2322           x_blob = (pc_blob * widgets->profile_width);
2323         }
2324         y_blob = blobby_altitude_time (x_blob, widgets);
2325       }
2326
2327       gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2328       if (!isnan(pc)) {
2329         marker_x = (pc * widgets->profile_width) + MARGIN;
2330       }
2331
2332       save_image_and_draw_graph_marks (image,
2333                                        marker_x,
2334                                        window->style->black_gc,
2335                                        x_blob+MARGIN,
2336                                        y_blob,
2337                                        &widgets->elev_time_graph_saved_img,
2338                                        widgets->profile_width,
2339                                        widgets->profile_height,
2340                                        &widgets->is_marker_drawn,
2341                                        &widgets->is_blob_drawn);
2342     }
2343   }
2344
2345   // Draw speed distances
2346   if (widgets->speed_dist_box != NULL) {
2347
2348     // Saved image no longer any good as we've resized, so we remove it here
2349     if (resized && widgets->speed_dist_graph_saved_img.img) {
2350       g_object_unref(widgets->speed_dist_graph_saved_img.img);
2351       widgets->speed_dist_graph_saved_img.img = NULL;
2352       widgets->speed_dist_graph_saved_img.saved = FALSE;
2353     }
2354
2355     child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_dist_box));
2356     draw_sd (GTK_WIDGET(child->data), tr, widgets );
2357
2358     image = GTK_WIDGET(child->data);
2359     g_list_free(child);
2360
2361     // Ensure marker or blob are redrawn if necessary
2362     if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2363
2364       pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
2365       gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2366       gint y_blob = 0;
2367       if (widgets->is_blob_drawn) {
2368         pc_blob = tp_percentage_by_distance ( tr, widgets->blob_tp, widgets->track_length_inc_gaps );
2369         if (!isnan(pc_blob)) {
2370           x_blob = (pc_blob * widgets->profile_width);
2371         }
2372         y_blob = blobby_speed_dist (x_blob, widgets);
2373       }
2374
2375       gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2376       if (!isnan(pc)) {
2377         marker_x = (pc * widgets->profile_width) + MARGIN;
2378       }
2379
2380       save_image_and_draw_graph_marks (image,
2381                                        marker_x,
2382                                        window->style->black_gc,
2383                                        x_blob+MARGIN,
2384                                        y_blob,
2385                                        &widgets->speed_dist_graph_saved_img,
2386                                        widgets->profile_width,
2387                                        widgets->profile_height,
2388                                        &widgets->is_marker_drawn,
2389                                        &widgets->is_blob_drawn);
2390     }
2391   }
2392
2393 }
2394
2395 /**
2396  * Configure/Resize the profile & speed/time images
2397  */
2398 static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, gpointer *pass_along )
2399 {
2400   PropWidgets *widgets = pass_along[3];
2401
2402   if (widgets->configure_dialog) {
2403     // Determine size offsets between dialog size and size for images
2404     // Only on the initialisation of the dialog
2405     widgets->profile_width_offset = event->width - widgets->profile_width;
2406     widgets->profile_height_offset = event->height - widgets->profile_height;
2407     widgets->configure_dialog = FALSE;
2408
2409     // Without this the settting, the dialog will only grow in vertical size - one can not then make it smaller!
2410     gtk_widget_set_size_request ( widget, widgets->profile_width+widgets->profile_width_offset, widgets->profile_height );
2411     // In fact this allows one to compress it a bit more vertically as I don't add on the height offset
2412   }
2413   else {
2414     widgets->profile_width_old = widgets->profile_width;
2415     widgets->profile_height_old = widgets->profile_height;
2416   }
2417
2418   // Now adjust From Dialog size to get image size
2419   widgets->profile_width = event->width - widgets->profile_width_offset;
2420   widgets->profile_height = event->height - widgets->profile_height_offset;
2421
2422   // ATM we receive configure_events when the dialog is moved and so no further action is necessary
2423   if ( !widgets->configure_dialog &&
2424        (widgets->profile_width_old == widgets->profile_width) && (widgets->profile_height_old == widgets->profile_height) )
2425     return FALSE;
2426
2427   // Draw stuff
2428   draw_all_graphs ( widget, pass_along, TRUE );
2429
2430   return FALSE;
2431 }
2432
2433 /**
2434  * Create height profile widgets including the image and callbacks
2435  */
2436 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
2437 {
2438   GdkPixmap *pix;
2439   GtkWidget *image;
2440   GtkWidget *eventbox;
2441   gpointer *pass_along;
2442
2443   // First allocation
2444   widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
2445
2446   if ( widgets->altitudes == NULL ) {
2447     *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
2448     return NULL;
2449   }
2450
2451   minmax_array(widgets->altitudes, min_alt, max_alt, TRUE, widgets->profile_width);
2452   
2453   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2454   image = gtk_image_new_from_pixmap ( pix, NULL );
2455
2456   g_object_unref ( G_OBJECT(pix) );
2457
2458   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2459   pass_along[0] = tr;
2460   pass_along[1] = vlp;
2461   pass_along[2] = vvp;
2462   pass_along[3] = widgets;
2463
2464   eventbox = gtk_event_box_new ();
2465   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
2466   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
2467   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2468   gtk_container_add ( GTK_CONTAINER(eventbox), image );
2469   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
2470
2471   return eventbox;
2472 }
2473
2474 /**
2475  * Create height profile widgets including the image and callbacks
2476  */
2477 GtkWidget *vik_trw_layer_create_gradient ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2478 {
2479   GdkPixmap *pix;
2480   GtkWidget *image;
2481   GtkWidget *eventbox;
2482   gpointer *pass_along;
2483
2484   // First allocation
2485   widgets->gradients = vik_track_make_gradient_map ( tr, widgets->profile_width );
2486
2487   if ( widgets->gradients == NULL ) {
2488     return NULL;
2489   }
2490
2491   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2492   image = gtk_image_new_from_pixmap ( pix, NULL );
2493
2494   g_object_unref ( G_OBJECT(pix) );
2495
2496   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2497   pass_along[0] = tr;
2498   pass_along[1] = vlp;
2499   pass_along[2] = vvp;
2500   pass_along[3] = widgets;
2501
2502   eventbox = gtk_event_box_new ();
2503   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_gradient_click), pass_along );
2504   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_gradient_move), pass_along );
2505   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2506   gtk_container_add ( GTK_CONTAINER(eventbox), image );
2507   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
2508
2509   return eventbox;
2510 }
2511
2512 /**
2513  * Create speed/time widgets including the image and callbacks
2514  */
2515 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2516 {
2517   GdkPixmap *pix;
2518   GtkWidget *image;
2519   GtkWidget *eventbox;
2520   gpointer *pass_along;
2521
2522   // First allocation
2523   widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
2524   if ( widgets->speeds == NULL )
2525     return NULL;
2526
2527   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2528   pass_along[0] = tr;
2529   pass_along[1] = vlp;
2530   pass_along[2] = vvp;
2531   pass_along[3] = widgets;
2532
2533   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2534   image = gtk_image_new_from_pixmap ( pix, NULL );
2535
2536 #if 0
2537   /* XXX this can go out, it's just a helpful dev tool */
2538   {
2539     int j;
2540     GdkGC **colors[8] = { window->style->bg_gc,
2541                           window->style->fg_gc,
2542                           window->style->light_gc,
2543                           window->style->dark_gc,
2544                           window->style->mid_gc,
2545                           window->style->text_gc,
2546                           window->style->base_gc,
2547                           window->style->text_aa_gc };
2548     for (i=0; i<5; i++) {
2549       for (j=0; j<8; j++) {
2550         gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
2551                            TRUE, i*20, j*20, 20, 20);
2552         gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
2553                            FALSE, i*20, j*20, 20, 20);
2554       }
2555     }
2556   }
2557 #endif
2558
2559   g_object_unref ( G_OBJECT(pix) );
2560
2561   eventbox = gtk_event_box_new ();
2562   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along );
2563   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along );
2564   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2565   gtk_container_add ( GTK_CONTAINER(eventbox), image );
2566   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2567
2568   return eventbox;
2569 }
2570
2571 /**
2572  * Create distance / time widgets including the image and callbacks
2573  */
2574 GtkWidget *vik_trw_layer_create_dtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2575 {
2576   GdkPixmap *pix;
2577   GtkWidget *image;
2578   GtkWidget *eventbox;
2579   gpointer *pass_along;
2580
2581   // First allocation
2582   widgets->distances = vik_track_make_distance_map ( tr, widgets->profile_width );
2583   if ( widgets->distances == NULL )
2584     return NULL;
2585
2586   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2587   pass_along[0] = tr;
2588   pass_along[1] = vlp;
2589   pass_along[2] = vvp;
2590   pass_along[3] = widgets;
2591
2592   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2593   image = gtk_image_new_from_pixmap ( pix, NULL );
2594
2595   g_object_unref ( G_OBJECT(pix) );
2596
2597   eventbox = gtk_event_box_new ();
2598   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_dt_click), pass_along );
2599   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_dt_move), pass_along );
2600   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2601   gtk_container_add ( GTK_CONTAINER(eventbox), image );
2602   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2603
2604   return eventbox;
2605 }
2606
2607 /**
2608  * Create elevation / time widgets including the image and callbacks
2609  */
2610 GtkWidget *vik_trw_layer_create_etdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2611 {
2612   GdkPixmap *pix;
2613   GtkWidget *image;
2614   GtkWidget *eventbox;
2615   gpointer *pass_along;
2616
2617   // First allocation
2618   widgets->ats = vik_track_make_elevation_time_map ( tr, widgets->profile_width );
2619   if ( widgets->ats == NULL )
2620     return NULL;
2621
2622   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2623   pass_along[0] = tr;
2624   pass_along[1] = vlp;
2625   pass_along[2] = vvp;
2626   pass_along[3] = widgets;
2627
2628   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2629   image = gtk_image_new_from_pixmap ( pix, NULL );
2630
2631   g_object_unref ( G_OBJECT(pix) );
2632
2633   eventbox = gtk_event_box_new ();
2634   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_et_click), pass_along );
2635   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_et_move), pass_along );
2636   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2637   gtk_container_add ( GTK_CONTAINER(eventbox), image );
2638   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2639
2640   return eventbox;
2641 }
2642
2643 /**
2644  * Create speed/distance widgets including the image and callbacks
2645  */
2646 GtkWidget *vik_trw_layer_create_sddiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2647 {
2648   GdkPixmap *pix;
2649   GtkWidget *image;
2650   GtkWidget *eventbox;
2651   gpointer *pass_along;
2652
2653   // First allocation
2654   widgets->speeds_dist = vik_track_make_speed_dist_map ( tr, widgets->profile_width );
2655   if ( widgets->speeds_dist == NULL )
2656     return NULL;
2657
2658   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2659   pass_along[0] = tr;
2660   pass_along[1] = vlp;
2661   pass_along[2] = vvp;
2662   pass_along[3] = widgets;
2663
2664   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2665   image = gtk_image_new_from_pixmap ( pix, NULL );
2666
2667   g_object_unref ( G_OBJECT(pix) );
2668
2669   eventbox = gtk_event_box_new ();
2670   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_sd_click), pass_along );
2671   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_sd_move), pass_along );
2672   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2673   gtk_container_add ( GTK_CONTAINER(eventbox), image );
2674   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2675
2676   return eventbox;
2677 }
2678 #undef MARGIN
2679
2680 static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets)
2681 {
2682   VikTrack *tr = widgets->tr;
2683   VikTrwLayer *vtl = widgets->vtl;
2684   gboolean keep_dialog = FALSE;
2685
2686   /* FIXME: check and make sure the track still exists before doing anything to it */
2687   /* Note: destroying diaglog (eg, parent window exit) won't give "response" */
2688   switch (resp) {
2689     case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
2690     case GTK_RESPONSE_REJECT:
2691       break;
2692     case GTK_RESPONSE_ACCEPT:
2693       vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
2694       break;
2695     case VIK_TRW_LAYER_PROPWIN_REVERSE:
2696       vik_track_reverse(tr);
2697       vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
2698       break;
2699     case VIK_TRW_LAYER_PROPWIN_DEL_DUP:
2700       vik_track_remove_dup_points(tr); // NB ignore the returned answer
2701       // As we could have seen the nuber of dulplicates that would be deleted in the properties statistics tab,
2702       //   choose not to inform the user unnecessarily
2703
2704       /* above operation could have deleted current_tp or last_tp */
2705       trw_layer_cancel_tps_of_track ( vtl, tr );
2706       vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
2707       break;
2708     case VIK_TRW_LAYER_PROPWIN_SPLIT:
2709       {
2710         /* get new tracks, add them and then the delete old one. old can still exist on clipboard. */
2711         guint ntracks;
2712         
2713         VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
2714         gchar *new_tr_name;
2715         guint i;
2716         for ( i = 0; i < ntracks; i++ )
2717         {
2718           if ( tracks[i] ) {
2719             new_tr_name = trw_layer_new_unique_sublayer_name ( vtl, VIK_TRW_LAYER_SUBLAYER_TRACK, widgets->tr->name);
2720             vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
2721           }
2722         }
2723         if ( tracks )
2724         {
2725           g_free ( tracks );
2726           /* Don't let track destroy this dialog */
2727           vik_track_clear_property_dialog(tr);
2728           vik_trw_layer_delete_track ( vtl, tr );
2729           vik_layer_emit_update ( VIK_LAYER(vtl), FALSE ); /* chase thru the hoops */
2730         }
2731       }
2732       break;
2733     case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER:
2734       {
2735         GList *iter = tr->trackpoints;
2736         while ((iter = iter->next)) {
2737           if (widgets->marker_tp == VIK_TRACKPOINT(iter->data))
2738             break;
2739         }
2740         if (iter == NULL) {
2741           a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR,
2742                   _("Failed spliting track. Track unchanged"), NULL);
2743           keep_dialog = TRUE;
2744           break;
2745         }
2746
2747         gchar *r_name = trw_layer_new_unique_sublayer_name(vtl, VIK_TRW_LAYER_SUBLAYER_TRACK, widgets->tr->name);
2748         iter->prev->next = NULL;
2749         iter->prev = NULL;
2750         VikTrack *tr_right = vik_track_new();
2751         if ( tr->comment )
2752           vik_track_set_comment ( tr_right, tr->comment );
2753         tr_right->visible = tr->visible;
2754         tr_right->trackpoints = iter;
2755
2756         vik_trw_layer_add_track(vtl, r_name, tr_right);
2757         vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
2758       }
2759       break;
2760     default:
2761       fprintf(stderr, "DEBUG: unknown response\n");
2762       return;
2763   }
2764
2765   /* Keep same behaviour for now: destroy dialog if click on any button */
2766   if (!keep_dialog) {
2767     prop_widgets_free(widgets);
2768     vik_track_clear_property_dialog(tr);
2769     gtk_widget_destroy ( GTK_WIDGET(dialog) );
2770   }
2771 }
2772
2773 /**
2774  * Force a redraw when checkbutton has been toggled to show/hide that information
2775  */
2776 static void checkbutton_toggle_cb ( GtkToggleButton *togglebutton, gpointer *pass_along, gpointer dummy)
2777 {
2778   PropWidgets *widgets = pass_along[3];
2779   // Even though not resized, we'll pretend it is -
2780   //  as this invalidates the saved images (since the image may have changed)
2781   draw_all_graphs ( widgets->dialog, pass_along, TRUE);
2782 }
2783
2784 /**
2785  *  Create the widgets for the given graph tab
2786  */
2787 static GtkWidget *create_graph_page ( GtkWidget *graph,
2788                                       const gchar *markup,
2789                                       GtkWidget *value,
2790                                       const gchar *markup2,
2791                                       GtkWidget *value2,
2792                                       GtkWidget *checkbutton1,
2793                                       gboolean checkbutton1_default,
2794                                       GtkWidget *checkbutton2,
2795                                       gboolean checkbutton2_default )
2796 {
2797   GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 );
2798   GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 );
2799   GtkWidget *label = gtk_label_new (NULL);
2800   GtkWidget *label2 = gtk_label_new (NULL);
2801   gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0);
2802   gtk_label_set_markup ( GTK_LABEL(label), markup );
2803   gtk_label_set_markup ( GTK_LABEL(label2), markup2 );
2804   gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
2805   gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0);
2806   gtk_box_pack_start (GTK_BOX(hbox), label2, FALSE, FALSE, 0);
2807   gtk_box_pack_start (GTK_BOX(hbox), value2, FALSE, FALSE, 0);
2808   if (checkbutton2) {
2809     gtk_box_pack_end (GTK_BOX(hbox), checkbutton2, FALSE, FALSE, 0);
2810     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton2), checkbutton2_default);
2811   }
2812   if (checkbutton1) {
2813     gtk_box_pack_end (GTK_BOX(hbox), checkbutton1, FALSE, FALSE, 0);
2814     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton1), checkbutton1_default);
2815   }
2816   gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
2817
2818   return vbox;
2819 }
2820
2821 void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, VikViewport *vvp )
2822 {
2823   /* FIXME: free widgets when destroy signal received */
2824   PropWidgets *widgets = prop_widgets_new();
2825   widgets->vtl = vtl;
2826   widgets->tr = tr;
2827   widgets->profile_width  = PROPWIN_PROFILE_WIDTH;
2828   widgets->profile_height = PROPWIN_PROFILE_HEIGHT;
2829   gchar *title = g_strdup_printf(_("%s - Track Properties"), tr->name);
2830   GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
2831                          parent,
2832                          GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
2833                          GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
2834                          _("Split at _Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER,
2835                          _("Split _Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT,
2836                          _("_Reverse"),        VIK_TRW_LAYER_PROPWIN_REVERSE,
2837                          _("_Delete Dupl."),   VIK_TRW_LAYER_PROPWIN_DEL_DUP,
2838                          GTK_STOCK_OK,     GTK_RESPONSE_ACCEPT,
2839                          NULL);
2840   widgets->dialog = dialog;
2841   g_free(title);
2842   g_signal_connect(dialog, "response", G_CALLBACK(propwin_response_cb), widgets);
2843   GtkTable *table;
2844   gdouble tr_len;
2845   guint32 tp_count, seg_count;
2846
2847   gdouble min_alt, max_alt;
2848   widgets->elev_box = vik_trw_layer_create_profile(GTK_WIDGET(parent), tr, vlp, vvp, widgets, &min_alt, &max_alt);
2849   widgets->gradient_box = vik_trw_layer_create_gradient(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2850   widgets->speed_box = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2851   widgets->dist_box = vik_trw_layer_create_dtdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2852   widgets->elev_time_box = vik_trw_layer_create_etdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2853   widgets->speed_dist_box = vik_trw_layer_create_sddiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2854   GtkWidget *graphs = gtk_notebook_new();
2855
2856   GtkWidget *content[20];
2857   int cnt;
2858   int i;
2859
2860   static gchar *label_texts[] = { N_("<b>Comment:</b>"), N_("<b>Track Length:</b>"), N_("<b>Trackpoints:</b>"), N_("<b>Segments:</b>"), N_("<b>Duplicate Points:</b>"), N_("<b>Max Speed:</b>"), N_("<b>Avg. Speed:</b>"), N_("<b>Moving Avg. Speed:</b>"), N_("<b>Avg. Dist. Between TPs:</b>"), N_("<b>Elevation Range:</b>"), N_("<b>Total Elevation Gain/Loss:</b>"), N_("<b>Start:</b>"), N_("<b>End:</b>"), N_("<b>Duration:</b>") };
2861   static gchar tmp_buf[50];
2862   gdouble tmp_speed;
2863
2864   cnt = 0;
2865   widgets->w_comment = gtk_entry_new ();
2866   if ( tr->comment )
2867     gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment );
2868   g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
2869   content[cnt++] = widgets->w_comment;
2870
2871   vik_units_distance_t dist_units = a_vik_get_units_distance ();
2872
2873   // NB This value not shown yet - but is used by internal calculations
2874   widgets->track_length_inc_gaps = vik_track_get_length_including_gaps(tr);
2875
2876   tr_len = widgets->track_length = vik_track_get_length(tr);
2877   switch (dist_units) {
2878   case VIK_UNITS_DISTANCE_KILOMETRES:
2879     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", tr_len/1000.0 );
2880     break;
2881   case VIK_UNITS_DISTANCE_MILES:
2882     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(tr_len) );
2883     break;
2884   default:
2885     g_critical("Houston, we've had a problem. distance=%d", dist_units);
2886   }
2887   widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf );
2888
2889   tp_count = vik_track_get_tp_count(tr);
2890   g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
2891   widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf );
2892
2893   seg_count = vik_track_get_segment_count(tr) ;
2894   g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
2895   widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf );
2896
2897   g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
2898   widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf );
2899
2900   vik_units_speed_t speed_units = a_vik_get_units_speed ();
2901   tmp_speed = vik_track_get_max_speed(tr);
2902   if ( tmp_speed == 0 )
2903     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2904   else {
2905     switch (speed_units) {
2906     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2907       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
2908       break;
2909     case VIK_UNITS_SPEED_MILES_PER_HOUR:
2910       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
2911       break;
2912     case VIK_UNITS_SPEED_METRES_PER_SECOND:
2913       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2914       break;
2915     case VIK_UNITS_SPEED_KNOTS:
2916       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
2917       break;
2918     default:
2919       g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2920       g_critical("Houston, we've had a problem. speed=%d", speed_units);
2921     }
2922   }
2923   widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf );
2924
2925   tmp_speed = vik_track_get_average_speed(tr);
2926   if ( tmp_speed == 0 )
2927     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2928   else {
2929     switch (speed_units) {
2930     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2931       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
2932       break;
2933     case VIK_UNITS_SPEED_MILES_PER_HOUR:
2934       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
2935       break;
2936     case VIK_UNITS_SPEED_METRES_PER_SECOND:
2937       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2938       break;
2939     case VIK_UNITS_SPEED_KNOTS:
2940       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
2941       break;
2942     default:
2943       g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2944       g_critical("Houston, we've had a problem. speed=%d", speed_units);
2945     }
2946   }
2947   widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
2948
2949   // Use 60sec as the default period to be considered stopped
2950   //  this is the TrackWaypoint draw stops default value 'vtl->stop_length'
2951   //  however this variable is not directly accessible - and I don't expect it's often changed from the default
2952   //  so ATM just put in the number
2953   tmp_speed = vik_track_get_average_speed_moving(tr, 60);
2954   if ( tmp_speed == 0 )
2955     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2956   else {
2957     switch (speed_units) {
2958     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2959       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
2960       break;
2961     case VIK_UNITS_SPEED_MILES_PER_HOUR:
2962       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
2963       break;
2964     case VIK_UNITS_SPEED_METRES_PER_SECOND:
2965       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2966       break;
2967     case VIK_UNITS_SPEED_KNOTS:
2968       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
2969       break;
2970     default:
2971       g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2972       g_critical("Houston, we've had a problem. speed=%d", speed_units);
2973     }
2974   }
2975   widgets->w_mvg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
2976
2977   switch (dist_units) {
2978   case VIK_UNITS_DISTANCE_KILOMETRES:
2979     // Even though kilometres, the average distance between points is going to be quite small so keep in metres
2980     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
2981     break;
2982   case VIK_UNITS_DISTANCE_MILES:
2983     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.3f miles", (tp_count - seg_count) == 0 ? 0 : VIK_METERS_TO_MILES(tr_len / ( tp_count - seg_count )) );
2984     break;
2985   default:
2986     g_critical("Houston, we've had a problem. distance=%d", dist_units);
2987   }
2988   widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf );
2989
2990   vik_units_height_t height_units = a_vik_get_units_height ();
2991   if ( min_alt == VIK_DEFAULT_ALTITUDE )
2992     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2993   else {
2994     switch (height_units) {
2995     case VIK_UNITS_HEIGHT_METRES:
2996       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
2997       break;
2998     case VIK_UNITS_HEIGHT_FEET:
2999       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet - %.0f feet", VIK_METERS_TO_FEET(min_alt), VIK_METERS_TO_FEET(max_alt) );
3000       break;
3001     default:
3002       g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
3003       g_critical("Houston, we've had a problem. height=%d", height_units);
3004     }
3005   }
3006   widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf );
3007
3008   vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
3009   if ( min_alt == VIK_DEFAULT_ALTITUDE )
3010     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
3011   else {
3012     switch (height_units) {
3013     case VIK_UNITS_HEIGHT_METRES:
3014       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
3015       break;
3016     case VIK_UNITS_HEIGHT_FEET:
3017       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet / %.0f feet", VIK_METERS_TO_FEET(max_alt), VIK_METERS_TO_FEET(min_alt) );
3018       break;
3019     default:
3020       g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
3021       g_critical("Houston, we've had a problem. height=%d", height_units);
3022     }
3023   }
3024   widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf );
3025
3026 #if 0
3027 #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
3028   gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0); 
3029   PACK(l_len);
3030   PACK(l_tps);
3031   PACK(l_segs);
3032   PACK(l_dups);
3033   PACK(l_maxs);
3034   PACK(l_avgs);
3035   PACK(l_avgd);
3036   PACK(l_elev);
3037   PACK(l_galo);
3038 #undef PACK;
3039 #endif
3040
3041   if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
3042   {
3043     time_t t1, t2;
3044     t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
3045     t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
3046
3047     strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
3048     tmp_buf[sizeof(tmp_buf)-1] = 0;
3049     g_strchomp(tmp_buf);
3050     widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf);
3051
3052     strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
3053     tmp_buf[sizeof(tmp_buf)-1] = 0;
3054     g_strchomp(tmp_buf);
3055     widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf);
3056
3057     g_snprintf(tmp_buf, sizeof(tmp_buf), _("%d minutes"), (int)(t2-t1)/60);
3058     widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf);
3059   } else {
3060     widgets->w_time_start = content[cnt++] = gtk_label_new(_("No Data"));
3061     widgets->w_time_end = content[cnt++] = gtk_label_new(_("No Data"));
3062     widgets->w_time_dur = content[cnt++] = gtk_label_new(_("No Data"));
3063   }
3064
3065   table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
3066   gtk_table_set_col_spacing (table, 0, 10);
3067   for (i=0; i<cnt; i++) {
3068     GtkWidget *label;
3069
3070     // Settings so the text positioning only moves around vertically when the dialog is resized
3071     // This also gives more room to see the track comment
3072     label = gtk_label_new(NULL);
3073     gtk_misc_set_alignment ( GTK_MISC(label), 1, 0.5 ); // Position text centrally in vertical plane
3074     gtk_label_set_markup ( GTK_LABEL(label), _(label_texts[i]) );
3075     gtk_table_attach ( table, label, 0, 1, i, i+1, GTK_FILL, GTK_SHRINK, 0, 0 );
3076     if (GTK_IS_MISC(content[i])) {
3077       gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0.5 );
3078     }
3079     gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
3080   }
3081
3082   gtk_notebook_append_page(GTK_NOTEBOOK(graphs), GTK_WIDGET(table), gtk_label_new(_("Statistics")));
3083
3084   gpointer *pass_along;
3085   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
3086   pass_along[0] = tr;
3087   pass_along[1] = vlp;
3088   pass_along[2] = vvp;
3089   pass_along[3] = widgets;
3090
3091   if ( widgets->elev_box ) {
3092     GtkWidget *page = NULL;
3093     widgets->w_cur_dist = gtk_label_new(_("No Data"));
3094     widgets->w_cur_elevation = gtk_label_new(_("No Data"));
3095     widgets->w_show_dem = gtk_check_button_new_with_mnemonic(_("Show D_EM"));
3096     widgets->w_show_alt_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
3097     page = create_graph_page (widgets->elev_box,
3098                               _("<b>Track Distance:</b>"), widgets->w_cur_dist,
3099                               _("<b>Track Height:</b>"), widgets->w_cur_elevation,
3100                               widgets->w_show_dem, TRUE,
3101                               widgets->w_show_alt_gps_speed, TRUE);
3102     g_signal_connect (widgets->w_show_dem, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
3103     g_signal_connect (widgets->w_show_alt_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
3104     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-distance")));
3105   }
3106
3107   if ( widgets->gradient_box ) {
3108     GtkWidget *page = NULL;
3109     widgets->w_cur_gradient_dist = gtk_label_new(_("No Data"));
3110     widgets->w_cur_gradient_gradient = gtk_label_new(_("No Data"));
3111     widgets->w_show_gradient_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
3112     page = create_graph_page (widgets->gradient_box,
3113                               _("<b>Track Distance:</b>"), widgets->w_cur_gradient_dist,
3114                               _("<b>Track Gradient:</b>"), widgets->w_cur_gradient_gradient,
3115                               widgets->w_show_gradient_gps_speed, TRUE,
3116                   NULL, FALSE);
3117     g_signal_connect (widgets->w_show_gradient_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
3118     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Gradient-distance")));
3119   }
3120
3121   if ( widgets->speed_box ) {
3122     GtkWidget *page = NULL;
3123     widgets->w_cur_time = gtk_label_new(_("No Data"));
3124     widgets->w_cur_speed = gtk_label_new(_("No Data"));
3125     widgets->w_show_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
3126     page = create_graph_page (widgets->speed_box,
3127                               _("<b>Track Time:</b>"), widgets->w_cur_time,
3128                               _("<b>Track Speed:</b>"), widgets->w_cur_speed,
3129                               widgets->w_show_gps_speed, TRUE,
3130                               NULL, FALSE);
3131     g_signal_connect (widgets->w_show_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
3132     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-time")));
3133   }
3134
3135   if ( widgets->dist_box ) {
3136     GtkWidget *page = NULL;
3137     widgets->w_cur_dist_time = gtk_label_new(_("No Data"));
3138     widgets->w_cur_dist_dist = gtk_label_new(_("No Data"));
3139     widgets->w_show_dist_speed = gtk_check_button_new_with_mnemonic(_("Show S_peed"));
3140     page = create_graph_page (widgets->dist_box,
3141                               _("<b>Track Distance:</b>"), widgets->w_cur_dist_dist,
3142                               _("<b>Track Time:</b>"), widgets->w_cur_dist_time,
3143                               widgets->w_show_dist_speed, FALSE,
3144                               NULL, FALSE);
3145     g_signal_connect (widgets->w_show_dist_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
3146     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Distance-time")));
3147   }
3148
3149   if ( widgets->elev_time_box ) {
3150     GtkWidget *page = NULL;
3151     widgets->w_cur_elev_time = gtk_label_new(_("No Data"));
3152     widgets->w_cur_elev_elev = gtk_label_new(_("No Data"));
3153     widgets->w_show_elev_speed = gtk_check_button_new_with_mnemonic(_("Show S_peed"));
3154     page = create_graph_page (widgets->elev_time_box,
3155                               _("<b>Track Time:</b>"), widgets->w_cur_elev_time,
3156                               _("<b>Track Height:</b>"), widgets->w_cur_elev_elev,
3157                               widgets->w_show_elev_speed, FALSE,
3158                               NULL, FALSE);
3159     g_signal_connect (widgets->w_show_elev_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
3160     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-time")));
3161   }
3162
3163   if ( widgets->speed_dist_box ) {
3164     GtkWidget *page = NULL;
3165     widgets->w_cur_speed_dist = gtk_label_new(_("No Data"));
3166     widgets->w_cur_speed_speed = gtk_label_new(_("No Data"));
3167     widgets->w_show_sd_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
3168     page = create_graph_page (widgets->speed_dist_box,
3169                               _("<b>Track Distance:</b>"), widgets->w_cur_speed_dist,
3170                               _("<b>Track Speed:</b>"), widgets->w_cur_speed_speed,
3171                               widgets->w_show_sd_gps_speed, TRUE,
3172                               NULL, FALSE);
3173     g_signal_connect (widgets->w_show_sd_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
3174     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-distance")));
3175   }
3176
3177   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
3178
3179   gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
3180   if (seg_count <= 1)
3181     gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE);
3182   if (vik_track_get_dup_point_count(tr) <= 0)
3183     gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE);
3184
3185   // On dialog realization configure_event casues the graphs to be initially drawn
3186   widgets->configure_dialog = TRUE;
3187   g_signal_connect ( G_OBJECT(dialog), "configure-event", G_CALLBACK (configure_event), pass_along);
3188
3189   vik_track_set_property_dialog(tr, dialog);
3190   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
3191   gtk_widget_show_all ( dialog );
3192 }
3193
3194
3195 /**
3196  * Update this property dialog
3197  * e.g. if the track has been renamed
3198  */
3199 void vik_trw_layer_propwin_update ( VikTrack *trk )
3200 {
3201   // If not displayed do nothing
3202   if ( !trk->property_dialog )
3203     return;
3204
3205   // Update title with current name
3206   if ( trk->name ) {
3207     gchar *title = g_strdup_printf ( _("%s - Track Properties"), trk->name );
3208     gtk_window_set_title ( GTK_WINDOW(trk->property_dialog), title );
3209     g_free(title);
3210   }
3211
3212 }