]> git.street.me.uk Git - andy/viking.git/blob - src/viktrwlayer_analysis.c
Fix issue in display of maximum speed when there is no speed.
[andy/viking.git] / src / viktrwlayer_analysis.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4  *
5  * Copyright (C) 2013 Rob Norris <rw_norris@hotmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  ***********************************************************
22  *
23  */
24
25 #include <math.h>
26 #include <time.h>
27 #include <string.h>
28 #include <glib/gprintf.h>
29 #include <glib/gi18n.h>
30
31 #include "viking.h"
32 #include "viktrwlayer_analysis.h"
33 #include "ui_util.h"
34
35 // Units of each item are in SI Units
36 // (as returned by the appropriate internal viking track functions)
37 typedef struct {
38         gdouble  min_alt;
39         gdouble  max_alt;
40         gdouble  elev_gain;
41         gdouble  elev_loss;
42         gdouble  length;
43         gdouble  length_gaps;
44         gdouble  max_speed;
45         gulong   trackpoints;
46         guint    segments;
47         gint     duration;
48         time_t   start_time;
49         time_t   end_time;
50         gint     count;
51 } track_stats;
52
53 // Early incarnations of the code had facilities to print output for multiple files
54 //  but has been rescoped to work on a single list of tracks for the GUI
55 typedef enum {
56         //TS_TRACK,
57         TS_TRACKS,
58         //TS_FILES,
59 } track_stat_block;
60 static track_stats tracks_stats[1];
61
62 // cf with vik_track_get_minmax_alt internals
63 #define VIK_VAL_MIN_ALT 25000.0
64 #define VIK_VAL_MAX_ALT -5000.0
65
66 /**
67  * Reset the specified block
68  * Call this when starting to processing multiple items
69  */
70 static void val_reset ( track_stat_block block )
71 {
72         tracks_stats[block].min_alt     = VIK_VAL_MIN_ALT;
73         tracks_stats[block].max_alt     = VIK_VAL_MAX_ALT;
74         tracks_stats[block].elev_gain   = 0.0;
75         tracks_stats[block].elev_loss   = 0.0;
76         tracks_stats[block].length      = 0.0;
77         tracks_stats[block].length_gaps = 0.0;
78         tracks_stats[block].max_speed   = 0.0;
79         tracks_stats[block].trackpoints = 0;
80         tracks_stats[block].segments    = 0;
81         tracks_stats[block].duration    = 0;
82         tracks_stats[block].start_time  = 0;
83         tracks_stats[block].end_time    = 0;
84         tracks_stats[block].count       = 0;
85 }
86
87 /**
88  * @val_analyse_track:
89  * @trk: The track to be analyse
90  *
91  * Function to collect statistics, using the internal track functions
92  */
93 static void val_analyse_track ( VikTrack *trk )
94 {
95         //val_reset ( TS_TRACK );
96         gdouble min_alt;
97         gdouble max_alt;
98         gdouble up;
99         gdouble down;
100
101         gdouble  length      = 0.0;
102         gdouble  length_gaps = 0.0;
103         gdouble  max_speed   = 0.0;
104         gulong   trackpoints = 0;
105         guint    segments    = 0;
106
107         tracks_stats[TS_TRACKS].count++;
108
109         trackpoints = vik_track_get_tp_count (trk);
110         segments    = vik_track_get_segment_count (trk);
111         length      = vik_track_get_length (trk);
112         length_gaps = vik_track_get_length_including_gaps (trk);
113         max_speed   = vik_track_get_max_speed (trk);
114
115         int ii;
116         for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
117                 tracks_stats[ii].trackpoints += trackpoints;
118                 tracks_stats[ii].segments    += segments;
119                 tracks_stats[ii].length      += length;
120                 tracks_stats[ii].length_gaps += length_gaps;
121                 if ( max_speed > tracks_stats[ii].max_speed )
122                         tracks_stats[ii].max_speed = max_speed;
123         }
124
125         if ( vik_track_get_minmax_alt (trk, &min_alt, &max_alt) ) {
126                 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
127                         if ( min_alt < tracks_stats[ii].min_alt )
128                                 tracks_stats[ii].min_alt = min_alt;
129                         if ( max_alt > tracks_stats[ii].max_alt )
130                                 tracks_stats[ii].max_alt = max_alt;
131                 }
132         }
133
134         vik_track_get_total_elevation_gain (trk, &up, &down );
135
136         for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
137                 tracks_stats[ii].elev_gain += up;
138                 tracks_stats[ii].elev_loss += down;
139         }
140
141         if ( trk->trackpoints && VIK_TRACKPOINT(trk->trackpoints->data)->timestamp ) {
142                 time_t t1, t2;
143                 t1 = VIK_TRACKPOINT(g_list_first(trk->trackpoints)->data)->timestamp;
144                 t2 = VIK_TRACKPOINT(g_list_last(trk->trackpoints)->data)->timestamp;
145
146                 // Assume never actually have a track with a time of 0 (1st Jan 1970)
147                 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
148                         if ( tracks_stats[ii].start_time == 0)
149                                 tracks_stats[ii].start_time = t1;
150                         if ( tracks_stats[ii].end_time == 0)
151                                 tracks_stats[ii].end_time = t2;
152                 }
153
154                 // Initialize to the first value
155                 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
156                         if (t1 < tracks_stats[ii].start_time)
157                                 tracks_stats[ii].start_time = t1;
158                         if (t2 > tracks_stats[ii].end_time)
159                                 tracks_stats[ii].end_time = t2;
160                 }
161
162                 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
163                         tracks_stats[ii].duration = tracks_stats[ii].duration + (int)(t2-t1);
164                 }
165         }
166 }
167
168 // Could use GtkGrids but that is Gtk3+
169 static GtkWidget *create_table (int cnt, char *labels[], GtkWidget *contents[])
170 {
171         GtkTable *table;
172         int i;
173
174         table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
175         gtk_table_set_col_spacing (table, 0, 10);
176         for (i=0; i<cnt; i++) {
177                 GtkWidget *label;
178                 label = gtk_label_new(NULL);
179                 gtk_misc_set_alignment ( GTK_MISC(label), 1, 0.5 ); // Position text centrally in vertical plane
180                 // All text labels are set to be in bold
181                 char *markup = g_markup_printf_escaped ("<b>%s:</b>", _(labels[i]) );
182                 gtk_label_set_markup ( GTK_LABEL(label), markup );
183                 g_free ( markup );
184                 gtk_table_attach ( table, label, 0, 1, i, i+1, GTK_FILL, GTK_EXPAND, 4, 2 );
185                 if (GTK_IS_MISC(contents[i])) {
186                         gtk_misc_set_alignment ( GTK_MISC(contents[i]), 0, 0.5 );
187                 }
188                 gtk_table_attach_defaults ( table, contents[i], 1, 2, i, i+1 );
189         }
190         return GTK_WIDGET (table);
191 }
192
193 static gchar *label_texts[] = {
194         N_("Number of Tracks"),
195         N_("Date Range"),
196         N_("Total Length"),
197         N_("Average Length"),
198         N_("Max Speed"),
199         N_("Avg. Speed"),
200         N_("Minimum Altitude"),
201         N_("Maximum Altitude"),
202         N_("Total Elevation Gain/Loss"),
203         N_("Avg. Elevation Gain/Loss"),
204         N_("Total Duration"),
205         N_("Avg. Duration"),
206 };
207
208 /**
209  * create_layout:
210  *
211  * Returns a widget to hold the stats information in a table grid layout
212  */
213 static GtkWidget *create_layout ( GtkWidget *content[] )
214 {
215         int cnt = 0;
216         for ( cnt = 0; cnt < G_N_ELEMENTS(label_texts); cnt++ )
217                 content[cnt] = ui_label_new_selectable ( NULL );
218
219         return create_table (cnt, label_texts, content);
220 }
221
222 /**
223  * table_output:
224  *
225  * Update the given widgets table with the values from the track stats
226  */
227 static void table_output ( track_stats ts, GtkWidget *content[] )
228 {
229         int cnt = 0;
230
231         gchar tmp_buf[64];
232         g_snprintf ( tmp_buf, sizeof(tmp_buf), "%d", ts.count );
233         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
234
235         if ( ts.count == 0 ) {
236                 // Blank all other fields
237                 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
238                 for ( cnt = 1; cnt < G_N_ELEMENTS(label_texts); cnt++ )
239                         gtk_label_set_text ( GTK_LABEL(content[cnt]), tmp_buf );
240                 return;
241         }
242
243         // Check for potential date range
244         // Test if the same day by comparing the date string of the timestamp
245         GDate* gdate_start = g_date_new ();
246         g_date_set_time_t ( gdate_start, ts.start_time );
247         gchar time_start[32];
248         g_date_strftime ( time_start, sizeof(time_start), "%x", gdate_start );
249         g_date_free ( gdate_start );
250
251         GDate* gdate_end = g_date_new ();
252         g_date_set_time_t ( gdate_end, ts.end_time );
253         gchar time_end[32];
254         g_date_strftime ( time_end, sizeof(time_end), "%x", gdate_end );
255         g_date_free ( gdate_end );
256
257         if ( ts.start_time == ts.end_time )
258                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("No Data") );
259         else if ( strncmp(time_start, time_end, 32) )
260                 g_snprintf ( tmp_buf, sizeof(tmp_buf), "%s --> %s", time_start, time_end );
261         else
262                 g_snprintf ( tmp_buf, sizeof(tmp_buf), "%s", time_start );
263
264         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
265
266         switch (a_vik_get_units_distance ()) {
267         case VIK_UNITS_DISTANCE_MILES:
268                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f miles"), VIK_METERS_TO_MILES(ts.length) );
269                 break;
270         default:
271                 //VIK_UNITS_DISTANCE_KILOMETRES
272                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f km"), ts.length/1000.0 );
273                 break;
274         }
275         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
276
277         switch (a_vik_get_units_distance ()) {
278         case VIK_UNITS_DISTANCE_MILES:
279                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f miles"), (VIK_METERS_TO_MILES(ts.length)/ts.count) );
280                 break;
281         default:
282                 //VIK_UNITS_DISTANCE_KILOMETRES
283                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km"), ts.length/(1000.0*ts.count) );
284                 break;
285         }
286         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
287
288         // I'm sure this could be cleaner...
289         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
290         switch (a_vik_get_units_speed()) {
291         case VIK_UNITS_SPEED_MILES_PER_HOUR:
292                 if ( ts.max_speed > 0 )
293                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f mph"), (double)VIK_MPS_TO_MPH(ts.max_speed) );
294                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
295                 if ( ts.duration > 0 )
296                         g_snprintf ( tmp_buf, sizeof(tmp_buf), ("%.1f mph"), (double)VIK_MPS_TO_MPH(ts.length/ts.duration) );
297                 else
298                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
299                 break;
300         case VIK_UNITS_SPEED_METRES_PER_SECOND:
301                 if ( ts.max_speed > 0 )
302                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f m/s"), (double)ts.max_speed );
303                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
304                 if ( ts.duration > 0 )
305                         g_snprintf ( tmp_buf, sizeof(tmp_buf), ("%.2f m/s"), (double)(ts.length/ts.duration) );
306                 else
307                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
308                 break;
309         case VIK_UNITS_SPEED_KNOTS:
310                 if ( ts.max_speed > 0 )
311                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f knots\n"), (double)VIK_MPS_TO_KNOTS(ts.max_speed) );
312                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
313                 if ( ts.duration > 0 )
314                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f knots"), (double)VIK_MPS_TO_KNOTS(ts.length/ts.duration) );
315                 else
316                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
317                 break;
318         default:
319                 //VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
320                 if ( ts.max_speed > 0 )
321                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km/h"), (double)VIK_MPS_TO_KPH(ts.max_speed) );
322                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
323                 if ( ts.duration > 0 )
324                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km/h"), (double)VIK_MPS_TO_KPH(ts.length/ts.duration) );
325                 else
326                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
327                 break;
328         }
329         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
330
331         switch ( a_vik_get_units_height() ) {
332                 // Note always round off height value output since sub unit accuracy is overkill
333         case VIK_UNITS_HEIGHT_FEET:
334                 if ( ts.min_alt != VIK_VAL_MIN_ALT )
335                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet"), (int)round(VIK_METERS_TO_FEET(ts.min_alt)) );
336                 else
337                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
338                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
339
340                 if ( ts.max_alt != VIK_VAL_MAX_ALT )
341                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet"), (int)round(VIK_METERS_TO_FEET(ts.max_alt)) );
342                 else
343                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
344                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
345
346                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet / %d feet"), (int)round(VIK_METERS_TO_FEET(ts.elev_gain)), (int)round(VIK_METERS_TO_FEET(ts.elev_loss)) );
347                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
348                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet / %d feet"), (int)round(VIK_METERS_TO_FEET(ts.elev_gain/ts.count)), (int)round(VIK_METERS_TO_FEET(ts.elev_loss/ts.count)) );
349                 break;
350         default:
351                 //VIK_UNITS_HEIGHT_METRES
352                 if ( ts.min_alt != VIK_VAL_MIN_ALT )
353                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m"), (int)round(ts.min_alt) );
354                 else
355                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
356                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
357
358                 if ( ts.max_alt != VIK_VAL_MAX_ALT )
359                         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m"), (int)round(ts.max_alt) );
360                 else
361                         g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
362                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
363
364                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m / %d m"), (int)round(ts.elev_gain), (int)round(ts.elev_loss) );
365                 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
366                 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m / %d m"), (int)round(ts.elev_gain/ts.count), (int)round(ts.elev_loss/ts.count) );
367                 break;
368         }
369         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
370
371         gint hours;
372         gint minutes;
373         gint days;
374         // Total Duration
375         days    = (gint)(ts.duration / (60*60*24));
376         hours   = (gint)floor((ts.duration - (days*60*60*24)) / (60*60));
377         minutes = (gint)((ts.duration - (days*60*60*24) - (hours*60*60)) / 60);
378         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d:%02d:%02d days:hrs:mins"), days, hours, minutes );
379         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
380
381         // Average Duration
382         gint avg_dur = ts.duration / ts.count;
383         hours   = (gint)floor(avg_dur / (60*60));
384         minutes = (gint)((avg_dur - (hours*60*60)) / 60);
385         g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d:%02d hrs:mins"), hours, minutes );
386         gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
387 }
388
389 /**
390  * val_analyse_item_maybe:
391  * @vtlist: A track and the associated layer to consider for analysis
392  * @data:   Whether to include invisible items
393  *
394  * Analyse this particular track
395  *  considering whether it should be included depending on it's visibility
396  */
397 static void val_analyse_item_maybe ( vik_trw_track_list_t *vtlist, const gpointer data )
398 {
399         gboolean include_invisible = GPOINTER_TO_INT(data);
400         VikTrack *trk = vtlist->trk;
401         VikTrwLayer *vtl = vtlist->vtl;
402
403         // Safety first - items shouldn't be deleted...
404         if ( !IS_VIK_TRW_LAYER(vtl) ) return;
405         if ( !trk ) return;
406
407         if ( !include_invisible ) {
408                 // Skip invisible layers or sublayers
409                 if ( !VIK_LAYER(vtl)->visible ||
410                          (trk->is_route && !vik_trw_layer_get_routes_visibility(vtl)) ||
411                          (!trk->is_route && !vik_trw_layer_get_tracks_visibility(vtl)) )
412                         return;
413
414                 // Skip invisible tracks
415                 if ( !trk->visible )
416                         return;
417         }
418
419         val_analyse_track ( trk );
420 }
421
422 /**
423  * val_analyse:
424  * @widgets:           The widget layout
425  * @tracks_and_layers: A list of #vik_trw_track_list_t
426  * @include_invisible: Whether to include invisible layers and tracks
427  *
428  * Analyse each item in the @tracks_and_layers list
429  *
430  */
431 void val_analyse ( GtkWidget *widgets[], GList *tracks_and_layers, gboolean include_invisible )
432 {
433         val_reset ( TS_TRACKS );
434
435         GList *gl = g_list_first ( tracks_and_layers );
436         if ( gl ) {
437                 g_list_foreach ( gl, (GFunc) val_analyse_item_maybe, GINT_TO_POINTER(include_invisible) );
438         }
439
440         table_output ( tracks_stats[TS_TRACKS], widgets );
441 }
442
443 typedef struct {
444         GtkWidget **widgets;
445         GtkWidget *layout;
446         GtkWidget *check_button;
447         GList *tracks_and_layers;
448         VikLayer *vl;
449         gpointer user_data;
450         VikTrwlayerGetTracksAndLayersFunc get_tracks_and_layers_cb;
451         VikTrwlayerAnalyseCloseFunc on_close_cb;
452 } analyse_cb_t;
453
454 static void include_invisible_toggled_cb ( GtkToggleButton *togglebutton, analyse_cb_t *acb )
455 {
456         gboolean value = FALSE;
457         if ( gtk_toggle_button_get_active ( togglebutton ) )
458                 value = TRUE;
459
460         // Delete old list of items
461         if ( acb->tracks_and_layers ) {
462                 g_list_foreach ( acb->tracks_and_layers, (GFunc) g_free, NULL );
463                 g_list_free ( acb->tracks_and_layers );
464         }
465
466         // Get the latest list of items to analyse
467         acb->tracks_and_layers = acb->get_tracks_and_layers_cb ( acb->vl, acb->user_data );
468
469         val_analyse ( acb->widgets, acb->tracks_and_layers, value );
470         gtk_widget_show_all ( acb->layout );
471 }
472
473 #define VIK_SETTINGS_ANALYSIS_DO_INVISIBLE "track_analysis_do_invisible"
474
475 /**
476  * analyse_close:
477  *
478  * Multi stage closure - as we need to clear allocations made here
479  *  before passing on to the callee so they know then the dialog is closed too
480  */
481 static void analyse_close ( GtkWidget *dialog, gint resp, analyse_cb_t *data )
482 {
483         // Save current invisible value for next time
484         gboolean do_invisible = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(data->check_button) );
485         a_settings_set_boolean ( VIK_SETTINGS_ANALYSIS_DO_INVISIBLE, do_invisible );
486
487         //g_free ( data->layout );
488         g_free ( data->widgets );
489         g_list_foreach ( data->tracks_and_layers, (GFunc) g_free, NULL );
490         g_list_free ( data->tracks_and_layers );
491
492         if ( data->on_close_cb )
493                 data->on_close_cb ( dialog, resp, data->vl );
494
495         g_free ( data );
496 }
497
498 /**
499  * vik_trw_layer_analyse_this:
500  * @window:                   A window from which the dialog will be derived
501  * @name:                     The name to be shown
502  * @vl:                       The #VikLayer passed on into get_tracks_and_layers_cb()
503  * @user_data:                Data passed on into get_tracks_and_layers_cb()
504  * @get_tracks_and_layers_cb: The function to call to construct items to be analysed
505  *
506  * Display a dialog with stats across many tracks
507  *
508  * Returns: The dialog that is created to display the analyse information
509  */
510 GtkWidget* vik_trw_layer_analyse_this ( GtkWindow *window,
511                                         const gchar *name,
512                                         VikLayer *vl,
513                                         gpointer user_data,
514                                         VikTrwlayerGetTracksAndLayersFunc get_tracks_and_layers_cb,
515                                         VikTrwlayerAnalyseCloseFunc on_close_cb )
516 {
517         //VikWindow *vw = VIK_WINDOW(window);
518
519         GtkWidget *dialog;
520         dialog = gtk_dialog_new_with_buttons ( _("Statistics"),
521                                                window,
522                                                GTK_DIALOG_DESTROY_WITH_PARENT,
523                                                GTK_STOCK_CLOSE,     GTK_RESPONSE_CANCEL,
524                                                NULL );
525
526         GtkWidget *name_l = gtk_label_new ( NULL );
527         gchar *myname = g_markup_printf_escaped ( "<b>%s</b>", name );
528         gtk_label_set_markup ( GTK_LABEL(name_l), myname );
529         g_free ( myname );
530
531         GtkWidget *content = gtk_dialog_get_content_area ( GTK_DIALOG(dialog) );
532         gtk_box_pack_start ( GTK_BOX(content), name_l, FALSE, FALSE, 10);
533
534         // Get previous value (if any) from the settings
535         gboolean include_invisible;
536         if ( ! a_settings_get_boolean ( VIK_SETTINGS_ANALYSIS_DO_INVISIBLE, &include_invisible ) )
537                 include_invisible = TRUE;
538
539         analyse_cb_t *acb = g_malloc (sizeof(analyse_cb_t));
540         acb->vl = vl;
541         acb->user_data = user_data;
542         acb->get_tracks_and_layers_cb = get_tracks_and_layers_cb;
543         acb->on_close_cb = on_close_cb;
544         acb->tracks_and_layers = get_tracks_and_layers_cb ( vl, user_data );
545         acb->widgets = g_malloc ( sizeof(GtkWidget*) * G_N_ELEMENTS(label_texts) );
546         acb->layout = create_layout ( acb->widgets );
547
548         gtk_box_pack_start ( GTK_BOX(content), acb->layout, FALSE, FALSE, 0 );
549
550         // Analysis seems reasonably quick
551         //  unless you have really large numbers of tracks (i.e. many many thousands or a really slow computer)
552         // One day might store stats in the track itself....
553         val_analyse ( acb->widgets, acb->tracks_and_layers, include_invisible );
554         
555         GtkWidget *cb = gtk_check_button_new_with_label ( _("Include Invisible Items") );
556         gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(cb), include_invisible );
557         gtk_box_pack_start ( GTK_BOX(content), cb, FALSE, FALSE, 10);
558         acb->check_button = cb;
559         
560         gtk_widget_show_all ( dialog );
561
562         g_signal_connect ( G_OBJECT(cb), "toggled", G_CALLBACK(include_invisible_toggled_cb), acb );
563         g_signal_connect ( G_OBJECT(dialog), "response", G_CALLBACK(analyse_close), acb );
564
565         return dialog;
566 }