]> git.tdb.fi Git - ext/subsurface.git/blob - divelist.c
a3bb1fb5eb897320cdd382728778fc01f3274f15
[ext/subsurface.git] / divelist.c
1 /* divelist.c */
2 /* this creates the UI for the dive list -
3  * controlled through the following interfaces:
4  *
5  * void flush_divelist(struct dive *dive)
6  * GtkWidget dive_list_create(void)
7  * void dive_list_update_dives(void)
8  * void update_dive_list_units(void)
9  * void set_divelist_font(const char *font)
10  * void mark_divelist_changed(int changed)
11  * int unsaved_changes()
12  */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include <math.h>
18
19 #include "divelist.h"
20 #include "dive.h"
21 #include "display.h"
22 #include "display-gtk.h"
23
24 struct DiveList {
25         GtkWidget    *tree_view;
26         GtkWidget    *container_widget;
27         GtkTreeStore *model, *listmodel, *treemodel;
28         GtkTreeViewColumn *nr, *date, *stars, *depth, *duration, *location;
29         GtkTreeViewColumn *temperature, *cylinder, *totalweight, *suit, *nitrox, *sac, *otu;
30         int changed;
31 };
32
33 static struct DiveList dive_list;
34 GList *dive_trip_list;
35 gboolean autogroup = FALSE;
36
37 const char *tripflag_names[NUM_TRIPFLAGS] = { "TF_NONE", "NOTRIP", "INTRIP" };
38
39 /*
40  * The dive list has the dive data in both string format (for showing)
41  * and in "raw" format (for sorting purposes)
42  */
43 enum {
44         DIVE_INDEX = 0,
45         DIVE_NR,                /* int: dive->nr */
46         DIVE_DATE,              /* time_t: dive->when */
47         DIVE_RATING,            /* int: 0-5 stars */
48         DIVE_DEPTH,             /* int: dive->maxdepth in mm */
49         DIVE_DURATION,          /* int: in seconds */
50         DIVE_TEMPERATURE,       /* int: in mkelvin */
51         DIVE_TOTALWEIGHT,       /* int: in grams */
52         DIVE_SUIT,              /* "wet, 3mm" */
53         DIVE_CYLINDER,
54         DIVE_NITROX,            /* int: dummy */
55         DIVE_SAC,               /* int: in ml/min */
56         DIVE_OTU,               /* int: in OTUs */
57         DIVE_LOCATION,          /* "2nd Cathedral, Lanai" */
58         DIVELIST_COLUMNS
59 };
60
61 #ifdef DEBUG_MODEL
62 static gboolean dump_model_entry(GtkTreeModel *model, GtkTreePath *path,
63                                 GtkTreeIter *iter, gpointer data)
64 {
65         char *location;
66         int idx, nr, duration;
67         struct dive *dive;
68
69         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, DIVE_DURATION, &duration, DIVE_LOCATION, &location, -1);
70         printf("entry #%d : nr %d duration %d location %s ", idx, nr, duration, location);
71         dive = get_dive(idx);
72         if (dive)
73                 printf("tripflag %d\n", dive->tripflag);
74         else
75                 printf("without matching dive\n");
76
77         free(location);
78
79         return FALSE;
80 }
81
82 static void dump_model(GtkListStore *store)
83 {
84         gtk_tree_model_foreach(GTK_TREE_MODEL(store), dump_model_entry, NULL);
85 }
86 #endif
87
88 #if DEBUG_SELECTION_TRACKING
89 void dump_selection(void)
90 {
91         int i;
92         struct dive *dive;
93
94         printf("currently selected are %d dives:", amount_selected);
95         for (i = 0; (dive = get_dive(i)) != NULL; i++) {
96                 if (dive->selected)
97                         printf(" %d", i);
98         }
99         printf("\n");
100 }
101 #endif
102
103 /* when subsurface starts we want to have the last dive selected. So we simply
104    walk to the first leaf (and skip the summary entries - which have negative
105    DIVE_INDEX) */
106 static void first_leaf(GtkTreeModel *model, GtkTreeIter *iter, int *diveidx)
107 {
108         GtkTreeIter parent;
109         GtkTreePath *tpath;
110
111         while (*diveidx < 0) {
112                 memcpy(&parent, iter, sizeof(parent));
113                 tpath = gtk_tree_model_get_path(model, &parent);
114                 if (!gtk_tree_model_iter_children(model, iter, &parent))
115                         /* we should never have a parent without child */
116                         return;
117                 if(!gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath))
118                         gtk_tree_view_expand_row(GTK_TREE_VIEW(dive_list.tree_view), tpath, FALSE);
119                 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, DIVE_INDEX, diveidx, -1);
120         }
121 }
122
123 /* make sure that if we expand a summary row that is selected, the children show
124    up as selected, too */
125 void row_expanded_cb(GtkTreeView *tree_view, GtkTreeIter *iter, GtkTreePath *path, gpointer data)
126 {
127         GtkTreeIter child;
128         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
129         GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
130
131         if (!gtk_tree_model_iter_children(model, &child, iter))
132                 return;
133
134         do {
135                 int idx;
136                 struct dive *dive;
137
138                 gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
139                 dive = get_dive(idx);
140
141                 if (dive->selected)
142                         gtk_tree_selection_select_iter(selection, &child);
143                 else
144                         gtk_tree_selection_unselect_iter(selection, &child);
145         } while (gtk_tree_model_iter_next(model, &child));
146 }
147
148 static int selected_children(GtkTreeModel *model, GtkTreeIter *iter)
149 {
150         GtkTreeIter child;
151
152         if (!gtk_tree_model_iter_children(model, &child, iter))
153                 return FALSE;
154
155         do {
156                 int idx;
157                 struct dive *dive;
158
159                 gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
160                 dive = get_dive(idx);
161
162                 if (dive->selected)
163                         return TRUE;
164         } while (gtk_tree_model_iter_next(model, &child));
165         return FALSE;
166 }
167
168 /* Make sure that if we collapse a summary row with any selected children, the row
169    shows up as selected too */
170 void row_collapsed_cb(GtkTreeView *tree_view, GtkTreeIter *iter, GtkTreePath *path, gpointer data)
171 {
172         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
173         GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
174
175         if (selected_children(model, iter))
176                 gtk_tree_selection_select_iter(selection, iter);
177 }
178
179 static GList *selection_changed = NULL;
180
181 /*
182  * This is called _before_ the selection is changed, for every single entry;
183  *
184  * We simply create a list of all changed entries, and make sure that the
185  * group entries go at the end of the list.
186  */
187 gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *model,
188                         GtkTreePath *path, gboolean was_selected, gpointer userdata)
189 {
190         GtkTreeIter iter, *p;
191
192         if (!gtk_tree_model_get_iter(model, &iter, path))
193                 return TRUE;
194
195         /* Add the group entries to the end */
196         p = gtk_tree_iter_copy(&iter);
197         if (gtk_tree_model_iter_has_child(model, p))
198                 selection_changed = g_list_append(selection_changed, p);
199         else
200                 selection_changed = g_list_prepend(selection_changed, p);
201         return TRUE;
202 }
203
204 static void select_dive(struct dive *dive, int selected)
205 {
206         if (dive->selected != selected) {
207                 amount_selected += selected ? 1 : -1;
208                 dive->selected = selected;
209         }
210 }
211
212 /*
213  * This gets called when a dive group has changed selection.
214  */
215 static void select_dive_group(GtkTreeModel *model, GtkTreeSelection *selection, GtkTreeIter *iter, int selected)
216 {
217         int first = 1;
218         GtkTreeIter child;
219
220         if (selected == selected_children(model, iter))
221                 return;
222
223         if (!gtk_tree_model_iter_children(model, &child, iter))
224                 return;
225
226         do {
227                 int idx;
228                 struct dive *dive;
229
230                 gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
231                 if (first && selected)
232                         selected_dive = idx;
233                 first = 0;
234                 dive = get_dive(idx);
235                 if (dive->selected == selected)
236                         continue;
237
238                 select_dive(dive, selected);
239                 if (selected)
240                         gtk_tree_selection_select_iter(selection, &child);
241                 else
242                         gtk_tree_selection_unselect_iter(selection, &child);
243         } while (gtk_tree_model_iter_next(model, &child));
244 }
245
246 /*
247  * This gets called _after_ the selections have changed, for each entry that
248  * may have changed. Check if the gtk selection state matches our internal
249  * selection state to verify.
250  *
251  * The group entries are at the end, this guarantees that we have handled
252  * all the dives before we handle groups.
253  */
254 static void check_selection_cb(GtkTreeIter *iter, GtkTreeSelection *selection)
255 {
256         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
257         struct dive *dive;
258         int idx, gtk_selected;
259
260         gtk_tree_model_get(model, iter,
261                 DIVE_INDEX, &idx,
262                 -1);
263         dive = get_dive(idx);
264         gtk_selected = gtk_tree_selection_iter_is_selected(selection, iter);
265         if (idx < 0)
266                 select_dive_group(model, selection, iter, gtk_selected);
267         else {
268                 select_dive(dive, gtk_selected);
269                 if (gtk_selected)
270                         selected_dive = idx;
271         }
272         gtk_tree_iter_free(iter);
273 }
274
275 /* this is called when gtk thinks that the selection has changed */
276 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
277 {
278         GList *changed = selection_changed;
279
280         selection_changed = NULL;
281         g_list_foreach(changed, (GFunc) check_selection_cb, selection);
282         g_list_free(changed);
283 #if DEBUG_SELECTION_TRACKING
284         dump_selection();
285 #endif
286
287         process_selected_dives();
288         repaint_dive();
289 }
290
291 const char *star_strings[] = {
292         ZERO_STARS,
293         ONE_STARS,
294         TWO_STARS,
295         THREE_STARS,
296         FOUR_STARS,
297         FIVE_STARS
298 };
299
300 static void star_data_func(GtkTreeViewColumn *col,
301                            GtkCellRenderer *renderer,
302                            GtkTreeModel *model,
303                            GtkTreeIter *iter,
304                            gpointer data)
305 {
306         int nr_stars, idx;
307         char buffer[40];
308
309         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_RATING, &nr_stars, -1);
310         if (idx < 0) {
311                 *buffer = '\0';
312         } else {
313                 if (nr_stars < 0 || nr_stars > 5)
314                         nr_stars = 0;
315                 snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]);
316         }
317         g_object_set(renderer, "text", buffer, NULL);
318 }
319
320 static void date_data_func(GtkTreeViewColumn *col,
321                            GtkCellRenderer *renderer,
322                            GtkTreeModel *model,
323                            GtkTreeIter *iter,
324                            gpointer data)
325 {
326         int val, idx, nr;
327         struct tm *tm;
328         time_t when;
329         char buffer[40];
330
331         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &val, DIVE_NR, &nr, -1);
332
333         /* 2038 problem */
334         when = val;
335
336         tm = gmtime(&when);
337         if (idx < 0) {
338                 snprintf(buffer, sizeof(buffer),
339                         "Trip %s, %s %d, %d (%d dive%s)",
340                         weekday(tm->tm_wday),
341                         monthname(tm->tm_mon),
342                         tm->tm_mday, tm->tm_year + 1900,
343                         nr, nr > 1 ? "s" : "");
344         } else {
345                 snprintf(buffer, sizeof(buffer),
346                         "%s, %s %d, %d %02d:%02d",
347                         weekday(tm->tm_wday),
348                         monthname(tm->tm_mon),
349                         tm->tm_mday, tm->tm_year + 1900,
350                         tm->tm_hour, tm->tm_min);
351         }
352         g_object_set(renderer, "text", buffer, NULL);
353 }
354
355 static void depth_data_func(GtkTreeViewColumn *col,
356                             GtkCellRenderer *renderer,
357                             GtkTreeModel *model,
358                             GtkTreeIter *iter,
359                             gpointer data)
360 {
361         int depth, integer, frac, len, idx;
362         char buffer[40];
363
364         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DEPTH, &depth, -1);
365
366         if (idx < 0) {
367                 *buffer = '\0';
368         } else {
369                 switch (output_units.length) {
370                 case METERS:
371                         /* To tenths of meters */
372                         depth = (depth + 49) / 100;
373                         integer = depth / 10;
374                         frac = depth % 10;
375                         if (integer < 20)
376                                 break;
377                         if (frac >= 5)
378                                 integer++;
379                         frac = -1;
380                         break;
381                 case FEET:
382                         integer = mm_to_feet(depth) + 0.5;
383                         frac = -1;
384                         break;
385                 default:
386                         return;
387                 }
388                 len = snprintf(buffer, sizeof(buffer), "%d", integer);
389                 if (frac >= 0)
390                         len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
391         }
392         g_object_set(renderer, "text", buffer, NULL);
393 }
394
395 static void duration_data_func(GtkTreeViewColumn *col,
396                                GtkCellRenderer *renderer,
397                                GtkTreeModel *model,
398                                GtkTreeIter *iter,
399                                gpointer data)
400 {
401         unsigned int sec;
402         int idx;
403         char buffer[16];
404
405         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DURATION, &sec, -1);
406         if (idx < 0)
407                 *buffer = '\0';
408         else
409                 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
410
411         g_object_set(renderer, "text", buffer, NULL);
412 }
413
414 static void temperature_data_func(GtkTreeViewColumn *col,
415                                   GtkCellRenderer *renderer,
416                                   GtkTreeModel *model,
417                                   GtkTreeIter *iter,
418                                   gpointer data)
419 {
420         int value, idx;
421         char buffer[80];
422
423         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_TEMPERATURE, &value, -1);
424
425         *buffer = 0;
426         if (idx >= 0 && value) {
427                 double deg;
428                 switch (output_units.temperature) {
429                 case CELSIUS:
430                         deg = mkelvin_to_C(value);
431                         break;
432                 case FAHRENHEIT:
433                         deg = mkelvin_to_F(value);
434                         break;
435                 default:
436                         return;
437                 }
438                 snprintf(buffer, sizeof(buffer), "%.1f", deg);
439         }
440
441         g_object_set(renderer, "text", buffer, NULL);
442 }
443
444 static void nr_data_func(GtkTreeViewColumn *col,
445                            GtkCellRenderer *renderer,
446                            GtkTreeModel *model,
447                            GtkTreeIter *iter,
448                            gpointer data)
449 {
450         int idx, nr;
451         char buffer[40];
452
453         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, -1);
454         if (idx < 0)
455                 *buffer = '\0';
456         else
457                 snprintf(buffer, sizeof(buffer), "%d", nr);
458         g_object_set(renderer, "text", buffer, NULL);
459 }
460
461 /*
462  * Get "maximal" dive gas for a dive.
463  * Rules:
464  *  - Trimix trumps nitrox (highest He wins, O2 breaks ties)
465  *  - Nitrox trumps air (even if hypoxic)
466  * These are the same rules as the inter-dive sorting rules.
467  */
468 static void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p)
469 {
470         int i;
471         int maxo2 = -1, maxhe = -1, mino2 = 1000;
472
473         for (i = 0; i < MAX_CYLINDERS; i++) {
474                 cylinder_t *cyl = dive->cylinder + i;
475                 struct gasmix *mix = &cyl->gasmix;
476                 int o2 = mix->o2.permille;
477                 int he = mix->he.permille;
478
479                 if (cylinder_none(cyl))
480                         continue;
481                 if (!o2)
482                         o2 = AIR_PERMILLE;
483                 if (o2 < mino2)
484                         mino2 = o2;
485                 if (he > maxhe)
486                         goto newmax;
487                 if (he < maxhe)
488                         continue;
489                 if (o2 <= maxo2)
490                         continue;
491 newmax:
492                 maxhe = he;
493                 maxo2 = o2;
494         }
495         /* All air? Show/sort as "air"/zero */
496         if (!maxhe && maxo2 == AIR_PERMILLE && mino2 == maxo2)
497                 maxo2 = mino2 = 0;
498         *o2_p = maxo2;
499         *he_p = maxhe;
500         *o2low_p = mino2;
501 }
502
503 static int total_weight(struct dive *dive)
504 {
505         int i, total_grams = 0;
506
507         if (dive)
508                 for (i=0; i< MAX_WEIGHTSYSTEMS; i++)
509                         total_grams += dive->weightsystem[i].weight.grams;
510         return total_grams;
511 }
512
513 static void weight_data_func(GtkTreeViewColumn *col,
514                              GtkCellRenderer *renderer,
515                              GtkTreeModel *model,
516                              GtkTreeIter *iter,
517                              gpointer data)
518 {
519         int indx, decimals;
520         double value;
521         char buffer[80];
522         struct dive *dive;
523
524         gtk_tree_model_get(model, iter, DIVE_INDEX, &indx, -1);
525         dive = get_dive(indx);
526         value = get_weight_units(total_weight(dive), &decimals, NULL);
527         if (value == 0.0)
528                 *buffer = '\0';
529         else
530                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
531
532         g_object_set(renderer, "text", buffer, NULL);
533 }
534
535 static gint nitrox_sort_func(GtkTreeModel *model,
536         GtkTreeIter *iter_a,
537         GtkTreeIter *iter_b,
538         gpointer user_data)
539 {
540         int index_a, index_b;
541         struct dive *a, *b;
542         int a_o2, b_o2;
543         int a_he, b_he;
544         int a_o2low, b_o2low;
545
546         gtk_tree_model_get(model, iter_a, DIVE_INDEX, &index_a, -1);
547         gtk_tree_model_get(model, iter_b, DIVE_INDEX, &index_b, -1);
548         a = get_dive(index_a);
549         b = get_dive(index_b);
550         get_dive_gas(a, &a_o2, &a_he, &a_o2low);
551         get_dive_gas(b, &b_o2, &b_he, &b_o2low);
552
553         /* Sort by Helium first, O2 second */
554         if (a_he == b_he) {
555                 if (a_o2 == b_o2)
556                         return a_o2low - b_o2low;
557                 return a_o2 - b_o2;
558         }
559         return a_he - b_he;
560 }
561
562 #define UTF8_ELLIPSIS "\xE2\x80\xA6"
563
564 static void nitrox_data_func(GtkTreeViewColumn *col,
565                              GtkCellRenderer *renderer,
566                              GtkTreeModel *model,
567                              GtkTreeIter *iter,
568                              gpointer data)
569 {
570         int idx, o2, he, o2low;
571         char buffer[80];
572         struct dive *dive;
573
574         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
575         if (idx < 0) {
576                 *buffer = '\0';
577                 goto exit;
578         }
579         dive = get_dive(idx);
580         get_dive_gas(dive, &o2, &he, &o2low);
581         o2 = (o2 + 5) / 10;
582         he = (he + 5) / 10;
583         o2low = (o2low + 5) / 10;
584
585         if (he)
586                 snprintf(buffer, sizeof(buffer), "%d/%d", o2, he);
587         else if (o2)
588                 if (o2 == o2low)
589                         snprintf(buffer, sizeof(buffer), "%d", o2);
590                 else
591                         snprintf(buffer, sizeof(buffer), "%d" UTF8_ELLIPSIS "%d", o2low, o2);
592         else
593                 strcpy(buffer, "air");
594 exit:
595         g_object_set(renderer, "text", buffer, NULL);
596 }
597
598 /* Render the SAC data (integer value of "ml / min") */
599 static void sac_data_func(GtkTreeViewColumn *col,
600                           GtkCellRenderer *renderer,
601                           GtkTreeModel *model,
602                           GtkTreeIter *iter,
603                           gpointer data)
604 {
605         int value, idx;
606         const char *fmt;
607         char buffer[16];
608         double sac;
609
610         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_SAC, &value, -1);
611
612         if (idx < 0 || !value) {
613                 *buffer = '\0';
614                 goto exit;
615         }
616
617         sac = value / 1000.0;
618         switch (output_units.volume) {
619         case LITER:
620                 fmt = "%4.1f";
621                 break;
622         case CUFT:
623                 fmt = "%4.2f";
624                 sac = ml_to_cuft(sac * 1000);
625                 break;
626         }
627         snprintf(buffer, sizeof(buffer), fmt, sac);
628 exit:
629         g_object_set(renderer, "text", buffer, NULL);
630 }
631
632 /* Render the OTU data (integer value of "OTU") */
633 static void otu_data_func(GtkTreeViewColumn *col,
634                           GtkCellRenderer *renderer,
635                           GtkTreeModel *model,
636                           GtkTreeIter *iter,
637                           gpointer data)
638 {
639         int value, idx;
640         char buffer[16];
641
642         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_OTU, &value, -1);
643
644         if (idx < 0 || !value)
645                 *buffer = '\0';
646         else
647                 snprintf(buffer, sizeof(buffer), "%d", value);
648
649         g_object_set(renderer, "text", buffer, NULL);
650 }
651
652 /* calculate OTU for a dive */
653 static int calculate_otu(struct dive *dive)
654 {
655         int i;
656         double otu = 0.0;
657
658         for (i = 1; i < dive->samples; i++) {
659                 int t;
660                 double po2;
661                 struct sample *sample = dive->sample + i;
662                 struct sample *psample = sample - 1;
663                 t = sample->time.seconds - psample->time.seconds;
664                 int o2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille;
665                 if (!o2)
666                         o2 = AIR_PERMILLE;
667                 po2 = o2 / 1000.0 * (sample->depth.mm + 10000) / 10000.0;
668                 if (po2 >= 0.5)
669                         otu += pow(po2 - 0.5, 0.83) * t / 30.0;
670         }
671         return otu + 0.5;
672 }
673 /*
674  * Return air usage (in liters).
675  */
676 static double calculate_airuse(struct dive *dive)
677 {
678         double airuse = 0;
679         int i;
680
681         for (i = 0; i < MAX_CYLINDERS; i++) {
682                 pressure_t start, end;
683                 cylinder_t *cyl = dive->cylinder + i;
684                 int size = cyl->type.size.mliter;
685                 double kilo_atm;
686
687                 if (!size)
688                         continue;
689
690                 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
691                 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
692                 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
693
694                 /* Liters of air at 1 atm == milliliters at 1k atm*/
695                 airuse += kilo_atm * size;
696         }
697         return airuse;
698 }
699
700 static int calculate_sac(struct dive *dive)
701 {
702         double airuse, pressure, sac;
703         int duration, i;
704
705         airuse = calculate_airuse(dive);
706         if (!airuse)
707                 return 0;
708         if (!dive->duration.seconds)
709                 return 0;
710
711         /* find and eliminate long surface intervals */
712         duration = dive->duration.seconds;
713         for (i = 0; i < dive->samples; i++) {
714                 if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
715                         int end = i + 1;
716                         while (end < dive->samples && dive->sample[end].depth.mm < 100)
717                                 end++;
718                         /* we only want the actual surface time during a dive */
719                         if (end < dive->samples) {
720                                 end--;
721                                 duration -= dive->sample[end].time.seconds -
722                                                 dive->sample[i].time.seconds;
723                                 i = end + 1;
724                         }
725                 }
726         }
727         /* Mean pressure in atm: 1 atm per 10m */
728         pressure = 1 + (dive->meandepth.mm / 10000.0);
729         sac = airuse / pressure * 60 / duration;
730
731         /* milliliters per minute.. */
732         return sac * 1000;
733 }
734
735 void update_cylinder_related_info(struct dive *dive)
736 {
737         if (dive != NULL) {
738                 dive->sac = calculate_sac(dive);
739                 dive->otu = calculate_otu(dive);
740         }
741 }
742
743 static void get_string(char **str, const char *s)
744 {
745         int len;
746         char *n;
747
748         if (!s)
749                 s = "";
750         len = strlen(s);
751         if (len > 60)
752                 len = 60;
753         n = malloc(len+1);
754         memcpy(n, s, len);
755         n[len] = 0;
756         *str = n;
757 }
758
759 static void get_location(struct dive *dive, char **str)
760 {
761         get_string(str, dive->location);
762 }
763
764 static void get_cylinder(struct dive *dive, char **str)
765 {
766         get_string(str, dive->cylinder[0].type.description);
767 }
768
769 static void get_suit(struct dive *dive, char **str)
770 {
771         get_string(str, dive->suit);
772 }
773
774 /*
775  * Set up anything that could have changed due to editing
776  * of dive information; we need to do this for both models,
777  * so we simply call set_one_dive again with the non-current model
778  */
779 /* forward declaration for recursion */
780 static gboolean set_one_dive(GtkTreeModel *model,
781                              GtkTreePath *path,
782                              GtkTreeIter *iter,
783                              gpointer data);
784
785 static void fill_one_dive(struct dive *dive,
786                           GtkTreeModel *model,
787                           GtkTreeIter *iter)
788 {
789         char *location, *cylinder, *suit;
790         GtkTreeStore *othermodel;
791
792         get_cylinder(dive, &cylinder);
793         get_location(dive, &location);
794         get_suit(dive, &suit);
795
796         gtk_tree_store_set(GTK_TREE_STORE(model), iter,
797                 DIVE_NR, dive->number,
798                 DIVE_LOCATION, location,
799                 DIVE_CYLINDER, cylinder,
800                 DIVE_RATING, dive->rating,
801                 DIVE_SAC, dive->sac,
802                 DIVE_OTU, dive->otu,
803                 DIVE_TOTALWEIGHT, total_weight(dive),
804                 DIVE_SUIT, suit,
805                 -1);
806
807         free(location);
808         free(cylinder);
809         free(suit);
810
811         if (model == GTK_TREE_MODEL(dive_list.treemodel))
812                 othermodel = dive_list.listmodel;
813         else
814                 othermodel = dive_list.treemodel;
815         if (othermodel != dive_list.model)
816                 /* recursive call */
817                 gtk_tree_model_foreach(GTK_TREE_MODEL(othermodel), set_one_dive, dive);
818 }
819
820 static gboolean set_one_dive(GtkTreeModel *model,
821                              GtkTreePath *path,
822                              GtkTreeIter *iter,
823                              gpointer data)
824 {
825         int idx;
826         struct dive *dive;
827
828         /* Get the dive number */
829         gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
830         if (idx < 0)
831                 return FALSE;
832         dive = get_dive(idx);
833         if (!dive)
834                 return TRUE;
835         if (data && dive != data)
836                 return FALSE;
837
838         fill_one_dive(dive, model, iter);
839         return dive == data;
840 }
841
842 void flush_divelist(struct dive *dive)
843 {
844         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
845
846         gtk_tree_model_foreach(model, set_one_dive, dive);
847 }
848
849 void set_divelist_font(const char *font)
850 {
851         PangoFontDescription *font_desc = pango_font_description_from_string(font);
852         gtk_widget_modify_font(dive_list.tree_view, font_desc);
853         pango_font_description_free(font_desc);
854 }
855
856 void update_dive_list_units(void)
857 {
858         const char *unit;
859         GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
860
861         (void) get_depth_units(0, NULL, &unit);
862         gtk_tree_view_column_set_title(dive_list.depth, unit);
863
864         (void) get_temp_units(0, &unit);
865         gtk_tree_view_column_set_title(dive_list.temperature, unit);
866
867         (void) get_weight_units(0, NULL, &unit);
868         gtk_tree_view_column_set_title(dive_list.totalweight, unit);
869
870         gtk_tree_model_foreach(model, set_one_dive, NULL);
871 }
872
873 void update_dive_list_col_visibility(void)
874 {
875         gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
876         gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
877         gtk_tree_view_column_set_visible(dive_list.totalweight, visible_cols.totalweight);
878         gtk_tree_view_column_set_visible(dive_list.suit, visible_cols.suit);
879         gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
880         gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
881         gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
882         return;
883 }
884
885 static void fill_dive_list(void)
886 {
887         int i;
888         GtkTreeIter iter, parent_iter, *parent_ptr = NULL;
889         GtkTreeStore *liststore, *treestore;
890         struct dive *last_trip = NULL;
891         GList *trip;
892         struct dive *dive_trip = NULL;
893
894         /* if we have pre-existing trips, start on the last one */
895         trip = g_list_last(dive_trip_list);
896         if (trip)
897                 dive_trip = DIVE_TRIP(trip);
898
899         treestore = GTK_TREE_STORE(dive_list.treemodel);
900         liststore = GTK_TREE_STORE(dive_list.listmodel);
901
902         i = dive_table.nr;
903         while (--i >= 0) {
904                 struct dive *dive = get_dive(i);
905
906                 /* make sure we display the first date of the trip in previous summary */
907                 if (dive_trip && parent_ptr) {
908                         gtk_tree_store_set(treestore, parent_ptr,
909                                         DIVE_NR, dive_trip->number,
910                                         DIVE_DATE, dive_trip->when,
911                                         DIVE_LOCATION, dive_trip->location,
912                                         -1);
913                 }
914                 /* tripflag defines how dives are handled;
915                  * TF_NONE "not handled yet" - create time based group if autogroup == TRUE
916                  * NO_TRIP "set as no group" - simply leave at top level
917                  * IN_TRIP "use the trip with the largest trip time (when) that is <= this dive"
918                  */
919                 if (UNGROUPED_DIVE(dive)) {
920                         /* first dives that go to the top level */
921                         parent_ptr = NULL;
922                         dive_trip = NULL;
923                 } else if (autogroup && !DIVE_IN_TRIP(dive)) {
924                         if ( ! dive_trip || ! DIVE_FITS_TRIP(dive, dive_trip)) {
925                                 /* allocate new trip - all fields default to 0
926                                    and get filled in further down */
927                                 dive_trip = alloc_dive();
928                                 dive_trip_list = INSERT_TRIP(dive_trip, dive_trip_list);
929                                 trip = FIND_TRIP(dive_trip, dive_trip_list);
930                         }
931                 } else { /* either the dive has a trip or we aren't creating trips */
932                         if (! (trip && DIVE_FITS_TRIP(dive, DIVE_TRIP(trip)))) {
933                                 GList *last_trip = trip;
934                                 trip = PREV_TRIP(trip, dive_trip_list);
935                                 if (! (trip && DIVE_FITS_TRIP(dive, DIVE_TRIP(trip)))) {
936                                         /* we could get here if there are no trips in the XML file
937                                          * and we aren't creating trips, either.
938                                          * Otherwise we need to create a new trip */
939                                         if (autogroup) {
940                                                 dive_trip = alloc_dive();
941                                                 dive_trip_list = INSERT_TRIP(dive_trip, dive_trip_list);
942                                                 trip = FIND_TRIP(dive_trip, dive_trip_list);
943                                         } else {
944                                                 /* let's go back to the last valid trip */
945                                                 trip = last_trip;
946                                         }
947                                 } else {
948                                         dive_trip = trip->data;
949                                         dive_trip->number = 0;
950                                 }
951                         }
952                 }
953                 /* update dive_trip to include this dive, increase number of dives in
954                    the trip and update location if necessary */
955                 if (dive_trip) {
956                         dive->tripflag = IN_TRIP;
957                         dive_trip->number++;
958                         dive_trip->when = dive->when;
959                         if (!dive_trip->location && dive->location)
960                                 dive_trip->location = dive->location;
961                         if (dive_trip != last_trip) {
962                                 last_trip = dive_trip;
963                                 /* create trip entry */
964                                 gtk_tree_store_append(treestore, &parent_iter, NULL);
965                                 parent_ptr = &parent_iter;
966                                 /* a duration of 0 (and negative index) identifies a group */
967                                 gtk_tree_store_set(treestore, parent_ptr,
968                                                 DIVE_INDEX, -1,
969                                                 DIVE_NR, dive_trip->number,
970                                                 DIVE_DATE, dive_trip->when,
971                                                 DIVE_LOCATION, dive_trip->location,
972                                                 DIVE_DURATION, 0,
973                                                 -1);
974                         }
975                 }
976
977                 /* store dive */
978                 update_cylinder_related_info(dive);
979                 gtk_tree_store_append(treestore, &iter, parent_ptr);
980                 gtk_tree_store_set(treestore, &iter,
981                         DIVE_INDEX, i,
982                         DIVE_NR, dive->number,
983                         DIVE_DATE, dive->when,
984                         DIVE_DEPTH, dive->maxdepth,
985                         DIVE_DURATION, dive->duration.seconds,
986                         DIVE_LOCATION, dive->location,
987                         DIVE_RATING, dive->rating,
988                         DIVE_TEMPERATURE, dive->watertemp.mkelvin,
989                         DIVE_SAC, 0,
990                         -1);
991                 gtk_tree_store_append(liststore, &iter, NULL);
992                 gtk_tree_store_set(liststore, &iter,
993                         DIVE_INDEX, i,
994                         DIVE_NR, dive->number,
995                         DIVE_DATE, dive->when,
996                         DIVE_DEPTH, dive->maxdepth,
997                         DIVE_DURATION, dive->duration.seconds,
998                         DIVE_LOCATION, dive->location,
999                         DIVE_RATING, dive->rating,
1000                         DIVE_TEMPERATURE, dive->watertemp.mkelvin,
1001                         DIVE_TOTALWEIGHT, 0,
1002                         DIVE_SUIT, dive->suit,
1003                         DIVE_SAC, 0,
1004                         -1);
1005         }
1006
1007         /* make sure we display the first date of the trip in previous summary */
1008         if (parent_ptr && dive_trip)
1009                 gtk_tree_store_set(treestore, parent_ptr,
1010                                 DIVE_NR, dive_trip->number,
1011                                 DIVE_DATE, dive_trip->when,
1012                                 DIVE_LOCATION, dive_trip->location,
1013                                 -1);
1014         update_dive_list_units();
1015         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
1016                 GtkTreeSelection *selection;
1017
1018                 /* select the last dive (and make sure it's an actual dive that is selected) */
1019                 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &selected_dive, -1);
1020                 first_leaf(GTK_TREE_MODEL(dive_list.model), &iter, &selected_dive);
1021                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1022                 gtk_tree_selection_select_iter(selection, &iter);
1023         }
1024 }
1025
1026 void dive_list_update_dives(void)
1027 {
1028         gtk_tree_store_clear(GTK_TREE_STORE(dive_list.treemodel));
1029         gtk_tree_store_clear(GTK_TREE_STORE(dive_list.listmodel));
1030         fill_dive_list();
1031         repaint_dive();
1032 }
1033
1034 static struct divelist_column {
1035         const char *header;
1036         data_func_t data;
1037         sort_func_t sort;
1038         unsigned int flags;
1039         int *visible;
1040 } dl_column[] = {
1041         [DIVE_NR] = { "#", nr_data_func, NULL, ALIGN_RIGHT | UNSORTABLE },
1042         [DIVE_DATE] = { "Date", date_data_func, NULL, ALIGN_LEFT },
1043         [DIVE_RATING] = { UTF8_BLACKSTAR, star_data_func, NULL, ALIGN_LEFT },
1044         [DIVE_DEPTH] = { "ft", depth_data_func, NULL, ALIGN_RIGHT },
1045         [DIVE_DURATION] = { "min", duration_data_func, NULL, ALIGN_RIGHT },
1046         [DIVE_TEMPERATURE] = { UTF8_DEGREE "F", temperature_data_func, NULL, ALIGN_RIGHT, &visible_cols.temperature },
1047         [DIVE_TOTALWEIGHT] = { "lbs", weight_data_func, NULL, ALIGN_RIGHT, &visible_cols.totalweight },
1048         [DIVE_SUIT] = { "Suit", NULL, NULL, ALIGN_LEFT, &visible_cols.suit },
1049         [DIVE_CYLINDER] = { "Cyl", NULL, NULL, 0, &visible_cols.cylinder },
1050         [DIVE_NITROX] = { "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, nitrox_sort_func, 0, &visible_cols.nitrox },
1051         [DIVE_SAC] = { "SAC", sac_data_func, NULL, 0, &visible_cols.sac },
1052         [DIVE_OTU] = { "OTU", otu_data_func, NULL, 0, &visible_cols.otu },
1053         [DIVE_LOCATION] = { "Location", NULL, NULL, ALIGN_LEFT },
1054 };
1055
1056
1057 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, struct divelist_column *col)
1058 {
1059         int index = col - &dl_column[0];
1060         const char *title = col->header;
1061         data_func_t data_func = col->data;
1062         sort_func_t sort_func = col->sort;
1063         unsigned int flags = col->flags;
1064         int *visible = col->visible;
1065         GtkWidget *tree_view = dl->tree_view;
1066         GtkTreeStore *treemodel = dl->treemodel;
1067         GtkTreeStore *listmodel = dl->listmodel;
1068         GtkTreeViewColumn *ret;
1069
1070         if (visible && !*visible)
1071                 flags |= INVISIBLE;
1072         ret = tree_view_column(tree_view, index, title, data_func, flags);
1073         if (sort_func) {
1074                 /* the sort functions are needed in the corresponding models */
1075                 if (index == DIVE_DATE)
1076                         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(treemodel), index, sort_func, NULL, NULL);
1077                 else
1078                         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(listmodel), index, sort_func, NULL, NULL);
1079         }
1080         return ret;
1081 }
1082
1083 /*
1084  * This is some crazy crap. The only way to get default focus seems
1085  * to be to grab focus as the widget is being shown the first time.
1086  */
1087 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
1088 {
1089         gtk_widget_grab_focus(tree_view);
1090 }
1091
1092 /*
1093  * Double-clicking on a group entry will expand a collapsed group
1094  * and vice versa.
1095  */
1096 static void collapse_expand(GtkTreeView *tree_view, GtkTreePath *path)
1097 {
1098         if (!gtk_tree_view_row_expanded(tree_view, path))
1099                 gtk_tree_view_expand_row(tree_view, path, FALSE);
1100         else
1101                 gtk_tree_view_collapse_row(tree_view, path);
1102
1103 }
1104
1105 /* Double-click on a dive list */
1106 static void row_activated_cb(GtkTreeView *tree_view,
1107                         GtkTreePath *path,
1108                         GtkTreeViewColumn *column,
1109                         gpointer userdata)
1110 {
1111         int index;
1112         GtkTreeIter iter;
1113
1114         if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(dive_list.model), &iter, path))
1115                 return;
1116
1117         gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &index, -1);
1118         /* a negative index is special for the "group by date" entries */
1119         if (index < 0) {
1120                 collapse_expand(tree_view, path);
1121                 return;
1122         }
1123         edit_dive_info(get_dive(index));
1124 }
1125
1126 void add_dive_cb(GtkWidget *menuitem, gpointer data)
1127 {
1128         struct dive *dive;
1129
1130         dive = alloc_dive();
1131         if (add_new_dive(dive)) {
1132                 record_dive(dive);
1133                 report_dives(TRUE);
1134                 return;
1135         }
1136         free(dive);
1137 }
1138
1139 void edit_dive_cb(GtkWidget *menuitem, gpointer data)
1140 {
1141         edit_multi_dive_info(-1);
1142 }
1143
1144 static void expand_all_cb(GtkWidget *menuitem, GtkTreeView *tree_view)
1145 {
1146         gtk_tree_view_expand_all(tree_view);
1147 }
1148
1149 static void collapse_all_cb(GtkWidget *menuitem, GtkTreeView *tree_view)
1150 {
1151         gtk_tree_view_collapse_all(tree_view);
1152 }
1153
1154 static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int button)
1155 {
1156         GtkWidget *menu, *menuitem, *image;
1157         char editlabel[] = "Edit dives";
1158
1159         menu = gtk_menu_new();
1160         menuitem = gtk_image_menu_item_new_with_label("Add dive");
1161         image = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
1162         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
1163         g_signal_connect(menuitem, "activate", G_CALLBACK(add_dive_cb), NULL);
1164         gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1165         if (amount_selected) {
1166                 if (amount_selected == 1)
1167                         editlabel[strlen(editlabel) - 1] = '\0';
1168                 menuitem = gtk_menu_item_new_with_label(editlabel);
1169                 g_signal_connect(menuitem, "activate", G_CALLBACK(edit_dive_cb), model);
1170                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1171         }
1172         menuitem = gtk_menu_item_new_with_label("Expand all");
1173         g_signal_connect(menuitem, "activate", G_CALLBACK(expand_all_cb), tree_view);
1174         gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1175         menuitem = gtk_menu_item_new_with_label("Collapse all");
1176         g_signal_connect(menuitem, "activate", G_CALLBACK(collapse_all_cb), tree_view);
1177         gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1178         gtk_widget_show_all(menu);
1179
1180         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
1181                 button, gtk_get_current_event_time());
1182 }
1183
1184 static void popup_menu_cb(GtkTreeView *tree_view, gpointer userdata)
1185 {
1186         popup_divelist_menu(tree_view, GTK_TREE_MODEL(dive_list.model), 0);
1187 }
1188
1189 static gboolean button_press_cb(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
1190 {
1191         /* Right-click? Bring up the menu */
1192         if (event->type == GDK_BUTTON_PRESS  &&  event->button == 3) {
1193                 popup_divelist_menu(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(dive_list.model), 3);
1194                 return TRUE;
1195         }
1196         return FALSE;
1197 }
1198
1199 /* we need to have a temporary copy of the selected dives while
1200    switching model as the selection_cb function keeps getting called
1201    when gtk_tree_selection_select_path is called.  We also need to
1202    keep copies of the sort order so we can restore that as well after
1203    switching models. */
1204 static gboolean second_call = FALSE;
1205 static GtkSortType sortorder[] = { [0 ... DIVELIST_COLUMNS - 1] = GTK_SORT_DESCENDING, };
1206 static int lastcol = DIVE_DATE;
1207
1208 /* Check if this dive was selected previously and select it again in the new model;
1209  * This is used after we switch models to maintain consistent selections.
1210  * We always return FALSE to iterate through all dives */
1211 static gboolean set_selected(GtkTreeModel *model, GtkTreePath *path,
1212                                 GtkTreeIter *iter, gpointer data)
1213 {
1214         GtkTreeSelection *selection = GTK_TREE_SELECTION(data);
1215         int idx, selected;
1216         struct dive *dive;
1217
1218         gtk_tree_model_get(model, iter,
1219                 DIVE_INDEX, &idx,
1220                 -1);
1221         if (idx < 0) {
1222                 GtkTreeIter child;
1223                 if (gtk_tree_model_iter_children(model, &child, iter))
1224                         gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
1225         }
1226         dive = get_dive(idx);
1227         selected = dive && dive->selected;
1228         if (selected) {
1229                 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(dive_list.tree_view), path);
1230                 gtk_tree_selection_select_path(selection, path);
1231         }
1232         return FALSE;
1233
1234 }
1235
1236 static void update_column_and_order(int colid)
1237 {
1238         /* Careful: the index into treecolumns is off by one as we don't have a
1239            tree_view column for DIVE_INDEX */
1240         GtkTreeViewColumn **treecolumns = &dive_list.nr;
1241
1242         /* this will trigger a second call into sort_column_change_cb,
1243            so make sure we don't start an infinite recursion... */
1244         second_call = TRUE;
1245         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dive_list.model), colid, sortorder[colid]);
1246         gtk_tree_view_column_set_sort_order(treecolumns[colid - 1], sortorder[colid]);
1247         second_call = FALSE;
1248 }
1249
1250 /* If the sort column is date (default), show the tree model.
1251    For every other sort column only show the list model.
1252    If the model changed, inform the new model of the chosen sort column and make
1253    sure the same dives are still selected.
1254
1255    The challenge with this function is that once we change the model
1256    we also need to change the sort column again (as it was changed in
1257    the other model) and that causes this function to be called
1258    recursively - so we need to catch that.
1259 */
1260 static void sort_column_change_cb(GtkTreeSortable *treeview, gpointer data)
1261 {
1262         int colid;
1263         GtkSortType order;
1264         GtkTreeStore *currentmodel = dive_list.model;
1265
1266         if (second_call)
1267                 return;
1268
1269         gtk_tree_sortable_get_sort_column_id(treeview, &colid, &order);
1270         if(colid == lastcol) {
1271                 /* we just changed sort order */
1272                 sortorder[colid] = order;
1273                 return;
1274         } else {
1275                 lastcol = colid;
1276         }
1277         if(colid == DIVE_DATE)
1278                 dive_list.model = dive_list.treemodel;
1279         else
1280                 dive_list.model = dive_list.listmodel;
1281         if (dive_list.model != currentmodel) {
1282                 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1283
1284                 gtk_tree_view_set_model(GTK_TREE_VIEW(dive_list.tree_view), GTK_TREE_MODEL(dive_list.model));
1285                 update_column_and_order(colid);
1286                 gtk_tree_model_foreach(GTK_TREE_MODEL(dive_list.model), set_selected, selection);
1287         } else {
1288                 if (order != sortorder[colid]) {
1289                         update_column_and_order(colid);
1290                 }
1291         }
1292 }
1293
1294 GtkWidget *dive_list_create(void)
1295 {
1296         GtkTreeSelection  *selection;
1297
1298         dive_list.listmodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1299                                 G_TYPE_INT,                     /* index */
1300                                 G_TYPE_INT,                     /* nr */
1301                                 G_TYPE_INT,                     /* Date */
1302                                 G_TYPE_INT,                     /* Star rating */
1303                                 G_TYPE_INT,                     /* Depth */
1304                                 G_TYPE_INT,                     /* Duration */
1305                                 G_TYPE_INT,                     /* Temperature */
1306                                 G_TYPE_INT,                     /* Total weight */
1307                                 G_TYPE_STRING,                  /* Suit */
1308                                 G_TYPE_STRING,                  /* Cylinder */
1309                                 G_TYPE_INT,                     /* Nitrox */
1310                                 G_TYPE_INT,                     /* SAC */
1311                                 G_TYPE_INT,                     /* OTU */
1312                                 G_TYPE_STRING                   /* Location */
1313                                 );
1314         dive_list.treemodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1315                                 G_TYPE_INT,                     /* index */
1316                                 G_TYPE_INT,                     /* nr */
1317                                 G_TYPE_INT,                     /* Date */
1318                                 G_TYPE_INT,                     /* Star rating */
1319                                 G_TYPE_INT,                     /* Depth */
1320                                 G_TYPE_INT,                     /* Duration */
1321                                 G_TYPE_INT,                     /* Temperature */
1322                                 G_TYPE_INT,                     /* Total weight */
1323                                 G_TYPE_STRING,                  /* Suit */
1324                                 G_TYPE_STRING,                  /* Cylinder */
1325                                 G_TYPE_INT,                     /* Nitrox */
1326                                 G_TYPE_INT,                     /* SAC */
1327                                 G_TYPE_INT,                     /* OTU */
1328                                 G_TYPE_STRING                   /* Location */
1329                                 );
1330         dive_list.model = dive_list.treemodel;
1331         dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
1332         set_divelist_font(divelist_font);
1333
1334         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1335
1336         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
1337         gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
1338
1339         dive_list.nr = divelist_column(&dive_list, dl_column + DIVE_NR);
1340         dive_list.date = divelist_column(&dive_list, dl_column + DIVE_DATE);
1341         dive_list.stars = divelist_column(&dive_list, dl_column + DIVE_RATING);
1342         dive_list.depth = divelist_column(&dive_list, dl_column + DIVE_DEPTH);
1343         dive_list.duration = divelist_column(&dive_list, dl_column + DIVE_DURATION);
1344         dive_list.temperature = divelist_column(&dive_list, dl_column + DIVE_TEMPERATURE);
1345         dive_list.totalweight = divelist_column(&dive_list, dl_column + DIVE_TOTALWEIGHT);
1346         dive_list.suit = divelist_column(&dive_list, dl_column + DIVE_SUIT);
1347         dive_list.cylinder = divelist_column(&dive_list, dl_column + DIVE_CYLINDER);
1348         dive_list.nitrox = divelist_column(&dive_list, dl_column + DIVE_NITROX);
1349         dive_list.sac = divelist_column(&dive_list, dl_column + DIVE_SAC);
1350         dive_list.otu = divelist_column(&dive_list, dl_column + DIVE_OTU);
1351         dive_list.location = divelist_column(&dive_list, dl_column + DIVE_LOCATION);
1352
1353         fill_dive_list();
1354
1355         g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
1356                                           "search-column", DIVE_LOCATION,
1357                                           "rules-hint", TRUE,
1358                                           NULL);
1359
1360         g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
1361         g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), NULL);
1362         g_signal_connect(dive_list.tree_view, "row-expanded", G_CALLBACK(row_expanded_cb), NULL);
1363         g_signal_connect(dive_list.tree_view, "row-collapsed", G_CALLBACK(row_collapsed_cb), NULL);
1364         g_signal_connect(dive_list.tree_view, "button-press-event", G_CALLBACK(button_press_cb), NULL);
1365         g_signal_connect(dive_list.tree_view, "popup-menu", G_CALLBACK(popup_menu_cb), NULL);
1366         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
1367         g_signal_connect(dive_list.listmodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1368         g_signal_connect(dive_list.treemodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1369
1370         gtk_tree_selection_set_select_function(selection, modify_selection_cb, NULL, NULL);
1371
1372         dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
1373         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
1374                                GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1375         gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
1376
1377         dive_list.changed = 0;
1378
1379         return dive_list.container_widget;
1380 }
1381
1382 void mark_divelist_changed(int changed)
1383 {
1384         dive_list.changed = changed;
1385 }
1386
1387 int unsaved_changes()
1388 {
1389         return dive_list.changed;
1390 }