]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_propwin.c
Track Properties internal refactor: make getting percentage along a track via distanc...
[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  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #ifdef HAVE_MATH_H
29 #include <math.h>
30 #endif
31
32 #include <gtk/gtk.h>
33 #include <glib/gi18n.h>
34 #include <time.h>
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38 #include "coords.h"
39 #include "vikcoord.h"
40 #include "viktrack.h"
41 #include "viktrwlayer.h"
42 #include "viktrwlayer_propwin.h"
43 #include "vikwaypoint.h"
44 #include "dialog.h"
45 #include "globals.h"
46 #include "dems.h"
47
48 #include "vikviewport.h" /* ugh */
49 #include "viktreeview.h" /* ugh */
50 #include <gdk-pixbuf/gdk-pixdata.h>
51 #include "viklayer.h" /* ugh */
52 #include "vikaggregatelayer.h"
53 #include "viklayerspanel.h" /* ugh */
54
55 #define PROPWIN_PROFILE_WIDTH 600
56 #define PROPWIN_PROFILE_HEIGHT 300
57
58 #define PROPWIN_LABEL_FONT "Sans 7"
59
60 #define MIN_ALT_DIFF 100.0
61 #define MIN_SPEED_DIFF 5.0
62
63 typedef struct _propsaved {
64   gboolean saved;
65   gint pos;
66   GdkImage *img;
67 } PropSaved;
68
69 typedef struct _propwidgets {
70   gboolean  configure_dialog;
71   VikTrwLayer *vtl;
72   VikTrack *tr;
73   VikLayersPanel *vlp;
74   gchar *track_name;
75   gint      profile_width;
76   gint      profile_height;
77   GtkWidget *dialog;
78   GtkWidget *w_comment;
79   GtkWidget *w_track_length;
80   GtkWidget *w_tp_count;
81   GtkWidget *w_segment_count;
82   GtkWidget *w_duptp_count;
83   GtkWidget *w_max_speed;
84   GtkWidget *w_avg_speed;
85   GtkWidget *w_avg_dist;
86   GtkWidget *w_elev_range;
87   GtkWidget *w_elev_gain;
88   GtkWidget *w_time_start;
89   GtkWidget *w_time_end;
90   GtkWidget *w_time_dur;
91   GtkWidget *w_cur_dist; /*< Current distance */
92   GtkWidget *w_cur_time; /*< Current time */
93   gdouble   track_length;
94   PropSaved elev_graph_saved_img;
95   PropSaved speed_graph_saved_img;
96   GtkWidget *elev_box;
97   GtkWidget *speed_box;
98   gdouble   *altitudes;
99   gdouble   *speeds;
100   VikTrackpoint *marker_tp;
101   gboolean  is_marker_drawn;
102 } PropWidgets;
103
104 static PropWidgets *prop_widgets_new()
105 {
106   PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
107
108   return widgets;
109 }
110
111 static void prop_widgets_free(PropWidgets *widgets)
112 {
113   if (widgets->elev_graph_saved_img.img)
114     g_object_unref(widgets->elev_graph_saved_img.img);
115   if (widgets->speed_graph_saved_img.img)
116     g_object_unref(widgets->speed_graph_saved_img.img);
117   if (widgets->altitudes)
118     g_free(widgets->altitudes);
119   if (widgets->speeds)
120     g_free(widgets->speeds);
121   g_free(widgets);
122 }
123
124 static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gboolean NO_ALT_TEST, gint PROFILE_WIDTH)
125 {
126   *max = -1000;
127   *min = 20000;
128   guint i;
129   for ( i=0; i < PROFILE_WIDTH; i++ ) {
130     if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) {
131       if ( array[i] > *max )
132         *max = array[i];
133       if ( array[i] < *min )
134         *min = array[i];
135     }
136   }
137 }
138
139 #define MARGIN 70
140 #define LINES 5
141 static VikTrackpoint *set_center_at_graph_position(gdouble event_x,
142                                                    gint img_width,
143                                                    VikTrwLayer *vtl,
144                                                    VikLayersPanel *vlp,
145                                                    VikViewport *vvp,
146                                                    VikTrack *tr,
147                                                    gboolean time_base,
148                                                    gint PROFILE_WIDTH)
149 {
150   VikTrackpoint *trackpoint;
151   gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
152   if (x < 0)
153     x = 0;
154   if (x > PROFILE_WIDTH)
155     x = PROFILE_WIDTH;
156
157   if (time_base)
158     trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
159   else
160     trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
161
162   if ( trackpoint ) {
163     VikCoord coord = trackpoint->coord;
164     if ( vlp ) {
165       vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
166       vik_layers_panel_emit_update ( vlp );
167     }
168     else {
169       /* since vlp not set, vvp should be valid instead! */
170       if ( vvp )
171         vik_viewport_set_center_coord ( vvp, &coord );
172       vik_layer_emit_update ( VIK_LAYER(vtl) );
173     }
174   }
175   return trackpoint;
176 }
177
178 /**
179  * Returns whether the marker was drawn or not
180  */
181 static void draw_graph_mark (GtkWidget *image,
182                              gdouble event_x,
183                              gint img_width,
184                              GdkGC *gc,
185                              PropSaved *saved_img,
186                              gint PROFILE_WIDTH,
187                              gint PROFILE_HEIGHT,
188                              gboolean *marker_drawn)
189 {
190   GdkPixmap *pix = NULL;
191   /* the pixmap = margin + graph area */
192   gdouble x = event_x - img_width/2 + PROFILE_WIDTH/2 + MARGIN/2;
193
194   // fprintf(stderr, "event_x=%f img_width=%d x=%f\n", event_x, img_width, x);
195
196   gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL);
197   /* Restore previously saved image */
198   if (saved_img->saved) {
199     gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0,
200         saved_img->pos, 0, -1, -1);
201     saved_img->saved = FALSE;
202     gtk_widget_queue_draw_area(image,
203         saved_img->pos + img_width/2 - PROFILE_WIDTH/2 - MARGIN/2, 0,
204         saved_img->img->width, saved_img->img->height);
205   }
206   if ((x >= MARGIN) && (x < (PROFILE_WIDTH + MARGIN))) {
207     /* Save part of the image */
208     if (saved_img->img)
209       gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img,
210           x, 0, 0, 0, saved_img->img->width, saved_img->img->height);
211     else
212       saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix),
213           saved_img->img, x, 0, 0, 0, 1, PROFILE_HEIGHT);
214     saved_img->pos = x;
215     saved_img->saved = TRUE;
216     gdk_draw_line (GDK_DRAWABLE(pix), gc, x, 0, x, image->allocation.height);
217     /* redraw the area which contains the line, saved_width is just convenient */
218     gtk_widget_queue_draw_area(image, event_x, 0, 1, PROFILE_HEIGHT);
219     *marker_drawn = TRUE;
220   }
221   else
222     *marker_drawn = FALSE;
223 }
224
225 /**
226  * Return the percentage of how far a trackpoint is a long a track via the time method
227  */
228 static gdouble tp_percentage_by_time ( VikTrack *tr, VikTrackpoint *trackpoint )
229 {
230   gdouble pc = NAN;
231   if (trackpoint == NULL)
232     return pc;
233   time_t t_start, t_end, t_total;
234   t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
235   t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
236   t_total = t_end - t_start;
237   pc = (gdouble)(trackpoint->timestamp - t_start)/t_total;
238   return pc;
239 }
240
241 /**
242  * Return the percentage of how far a trackpoint is a long a track via the distance method
243  */
244 static gdouble tp_percentage_by_distance ( VikTrack *tr, VikTrackpoint *trackpoint, gdouble track_length )
245 {
246   gdouble pc = NAN;
247   if (trackpoint == NULL)
248     return pc;
249   gdouble dist = 0.0;
250   GList *iter;
251   for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) {
252     dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord),
253                            &(VIK_TRACKPOINT(iter->prev->data)->coord));
254     /* Assuming trackpoint is not a copy */
255     if (trackpoint == VIK_TRACKPOINT(iter->data))
256       break;
257   }
258   if (iter != NULL)
259     pc = dist/track_length;
260   return pc;
261 }
262
263 static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, gboolean is_vt_graph )
264 {
265   VikTrack *tr = pass_along[0];
266   VikLayersPanel *vlp = pass_along[1];
267   VikViewport *vvp = pass_along[2];
268   PropWidgets *widgets = pass_along[3];
269   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
270   GtkWidget *image = GTK_WIDGET(child->data);
271   GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box));
272
273   VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, widgets->vtl, vlp, vvp, tr, is_vt_graph, widgets->profile_width);
274   draw_graph_mark(image,
275                   event->x,
276                   event_box->allocation.width,
277                   window->style->black_gc,
278                   is_vt_graph ? &widgets->speed_graph_saved_img : &widgets->elev_graph_saved_img,
279                   widgets->profile_width,
280                   widgets->profile_height,
281                   &widgets->is_marker_drawn);
282   g_list_free(child);
283   widgets->marker_tp = trackpoint;
284   gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, widgets->is_marker_drawn);
285
286   /* draw on the other graph */
287   if (trackpoint == NULL || widgets->elev_box == NULL || widgets->speed_box == NULL)
288     /* This test assumes we have only 2 graphs */
289     return;
290
291   gdouble pc = NAN;
292   gdouble x2;
293   GList *other_child = gtk_container_get_children(GTK_CONTAINER(
294                          is_vt_graph ? widgets->elev_box : widgets->speed_box));
295   GtkWidget *other_image = GTK_WIDGET(other_child->data);
296   if (is_vt_graph) {
297     pc = tp_percentage_by_distance ( tr, trackpoint, widgets->track_length );
298   } else {
299     pc = tp_percentage_by_time ( tr, trackpoint );
300   }
301   if (!isnan(pc)) {
302     x2 = pc * widgets->profile_width + MARGIN + (event_box->allocation.width/2 - widgets->profile_width/2 - MARGIN/2);
303     draw_graph_mark(other_image,
304                     x2,
305                     event_box->allocation.width,
306                     window->style->black_gc,
307                     is_vt_graph ? &widgets->elev_graph_saved_img : &widgets->speed_graph_saved_img,
308                     widgets->profile_width,
309                     widgets->profile_height,
310                     &widgets->is_marker_drawn);
311   }
312
313   g_list_free(other_child);
314
315 }
316
317 static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
318 {
319   track_graph_click(event_box, event, pass_along, FALSE);
320   return TRUE;  /* don't call other (further) callbacks */
321 }
322
323 static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
324 {
325   track_graph_click(event_box, event, pass_along, TRUE);
326   return TRUE;  /* don't call other (further) callbacks */
327 }
328
329 void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
330 {
331   VikTrack *tr = pass_along[0];
332   PropWidgets *widgets = pass_along[3];
333   int mouse_x, mouse_y;
334   GdkModifierType state;
335
336   if (event->is_hint)
337     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
338   else
339     mouse_x = event->x;
340
341   gdouble x = mouse_x - image->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
342   if (x < 0)
343     x = 0;
344   if (x > widgets->profile_width)
345     x = widgets->profile_width;
346
347   gdouble meters_from_start;
348   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
349   if (trackpoint && widgets->w_cur_dist) {
350     static gchar tmp_buf[20];
351     vik_units_distance_t dist_units = a_vik_get_units_distance ();
352     switch (dist_units) {
353     case VIK_UNITS_DISTANCE_KILOMETRES:
354       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
355       break;
356     case VIK_UNITS_DISTANCE_MILES:
357       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
358       break;
359     default:
360       g_critical("Houston, we've had a problem. distance=%d", dist_units);
361     }
362     gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf);
363   }
364 }
365
366 void track_vt_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
367 {
368   VikTrack *tr = pass_along[0];
369   PropWidgets *widgets = pass_along[3];
370   int mouse_x, mouse_y;
371   GdkModifierType state;
372
373   if (event->is_hint)
374     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
375   else
376     mouse_x = event->x;
377
378   gdouble x = mouse_x - image->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
379   if (x < 0)
380     x = 0;
381   if (x > widgets->profile_width)
382     x = widgets->profile_width;
383
384   time_t seconds_from_start;
385   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
386   if (trackpoint && widgets->w_cur_time) {
387     static gchar tmp_buf[20];
388     guint h, m, s;
389     h = seconds_from_start/3600;
390     m = (seconds_from_start - h*3600)/60;
391     s = seconds_from_start - (3600*h) - (60*m);
392     g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
393
394     gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf);
395   }
396 }
397
398 static void draw_dem_alt_speed_dist(VikTrack *tr, GdkDrawable *pix, GdkGC *alt_gc, GdkGC *speed_gc, gdouble alt_offset, gdouble alt_diff, gint width, gint height, gint margin)
399 {
400   GList *iter;
401   gdouble dist = 0;
402   gdouble max_speed = 0;
403   gdouble total_length = vik_track_get_length_including_gaps(tr);
404
405   for (iter = tr->trackpoints->next; iter; iter = iter->next) {
406     if (!isnan(VIK_TRACKPOINT(iter->data)->speed))
407       max_speed = MAX(max_speed, VIK_TRACKPOINT(iter->data)->speed);
408   }
409   max_speed = max_speed * 110 / 100;
410
411   for (iter = tr->trackpoints->next; iter; iter = iter->next) {
412     int x, y_alt, y_speed;
413     gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST);
414     elev -= alt_offset;
415     dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
416       &(VIK_TRACKPOINT(iter->prev->data)->coord) );
417     x = (width * dist)/total_length + margin;
418     if ( elev != VIK_DEM_INVALID_ELEVATION ) {
419       y_alt = height - ((height * elev)/alt_diff);
420       gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4);
421     }
422     if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
423       y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
424       gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
425     }
426   }
427 }
428
429 /**
430  * Draw just the height profile image
431  */
432 static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
433 {
434   GtkWidget *window;
435   GdkPixmap *pix;
436   gdouble mina, maxa;
437   guint i;
438
439   GdkGC *no_alt_info;
440   GdkGC *dem_alt_gc;
441   GdkGC *gps_speed_gc;
442
443   GdkColor color;
444
445   // Free previous allocation
446   if ( widgets->altitudes )
447     g_free ( widgets->altitudes );
448
449   widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
450
451   if ( widgets->altitudes == NULL )
452     return;
453
454   minmax_array(widgets->altitudes, &mina, &maxa, TRUE, widgets->profile_width);
455   maxa = maxa + ((maxa - mina) * 0.25); // Make visible window a bit bigger than highest point
456
457   window = gtk_widget_get_toplevel (widgets->elev_box);
458
459   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
460
461   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
462
463   no_alt_info = gdk_gc_new ( window->window );
464   gdk_color_parse ( "yellow", &color );
465   gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
466
467   dem_alt_gc = gdk_gc_new ( window->window );
468   gdk_color_parse ( "green", &color );
469   gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
470
471   gps_speed_gc = gdk_gc_new ( window->window );
472   gdk_color_parse ( "red", &color );
473   gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
474
475   /* clear the image */
476   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], 
477                      TRUE, 0, 0, MARGIN, widgets->profile_height);
478   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], 
479                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
480   
481   /* draw grid */
482   vik_units_height_t height_units = a_vik_get_units_height ();
483   for (i=0; i<=LINES; i++) {
484     PangoFontDescription *pfd;
485     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
486     gchar s[32];
487     int w, h;
488
489     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
490     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
491     pango_layout_set_font_description (pl, pfd);
492     pango_font_description_free (pfd);
493     switch (height_units) {
494     case VIK_UNITS_HEIGHT_METRES:
495       sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
496       break;
497     case VIK_UNITS_HEIGHT_FEET:
498       sprintf(s, "%8dft", (int)VIK_METERS_TO_FEET(mina + (LINES-i)*(maxa-mina)/LINES));
499       break;
500     default:
501       sprintf(s, "--");
502       g_critical("Houston, we've had a problem. height=%d", height_units);
503     }
504     pango_layout_set_text(pl, s, -1);
505     pango_layout_get_pixel_size (pl, &w, &h);
506     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, 
507                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
508
509     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], 
510                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
511     g_object_unref ( G_OBJECT ( pl ) );
512     pl = NULL;
513   }
514
515   /* draw elevations */
516   for ( i = 0; i < widgets->profile_width; i++ )
517     if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE )
518       gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info, 
519                       i + MARGIN, 0, i + MARGIN, widgets->profile_height );
520     else 
521       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], 
522                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->altitudes[i]-mina)/(maxa-mina) );
523
524   draw_dem_alt_speed_dist(tr, GDK_DRAWABLE(pix), dem_alt_gc, gps_speed_gc, mina, maxa - mina, widgets->profile_width, widgets->profile_height, MARGIN);
525
526   /* draw border */
527   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
528
529   g_object_unref ( G_OBJECT(pix) );
530   g_object_unref ( G_OBJECT(no_alt_info) );
531   g_object_unref ( G_OBJECT(dem_alt_gc) );
532   g_object_unref ( G_OBJECT(gps_speed_gc) );
533
534 }
535
536 /**
537  * Draw just the speed (velocity)/time image
538  */
539 static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
540 {
541   GtkWidget *window;
542   GdkPixmap *pix;
543   gdouble mins, maxs;
544   guint i;
545
546   // Free previous allocation
547   if ( widgets->speeds )
548     g_free ( widgets->speeds );
549
550   widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
551   if ( widgets->speeds == NULL )
552     return;
553
554   GdkGC *gps_speed_gc;
555   GdkColor color;
556
557   window = gtk_widget_get_toplevel (widgets->speed_box);
558
559   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
560
561   gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
562
563   gps_speed_gc = gdk_gc_new ( window->window );
564   gdk_color_parse ( "red", &color );
565   gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
566
567   minmax_array(widgets->speeds, &mins, &maxs, FALSE, widgets->profile_width);
568   if (mins < 0.0)
569     mins = 0; /* splines sometimes give negative speeds */
570   maxs = maxs + ((maxs - mins) * 0.1);
571   if  (maxs-mins < MIN_SPEED_DIFF) {
572     maxs = mins + MIN_SPEED_DIFF;
573   }
574   
575   /* clear the image */
576   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], 
577                      TRUE, 0, 0, MARGIN, widgets->profile_height);
578   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], 
579                      TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
580
581   /* draw grid */
582   for (i=0; i<=LINES; i++) {
583     PangoFontDescription *pfd;
584     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
585     gchar s[32];
586     int w, h;
587
588     pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
589     pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
590     pango_layout_set_font_description (pl, pfd);
591     pango_font_description_free (pfd);
592     vik_units_speed_t speed_units = a_vik_get_units_speed ();
593     switch (speed_units) {
594     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
595       sprintf(s, "%6.1fkm/h", VIK_MPS_TO_KPH(mins + (LINES-i)*(maxs-mins)/LINES));
596       break;
597     case VIK_UNITS_SPEED_MILES_PER_HOUR:
598       sprintf(s, "%6.1fmph", VIK_MPS_TO_MPH(mins + (LINES-i)*(maxs-mins)/LINES));
599       break;
600     case VIK_UNITS_SPEED_METRES_PER_SECOND:
601       sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
602       break;
603     case VIK_UNITS_SPEED_KNOTS:
604       sprintf(s, "%6.1fknots", VIK_MPS_TO_KNOTS(mins + (LINES-i)*(maxs-mins)/LINES));
605       break;
606     default:
607       sprintf(s, "--");
608       g_critical("Houston, we've had a problem. speed=%d", speed_units);
609     }
610
611     pango_layout_set_text(pl, s, -1);
612     pango_layout_get_pixel_size (pl, &w, &h);
613     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3, 
614                     CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
615
616     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], 
617                    MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
618     g_object_unref ( G_OBJECT ( pl ) );
619     pl = NULL;
620   }
621   
622
623   /* draw speeds */
624   for ( i = 0; i < widgets->profile_width; i++ )
625       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], 
626                       i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(maxs-mins) );
627
628
629   time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
630   time_t dur =  VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
631   GList *iter;
632   for (iter = tr->trackpoints; iter; iter = iter->next) {
633     gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
634     if (isnan(gps_speed))
635         continue;
636     int x = MARGIN + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
637     int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(maxs - mins);
638     gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
639   }
640
641   /* draw border */
642   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
643
644   g_object_unref ( G_OBJECT(pix) );
645   g_object_unref ( G_OBJECT(gps_speed_gc) );
646
647 }
648 #undef LINES
649
650
651 /**
652  * Configure the profile & speed/time images
653  */
654 static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, gpointer *pass_along )
655 {
656   VikTrack *tr = pass_along[0];
657   PropWidgets *widgets = pass_along[2];
658
659   // ATM we receive configure_events when the dialog is moved and so no further action is necessary
660   if ( !widgets->configure_dialog )
661     return FALSE;
662
663   widgets->configure_dialog = FALSE;
664
665   // Draw graphs even if they are not visible
666
667   GList *child = NULL;
668   // Draw elevations
669   if (widgets->elev_box != NULL) {
670     child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box));
671     draw_elevations (GTK_WIDGET(child->data), tr, widgets );
672     g_list_free(child);
673   }
674
675   // Draw speeds
676   if (widgets->speed_box != NULL) {
677     child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box));
678     draw_vt (GTK_WIDGET(child->data), tr, widgets );
679     g_list_free(child);
680   }
681     
682   return FALSE;
683 }
684
685 /**
686  * Create height profile widgets including the image and callbacks
687  */
688 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
689 {
690   GdkPixmap *pix;
691   GtkWidget *image;
692   GtkWidget *eventbox;
693   gpointer *pass_along;
694
695   // First allocation
696   widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
697
698   if ( widgets->altitudes == NULL ) {
699     *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
700     return NULL;
701   }
702
703   minmax_array(widgets->altitudes, min_alt, max_alt, TRUE, widgets->profile_width);
704   
705   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
706   image = gtk_image_new_from_pixmap ( pix, NULL );
707
708   g_object_unref ( G_OBJECT(pix) );
709
710   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
711   pass_along[0] = tr;
712   pass_along[1] = vlp;
713   pass_along[2] = vvp;
714   pass_along[3] = widgets;
715
716   eventbox = gtk_event_box_new ();
717   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
718   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
719   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
720   gtk_container_add ( GTK_CONTAINER(eventbox), image );
721   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
722
723   return eventbox;
724 }
725
726 /**
727  * Create speed/time widgets including the image and callbacks
728  */
729 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
730 {
731   GdkPixmap *pix;
732   GtkWidget *image;
733   GtkWidget *eventbox;
734   gpointer *pass_along;
735
736   // First allocation
737   widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
738   if ( widgets->speeds == NULL )
739     return NULL;
740
741   pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
742   pass_along[0] = tr;
743   pass_along[1] = vlp;
744   pass_along[2] = vvp;
745   pass_along[3] = widgets;
746
747   pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
748   image = gtk_image_new_from_pixmap ( pix, NULL );
749
750 #if 0
751   /* XXX this can go out, it's just a helpful dev tool */
752   {
753     int j;
754     GdkGC **colors[8] = { window->style->bg_gc,
755                           window->style->fg_gc,
756                           window->style->light_gc,
757                           window->style->dark_gc,
758                           window->style->mid_gc,
759                           window->style->text_gc,
760                           window->style->base_gc,
761                           window->style->text_aa_gc };
762     for (i=0; i<5; i++) {
763       for (j=0; j<8; j++) {
764         gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
765                            TRUE, i*20, j*20, 20, 20);
766         gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
767                            FALSE, i*20, j*20, 20, 20);
768       }
769     }
770   }
771 #endif
772
773   g_object_unref ( G_OBJECT(pix) );
774
775   eventbox = gtk_event_box_new ();
776   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along );
777   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along );
778   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
779   gtk_container_add ( GTK_CONTAINER(eventbox), image );
780   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
781
782   return eventbox;
783 }
784 #undef MARGIN
785
786 static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets)
787 {
788   VikTrack *tr = widgets->tr;
789   VikTrwLayer *vtl = widgets->vtl;
790   gboolean keep_dialog = FALSE;
791
792   /* FIXME: check and make sure the track still exists before doing anything to it */
793   /* Note: destroying diaglog (eg, parent window exit) won't give "response" */
794   switch (resp) {
795     case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
796     case GTK_RESPONSE_REJECT:
797       break;
798     case GTK_RESPONSE_ACCEPT:
799       vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
800       break;
801     case VIK_TRW_LAYER_PROPWIN_REVERSE:
802       vik_track_reverse(tr);
803       vik_layer_emit_update ( VIK_LAYER(vtl) );
804       break;
805     case VIK_TRW_LAYER_PROPWIN_DEL_DUP:
806       vik_track_remove_dup_points(tr);
807       /* above operation could have deleted current_tp or last_tp */
808       trw_layer_cancel_tps_of_track ( vtl, widgets->track_name );
809       vik_layer_emit_update ( VIK_LAYER(vtl) );
810       break;
811     case VIK_TRW_LAYER_PROPWIN_SPLIT:
812       {
813         /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */
814         guint ntracks;
815         VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
816         gchar *new_tr_name;
817         guint i;
818         for ( i = 0; i < ntracks; i++ )
819         {
820           g_assert ( tracks[i] );
821           new_tr_name = g_strdup_printf("%s #%d", widgets->track_name, i+1);
822           /* if ( (wp_exists) && (! overwrite) ) */
823           /* don't need to upper case new_tr_name because old tr name was uppercase */
824           if ( vik_trw_layer_get_track(vtl, new_tr_name ) && 
825              ( ! a_dialog_yes_or_no ( VIK_GTK_WINDOW_FROM_LAYER(vtl), "The track \"%s\" exists, do you wish to overwrite it?", new_tr_name ) ) )
826           {
827             gchar *new_new_tr_name = a_dialog_new_track ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
828             g_free ( new_tr_name );
829             if (new_new_tr_name)
830               new_tr_name = new_new_tr_name;
831             else
832             {
833               new_tr_name = NULL;
834               vik_track_free ( tracks[i] );
835             }
836           }
837           if ( new_tr_name )
838             vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
839         }
840         if ( tracks )
841         {
842           g_free ( tracks );
843           /* Don't let track destroy this dialog */
844           vik_track_clear_property_dialog(tr);
845           vik_trw_layer_delete_track ( vtl, widgets->track_name );
846           vik_layer_emit_update ( VIK_LAYER(vtl) ); /* chase thru the hoops */
847         }
848       }
849       break;
850     case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER:
851       {
852         GList *iter = tr->trackpoints;
853         while ((iter = iter->next)) {
854           if (widgets->marker_tp == VIK_TRACKPOINT(iter->data))
855             break;
856         }
857         if (iter == NULL) {
858           a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR,
859                   _("Failed spliting track. Track unchanged"), NULL);
860           keep_dialog = TRUE;
861           break;
862         }
863
864         gchar *r_name = g_strdup_printf("%s #R", widgets->track_name);
865         if (vik_trw_layer_get_track(vtl, r_name ) && 
866              ( ! a_dialog_yes_or_no( VIK_GTK_WINDOW_FROM_LAYER(vtl),
867               "The track \"%s\" exists, do you wish to overwrite it?", r_name)))
868         {
869           gchar *new_r_name = a_dialog_new_track( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
870             if (new_r_name) {
871               g_free( r_name );
872               r_name = new_r_name;
873             }
874             else {
875               a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_WARNING,
876                   _("Operation Aborted. Track unchanged"), NULL);
877               keep_dialog = TRUE;
878               break;
879             }
880         }
881         iter->prev->next = NULL;
882         iter->prev = NULL;
883         VikTrack *tr_right = vik_track_new();
884         if ( tr->comment )
885           vik_track_set_comment ( tr_right, tr->comment );
886         tr_right->visible = tr->visible;
887         tr_right->trackpoints = iter;
888
889         vik_trw_layer_add_track(vtl, r_name, tr_right);
890         vik_layer_emit_update ( VIK_LAYER(vtl) );
891       }
892       break;
893     default:
894       fprintf(stderr, "DEBUG: unknown response\n");
895       return;
896   }
897
898   /* Keep same behaviour for now: destroy dialog if click on any button */
899   if (!keep_dialog) {
900     prop_widgets_free(widgets);
901     vik_track_clear_property_dialog(tr);
902     gtk_widget_destroy ( GTK_WIDGET(dialog) );
903   }
904 }
905
906 static GtkWidget *create_graph_page ( GtkWidget *graph,
907                                       const gchar *markup,
908                                       GtkWidget *value)
909 {
910   GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 );
911   GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 );
912   GtkWidget *label = gtk_label_new (NULL);
913   gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0);
914   gtk_label_set_markup ( GTK_LABEL(label), markup );
915   gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
916   gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0);
917   gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
918   
919   return vbox;
920 }
921
922 void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name, VikViewport *vvp )
923 {
924   /* FIXME: free widgets when destroy signal received */
925   PropWidgets *widgets = prop_widgets_new();
926   widgets->vtl = vtl;
927   widgets->tr = tr;
928   widgets->vlp = vlp;
929   widgets->profile_width  = PROPWIN_PROFILE_WIDTH;
930   widgets->profile_height = PROPWIN_PROFILE_HEIGHT;
931   widgets->track_name = track_name;
932   gchar *title = g_strdup_printf(_("%s - Track Properties"), track_name);
933   GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
934                          parent,
935                          GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
936                          GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
937                          _("Split at Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER,
938                          _("Split Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT,
939                          _("Reverse"),        VIK_TRW_LAYER_PROPWIN_REVERSE,
940                          _("Delete Dupl."),   VIK_TRW_LAYER_PROPWIN_DEL_DUP,
941                          GTK_STOCK_OK,     GTK_RESPONSE_ACCEPT,
942                          NULL);
943   widgets->dialog = dialog;
944   g_free(title);
945   g_signal_connect(dialog, "response", G_CALLBACK(propwin_response_cb), widgets);
946   GtkTable *table;
947   gdouble tr_len;
948   guint32 tp_count, seg_count;
949
950   gdouble min_alt, max_alt;
951   widgets->elev_box = vik_trw_layer_create_profile(GTK_WIDGET(parent), tr, vlp, vvp, widgets, &min_alt, &max_alt);
952   widgets->speed_box = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
953   GtkWidget *graphs = gtk_notebook_new();
954
955   GtkWidget *content[20];
956   int cnt;
957   int i;
958
959   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>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>") };
960   static gchar tmp_buf[50];
961   gdouble tmp_speed;
962
963   cnt = 0;
964   widgets->w_comment = gtk_entry_new ();
965   if ( tr->comment )
966     gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment );
967   g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
968   content[cnt++] = widgets->w_comment;
969
970   vik_units_distance_t dist_units = a_vik_get_units_distance ();
971
972   tr_len = widgets->track_length = vik_track_get_length(tr);
973   switch (dist_units) {
974   case VIK_UNITS_DISTANCE_KILOMETRES:
975     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", tr_len/1000.0 );
976     break;
977   case VIK_UNITS_DISTANCE_MILES:
978     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(tr_len) );
979     break;
980   default:
981     g_critical("Houston, we've had a problem. distance=%d", dist_units);
982   }
983   widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf );
984
985   tp_count = vik_track_get_tp_count(tr);
986   g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
987   widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf );
988
989   seg_count = vik_track_get_segment_count(tr) ;
990   g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
991   widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf );
992
993   g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
994   widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf );
995
996   vik_units_speed_t speed_units = a_vik_get_units_speed ();
997   tmp_speed = vik_track_get_max_speed(tr);
998   if ( tmp_speed == 0 )
999     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1000   else {
1001     switch (speed_units) {
1002     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1003       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
1004       break;
1005     case VIK_UNITS_SPEED_MILES_PER_HOUR:
1006       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
1007       break;
1008     case VIK_UNITS_SPEED_METRES_PER_SECOND:
1009       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
1010       break;
1011     case VIK_UNITS_SPEED_KNOTS:
1012       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
1013       break;
1014     default:
1015       g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
1016       g_critical("Houston, we've had a problem. speed=%d", speed_units);
1017     }
1018   }
1019   widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf );
1020
1021   tmp_speed = vik_track_get_average_speed(tr);
1022   if ( tmp_speed == 0 )
1023     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1024   else {
1025     switch (speed_units) {
1026     case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1027       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
1028       break;
1029     case VIK_UNITS_SPEED_MILES_PER_HOUR:
1030       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
1031       break;
1032     case VIK_UNITS_SPEED_METRES_PER_SECOND:
1033       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
1034       break;
1035     case VIK_UNITS_SPEED_KNOTS:
1036       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
1037       break;
1038     default:
1039       g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
1040       g_critical("Houston, we've had a problem. speed=%d", speed_units);
1041     }
1042   }
1043   widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
1044
1045   switch (dist_units) {
1046   case VIK_UNITS_DISTANCE_KILOMETRES:
1047     // Even though kilometres, the average distance between points is going to be quite small so keep in metres
1048     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
1049     break;
1050   case VIK_UNITS_DISTANCE_MILES:
1051     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 )) );
1052     break;
1053   default:
1054     g_critical("Houston, we've had a problem. distance=%d", dist_units);
1055   }
1056   widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf );
1057
1058   vik_units_height_t height_units = a_vik_get_units_height ();
1059   if ( min_alt == VIK_DEFAULT_ALTITUDE )
1060     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1061   else {
1062     switch (height_units) {
1063     case VIK_UNITS_HEIGHT_METRES:
1064       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
1065       break;
1066     case VIK_UNITS_HEIGHT_FEET:
1067       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet - %.0f feet", VIK_METERS_TO_FEET(min_alt), VIK_METERS_TO_FEET(max_alt) );
1068       break;
1069     default:
1070       g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
1071       g_critical("Houston, we've had a problem. height=%d", height_units);
1072     }
1073   }
1074   widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf );
1075
1076   vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
1077   if ( min_alt == VIK_DEFAULT_ALTITUDE )
1078     g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
1079   else {
1080     switch (height_units) {
1081     case VIK_UNITS_HEIGHT_METRES:
1082       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
1083       break;
1084     case VIK_UNITS_HEIGHT_FEET:
1085       g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet / %.0f feet", VIK_METERS_TO_FEET(max_alt), VIK_METERS_TO_FEET(min_alt) );
1086       break;
1087     default:
1088       g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
1089       g_critical("Houston, we've had a problem. height=%d", height_units);
1090     }
1091   }
1092   widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf );
1093
1094 #if 0
1095 #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
1096   gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0); 
1097   PACK(l_len);
1098   PACK(l_tps);
1099   PACK(l_segs);
1100   PACK(l_dups);
1101   PACK(l_maxs);
1102   PACK(l_avgs);
1103   PACK(l_avgd);
1104   PACK(l_elev);
1105   PACK(l_galo);
1106 #undef PACK;
1107 #endif
1108
1109   if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
1110   {
1111     time_t t1, t2;
1112     t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1113     t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1114
1115     strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
1116     tmp_buf[sizeof(tmp_buf)-1] = 0;
1117     g_strchomp(tmp_buf);
1118     widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf);
1119
1120     strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
1121     tmp_buf[sizeof(tmp_buf)-1] = 0;
1122     g_strchomp(tmp_buf);
1123     widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf);
1124
1125     g_snprintf(tmp_buf, sizeof(tmp_buf), _("%d minutes"), (int)(t2-t1)/60);
1126     widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf);
1127   } else {
1128     widgets->w_time_start = content[cnt++] = gtk_label_new(_("No Data"));
1129     widgets->w_time_end = content[cnt++] = gtk_label_new(_("No Data"));
1130     widgets->w_time_dur = content[cnt++] = gtk_label_new(_("No Data"));
1131   }
1132
1133   table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
1134   gtk_table_set_col_spacing (table, 0, 10);
1135   for (i=0; i<cnt; i++) {
1136     GtkWidget *label;
1137
1138     label = gtk_label_new(NULL);
1139     gtk_misc_set_alignment ( GTK_MISC(label), 1, 0 );
1140     gtk_label_set_markup ( GTK_LABEL(label), _(label_texts[i]) );
1141     gtk_table_attach_defaults ( table, label, 0, 1, i, i+1 );
1142     if (GTK_IS_MISC(content[i])) {
1143       gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0 );
1144     }
1145     gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
1146   }
1147
1148   gtk_notebook_append_page(GTK_NOTEBOOK(graphs), GTK_WIDGET(table), gtk_label_new(_("Statistics")));
1149
1150   if ( widgets->elev_box ) {
1151     GtkWidget *page = NULL;
1152     widgets->w_cur_dist = gtk_label_new(_("No Data"));
1153     page = create_graph_page (widgets->elev_box, _("<b>Track Distance:</b>"), widgets->w_cur_dist );
1154     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-distance")));
1155   }
1156
1157   if ( widgets->speed_box ) {
1158     GtkWidget *page = NULL;
1159     widgets->w_cur_time = gtk_label_new(_("No Data"));
1160     page = create_graph_page (widgets->speed_box, _("<b>Track Time:</b>"), widgets->w_cur_time );
1161     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-time")));
1162   }
1163
1164   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
1165
1166   gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
1167   if (seg_count <= 1)
1168     gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE);
1169   if (vik_track_get_dup_point_count(tr) <= 0)
1170     gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE);
1171
1172   gpointer *pass_along;
1173   pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */
1174   pass_along[0] = tr;
1175   pass_along[1] = vlp;
1176   pass_along[2] = widgets;
1177
1178   // On dialog realization configure_event casues the graphs to be initially drawn
1179   widgets->configure_dialog = TRUE;
1180   g_signal_connect ( G_OBJECT(dialog), "configure-event", G_CALLBACK (configure_event), pass_along);
1181
1182   vik_track_set_property_dialog(tr, dialog);
1183   gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
1184   gtk_widget_show_all ( dialog );
1185 }