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