]> git.street.me.uk Git - andy/viking.git/blame - src/viktrwlayer_propwin.c
Add a startup option to load a default map (as defined by layer default for the map...
[andy/viking.git] / src / viktrwlayer_propwin.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
a482007a
GB
5 * Copyright (C) 2005-2007, Alex Foobarian <foobarian@gmail.com>
6 * Copyright (C) 2007-2008, Quy Tonthat <qtonthat@gmail.com>
ce4bd1cf 7 * Copyright (C) 2012, Rob Norris <rw_norris@hotmail.com>
50a14534
EB
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
4c77d5e0
GB
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
8c00358d 29#ifdef HAVE_MATH_H
07596bf4 30#include <math.h>
8c00358d 31#endif
4c77d5e0 32
50a14534 33#include <gtk/gtk.h>
4c77d5e0 34#include <glib/gi18n.h>
50a14534 35#include <time.h>
8c00358d 36#ifdef HAVE_STRING_H
f583082b 37#include <string.h>
8c00358d 38#endif
21700912 39#include "viktrwlayer.h"
50a14534 40#include "viktrwlayer_propwin.h"
07596bf4 41#include "dems.h"
972fc895 42#include "viking.h"
24d5c7e2 43#include "vikviewport.h" /* ugh */
24d5c7e2 44#include <gdk-pixbuf/gdk-pixdata.h>
950d8a07 45
926c8140
RN
46typedef enum {
47 PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE,
0ba33e1d 48 PROPWIN_GRAPH_TYPE_GRADIENT_DISTANCE,
926c8140
RN
49 PROPWIN_GRAPH_TYPE_SPEED_TIME,
50 PROPWIN_GRAPH_TYPE_DISTANCE_TIME,
8de26632 51 PROPWIN_GRAPH_TYPE_ELEVATION_TIME,
7b624086 52 PROPWIN_GRAPH_TYPE_SPEED_DISTANCE,
926c8140
RN
53 PROPWIN_GRAPH_TYPE_END,
54} VikPropWinGraphType_t;
55
c6bc2190
RN
56/* (Hopefully!) Human friendly altitude grid sizes - note no fixed 'ratio' just numbers that look nice...*/
57static const gdouble chunksa[] = {2.0, 5.0, 10.0, 15.0, 20.0,
2501f239 58 25.0, 40.0, 50.0, 75.0, 100.0,
c6bc2190
RN
59 150.0, 200.0, 250.0, 375.0, 500.0,
60 750.0, 1000.0, 2000.0, 5000.0, 10000.0, 100000.0};
61
0ba33e1d
FA
62/* (Hopefully!) Human friendly gradient grid sizes - note no fixed 'ratio' just numbers that look nice...*/
63static const gdouble chunksg[] = {1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
64 12.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 75.0,
65 100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
66 750.0, 1000.0, 10000.0};
67
976c7df6
RN
68/* (Hopefully!) Human friendly grid sizes - note no fixed 'ratio' just numbers that look nice...*/
69/* As need to cover walking speeds - have many low numbers (but also may go up to airplane speeds!) */
70static const gdouble chunkss[] = {1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
71 15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
72 100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
73 750.0, 1000.0, 10000.0};
74
926c8140
RN
75/* (Hopefully!) Human friendly distance grid sizes - note no fixed 'ratio' just numbers that look nice...*/
76static const gdouble chunksd[] = {0.1, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 8.0, 10.0,
77 15.0, 20.0, 25.0, 40.0, 50.0, 75.0,
78 100.0, 150.0, 200.0, 250.0, 375.0, 500.0,
79 750.0, 1000.0, 10000.0};
80
ca7e67ef
QT
81typedef struct _propsaved {
82 gboolean saved;
ca7e67ef
QT
83 GdkImage *img;
84} PropSaved;
85
21700912 86typedef struct _propwidgets {
950d8a07 87 gboolean configure_dialog;
21700912
QT
88 VikTrwLayer *vtl;
89 VikTrack *tr;
b1453c16 90 gpointer trk_id;
2cf168ac
RN
91 VikViewport *vvp;
92 VikLayersPanel *vlp;
b66b0b38
RN
93 gint profile_width;
94 gint profile_height;
2f078f56
RN
95 gint profile_width_old;
96 gint profile_height_old;
97 gint profile_width_offset;
98 gint profile_height_offset;
8dcb3ba4 99 GtkWidget *dialog;
21700912 100 GtkWidget *w_comment;
6b2f262e 101 GtkWidget *w_description;
21700912
QT
102 GtkWidget *w_track_length;
103 GtkWidget *w_tp_count;
104 GtkWidget *w_segment_count;
105 GtkWidget *w_duptp_count;
106 GtkWidget *w_max_speed;
107 GtkWidget *w_avg_speed;
a68ddd9c 108 GtkWidget *w_mvg_speed;
21700912
QT
109 GtkWidget *w_avg_dist;
110 GtkWidget *w_elev_range;
111 GtkWidget *w_elev_gain;
112 GtkWidget *w_time_start;
113 GtkWidget *w_time_end;
114 GtkWidget *w_time_dur;
b1453c16 115 GtkWidget *w_color;
065f60f1 116 GtkWidget *w_cur_dist; /*< Current distance */
780d3771 117 GtkWidget *w_cur_elevation;
0ba33e1d
FA
118 GtkWidget *w_cur_gradient_dist; /*< Current distance on gradient graph */
119 GtkWidget *w_cur_gradient_gradient; /*< Current gradient on gradient graph */
065f60f1 120 GtkWidget *w_cur_time; /*< Current time */
780d3771 121 GtkWidget *w_cur_speed;
926c8140
RN
122 GtkWidget *w_cur_dist_dist; /*< Current distance on distance graph */
123 GtkWidget *w_cur_dist_time; /*< Current time on distance graph */
8de26632
RN
124 GtkWidget *w_cur_elev_elev;
125 GtkWidget *w_cur_elev_time;
7b624086
RN
126 GtkWidget *w_cur_speed_dist;
127 GtkWidget *w_cur_speed_speed;
ec32b567
RN
128 GtkWidget *w_show_dem;
129 GtkWidget *w_show_alt_gps_speed;
130 GtkWidget *w_show_gps_speed;
0ba33e1d 131 GtkWidget *w_show_gradient_gps_speed;
926c8140 132 GtkWidget *w_show_dist_speed;
8de26632 133 GtkWidget *w_show_elev_speed;
7b624086 134 GtkWidget *w_show_sd_gps_speed;
e60fc2c9 135 gdouble track_length;
f9744e8f 136 gdouble track_length_inc_gaps;
ca7e67ef 137 PropSaved elev_graph_saved_img;
0ba33e1d 138 PropSaved gradient_graph_saved_img;
ca7e67ef 139 PropSaved speed_graph_saved_img;
926c8140 140 PropSaved dist_graph_saved_img;
8de26632 141 PropSaved elev_time_graph_saved_img;
7b624086 142 PropSaved speed_dist_graph_saved_img;
e60fc2c9 143 GtkWidget *elev_box;
0ba33e1d 144 GtkWidget *gradient_box;
e60fc2c9 145 GtkWidget *speed_box;
926c8140 146 GtkWidget *dist_box;
8de26632 147 GtkWidget *elev_time_box;
7b624086 148 GtkWidget *speed_dist_box;
0b2bfa08 149 gdouble *altitudes;
8de26632 150 gdouble *ats; // altitudes in time
83cd69cb
RN
151 gdouble min_altitude;
152 gdouble max_altitude;
c6bc2190 153 gdouble draw_min_altitude;
d2bae53c 154 gdouble draw_min_altitude_time;
c6bc2190 155 gint cia; // Chunk size Index into Altitudes
d2bae53c
RN
156 gint ciat; // Chunk size Index into Altitudes / Time
157 // NB cia & ciat are normally same value but sometimes not due to differing methods of altitude array creation
158 // thus also have draw_min_altitude for each altitude graph type
0ba33e1d
FA
159 gdouble *gradients;
160 gdouble min_gradient;
161 gdouble max_gradient;
162 gdouble draw_min_gradient;
163 gint cig; // Chunk size Index into Gradients
0b2bfa08 164 gdouble *speeds;
7b624086 165 gdouble *speeds_dist;
83cd69cb
RN
166 gdouble min_speed;
167 gdouble max_speed;
976c7df6 168 gdouble draw_min_speed;
7b624086 169 gdouble max_speed_dist;
976c7df6 170 gint cis; // Chunk size Index into Speeds
7b624086 171 gint cisd; // Chunk size Index into Speed/Distance
926c8140
RN
172 gdouble *distances;
173 gint cid; // Chunk size Index into Distance
8dcb3ba4 174 VikTrackpoint *marker_tp;
7ee55449 175 gboolean is_marker_drawn;
50391e88
RN
176 VikTrackpoint *blob_tp;
177 gboolean is_blob_drawn;
21700912
QT
178} PropWidgets;
179
ca7e67ef
QT
180static PropWidgets *prop_widgets_new()
181{
182 PropWidgets *widgets = g_malloc0(sizeof(PropWidgets));
183
184 return widgets;
185}
186
187static void prop_widgets_free(PropWidgets *widgets)
188{
ca7e67ef
QT
189 if (widgets->elev_graph_saved_img.img)
190 g_object_unref(widgets->elev_graph_saved_img.img);
0ba33e1d
FA
191 if (widgets->gradient_graph_saved_img.img)
192 g_object_unref(widgets->gradient_graph_saved_img.img);
ca7e67ef
QT
193 if (widgets->speed_graph_saved_img.img)
194 g_object_unref(widgets->speed_graph_saved_img.img);
926c8140
RN
195 if (widgets->dist_graph_saved_img.img)
196 g_object_unref(widgets->dist_graph_saved_img.img);
8de26632
RN
197 if (widgets->elev_time_graph_saved_img.img)
198 g_object_unref(widgets->elev_time_graph_saved_img.img);
7b624086
RN
199 if (widgets->speed_dist_graph_saved_img.img)
200 g_object_unref(widgets->speed_dist_graph_saved_img.img);
0b2bfa08
RN
201 if (widgets->altitudes)
202 g_free(widgets->altitudes);
0ba33e1d
FA
203 if (widgets->gradients)
204 g_free(widgets->gradients);
0b2bfa08
RN
205 if (widgets->speeds)
206 g_free(widgets->speeds);
926c8140
RN
207 if (widgets->distances)
208 g_free(widgets->distances);
8de26632
RN
209 if (widgets->ats)
210 g_free(widgets->ats);
7b624086
RN
211 if (widgets->speeds_dist)
212 g_free(widgets->speeds_dist);
ca7e67ef
QT
213 g_free(widgets);
214}
215
b66b0b38 216static void minmax_array(const gdouble *array, gdouble *min, gdouble *max, gboolean NO_ALT_TEST, gint PROFILE_WIDTH)
50a14534
EB
217{
218 *max = -1000;
219 *min = 20000;
220 guint i;
221 for ( i=0; i < PROFILE_WIDTH; i++ ) {
93a85f58
RN
222 if ( NO_ALT_TEST || (array[i] != VIK_DEFAULT_ALTITUDE) ) {
223 if ( array[i] > *max )
224 *max = array[i];
225 if ( array[i] < *min )
226 *min = array[i];
50a14534
EB
227 }
228 }
50a14534
EB
229}
230
d03d80e6 231#define MARGIN 70
25e44eac 232#define LINES 5
c6bc2190
RN
233/**
234 * Returns via pointers:
235 * the new minimum value to be used for the altitude graph
236 * the index in to the altitudes chunk sizes array (ci = Chunk Index)
237 */
238static void get_new_min_and_chunk_index_altitude (gdouble mina, gdouble maxa, gdouble *new_min, gint *ci)
239{
240 /* Get unitized chunk */
241 /* Find suitable chunk index */
242 *ci = 0;
243 gdouble diffa_chunk = (maxa - mina)/LINES;
244
245 /* Loop through to find best match */
246 while (diffa_chunk > chunksa[*ci]) {
247 (*ci)++;
248 /* Last Resort Check */
249 if ( *ci == sizeof(chunksa)/sizeof(chunksa[0]) )
250 break;
251 }
252
253 /* Ensure adjusted minimum .. maximum covers mina->maxa */
254
255 // Now work out adjusted minimum point to the nearest lowest chunk divisor value
256 // When negative ensure logic uses lowest value
257 if ( mina < 0 )
258 *new_min = (gdouble) ( ( (gint)(mina - chunksa[*ci]) / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
259 else
260 *new_min = (gdouble) ( ( (gint)mina / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
261
262 // Range not big enough - as new minimum has lowered
263 if ((*new_min + (chunksa[*ci] * LINES) < maxa)) {
264 // Next chunk should cover it
265 if ( *ci < sizeof(chunksa)/sizeof(chunksa[0]) ) {
266 (*ci)++;
e0826724
RN
267 // Remember to adjust the minimum too...
268 if ( mina < 0 )
269 *new_min = (gdouble) ( ( (gint)(mina - chunksa[*ci]) / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
270 else
271 *new_min = (gdouble) ( ( (gint)mina / (gint)chunksa[*ci] ) * (gint)chunksa[*ci] );
c6bc2190
RN
272 }
273 }
274}
275
0ba33e1d
FA
276/**
277 * Returns via pointers:
278 * the new minimum value to be used for the altitude graph
279 * the index in to the altitudes chunk sizes array (ci = Chunk Index)
280 */
281static void get_new_min_and_chunk_index_gradient (gdouble mina, gdouble maxa, gdouble *new_min, gint *ci)
282{
283 /* Get unitized chunk */
284 /* Find suitable chunk index */
285 *ci = 0;
286 gdouble diffa_chunk = (maxa - mina)/LINES;
287
288 /* Loop through to find best match */
289 while (diffa_chunk > chunksg[*ci]) {
290 (*ci)++;
291 /* Last Resort Check */
292 if ( *ci == sizeof(chunksg)/sizeof(chunksg[0]) )
293 break;
294 }
295
296 /* Ensure adjusted minimum .. maximum covers mina->maxa */
297
298 // Now work out adjusted minimum point to the nearest lowest chunk divisor value
299 // When negative ensure logic uses lowest value
300 if ( mina < 0 )
301 *new_min = (gdouble) ( ( (gint)(mina - chunksg[*ci]) / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
302 else
303 *new_min = (gdouble) ( ( (gint)mina / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
304
305 // Range not big enough - as new minimum has lowered
306 if ((*new_min + (chunksg[*ci] * LINES) < maxa)) {
307 // Next chunk should cover it
308 if ( *ci < sizeof(chunksg)/sizeof(chunksg[0]) ) {
309 (*ci)++;
310 // Remember to adjust the minimum too...
311 if ( mina < 0 )
312 *new_min = (gdouble) ( ( (gint)(mina - chunksg[*ci]) / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
313 else
314 *new_min = (gdouble) ( ( (gint)mina / (gint)chunksg[*ci] ) * (gint)chunksg[*ci] );
315 }
316 }
317}
318
976c7df6
RN
319/**
320 * Returns via pointers:
321 * the new minimum value to be used for the appropriate graph
322 * the index in to that array (ci = Chunk Index)
323 */
324static void get_new_min_and_chunk_index (gdouble mins, gdouble maxs, const gdouble *chunks, size_t chunky, gdouble *new_min, gint *ci)
325{
326 *ci = 0;
327 gdouble diff_chunk = (maxs - mins)/LINES;
328
329 /* Loop through to find best match */
330 while (diff_chunk > chunks[*ci]) {
331 (*ci)++;
332 /* Last Resort Check */
333 if ( *ci == chunky )
334 break;
335 }
336 *new_min = (gdouble) ( (gint)((mins / chunks[*ci] ) * chunks[*ci]) );
337
338 // Speeds are never negative so don't need to worry about a negative new minimum
339}
340
b66b0b38
RN
341static VikTrackpoint *set_center_at_graph_position(gdouble event_x,
342 gint img_width,
343 VikTrwLayer *vtl,
344 VikLayersPanel *vlp,
345 VikViewport *vvp,
346 VikTrack *tr,
347 gboolean time_base,
348 gint PROFILE_WIDTH)
24d5c7e2 349{
32e48121
QT
350 VikTrackpoint *trackpoint;
351 gdouble x = event_x - img_width / 2 + PROFILE_WIDTH / 2 - MARGIN / 2;
e1e2f2c6
JJ
352 if (x < 0)
353 x = 0;
354 if (x > PROFILE_WIDTH)
355 x = PROFILE_WIDTH;
32e48121
QT
356
357 if (time_base)
ddc2372e 358 trackpoint = vik_track_get_closest_tp_by_percentage_time ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
32e48121 359 else
ddc2372e 360 trackpoint = vik_track_get_closest_tp_by_percentage_dist ( tr, (gdouble) x / PROFILE_WIDTH, NULL );
32e48121 361
e1e2f2c6
JJ
362 if ( trackpoint ) {
363 VikCoord coord = trackpoint->coord;
6bb72350
RN
364 if ( vlp ) {
365 vik_viewport_set_center_coord ( vik_layers_panel_get_viewport(vlp), &coord );
366 vik_layers_panel_emit_update ( vlp );
367 }
368 else {
369 /* since vlp not set, vvp should be valid instead! */
370 if ( vvp )
371 vik_viewport_set_center_coord ( vvp, &coord );
da121f9b 372 vik_layer_emit_update ( VIK_LAYER(vtl) );
6bb72350 373 }
e1e2f2c6 374 }
e60fc2c9 375 return trackpoint;
e1e2f2c6 376}
ca7e67ef 377
7ee55449 378/**
50391e88 379 * Returns whether the marker was drawn or not and whether the blob was drawn or not
7ee55449 380 */
50391e88
RN
381static void save_image_and_draw_graph_marks (GtkWidget *image,
382 gdouble marker_x,
383 GdkGC *gc,
384 gint blob_x,
385 gint blob_y,
386 PropSaved *saved_img,
387 gint PROFILE_WIDTH,
388 gint PROFILE_HEIGHT,
389 gboolean *marker_drawn,
390 gboolean *blob_drawn)
32e48121 391{
d973878f 392 GdkPixmap *pix = NULL;
9dc30292 393 /* the pixmap = margin + graph area */
ca7e67ef 394 gtk_image_get_pixmap(GTK_IMAGE(image), &pix, NULL);
50391e88 395
7ee55449 396 /* Restore previously saved image */
ca7e67ef 397 if (saved_img->saved) {
50391e88 398 gdk_draw_image(GDK_DRAWABLE(pix), gc, saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
ca7e67ef 399 saved_img->saved = FALSE;
ca7e67ef 400 }
50391e88
RN
401
402 // ATM always save whole image - as anywhere could have changed
403 if (saved_img->img)
404 gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
405 else
406 saved_img->img = gdk_drawable_copy_to_image(GDK_DRAWABLE(pix), saved_img->img, 0, 0, 0, 0, MARGIN+PROFILE_WIDTH, PROFILE_HEIGHT);
407 saved_img->saved = TRUE;
408
409 if ((marker_x >= MARGIN) && (marker_x < (PROFILE_WIDTH + MARGIN))) {
410 gdk_draw_line (GDK_DRAWABLE(pix), gc, marker_x, 0, marker_x, image->allocation.height);
7ee55449 411 *marker_drawn = TRUE;
ca7e67ef 412 }
7ee55449
RN
413 else
414 *marker_drawn = FALSE;
50391e88
RN
415
416 // Draw a square blob to indicate where we are on track for this graph
417 if ( (blob_x >= MARGIN) && (blob_x < (PROFILE_WIDTH + MARGIN)) && (blob_y < PROFILE_HEIGHT) ) {
418 gdk_draw_rectangle (GDK_DRAWABLE(pix), gc, TRUE, blob_x-3, blob_y-3, 6, 6);
419 *blob_drawn = TRUE;
420 }
421 else
422 *blob_drawn = FALSE;
423
424 // Anywhere on image could have changed
425 if (*marker_drawn || *blob_drawn)
426 gtk_widget_queue_draw(image);
32e48121
QT
427}
428
fb822443
RN
429/**
430 * Return the percentage of how far a trackpoint is a long a track via the time method
431 */
432static gdouble tp_percentage_by_time ( VikTrack *tr, VikTrackpoint *trackpoint )
433{
434 gdouble pc = NAN;
435 if (trackpoint == NULL)
436 return pc;
437 time_t t_start, t_end, t_total;
438 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
439 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
440 t_total = t_end - t_start;
441 pc = (gdouble)(trackpoint->timestamp - t_start)/t_total;
442 return pc;
443}
444
445/**
446 * Return the percentage of how far a trackpoint is a long a track via the distance method
447 */
448static gdouble tp_percentage_by_distance ( VikTrack *tr, VikTrackpoint *trackpoint, gdouble track_length )
449{
450 gdouble pc = NAN;
451 if (trackpoint == NULL)
452 return pc;
453 gdouble dist = 0.0;
454 GList *iter;
455 for (iter = tr->trackpoints->next; iter != NULL; iter = iter->next) {
456 dist += vik_coord_diff(&(VIK_TRACKPOINT(iter->data)->coord),
457 &(VIK_TRACKPOINT(iter->prev->data)->coord));
458 /* Assuming trackpoint is not a copy */
459 if (trackpoint == VIK_TRACKPOINT(iter->data))
460 break;
461 }
462 if (iter != NULL)
463 pc = dist/track_length;
464 return pc;
465}
466
2cf168ac 467static void track_graph_click( GtkWidget *event_box, GdkEventButton *event, PropWidgets *widgets, VikPropWinGraphType_t graph_type )
32e48121 468{
7b624086
RN
469 gboolean is_time_graph =
470 ( graph_type == PROPWIN_GRAPH_TYPE_SPEED_TIME ||
471 graph_type == PROPWIN_GRAPH_TYPE_DISTANCE_TIME ||
472 graph_type == PROPWIN_GRAPH_TYPE_ELEVATION_TIME );
ca7e67ef 473
2cf168ac 474 VikTrackpoint *trackpoint = set_center_at_graph_position(event->x, event_box->allocation.width, widgets->vtl, widgets->vlp, widgets->vvp, widgets->tr, is_time_graph, widgets->profile_width);
926c8140
RN
475 // Unable to get the point so give up
476 if ( trackpoint == NULL ) {
477 gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
e60fc2c9 478 return;
926c8140
RN
479 }
480
481 widgets->marker_tp = trackpoint;
e60fc2c9 482
926c8140
RN
483 GList *child;
484 GtkWidget *image;
485 GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(event_box));
486 GtkWidget *graph_box;
487 PropSaved *graph_saved_img;
e60fc2c9 488 gdouble pc = NAN;
e60fc2c9 489
926c8140
RN
490 // Attempt to redraw marker on all graph types
491 gint graphite;
492 for ( graphite = PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE;
493 graphite < PROPWIN_GRAPH_TYPE_END;
494 graphite++ ) {
495
496 // Switch commonal variables to particular graph type
497 switch (graphite) {
498 default:
499 case PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE:
500 graph_box = widgets->elev_box;
501 graph_saved_img = &widgets->elev_graph_saved_img;
502 is_time_graph = FALSE;
503 break;
0ba33e1d
FA
504 case PROPWIN_GRAPH_TYPE_GRADIENT_DISTANCE:
505 graph_box = widgets->gradient_box;
506 graph_saved_img = &widgets->gradient_graph_saved_img;
507 is_time_graph = FALSE;
508 break;
926c8140
RN
509 case PROPWIN_GRAPH_TYPE_SPEED_TIME:
510 graph_box = widgets->speed_box;
511 graph_saved_img = &widgets->speed_graph_saved_img;
512 is_time_graph = TRUE;
513 break;
514 case PROPWIN_GRAPH_TYPE_DISTANCE_TIME:
515 graph_box = widgets->dist_box;
516 graph_saved_img = &widgets->dist_graph_saved_img;
517 is_time_graph = TRUE;
518 break;
8de26632
RN
519 case PROPWIN_GRAPH_TYPE_ELEVATION_TIME:
520 graph_box = widgets->elev_time_box;
521 graph_saved_img = &widgets->elev_time_graph_saved_img;
522 is_time_graph = TRUE;
523 break;
7b624086
RN
524 case PROPWIN_GRAPH_TYPE_SPEED_DISTANCE:
525 graph_box = widgets->speed_dist_box;
526 graph_saved_img = &widgets->speed_dist_graph_saved_img;
527 is_time_graph = FALSE;
528 break;
926c8140
RN
529 }
530
531 // Commonal method of redrawing marker
532 if ( graph_box ) {
e60fc2c9 533
926c8140
RN
534 child = gtk_container_get_children(GTK_CONTAINER(graph_box));
535 image = GTK_WIDGET(child->data);
536
537 if (is_time_graph)
2cf168ac 538 pc = tp_percentage_by_time ( widgets->tr, trackpoint );
926c8140 539 else
2cf168ac 540 pc = tp_percentage_by_distance ( widgets->tr, trackpoint, widgets->track_length_inc_gaps );
926c8140
RN
541
542 if (!isnan(pc)) {
543 gdouble marker_x = (pc * widgets->profile_width) + MARGIN;
544 save_image_and_draw_graph_marks(image,
545 marker_x,
ff37db21 546 gtk_widget_get_style(window)->black_gc,
926c8140
RN
547 -1, // Don't draw blob on clicks
548 0,
549 graph_saved_img,
550 widgets->profile_width,
551 widgets->profile_height,
552 &widgets->is_marker_drawn,
553 &widgets->is_blob_drawn);
554 }
555 g_list_free(child);
556 }
557 }
558
559 gtk_dialog_set_response_sensitive(GTK_DIALOG(widgets->dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, widgets->is_marker_drawn);
ca7e67ef
QT
560}
561
2cf168ac 562static gboolean track_profile_click( GtkWidget *event_box, GdkEventButton *event, gpointer ptr )
ca7e67ef 563{
2cf168ac 564 track_graph_click(event_box, event, ptr, PROPWIN_GRAPH_TYPE_ELEVATION_DISTANCE);
ca7e67ef
QT
565 return TRUE; /* don't call other (further) callbacks */
566}
567
2cf168ac 568static gboolean track_gradient_click( GtkWidget *event_box, GdkEventButton *event, gpointer ptr )
0ba33e1d 569{
2cf168ac 570 track_graph_click(event_box, event, ptr, PROPWIN_GRAPH_TYPE_GRADIENT_DISTANCE);
0ba33e1d
FA
571 return TRUE; /* don't call other (further) callbacks */
572}
573
2cf168ac 574static gboolean track_vt_click( GtkWidget *event_box, GdkEventButton *event, gpointer ptr )
ca7e67ef 575{
2cf168ac 576 track_graph_click(event_box, event, ptr, PROPWIN_GRAPH_TYPE_SPEED_TIME);
926c8140
RN
577 return TRUE; /* don't call other (further) callbacks */
578}
579
2cf168ac 580static gboolean track_dt_click( GtkWidget *event_box, GdkEventButton *event, gpointer ptr )
926c8140 581{
2cf168ac 582 track_graph_click(event_box, event, ptr, PROPWIN_GRAPH_TYPE_DISTANCE_TIME);
ca7e67ef 583 return TRUE; /* don't call other (further) callbacks */
32e48121
QT
584}
585
2cf168ac 586static gboolean track_et_click( GtkWidget *event_box, GdkEventButton *event, gpointer ptr )
8de26632 587{
2cf168ac 588 track_graph_click(event_box, event, ptr, PROPWIN_GRAPH_TYPE_ELEVATION_TIME);
8de26632
RN
589 return TRUE; /* don't call other (further) callbacks */
590}
591
2cf168ac 592static gboolean track_sd_click( GtkWidget *event_box, GdkEventButton *event, gpointer ptr )
7b624086 593{
2cf168ac 594 track_graph_click(event_box, event, ptr, PROPWIN_GRAPH_TYPE_SPEED_DISTANCE);
7b624086
RN
595 return TRUE; /* don't call other (further) callbacks */
596}
597
50391e88
RN
598/**
599 * Calculate y position for blob on elevation graph
600 */
601static gint blobby_altitude ( gdouble x_blob, PropWidgets *widgets )
602{
603 gint ix = (gint)x_blob;
604 // Ensure ix is inbounds
605 if (ix == widgets->profile_width)
606 ix--;
607
c6bc2190 608 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->altitudes[ix]-widgets->draw_min_altitude)/(chunksa[widgets->cia]*LINES);
50391e88
RN
609
610 return y_blob;
611}
612
0ba33e1d
FA
613/**
614 * Calculate y position for blob on gradient graph
615 */
616static gint blobby_gradient ( gdouble x_blob, PropWidgets *widgets )
617{
618 gint ix = (gint)x_blob;
619 // Ensure ix is inbounds
620 if (ix == widgets->profile_width)
621 ix--;
622
623 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->gradients[ix]-widgets->draw_min_gradient)/(chunksg[widgets->cig]*LINES);
624
625 return y_blob;
626}
627
50391e88
RN
628/**
629 * Calculate y position for blob on speed graph
630 */
631static gint blobby_speed ( gdouble x_blob, PropWidgets *widgets )
632{
633 gint ix = (gint)x_blob;
634 // Ensure ix is inbounds
635 if (ix == widgets->profile_width)
636 ix--;
637
976c7df6 638 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->speeds[ix]-widgets->draw_min_speed)/(chunkss[widgets->cis]*LINES);
50391e88
RN
639
640 return y_blob;
641}
642
926c8140
RN
643/**
644 * Calculate y position for blob on distance graph
645 */
646static gint blobby_distance ( gdouble x_blob, PropWidgets *widgets )
647{
648 gint ix = (gint)x_blob;
649 // Ensure ix is inbounds
650 if (ix == widgets->profile_width)
651 ix--;
652
653 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->distances[ix])/(chunksd[widgets->cid]*LINES);
654 //NB min distance is always 0, so no need to subtract that from this ______/
655
656 return y_blob;
657}
658
8de26632
RN
659/**
660 * Calculate y position for blob on elevation/time graph
661 */
662static gint blobby_altitude_time ( gdouble x_blob, PropWidgets *widgets )
663{
664 gint ix = (gint)x_blob;
665 // Ensure ix is inbounds
666 if (ix == widgets->profile_width)
667 ix--;
668
d2bae53c 669 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->ats[ix]-widgets->draw_min_altitude_time)/(chunksa[widgets->ciat]*LINES);
8de26632
RN
670 return y_blob;
671}
672
7b624086
RN
673/**
674 * Calculate y position for blob on speed/dist graph
675 */
676static gint blobby_speed_dist ( gdouble x_blob, PropWidgets *widgets )
677{
678 gint ix = (gint)x_blob;
679 // Ensure ix is inbounds
680 if (ix == widgets->profile_width)
681 ix--;
682
683 gint y_blob = widgets->profile_height-widgets->profile_height*(widgets->speeds_dist[ix]-widgets->draw_min_speed)/(chunkss[widgets->cisd]*LINES);
684
685 return y_blob;
686}
687
8de26632 688
2cf168ac 689void track_profile_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
e1e2f2c6 690{
e1e2f2c6
JJ
691 int mouse_x, mouse_y;
692 GdkModifierType state;
693
694 if (event->is_hint)
695 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
696 else
697 mouse_x = event->x;
698
50391e88 699 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
e1e2f2c6
JJ
700 if (x < 0)
701 x = 0;
b66b0b38
RN
702 if (x > widgets->profile_width)
703 x = widgets->profile_width;
e1e2f2c6 704
ddc2372e 705 gdouble meters_from_start;
2cf168ac 706 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( widgets->tr, (gdouble) x / widgets->profile_width, &meters_from_start );
065f60f1 707 if (trackpoint && widgets->w_cur_dist) {
ddc2372e 708 static gchar tmp_buf[20];
6f9336aa
RN
709 vik_units_distance_t dist_units = a_vik_get_units_distance ();
710 switch (dist_units) {
711 case VIK_UNITS_DISTANCE_KILOMETRES:
712 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
713 break;
714 case VIK_UNITS_DISTANCE_MILES:
433b3f7f 715 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
6f9336aa
RN
716 break;
717 default:
718 g_critical("Houston, we've had a problem. distance=%d", dist_units);
719 }
065f60f1 720 gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist), tmp_buf);
ddc2372e 721 }
780d3771
RN
722
723 // Show track elevation for this position - to the nearest whole number
724 if (trackpoint && widgets->w_cur_elevation) {
725 static gchar tmp_buf[20];
726 if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
727 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude));
728 else
729 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude);
730 gtk_label_set_text(GTK_LABEL(widgets->w_cur_elevation), tmp_buf);
731 }
50391e88
RN
732
733 widgets->blob_tp = trackpoint;
734
735 if ( widgets->altitudes == NULL )
736 return;
737
738 GtkWidget *window = gtk_widget_get_toplevel (event_box);
739 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
740 GtkWidget *image = GTK_WIDGET(child->data);
741
742 gint y_blob = blobby_altitude (x, widgets);
743
744 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
745 if (widgets->is_marker_drawn) {
2cf168ac 746 gdouble pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
50391e88
RN
747 if (!isnan(pc)) {
748 marker_x = (pc * widgets->profile_width) + MARGIN;
749 }
750 }
751
752 save_image_and_draw_graph_marks (image,
753 marker_x,
ff37db21 754 gtk_widget_get_style(window)->black_gc,
50391e88
RN
755 MARGIN+x,
756 y_blob,
757 &widgets->elev_graph_saved_img,
758 widgets->profile_width,
759 widgets->profile_height,
760 &widgets->is_marker_drawn,
761 &widgets->is_blob_drawn);
762
763 g_list_free(child);
ddc2372e
QT
764}
765
2cf168ac 766void track_gradient_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
0ba33e1d 767{
0ba33e1d
FA
768 int mouse_x, mouse_y;
769 GdkModifierType state;
770
771 if (event->is_hint)
772 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
773 else
774 mouse_x = event->x;
775
776 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
777 if (x < 0)
778 x = 0;
779 if (x > widgets->profile_width)
780 x = widgets->profile_width;
781
782 gdouble meters_from_start;
2cf168ac 783 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( widgets->tr, (gdouble) x / widgets->profile_width, &meters_from_start );
0ba33e1d
FA
784 if (trackpoint && widgets->w_cur_gradient_dist) {
785 static gchar tmp_buf[20];
786 vik_units_distance_t dist_units = a_vik_get_units_distance ();
787 switch (dist_units) {
788 case VIK_UNITS_DISTANCE_KILOMETRES:
789 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
790 break;
791 case VIK_UNITS_DISTANCE_MILES:
792 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
793 break;
794 default:
795 g_critical("Houston, we've had a problem. distance=%d", dist_units);
796 }
797 gtk_label_set_text(GTK_LABEL(widgets->w_cur_gradient_dist), tmp_buf);
798 }
799
800 // Show track gradient for this position - to the nearest whole number
801 if (trackpoint && widgets->w_cur_gradient_gradient) {
802 static gchar tmp_buf[20];
803
804 double gradient = widgets->gradients[(int) x];
805
806 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d%%", (int)gradient);
807 gtk_label_set_text(GTK_LABEL(widgets->w_cur_gradient_gradient), tmp_buf);
808 }
809
810 widgets->blob_tp = trackpoint;
811
812 if ( widgets->gradients == NULL )
813 return;
814
815 GtkWidget *window = gtk_widget_get_toplevel (event_box);
816 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
817 GtkWidget *image = GTK_WIDGET(child->data);
818
819 gint y_blob = blobby_gradient (x, widgets);
820
821 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
822 if (widgets->is_marker_drawn) {
2cf168ac 823 gdouble pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
0ba33e1d
FA
824 if (!isnan(pc)) {
825 marker_x = (pc * widgets->profile_width) + MARGIN;
826 }
827 }
828
829 save_image_and_draw_graph_marks (image,
830 marker_x,
ff37db21 831 gtk_widget_get_style(window)->black_gc,
0ba33e1d
FA
832 MARGIN+x,
833 y_blob,
834 &widgets->gradient_graph_saved_img,
835 widgets->profile_width,
836 widgets->profile_height,
837 &widgets->is_marker_drawn,
838 &widgets->is_blob_drawn);
839
840 g_list_free(child);
841}
842
2cf168ac 843void track_vt_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
ddc2372e 844{
ddc2372e
QT
845 int mouse_x, mouse_y;
846 GdkModifierType state;
847
848 if (event->is_hint)
849 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
850 else
851 mouse_x = event->x;
852
50391e88 853 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
ddc2372e
QT
854 if (x < 0)
855 x = 0;
b66b0b38
RN
856 if (x > widgets->profile_width)
857 x = widgets->profile_width;
ddc2372e
QT
858
859 time_t seconds_from_start;
2cf168ac 860 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( widgets->tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
065f60f1 861 if (trackpoint && widgets->w_cur_time) {
ddc2372e
QT
862 static gchar tmp_buf[20];
863 guint h, m, s;
864 h = seconds_from_start/3600;
865 m = (seconds_from_start - h*3600)/60;
866 s = seconds_from_start - (3600*h) - (60*m);
867 g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
e1e2f2c6 868
065f60f1 869 gtk_label_set_text(GTK_LABEL(widgets->w_cur_time), tmp_buf);
24d5c7e2 870 }
780d3771 871
50391e88
RN
872 gint ix = (gint)x;
873 // Ensure ix is inbounds
874 if (ix == widgets->profile_width)
875 ix--;
876
780d3771
RN
877 // Show track speed for this position
878 if (trackpoint && widgets->w_cur_speed) {
879 static gchar tmp_buf[20];
880 // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown
976c7df6 881 // No conversions needed as already in appropriate units
780d3771
RN
882 vik_units_speed_t speed_units = a_vik_get_units_speed ();
883 switch (speed_units) {
884 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
976c7df6 885 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), widgets->speeds[ix]);
780d3771
RN
886 break;
887 case VIK_UNITS_SPEED_MILES_PER_HOUR:
976c7df6 888 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds[ix]);
780d3771
RN
889 break;
890 case VIK_UNITS_SPEED_KNOTS:
976c7df6 891 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds[ix]);
780d3771
RN
892 break;
893 default:
894 // VIK_UNITS_SPEED_METRES_PER_SECOND:
780d3771
RN
895 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds[ix]);
896 break;
897 }
898 gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed), tmp_buf);
899 }
50391e88
RN
900
901 widgets->blob_tp = trackpoint;
902
903 if ( widgets->speeds == NULL )
904 return;
905
906 GtkWidget *window = gtk_widget_get_toplevel (event_box);
907 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
908 GtkWidget *image = GTK_WIDGET(child->data);
909
910 gint y_blob = blobby_speed (x, widgets);
911
912 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
913 if (widgets->is_marker_drawn) {
2cf168ac 914 gdouble pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
50391e88
RN
915 if (!isnan(pc)) {
916 marker_x = (pc * widgets->profile_width) + MARGIN;
917 }
918 }
919
920 save_image_and_draw_graph_marks (image,
921 marker_x,
ff37db21 922 gtk_widget_get_style(window)->black_gc,
50391e88
RN
923 MARGIN+x,
924 y_blob,
925 &widgets->speed_graph_saved_img,
926 widgets->profile_width,
927 widgets->profile_height,
928 &widgets->is_marker_drawn,
929 &widgets->is_blob_drawn);
930
931 g_list_free(child);
24d5c7e2
EB
932}
933
926c8140
RN
934/**
935 * Update labels and blob marker on mouse moves in the distance/time graph
936 */
2cf168ac 937void track_dt_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
926c8140 938{
926c8140
RN
939 int mouse_x, mouse_y;
940 GdkModifierType state;
941
942 if (event->is_hint)
943 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
944 else
945 mouse_x = event->x;
946
947 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
948 if (x < 0)
949 x = 0;
950 if (x > widgets->profile_width)
951 x = widgets->profile_width;
952
953 time_t seconds_from_start;
2cf168ac 954 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( widgets->tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
926c8140
RN
955 if (trackpoint && widgets->w_cur_dist_time) {
956 static gchar tmp_buf[20];
957 guint h, m, s;
958 h = seconds_from_start/3600;
959 m = (seconds_from_start - h*3600)/60;
960 s = seconds_from_start - (3600*h) - (60*m);
961 g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
962
963 gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_time), tmp_buf);
964 }
965
966 gint ix = (gint)x;
967 // Ensure ix is inbounds
968 if (ix == widgets->profile_width)
969 ix--;
970
971 if (trackpoint && widgets->w_cur_dist_dist) {
972 static gchar tmp_buf[20];
973 if ( a_vik_get_units_distance () == VIK_UNITS_DISTANCE_MILES )
974 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", widgets->distances[ix]);
975 else
976 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", widgets->distances[ix]);
977 gtk_label_set_text(GTK_LABEL(widgets->w_cur_dist_dist), tmp_buf);
978 }
979
980 widgets->blob_tp = trackpoint;
981
982 if ( widgets->distances == NULL )
983 return;
984
985 GtkWidget *window = gtk_widget_get_toplevel (event_box);
986 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
987 GtkWidget *image = GTK_WIDGET(child->data);
988
989 gint y_blob = blobby_distance (x, widgets);
990
991 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
992 if (widgets->is_marker_drawn) {
2cf168ac 993 gdouble pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
926c8140
RN
994 if (!isnan(pc)) {
995 marker_x = (pc * widgets->profile_width) + MARGIN;
996 }
997 }
998
999 save_image_and_draw_graph_marks (image,
1000 marker_x,
ff37db21 1001 gtk_widget_get_style(window)->black_gc,
926c8140
RN
1002 MARGIN+x,
1003 y_blob,
1004 &widgets->dist_graph_saved_img,
1005 widgets->profile_width,
1006 widgets->profile_height,
1007 &widgets->is_marker_drawn,
1008 &widgets->is_blob_drawn);
1009
1010 g_list_free(child);
1011}
1012
8de26632
RN
1013/**
1014 * Update labels and blob marker on mouse moves in the elevation/time graph
1015 */
2cf168ac 1016void track_et_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
8de26632 1017{
8de26632
RN
1018 int mouse_x, mouse_y;
1019 GdkModifierType state;
1020
1021 if (event->is_hint)
1022 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
1023 else
1024 mouse_x = event->x;
1025
1026 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
1027 if (x < 0)
1028 x = 0;
1029 if (x > widgets->profile_width)
1030 x = widgets->profile_width;
1031
1032 time_t seconds_from_start;
2cf168ac 1033 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_time ( widgets->tr, (gdouble) x / widgets->profile_width, &seconds_from_start );
8de26632
RN
1034 if (trackpoint && widgets->w_cur_elev_time) {
1035 static gchar tmp_buf[20];
1036 guint h, m, s;
1037 h = seconds_from_start/3600;
1038 m = (seconds_from_start - h*3600)/60;
1039 s = seconds_from_start - (3600*h) - (60*m);
1040 g_snprintf(tmp_buf, sizeof(tmp_buf), "%02d:%02d:%02d", h, m, s);
1041
1042 gtk_label_set_text(GTK_LABEL(widgets->w_cur_elev_time), tmp_buf);
1043 }
1044
1045 gint ix = (gint)x;
1046 // Ensure ix is inbounds
1047 if (ix == widgets->profile_width)
1048 ix--;
1049
1050 if (trackpoint && widgets->w_cur_elev_elev) {
1051 static gchar tmp_buf[20];
1052 if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
1053 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d ft", (int)VIK_METERS_TO_FEET(trackpoint->altitude));
1054 else
1055 g_snprintf(tmp_buf, sizeof(tmp_buf), "%d m", (int)trackpoint->altitude);
1056 gtk_label_set_text(GTK_LABEL(widgets->w_cur_elev_elev), tmp_buf);
1057 }
1058
1059 widgets->blob_tp = trackpoint;
1060
1061 if ( widgets->ats == NULL )
1062 return;
1063
1064 GtkWidget *window = gtk_widget_get_toplevel (event_box);
1065 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
1066 GtkWidget *image = GTK_WIDGET(child->data);
1067
1068 gint y_blob = blobby_altitude_time (x, widgets);
1069
1070 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1071 if (widgets->is_marker_drawn) {
2cf168ac 1072 gdouble pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
8de26632
RN
1073 if (!isnan(pc)) {
1074 marker_x = (pc * widgets->profile_width) + MARGIN;
1075 }
1076 }
1077
1078 save_image_and_draw_graph_marks (image,
1079 marker_x,
ff37db21 1080 gtk_widget_get_style(window)->black_gc,
8de26632
RN
1081 MARGIN+x,
1082 y_blob,
1083 &widgets->elev_time_graph_saved_img,
1084 widgets->profile_width,
1085 widgets->profile_height,
1086 &widgets->is_marker_drawn,
1087 &widgets->is_blob_drawn);
1088
1089 g_list_free(child);
1090}
1091
2cf168ac 1092void track_sd_move( GtkWidget *event_box, GdkEventMotion *event, PropWidgets *widgets )
7b624086 1093{
7b624086
RN
1094 int mouse_x, mouse_y;
1095 GdkModifierType state;
1096
1097 if (event->is_hint)
1098 gdk_window_get_pointer (event->window, &mouse_x, &mouse_y, &state);
1099 else
1100 mouse_x = event->x;
1101
1102 gdouble x = mouse_x - event_box->allocation.width / 2 + widgets->profile_width / 2 - MARGIN / 2;
1103 if (x < 0)
1104 x = 0;
1105 if (x > widgets->profile_width)
1106 x = widgets->profile_width;
1107
1108 gdouble meters_from_start;
2cf168ac 1109 VikTrackpoint *trackpoint = vik_track_get_closest_tp_by_percentage_dist ( widgets->tr, (gdouble) x / widgets->profile_width, &meters_from_start );
7b624086
RN
1110 if (trackpoint && widgets->w_cur_speed_dist) {
1111 static gchar tmp_buf[20];
1112 vik_units_distance_t dist_units = a_vik_get_units_distance ();
1113 switch (dist_units) {
1114 case VIK_UNITS_DISTANCE_KILOMETRES:
1115 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", meters_from_start/1000.0);
1116 break;
1117 case VIK_UNITS_DISTANCE_MILES:
1118 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(meters_from_start) );
1119 break;
1120 default:
1121 g_critical("Houston, we've had a problem. distance=%d", dist_units);
1122 }
1123 gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed_dist), tmp_buf);
1124 }
1125
1126 gint ix = (gint)x;
1127 // Ensure ix is inbounds
1128 if (ix == widgets->profile_width)
1129 ix--;
1130
1131 if ( widgets->speeds_dist == NULL )
1132 return;
1133
1134 // Show track speed for this position
1135 if (widgets->w_cur_speed_speed) {
1136 static gchar tmp_buf[20];
1137 // Even if GPS speed available (trackpoint->speed), the text will correspond to the speed map shown
1138 // No conversions needed as already in appropriate units
1139 vik_units_speed_t speed_units = a_vik_get_units_speed ();
1140 switch (speed_units) {
1141 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1142 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f kph"), widgets->speeds_dist[ix]);
1143 break;
1144 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1145 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f mph"), widgets->speeds_dist[ix]);
1146 break;
1147 case VIK_UNITS_SPEED_KNOTS:
1148 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f knots"), widgets->speeds_dist[ix]);
1149 break;
1150 default:
1151 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1152 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%.1f m/s"), widgets->speeds_dist[ix]);
1153 break;
1154 }
1155 gtk_label_set_text(GTK_LABEL(widgets->w_cur_speed_speed), tmp_buf);
1156 }
1157
1158 widgets->blob_tp = trackpoint;
1159
1160 GtkWidget *window = gtk_widget_get_toplevel (event_box);
1161 GList *child = gtk_container_get_children(GTK_CONTAINER(event_box));
1162 GtkWidget *image = GTK_WIDGET(child->data);
1163
1164 gint y_blob = blobby_speed_dist (x, widgets);
1165
1166 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
1167 if (widgets->is_marker_drawn) {
2cf168ac 1168 gdouble pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
7b624086
RN
1169 if (!isnan(pc)) {
1170 marker_x = (pc * widgets->profile_width) + MARGIN;
1171 }
1172 }
1173
1174 save_image_and_draw_graph_marks (image,
1175 marker_x,
ff37db21 1176 gtk_widget_get_style(window)->black_gc,
7b624086
RN
1177 MARGIN+x,
1178 y_blob,
1179 &widgets->speed_dist_graph_saved_img,
1180 widgets->profile_width,
1181 widgets->profile_height,
1182 &widgets->is_marker_drawn,
1183 &widgets->is_blob_drawn);
1184
1185 g_list_free(child);
1186}
1187
6bca6a7d
RN
1188/**
1189 * Draws DEM points and a respresentative speed on the supplied pixmap
1190 * (which is the elevations graph)
1191 */
1192static void draw_dem_alt_speed_dist(VikTrack *tr,
1193 GdkDrawable *pix,
1194 GdkGC *alt_gc,
1195 GdkGC *speed_gc,
1196 gdouble alt_offset,
1197 gdouble alt_diff,
83cd69cb 1198 gdouble max_speed_in,
c6bc2190 1199 gint cia,
6bca6a7d
RN
1200 gint width,
1201 gint height,
1202 gint margin,
1203 gboolean do_dem,
1204 gboolean do_speed)
07596bf4
QT
1205{
1206 GList *iter;
07596bf4
QT
1207 gdouble max_speed = 0;
1208 gdouble total_length = vik_track_get_length_including_gaps(tr);
1209
83cd69cb
RN
1210 // Calculate the max speed factor
1211 if (do_speed)
1212 max_speed = max_speed_in * 110 / 100;
07596bf4 1213
6bca6a7d 1214 gdouble dist = 0;
07596bf4 1215 for (iter = tr->trackpoints->next; iter; iter = iter->next) {
6bca6a7d 1216 int x;
07596bf4 1217 dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
6bca6a7d 1218 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
07596bf4 1219 x = (width * dist)/total_length + margin;
6bca6a7d
RN
1220 if (do_dem) {
1221 gint16 elev = a_dems_get_elev_by_coord(&(VIK_TRACKPOINT(iter->data)->coord), VIK_DEM_INTERPOL_BEST);
1222 elev -= alt_offset;
1223 if ( elev != VIK_DEM_INVALID_ELEVATION ) {
c6bc2190
RN
1224 // Convert into height units
1225 if (a_vik_get_units_height () == VIK_UNITS_HEIGHT_FEET)
1226 elev = VIK_METERS_TO_FEET(elev);
1227 // No conversion needed if already in metres
1228
1229 // consider chunk size
3146f980 1230 int y_alt = height - ((height * elev)/(chunksa[cia]*LINES) );
6bca6a7d
RN
1231 gdk_draw_rectangle(GDK_DRAWABLE(pix), alt_gc, TRUE, x-2, y_alt-2, 4, 4);
1232 }
07596bf4 1233 }
6bca6a7d
RN
1234 if (do_speed) {
1235 // This is just a speed indicator - no actual values can be inferred by user
1236 if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
1237 int y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
1238 gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
1239 }
07596bf4
QT
1240 }
1241 }
1242}
1243
950d8a07
RN
1244/**
1245 * Draw just the height profile image
1246 */
1247static void draw_elevations (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
50a14534 1248{
950d8a07 1249 GtkWidget *window;
c79f0206 1250 GdkPixmap *pix;
25e44eac 1251 gdouble mina, maxa;
50a14534
EB
1252 guint i;
1253
950d8a07 1254 GdkGC *no_alt_info;
950d8a07
RN
1255 GdkColor color;
1256
0b2bfa08
RN
1257 // Free previous allocation
1258 if ( widgets->altitudes )
1259 g_free ( widgets->altitudes );
1260
b66b0b38 1261 widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
950d8a07 1262
0b2bfa08 1263 if ( widgets->altitudes == NULL )
950d8a07
RN
1264 return;
1265
c6bc2190
RN
1266 // Convert into appropriate units
1267 vik_units_height_t height_units = a_vik_get_units_height ();
1268 if ( height_units == VIK_UNITS_HEIGHT_FEET ) {
1269 // Convert altitudes into feet units
1270 for ( i = 0; i < widgets->profile_width; i++ ) {
1271 widgets->altitudes[i] = VIK_METERS_TO_FEET(widgets->altitudes[i]);
1272 }
1273 }
1274 // Otherwise leave in metres
1275
83cd69cb 1276 minmax_array(widgets->altitudes, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
c6bc2190
RN
1277
1278 get_new_min_and_chunk_index_altitude (widgets->min_altitude, widgets->max_altitude, &widgets->draw_min_altitude, &widgets->cia);
1279
83cd69cb 1280 // Assign locally
c6bc2190 1281 mina = widgets->draw_min_altitude;
83cd69cb 1282 maxa = widgets->max_altitude;
950d8a07
RN
1283
1284 window = gtk_widget_get_toplevel (widgets->elev_box);
c79f0206 1285
9b082b39 1286 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
c79f0206 1287
950d8a07 1288 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
50a14534 1289
9b082b39 1290 no_alt_info = gdk_gc_new ( gtk_widget_get_window(window) );
07596bf4 1291 gdk_color_parse ( "yellow", &color );
50a14534 1292 gdk_gc_set_rgb_fg_color ( no_alt_info, &color);
950d8a07 1293
25e44eac 1294 /* clear the image */
ff37db21 1295 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->bg_gc[0],
b66b0b38 1296 TRUE, 0, 0, MARGIN, widgets->profile_height);
ff37db21 1297 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->mid_gc[0],
b66b0b38 1298 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
950d8a07 1299
25e44eac 1300 /* draw grid */
25e44eac 1301 for (i=0; i<=LINES; i++) {
25e44eac
AF
1302 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1303 gchar s[32];
d03d80e6 1304 int w, h;
25e44eac 1305
d03d80e6 1306 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
ff37db21 1307 pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
56cb1807 1308
6027a9c5
RN
1309 switch (height_units) {
1310 case VIK_UNITS_HEIGHT_METRES:
c6bc2190 1311 sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
6027a9c5
RN
1312 break;
1313 case VIK_UNITS_HEIGHT_FEET:
c6bc2190
RN
1314 // NB values already converted into feet
1315 sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->cia]));
6027a9c5
RN
1316 break;
1317 default:
1318 sprintf(s, "--");
1319 g_critical("Houston, we've had a problem. height=%d", height_units);
1320 }
25e44eac 1321 pango_layout_set_text(pl, s, -1);
d03d80e6 1322 pango_layout_get_pixel_size (pl, &w, &h);
ff37db21 1323 gdk_draw_layout(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0], MARGIN-w-3,
b66b0b38 1324 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
25e44eac 1325
ff37db21 1326 gdk_draw_line (GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
b66b0b38 1327 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
6278ccc6 1328 g_object_unref ( G_OBJECT ( pl ) );
e6869c62 1329 pl = NULL;
25e44eac
AF
1330 }
1331
1332 /* draw elevations */
b66b0b38 1333 for ( i = 0; i < widgets->profile_width; i++ )
0b2bfa08 1334 if ( widgets->altitudes[i] == VIK_DEFAULT_ALTITUDE )
25e44eac 1335 gdk_draw_line ( GDK_DRAWABLE(pix), no_alt_info,
b66b0b38 1336 i + MARGIN, 0, i + MARGIN, widgets->profile_height );
bb71de8b 1337 else
ff37db21 1338 gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
c6bc2190 1339 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->altitudes[i]-mina)/(chunksa[widgets->cia]*LINES) );
734652bf 1340
2280c34f
RN
1341 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_dem)) ||
1342 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)) ) {
1343
9b082b39
RN
1344 GdkGC *dem_alt_gc = gdk_gc_new ( gtk_widget_get_window(window) );
1345 GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
2280c34f
RN
1346
1347 gdk_color_parse ( "green", &color );
1348 gdk_gc_set_rgb_fg_color ( dem_alt_gc, &color);
1349
1350 gdk_color_parse ( "red", &color );
1351 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1352
1353 // Ensure somekind of max speed when not set
1354 if ( widgets->max_speed < 0.01 )
1355 widgets->max_speed = vik_track_get_max_speed(tr);
1356
1357 draw_dem_alt_speed_dist(tr,
1358 GDK_DRAWABLE(pix),
1359 dem_alt_gc,
1360 gps_speed_gc,
1361 mina,
1362 maxa - mina,
1363 widgets->max_speed,
1364 widgets->cia,
1365 widgets->profile_width,
1366 widgets->profile_height,
1367 MARGIN,
1368 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_dem)),
1369 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)));
1370
1371 g_object_unref ( G_OBJECT(dem_alt_gc) );
1372 g_object_unref ( G_OBJECT(gps_speed_gc) );
1373 }
07596bf4 1374
25e44eac 1375 /* draw border */
ff37db21 1376 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
25e44eac 1377
50a14534 1378 g_object_unref ( G_OBJECT(pix) );
50a14534 1379 g_object_unref ( G_OBJECT(no_alt_info) );
50a14534 1380}
24d5c7e2 1381
0ba33e1d
FA
1382/**
1383 * Draws representative speed on the supplied pixmap
1384 * (which is the gradients graph)
1385 */
1386static void draw_speed_dist(VikTrack *tr,
1387 GdkDrawable *pix,
1388 GdkGC *speed_gc,
1389 gdouble max_speed_in,
1390 gint width,
1391 gint height,
1392 gint margin,
1393 gboolean do_speed)
1394{
1395 GList *iter;
1396 gdouble max_speed = 0;
1397 gdouble total_length = vik_track_get_length_including_gaps(tr);
1398
1399 // Calculate the max speed factor
1400 if (do_speed)
1401 max_speed = max_speed_in * 110 / 100;
1402
1403 gdouble dist = 0;
1404 for (iter = tr->trackpoints->next; iter; iter = iter->next) {
1405 int x;
1406 dist += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1407 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1408 x = (width * dist)/total_length + margin;
1409 if (do_speed) {
1410 // This is just a speed indicator - no actual values can be inferred by user
1411 if (!isnan(VIK_TRACKPOINT(iter->data)->speed)) {
1412 int y_speed = height - (height * VIK_TRACKPOINT(iter->data)->speed)/max_speed;
1413 gdk_draw_rectangle(GDK_DRAWABLE(pix), speed_gc, TRUE, x-2, y_speed-2, 4, 4);
1414 }
1415 }
1416 }
1417}
1418
1419/**
1420 * Draw just the gradient image
1421 */
1422static void draw_gradients (GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1423{
1424 GtkWidget *window;
1425 GdkPixmap *pix;
1426 gdouble mina;
1427 guint i;
1428
1429 GdkColor color;
1430
1431 // Free previous allocation
1432 if ( widgets->gradients )
1433 g_free ( widgets->gradients );
1434
1435 widgets->gradients = vik_track_make_gradient_map ( tr, widgets->profile_width );
1436
1437 if ( widgets->gradients == NULL )
1438 return;
1439
1440 minmax_array(widgets->gradients, &widgets->min_gradient, &widgets->max_gradient, TRUE, widgets->profile_width);
1441
1442 get_new_min_and_chunk_index_gradient (widgets->min_gradient, widgets->max_gradient, &widgets->draw_min_gradient, &widgets->cig);
1443
1444 // Assign locally
1445 mina = widgets->draw_min_gradient;
1446
1447 window = gtk_widget_get_toplevel (widgets->gradient_box);
1448
9b082b39 1449 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
0ba33e1d
FA
1450
1451 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1452
1453 /* clear the image */
ff37db21 1454 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->bg_gc[0],
0ba33e1d 1455 TRUE, 0, 0, MARGIN, widgets->profile_height);
ff37db21 1456 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->mid_gc[0],
0ba33e1d
FA
1457 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1458
1459 /* draw grid */
1460 for (i=0; i<=LINES; i++) {
0ba33e1d
FA
1461 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1462 gchar s[32];
1463 int w, h;
1464
1465 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
ff37db21 1466 pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
56cb1807 1467
0ba33e1d
FA
1468 sprintf(s, "%8d%%", (int)(mina + (LINES-i)*chunksg[widgets->cig]));
1469 pango_layout_set_text(pl, s, -1);
1470 pango_layout_get_pixel_size (pl, &w, &h);
ff37db21 1471 gdk_draw_layout(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0], MARGIN-w-3,
0ba33e1d
FA
1472 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1473
ff37db21 1474 gdk_draw_line (GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
0ba33e1d
FA
1475 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1476 g_object_unref ( G_OBJECT ( pl ) );
1477 pl = NULL;
1478 }
1479
1480 /* draw gradients */
1481 for ( i = 0; i < widgets->profile_width; i++ )
ff37db21 1482 gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
0ba33e1d
FA
1483 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->gradients[i]-mina)/(chunksg[widgets->cig]*LINES) );
1484
1485 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_gradient_gps_speed)) ) {
9b082b39 1486 GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
0ba33e1d
FA
1487
1488 gdk_color_parse ( "red", &color );
1489 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1490
1491 // Ensure somekind of max speed when not set
1492 if ( widgets->max_speed < 0.01 )
1493 widgets->max_speed = vik_track_get_max_speed(tr);
1494
1495 draw_speed_dist(tr,
1496 GDK_DRAWABLE(pix),
1497 gps_speed_gc,
1498 widgets->max_speed,
1499 widgets->profile_width,
1500 widgets->profile_height,
1501 MARGIN,
1502 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widgets->w_show_alt_gps_speed)));
1503
1504 g_object_unref ( G_OBJECT(gps_speed_gc) );
1505 }
1506
1507 /* draw border */
ff37db21 1508 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
0ba33e1d
FA
1509
1510 g_object_unref ( G_OBJECT(pix) );
1511}
1512
950d8a07
RN
1513/**
1514 * Draw just the speed (velocity)/time image
1515 */
1516static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
25e44eac 1517{
950d8a07 1518 GtkWidget *window;
c79f0206 1519 GdkPixmap *pix;
7169b863 1520 gdouble mins;
25e44eac 1521 guint i;
c79f0206 1522
0b2bfa08
RN
1523 // Free previous allocation
1524 if ( widgets->speeds )
1525 g_free ( widgets->speeds );
1526
b66b0b38 1527 widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
0b2bfa08 1528 if ( widgets->speeds == NULL )
950d8a07
RN
1529 return;
1530
976c7df6
RN
1531 // Convert into appropriate units
1532 vik_units_speed_t speed_units = a_vik_get_units_speed ();
1533 switch (speed_units) {
1534 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1535 for ( i = 0; i < widgets->profile_width; i++ ) {
1536 widgets->speeds[i] = VIK_MPS_TO_KPH(widgets->speeds[i]);
1537 }
1538 break;
1539 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1540 for ( i = 0; i < widgets->profile_width; i++ ) {
1541 widgets->speeds[i] = VIK_MPS_TO_MPH(widgets->speeds[i]);
1542 }
1543 break;
1544 case VIK_UNITS_SPEED_KNOTS:
1545 for ( i = 0; i < widgets->profile_width; i++ ) {
1546 widgets->speeds[i] = VIK_MPS_TO_KNOTS(widgets->speeds[i]);
1547 }
1548 break;
1549 default:
1550 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1551 // No need to convert as already in m/s
1552 break;
1553 }
1554
950d8a07 1555 window = gtk_widget_get_toplevel (widgets->speed_box);
c79f0206 1556
9b082b39 1557 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
950d8a07
RN
1558
1559 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1560
83cd69cb
RN
1561 minmax_array(widgets->speeds, &widgets->min_speed, &widgets->max_speed, FALSE, widgets->profile_width);
1562 if (widgets->min_speed < 0.0)
1563 widgets->min_speed = 0; /* splines sometimes give negative speeds */
976c7df6
RN
1564
1565 /* Find suitable chunk index */
1566 get_new_min_and_chunk_index (widgets->min_speed, widgets->max_speed, chunkss, sizeof(chunkss)/sizeof(chunkss[0]), &widgets->draw_min_speed, &widgets->cis);
1567
83cd69cb 1568 // Assign locally
976c7df6 1569 mins = widgets->draw_min_speed;
25e44eac
AF
1570
1571 /* clear the image */
ff37db21 1572 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->bg_gc[0],
b66b0b38 1573 TRUE, 0, 0, MARGIN, widgets->profile_height);
ff37db21 1574 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->mid_gc[0],
b66b0b38 1575 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
25e44eac 1576
25e44eac 1577 /* draw grid */
25e44eac 1578 for (i=0; i<=LINES; i++) {
25e44eac
AF
1579 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1580 gchar s[32];
d03d80e6 1581 int w, h;
25e44eac 1582
d03d80e6 1583 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
ff37db21 1584 pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
56cb1807 1585
976c7df6 1586 // NB: No need to convert here anymore as numbers are in the appropriate units
13bdea80
RN
1587 switch (speed_units) {
1588 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
976c7df6 1589 sprintf(s, "%8dkm/h", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
13bdea80
RN
1590 break;
1591 case VIK_UNITS_SPEED_MILES_PER_HOUR:
976c7df6 1592 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
13bdea80
RN
1593 break;
1594 case VIK_UNITS_SPEED_METRES_PER_SECOND:
976c7df6 1595 sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
13bdea80 1596 break;
a4c92313 1597 case VIK_UNITS_SPEED_KNOTS:
976c7df6 1598 sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cis]));
a4c92313 1599 break;
13bdea80
RN
1600 default:
1601 sprintf(s, "--");
1602 g_critical("Houston, we've had a problem. speed=%d", speed_units);
1603 }
1604
25e44eac 1605 pango_layout_set_text(pl, s, -1);
d03d80e6 1606 pango_layout_get_pixel_size (pl, &w, &h);
ff37db21 1607 gdk_draw_layout(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0], MARGIN-w-3,
b66b0b38 1608 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
25e44eac 1609
ff37db21 1610 gdk_draw_line (GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
b66b0b38 1611 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
6278ccc6 1612 g_object_unref ( G_OBJECT ( pl ) );
e6869c62 1613 pl = NULL;
25e44eac 1614 }
d03d80e6 1615
25e44eac
AF
1616
1617 /* draw speeds */
b66b0b38 1618 for ( i = 0; i < widgets->profile_width; i++ )
ff37db21 1619 gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
976c7df6 1620 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(chunkss[widgets->cis]*LINES) );
07596bf4 1621
07596bf4 1622
ec32b567 1623 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_gps_speed)) ) {
2280c34f 1624
9b082b39 1625 GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
2280c34f
RN
1626 GdkColor color;
1627 gdk_color_parse ( "red", &color );
1628 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
1629
1630 time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1631 time_t dur = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
1632
ec32b567
RN
1633 GList *iter;
1634 for (iter = tr->trackpoints; iter; iter = iter->next) {
1635 gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
1636 if (isnan(gps_speed))
07596bf4 1637 continue;
976c7df6
RN
1638 switch (speed_units) {
1639 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1640 gps_speed = VIK_MPS_TO_KPH(gps_speed);
1641 break;
1642 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1643 gps_speed = VIK_MPS_TO_MPH(gps_speed);
1644 break;
1645 case VIK_UNITS_SPEED_KNOTS:
1646 gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
1647 break;
1648 default:
1649 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1650 // No need to convert as already in m/s
1651 break;
1652 }
ec32b567 1653 int x = MARGIN + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
976c7df6 1654 int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cis]*LINES);
ec32b567
RN
1655 gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
1656 }
2280c34f 1657 g_object_unref ( G_OBJECT(gps_speed_gc) );
07596bf4
QT
1658 }
1659
25e44eac 1660 /* draw border */
ff37db21 1661 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
25e44eac
AF
1662
1663 g_object_unref ( G_OBJECT(pix) );
926c8140
RN
1664}
1665
1666/**
1667 * Draw just the distance/time image
1668 */
1669static void draw_dt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1670{
1671 GtkWidget *window;
1672 GdkPixmap *pix;
1673 gdouble maxd;
1674 guint i;
1675
1676 // Free previous allocation
1677 if ( widgets->distances )
1678 g_free ( widgets->distances );
1679
1680 widgets->distances = vik_track_make_distance_map ( tr, widgets->profile_width );
1681 if ( widgets->distances == NULL )
1682 return;
1683
1684 // Convert into appropriate units
1685 vik_units_distance_t dist_units = a_vik_get_units_distance ();
1686 if ( dist_units == VIK_UNITS_DISTANCE_MILES ) {
1687 for ( i = 0; i < widgets->profile_width; i++ ) {
1688 widgets->distances[i] = VIK_METERS_TO_MILES(widgets->distances[i]);
1689 }
1690 }
1691 else {
1692 // Metres - but want in kms
1693 for ( i = 0; i < widgets->profile_width; i++ ) {
1694 widgets->distances[i] = widgets->distances[i]/1000.0;
1695 }
1696 }
1697
1698 window = gtk_widget_get_toplevel (widgets->dist_box);
1699
9b082b39 1700 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
926c8140
RN
1701
1702 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1703
1704 // easy to work out min / max of distance!
1705 // Assign locally
1706 // mind = 0.0; - Thus not used
1707 if ( dist_units == VIK_UNITS_DISTANCE_MILES )
1708 maxd = VIK_METERS_TO_MILES(vik_track_get_length_including_gaps (tr));
1709 else
1710 maxd = vik_track_get_length_including_gaps (tr) / 1000.0;
1711
1712 /* Find suitable chunk index */
1713 gdouble dummy = 0.0; // expect this to remain the same! (not that it's used)
1714 get_new_min_and_chunk_index (0, maxd, chunksd, sizeof(chunksd)/sizeof(chunksd[0]), &dummy, &widgets->cid);
1715
1716 /* clear the image */
ff37db21 1717 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->bg_gc[0],
926c8140 1718 TRUE, 0, 0, MARGIN, widgets->profile_height);
ff37db21 1719 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->mid_gc[0],
926c8140
RN
1720 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1721
1722 /* draw grid */
1723 for (i=0; i<=LINES; i++) {
926c8140
RN
1724 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1725 gchar s[32];
1726 int w, h;
1727
1728 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
ff37db21 1729 pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
56cb1807 1730
926c8140
RN
1731 if ( dist_units == VIK_UNITS_DISTANCE_MILES )
1732 sprintf(s, _("%.1f miles"), ((LINES-i)*chunksd[widgets->cid]));
1733 else
1734 sprintf(s, _("%.1f km"), ((LINES-i)*chunksd[widgets->cid]));
1735
1736 pango_layout_set_text(pl, s, -1);
1737 pango_layout_get_pixel_size (pl, &w, &h);
ff37db21 1738 gdk_draw_layout(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0], MARGIN-w-3,
926c8140
RN
1739 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1740
ff37db21 1741 gdk_draw_line (GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
926c8140
RN
1742 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1743 g_object_unref ( G_OBJECT ( pl ) );
1744 pl = NULL;
1745 }
1746
1747 /* draw distance */
1748 for ( i = 0; i < widgets->profile_width; i++ )
ff37db21 1749 gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
926c8140
RN
1750 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->distances[i])/(chunksd[widgets->cid]*LINES) );
1751
1752 // Show speed indicator
1753 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_dist_speed)) ) {
9b082b39 1754 GdkGC *dist_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
926c8140
RN
1755 GdkColor color;
1756 gdk_color_parse ( "red", &color );
1757 gdk_gc_set_rgb_fg_color ( dist_speed_gc, &color);
1758
1759 gdouble max_speed = 0;
1760 max_speed = widgets->max_speed * 110 / 100;
1761
1762 // This is just an indicator - no actual values can be inferred by user
1763 gint i;
1764 for ( i = 0; i < widgets->profile_width; i++ ) {
1765 int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
1766 gdk_draw_rectangle(GDK_DRAWABLE(pix), dist_speed_gc, TRUE, i+MARGIN-2, y_speed-2, 4, 4);
1767 }
1768 g_object_unref ( G_OBJECT(dist_speed_gc) );
1769 }
1770
1771 /* draw border */
ff37db21 1772 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
926c8140
RN
1773
1774 g_object_unref ( G_OBJECT(pix) );
1775
8de26632
RN
1776}
1777
1778/**
1779 * Draw just the elevation/time image
1780 */
1781static void draw_et ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets )
1782{
1783 GtkWidget *window;
1784 GdkPixmap *pix;
7169b863 1785 gdouble mina;
8de26632
RN
1786 guint i;
1787
1788 // Free previous allocation
1789 if ( widgets->ats )
1790 g_free ( widgets->ats );
1791
1792 widgets->ats = vik_track_make_elevation_time_map ( tr, widgets->profile_width );
1793
1794 if ( widgets->ats == NULL )
1795 return;
1796
1797 // Convert into appropriate units
1798 vik_units_height_t height_units = a_vik_get_units_height ();
1799 if ( height_units == VIK_UNITS_HEIGHT_FEET ) {
1800 // Convert altitudes into feet units
1801 for ( i = 0; i < widgets->profile_width; i++ ) {
1802 widgets->ats[i] = VIK_METERS_TO_FEET(widgets->ats[i]);
1803 }
1804 }
1805 // Otherwise leave in metres
1806
1807 minmax_array(widgets->ats, &widgets->min_altitude, &widgets->max_altitude, TRUE, widgets->profile_width);
1808
d2bae53c 1809 get_new_min_and_chunk_index_altitude (widgets->min_altitude, widgets->max_altitude, &widgets->draw_min_altitude_time, &widgets->ciat);
8de26632
RN
1810
1811 // Assign locally
d2bae53c 1812 mina = widgets->draw_min_altitude_time;
8de26632
RN
1813
1814 window = gtk_widget_get_toplevel (widgets->elev_time_box);
1815
9b082b39 1816 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
8de26632
RN
1817
1818 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1819
1820 /* clear the image */
ff37db21 1821 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->bg_gc[0],
8de26632 1822 TRUE, 0, 0, MARGIN, widgets->profile_height);
ff37db21 1823 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->mid_gc[0],
8de26632
RN
1824 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1825
1826 /* draw grid */
1827 for (i=0; i<=LINES; i++) {
8de26632
RN
1828 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1829 gchar s[32];
1830 int w, h;
1831
1832 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
ff37db21 1833 pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
56cb1807 1834
8de26632
RN
1835 switch (height_units) {
1836 case VIK_UNITS_HEIGHT_METRES:
d2bae53c 1837 sprintf(s, "%8dm", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
8de26632
RN
1838 break;
1839 case VIK_UNITS_HEIGHT_FEET:
1840 // NB values already converted into feet
d2bae53c 1841 sprintf(s, "%8dft", (int)(mina + (LINES-i)*chunksa[widgets->ciat]));
8de26632
RN
1842 break;
1843 default:
1844 sprintf(s, "--");
1845 g_critical("Houston, we've had a problem. height=%d", height_units);
1846 }
1847 pango_layout_set_text(pl, s, -1);
1848 pango_layout_get_pixel_size (pl, &w, &h);
ff37db21 1849 gdk_draw_layout(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0], MARGIN-w-3,
8de26632
RN
1850 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1851
ff37db21 1852 gdk_draw_line (GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
8de26632
RN
1853 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1854 g_object_unref ( G_OBJECT ( pl ) );
1855 pl = NULL;
1856 }
1857
1858 /* draw elevations */
1859 for ( i = 0; i < widgets->profile_width; i++ )
ff37db21 1860 gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
d2bae53c 1861 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->ats[i]-mina)/(chunksa[widgets->ciat]*LINES) );
8de26632
RN
1862
1863 // Show speed indicator
1864 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_elev_speed)) ) {
9b082b39 1865 GdkGC *elev_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
8de26632
RN
1866 GdkColor color;
1867 gdk_color_parse ( "red", &color );
1868 gdk_gc_set_rgb_fg_color ( elev_speed_gc, &color);
1869
1870 gdouble max_speed = 0;
1871 max_speed = widgets->max_speed * 110 / 100;
1872
1873 // This is just an indicator - no actual values can be inferred by user
1874 gint i;
1875 for ( i = 0; i < widgets->profile_width; i++ ) {
1876 int y_speed = widgets->profile_height - (widgets->profile_height * widgets->speeds[i])/max_speed;
1877 gdk_draw_rectangle(GDK_DRAWABLE(pix), elev_speed_gc, TRUE, i+MARGIN-2, y_speed-2, 4, 4);
1878 }
1879 g_object_unref ( G_OBJECT(elev_speed_gc) );
1880 }
1881
1882 /* draw border */
ff37db21 1883 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
8de26632
RN
1884
1885 g_object_unref ( G_OBJECT(pix) );
1886
950d8a07 1887}
7b624086
RN
1888
1889/**
1890 * Draw just the speed/distance image
1891 */
1892static void draw_sd ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
1893{
1894 GtkWidget *window;
1895 GdkPixmap *pix;
7169b863 1896 gdouble mins;
7b624086
RN
1897 guint i;
1898
1899 // Free previous allocation
1900 if ( widgets->speeds_dist )
1901 g_free ( widgets->speeds_dist );
1902
1903 widgets->speeds_dist = vik_track_make_speed_dist_map ( tr, widgets->profile_width );
1904 if ( widgets->speeds_dist == NULL )
1905 return;
1906
1907 // Convert into appropriate units
1908 vik_units_speed_t speed_units = a_vik_get_units_speed ();
1909 switch (speed_units) {
1910 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1911 for ( i = 0; i < widgets->profile_width; i++ ) {
1912 widgets->speeds_dist[i] = VIK_MPS_TO_KPH(widgets->speeds_dist[i]);
1913 }
1914 break;
1915 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1916 for ( i = 0; i < widgets->profile_width; i++ ) {
1917 widgets->speeds_dist[i] = VIK_MPS_TO_MPH(widgets->speeds_dist[i]);
1918 }
1919 break;
1920 case VIK_UNITS_SPEED_KNOTS:
1921 for ( i = 0; i < widgets->profile_width; i++ ) {
1922 widgets->speeds_dist[i] = VIK_MPS_TO_KNOTS(widgets->speeds_dist[i]);
1923 }
1924 break;
1925 default:
1926 // VIK_UNITS_SPEED_METRES_PER_SECOND:
1927 // No need to convert as already in m/s
1928 break;
1929 }
1930
1931 window = gtk_widget_get_toplevel (widgets->speed_dist_box);
1932
9b082b39 1933 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
7b624086
RN
1934
1935 gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
1936
1937 // OK to resuse min_speed here
1938 minmax_array(widgets->speeds_dist, &widgets->min_speed, &widgets->max_speed_dist, FALSE, widgets->profile_width);
1939 if (widgets->min_speed < 0.0)
1940 widgets->min_speed = 0; /* splines sometimes give negative speeds */
1941
1942 /* Find suitable chunk index */
1943 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);
1944
1945 // Assign locally
1946 mins = widgets->draw_min_speed;
7b624086
RN
1947
1948 /* clear the image */
ff37db21 1949 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->bg_gc[0],
7b624086 1950 TRUE, 0, 0, MARGIN, widgets->profile_height);
ff37db21 1951 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->mid_gc[0],
7b624086
RN
1952 TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
1953
1954 /* draw grid */
1955 for (i=0; i<=LINES; i++) {
7b624086
RN
1956 PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
1957 gchar s[32];
1958 int w, h;
1959
1960 pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
ff37db21 1961 pango_layout_set_font_description (pl, gtk_widget_get_style(window)->font_desc);
56cb1807 1962
7b624086
RN
1963 // NB: No need to convert here anymore as numbers are in the appropriate units
1964 switch (speed_units) {
1965 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
1966 sprintf(s, "%8dkm/h", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1967 break;
1968 case VIK_UNITS_SPEED_MILES_PER_HOUR:
1969 sprintf(s, "%8dmph", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1970 break;
1971 case VIK_UNITS_SPEED_METRES_PER_SECOND:
1972 sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1973 break;
1974 case VIK_UNITS_SPEED_KNOTS:
1975 sprintf(s, "%8dknots", (int)(mins + (LINES-i)*chunkss[widgets->cisd]));
1976 break;
1977 default:
1978 sprintf(s, "--");
1979 g_critical("Houston, we've had a problem. speed=%d", speed_units);
1980 }
1981
1982 pango_layout_set_text(pl, s, -1);
1983 pango_layout_get_pixel_size (pl, &w, &h);
ff37db21 1984 gdk_draw_layout(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->fg_gc[0], MARGIN-w-3,
7b624086
RN
1985 CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
1986
ff37db21 1987 gdk_draw_line (GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[0],
7b624086
RN
1988 MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
1989 g_object_unref ( G_OBJECT ( pl ) );
1990 pl = NULL;
1991 }
1992
1993
1994 /* draw speeds */
1995 for ( i = 0; i < widgets->profile_width; i++ )
ff37db21 1996 gdk_draw_line ( GDK_DRAWABLE(pix), gtk_widget_get_style(window)->dark_gc[3],
7b624086
RN
1997 i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds_dist[i]-mins)/(chunkss[widgets->cisd]*LINES) );
1998
1999
2000 if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->w_show_sd_gps_speed)) ) {
2001
9b082b39 2002 GdkGC *gps_speed_gc = gdk_gc_new ( gtk_widget_get_window(window) );
7b624086
RN
2003 GdkColor color;
2004 gdk_color_parse ( "red", &color );
2005 gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
2006
2007 gdouble dist = vik_track_get_length_including_gaps(tr);
2008 gdouble dist_tp = 0.0;
2009
2010 GList *iter = tr->trackpoints;
2011 for (iter = iter->next; iter; iter = iter->next) {
2012 gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
2013 if (isnan(gps_speed))
2014 continue;
2015 switch (speed_units) {
2016 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2017 gps_speed = VIK_MPS_TO_KPH(gps_speed);
2018 break;
2019 case VIK_UNITS_SPEED_MILES_PER_HOUR:
2020 gps_speed = VIK_MPS_TO_MPH(gps_speed);
2021 break;
2022 case VIK_UNITS_SPEED_KNOTS:
2023 gps_speed = VIK_MPS_TO_KNOTS(gps_speed);
2024 break;
2025 default:
2026 // VIK_UNITS_SPEED_METRES_PER_SECOND:
2027 // No need to convert as already in m/s
2028 break;
2029 }
2030 dist_tp += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
2031 int x = MARGIN + (widgets->profile_width * dist_tp / dist);
2032 int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(chunkss[widgets->cisd]*LINES);
2033 gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
2034 }
2035 g_object_unref ( G_OBJECT(gps_speed_gc) );
2036 }
2037
2038 /* draw border */
ff37db21 2039 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
7b624086
RN
2040
2041 g_object_unref ( G_OBJECT(pix) );
2042}
950d8a07
RN
2043#undef LINES
2044
950d8a07 2045/**
2f078f56 2046 * Draw all graphs
950d8a07 2047 */
2cf168ac 2048static void draw_all_graphs ( GtkWidget *widget, PropWidgets *widgets, gboolean resized )
950d8a07 2049{
950d8a07
RN
2050 // Draw graphs even if they are not visible
2051
2052 GList *child = NULL;
2f078f56
RN
2053 GtkWidget *image = NULL;
2054 GtkWidget *window = gtk_widget_get_toplevel(widget);
2055 gdouble pc = NAN;
50391e88 2056 gdouble pc_blob = NAN;
2f078f56 2057
950d8a07
RN
2058 // Draw elevations
2059 if (widgets->elev_box != NULL) {
2f078f56
RN
2060
2061 // Saved image no longer any good as we've resized, so we remove it here
2062 if (resized && widgets->elev_graph_saved_img.img) {
2063 g_object_unref(widgets->elev_graph_saved_img.img);
2064 widgets->elev_graph_saved_img.img = NULL;
2065 widgets->elev_graph_saved_img.saved = FALSE;
2066 }
2067
950d8a07 2068 child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box));
2cf168ac 2069 draw_elevations (GTK_WIDGET(child->data), widgets->tr, widgets );
2f078f56
RN
2070
2071 image = GTK_WIDGET(child->data);
950d8a07 2072 g_list_free(child);
2f078f56 2073
50391e88
RN
2074 // Ensure marker or blob are redrawn if necessary
2075 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2f078f56 2076
2cf168ac 2077 pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
50391e88
RN
2078 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2079 gint y_blob = 0;
2080 if (widgets->is_blob_drawn) {
2cf168ac 2081 pc_blob = tp_percentage_by_distance ( widgets->tr, widgets->blob_tp, widgets->track_length_inc_gaps );
50391e88
RN
2082 if (!isnan(pc_blob)) {
2083 x_blob = (pc_blob * widgets->profile_width);
2084 }
2085 y_blob = blobby_altitude (x_blob, widgets);
2086 }
2087
2088 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2f078f56 2089 if (!isnan(pc)) {
50391e88
RN
2090 marker_x = (pc * widgets->profile_width) + MARGIN;
2091 }
2092
2093 save_image_and_draw_graph_marks (image,
2f078f56 2094 marker_x,
ff37db21 2095 gtk_widget_get_style(window)->black_gc,
50391e88
RN
2096 x_blob+MARGIN,
2097 y_blob,
2f078f56
RN
2098 &widgets->elev_graph_saved_img,
2099 widgets->profile_width,
2100 widgets->profile_height,
50391e88
RN
2101 &widgets->is_marker_drawn,
2102 &widgets->is_blob_drawn);
2f078f56 2103 }
950d8a07
RN
2104 }
2105
0ba33e1d
FA
2106 // Draw gradients
2107 if (widgets->gradient_box != NULL) {
2108
2109 // Saved image no longer any good as we've resized, so we remove it here
2110 if (resized && widgets->gradient_graph_saved_img.img) {
2111 g_object_unref(widgets->gradient_graph_saved_img.img);
2112 widgets->gradient_graph_saved_img.img = NULL;
2113 widgets->gradient_graph_saved_img.saved = FALSE;
2114 }
2115
2116 child = gtk_container_get_children(GTK_CONTAINER(widgets->gradient_box));
2cf168ac 2117 draw_gradients (GTK_WIDGET(child->data), widgets->tr, widgets );
0ba33e1d
FA
2118
2119 image = GTK_WIDGET(child->data);
2120 g_list_free(child);
2121
2122 // Ensure marker or blob are redrawn if necessary
2123 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2124
2cf168ac 2125 pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
0ba33e1d
FA
2126 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2127 gint y_blob = 0;
2128 if (widgets->is_blob_drawn) {
2cf168ac 2129 pc_blob = tp_percentage_by_distance ( widgets->tr, widgets->blob_tp, widgets->track_length_inc_gaps );
0ba33e1d
FA
2130 if (!isnan(pc_blob)) {
2131 x_blob = (pc_blob * widgets->profile_width);
2132 }
2133 y_blob = blobby_gradient (x_blob, widgets);
2134 }
2135
2136 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2137 if (!isnan(pc)) {
2138 marker_x = (pc * widgets->profile_width) + MARGIN;
2139 }
2140
2141 save_image_and_draw_graph_marks (image,
2142 marker_x,
ff37db21 2143 gtk_widget_get_style(window)->black_gc,
0ba33e1d
FA
2144 x_blob+MARGIN,
2145 y_blob,
2146 &widgets->gradient_graph_saved_img,
2147 widgets->profile_width,
2148 widgets->profile_height,
2149 &widgets->is_marker_drawn,
2150 &widgets->is_blob_drawn);
2151 }
2152 }
2153
950d8a07
RN
2154 // Draw speeds
2155 if (widgets->speed_box != NULL) {
2f078f56
RN
2156
2157 // Saved image no longer any good as we've resized
2158 if (resized && widgets->speed_graph_saved_img.img) {
2159 g_object_unref(widgets->speed_graph_saved_img.img);
2160 widgets->speed_graph_saved_img.img = NULL;
2161 widgets->speed_graph_saved_img.saved = FALSE;
2162 }
2163
950d8a07 2164 child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box));
2cf168ac 2165 draw_vt (GTK_WIDGET(child->data), widgets->tr, widgets );
2f078f56
RN
2166
2167 image = GTK_WIDGET(child->data);
950d8a07 2168 g_list_free(child);
2f078f56 2169
50391e88
RN
2170 // Ensure marker or blob are redrawn if necessary
2171 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2f078f56 2172
2cf168ac 2173 pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
2f078f56 2174
50391e88
RN
2175 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2176 gint y_blob = 0;
2177 if (widgets->is_blob_drawn) {
2cf168ac 2178 pc_blob = tp_percentage_by_time ( widgets->tr, widgets->blob_tp );
50391e88
RN
2179 if (!isnan(pc_blob)) {
2180 x_blob = (pc_blob * widgets->profile_width);
2181 }
2182
2183 y_blob = blobby_speed (x_blob, widgets);
2184 }
2185
2186 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2f078f56 2187 if (!isnan(pc)) {
50391e88
RN
2188 marker_x = (pc * widgets->profile_width) + MARGIN;
2189 }
2190
2191 save_image_and_draw_graph_marks (image,
2f078f56 2192 marker_x,
ff37db21 2193 gtk_widget_get_style(window)->black_gc,
50391e88
RN
2194 x_blob+MARGIN,
2195 y_blob,
2f078f56
RN
2196 &widgets->speed_graph_saved_img,
2197 widgets->profile_width,
2198 widgets->profile_height,
50391e88
RN
2199 &widgets->is_marker_drawn,
2200 &widgets->is_blob_drawn);
2f078f56
RN
2201 }
2202 }
926c8140
RN
2203
2204 // Draw Distances
2205 if (widgets->dist_box != NULL) {
2206
2207 // Saved image no longer any good as we've resized
2208 if (resized && widgets->dist_graph_saved_img.img) {
2209 g_object_unref(widgets->dist_graph_saved_img.img);
2210 widgets->dist_graph_saved_img.img = NULL;
2211 widgets->dist_graph_saved_img.saved = FALSE;
2212 }
2213
2214 child = gtk_container_get_children(GTK_CONTAINER(widgets->dist_box));
2cf168ac 2215 draw_dt (GTK_WIDGET(child->data), widgets->tr, widgets );
926c8140
RN
2216
2217 image = GTK_WIDGET(child->data);
2218 g_list_free(child);
2219
2220 // Ensure marker or blob are redrawn if necessary
2221 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2222
2cf168ac 2223 pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
926c8140
RN
2224
2225 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2226 gint y_blob = 0;
2227 if (widgets->is_blob_drawn) {
2cf168ac 2228 pc_blob = tp_percentage_by_time ( widgets->tr, widgets->blob_tp );
926c8140
RN
2229 if (!isnan(pc_blob)) {
2230 x_blob = (pc_blob * widgets->profile_width);
2231 }
2232
2233 y_blob = blobby_distance (x_blob, widgets);
2234 }
2235
2236 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2237 if (!isnan(pc)) {
2238 marker_x = (pc * widgets->profile_width) + MARGIN;
2239 }
2240
2241 save_image_and_draw_graph_marks (image,
2242 marker_x,
ff37db21 2243 gtk_widget_get_style(window)->black_gc,
926c8140
RN
2244 x_blob+MARGIN,
2245 y_blob,
2246 &widgets->dist_graph_saved_img,
2247 widgets->profile_width,
2248 widgets->profile_height,
2249 &widgets->is_marker_drawn,
2250 &widgets->is_blob_drawn);
2251 }
2252 }
2253
8de26632
RN
2254 // Draw Elevations in timely manner
2255 if (widgets->elev_time_box != NULL) {
2256
2257 // Saved image no longer any good as we've resized
2258 if (resized && widgets->elev_time_graph_saved_img.img) {
2259 g_object_unref(widgets->elev_time_graph_saved_img.img);
2260 widgets->elev_time_graph_saved_img.img = NULL;
2261 widgets->elev_time_graph_saved_img.saved = FALSE;
2262 }
2263
2264 child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_time_box));
2cf168ac 2265 draw_et (GTK_WIDGET(child->data), widgets->tr, widgets );
8de26632
RN
2266
2267 image = GTK_WIDGET(child->data);
2268 g_list_free(child);
2269
2270 // Ensure marker or blob are redrawn if necessary
2271 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2272
2cf168ac 2273 pc = tp_percentage_by_time ( widgets->tr, widgets->marker_tp );
8de26632
RN
2274
2275 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2276 gint y_blob = 0;
2277 if (widgets->is_blob_drawn) {
2cf168ac 2278 pc_blob = tp_percentage_by_time ( widgets->tr, widgets->blob_tp );
8de26632
RN
2279 if (!isnan(pc_blob)) {
2280 x_blob = (pc_blob * widgets->profile_width);
2281 }
2282 y_blob = blobby_altitude_time (x_blob, widgets);
2283 }
2284
2285 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2286 if (!isnan(pc)) {
2287 marker_x = (pc * widgets->profile_width) + MARGIN;
2288 }
2289
2290 save_image_and_draw_graph_marks (image,
2291 marker_x,
ff37db21 2292 gtk_widget_get_style(window)->black_gc,
8de26632
RN
2293 x_blob+MARGIN,
2294 y_blob,
2295 &widgets->elev_time_graph_saved_img,
2296 widgets->profile_width,
2297 widgets->profile_height,
2298 &widgets->is_marker_drawn,
2299 &widgets->is_blob_drawn);
2300 }
2301 }
2302
7b624086
RN
2303 // Draw speed distances
2304 if (widgets->speed_dist_box != NULL) {
2305
2306 // Saved image no longer any good as we've resized, so we remove it here
2307 if (resized && widgets->speed_dist_graph_saved_img.img) {
2308 g_object_unref(widgets->speed_dist_graph_saved_img.img);
2309 widgets->speed_dist_graph_saved_img.img = NULL;
2310 widgets->speed_dist_graph_saved_img.saved = FALSE;
2311 }
2312
2313 child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_dist_box));
2cf168ac 2314 draw_sd (GTK_WIDGET(child->data), widgets->tr, widgets );
7b624086
RN
2315
2316 image = GTK_WIDGET(child->data);
2317 g_list_free(child);
2318
2319 // Ensure marker or blob are redrawn if necessary
2320 if (widgets->is_marker_drawn || widgets->is_blob_drawn) {
2321
2cf168ac 2322 pc = tp_percentage_by_distance ( widgets->tr, widgets->marker_tp, widgets->track_length_inc_gaps );
7b624086
RN
2323 gdouble x_blob = -MARGIN - 1.0; // i.e. Don't draw unless we get a valid value
2324 gint y_blob = 0;
2325 if (widgets->is_blob_drawn) {
2cf168ac 2326 pc_blob = tp_percentage_by_distance ( widgets->tr, widgets->blob_tp, widgets->track_length_inc_gaps );
7b624086
RN
2327 if (!isnan(pc_blob)) {
2328 x_blob = (pc_blob * widgets->profile_width);
2329 }
2330 y_blob = blobby_speed_dist (x_blob, widgets);
2331 }
2332
2333 gdouble marker_x = -1.0; // i.e. Don't draw unless we get a valid value
2334 if (!isnan(pc)) {
2335 marker_x = (pc * widgets->profile_width) + MARGIN;
2336 }
2337
2338 save_image_and_draw_graph_marks (image,
2339 marker_x,
ff37db21 2340 gtk_widget_get_style(window)->black_gc,
7b624086
RN
2341 x_blob+MARGIN,
2342 y_blob,
2343 &widgets->speed_dist_graph_saved_img,
2344 widgets->profile_width,
2345 widgets->profile_height,
2346 &widgets->is_marker_drawn,
2347 &widgets->is_blob_drawn);
2348 }
2349 }
2350
2f078f56
RN
2351}
2352
2353/**
2354 * Configure/Resize the profile & speed/time images
2355 */
2cf168ac 2356static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, PropWidgets *widgets )
2f078f56 2357{
2f078f56
RN
2358 if (widgets->configure_dialog) {
2359 // Determine size offsets between dialog size and size for images
2360 // Only on the initialisation of the dialog
2361 widgets->profile_width_offset = event->width - widgets->profile_width;
2362 widgets->profile_height_offset = event->height - widgets->profile_height;
2363 widgets->configure_dialog = FALSE;
2364
2365 // Without this the settting, the dialog will only grow in vertical size - one can not then make it smaller!
d45ac152 2366 gtk_widget_set_size_request ( widget, widgets->profile_width+widgets->profile_width_offset, widgets->profile_height+widgets->profile_height_offset );
972fc895
RN
2367
2368 // Allow resizing back down to a minimal size (especially useful if the initial size has been made bigger after restoring from the saved settings)
2369 GdkGeometry geom = { 600+widgets->profile_width_offset, 300+widgets->profile_height_offset, 0, 0, 0, 0, 0, 0, 0, 0, GDK_GRAVITY_STATIC };
2370 gdk_window_set_geometry_hints ( gtk_widget_get_window(widget), &geom, GDK_HINT_MIN_SIZE );
950d8a07 2371 }
2f078f56
RN
2372 else {
2373 widgets->profile_width_old = widgets->profile_width;
2374 widgets->profile_height_old = widgets->profile_height;
2375 }
2376
2377 // Now adjust From Dialog size to get image size
2378 widgets->profile_width = event->width - widgets->profile_width_offset;
2379 widgets->profile_height = event->height - widgets->profile_height_offset;
2380
2381 // ATM we receive configure_events when the dialog is moved and so no further action is necessary
2382 if ( !widgets->configure_dialog &&
2383 (widgets->profile_width_old == widgets->profile_width) && (widgets->profile_height_old == widgets->profile_height) )
2384 return FALSE;
2385
2386 // Draw stuff
2cf168ac 2387 draw_all_graphs ( widget, widgets, TRUE );
2f078f56 2388
950d8a07
RN
2389 return FALSE;
2390}
2391
2392/**
2393 * Create height profile widgets including the image and callbacks
2394 */
2cf168ac 2395GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
950d8a07
RN
2396{
2397 GdkPixmap *pix;
2398 GtkWidget *image;
950d8a07 2399 GtkWidget *eventbox;
950d8a07 2400
0b2bfa08 2401 // First allocation
2cf168ac 2402 widgets->altitudes = vik_track_make_elevation_map ( widgets->tr, widgets->profile_width );
0b2bfa08
RN
2403
2404 if ( widgets->altitudes == NULL ) {
950d8a07
RN
2405 *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
2406 return NULL;
2407 }
2408
b66b0b38 2409 minmax_array(widgets->altitudes, min_alt, max_alt, TRUE, widgets->profile_width);
950d8a07 2410
9b082b39 2411 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
950d8a07
RN
2412 image = gtk_image_new_from_pixmap ( pix, NULL );
2413
950d8a07
RN
2414 g_object_unref ( G_OBJECT(pix) );
2415
950d8a07 2416 eventbox = gtk_event_box_new ();
2cf168ac
RN
2417 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), widgets );
2418 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), widgets );
950d8a07
RN
2419 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2420 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
2421
2422 return eventbox;
2423}
2424
0ba33e1d
FA
2425/**
2426 * Create height profile widgets including the image and callbacks
2427 */
2cf168ac 2428GtkWidget *vik_trw_layer_create_gradient ( GtkWidget *window, PropWidgets *widgets)
0ba33e1d
FA
2429{
2430 GdkPixmap *pix;
2431 GtkWidget *image;
2432 GtkWidget *eventbox;
0ba33e1d
FA
2433
2434 // First allocation
2cf168ac 2435 widgets->gradients = vik_track_make_gradient_map ( widgets->tr, widgets->profile_width );
0ba33e1d
FA
2436
2437 if ( widgets->gradients == NULL ) {
2438 return NULL;
2439 }
2440
9b082b39 2441 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
0ba33e1d
FA
2442 image = gtk_image_new_from_pixmap ( pix, NULL );
2443
2444 g_object_unref ( G_OBJECT(pix) );
2445
0ba33e1d 2446 eventbox = gtk_event_box_new ();
2cf168ac
RN
2447 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_gradient_click), widgets );
2448 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_gradient_move), widgets );
0ba33e1d
FA
2449 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2450 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
2451
2452 return eventbox;
2453}
2454
950d8a07
RN
2455/**
2456 * Create speed/time widgets including the image and callbacks
2457 */
2cf168ac 2458GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, PropWidgets *widgets)
950d8a07
RN
2459{
2460 GdkPixmap *pix;
2461 GtkWidget *image;
2462 GtkWidget *eventbox;
950d8a07 2463
0b2bfa08 2464 // First allocation
2cf168ac 2465 widgets->speeds = vik_track_make_speed_map ( widgets->tr, widgets->profile_width );
0b2bfa08 2466 if ( widgets->speeds == NULL )
950d8a07
RN
2467 return NULL;
2468
9b082b39 2469 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
950d8a07
RN
2470 image = gtk_image_new_from_pixmap ( pix, NULL );
2471
2472#if 0
2473 /* XXX this can go out, it's just a helpful dev tool */
2474 {
2475 int j;
ff37db21
RN
2476 GdkGC **colors[8] = { gtk_widget_get_style(window)->bg_gc,
2477 gtk_widget_get_style(window)->fg_gc,
2478 gtk_widget_get_style(window)->light_gc,
2479 gtk_widget_get_style(window)->dark_gc,
2480 gtk_widget_get_style(window)->mid_gc,
2481 gtk_widget_get_style(window)->text_gc,
2482 gtk_widget_get_style(window)->base_gc,
2483 gtk_widget_get_style(window)->text_aa_gc };
950d8a07
RN
2484 for (i=0; i<5; i++) {
2485 for (j=0; j<8; j++) {
2486 gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
2487 TRUE, i*20, j*20, 20, 20);
ff37db21 2488 gdk_draw_rectangle(GDK_DRAWABLE(pix), gtk_widget_get_style(window)->black_gc,
950d8a07
RN
2489 FALSE, i*20, j*20, 20, 20);
2490 }
2491 }
2492 }
2493#endif
2494
950d8a07
RN
2495 g_object_unref ( G_OBJECT(pix) );
2496
561e6ad0 2497 eventbox = gtk_event_box_new ();
2cf168ac
RN
2498 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), widgets );
2499 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), widgets );
561e6ad0 2500 gtk_container_add ( GTK_CONTAINER(eventbox), image );
950d8a07 2501 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
561e6ad0
EB
2502
2503 return eventbox;
25e44eac 2504}
926c8140
RN
2505
2506/**
2507 * Create distance / time widgets including the image and callbacks
2508 */
2cf168ac 2509GtkWidget *vik_trw_layer_create_dtdiag ( GtkWidget *window, PropWidgets *widgets)
926c8140
RN
2510{
2511 GdkPixmap *pix;
2512 GtkWidget *image;
2513 GtkWidget *eventbox;
926c8140
RN
2514
2515 // First allocation
2cf168ac 2516 widgets->distances = vik_track_make_distance_map ( widgets->tr, widgets->profile_width );
926c8140
RN
2517 if ( widgets->distances == NULL )
2518 return NULL;
2519
9b082b39 2520 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
926c8140
RN
2521 image = gtk_image_new_from_pixmap ( pix, NULL );
2522
2523 g_object_unref ( G_OBJECT(pix) );
2524
2525 eventbox = gtk_event_box_new ();
2cf168ac
RN
2526 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_dt_click), widgets );
2527 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_dt_move), widgets );
2528 //g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), widgets );
926c8140
RN
2529 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2530 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2531
2532 return eventbox;
2533}
8de26632
RN
2534
2535/**
2536 * Create elevation / time widgets including the image and callbacks
2537 */
2cf168ac 2538GtkWidget *vik_trw_layer_create_etdiag ( GtkWidget *window, PropWidgets *widgets)
8de26632
RN
2539{
2540 GdkPixmap *pix;
2541 GtkWidget *image;
2542 GtkWidget *eventbox;
8de26632
RN
2543
2544 // First allocation
2cf168ac 2545 widgets->ats = vik_track_make_elevation_time_map ( widgets->tr, widgets->profile_width );
8de26632
RN
2546 if ( widgets->ats == NULL )
2547 return NULL;
2548
9b082b39 2549 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
8de26632
RN
2550 image = gtk_image_new_from_pixmap ( pix, NULL );
2551
2552 g_object_unref ( G_OBJECT(pix) );
2553
2554 eventbox = gtk_event_box_new ();
2cf168ac
RN
2555 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_et_click), widgets );
2556 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_et_move), widgets );
8de26632
RN
2557 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2558 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2559
2560 return eventbox;
2561}
7b624086
RN
2562
2563/**
2564 * Create speed/distance widgets including the image and callbacks
2565 */
2cf168ac 2566GtkWidget *vik_trw_layer_create_sddiag ( GtkWidget *window, PropWidgets *widgets)
7b624086
RN
2567{
2568 GdkPixmap *pix;
2569 GtkWidget *image;
2570 GtkWidget *eventbox;
7b624086
RN
2571
2572 // First allocation
2cf168ac 2573 widgets->speeds_dist = vik_track_make_speed_dist_map ( widgets->tr, widgets->profile_width );
7b624086
RN
2574 if ( widgets->speeds_dist == NULL )
2575 return NULL;
2576
9b082b39 2577 pix = gdk_pixmap_new( gtk_widget_get_window(window), widgets->profile_width + MARGIN, widgets->profile_height, -1 );
7b624086
RN
2578 image = gtk_image_new_from_pixmap ( pix, NULL );
2579
2580 g_object_unref ( G_OBJECT(pix) );
2581
2582 eventbox = gtk_event_box_new ();
2cf168ac
RN
2583 g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_sd_click), widgets );
2584 g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_sd_move), widgets );
7b624086
RN
2585 gtk_container_add ( GTK_CONTAINER(eventbox), image );
2586 gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
2587
2588 return eventbox;
2589}
25e44eac 2590#undef MARGIN
50a14534 2591
972fc895
RN
2592#define VIK_SETTINGS_TRACK_PROFILE_WIDTH "track_profile_display_width"
2593#define VIK_SETTINGS_TRACK_PROFILE_HEIGHT "track_profile_display_height"
2594
2595static void save_values ( PropWidgets *widgets )
2596{
2597 a_settings_set_integer ( VIK_SETTINGS_TRACK_PROFILE_WIDTH, widgets->profile_width );
2598 a_settings_set_integer ( VIK_SETTINGS_TRACK_PROFILE_HEIGHT, widgets->profile_height );
2599}
2600
2cf168ac
RN
2601static void destroy_cb ( GtkDialog *dialog, PropWidgets *widgets )
2602{
972fc895 2603 save_values(widgets);
2cf168ac
RN
2604 prop_widgets_free(widgets);
2605}
2606
2607static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets )
50a14534 2608{
21700912
QT
2609 VikTrack *tr = widgets->tr;
2610 VikTrwLayer *vtl = widgets->vtl;
8dcb3ba4 2611 gboolean keep_dialog = FALSE;
21700912
QT
2612
2613 /* FIXME: check and make sure the track still exists before doing anything to it */
2614 /* Note: destroying diaglog (eg, parent window exit) won't give "response" */
2615 switch (resp) {
2616 case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
2617 case GTK_RESPONSE_REJECT:
2618 break;
2619 case GTK_RESPONSE_ACCEPT:
2620 vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
6b2f262e 2621 vik_track_set_description(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_description)));
b1453c16
RN
2622 gtk_color_button_get_color ( GTK_COLOR_BUTTON(widgets->w_color), &(tr->color) );
2623 trw_layer_update_treeview ( widgets->vtl, widgets->tr, widgets->trk_id );
da121f9b 2624 vik_layer_emit_update ( VIK_LAYER(vtl) );
21700912
QT
2625 break;
2626 case VIK_TRW_LAYER_PROPWIN_REVERSE:
2627 vik_track_reverse(tr);
da121f9b 2628 vik_layer_emit_update ( VIK_LAYER(vtl) );
21700912
QT
2629 break;
2630 case VIK_TRW_LAYER_PROPWIN_DEL_DUP:
4f48c541
RN
2631 vik_track_remove_dup_points(tr); // NB ignore the returned answer
2632 // As we could have seen the nuber of dulplicates that would be deleted in the properties statistics tab,
2633 // choose not to inform the user unnecessarily
2634
21700912 2635 /* above operation could have deleted current_tp or last_tp */
ce4bd1cf 2636 trw_layer_cancel_tps_of_track ( vtl, tr );
da121f9b 2637 vik_layer_emit_update ( VIK_LAYER(vtl) );
21700912
QT
2638 break;
2639 case VIK_TRW_LAYER_PROPWIN_SPLIT:
2640 {
9914238e 2641 /* get new tracks, add them and then the delete old one. old can still exist on clipboard. */
21700912 2642 guint ntracks;
ce4bd1cf 2643
21700912
QT
2644 VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
2645 gchar *new_tr_name;
2646 guint i;
2647 for ( i = 0; i < ntracks; i++ )
2648 {
9914238e 2649 if ( tracks[i] ) {
0d2b891f
RN
2650 new_tr_name = trw_layer_new_unique_sublayer_name ( vtl,
2651 widgets->tr->is_route ? VIK_TRW_LAYER_SUBLAYER_ROUTE : VIK_TRW_LAYER_SUBLAYER_TRACK,
2652 widgets->tr->name);
2653 if ( widgets->tr->is_route )
2654 vik_trw_layer_add_route ( vtl, new_tr_name, tracks[i] );
2655 else
2656 vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
9914238e 2657 }
21700912
QT
2658 }
2659 if ( tracks )
2660 {
2661 g_free ( tracks );
2662 /* Don't let track destroy this dialog */
2663 vik_track_clear_property_dialog(tr);
0d2b891f
RN
2664 if ( widgets->tr->is_route )
2665 vik_trw_layer_delete_route ( vtl, tr );
2666 else
2667 vik_trw_layer_delete_track ( vtl, tr );
da121f9b 2668 vik_layer_emit_update ( VIK_LAYER(vtl) ); /* chase thru the hoops */
21700912
QT
2669 }
2670 }
2671 break;
8dcb3ba4
QT
2672 case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER:
2673 {
2674 GList *iter = tr->trackpoints;
2675 while ((iter = iter->next)) {
2676 if (widgets->marker_tp == VIK_TRACKPOINT(iter->data))
2677 break;
2678 }
2679 if (iter == NULL) {
2680 a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR,
2681 _("Failed spliting track. Track unchanged"), NULL);
2682 keep_dialog = TRUE;
2683 break;
2684 }
2685
0d2b891f
RN
2686 gchar *r_name = trw_layer_new_unique_sublayer_name(vtl,
2687 widgets->tr->is_route ? VIK_TRW_LAYER_SUBLAYER_ROUTE : VIK_TRW_LAYER_SUBLAYER_TRACK,
2688 widgets->tr->name);
8dcb3ba4
QT
2689 iter->prev->next = NULL;
2690 iter->prev = NULL;
2691 VikTrack *tr_right = vik_track_new();
2692 if ( tr->comment )
2693 vik_track_set_comment ( tr_right, tr->comment );
2694 tr_right->visible = tr->visible;
0d2b891f 2695 tr_right->is_route = tr->is_route;
8dcb3ba4
QT
2696 tr_right->trackpoints = iter;
2697
0d2b891f
RN
2698 if ( widgets->tr->is_route )
2699 vik_trw_layer_add_route(vtl, r_name, tr_right);
2700 else
2701 vik_trw_layer_add_track(vtl, r_name, tr_right);
da121f9b 2702 vik_layer_emit_update ( VIK_LAYER(vtl) );
8dcb3ba4
QT
2703 }
2704 break;
21700912
QT
2705 default:
2706 fprintf(stderr, "DEBUG: unknown response\n");
2707 return;
2708 }
2709
2710 /* Keep same behaviour for now: destroy dialog if click on any button */
8dcb3ba4 2711 if (!keep_dialog) {
8dcb3ba4
QT
2712 vik_track_clear_property_dialog(tr);
2713 gtk_widget_destroy ( GTK_WIDGET(dialog) );
2714 }
21700912
QT
2715}
2716
ec32b567
RN
2717/**
2718 * Force a redraw when checkbutton has been toggled to show/hide that information
2719 */
2cf168ac 2720static void checkbutton_toggle_cb ( GtkToggleButton *togglebutton, PropWidgets *widgets, gpointer dummy )
ec32b567 2721{
ec32b567
RN
2722 // Even though not resized, we'll pretend it is -
2723 // as this invalidates the saved images (since the image may have changed)
2cf168ac 2724 draw_all_graphs ( widgets->dialog, widgets, TRUE );
ec32b567
RN
2725}
2726
780d3771
RN
2727/**
2728 * Create the widgets for the given graph tab
2729 */
065f60f1
GB
2730static GtkWidget *create_graph_page ( GtkWidget *graph,
2731 const gchar *markup,
780d3771
RN
2732 GtkWidget *value,
2733 const gchar *markup2,
ec32b567
RN
2734 GtkWidget *value2,
2735 GtkWidget *checkbutton1,
2736 gboolean checkbutton1_default,
2737 GtkWidget *checkbutton2,
2738 gboolean checkbutton2_default )
065f60f1
GB
2739{
2740 GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 );
2741 GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 );
2742 GtkWidget *label = gtk_label_new (NULL);
780d3771 2743 GtkWidget *label2 = gtk_label_new (NULL);
065f60f1
GB
2744 gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0);
2745 gtk_label_set_markup ( GTK_LABEL(label), markup );
780d3771 2746 gtk_label_set_markup ( GTK_LABEL(label2), markup2 );
065f60f1
GB
2747 gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
2748 gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0);
780d3771
RN
2749 gtk_box_pack_start (GTK_BOX(hbox), label2, FALSE, FALSE, 0);
2750 gtk_box_pack_start (GTK_BOX(hbox), value2, FALSE, FALSE, 0);
ec32b567
RN
2751 if (checkbutton2) {
2752 gtk_box_pack_end (GTK_BOX(hbox), checkbutton2, FALSE, FALSE, 0);
2753 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton2), checkbutton2_default);
2754 }
2755 if (checkbutton1) {
2756 gtk_box_pack_end (GTK_BOX(hbox), checkbutton1, FALSE, FALSE, 0);
2757 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(checkbutton1), checkbutton1_default);
2758 }
065f60f1 2759 gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
ec32b567 2760
065f60f1
GB
2761 return vbox;
2762}
2763
b1453c16
RN
2764void vik_trw_layer_propwin_run ( GtkWindow *parent,
2765 VikTrwLayer *vtl,
2766 VikTrack *tr,
2767 gpointer vlp,
2768 VikViewport *vvp,
2769 gpointer *trk_id )
21700912 2770{
ca7e67ef 2771 PropWidgets *widgets = prop_widgets_new();
21700912 2772 widgets->vtl = vtl;
2cf168ac
RN
2773 widgets->vvp = vvp;
2774 widgets->vlp = vlp;
21700912 2775 widgets->tr = tr;
b1453c16 2776 widgets->trk_id = trk_id;
972fc895
RN
2777
2778 gint profile_size_value;
2779 // Ensure minimum values
2780 widgets->profile_width = 600;
2781 if ( a_settings_get_integer ( VIK_SETTINGS_TRACK_PROFILE_WIDTH, &profile_size_value ) )
2782 if ( profile_size_value > widgets->profile_width )
2783 widgets->profile_width = profile_size_value;
2784
2785 widgets->profile_height = 300;
2786 if ( a_settings_get_integer ( VIK_SETTINGS_TRACK_PROFILE_HEIGHT, &profile_size_value ) )
2787 if ( profile_size_value > widgets->profile_height )
2788 widgets->profile_height = profile_size_value;
2789
ce4bd1cf 2790 gchar *title = g_strdup_printf(_("%s - Track Properties"), tr->name);
dc27aba1 2791 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
21700912
QT
2792 parent,
2793 GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
2794 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
c51f24b1
RN
2795 _("Split at _Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER,
2796 _("Split _Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT,
2797 _("_Reverse"), VIK_TRW_LAYER_PROPWIN_REVERSE,
2798 _("_Delete Dupl."), VIK_TRW_LAYER_PROPWIN_DEL_DUP,
21700912
QT
2799 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2800 NULL);
8dcb3ba4 2801 widgets->dialog = dialog;
2cf168ac
RN
2802 g_signal_connect( G_OBJECT(dialog), "response", G_CALLBACK(propwin_response_cb), widgets);
2803
dc27aba1 2804 g_free(title);
4b00e581 2805 GtkTable *table;
50a14534 2806 gdouble tr_len;
901fd9e2
RN
2807 gulong tp_count;
2808 guint seg_count;
50a14534
EB
2809
2810 gdouble min_alt, max_alt;
2cf168ac
RN
2811 widgets->elev_box = vik_trw_layer_create_profile(GTK_WIDGET(parent), widgets, &min_alt, &max_alt);
2812 widgets->gradient_box = vik_trw_layer_create_gradient(GTK_WIDGET(parent), widgets);
2813 widgets->speed_box = vik_trw_layer_create_vtdiag(GTK_WIDGET(parent), widgets);
2814 widgets->dist_box = vik_trw_layer_create_dtdiag(GTK_WIDGET(parent), widgets);
2815 widgets->elev_time_box = vik_trw_layer_create_etdiag(GTK_WIDGET(parent), widgets);
2816 widgets->speed_dist_box = vik_trw_layer_create_sddiag(GTK_WIDGET(parent), widgets);
25e44eac 2817 GtkWidget *graphs = gtk_notebook_new();
50a14534 2818
4b00e581
AF
2819 GtkWidget *content[20];
2820 int cnt;
2821 int i;
2822
6b2f262e
RN
2823 static gchar *label_texts[] = {
2824 N_("<b>Comment:</b>"),
2825 N_("<b>Description:</b>"),
2826 N_("<b>Track Length:</b>"),
2827 N_("<b>Trackpoints:</b>"),
2828 N_("<b>Segments:</b>"),
2829 N_("<b>Duplicate Points:</b>"),
2830 N_("<b>Max Speed:</b>"),
2831 N_("<b>Avg. Speed:</b>"),
2832 N_("<b>Moving Avg. Speed:</b>"),
2833 N_("<b>Avg. Dist. Between TPs:</b>"),
2834 N_("<b>Elevation Range:</b>"),
2835 N_("<b>Total Elevation Gain/Loss:</b>"),
2836 N_("<b>Start:</b>"),
2837 N_("<b>End:</b>"),
b1453c16
RN
2838 N_("<b>Duration:</b>"),
2839 N_("<b>Color:</b>") };
32e48121 2840 static gchar tmp_buf[50];
8c4f1350 2841 gdouble tmp_speed;
50a14534 2842
4b00e581 2843 cnt = 0;
21700912 2844 widgets->w_comment = gtk_entry_new ();
50a14534 2845 if ( tr->comment )
21700912
QT
2846 gtk_entry_set_text ( GTK_ENTRY(widgets->w_comment), tr->comment );
2847 g_signal_connect_swapped ( widgets->w_comment, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
2848 content[cnt++] = widgets->w_comment;
50a14534 2849
6b2f262e
RN
2850 widgets->w_description = gtk_entry_new ();
2851 if ( tr->description )
2852 gtk_entry_set_text ( GTK_ENTRY(widgets->w_description), tr->description );
2853 g_signal_connect_swapped ( widgets->w_description, "activate", G_CALLBACK(a_dialog_response_accept), GTK_DIALOG(dialog) );
2854 content[cnt++] = widgets->w_description;
2855
6f9336aa
RN
2856 vik_units_distance_t dist_units = a_vik_get_units_distance ();
2857
f9744e8f
RN
2858 // NB This value not shown yet - but is used by internal calculations
2859 widgets->track_length_inc_gaps = vik_track_get_length_including_gaps(tr);
2860
e60fc2c9 2861 tr_len = widgets->track_length = vik_track_get_length(tr);
6f9336aa
RN
2862 switch (dist_units) {
2863 case VIK_UNITS_DISTANCE_KILOMETRES:
2864 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km", tr_len/1000.0 );
2865 break;
2866 case VIK_UNITS_DISTANCE_MILES:
433b3f7f 2867 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f miles", VIK_METERS_TO_MILES(tr_len) );
6f9336aa
RN
2868 break;
2869 default:
2870 g_critical("Houston, we've had a problem. distance=%d", dist_units);
2871 }
21700912 2872 widgets->w_track_length = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534
EB
2873
2874 tp_count = vik_track_get_tp_count(tr);
901fd9e2 2875 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", tp_count );
21700912 2876 widgets->w_tp_count = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534
EB
2877
2878 seg_count = vik_track_get_segment_count(tr) ;
2879 g_snprintf(tmp_buf, sizeof(tmp_buf), "%u", seg_count );
21700912 2880 widgets->w_segment_count = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534
EB
2881
2882 g_snprintf(tmp_buf, sizeof(tmp_buf), "%lu", vik_track_get_dup_point_count(tr) );
21700912 2883 widgets->w_duptp_count = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534 2884
13bdea80 2885 vik_units_speed_t speed_units = a_vik_get_units_speed ();
8c4f1350
EB
2886 tmp_speed = vik_track_get_max_speed(tr);
2887 if ( tmp_speed == 0 )
4c77d5e0 2888 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
13bdea80
RN
2889 else {
2890 switch (speed_units) {
2891 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
ab1e0693 2892 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
13bdea80
RN
2893 break;
2894 case VIK_UNITS_SPEED_MILES_PER_HOUR:
ab1e0693 2895 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
13bdea80
RN
2896 break;
2897 case VIK_UNITS_SPEED_METRES_PER_SECOND:
2898 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2899 break;
a4c92313 2900 case VIK_UNITS_SPEED_KNOTS:
ab1e0693 2901 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
a4c92313 2902 break;
13bdea80
RN
2903 default:
2904 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2905 g_critical("Houston, we've had a problem. speed=%d", speed_units);
2906 }
2907 }
21700912 2908 widgets->w_max_speed = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534 2909
8c4f1350
EB
2910 tmp_speed = vik_track_get_average_speed(tr);
2911 if ( tmp_speed == 0 )
4c77d5e0 2912 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
13bdea80
RN
2913 else {
2914 switch (speed_units) {
2915 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
ab1e0693 2916 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
13bdea80
RN
2917 break;
2918 case VIK_UNITS_SPEED_MILES_PER_HOUR:
ab1e0693 2919 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
13bdea80
RN
2920 break;
2921 case VIK_UNITS_SPEED_METRES_PER_SECOND:
2922 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2923 break;
a4c92313 2924 case VIK_UNITS_SPEED_KNOTS:
ab1e0693 2925 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
a4c92313 2926 break;
13bdea80
RN
2927 default:
2928 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2929 g_critical("Houston, we've had a problem. speed=%d", speed_units);
2930 }
2931 }
21700912 2932 widgets->w_avg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534 2933
a68ddd9c
RN
2934 // Use 60sec as the default period to be considered stopped
2935 // this is the TrackWaypoint draw stops default value 'vtl->stop_length'
2936 // however this variable is not directly accessible - and I don't expect it's often changed from the default
2937 // so ATM just put in the number
2938 tmp_speed = vik_track_get_average_speed_moving(tr, 60);
2939 if ( tmp_speed == 0 )
2940 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
2941 else {
2942 switch (speed_units) {
2943 case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
2944 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f km/h", VIK_MPS_TO_KPH(tmp_speed));
2945 break;
2946 case VIK_UNITS_SPEED_MILES_PER_HOUR:
2947 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f mph", VIK_MPS_TO_MPH(tmp_speed));
2948 break;
2949 case VIK_UNITS_SPEED_METRES_PER_SECOND:
2950 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m/s", tmp_speed );
2951 break;
2952 case VIK_UNITS_SPEED_KNOTS:
2953 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f knots", VIK_MPS_TO_KNOTS(tmp_speed));
2954 break;
2955 default:
2956 g_snprintf (tmp_buf, sizeof(tmp_buf), "--" );
2957 g_critical("Houston, we've had a problem. speed=%d", speed_units);
2958 }
2959 }
2960 widgets->w_mvg_speed = content[cnt++] = gtk_label_new ( tmp_buf );
2961
6f9336aa
RN
2962 switch (dist_units) {
2963 case VIK_UNITS_DISTANCE_KILOMETRES:
2964 // Even though kilometres, the average distance between points is going to be quite small so keep in metres
2965 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.2f m", (tp_count - seg_count) == 0 ? 0 : tr_len / ( tp_count - seg_count ) );
2966 break;
2967 case VIK_UNITS_DISTANCE_MILES:
433b3f7f 2968 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 )) );
6f9336aa
RN
2969 break;
2970 default:
2971 g_critical("Houston, we've had a problem. distance=%d", dist_units);
2972 }
21700912 2973 widgets->w_avg_dist = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534 2974
6027a9c5 2975 vik_units_height_t height_units = a_vik_get_units_height ();
8c4f1350 2976 if ( min_alt == VIK_DEFAULT_ALTITUDE )
4c77d5e0 2977 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
6027a9c5
RN
2978 else {
2979 switch (height_units) {
2980 case VIK_UNITS_HEIGHT_METRES:
2981 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m - %.0f m", min_alt, max_alt );
2982 break;
2983 case VIK_UNITS_HEIGHT_FEET:
6c20e59a 2984 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet - %.0f feet", VIK_METERS_TO_FEET(min_alt), VIK_METERS_TO_FEET(max_alt) );
6027a9c5
RN
2985 break;
2986 default:
2987 g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
2988 g_critical("Houston, we've had a problem. height=%d", height_units);
2989 }
2990 }
21700912 2991 widgets->w_elev_range = content[cnt++] = gtk_label_new ( tmp_buf );
50a14534
EB
2992
2993 vik_track_get_total_elevation_gain(tr, &max_alt, &min_alt );
bf35388d 2994 if ( min_alt == VIK_DEFAULT_ALTITUDE )
4c77d5e0 2995 g_snprintf(tmp_buf, sizeof(tmp_buf), _("No Data"));
6027a9c5
RN
2996 else {
2997 switch (height_units) {
2998 case VIK_UNITS_HEIGHT_METRES:
2999 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f m / %.0f m", max_alt, min_alt );
3000 break;
3001 case VIK_UNITS_HEIGHT_FEET:
6c20e59a 3002 g_snprintf(tmp_buf, sizeof(tmp_buf), "%.0f feet / %.0f feet", VIK_METERS_TO_FEET(max_alt), VIK_METERS_TO_FEET(min_alt) );
6027a9c5
RN
3003 break;
3004 default:
3005 g_snprintf(tmp_buf, sizeof(tmp_buf), "--" );
3006 g_critical("Houston, we've had a problem. height=%d", height_units);
3007 }
3008 }
21700912 3009 widgets->w_elev_gain = content[cnt++] = gtk_label_new ( tmp_buf );
24d5c7e2 3010
4b00e581
AF
3011#if 0
3012#define PACK(w) gtk_box_pack_start (GTK_BOX(right_vbox), w, FALSE, FALSE, 0);
3013 gtk_box_pack_start (GTK_BOX(right_vbox), e_cmt, FALSE, FALSE, 0);
3014 PACK(l_len);
3015 PACK(l_tps);
3016 PACK(l_segs);
3017 PACK(l_dups);
3018 PACK(l_maxs);
3019 PACK(l_avgs);
3020 PACK(l_avgd);
3021 PACK(l_elev);
3022 PACK(l_galo);
3023#undef PACK;
3024#endif
24d5c7e2 3025
8c4f1350 3026 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->timestamp )
f583082b
AF
3027 {
3028 time_t t1, t2;
3029 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
3030 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
3031
7f0d1591 3032 strftime (tmp_buf, sizeof(tmp_buf), "%c", localtime(&(t1)));
21700912 3033 widgets->w_time_start = content[cnt++] = gtk_label_new(tmp_buf);
f583082b 3034
7f0d1591 3035 strftime (tmp_buf, sizeof(tmp_buf), "%c", localtime(&(t2)));
21700912 3036 widgets->w_time_end = content[cnt++] = gtk_label_new(tmp_buf);
f583082b 3037
4c77d5e0 3038 g_snprintf(tmp_buf, sizeof(tmp_buf), _("%d minutes"), (int)(t2-t1)/60);
21700912 3039 widgets->w_time_dur = content[cnt++] = gtk_label_new(tmp_buf);
8c4f1350 3040 } else {
4c77d5e0
GB
3041 widgets->w_time_start = content[cnt++] = gtk_label_new(_("No Data"));
3042 widgets->w_time_end = content[cnt++] = gtk_label_new(_("No Data"));
3043 widgets->w_time_dur = content[cnt++] = gtk_label_new(_("No Data"));
24d5c7e2 3044 }
4b00e581 3045
b1453c16
RN
3046 widgets->w_color = content[cnt++] = gtk_color_button_new_with_color ( &(tr->color) );
3047
4b00e581
AF
3048 table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
3049 gtk_table_set_col_spacing (table, 0, 10);
3050 for (i=0; i<cnt; i++) {
3051 GtkWidget *label;
3052
2f078f56
RN
3053 // Settings so the text positioning only moves around vertically when the dialog is resized
3054 // This also gives more room to see the track comment
4b00e581 3055 label = gtk_label_new(NULL);
2f078f56 3056 gtk_misc_set_alignment ( GTK_MISC(label), 1, 0.5 ); // Position text centrally in vertical plane
4c77d5e0 3057 gtk_label_set_markup ( GTK_LABEL(label), _(label_texts[i]) );
2f078f56 3058 gtk_table_attach ( table, label, 0, 1, i, i+1, GTK_FILL, GTK_SHRINK, 0, 0 );
4b00e581 3059 if (GTK_IS_MISC(content[i])) {
2f078f56 3060 gtk_misc_set_alignment ( GTK_MISC(content[i]), 0, 0.5 );
4b00e581
AF
3061 }
3062 gtk_table_attach_defaults ( table, content[i], 1, 2, i, i+1 );
3063 }
3064
954e061e 3065 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), GTK_WIDGET(table), gtk_label_new(_("Statistics")));
50a14534 3066
950d8a07 3067 if ( widgets->elev_box ) {
065f60f1
GB
3068 GtkWidget *page = NULL;
3069 widgets->w_cur_dist = gtk_label_new(_("No Data"));
780d3771 3070 widgets->w_cur_elevation = gtk_label_new(_("No Data"));
ec32b567
RN
3071 widgets->w_show_dem = gtk_check_button_new_with_mnemonic(_("Show D_EM"));
3072 widgets->w_show_alt_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
780d3771
RN
3073 page = create_graph_page (widgets->elev_box,
3074 _("<b>Track Distance:</b>"), widgets->w_cur_dist,
ec32b567
RN
3075 _("<b>Track Height:</b>"), widgets->w_cur_elevation,
3076 widgets->w_show_dem, TRUE,
3077 widgets->w_show_alt_gps_speed, TRUE);
2cf168ac
RN
3078 g_signal_connect (widgets->w_show_dem, "toggled", G_CALLBACK (checkbutton_toggle_cb), widgets);
3079 g_signal_connect (widgets->w_show_alt_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), widgets);
065f60f1
GB
3080 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-distance")));
3081 }
c79f0206 3082
0ba33e1d
FA
3083 if ( widgets->gradient_box ) {
3084 GtkWidget *page = NULL;
3085 widgets->w_cur_gradient_dist = gtk_label_new(_("No Data"));
3086 widgets->w_cur_gradient_gradient = gtk_label_new(_("No Data"));
3087 widgets->w_show_gradient_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
3088 page = create_graph_page (widgets->gradient_box,
3089 _("<b>Track Distance:</b>"), widgets->w_cur_gradient_dist,
3090 _("<b>Track Gradient:</b>"), widgets->w_cur_gradient_gradient,
3091 widgets->w_show_gradient_gps_speed, TRUE,
3092 NULL, FALSE);
2cf168ac 3093 g_signal_connect (widgets->w_show_gradient_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), widgets);
0ba33e1d
FA
3094 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Gradient-distance")));
3095 }
3096
950d8a07 3097 if ( widgets->speed_box ) {
065f60f1
GB
3098 GtkWidget *page = NULL;
3099 widgets->w_cur_time = gtk_label_new(_("No Data"));
780d3771 3100 widgets->w_cur_speed = gtk_label_new(_("No Data"));
ec32b567 3101 widgets->w_show_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
780d3771
RN
3102 page = create_graph_page (widgets->speed_box,
3103 _("<b>Track Time:</b>"), widgets->w_cur_time,
ec32b567
RN
3104 _("<b>Track Speed:</b>"), widgets->w_cur_speed,
3105 widgets->w_show_gps_speed, TRUE,
3106 NULL, FALSE);
2cf168ac 3107 g_signal_connect (widgets->w_show_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), widgets);
065f60f1
GB
3108 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-time")));
3109 }
50a14534 3110
926c8140
RN
3111 if ( widgets->dist_box ) {
3112 GtkWidget *page = NULL;
3113 widgets->w_cur_dist_time = gtk_label_new(_("No Data"));
3114 widgets->w_cur_dist_dist = gtk_label_new(_("No Data"));
3115 widgets->w_show_dist_speed = gtk_check_button_new_with_mnemonic(_("Show S_peed"));
3116 page = create_graph_page (widgets->dist_box,
3117 _("<b>Track Distance:</b>"), widgets->w_cur_dist_dist,
3118 _("<b>Track Time:</b>"), widgets->w_cur_dist_time,
3119 widgets->w_show_dist_speed, FALSE,
3120 NULL, FALSE);
2cf168ac 3121 g_signal_connect (widgets->w_show_dist_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), widgets);
926c8140
RN
3122 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Distance-time")));
3123 }
3124
8de26632
RN
3125 if ( widgets->elev_time_box ) {
3126 GtkWidget *page = NULL;
3127 widgets->w_cur_elev_time = gtk_label_new(_("No Data"));
3128 widgets->w_cur_elev_elev = gtk_label_new(_("No Data"));
3129 widgets->w_show_elev_speed = gtk_check_button_new_with_mnemonic(_("Show S_peed"));
3130 page = create_graph_page (widgets->elev_time_box,
3131 _("<b>Track Time:</b>"), widgets->w_cur_elev_time,
3132 _("<b>Track Height:</b>"), widgets->w_cur_elev_elev,
3133 widgets->w_show_elev_speed, FALSE,
3134 NULL, FALSE);
2cf168ac 3135 g_signal_connect (widgets->w_show_elev_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), widgets);
8de26632
RN
3136 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Elevation-time")));
3137 }
3138
7b624086
RN
3139 if ( widgets->speed_dist_box ) {
3140 GtkWidget *page = NULL;
3141 widgets->w_cur_speed_dist = gtk_label_new(_("No Data"));
3142 widgets->w_cur_speed_speed = gtk_label_new(_("No Data"));
3143 widgets->w_show_sd_gps_speed = gtk_check_button_new_with_mnemonic(_("Show _GPS Speed"));
3144 page = create_graph_page (widgets->speed_dist_box,
3145 _("<b>Track Distance:</b>"), widgets->w_cur_speed_dist,
3146 _("<b>Track Speed:</b>"), widgets->w_cur_speed_speed,
3147 widgets->w_show_sd_gps_speed, TRUE,
3148 NULL, FALSE);
2cf168ac 3149 g_signal_connect (widgets->w_show_sd_gps_speed, "toggled", G_CALLBACK (checkbutton_toggle_cb), widgets);
7b624086
RN
3150 gtk_notebook_append_page(GTK_NOTEBOOK(graphs), page, gtk_label_new(_("Speed-distance")));
3151 }
3152
9b082b39 3153 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), graphs, FALSE, FALSE, 0);
1d0135d8 3154
8dcb3ba4 3155 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER, FALSE);
1d0135d8 3156 if (seg_count <= 1)
8dbfe7a3 3157 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_SPLIT, FALSE);
1d0135d8 3158 if (vik_track_get_dup_point_count(tr) <= 0)
8dbfe7a3 3159 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), VIK_TRW_LAYER_PROPWIN_DEL_DUP, FALSE);
1d0135d8 3160
d45ac152 3161 // On dialog realization configure_event causes the graphs to be initially drawn
950d8a07 3162 widgets->configure_dialog = TRUE;
2cf168ac
RN
3163 g_signal_connect ( G_OBJECT(dialog), "configure-event", G_CALLBACK (configure_event), widgets );
3164
3165 g_signal_connect ( G_OBJECT(dialog), "destroy", G_CALLBACK (destroy_cb), widgets );
950d8a07 3166
21700912 3167 vik_track_set_property_dialog(tr, dialog);
77b591fa 3168 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
50a14534 3169 gtk_widget_show_all ( dialog );
50a14534 3170}
ce4bd1cf
RN
3171
3172
3173/**
3174 * Update this property dialog
3175 * e.g. if the track has been renamed
3176 */
3177void vik_trw_layer_propwin_update ( VikTrack *trk )
3178{
3179 // If not displayed do nothing
3180 if ( !trk->property_dialog )
3181 return;
3182
3183 // Update title with current name
3184 if ( trk->name ) {
3185 gchar *title = g_strdup_printf ( _("%s - Track Properties"), trk->name );
3186 gtk_window_set_title ( GTK_WINDOW(trk->property_dialog), title );
3187 g_free(title);
3188 }
3189
3190}