2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2005-2007, Alex Foobarian <foobarian@gmail.com>
6 * Copyright (C) 2007-2008, Quy Tonthat <qtonthat@gmail.com>
7 * Copyright (C) 2011, Rob Norris <rw_norris@hotmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <glib/gi18n.h>
42 #include "viktrwlayer.h"
43 #include "viktrwlayer_propwin.h"
44 #include "vikwaypoint.h"
49 #include "vikviewport.h" /* ugh */
50 #include "viktreeview.h" /* ugh */
51 #include <gdk-pixbuf/gdk-pixdata.h>
52 #include "viklayer.h" /* ugh */
53 #include "vikaggregatelayer.h"
54 #include "viklayerspanel.h" /* ugh */
56 #define PROPWIN_PROFILE_WIDTH 600
57 #define PROPWIN_PROFILE_HEIGHT 300
59 #define PROPWIN_LABEL_FONT "Sans 7"
62 PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE,
63 PROPWIN_GRAPH_TYPE_SPEED_TIME,
64 PROPWIN_GRAPH_TYPE_DISTANCE_TIME,
65 PROPWIN_GRAPH_TYPE_ELEVATION_TIME,
66 PROPWIN_GRAPH_TYPE_SPEED_DISTANCE,
67 PROPWIN_GRAPH_TYPE_END,
68 } VikPropWinGraphType_t;
70 /* (Hopefully!) Human friendly altitude grid sizes - note no fixed 'ratio' just numbers that look nice...*/
71 static const gdouble chunksa[] = {2.0, 5.0, 10.0, 15.0, 20.0,
72 25.0, 40.0, 50.0, 75.0, 100.0,
73 150.0, 200.0, 250.0, 375.0, 500.0,
74 750.0, 1000.0, 2000.0, 5000.0, 10000.0, 100000.0};
76 /* (Hopefully!) Human friendly grid sizes - note no fixed 'ratio' just numbers that look nice...*/
77 /* As need to cover walking speeds - have many low numbers (but also may go up to airplane speeds!) */
78 static const gdouble chunkss[] = {1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
79 15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
80 100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
81 750.0, 1000.0, 10000.0};
83 /* (Hopefully!) Human friendly distance grid sizes - note no fixed 'ratio' just numbers that look nice...*/
84 static const gdouble chunksd[] = {0.1, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
85 15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
86 100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
87 750.0, 1000.0, 10000.0};
89 typedef struct _propsaved {
94 typedef struct _propwidgets {
95 gboolean configure_dialog;
101 gint profile_width_old;
102 gint profile_height_old;
103 gint profile_width_offset;
104 gint profile_height_offset;
106 GtkWidget *w_comment;
107 GtkWidget *w_track_length;
108 GtkWidget *w_tp_count;
109 GtkWidget *w_segment_count;
110 GtkWidget *w_duptp_count;
111 GtkWidget *w_max_speed;
112 GtkWidget *w_avg_speed;
113 GtkWidget *w_mvg_speed;
114 GtkWidget *w_avg_dist;
115 GtkWidget *w_elev_range;
116 GtkWidget *w_elev_gain;
117 GtkWidget *w_time_start;
118 GtkWidget *w_time_end;
119 GtkWidget *w_time_dur;
120 GtkWidget *w_cur_dist; /*< Current distance */
121 GtkWidget *w_cur_elevation;
122 GtkWidget *w_cur_time; /*< Current time */
123 GtkWidget *w_cur_speed;
124 GtkWidget *w_cur_dist_dist; /*< Current distance on distance graph */
125 GtkWidget *w_cur_dist_time; /*< Current time on distance graph */
126 GtkWidget *w_cur_elev_elev;
127 GtkWidget *w_cur_elev_time;
128 GtkWidget *w_cur_speed_dist;
129 GtkWidget *w_cur_speed_speed;
130 GtkWidget *w_show_dem;
131 GtkWidget *w_show_alt_gps_speed;
132 GtkWidget *w_show_gps_speed;
133 GtkWidget *w_show_dist_speed;
134 GtkWidget *w_show_elev_speed;
135 GtkWidget *w_show_sd_gps_speed;
136 gdouble track_length;
137 gdouble track_length_inc_gaps;
138 PropSaved elev_graph_saved_img;
139 PropSaved speed_graph_saved_img;
140 PropSaved dist_graph_saved_img;
141 PropSaved elev_time_graph_saved_img;
142 PropSaved speed_dist_graph_saved_img;
144 GtkWidget *speed_box;
146 GtkWidget *elev_time_box;
147 GtkWidget *speed_dist_box;
149 gdouble *ats; // altitudes in time
150 gdouble min_altitude;
151 gdouble max_altitude;
152 gdouble draw_min_altitude;
153 gdouble draw_min_altitude_time;
154 gint cia; // Chunk size Index into Altitudes
155 gint ciat; // Chunk size Index into Altitudes / Time
156 // NB cia & ciat are normally same value but sometimes not due to differing methods of altitude array creation
157 // thus also have draw_min_altitude for each altitude graph type
159 gdouble *speeds_dist;
162 gdouble draw_min_speed;
163 gdouble max_speed_dist;
164 gint cis; // Chunk size Index into Speeds
165 gint cisd; // Chunk size Index into Speed/Distance
167 gint cid; // Chunk size Index into Distance
168 VikTrackpoint *marker_tp;
169 gboolean is_marker_drawn;
170 VikTrackpoint *blob_tp;
171 gboolean is_blob_drawn;
174 static PropWidgets *prop_widgets_new()
176 PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
181 static void prop_widgets_free(PropWidgets *widgets)
183 if (widgets->elev_graph_saved_img.img)
184 g_object_unref(widgets->elev_graph_saved_img.img);
185 if (widgets->speed_graph_saved_img.img)
186 g_object_unref(widgets->speed_graph_saved_img.img);
187 if (widgets->dist_graph_saved_img.img)
188 g_object_unref(widgets->dist_graph_saved_img.img);
189 if (widgets->elev_time_graph_saved_img.img)
190 g_object_unref(widgets->elev_time_graph_saved_img.img);
191 if (widgets->speed_dist_graph_saved_img.img)
192 g_object_unref(widgets->speed_dist_graph_saved_img.img);
193 if (widgets->altitudes)
194 g_free(widgets->altitudes);
196 g_free(widgets->speeds);
197 if (widgets->distances)
198 g_free(widgets->distances);
200 g_free(widgets->ats);
201 if (widgets->speeds_dist)
202 g_free(widgets->speeds_dist);
206 static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gboolean NO_ALT_TEST, gint PROFILE_WIDTH)
211 for ( i=0; i < PROFILE_WIDTH; i++ ) {
212 if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) {
213 if ( array[i] > *max )
215 if ( array[i] < *min )
224 * Returns via pointers:
225 * the new minimum value to be used for the altitude graph
226 * the index in to the altitudes chunk sizes array (ci = Chunk Index)
228 static void get_new_min_and_chunk_index_altitude (gdouble mina, gdouble maxa, gdouble *new_min, gint *ci)
230 /* Get unitized chunk */
231 /* Find suitable chunk index */
233 gdouble diffa_chunk = (maxa - mina)/LINES;
235 /* Loop through to find best match */
236 while (diffa_chunk > chunksa[*ci]) {
238 /* Last Resort Check */
239 if ( *ci == sizeof(chunksa)/sizeof(chunksa[0]) )
243 /* Ensure adjusted minimum .. maximum covers mina->maxa */
245 // Now work out adjusted minimum point to the nearest lowest chunk divisor value
246 // When negative ensure logic uses lowest value
248 *new_min = (gdouble) ( ( (gint)(mina - chunksa[*ci]) / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
250 *new_min = (gdouble) ( ( (gint)mina / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
252 // Range not big enough - as new minimum has lowered
253 if ((*new_min + (chunksa[*ci] * LINES) < maxa)) {
254 // Next chunk should cover it
255 if ( *ci < sizeof(chunksa)/sizeof(chunksa[0]) ) {
257 // Remember to adjust the minimum too...
259 *new_min = (gdouble) ( ( (gint)(mina - chunksa[*ci]) / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
261 *new_min = (gdouble) ( ( (gint)mina / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
267 * Returns via pointers:
268 * the new minimum value to be used for the appropriate graph
269 * the index in to that array (ci = Chunk Index)
271 static void get_new_min_and_chunk_index (gdouble mins, gdouble maxs, const gdouble *chunks, size_t chunky, gdouble *new_min, gint *ci)
274 gdouble diff_chunk = (maxs - mins)/LINES;
276 /* Loop through to find best match */
277 while (diff_chunk > chunks[*ci]) {
279 /* Last Resort Check */
283 *new_min = (gdouble) ( (gint)((mins / chunks[*ci] ) * chunks[*ci]) );
285 // Speeds are never negative so don't need to worry about a negative new minimum
288 static VikTrackpoint *set_center_at_graph_position(gdouble event_x,
297 VikTrackpoint *trackpoint;
298 gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
301 if (x > PROFILE_WIDTH)
305 trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
307 trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
310 VikCoord coord = trackpoint->coord;
312 vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
313 vik_layers_panel_emit_update ( vlp );
316 /* since vlp not set, vvp should be valid instead! */
318 vik_viewport_set_center_coord ( vvp, &coord );
319 vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
326 * Returns whether the marker was drawn or not and whether the blob was drawn or not
328 static void save_image_and_draw_graph_marks (GtkWidget *image,
333 PropSaved *saved_img,
336 gboolean *marker_drawn,
337 gboolean *blob_drawn)
339 GdkPixmap *pix = NULL;
340 /* the pixmap = margin + graph area */
341 gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL);
343 /* Restore previously saved image */
344 if (saved_img->saved) {
345 gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
346 saved_img->saved = FALSE;
349 // ATM always save whole image - as anywhere could have changed
351 gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
353 saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
354 saved_img->saved = TRUE;
356 if ((marker_x >= MARGIN) && (marker_x < (PROFILE_WIDTH + MARGIN))) {
357 gdk_draw_line (GDK_DRAWABLE(pix), gc, marker_x, 0, marker_x, image->allocation.height);
358 *marker_drawn = TRUE;
361 *marker_drawn = FALSE;
363 // Draw a square blob to indicate where we are on track for this graph
364 if ( (blob_x >= MARGIN) && (blob_x < (PROFILE_WIDTH + MARGIN)) && (blob_y < PROFILE_HEIGHT) ) {
365 gdk_draw_rectangle (GDK_DRAWABLE(pix), gc, TRUE, blob_x-3, blob_y-3, 6, 6);
371 // Anywhere on image could have changed
372 if (*marker_drawn || *blob_drawn)
373 gtk_widget_queue_draw(image);
377 * Return the percentage of how far a trackpoint is a long a track via the time method
379 static gdouble tp_percentage_by_time ( VikTrack *tr, VikTrackpoint *trackpoint )
382 if (trackpoint == NULL)
384 time_t t_start, t_end, t_total;
385 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
386 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
387 t_total = t_end - t_start;
388 pc = (gdouble)(trackpoint->timestamp - t_start)/t_total;
393 * Return the percentage of how far a trackpoint is a long a track via the distance method
395 static gdouble tp_percentage_by_distance ( VikTrack *tr, VikTrackpoint *trackpoint, gdouble track_length )
398 if (trackpoint == NULL)
402 for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) {
403 dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord),
404 &(VIK_TRACKPOINT(iter->prev->data)->coord));
405 /* Assuming trackpoint is not a copy */
406 if (trackpoint == VIK_TRACKPOINT(iter->data))
410 pc = dist/track_length;
414 static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along, VikPropWinGraphType_t graph_type )
416 VikTrack *tr = pass_along[0];
417 VikLayersPanel *vlp = pass_along[1];
418 VikViewport *vvp = pass_along[2];
419 PropWidgets *widgets = pass_along[3];
421 gboolean is_time_graph =
422 ( graph_type == PROPWIN_GRAPH_TYPE_SPEED_TIME ||
423 graph_type == PROPWIN_GRAPH_TYPE_DISTANCE_TIME ||
424 graph_type == PROPWIN_GRAPH_TYPE_ELEVATION_TIME );
426 VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, widgets->vtl, vlp, vvp, tr, is_time_graph, widgets->profile_width);
427 // Unable to get the point so give up
428 if ( trackpoint == NULL ) {
429 gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
433 widgets->marker_tp = trackpoint;
437 GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box));
438 GtkWidget *graph_box;
439 PropSaved *graph_saved_img;
442 // Attempt to redraw marker on all graph types
444 for ( graphite = PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE;
445 graphite < PROPWIN_GRAPH_TYPE_END;
448 // Switch commonal variables to particular graph type
451 case PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE:
452 graph_box = widgets->elev_box;
453 graph_saved_img = &widgets->elev_graph_saved_img;
454 is_time_graph = FALSE;
456 case PROPWIN_GRAPH_TYPE_SPEED_TIME:
457 graph_box = widgets->speed_box;
458 graph_saved_img = &widgets->speed_graph_saved_img;
459 is_time_graph = TRUE;
461 case PROPWIN_GRAPH_TYPE_DISTANCE_TIME:
462 graph_box = widgets->dist_box;
463 graph_saved_img = &widgets->dist_graph_saved_img;
464 is_time_graph = TRUE;
466 case PROPWIN_GRAPH_TYPE_ELEVATION_TIME:
467 graph_box = widgets->elev_time_box;
468 graph_saved_img = &widgets->elev_time_graph_saved_img;
469 is_time_graph = TRUE;
471 case PROPWIN_GRAPH_TYPE_SPEED_DISTANCE:
472 graph_box = widgets->speed_dist_box;
473 graph_saved_img = &widgets->speed_dist_graph_saved_img;
474 is_time_graph = FALSE;
478 // Commonal method of redrawing marker
481 child = gtk_container_get_children(GTK_CONTAINER(graph_box));
482 image = GTK_WIDGET(child->data);
485 pc = tp_percentage_by_time ( tr, trackpoint );
487 pc = tp_percentage_by_distance ( tr, trackpoint, widgets->track_length_inc_gaps );
490 gdouble marker_x = (pc * widgets->profile_width) + MARGIN;
491 save_image_and_draw_graph_marks(image,
493 window->style->black_gc,
494 -1, // Don't draw blob on clicks
497 widgets->profile_width,
498 widgets->profile_height,
499 &widgets->is_marker_drawn,
500 &widgets->is_blob_drawn);
506 gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, widgets->is_marker_drawn);
509 static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
511 track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE);
512 return TRUE; /* don't call other (further) callbacks */
515 static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
517 track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_SPEED_TIME);
518 return TRUE; /* don't call other (further) callbacks */
521 static gboolean track_dt_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
523 track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_DISTANCE_TIME);
524 return TRUE; /* don't call other (further) callbacks */
527 static gboolean track_et_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
529 track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_ELEVATION_TIME);
530 return TRUE; /* don't call other (further) callbacks */
533 static gboolean track_sd_click( GtkWidget *event_box, GdkEventButton *event, gpointer *pass_along )
535 track_graph_click(event_box, event, pass_along, PROPWIN_GRAPH_TYPE_SPEED_DISTANCE);
536 return TRUE; /* don't call other (further) callbacks */
540 * Calculate y position for blob on elevation graph
542 static gint blobby_altitude ( gdouble x_blob, PropWidgets *widgets )
544 gint ix = (gint)x_blob;
545 // Ensure ix is inbounds
546 if (ix == widgets->profile_width)
549 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->altitudes[ix]-widgets->draw_min_altitude)/(chunksa[widgets->cia]*LINES);
555 * Calculate y position for blob on speed graph
557 static gint blobby_speed ( gdouble x_blob, PropWidgets *widgets )
559 gint ix = (gint)x_blob;
560 // Ensure ix is inbounds
561 if (ix == widgets->profile_width)
564 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->speeds[ix]-widgets->draw_min_speed)/(chunkss[widgets->cis]*LINES);
570 * Calculate y position for blob on distance graph
572 static gint blobby_distance ( gdouble x_blob, PropWidgets *widgets )
574 gint ix = (gint)x_blob;
575 // Ensure ix is inbounds
576 if (ix == widgets->profile_width)
579 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->distances[ix])/(chunksd[widgets->cid]*LINES);
580 //NB min distance is always 0, so no need to subtract that from this ______/
586 * Calculate y position for blob on elevation/time graph
588 static gint blobby_altitude_time ( gdouble x_blob, PropWidgets *widgets )
590 gint ix = (gint)x_blob;
591 // Ensure ix is inbounds
592 if (ix == widgets->profile_width)
595 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->ats[ix]-widgets->draw_min_altitude_time)/(chunksa[widgets->ciat]*LINES);
600 * Calculate y position for blob on speed/dist graph
602 static gint blobby_speed_dist ( gdouble x_blob, PropWidgets *widgets )
604 gint ix = (gint)x_blob;
605 // Ensure ix is inbounds
606 if (ix == widgets->profile_width)
609 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->speeds_dist[ix]-widgets->draw_min_speed)/(chunkss[widgets->cisd]*LINES);
615 void track_profile_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
617 VikTrack *tr = pass_along[0];
618 PropWidgets *widgets = pass_along[3];
619 int mouse_x, mouse_y;
620 GdkModifierType state;
623 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
627 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
630 if (x > widgets->profile_width)
631 x = widgets->profile_width;
633 gdouble meters_from_start;
634 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
635 if (trackpoint && widgets->w_cur_dist) {
636 static gchar tmp_buf[20];
637 vik_units_distance_t dist_units = a_vik_get_units_distance ();
638 switch (dist_units) {
639 case VIK_UNITS_DISTANCE_KILOMETRES:
640 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
642 case VIK_UNITS_DISTANCE_MILES:
643 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
646 g_critical("Houston, we've had a problem. distance=%d", dist_units);
648 gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf);
651 // Show track elevation for this position - to the nearest whole number
652 if (trackpoint && widgets->w_cur_elevation) {
653 static gchar tmp_buf[20];
654 if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
655 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude));
657 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude);
658 gtk_label_set_text(GTK_LABEL(widgets->w_cur_elevation), tmp_buf);
661 widgets->blob_tp = trackpoint;
663 if ( widgets->altitudes == NULL )
666 GtkWidget *window = gtk_widget_get_toplevel (event_box);
667 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
668 GtkWidget *image = GTK_WIDGET(child->data);
670 gint y_blob = blobby_altitude (x, widgets);
672 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
673 if (widgets->is_marker_drawn) {
674 gdouble pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
676 marker_x = (pc * widgets->profile_width) + MARGIN;
680 save_image_and_draw_graph_marks (image,
682 window->style->black_gc,
685 &widgets->elev_graph_saved_img,
686 widgets->profile_width,
687 widgets->profile_height,
688 &widgets->is_marker_drawn,
689 &widgets->is_blob_drawn);
694 void track_vt_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
696 VikTrack *tr = pass_along[0];
697 PropWidgets *widgets = pass_along[3];
698 int mouse_x, mouse_y;
699 GdkModifierType state;
702 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
706 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
709 if (x > widgets->profile_width)
710 x = widgets->profile_width;
712 time_t seconds_from_start;
713 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
714 if (trackpoint && widgets->w_cur_time) {
715 static gchar tmp_buf[20];
717 h = seconds_from_start/3600;
718 m = (seconds_from_start - h*3600)/60;
719 s = seconds_from_start - (3600*h) - (60*m);
720 g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
722 gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf);
726 // Ensure ix is inbounds
727 if (ix == widgets->profile_width)
730 // Show track speed for this position
731 if (trackpoint && widgets->w_cur_speed) {
732 static gchar tmp_buf[20];
733 // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown
734 // No conversions needed as already in appropriate units
735 vik_units_speed_t speed_units = a_vik_get_units_speed ();
736 switch (speed_units) {
737 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
738 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), widgets->speeds[ix]);
740 case VIK_UNITS_SPEED_MILES_PER_HOUR:
741 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds[ix]);
743 case VIK_UNITS_SPEED_KNOTS:
744 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds[ix]);
747 // VIK_UNITS_SPEED_METRES_PER_SECOND:
748 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds[ix]);
751 gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed), tmp_buf);
754 widgets->blob_tp = trackpoint;
756 if ( widgets->speeds == NULL )
759 GtkWidget *window = gtk_widget_get_toplevel (event_box);
760 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
761 GtkWidget *image = GTK_WIDGET(child->data);
763 gint y_blob = blobby_speed (x, widgets);
765 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
766 if (widgets->is_marker_drawn) {
767 gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
769 marker_x = (pc * widgets->profile_width) + MARGIN;
773 save_image_and_draw_graph_marks (image,
775 window->style->black_gc,
778 &widgets->speed_graph_saved_img,
779 widgets->profile_width,
780 widgets->profile_height,
781 &widgets->is_marker_drawn,
782 &widgets->is_blob_drawn);
788 * Update labels and blob marker on mouse moves in the distance/time graph
790 void track_dt_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
792 VikTrack *tr = pass_along[0];
793 PropWidgets *widgets = pass_along[3];
794 int mouse_x, mouse_y;
795 GdkModifierType state;
798 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
802 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
805 if (x > widgets->profile_width)
806 x = widgets->profile_width;
808 time_t seconds_from_start;
809 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
810 if (trackpoint && widgets->w_cur_dist_time) {
811 static gchar tmp_buf[20];
813 h = seconds_from_start/3600;
814 m = (seconds_from_start - h*3600)/60;
815 s = seconds_from_start - (3600*h) - (60*m);
816 g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
818 gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_time), tmp_buf);
822 // Ensure ix is inbounds
823 if (ix == widgets->profile_width)
826 if (trackpoint && widgets->w_cur_dist_dist) {
827 static gchar tmp_buf[20];
828 if ( a_vik_get_units_distance () == VIK_UNITS_DISTANCE_MILES )
829 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", widgets->distances[ix]);
831 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", widgets->distances[ix]);
832 gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_dist), tmp_buf);
835 widgets->blob_tp = trackpoint;
837 if ( widgets->distances == NULL )
840 GtkWidget *window = gtk_widget_get_toplevel (event_box);
841 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
842 GtkWidget *image = GTK_WIDGET(child->data);
844 gint y_blob = blobby_distance (x, widgets);
846 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
847 if (widgets->is_marker_drawn) {
848 gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
850 marker_x = (pc * widgets->profile_width) + MARGIN;
854 save_image_and_draw_graph_marks (image,
856 window->style->black_gc,
859 &widgets->dist_graph_saved_img,
860 widgets->profile_width,
861 widgets->profile_height,
862 &widgets->is_marker_drawn,
863 &widgets->is_blob_drawn);
869 * Update labels and blob marker on mouse moves in the elevation/time graph
871 void track_et_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
873 VikTrack *tr = pass_along[0];
874 PropWidgets *widgets = pass_along[3];
875 int mouse_x, mouse_y;
876 GdkModifierType state;
879 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
883 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
886 if (x > widgets->profile_width)
887 x = widgets->profile_width;
889 time_t seconds_from_start;
890 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
891 if (trackpoint && widgets->w_cur_elev_time) {
892 static gchar tmp_buf[20];
894 h = seconds_from_start/3600;
895 m = (seconds_from_start - h*3600)/60;
896 s = seconds_from_start - (3600*h) - (60*m);
897 g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
899 gtk_label_set_text(GTK_LABEL(widgets->w_cur_elev_time), tmp_buf);
903 // Ensure ix is inbounds
904 if (ix == widgets->profile_width)
907 if (trackpoint && widgets->w_cur_elev_elev) {
908 static gchar tmp_buf[20];
909 if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
910 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude));
912 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude);
913 gtk_label_set_text(GTK_LABEL(widgets->w_cur_elev_elev), tmp_buf);
916 widgets->blob_tp = trackpoint;
918 if ( widgets->ats == NULL )
921 GtkWidget *window = gtk_widget_get_toplevel (event_box);
922 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
923 GtkWidget *image = GTK_WIDGET(child->data);
925 gint y_blob = blobby_altitude_time (x, widgets);
927 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
928 if (widgets->is_marker_drawn) {
929 gdouble pc = tp_percentage_by_time ( tr, widgets->marker_tp );
931 marker_x = (pc * widgets->profile_width) + MARGIN;
935 save_image_and_draw_graph_marks (image,
937 window->style->black_gc,
940 &widgets->elev_time_graph_saved_img,
941 widgets->profile_width,
942 widgets->profile_height,
943 &widgets->is_marker_drawn,
944 &widgets->is_blob_drawn);
949 void track_sd_move( GtkWidget *event_box, GdkEventMotion *event, gpointer *pass_along )
951 VikTrack *tr = pass_along[0];
952 PropWidgets *widgets = pass_along[3];
953 int mouse_x, mouse_y;
954 GdkModifierType state;
957 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
961 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
964 if (x > widgets->profile_width)
965 x = widgets->profile_width;
967 gdouble meters_from_start;
968 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / widgets->profile_width, &meters_from_start );
969 if (trackpoint && widgets->w_cur_speed_dist) {
970 static gchar tmp_buf[20];
971 vik_units_distance_t dist_units = a_vik_get_units_distance ();
972 switch (dist_units) {
973 case VIK_UNITS_DISTANCE_KILOMETRES:
974 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
976 case VIK_UNITS_DISTANCE_MILES:
977 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
980 g_critical("Houston, we've had a problem. distance=%d", dist_units);
982 gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed_dist), tmp_buf);
986 // Ensure ix is inbounds
987 if (ix == widgets->profile_width)
990 if ( widgets->speeds_dist == NULL )
993 // Show track speed for this position
994 if (widgets->w_cur_speed_speed) {
995 static gchar tmp_buf[20];
996 // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown
997 // No conversions needed as already in appropriate units
998 vik_units_speed_t speed_units = a_vik_get_units_speed ();
999 switch (speed_units) {
1000 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1001 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), widgets->speeds_dist[ix]);
1003 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1004 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds_dist[ix]);
1006 case VIK_UNITS_SPEED_KNOTS:
1007 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds_dist[ix]);
1010 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1011 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds_dist[ix]);
1014 gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed_speed), tmp_buf);
1017 widgets->blob_tp = trackpoint;
1019 GtkWidget *window = gtk_widget_get_toplevel (event_box);
1020 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
1021 GtkWidget *image = GTK_WIDGET(child->data);
1023 gint y_blob = blobby_speed_dist (x, widgets);
1025 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1026 if (widgets->is_marker_drawn) {
1027 gdouble pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
1029 marker_x = (pc * widgets->profile_width) + MARGIN;
1033 save_image_and_draw_graph_marks (image,
1035 window->style->black_gc,
1038 &widgets->speed_dist_graph_saved_img,
1039 widgets->profile_width,
1040 widgets->profile_height,
1041 &widgets->is_marker_drawn,
1042 &widgets->is_blob_drawn);
1048 * Draws DEM points and a respresentative speed on the supplied pixmap
1049 * (which is the elevations graph)
1051 static void draw_dem_alt_speed_dist(VikTrack *tr,
1057 gdouble max_speed_in,
1066 gdouble max_speed = 0;
1067 gdouble total_length = vik_track_get_length_including_gaps(tr);
1069 // Calculate the max speed factor
1071 max_speed = max_speed_in * 110 / 100;
1074 for (iter = tr->trackpoints->next; iter; iter = iter->next) {
1076 dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1077 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1078 x = (width * dist)/total_length + margin;
1080 gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST);
1082 if ( elev != VIK_DEM_INVALID_ELEVATION ) {
1083 // Convert into height units
1084 if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
1085 elev = VIK_METERS_TO_FEET(elev);
1086 // No conversion needed if already in metres
1088 // consider chunk size
1089 int y_alt = height - ((height * elev)/(chunksa[cia]*LINES) );
1090 gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4);
1094 // This is just a speed indicator - no actual values can be inferred by user
1095 if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
1096 int y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
1097 gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
1104 * Draw just the height profile image
1106 static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1116 // Free previous allocation
1117 if ( widgets->altitudes )
1118 g_free ( widgets->altitudes );
1120 widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
1122 if ( widgets->altitudes == NULL )
1125 // Convert into appropriate units
1126 vik_units_height_t height_units = a_vik_get_units_height ();
1127 if ( height_units == VIK_UNITS_HEIGHT_FEET ) {
1128 // Convert altitudes into feet units
1129 for ( i = 0; i < widgets->profile_width; i++ ) {
1130 widgets->altitudes[i] = VIK_METERS_TO_FEET(widgets->altitudes[i]);
1133 // Otherwise leave in metres
1135 minmax_array(widgets->altitudes, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
1137 get_new_min_and_chunk_index_altitude (widgets->min_altitude, widgets->max_altitude, &widgets->draw_min_altitude, &widgets->cia);
1140 mina = widgets->draw_min_altitude;
1141 maxa = widgets->max_altitude;
1143 window = gtk_widget_get_toplevel (widgets->elev_box);
1145 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1147 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1149 no_alt_info = gdk_gc_new ( window->window );
1150 gdk_color_parse ( "yellow", &color );
1151 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
1153 /* clear the image */
1154 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1155 TRUE, 0, 0, MARGIN, widgets->profile_height);
1156 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1157 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1160 for (i=0; i<=LINES; i++) {
1161 PangoFontDescription *pfd;
1162 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1166 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1167 pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1168 pango_layout_set_font_description (pl, pfd);
1169 pango_font_description_free (pfd);
1170 switch (height_units) {
1171 case VIK_UNITS_HEIGHT_METRES:
1172 sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
1174 case VIK_UNITS_HEIGHT_FEET:
1175 // NB values already converted into feet
1176 sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
1180 g_critical("Houston, we've had a problem. height=%d", height_units);
1182 pango_layout_set_text(pl, s, -1);
1183 pango_layout_get_pixel_size (pl, &w, &h);
1184 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
1185 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1187 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
1188 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1189 g_object_unref ( G_OBJECT ( pl ) );
1193 /* draw elevations */
1194 for ( i = 0; i < widgets->profile_width; i++ )
1195 if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE )
1196 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
1197 i + MARGIN, 0, i + MARGIN, widgets->profile_height );
1199 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
1200 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->altitudes[i]-mina)/(chunksa[widgets->cia]*LINES) );
1202 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_dem)) ||
1203 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)) ) {
1205 GdkGC *dem_alt_gc = gdk_gc_new ( window->window );
1206 GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
1208 gdk_color_parse ( "green", &color );
1209 gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
1211 gdk_color_parse ( "red", &color );
1212 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1214 // Ensure somekind of max speed when not set
1215 if ( widgets->max_speed < 0.01 )
1216 widgets->max_speed = vik_track_get_max_speed(tr);
1218 draw_dem_alt_speed_dist(tr,
1226 widgets->profile_width,
1227 widgets->profile_height,
1229 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_dem)),
1230 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)));
1232 g_object_unref ( G_OBJECT(dem_alt_gc) );
1233 g_object_unref ( G_OBJECT(gps_speed_gc) );
1237 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1239 g_object_unref ( G_OBJECT(pix) );
1240 g_object_unref ( G_OBJECT(no_alt_info) );
1244 * Draw just the speed (velocity)/time image
1246 static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
1253 // Free previous allocation
1254 if ( widgets->speeds )
1255 g_free ( widgets->speeds );
1257 widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
1258 if ( widgets->speeds == NULL )
1261 // Convert into appropriate units
1262 vik_units_speed_t speed_units = a_vik_get_units_speed ();
1263 switch (speed_units) {
1264 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1265 for ( i = 0; i < widgets->profile_width; i++ ) {
1266 widgets->speeds[i] = VIK_MPS_TO_KPH(widgets->speeds[i]);
1269 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1270 for ( i = 0; i < widgets->profile_width; i++ ) {
1271 widgets->speeds[i] = VIK_MPS_TO_MPH(widgets->speeds[i]);
1274 case VIK_UNITS_SPEED_KNOTS:
1275 for ( i = 0; i < widgets->profile_width; i++ ) {
1276 widgets->speeds[i] = VIK_MPS_TO_KNOTS(widgets->speeds[i]);
1280 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1281 // No need to convert as already in m/s
1285 window = gtk_widget_get_toplevel (widgets->speed_box);
1287 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1289 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1291 minmax_array(widgets->speeds, &widgets->min_speed, &widgets->max_speed, FALSE, widgets->profile_width);
1292 if (widgets->min_speed < 0.0)
1293 widgets->min_speed = 0; /* splines sometimes give negative speeds */
1295 /* Find suitable chunk index */
1296 get_new_min_and_chunk_index (widgets->min_speed, widgets->max_speed, chunkss, sizeof(chunkss)/sizeof(chunkss[0]), &widgets->draw_min_speed, &widgets->cis);
1299 mins = widgets->draw_min_speed;
1301 /* clear the image */
1302 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1303 TRUE, 0, 0, MARGIN, widgets->profile_height);
1304 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1305 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1308 for (i=0; i<=LINES; i++) {
1309 PangoFontDescription *pfd;
1310 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1314 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1315 pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1316 pango_layout_set_font_description (pl, pfd);
1317 pango_font_description_free (pfd);
1318 // NB: No need to convert here anymore as numbers are in the appropriate units
1319 switch (speed_units) {
1320 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1321 sprintf(s, "%8dkm/h", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1323 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1324 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1326 case VIK_UNITS_SPEED_METRES_PER_SECOND:
1327 sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1329 case VIK_UNITS_SPEED_KNOTS:
1330 sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
1334 g_critical("Houston, we've had a problem. speed=%d", speed_units);
1337 pango_layout_set_text(pl, s, -1);
1338 pango_layout_get_pixel_size (pl, &w, &h);
1339 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
1340 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1342 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
1343 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1344 g_object_unref ( G_OBJECT ( pl ) );
1350 for ( i = 0; i < widgets->profile_width; i++ )
1351 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
1352 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(chunkss[widgets->cis]*LINES) );
1355 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_gps_speed)) ) {
1357 GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
1359 gdk_color_parse ( "red", &color );
1360 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1362 time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1363 time_t dur = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
1366 for (iter = tr->trackpoints; iter; iter = iter->next) {
1367 gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
1368 if (isnan(gps_speed))
1370 switch (speed_units) {
1371 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1372 gps_speed = VIK_MPS_TO_KPH(gps_speed);
1374 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1375 gps_speed = VIK_MPS_TO_MPH(gps_speed);
1377 case VIK_UNITS_SPEED_KNOTS:
1378 gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
1381 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1382 // No need to convert as already in m/s
1385 int x = MARGIN + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
1386 int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cis]*LINES);
1387 gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
1389 g_object_unref ( G_OBJECT(gps_speed_gc) );
1393 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1395 g_object_unref ( G_OBJECT(pix) );
1399 * Draw just the distance/time image
1401 static void draw_dt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1408 // Free previous allocation
1409 if ( widgets->distances )
1410 g_free ( widgets->distances );
1412 widgets->distances = vik_track_make_distance_map ( tr, widgets->profile_width );
1413 if ( widgets->distances == NULL )
1416 // Convert into appropriate units
1417 vik_units_distance_t dist_units = a_vik_get_units_distance ();
1418 if ( dist_units == VIK_UNITS_DISTANCE_MILES ) {
1419 for ( i = 0; i < widgets->profile_width; i++ ) {
1420 widgets->distances[i] = VIK_METERS_TO_MILES(widgets->distances[i]);
1424 // Metres - but want in kms
1425 for ( i = 0; i < widgets->profile_width; i++ ) {
1426 widgets->distances[i] = widgets->distances[i]/1000.0;
1430 window = gtk_widget_get_toplevel (widgets->dist_box);
1432 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1434 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1436 // easy to work out min / max of distance!
1438 // mind = 0.0; - Thus not used
1439 if ( dist_units == VIK_UNITS_DISTANCE_MILES )
1440 maxd = VIK_METERS_TO_MILES(vik_track_get_length_including_gaps (tr));
1442 maxd = vik_track_get_length_including_gaps (tr) / 1000.0;
1444 /* Find suitable chunk index */
1445 gdouble dummy = 0.0; // expect this to remain the same! (not that it's used)
1446 get_new_min_and_chunk_index (0, maxd, chunksd, sizeof(chunksd)/sizeof(chunksd[0]), &dummy, &widgets->cid);
1448 /* clear the image */
1449 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1450 TRUE, 0, 0, MARGIN, widgets->profile_height);
1451 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1452 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1455 for (i=0; i<=LINES; i++) {
1456 PangoFontDescription *pfd;
1457 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1461 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1462 pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1463 pango_layout_set_font_description (pl, pfd);
1464 pango_font_description_free (pfd);
1465 if ( dist_units == VIK_UNITS_DISTANCE_MILES )
1466 sprintf(s, _("%.1f miles"), ((LINES-i)*chunksd[widgets->cid]));
1468 sprintf(s, _("%.1f km"), ((LINES-i)*chunksd[widgets->cid]));
1470 pango_layout_set_text(pl, s, -1);
1471 pango_layout_get_pixel_size (pl, &w, &h);
1472 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
1473 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1475 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
1476 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1477 g_object_unref ( G_OBJECT ( pl ) );
1482 for ( i = 0; i < widgets->profile_width; i++ )
1483 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
1484 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->distances[i])/(chunksd[widgets->cid]*LINES) );
1486 // Show speed indicator
1487 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_dist_speed)) ) {
1488 GdkGC *dist_speed_gc = gdk_gc_new ( window->window );
1490 gdk_color_parse ( "red", &color );
1491 gdk_gc_set_rgb_fg_color ( dist_speed_gc, &color);
1493 gdouble max_speed = 0;
1494 max_speed = widgets->max_speed * 110 / 100;
1496 // This is just an indicator - no actual values can be inferred by user
1498 for ( i = 0; i < widgets->profile_width; i++ ) {
1499 int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
1500 gdk_draw_rectangle(GDK_DRAWABLE(pix), dist_speed_gc, TRUE, i+MARGIN-2, y_speed-2, 4, 4);
1502 g_object_unref ( G_OBJECT(dist_speed_gc) );
1506 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1508 g_object_unref ( G_OBJECT(pix) );
1513 * Draw just the elevation/time image
1515 static void draw_et ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1522 // Free previous allocation
1524 g_free ( widgets->ats );
1526 widgets->ats = vik_track_make_elevation_time_map ( tr, widgets->profile_width );
1528 if ( widgets->ats == NULL )
1531 // Convert into appropriate units
1532 vik_units_height_t height_units = a_vik_get_units_height ();
1533 if ( height_units == VIK_UNITS_HEIGHT_FEET ) {
1534 // Convert altitudes into feet units
1535 for ( i = 0; i < widgets->profile_width; i++ ) {
1536 widgets->ats[i] = VIK_METERS_TO_FEET(widgets->ats[i]);
1539 // Otherwise leave in metres
1541 minmax_array(widgets->ats, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
1543 get_new_min_and_chunk_index_altitude (widgets->min_altitude, widgets->max_altitude, &widgets->draw_min_altitude_time, &widgets->ciat);
1546 mina = widgets->draw_min_altitude_time;
1548 window = gtk_widget_get_toplevel (widgets->elev_time_box);
1550 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1552 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1554 /* clear the image */
1555 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1556 TRUE, 0, 0, MARGIN, widgets->profile_height);
1557 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1558 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1561 for (i=0; i<=LINES; i++) {
1562 PangoFontDescription *pfd;
1563 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1567 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1568 pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1569 pango_layout_set_font_description (pl, pfd);
1570 pango_font_description_free (pfd);
1571 switch (height_units) {
1572 case VIK_UNITS_HEIGHT_METRES:
1573 sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
1575 case VIK_UNITS_HEIGHT_FEET:
1576 // NB values already converted into feet
1577 sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
1581 g_critical("Houston, we've had a problem. height=%d", height_units);
1583 pango_layout_set_text(pl, s, -1);
1584 pango_layout_get_pixel_size (pl, &w, &h);
1585 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
1586 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1588 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
1589 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1590 g_object_unref ( G_OBJECT ( pl ) );
1594 /* draw elevations */
1595 for ( i = 0; i < widgets->profile_width; i++ )
1596 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
1597 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->ats[i]-mina)/(chunksa[widgets->ciat]*LINES) );
1599 // Show speed indicator
1600 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_elev_speed)) ) {
1601 GdkGC *elev_speed_gc = gdk_gc_new ( window->window );
1603 gdk_color_parse ( "red", &color );
1604 gdk_gc_set_rgb_fg_color ( elev_speed_gc, &color);
1606 gdouble max_speed = 0;
1607 max_speed = widgets->max_speed * 110 / 100;
1609 // This is just an indicator - no actual values can be inferred by user
1611 for ( i = 0; i < widgets->profile_width; i++ ) {
1612 int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
1613 gdk_draw_rectangle(GDK_DRAWABLE(pix), elev_speed_gc, TRUE, i+MARGIN-2, y_speed-2, 4, 4);
1615 g_object_unref ( G_OBJECT(elev_speed_gc) );
1619 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1621 g_object_unref ( G_OBJECT(pix) );
1626 * Draw just the speed/distance image
1628 static void draw_sd ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
1635 // Free previous allocation
1636 if ( widgets->speeds_dist )
1637 g_free ( widgets->speeds_dist );
1639 widgets->speeds_dist = vik_track_make_speed_dist_map ( tr, widgets->profile_width );
1640 if ( widgets->speeds_dist == NULL )
1643 // Convert into appropriate units
1644 vik_units_speed_t speed_units = a_vik_get_units_speed ();
1645 switch (speed_units) {
1646 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1647 for ( i = 0; i < widgets->profile_width; i++ ) {
1648 widgets->speeds_dist[i] = VIK_MPS_TO_KPH(widgets->speeds_dist[i]);
1651 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1652 for ( i = 0; i < widgets->profile_width; i++ ) {
1653 widgets->speeds_dist[i] = VIK_MPS_TO_MPH(widgets->speeds_dist[i]);
1656 case VIK_UNITS_SPEED_KNOTS:
1657 for ( i = 0; i < widgets->profile_width; i++ ) {
1658 widgets->speeds_dist[i] = VIK_MPS_TO_KNOTS(widgets->speeds_dist[i]);
1662 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1663 // No need to convert as already in m/s
1667 window = gtk_widget_get_toplevel (widgets->speed_dist_box);
1669 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
1671 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1673 // OK to resuse min_speed here
1674 minmax_array(widgets->speeds_dist, &widgets->min_speed, &widgets->max_speed_dist, FALSE, widgets->profile_width);
1675 if (widgets->min_speed < 0.0)
1676 widgets->min_speed = 0; /* splines sometimes give negative speeds */
1678 /* Find suitable chunk index */
1679 get_new_min_and_chunk_index (widgets->min_speed, widgets->max_speed_dist, chunkss, sizeof(chunkss)/sizeof(chunkss[0]), &widgets->draw_min_speed, &widgets->cisd);
1682 mins = widgets->draw_min_speed;
1684 /* clear the image */
1685 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
1686 TRUE, 0, 0, MARGIN, widgets->profile_height);
1687 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
1688 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1691 for (i=0; i<=LINES; i++) {
1692 PangoFontDescription *pfd;
1693 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1697 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
1698 pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
1699 pango_layout_set_font_description (pl, pfd);
1700 pango_font_description_free (pfd);
1701 // NB: No need to convert here anymore as numbers are in the appropriate units
1702 switch (speed_units) {
1703 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1704 sprintf(s, "%8dkm/h", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1706 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1707 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1709 case VIK_UNITS_SPEED_METRES_PER_SECOND:
1710 sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1712 case VIK_UNITS_SPEED_KNOTS:
1713 sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1717 g_critical("Houston, we've had a problem. speed=%d", speed_units);
1720 pango_layout_set_text(pl, s, -1);
1721 pango_layout_get_pixel_size (pl, &w, &h);
1722 gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
1723 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1725 gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
1726 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1727 g_object_unref ( G_OBJECT ( pl ) );
1733 for ( i = 0; i < widgets->profile_width; i++ )
1734 gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
1735 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds_dist[i]-mins)/(chunkss[widgets->cisd]*LINES) );
1738 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_sd_gps_speed)) ) {
1740 GdkGC *gps_speed_gc = gdk_gc_new ( window->window );
1742 gdk_color_parse ( "red", &color );
1743 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1745 gdouble dist = vik_track_get_length_including_gaps(tr);
1746 gdouble dist_tp = 0.0;
1748 GList *iter = tr->trackpoints;
1749 for (iter = iter->next; iter; iter = iter->next) {
1750 gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
1751 if (isnan(gps_speed))
1753 switch (speed_units) {
1754 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1755 gps_speed = VIK_MPS_TO_KPH(gps_speed);
1757 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1758 gps_speed = VIK_MPS_TO_MPH(gps_speed);
1760 case VIK_UNITS_SPEED_KNOTS:
1761 gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
1764 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1765 // No need to convert as already in m/s
1768 dist_tp += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1769 int x = MARGIN + (widgets->profile_width * dist_tp / dist);
1770 int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cisd]*LINES);
1771 gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
1773 g_object_unref ( G_OBJECT(gps_speed_gc) );
1777 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
1779 g_object_unref ( G_OBJECT(pix) );
1786 static void draw_all_graphs ( GtkWidget *widget, gpointer *pass_along, gboolean resized )
1788 VikTrack *tr = pass_along[0];
1789 PropWidgets *widgets = pass_along[3];
1791 // Draw graphs even if they are not visible
1793 GList *child = NULL;
1794 GtkWidget *image = NULL;
1795 GtkWidget *window = gtk_widget_get_toplevel(widget);
1797 gdouble pc_blob = NAN;
1800 if (widgets->elev_box != NULL) {
1802 // Saved image no longer any good as we've resized, so we remove it here
1803 if (resized && widgets->elev_graph_saved_img.img) {
1804 g_object_unref(widgets->elev_graph_saved_img.img);
1805 widgets->elev_graph_saved_img.img = NULL;
1806 widgets->elev_graph_saved_img.saved = FALSE;
1809 child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box));
1810 draw_elevations (GTK_WIDGET(child->data), tr, widgets );
1812 image = GTK_WIDGET(child->data);
1815 // Ensure marker or blob are redrawn if necessary
1816 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
1818 pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
1819 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
1821 if (widgets->is_blob_drawn) {
1822 pc_blob = tp_percentage_by_distance ( tr, widgets->blob_tp, widgets->track_length_inc_gaps );
1823 if (!isnan(pc_blob)) {
1824 x_blob = (pc_blob * widgets->profile_width);
1826 y_blob = blobby_altitude (x_blob, widgets);
1829 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1831 marker_x = (pc * widgets->profile_width) + MARGIN;
1834 save_image_and_draw_graph_marks (image,
1836 window->style->black_gc,
1839 &widgets->elev_graph_saved_img,
1840 widgets->profile_width,
1841 widgets->profile_height,
1842 &widgets->is_marker_drawn,
1843 &widgets->is_blob_drawn);
1848 if (widgets->speed_box != NULL) {
1850 // Saved image no longer any good as we've resized
1851 if (resized && widgets->speed_graph_saved_img.img) {
1852 g_object_unref(widgets->speed_graph_saved_img.img);
1853 widgets->speed_graph_saved_img.img = NULL;
1854 widgets->speed_graph_saved_img.saved = FALSE;
1857 child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box));
1858 draw_vt (GTK_WIDGET(child->data), tr, widgets );
1860 image = GTK_WIDGET(child->data);
1863 // Ensure marker or blob are redrawn if necessary
1864 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
1866 pc = tp_percentage_by_time ( tr, widgets->marker_tp );
1868 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
1870 if (widgets->is_blob_drawn) {
1871 pc_blob = tp_percentage_by_time ( tr, widgets->blob_tp );
1872 if (!isnan(pc_blob)) {
1873 x_blob = (pc_blob * widgets->profile_width);
1876 y_blob = blobby_speed (x_blob, widgets);
1879 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1881 marker_x = (pc * widgets->profile_width) + MARGIN;
1884 save_image_and_draw_graph_marks (image,
1886 window->style->black_gc,
1889 &widgets->speed_graph_saved_img,
1890 widgets->profile_width,
1891 widgets->profile_height,
1892 &widgets->is_marker_drawn,
1893 &widgets->is_blob_drawn);
1898 if (widgets->dist_box != NULL) {
1900 // Saved image no longer any good as we've resized
1901 if (resized && widgets->dist_graph_saved_img.img) {
1902 g_object_unref(widgets->dist_graph_saved_img.img);
1903 widgets->dist_graph_saved_img.img = NULL;
1904 widgets->dist_graph_saved_img.saved = FALSE;
1907 child = gtk_container_get_children(GTK_CONTAINER(widgets->dist_box));
1908 draw_dt (GTK_WIDGET(child->data), tr, widgets );
1910 image = GTK_WIDGET(child->data);
1913 // Ensure marker or blob are redrawn if necessary
1914 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
1916 pc = tp_percentage_by_time ( tr, widgets->marker_tp );
1918 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
1920 if (widgets->is_blob_drawn) {
1921 pc_blob = tp_percentage_by_time ( tr, widgets->blob_tp );
1922 if (!isnan(pc_blob)) {
1923 x_blob = (pc_blob * widgets->profile_width);
1926 y_blob = blobby_distance (x_blob, widgets);
1929 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1931 marker_x = (pc * widgets->profile_width) + MARGIN;
1934 save_image_and_draw_graph_marks (image,
1936 window->style->black_gc,
1939 &widgets->dist_graph_saved_img,
1940 widgets->profile_width,
1941 widgets->profile_height,
1942 &widgets->is_marker_drawn,
1943 &widgets->is_blob_drawn);
1947 // Draw Elevations in timely manner
1948 if (widgets->elev_time_box != NULL) {
1950 // Saved image no longer any good as we've resized
1951 if (resized && widgets->elev_time_graph_saved_img.img) {
1952 g_object_unref(widgets->elev_time_graph_saved_img.img);
1953 widgets->elev_time_graph_saved_img.img = NULL;
1954 widgets->elev_time_graph_saved_img.saved = FALSE;
1957 child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_time_box));
1958 draw_et (GTK_WIDGET(child->data), tr, widgets );
1960 image = GTK_WIDGET(child->data);
1963 // Ensure marker or blob are redrawn if necessary
1964 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
1966 pc = tp_percentage_by_time ( tr, widgets->marker_tp );
1968 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
1970 if (widgets->is_blob_drawn) {
1971 pc_blob = tp_percentage_by_time ( tr, widgets->blob_tp );
1972 if (!isnan(pc_blob)) {
1973 x_blob = (pc_blob * widgets->profile_width);
1975 y_blob = blobby_altitude_time (x_blob, widgets);
1978 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1980 marker_x = (pc * widgets->profile_width) + MARGIN;
1983 save_image_and_draw_graph_marks (image,
1985 window->style->black_gc,
1988 &widgets->elev_time_graph_saved_img,
1989 widgets->profile_width,
1990 widgets->profile_height,
1991 &widgets->is_marker_drawn,
1992 &widgets->is_blob_drawn);
1996 // Draw speed distances
1997 if (widgets->speed_dist_box != NULL) {
1999 // Saved image no longer any good as we've resized, so we remove it here
2000 if (resized && widgets->speed_dist_graph_saved_img.img) {
2001 g_object_unref(widgets->speed_dist_graph_saved_img.img);
2002 widgets->speed_dist_graph_saved_img.img = NULL;
2003 widgets->speed_dist_graph_saved_img.saved = FALSE;
2006 child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_dist_box));
2007 draw_sd (GTK_WIDGET(child->data), tr, widgets );
2009 image = GTK_WIDGET(child->data);
2012 // Ensure marker or blob are redrawn if necessary
2013 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2015 pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length_inc_gaps );
2016 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2018 if (widgets->is_blob_drawn) {
2019 pc_blob = tp_percentage_by_distance ( tr, widgets->blob_tp, widgets->track_length_inc_gaps );
2020 if (!isnan(pc_blob)) {
2021 x_blob = (pc_blob * widgets->profile_width);
2023 y_blob = blobby_speed_dist (x_blob, widgets);
2026 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2028 marker_x = (pc * widgets->profile_width) + MARGIN;
2031 save_image_and_draw_graph_marks (image,
2033 window->style->black_gc,
2036 &widgets->speed_dist_graph_saved_img,
2037 widgets->profile_width,
2038 widgets->profile_height,
2039 &widgets->is_marker_drawn,
2040 &widgets->is_blob_drawn);
2047 * Configure/Resize the profile & speed/time images
2049 static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, gpointer *pass_along )
2051 PropWidgets *widgets = pass_along[3];
2053 if (widgets->configure_dialog) {
2054 // Determine size offsets between dialog size and size for images
2055 // Only on the initialisation of the dialog
2056 widgets->profile_width_offset = event->width - widgets->profile_width;
2057 widgets->profile_height_offset = event->height - widgets->profile_height;
2058 widgets->configure_dialog = FALSE;
2060 // Without this the settting, the dialog will only grow in vertical size - one can not then make it smaller!
2061 gtk_widget_set_size_request ( widget, widgets->profile_width+widgets->profile_width_offset, widgets->profile_height );
2062 // In fact this allows one to compress it a bit more vertically as I don't add on the height offset
2065 widgets->profile_width_old = widgets->profile_width;
2066 widgets->profile_height_old = widgets->profile_height;
2069 // Now adjust From Dialog size to get image size
2070 widgets->profile_width = event->width - widgets->profile_width_offset;
2071 widgets->profile_height = event->height - widgets->profile_height_offset;
2073 // ATM we receive configure_events when the dialog is moved and so no further action is necessary
2074 if ( !widgets->configure_dialog &&
2075 (widgets->profile_width_old == widgets->profile_width) && (widgets->profile_height_old == widgets->profile_height) )
2079 draw_all_graphs ( widget, pass_along, TRUE );
2085 * Create height profile widgets including the image and callbacks
2087 GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
2091 GtkWidget *eventbox;
2092 gpointer *pass_along;
2095 widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
2097 if ( widgets->altitudes == NULL ) {
2098 *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
2102 minmax_array(widgets->altitudes, min_alt, max_alt, TRUE, widgets->profile_width);
2104 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2105 image = gtk_image_new_from_pixmap ( pix, NULL );
2107 g_object_unref ( G_OBJECT(pix) );
2109 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2111 pass_along[1] = vlp;
2112 pass_along[2] = vvp;
2113 pass_along[3] = widgets;
2115 eventbox = gtk_event_box_new ();
2116 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
2117 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
2118 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2119 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2120 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
2126 * Create speed/time widgets including the image and callbacks
2128 GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2132 GtkWidget *eventbox;
2133 gpointer *pass_along;
2136 widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
2137 if ( widgets->speeds == NULL )
2140 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2142 pass_along[1] = vlp;
2143 pass_along[2] = vvp;
2144 pass_along[3] = widgets;
2146 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2147 image = gtk_image_new_from_pixmap ( pix, NULL );
2150 /* XXX this can go out, it's just a helpful dev tool */
2153 GdkGC **colors[8] = { window->style->bg_gc,
2154 window->style->fg_gc,
2155 window->style->light_gc,
2156 window->style->dark_gc,
2157 window->style->mid_gc,
2158 window->style->text_gc,
2159 window->style->base_gc,
2160 window->style->text_aa_gc };
2161 for (i=0; i<5; i++) {
2162 for (j=0; j<8; j++) {
2163 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
2164 TRUE, i*20, j*20, 20, 20);
2165 gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
2166 FALSE, i*20, j*20, 20, 20);
2172 g_object_unref ( G_OBJECT(pix) );
2174 eventbox = gtk_event_box_new ();
2175 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along );
2176 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along );
2177 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2178 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2179 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2185 * Create distance / time widgets including the image and callbacks
2187 GtkWidget *vik_trw_layer_create_dtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2191 GtkWidget *eventbox;
2192 gpointer *pass_along;
2195 widgets->distances = vik_track_make_distance_map ( tr, widgets->profile_width );
2196 if ( widgets->distances == NULL )
2199 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2201 pass_along[1] = vlp;
2202 pass_along[2] = vvp;
2203 pass_along[3] = widgets;
2205 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2206 image = gtk_image_new_from_pixmap ( pix, NULL );
2208 g_object_unref ( G_OBJECT(pix) );
2210 eventbox = gtk_event_box_new ();
2211 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_dt_click), pass_along );
2212 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_dt_move), pass_along );
2213 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2214 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2215 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2221 * Create elevation / time widgets including the image and callbacks
2223 GtkWidget *vik_trw_layer_create_etdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2227 GtkWidget *eventbox;
2228 gpointer *pass_along;
2231 widgets->ats = vik_track_make_elevation_time_map ( tr, widgets->profile_width );
2232 if ( widgets->ats == NULL )
2235 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2237 pass_along[1] = vlp;
2238 pass_along[2] = vvp;
2239 pass_along[3] = widgets;
2241 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2242 image = gtk_image_new_from_pixmap ( pix, NULL );
2244 g_object_unref ( G_OBJECT(pix) );
2246 eventbox = gtk_event_box_new ();
2247 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_et_click), pass_along );
2248 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_et_move), pass_along );
2249 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2250 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2251 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2257 * Create speed/distance widgets including the image and callbacks
2259 GtkWidget *vik_trw_layer_create_sddiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
2263 GtkWidget *eventbox;
2264 gpointer *pass_along;
2267 widgets->speeds_dist = vik_track_make_speed_dist_map ( tr, widgets->profile_width );
2268 if ( widgets->speeds_dist == NULL )
2271 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2273 pass_along[1] = vlp;
2274 pass_along[2] = vvp;
2275 pass_along[3] = widgets;
2277 pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
2278 image = gtk_image_new_from_pixmap ( pix, NULL );
2280 g_object_unref ( G_OBJECT(pix) );
2282 eventbox = gtk_event_box_new ();
2283 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_sd_click), pass_along );
2284 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_sd_move), pass_along );
2285 g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
2286 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2287 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2293 static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets)
2295 VikTrack *tr = widgets->tr;
2296 VikTrwLayer *vtl = widgets->vtl;
2297 gboolean keep_dialog = FALSE;
2299 /* FIXME: check and make sure the track still exists before doing anything to it */
2300 /* Note: destroying diaglog (eg, parent window exit) won't give "response" */
2302 case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
2303 case GTK_RESPONSE_REJECT:
2305 case GTK_RESPONSE_ACCEPT:
2306 vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
2308 case VIK_TRW_LAYER_PROPWIN_REVERSE:
2309 vik_track_reverse(tr);
2310 vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
2312 case VIK_TRW_LAYER_PROPWIN_DEL_DUP:
2313 vik_track_remove_dup_points(tr);
2314 /* above operation could have deleted current_tp or last_tp */
2315 trw_layer_cancel_tps_of_track ( vtl, widgets->track_name );
2316 vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
2318 case VIK_TRW_LAYER_PROPWIN_SPLIT:
2320 /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */
2322 VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
2325 for ( i = 0; i < ntracks; i++ )
2327 g_assert ( tracks[i] );
2328 new_tr_name = g_strdup_printf("%s #%d", widgets->track_name, i+1);
2329 /* if ( (wp_exists) && (! overwrite) ) */
2330 /* don't need to upper case new_tr_name because old tr name was uppercase */
2331 if ( vik_trw_layer_get_track(vtl, new_tr_name ) &&
2332 ( ! a_dialog_yes_or_no ( VIK_GTK_WINDOW_FROM_LAYER(vtl), "The track \"%s\" exists, do you wish to overwrite it?", new_tr_name ) ) )
2334 gchar *new_new_tr_name = a_dialog_new_track ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
2335 g_free ( new_tr_name );
2336 if (new_new_tr_name)
2337 new_tr_name = new_new_tr_name;
2341 vik_track_free ( tracks[i] );
2345 vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
2350 /* Don't let track destroy this dialog */
2351 vik_track_clear_property_dialog(tr);
2352 vik_trw_layer_delete_track ( vtl, widgets->track_name );
2353 vik_layer_emit_update ( VIK_LAYER(vtl), FALSE ); /* chase thru the hoops */
2357 case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER:
2359 GList *iter = tr->trackpoints;
2360 while ((iter = iter->next)) {
2361 if (widgets->marker_tp == VIK_TRACKPOINT(iter->data))
2365 a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR,
2366 _("Failed spliting track. Track unchanged"), NULL);
2371 gchar *r_name = g_strdup_printf("%s #2", widgets->track_name);
2372 if (vik_trw_layer_get_track(vtl, r_name ) &&
2373 ( ! a_dialog_yes_or_no( VIK_GTK_WINDOW_FROM_LAYER(vtl),
2374 "The track \"%s\" exists, do you wish to overwrite it?", r_name)))
2376 gchar *new_r_name = a_dialog_new_track( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
2379 r_name = new_r_name;
2382 a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_WARNING,
2383 _("Operation Aborted. Track unchanged"), NULL);
2388 iter->prev->next = NULL;
2390 VikTrack *tr_right = vik_track_new();
2392 vik_track_set_comment ( tr_right, tr->comment );
2393 tr_right->visible = tr->visible;
2394 tr_right->trackpoints = iter;
2396 vik_trw_layer_add_track(vtl, r_name, tr_right);
2397 vik_layer_emit_update ( VIK_LAYER(vtl), FALSE );
2401 fprintf(stderr, "DEBUG: unknown response\n");
2405 /* Keep same behaviour for now: destroy dialog if click on any button */
2407 prop_widgets_free(widgets);
2408 vik_track_clear_property_dialog(tr);
2409 gtk_widget_destroy ( GTK_WIDGET(dialog) );
2414 * Force a redraw when checkbutton has been toggled to show/hide that information
2416 static void checkbutton_toggle_cb ( GtkToggleButton *togglebutton, gpointer *pass_along, gpointer dummy)
2418 PropWidgets *widgets = pass_along[3];
2419 // Even though not resized, we'll pretend it is -
2420 // as this invalidates the saved images (since the image may have changed)
2421 draw_all_graphs ( widgets->dialog, pass_along, TRUE);
2425 * Create the widgets for the given graph tab
2427 static GtkWidget *create_graph_page ( GtkWidget *graph,
2428 const gchar *markup,
2430 const gchar *markup2,
2432 GtkWidget *checkbutton1,
2433 gboolean checkbutton1_default,
2434 GtkWidget *checkbutton2,
2435 gboolean checkbutton2_default )
2437 GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 );
2438 GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 );
2439 GtkWidget *label = gtk_label_new (NULL);
2440 GtkWidget *label2 = gtk_label_new (NULL);
2441 gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0);
2442 gtk_label_set_markup ( GTK_LABEL(label), markup );
2443 gtk_label_set_markup ( GTK_LABEL(label2), markup2 );
2444 gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
2445 gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0);
2446 gtk_box_pack_start (GTK_BOX(hbox), label2, FALSE, FALSE, 0);
2447 gtk_box_pack_start (GTK_BOX(hbox), value2, FALSE, FALSE, 0);
2449 gtk_box_pack_end (GTK_BOX(hbox), checkbutton2, FALSE, FALSE, 0);
2450 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton2), checkbutton2_default);
2453 gtk_box_pack_end (GTK_BOX(hbox), checkbutton1, FALSE, FALSE, 0);
2454 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton1), checkbutton1_default);
2456 gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
2461 void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name, VikViewport *vvp )
2463 /* FIXME: free widgets when destroy signal received */
2464 PropWidgets *widgets = prop_widgets_new();
2467 widgets->profile_width = PROPWIN_PROFILE_WIDTH;
2468 widgets->profile_height = PROPWIN_PROFILE_HEIGHT;
2469 widgets->track_name = track_name;
2470 gchar *title = g_strdup_printf(_("%s - Track Properties"), track_name);
2471 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
2473 GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
2474 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
2475 _("Split at _Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER,
2476 _("Split _Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT,
2477 _("_Reverse"), VIK_TRW_LAYER_PROPWIN_REVERSE,
2478 _("_Delete Dupl."), VIK_TRW_LAYER_PROPWIN_DEL_DUP,
2479 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2481 widgets->dialog = dialog;
2483 g_signal_connect(dialog, "response", G_CALLBACK(propwin_response_cb), widgets);
2486 guint32 tp_count, seg_count;
2488 gdouble min_alt, max_alt;
2489 widgets->elev_box = vik_trw_layer_create_profile(GTK_WIDGET(parent), tr, vlp, vvp, widgets, &min_alt, &max_alt);
2490 widgets->speed_box = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2491 widgets->dist_box = vik_trw_layer_create_dtdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2492 widgets->elev_time_box = vik_trw_layer_create_etdiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2493 widgets->speed_dist_box = vik_trw_layer_create_sddiag(GTK_WIDGET(parent), tr, vlp, vvp, widgets);
2494 GtkWidget *graphs = gtk_notebook_new();
2496 GtkWidget *content[20];
2500 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>Moving 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>") };
2501 static gchar tmp_buf[50];
2505 widgets->w_comment = gtk_entry_new ();
2507 gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment );
2508 g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
2509 content[cnt++] = widgets->w_comment;
2511 vik_units_distance_t dist_units = a_vik_get_units_distance ();
2513 // NB This value not shown yet - but is used by internal calculations
2514 widgets->track_length_inc_gaps = vik_track_get_length_including_gaps(tr);
2516 tr_len = widgets->track_length = vik_track_get_length(tr);
2517 switch (dist_units) {
2518 case VIK_UNITS_DISTANCE_KILOMETRES:
2519 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", tr_len/1000.0 );
2521 case VIK_UNITS_DISTANCE_MILES:
2522 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(tr_len) );
2525 g_critical("Houston, we've had a problem. distance=%d", dist_units);
2527 widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf );
2529 tp_count = vik_track_get_tp_count(tr);
2530 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", tp_count );
2531 widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf );
2533 seg_count = vik_track_get_segment_count(tr) ;
2534 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
2535 widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf );
2537 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
2538 widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf );
2540 vik_units_speed_t speed_units = a_vik_get_units_speed ();
2541 tmp_speed = vik_track_get_max_speed(tr);
2542 if ( tmp_speed == 0 )
2543 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2545 switch (speed_units) {
2546 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2547 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
2549 case VIK_UNITS_SPEED_MILES_PER_HOUR:
2550 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
2552 case VIK_UNITS_SPEED_METRES_PER_SECOND:
2553 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2555 case VIK_UNITS_SPEED_KNOTS:
2556 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
2559 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2560 g_critical("Houston, we've had a problem. speed=%d", speed_units);
2563 widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf );
2565 tmp_speed = vik_track_get_average_speed(tr);
2566 if ( tmp_speed == 0 )
2567 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2569 switch (speed_units) {
2570 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2571 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
2573 case VIK_UNITS_SPEED_MILES_PER_HOUR:
2574 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
2576 case VIK_UNITS_SPEED_METRES_PER_SECOND:
2577 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2579 case VIK_UNITS_SPEED_KNOTS:
2580 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
2583 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2584 g_critical("Houston, we've had a problem. speed=%d", speed_units);
2587 widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
2589 // Use 60sec as the default period to be considered stopped
2590 // this is the TrackWaypoint draw stops default value 'vtl->stop_length'
2591 // however this variable is not directly accessible - and I don't expect it's often changed from the default
2592 // so ATM just put in the number
2593 tmp_speed = vik_track_get_average_speed_moving(tr, 60);
2594 if ( tmp_speed == 0 )
2595 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2597 switch (speed_units) {
2598 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2599 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
2601 case VIK_UNITS_SPEED_MILES_PER_HOUR:
2602 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
2604 case VIK_UNITS_SPEED_METRES_PER_SECOND:
2605 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2607 case VIK_UNITS_SPEED_KNOTS:
2608 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
2611 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2612 g_critical("Houston, we've had a problem. speed=%d", speed_units);
2615 widgets->w_mvg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
2617 switch (dist_units) {
2618 case VIK_UNITS_DISTANCE_KILOMETRES:
2619 // Even though kilometres, the average distance between points is going to be quite small so keep in metres
2620 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
2622 case VIK_UNITS_DISTANCE_MILES:
2623 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.3f miles", (tp_count - seg_count) == 0 ? 0 : VIK_METERS_TO_MILES(tr_len / ( tp_count - seg_count )) );
2626 g_critical("Houston, we've had a problem. distance=%d", dist_units);
2628 widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf );
2630 vik_units_height_t height_units = a_vik_get_units_height ();
2631 if ( min_alt == VIK_DEFAULT_ALTITUDE )
2632 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2634 switch (height_units) {
2635 case VIK_UNITS_HEIGHT_METRES:
2636 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
2638 case VIK_UNITS_HEIGHT_FEET:
2639 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet - %.0f feet", VIK_METERS_TO_FEET(min_alt), VIK_METERS_TO_FEET(max_alt) );
2642 g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
2643 g_critical("Houston, we've had a problem. height=%d", height_units);
2646 widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf );
2648 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
2649 if ( min_alt == VIK_DEFAULT_ALTITUDE )
2650 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2652 switch (height_units) {
2653 case VIK_UNITS_HEIGHT_METRES:
2654 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
2656 case VIK_UNITS_HEIGHT_FEET:
2657 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet / %.0f feet", VIK_METERS_TO_FEET(max_alt), VIK_METERS_TO_FEET(min_alt) );
2660 g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
2661 g_critical("Houston, we've had a problem. height=%d", height_units);
2664 widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf );
2667 #define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
2668 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
2681 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
2684 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
2685 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
2687 strncpy(tmp_buf, ctime(&t1), sizeof(tmp_buf));
2688 tmp_buf[sizeof(tmp_buf)-1] = 0;
2689 g_strchomp(tmp_buf);
2690 widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf);
2692 strncpy(tmp_buf, ctime(&t2), sizeof(tmp_buf));
2693 tmp_buf[sizeof(tmp_buf)-1] = 0;
2694 g_strchomp(tmp_buf);
2695 widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf);
2697 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%d minutes"), (int)(t2-t1)/60);
2698 widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf);
2700 widgets->w_time_start = content[cnt++] = gtk_label_new(_("No Data"));
2701 widgets->w_time_end = content[cnt++] = gtk_label_new(_("No Data"));
2702 widgets->w_time_dur = content[cnt++] = gtk_label_new(_("No Data"));
2705 table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
2706 gtk_table_set_col_spacing (table, 0, 10);
2707 for (i=0; i<cnt; i++) {
2710 // Settings so the text positioning only moves around vertically when the dialog is resized
2711 // This also gives more room to see the track comment
2712 label = gtk_label_new(NULL);
2713 gtk_misc_set_alignment ( GTK_MISC(label), 1, 0.5 ); // Position text centrally in vertical plane
2714 gtk_label_set_markup ( GTK_LABEL(label), _(label_texts[i]) );
2715 gtk_table_attach ( table, label, 0, 1, i, i+1, GTK_FILL, GTK_SHRINK, 0, 0 );
2716 if (GTK_IS_MISC(content[i])) {
2717 gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0.5 );
2719 gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
2722 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), GTK_WIDGET(table), gtk_label_new(_("Statistics")));
2724 gpointer *pass_along;
2725 pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
2727 pass_along[1] = vlp;
2728 pass_along[2] = vvp;
2729 pass_along[3] = widgets;
2731 if ( widgets->elev_box ) {
2732 GtkWidget *page = NULL;
2733 widgets->w_cur_dist = gtk_label_new(_("No Data"));
2734 widgets->w_cur_elevation = gtk_label_new(_("No Data"));
2735 widgets->w_show_dem = gtk_check_button_new_with_mnemonic(_("Show D_EM"));
2736 widgets->w_show_alt_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
2737 page = create_graph_page (widgets->elev_box,
2738 _("<b>Track Distance:</b>"), widgets->w_cur_dist,
2739 _("<b>Track Height:</b>"), widgets->w_cur_elevation,
2740 widgets->w_show_dem, TRUE,
2741 widgets->w_show_alt_gps_speed, TRUE);
2742 g_signal_connect (widgets->w_show_dem, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
2743 g_signal_connect (widgets->w_show_alt_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
2744 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-distance")));
2747 if ( widgets->speed_box ) {
2748 GtkWidget *page = NULL;
2749 widgets->w_cur_time = gtk_label_new(_("No Data"));
2750 widgets->w_cur_speed = gtk_label_new(_("No Data"));
2751 widgets->w_show_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
2752 page = create_graph_page (widgets->speed_box,
2753 _("<b>Track Time:</b>"), widgets->w_cur_time,
2754 _("<b>Track Speed:</b>"), widgets->w_cur_speed,
2755 widgets->w_show_gps_speed, TRUE,
2757 g_signal_connect (widgets->w_show_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
2758 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-time")));
2761 if ( widgets->dist_box ) {
2762 GtkWidget *page = NULL;
2763 widgets->w_cur_dist_time = gtk_label_new(_("No Data"));
2764 widgets->w_cur_dist_dist = gtk_label_new(_("No Data"));
2765 widgets->w_show_dist_speed = gtk_check_button_new_with_mnemonic(_("Show S_peed"));
2766 page = create_graph_page (widgets->dist_box,
2767 _("<b>Track Distance:</b>"), widgets->w_cur_dist_dist,
2768 _("<b>Track Time:</b>"), widgets->w_cur_dist_time,
2769 widgets->w_show_dist_speed, FALSE,
2771 g_signal_connect (widgets->w_show_dist_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
2772 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Distance-time")));
2775 if ( widgets->elev_time_box ) {
2776 GtkWidget *page = NULL;
2777 widgets->w_cur_elev_time = gtk_label_new(_("No Data"));
2778 widgets->w_cur_elev_elev = gtk_label_new(_("No Data"));
2779 widgets->w_show_elev_speed = gtk_check_button_new_with_mnemonic(_("Show S_peed"));
2780 page = create_graph_page (widgets->elev_time_box,
2781 _("<b>Track Time:</b>"), widgets->w_cur_elev_time,
2782 _("<b>Track Height:</b>"), widgets->w_cur_elev_elev,
2783 widgets->w_show_elev_speed, FALSE,
2785 g_signal_connect (widgets->w_show_elev_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
2786 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-time")));
2789 if ( widgets->speed_dist_box ) {
2790 GtkWidget *page = NULL;
2791 widgets->w_cur_speed_dist = gtk_label_new(_("No Data"));
2792 widgets->w_cur_speed_speed = gtk_label_new(_("No Data"));
2793 widgets->w_show_sd_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
2794 page = create_graph_page (widgets->speed_dist_box,
2795 _("<b>Track Distance:</b>"), widgets->w_cur_speed_dist,
2796 _("<b>Track Speed:</b>"), widgets->w_cur_speed_speed,
2797 widgets->w_show_sd_gps_speed, TRUE,
2799 g_signal_connect (widgets->w_show_sd_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), pass_along);
2800 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-distance")));
2803 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), graphs, FALSE, FALSE, 0);
2805 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
2807 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE);
2808 if (vik_track_get_dup_point_count(tr) <= 0)
2809 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE);
2811 // On dialog realization configure_event casues the graphs to be initially drawn
2812 widgets->configure_dialog = TRUE;
2813 g_signal_connect ( G_OBJECT(dialog), "configure-event", G_CALLBACK (configure_event), pass_along);
2815 vik_track_set_property_dialog(tr, dialog);
2816 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
2817 gtk_widget_show_all ( dialog );