]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_propwin.c
Added track name to title of properties dialog.
[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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <math.h>
23 #include <gtk/gtk.h>
24 #include <time.h>
25 #include <string.h>
26 #include "coords.h"
27 #include "vikcoord.h"
28 #include "viktrack.h"
29 #include "viktrwlayer.h"
30 #include "viktrwlayer_propwin.h"
31 #include "vikwaypoint.h"
32 #include "dialog.h"
33 #include "globals.h"
34 #include "dems.h"
35
36 #include "vikviewport.h" /* ugh */
37 #include "viktreeview.h" /* ugh */
38 #include <gdk-pixbuf/gdk-pixdata.h>
39 #include "viklayer.h" /* ugh */
40 #include "vikaggregatelayer.h"
41 #include "viklayerspanel.h" /* ugh */
42
43 #define PROFILE_WIDTH 600
44 #define PROFILE_HEIGHT 300
45 #define MIN_ALT_DIFF 100.0
46 #define MIN_SPEED_DIFF 20.0
47
48 typedef struct _propsaved {
49   gboolean saved;
50   gint pos;
51   GdkImage *img;
52 } PropSaved;
53
54 typedef struct _propwidgets {
55   VikTrwLayer *vtl;
56   VikTrack *tr;
57   VikLayersPanel *vlp;
58   gchar *track_name;
59   GtkWidget *w_comment;
60   GtkWidget *w_track_length;
61   GtkWidget *w_tp_count;
62   GtkWidget *w_segment_count;
63   GtkWidget *w_duptp_count;
64   GtkWidget *w_max_speed;
65   GtkWidget *w_avg_speed;
66   GtkWidget *w_avg_dist;
67   GtkWidget *w_elev_range;
68   GtkWidget *w_elev_gain;
69   GtkWidget *w_time_start;
70   GtkWidget *w_time_end;
71   GtkWidget *w_time_dur;
72   GtkWidget *w_dist_time;
73   PropSaved elev_graph_saved_img;
74   PropSaved speed_graph_saved_img;
75 } PropWidgets;
76
77 static PropWidgets *prop_widgets_new()
78 {
79   PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
80
81   return widgets;
82 }
83
84 static void prop_widgets_free(PropWidgets *widgets)
85 {
86
87   if (widgets->elev_graph_saved_img.img)
88     g_object_unref(widgets->elev_graph_saved_img.img);
89   if (widgets->speed_graph_saved_img.img)
90     g_object_unref(widgets->speed_graph_saved_img.img);
91   g_free(widgets);
92 }
93
94 static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max)
95 {
96   *max = -1000;
97   *min = 20000;
98   guint i;
99   for ( i=0; i < PROFILE_WIDTH; i++ ) {
100     if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) {
101       if ( altitudes[i] > *max )
102         *max = altitudes[i];
103       if ( altitudes[i] < *min )
104         *min = altitudes[i];
105     }
106   }
107 }
108
109 #define MARGIN 50
110 #define LINES 5
111 static void set_center_at_graph_position(gdouble event_x, gint img_width, VikLayersPanel *vlp, VikTrack *tr, gboolean time_base)
112 {
113   VikTrackpoint *trackpoint;
114   gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
115   if (x < 0)
116     x = 0;
117   if (x > PROFILE_WIDTH)
118     x = PROFILE_WIDTH;
119
120   if (time_base)
121     trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
122   else
123     trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
124
125   if ( trackpoint ) {
126     VikCoord coord = trackpoint->coord;
127     vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
128     vik_layers_panel_emit_update ( vlp );
129   }
130 }
131
132 static void draw_graph_mark(GtkWidget *image, gdouble x, GdkGC *gc, PropSaved *saved_img)
133 {
134   GdkPixmap *pix;
135   const int saved_width = 5;
136
137   gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL);
138   if (saved_img->saved) {
139     gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0,
140         saved_img->pos, 0, -1, -1);
141     saved_img->saved = FALSE;
142     gtk_widget_queue_draw_area(image, saved_img->pos, 0,
143         saved_img->img->width, saved_img->img->height);
144   }
145   if ((x >= MARGIN) && (x < (PROFILE_WIDTH + MARGIN))) {
146     if (saved_img->img)
147       gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img,
148           x - (saved_width/2), 0, 0, 0, saved_img->img->width, saved_img->img->height);
149     else
150       saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix),
151           saved_img->img, x - (saved_width/2), 0, 0, 0, saved_width, PROFILE_HEIGHT);
152     saved_img->pos = x - (saved_width/2);
153     saved_img->saved = TRUE;
154     gdk_draw_line (GDK_DRAWABLE(pix), gc, x, 0, x, image->allocation.height);
155     /* redraw the area which contains the line, saved_width is just convenient */
156     gtk_widget_queue_draw_area(image, x - saved_width/2, 0, saved_width, PROFILE_HEIGHT);
157   }
158 }
159
160 static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, gboolean is_vt_graph )
161 {
162   VikTrack *tr = pass_along[0];
163   VikLayersPanel *vlp = pass_along[1];
164   PropWidgets *widgets = pass_along[2];
165   GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
166   GtkWidget *image = GTK_WIDGET(child->data);
167   GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box));
168
169
170   set_center_at_graph_position(event->x, event_box->allocation.width, vlp, tr, is_vt_graph);
171   draw_graph_mark(image, event->x, window->style->black_gc,
172       is_vt_graph ? &widgets->speed_graph_saved_img : &widgets->elev_graph_saved_img);
173   g_list_free(child);
174
175 }
176
177 static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
178 {
179   track_graph_click(event_box, event, pass_along, FALSE);
180   return TRUE;  /* don't call other (further) callbacks */
181 }
182
183 static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
184 {
185   track_graph_click(event_box, event, pass_along, TRUE);
186   return TRUE;  /* don't call other (further) callbacks */
187 }
188
189 void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
190 {
191   VikTrack *tr = pass_along[0];
192   PropWidgets *widgets = pass_along[2];
193   int mouse_x, mouse_y;
194   GdkModifierType state;
195
196   if (event->is_hint)
197     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
198   else
199     mouse_x = event->x;
200
201   gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
202   if (x < 0)
203     x = 0;
204   if (x > PROFILE_WIDTH)
205     x = PROFILE_WIDTH;
206
207   gdouble meters_from_start;
208   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, &meters_from_start );
209   if (trackpoint && widgets->w_dist_time) {
210     static gchar tmp_buf[20];
211     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m", meters_from_start);
212     gtk_label_set_text(GTK_LABEL(widgets->w_dist_time), tmp_buf);
213   }
214 }
215
216 void track_vt_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
217 {
218   VikTrack *tr = pass_along[0];
219   PropWidgets *widgets = pass_along[2];
220   int mouse_x, mouse_y;
221   GdkModifierType state;
222
223   if (event->is_hint)
224     gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
225   else
226     mouse_x = event->x;
227
228   gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
229   if (x < 0)
230     x = 0;
231   if (x > PROFILE_WIDTH)
232     x = PROFILE_WIDTH;
233
234   time_t seconds_from_start;
235   VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, &seconds_from_start );
236   if (trackpoint && widgets->w_dist_time) {
237     static gchar tmp_buf[20];
238     guint h, m, s;
239     h = seconds_from_start/3600;
240     m = (seconds_from_start - h*3600)/60;
241     s = seconds_from_start - (3600*h) - (60*m);
242     g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
243
244     gtk_label_set_text(GTK_LABEL(widgets->w_dist_time), tmp_buf);
245   }
246 }
247
248 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)
249 {
250   GList *iter;
251   gdouble dist = 0;
252   gdouble max_speed = 0;
253   gdouble total_length = vik_track_get_length_including_gaps(tr);
254
255   for (iter = tr->trackpoints->next; iter; iter = iter->next) {
256     if (!isnan(VIK_TRACKPOINT(iter->data)->speed))
257       max_speed = MAX(max_speed, VIK_TRACKPOINT(iter->data)->speed);
258   }
259   max_speed = max_speed * 110 / 100;
260
261   for (iter = tr->trackpoints->next; iter; iter = iter->next) {
262     int x, y_alt, y_speed;
263     gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST);
264     elev -= alt_offset;
265     dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
266       &(VIK_TRACKPOINT(iter->prev->data)->coord) );
267     x = (width * dist)/total_length + margin;
268     if ( elev != VIK_DEM_INVALID_ELEVATION ) {
269       y_alt = height - (height * elev)/alt_diff;
270       gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4);
271     }
272     if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
273       y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
274       gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
275     }
276   }
277 }
278
279 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
280 {
281   GdkPixmap *pix;
282   GtkWidget *image;
283   gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
284   gdouble mina, maxa;
285   GtkWidget *eventbox;
286   gpointer *pass_along;
287   guint i;
288
289   if ( altitudes == NULL ) {
290     *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
291     return NULL;
292   }
293
294   pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
295   image = gtk_image_new_from_pixmap ( pix, NULL );
296
297   GdkGC *no_alt_info = gdk_gc_new ( window->window );
298   GdkGC *dem_alt_gc = gdk_gc_new ( window->window );
299   GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
300   GdkColor color;
301
302   gdk_color_parse ( "yellow", &color );
303   gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
304   gdk_color_parse ( "green", &color );
305   gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
306   gdk_color_parse ( "red", &color );
307   gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
308
309
310   minmax_alt(altitudes, min_alt, max_alt);
311   mina = *min_alt;
312   maxa = *max_alt * 110 / 100;
313   if  (maxa-mina < MIN_ALT_DIFF) {
314     maxa = mina + MIN_ALT_DIFF;
315   }
316   
317   /* clear the image */
318   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], 
319                      TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
320   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], 
321                      TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
322
323   /* draw grid */
324 #define LABEL_FONT "Sans 8"
325   for (i=0; i<=LINES; i++) {
326     PangoFontDescription *pfd;
327     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
328     gchar s[32];
329
330     pfd = pango_font_description_from_string (LABEL_FONT);
331     pango_layout_set_font_description (pl, pfd);
332     pango_font_description_free (pfd);
333     sprintf(s, "%8dm", (int)(mina + (LINES-i)*(maxa-mina)/LINES));
334     pango_layout_set_text(pl, s, -1);
335     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0, 
336                     CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
337
338     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], 
339                    MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
340   }
341
342   /* draw elevations */
343   for ( i = 0; i < PROFILE_WIDTH; i++ )
344     if ( altitudes[i] == VIK_DEFAULT_ALTITUDE ) 
345       gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info, 
346                       i + MARGIN, 0, i + MARGIN, PROFILE_HEIGHT );
347     else 
348       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], 
349                       i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(altitudes[i]-mina)/(maxa-mina) );
350
351   draw_dem_alt_speed_dist(tr, GDK_DRAWABLE(pix), dem_alt_gc, gps_speed_gc, mina, maxa - mina, PROFILE_WIDTH, PROFILE_HEIGHT, MARGIN);
352
353   /* draw border */
354   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
355
356
357
358   g_object_unref ( G_OBJECT(pix) );
359   g_free ( altitudes );
360   g_object_unref ( G_OBJECT(no_alt_info) );
361   g_object_unref ( G_OBJECT(dem_alt_gc) );
362   g_object_unref ( G_OBJECT(gps_speed_gc) );
363
364   pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */
365   pass_along[0] = tr;
366   pass_along[1] = vlp;
367   pass_along[2] = widgets;
368
369   eventbox = gtk_event_box_new ();
370   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
371   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
372   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
373   gtk_container_add ( GTK_CONTAINER(eventbox), image );
374   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK
375                          | GDK_POINTER_MOTION_MASK
376                          | GDK_POINTER_MOTION_HINT_MASK);
377
378   return eventbox;
379 }
380
381 #define METRIC 1
382 #ifdef METRIC 
383 #define MTOK(v) ( (v)*3.6) /* m/s to km/h */
384 #else
385 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
386 #endif
387
388 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, PropWidgets *widgets)
389 {
390   GdkPixmap *pix;
391   GtkWidget *image;
392   gdouble mins, maxs;
393   guint i;
394   GtkWidget *eventbox;
395   gpointer *pass_along;
396
397   pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */
398   pass_along[0] = tr;
399   pass_along[1] = vlp;
400   pass_along[2] = widgets;
401
402   gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
403   if ( speeds == NULL )
404     return NULL;
405
406   pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
407   image = gtk_image_new_from_pixmap ( pix, NULL );
408
409   for (i=0; i<PROFILE_WIDTH; i++) {
410     speeds[i] = MTOK(speeds[i]);
411   }
412
413   minmax_alt(speeds, &mins, &maxs);
414   mins = 0; /* splines sometimes give negative speeds */
415   maxs = maxs * 110 / 100;
416   if  (maxs-mins < MIN_SPEED_DIFF) {
417     maxs = mins + MIN_SPEED_DIFF;
418   }
419   
420   /* clear the image */
421   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0], 
422                      TRUE, 0, 0, MARGIN, PROFILE_HEIGHT);
423   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0], 
424                      TRUE, MARGIN, 0, PROFILE_WIDTH, PROFILE_HEIGHT);
425
426 #if 0
427   /* XXX this can go out, it's just a helpful dev tool */
428   {
429     int j;
430     GdkGC **colors[8] = { window->style->bg_gc, window->style->fg_gc, 
431                          window->style->light_gc, 
432                          window->style->dark_gc, window->style->mid_gc, 
433                          window->style->text_gc, window->style->base_gc,
434                          window->style->text_aa_gc };
435     for (i=0; i<5; i++) {
436       for (j=0; j<8; j++) {
437         gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
438                            TRUE, i*20, j*20, 20, 20);
439         gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
440                            FALSE, i*20, j*20, 20, 20);
441       }
442     }
443   }
444 #else
445
446   /* draw grid */
447 #define LABEL_FONT "Sans 8"
448   for (i=0; i<=LINES; i++) {
449     PangoFontDescription *pfd;
450     PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
451     gchar s[32];
452
453     pfd = pango_font_description_from_string (LABEL_FONT);
454     pango_layout_set_font_description (pl, pfd);
455     pango_font_description_free (pfd);
456 #ifdef METRIC 
457     sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
458 #else
459     sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
460 #endif
461     pango_layout_set_text(pl, s, -1);
462     gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], 0, 
463                     CLAMP((int)i*PROFILE_HEIGHT/LINES - 5, 0, PROFILE_HEIGHT-15), pl);
464
465     gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0], 
466                    MARGIN, PROFILE_HEIGHT/LINES * i, MARGIN + PROFILE_WIDTH, PROFILE_HEIGHT/LINES * i);
467   }
468
469   /* draw speeds */
470   for ( i = 0; i < PROFILE_WIDTH; i++ )
471       gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3], 
472                       i + MARGIN, PROFILE_HEIGHT, i + MARGIN, PROFILE_HEIGHT-PROFILE_HEIGHT*(speeds[i]-mins)/(maxs-mins) );
473 #endif
474
475
476   GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
477   GdkColor color;
478
479   gdk_color_parse ( "red", &color );
480   gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
481
482   time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
483   time_t dur =  VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
484   GList *iter;
485   for (iter = tr->trackpoints; iter; iter = iter->next) {
486     gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
487     if (isnan(gps_speed))
488         continue;
489     int x = MARGIN + PROFILE_WIDTH * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
490     int y = PROFILE_HEIGHT - PROFILE_HEIGHT*(MTOK(gps_speed) - mins)/(maxs - mins);
491     gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
492   }
493
494   /* draw border */
495   gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
496
497   g_object_unref ( G_OBJECT(pix) );
498   g_object_unref ( G_OBJECT(gps_speed_gc) );
499   g_free ( speeds );
500
501   eventbox = gtk_event_box_new ();
502   g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along );
503   g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along );
504   g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
505   gtk_container_add ( GTK_CONTAINER(eventbox), image );
506   gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK
507                          | GDK_POINTER_MOTION_MASK
508                          | GDK_POINTER_MOTION_HINT_MASK);
509
510   return eventbox;
511 }
512 #undef MARGIN
513 #undef LINES
514
515 /* Notes: first and third arguments are swapped around compared to the manuals */
516 //static void propwin_response_cb( GtkDialog *widgets, gint resp, PropWidgets *widgets)
517 static void propwin_response_cb( gpointer p_widgets, gint resp, gpointer p_dialog)
518 {
519   PropWidgets *widgets = (PropWidgets *) p_widgets;
520   GtkDialog * dialog = p_dialog;
521
522   VikTrack *tr = widgets->tr;
523   VikTrwLayer *vtl = widgets->vtl;
524
525   /* FIXME: check and make sure the track still exists before doing anything to it */
526   /* Note: destroying diaglog (eg, parent window exit) won't give "response" */
527   switch (resp) {
528     case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
529     case GTK_RESPONSE_REJECT:
530       break;
531     case GTK_RESPONSE_ACCEPT:
532       vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
533       break;
534     case VIK_TRW_LAYER_PROPWIN_REVERSE:
535       vik_track_reverse(tr);
536       vik_layer_emit_update ( VIK_LAYER(vtl) );
537       break;
538     case VIK_TRW_LAYER_PROPWIN_DEL_DUP:
539       vik_track_remove_dup_points(tr);
540       /* above operation could have deleted current_tp or last_tp */
541       trw_layer_cancel_tps_of_track ( vtl, widgets->track_name );
542       vik_layer_emit_update ( VIK_LAYER(vtl) );
543       break;
544     case VIK_TRW_LAYER_PROPWIN_SPLIT:
545       {
546         /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */
547         guint ntracks;
548         VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
549         gchar *new_tr_name;
550         guint i;
551         for ( i = 0; i < ntracks; i++ )
552         {
553           g_assert ( tracks[i] );
554           new_tr_name = g_strdup_printf("%s #%d", widgets->track_name, i+1);
555           /* if ( (wp_exists) && (! overwrite) ) */
556           /* don't need to upper case new_tr_name because old tr name was uppercase */
557           if ( vik_trw_layer_get_track(vtl, new_tr_name ) && 
558              ( ! a_dialog_overwrite ( VIK_GTK_WINDOW_FROM_LAYER(vtl), "The track \"%s\" exists, do you wish to overwrite it?", new_tr_name ) ) )
559           {
560             gchar *new_new_tr_name = a_dialog_new_track ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl) );
561             g_free ( new_tr_name );
562             if (new_new_tr_name)
563               new_tr_name = new_new_tr_name;
564             else
565             {
566               new_tr_name = NULL;
567               vik_track_free ( tracks[i] );
568             }
569           }
570           if ( new_tr_name )
571             vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
572         }
573         if ( tracks )
574         {
575           g_free ( tracks );
576           /* Don't let track destroy this dialog */
577           vik_track_clear_property_dialog(tr);
578           vik_trw_layer_delete_track ( vtl, widgets->track_name );
579           vik_layer_emit_update ( VIK_LAYER(vtl) ); /* chase thru the hoops */
580         }
581       }
582       break;
583     default:
584       fprintf(stderr, "DEBUG: unknown response\n");
585       return;
586   }
587
588   /* Keep same behaviour for now: destroy dialog if click on any button */
589   prop_widgets_free(widgets);
590   vik_track_clear_property_dialog(tr);
591   gtk_widget_destroy ( GTK_WIDGET(dialog) );
592 }
593
594 void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name )
595 {
596   /* FIXME: free widgets when destroy signal received */
597   PropWidgets *widgets = prop_widgets_new();
598   widgets->vtl = vtl;
599   widgets->tr = tr;
600   widgets->vlp = vlp;
601   widgets->track_name = track_name;
602   gchar *title = g_strdup_printf("%s - Track Properties", track_name);
603   GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
604                          parent,
605                          GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
606                          GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
607                          "Split Segments", VIK_TRW_LAYER_PROPWIN_SPLIT,
608                          "Reverse",        VIK_TRW_LAYER_PROPWIN_REVERSE,
609                          "Delete Dupl.",   VIK_TRW_LAYER_PROPWIN_DEL_DUP,
610                          GTK_STOCK_OK,     GTK_RESPONSE_ACCEPT,
611                          NULL);
612   g_free(title);
613   g_signal_connect_swapped(dialog, "response", G_CALLBACK(propwin_response_cb), widgets);
614   //fprintf(stderr, "DEBUG: dialog=0x%p\n", dialog);
615   GtkTable *table;
616   gdouble tr_len;
617   guint32 tp_count, seg_count;
618
619   gdouble min_alt, max_alt;
620   GtkWidget *profile = vik_trw_layer_create_profile(GTK_WIDGET(parent),tr, vlp, widgets, &min_alt,&max_alt);
621   GtkWidget *vtdiag = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp, widgets);
622   GtkWidget *graphs = gtk_notebook_new();
623
624   GtkWidget *content[20];
625   int cnt;
626   int i;
627
628   static gchar *label_texts[] = { "<b>Comment:</b>", "<b>Track Length:</b>", "<b>Trackpoints:</b>", "<b>Segments:</b>", "<b>Duplicate Points:</b>", "<b>Max Speed:</b>", "<b>Avg. Speed:</b>", "<b>Avg. Dist. Between TPs:</b>", "<b>Elevation Range:</b>", "<b>Total Elevation Gain/Loss:</b>", "<b>Start:</b>",  "<b>End:</b>",  "<b>Duration:</b>", "<b>Track Distance/Time:</b>" };
629   static gchar tmp_buf[50];
630   gdouble tmp_speed;
631
632   cnt = 0;
633   widgets->w_comment = gtk_entry_new ();
634   if ( tr->comment )
635     gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment );
636   g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
637   content[cnt++] = widgets->w_comment;
638
639   tr_len = vik_track_get_length(tr);
640   g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", tr_len );
641   widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf );
642
643   tp_count = vik_track_get_tp_count(tr);
644   g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
645   widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf );
646
647   seg_count = vik_track_get_segment_count(tr) ;
648   g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
649   widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf );
650
651   g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
652   widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf );
653
654   tmp_speed = vik_track_get_max_speed(tr);
655   if ( tmp_speed == 0 )
656     g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
657   else
658     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s   (%.0f km/h)", tmp_speed, MTOK(tmp_speed) );
659   widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf );
660
661   tmp_speed = vik_track_get_average_speed(tr);
662   if ( tmp_speed == 0 )
663     g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
664   else
665     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s   (%.0f km/h)", tmp_speed, MTOK(tmp_speed) );
666   widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
667
668   g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
669   widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf );
670
671   if ( min_alt == VIK_DEFAULT_ALTITUDE )
672     g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
673   else
674     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
675   widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf );
676
677   vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
678   if ( min_alt == VIK_DEFAULT_ALTITUDE )
679     g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
680   else
681     g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
682   widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf );
683
684 #if 0
685 #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
686   gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0); 
687   PACK(l_len);
688   PACK(l_tps);
689   PACK(l_segs);
690   PACK(l_dups);
691   PACK(l_maxs);
692   PACK(l_avgs);
693   PACK(l_avgd);
694   PACK(l_elev);
695   PACK(l_galo);
696 #undef PACK;
697 #endif
698
699   if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
700   {
701     time_t t1, t2;
702     t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
703     t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
704
705     strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
706     tmp_buf[sizeof(tmp_buf)-1] = 0;
707     g_strchomp(tmp_buf);
708     widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf);
709
710     strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
711     tmp_buf[sizeof(tmp_buf)-1] = 0;
712     g_strchomp(tmp_buf);
713     widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf);
714
715     g_snprintf(tmp_buf, sizeof(tmp_buf), "%d minutes", (int)(t2-t1)/60);
716     widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf);
717   } else {
718     widgets->w_time_start = content[cnt++] = gtk_label_new("No Data");
719     widgets->w_time_end = content[cnt++] = gtk_label_new("No Data");
720     widgets->w_time_dur = content[cnt++] = gtk_label_new("No Data");
721   }
722   widgets->w_dist_time = content[cnt++] = gtk_label_new("No Data");
723
724   table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
725   gtk_table_set_col_spacing (table, 0, 10);
726   for (i=0; i<cnt; i++) {
727     GtkWidget *label;
728
729     label = gtk_label_new(NULL);
730     gtk_misc_set_alignment ( GTK_MISC(label), 1, 0 );
731     gtk_label_set_markup ( GTK_LABEL(label), label_texts[i] );
732     gtk_table_attach_defaults ( table, label, 0, 1, i, i+1 );
733     if (GTK_IS_MISC(content[i])) {
734       gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0 );
735     }
736     gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
737   }
738
739   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(table), FALSE, FALSE, 0);
740
741   if ( profile )
742     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
743
744   if ( vtdiag )
745     gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
746
747   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
748
749   if (seg_count <= 1)
750     gtk_dialog_set_response_sensitive(dialog, VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE);
751   if (vik_track_get_dup_point_count(tr) <= 0)
752     gtk_dialog_set_response_sensitive(dialog, VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE);
753
754   vik_track_set_property_dialog(tr, dialog);
755   gtk_widget_show_all ( dialog );
756 }