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