2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
29 #include "viktrwlayer.h"
30 #include "viktrwlayer_propwin.h"
31 #include "vikwaypoint.h"
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 */
43 #define PROFILE_WIDTH 600
44 #define PROFILE_HEIGHT 300
45 #define MIN_ALT_DIFF 100.0
46 #define MIN_SPEED_DIFF 20.0
48 typedef struct _propsaved {
54 typedef struct _propwidgets {
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;
77 static PropWidgets *prop_widgets_new()
79 PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
84 static void prop_widgets_free(PropWidgets *widgets)
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);
94 static void minmax_alt(const gdouble *altitudes, gdouble *min, gdouble *max)
99 for ( i=0; i < PROFILE_WIDTH; i++ ) {
100 if ( altitudes[i] != VIK_DEFAULT_ALTITUDE ) {
101 if ( altitudes[i] > *max )
103 if ( altitudes[i] < *min )
111 static void set_center_at_graph_position(gdouble event_x, gint img_width, VikLayersPanel *vlp, VikTrack *tr, gboolean time_base)
113 VikTrackpoint *trackpoint;
114 gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
117 if (x > PROFILE_WIDTH)
121 trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
123 trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
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 );
132 static void draw_graph_mark(GtkWidget *image, gdouble x, GdkGC *gc, PropSaved *saved_img)
135 const int saved_width = 5;
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);
145 if ((x >= MARGIN) && (x < (PROFILE_WIDTH + MARGIN))) {
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);
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);
160 static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, gboolean is_vt_graph )
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));
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);
177 static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
179 track_graph_click(event_box, event, pass_along, FALSE);
180 return TRUE; /* don't call other (further) callbacks */
183 static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
185 track_graph_click(event_box, event, pass_along, TRUE);
186 return TRUE; /* don't call other (further) callbacks */
189 void track_profile_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
191 VikTrack *tr = pass_along[0];
192 PropWidgets *widgets = pass_along[2];
193 int mouse_x, mouse_y;
194 GdkModifierType state;
197 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
201 gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
204 if (x > PROFILE_WIDTH)
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);
216 void track_vt_move( GtkWidget *image, GdkEventMotion *event, gpointer *pass_along )
218 VikTrack *tr = pass_along[0];
219 PropWidgets *widgets = pass_along[2];
220 int mouse_x, mouse_y;
221 GdkModifierType state;
224 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
228 gdouble x = mouse_x - image->allocation.width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
231 if (x > PROFILE_WIDTH)
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];
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);
244 gtk_label_set_text(GTK_LABEL(widgets->w_dist_time), tmp_buf);
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)
252 gdouble max_speed = 0;
253 gdouble total_length = vik_track_get_length_including_gaps(tr);
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);
259 max_speed = max_speed * 110 / 100;
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);
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);
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);
279 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
283 gdouble *altitudes = vik_track_make_elevation_map ( tr, PROFILE_WIDTH );
286 gpointer *pass_along;
289 if ( altitudes == NULL ) {
290 *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
294 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
295 image = gtk_image_new_from_pixmap ( pix, NULL );
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 );
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);
310 minmax_alt(altitudes, min_alt, max_alt);
312 maxa = *max_alt * 110 / 100;
313 if (maxa-mina < MIN_ALT_DIFF) {
314 maxa = mina + MIN_ALT_DIFF;
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);
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);
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);
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);
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 );
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) );
351 draw_dem_alt_speed_dist(tr, GDK_DRAWABLE(pix), dem_alt_gc, gps_speed_gc, mina, maxa - mina, PROFILE_WIDTH, PROFILE_HEIGHT, MARGIN);
354 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
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) );
364 pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */
367 pass_along[2] = widgets;
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);
383 #define MTOK(v) ( (v)*3.6) /* m/s to km/h */
385 #define MTOK(v) ( (v)*3600.0/1000.0 * 0.6214) /* m/s to mph - we'll handle this globally eventually but for now ...*/
388 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, PropWidgets *widgets)
395 gpointer *pass_along;
397 pass_along = g_malloc ( sizeof(gpointer) * 3 ); /* FIXME: mem leak -- never be freed */
400 pass_along[2] = widgets;
402 gdouble *speeds = vik_track_make_speed_map ( tr, PROFILE_WIDTH );
403 if ( speeds == NULL )
406 pix = gdk_pixmap_new( window->window, PROFILE_WIDTH + MARGIN, PROFILE_HEIGHT, -1 );
407 image = gtk_image_new_from_pixmap ( pix, NULL );
409 for (i=0; i<PROFILE_WIDTH; i++) {
410 speeds[i] = MTOK(speeds[i]);
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;
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);
427 /* XXX this can go out, it's just a helpful dev tool */
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);
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);
453 pfd = pango_font_description_from_string (LABEL_FONT);
454 pango_layout_set_font_description (pl, pfd);
455 pango_font_description_free (pfd);
457 sprintf(s, "%5dkm/h", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
459 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
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);
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);
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) );
476 GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
479 gdk_color_parse ( "red", &color );
480 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
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;
485 for (iter = tr->trackpoints; iter; iter = iter->next) {
486 gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
487 if (isnan(gps_speed))
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);
495 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, PROFILE_WIDTH-1, PROFILE_HEIGHT-1);
497 g_object_unref ( G_OBJECT(pix) );
498 g_object_unref ( G_OBJECT(gps_speed_gc) );
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);
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)
519 PropWidgets *widgets = (PropWidgets *) p_widgets;
520 GtkDialog * dialog = p_dialog;
522 VikTrack *tr = widgets->tr;
523 VikTrwLayer *vtl = widgets->vtl;
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" */
528 case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
529 case GTK_RESPONSE_REJECT:
531 case GTK_RESPONSE_ACCEPT:
532 vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
534 case VIK_TRW_LAYER_PROPWIN_REVERSE:
535 vik_track_reverse(tr);
536 vik_layer_emit_update ( VIK_LAYER(vtl) );
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) );
544 case VIK_TRW_LAYER_PROPWIN_SPLIT:
546 /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */
548 VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
551 for ( i = 0; i < ntracks; i++ )
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 ) ) )
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 );
563 new_tr_name = new_new_tr_name;
567 vik_track_free ( tracks[i] );
571 vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
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 */
584 fprintf(stderr, "DEBUG: unknown response\n");
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) );
594 void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name )
596 /* FIXME: free widgets when destroy signal received */
597 PropWidgets *widgets = prop_widgets_new();
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,
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,
613 g_signal_connect_swapped(dialog, "response", G_CALLBACK(propwin_response_cb), widgets);
614 //fprintf(stderr, "DEBUG: dialog=0x%p\n", dialog);
617 guint32 tp_count, seg_count;
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();
624 GtkWidget *content[20];
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];
633 widgets->w_comment = gtk_entry_new ();
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;
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 );
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 );
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 );
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 );
654 tmp_speed = vik_track_get_max_speed(tr);
655 if ( tmp_speed == 0 )
656 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
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 );
661 tmp_speed = vik_track_get_average_speed(tr);
662 if ( tmp_speed == 0 )
663 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
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 );
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 );
671 if ( min_alt == VIK_DEFAULT_ALTITUDE )
672 g_snprintf(tmp_buf, sizeof(tmp_buf), "No Data");
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 );
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");
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 );
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);
699 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
702 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
703 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
705 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
706 tmp_buf[sizeof(tmp_buf)-1] = 0;
708 widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf);
710 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
711 tmp_buf[sizeof(tmp_buf)-1] = 0;
713 widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf);
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);
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");
722 widgets->w_dist_time = content[cnt++] = gtk_label_new("No Data");
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++) {
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 );
736 gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
739 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(table), FALSE, FALSE, 0);
742 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), profile, gtk_label_new("Elevation-distance"));
745 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), vtdiag, gtk_label_new("Speed-time"));
747 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
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);
754 vik_track_set_property_dialog(tr, dialog);
755 gtk_widget_show_all ( dialog );