]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Fix profile and average depth for freedives
[ext/subsurface.git] / equipment.c
1 /* equipment.c */
2 /* creates the UI for the equipment page -
3  * controlled through the following interfaces:
4  *
5  * void show_dive_equipment(struct dive *dive, int w_idx)
6  *
7  * called from gtk-ui:
8  * GtkWidget *equipment_widget(int w_idx)
9  */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include <time.h>
15
16 #include "dive.h"
17 #include "display.h"
18 #include "display-gtk.h"
19 #include "divelist.h"
20
21 static GtkListStore *cylinder_model, *weightsystem_model;
22
23 enum {
24         CYL_DESC,
25         CYL_SIZE,
26         CYL_WORKP,
27         CYL_STARTP,
28         CYL_ENDP,
29         CYL_O2,
30         CYL_HE,
31         CYL_COLUMNS
32 };
33
34 enum {
35         WS_DESC,
36         WS_WEIGHT,
37         WS_COLUMNS
38 };
39
40 struct equipment_list {
41         int max_index;
42         GtkListStore *model;
43         GtkTreeView *tree_view;
44         GtkWidget *edit, *add, *del;
45 };
46
47 static struct equipment_list cylinder_list[2], weightsystem_list[2];
48
49
50 struct cylinder_widget {
51         int index, changed;
52         const char *name;
53         GtkWidget *hbox;
54         GtkComboBox *description;
55         GtkSpinButton *size, *pressure;
56         GtkWidget *start, *end, *pressure_button;
57         GtkWidget *o2, *he, *gasmix_button;
58 };
59
60 struct ws_widget {
61         int index, changed;
62         const char *name;
63         GtkWidget *hbox;
64         GtkComboBox *description;
65         GtkSpinButton *weight;
66 };
67
68 /* we want bar - so let's not use our unit functions */
69 static int convert_pressure(int mbar, double *p)
70 {
71         int decimals = 1;
72         double pressure;
73
74         if (output_units.pressure == PSI) {
75                 pressure = mbar_to_PSI(mbar);
76                 decimals = 0;
77         } else {
78                 pressure = mbar / 1000.0;
79         }
80
81         *p = pressure;
82         return decimals;
83 }
84
85 static void convert_volume_pressure(int ml, int mbar, double *v, double *p)
86 {
87         double volume, pressure;
88
89         volume = ml / 1000.0;
90         if (mbar) {
91                 if (output_units.volume == CUFT) {
92                         volume = ml_to_cuft(ml);
93                         volume *= bar_to_atm(mbar / 1000.0);
94                 }
95
96                 if (output_units.pressure == PSI) {
97                         pressure = mbar_to_PSI(mbar);
98                 } else
99                         pressure = mbar / 1000.0;
100         }
101         *v = volume;
102         *p = pressure;
103 }
104
105 static int convert_weight(int grams, double *m)
106 {
107         int decimals = 1; /* not sure - do people do less than whole lbs/kg ? */
108         double weight;
109
110         if (output_units.weight == LBS)
111                 weight = grams_to_lbs(grams);
112         else
113                 weight = grams / 1000.0;
114         *m = weight;
115         return decimals;
116 }
117
118 static void set_cylinder_type_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
119 {
120         double volume, pressure;
121
122         convert_volume_pressure(ml, mbar, &volume, &pressure);
123         gtk_spin_button_set_value(cylinder->size, volume);
124         gtk_spin_button_set_value(cylinder->pressure, pressure);
125 }
126
127 static void set_cylinder_pressure_spinbuttons(struct cylinder_widget *cylinder, cylinder_t *cyl)
128 {
129         int set;
130         unsigned int start, end;
131         double pressure;
132
133         start = cyl->start.mbar;
134         end = cyl->end.mbar;
135         set = start || end;
136         if (!set) {
137                 start = cyl->sample_start.mbar;
138                 end = cyl->sample_end.mbar;
139         }
140         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button), set);
141         gtk_widget_set_sensitive(cylinder->start, set);
142         gtk_widget_set_sensitive(cylinder->end, set);
143
144         convert_pressure(start, &pressure);
145         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->start), pressure);
146         convert_pressure(end, &pressure);
147         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->end), pressure);
148 }
149
150 static void set_weight_weight_spinbutton(struct ws_widget *ws_widget, int grams)
151 {
152         double weight;
153
154         convert_weight(grams, &weight);
155         gtk_spin_button_set_value(ws_widget->weight, weight);
156 }
157
158 /*
159  * The gtk_tree_model_foreach() interface is bad. It could have
160  * returned whether the callback ever returned true
161  */
162 static GtkTreeIter *found_match = NULL;
163 static GtkTreeIter match_iter;
164
165 static gboolean match_desc(GtkTreeModel *model, GtkTreePath *path,
166                         GtkTreeIter *iter, gpointer data)
167 {
168         int match;
169         gchar *name;
170         const char *desc = data;
171
172         gtk_tree_model_get(model, iter, 0, &name, -1);
173         match = !strcmp(desc, name);
174         g_free(name);
175         if (match) {
176                 match_iter = *iter;
177                 found_match = &match_iter;
178         }
179         return match;
180 }
181
182 static int get_active_item(GtkComboBox *combo_box, GtkTreeIter *iter, GtkListStore *model)
183 {
184         char *desc;
185
186         if (gtk_combo_box_get_active_iter(combo_box, iter))
187                 return TRUE;
188
189         desc = gtk_combo_box_get_active_text(combo_box);
190
191         found_match = NULL;
192         gtk_tree_model_foreach(GTK_TREE_MODEL(model), match_desc, (void *)desc);
193
194         g_free(desc);
195         if (!found_match)
196                 return FALSE;
197
198         *iter = *found_match;
199         gtk_combo_box_set_active_iter(combo_box, iter);
200         return TRUE;
201 }
202
203 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
204 {
205         GtkTreeIter iter;
206         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
207         int ml, mbar;
208         struct cylinder_widget *cylinder = data;
209         cylinder_t *cyl = current_dive->cylinder + cylinder->index;
210
211         /* Did the user set it to some non-standard value? */
212         if (!get_active_item(combo_box, &iter, cylinder_model)) {
213                 cylinder->changed = 1;
214                 return;
215         }
216
217         /*
218          * We get "change" signal callbacks just because we set
219          * the description by hand. Whatever. So ignore them if
220          * they are no-ops.
221          */
222         if (!cylinder->changed && cyl->type.description) {
223                 int same;
224                 char *desc = gtk_combo_box_get_active_text(combo_box);
225
226                 same = !strcmp(desc, cyl->type.description);
227                 g_free(desc);
228                 if (same)
229                         return;
230         }
231         cylinder->changed = 1;
232
233         gtk_tree_model_get(model, &iter,
234                 CYL_SIZE, &ml,
235                 CYL_WORKP, &mbar,
236                 -1);
237
238         set_cylinder_type_spinbuttons(cylinder, ml, mbar);
239 }
240
241 static void weight_cb(GtkComboBox *combo_box, gpointer data)
242 {
243         GtkTreeIter iter;
244         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
245         int weight;
246         struct ws_widget *ws_widget = data;
247         weightsystem_t *ws = current_dive->weightsystem + ws_widget->index;
248
249         /* Did the user set it to some non-standard value? */
250         if (!get_active_item(combo_box, &iter, weightsystem_model)) {
251                 ws_widget->changed = 1;
252                 return;
253         }
254
255         /*
256          * We get "change" signal callbacks just because we set
257          * the description by hand. Whatever. So ignore them if
258          * they are no-ops.
259          */
260         if (!ws_widget->changed && ws->description) {
261                 int same;
262                 char *desc = gtk_combo_box_get_active_text(combo_box);
263
264                 same = !strcmp(desc, ws->description);
265                 g_free(desc);
266                 if (same)
267                         return;
268         }
269         ws_widget->changed = 1;
270
271         gtk_tree_model_get(model, &iter,
272                 WS_WEIGHT, &weight,
273                 -1);
274
275         set_weight_weight_spinbutton(ws_widget, weight);
276 }
277
278 static GtkTreeIter *add_cylinder_type(const char *desc, int ml, int mbar, GtkTreeIter *iter)
279 {
280         GtkTreeModel *model;
281
282         /* Don't even bother adding stuff without a size */
283         if (!ml)
284                 return NULL;
285
286         found_match = NULL;
287         model = GTK_TREE_MODEL(cylinder_model);
288         gtk_tree_model_foreach(model, match_desc, (void *)desc);
289
290         if (!found_match) {
291                 GtkListStore *store = GTK_LIST_STORE(model);
292
293                 gtk_list_store_append(store, iter);
294                 gtk_list_store_set(store, iter,
295                         0, desc,
296                         1, ml,
297                         2, mbar,
298                         -1);
299                 return iter;
300         }
301         return found_match;
302 }
303
304 static GtkTreeIter *add_weightsystem_type(const char *desc, int weight, GtkTreeIter *iter)
305 {
306         GtkTreeModel *model;
307
308         found_match = NULL;
309         model = GTK_TREE_MODEL(weightsystem_model);
310         gtk_tree_model_foreach(model, match_desc, (void *)desc);
311
312         if (found_match) {
313                 gtk_list_store_set(GTK_LIST_STORE(model), found_match,
314                                 WS_WEIGHT, weight,
315                                 -1);
316         } else if (desc && desc[0]) {
317                 gtk_list_store_append(GTK_LIST_STORE(model), iter);
318                 gtk_list_store_set(GTK_LIST_STORE(model), iter,
319                         WS_DESC, desc,
320                         WS_WEIGHT, weight,
321                         -1);
322                 return iter;
323         }
324         return found_match;
325 }
326
327 /*
328  * When adding a dive, we'll add all the pre-existing cylinder
329  * information from that dive to our cylinder model.
330  */
331 void add_cylinder_description(cylinder_type_t *type)
332 {
333         GtkTreeIter iter;
334         const char *desc;
335         unsigned int size, workp;
336
337         desc = type->description;
338         if (!desc)
339                 return;
340         size = type->size.mliter;
341         workp = type->workingpressure.mbar;
342         add_cylinder_type(desc, size, workp, &iter);
343 }
344
345 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
346 {
347         GtkTreeIter iter, *match;
348
349         cylinder->name = desc;
350         match = add_cylinder_type(desc, ml, mbar, &iter);
351         if (match)
352                 gtk_combo_box_set_active_iter(cylinder->description, match);
353 }
354
355 void add_weightsystem_description(weightsystem_t *weightsystem)
356 {
357         GtkTreeIter iter;
358         const char *desc;
359         unsigned int weight;
360
361         desc = weightsystem->description;
362         if (!desc)
363                 return;
364         weight = weightsystem->weight.grams;
365         add_weightsystem_type(desc, weight, &iter);
366 }
367
368 static void add_weightsystem(struct ws_widget *ws_widget, const char *desc, int weight)
369 {
370         GtkTreeIter iter, *match;
371
372         ws_widget->name = desc;
373         match = add_weightsystem_type(desc, weight, &iter);
374         if (match)
375                 gtk_combo_box_set_active_iter(ws_widget->description, match);
376 }
377
378 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
379 {
380         const char *desc;
381         int ml, mbar;
382         int gasmix;
383         double o2, he;
384
385         /* Don't show uninitialized cylinder widgets */
386         if (!cylinder->description)
387                 return;
388
389         desc = cyl->type.description;
390         if (!desc)
391                 desc = "";
392         ml = cyl->type.size.mliter;
393         mbar = cyl->type.workingpressure.mbar;
394         add_cylinder(cylinder, desc, ml, mbar);
395
396         set_cylinder_type_spinbuttons(cylinder,
397                 cyl->type.size.mliter, cyl->type.workingpressure.mbar);
398         set_cylinder_pressure_spinbuttons(cylinder, cyl);
399
400         gasmix = cyl->gasmix.o2.permille || cyl->gasmix.he.permille;
401         gtk_widget_set_sensitive(cylinder->o2, gasmix);
402         gtk_widget_set_sensitive(cylinder->he, gasmix);
403         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), gasmix);
404
405         o2 = cyl->gasmix.o2.permille / 10.0;
406         he = cyl->gasmix.he.permille / 10.0;
407         if (!o2)
408                 o2 = AIR_PERMILLE / 10.0;
409         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
410         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->he), he);
411 }
412
413 static void show_weightsystem(weightsystem_t *ws, struct ws_widget *weightsystem_widget)
414 {
415         const char *desc;
416         int grams;
417
418         /* Don't show uninitialized widgets */
419         if (!weightsystem_widget->description)
420                 return;
421
422         desc = ws->description;
423         if (!desc)
424                 desc = "";
425         grams = ws->weight.grams;
426         add_weightsystem(weightsystem_widget, desc, grams);
427
428         set_weight_weight_spinbutton(weightsystem_widget, ws->weight.grams);
429 }
430
431 gboolean cylinder_none(void *_data)
432 {
433         cylinder_t *cyl = _data;
434         return  !cyl->type.size.mliter &&
435                 !cyl->type.workingpressure.mbar &&
436                 !cyl->type.description &&
437                 !cyl->gasmix.o2.permille &&
438                 !cyl->gasmix.he.permille &&
439                 !cyl->sample_start.mbar &&
440                 !cyl->sample_end.mbar &&
441                 !cyl->start.mbar &&
442                 !cyl->end.mbar;
443 }
444
445 gboolean no_cylinders(cylinder_t *cyl)
446 {
447         int i;
448
449         for (i = 0; i < MAX_CYLINDERS; i++)
450                 if (!cylinder_none(cyl + i))
451                         return FALSE;
452         return TRUE;
453 }
454
455 /* descriptions are equal if they are both NULL or both non-NULL
456    and the same text */
457 gboolean description_equal(const char *desc1, const char *desc2)
458 {
459                 return ((! desc1 && ! desc2) ||
460                         (desc1 && desc2 && strcmp(desc1, desc2) == 0));
461 }
462
463 /* when checking for the same cylinder we want the size and description to match
464    but don't compare the start and end pressures, nor the Nitrox/He values */
465 static gboolean one_cylinder_equal(cylinder_t *cyl1, cylinder_t *cyl2)
466 {
467         return cyl1->type.size.mliter == cyl2->type.size.mliter &&
468                 cyl1->type.workingpressure.mbar == cyl2->type.workingpressure.mbar &&
469                 description_equal(cyl1->type.description, cyl2->type.description);
470 }
471
472 gboolean cylinders_equal(cylinder_t *cyl1, cylinder_t *cyl2)
473 {
474         int i;
475
476         for (i = 0; i < MAX_CYLINDERS; i++)
477                 if (!one_cylinder_equal(cyl1 + i, cyl2 + i))
478                         return FALSE;
479         return TRUE;
480 }
481
482 /* copy size and description of all cylinders from cyl1 to cyl2 */
483 void copy_cylinders(cylinder_t *cyl1, cylinder_t *cyl2)
484 {
485         int i;
486
487         for (i = 0; i < MAX_CYLINDERS; i++) {
488                 cyl2[i].type.size.mliter = cyl1[i].type.size.mliter;
489                 cyl2[i].type.workingpressure.mbar = cyl1[i].type.workingpressure.mbar;
490                 if (cyl1[i].type.description)
491                         cyl2[i].type.description = strdup(cyl1[i].type.description);
492                 else
493                         cyl2[i].type.description = NULL;
494         }
495 }
496
497 static gboolean weightsystem_none(void *_data)
498 {
499         weightsystem_t *ws = _data;
500         return !ws->weight.grams && !ws->description;
501 }
502
503 gboolean no_weightsystems(weightsystem_t *ws)
504 {
505         int i;
506
507         for (i = 0; i < MAX_WEIGHTSYSTEMS; i++)
508                 if (!weightsystem_none(ws + i))
509                         return FALSE;
510         return TRUE;
511 }
512
513 static gboolean one_weightsystem_equal(weightsystem_t *ws1, weightsystem_t *ws2)
514 {
515         return ws1->weight.grams == ws2->weight.grams &&
516                 description_equal(ws1->description, ws2->description);
517 }
518
519 gboolean weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2)
520 {
521         int i;
522
523         for (i = 0; i < MAX_WEIGHTSYSTEMS; i++)
524                 if (!one_weightsystem_equal(ws1 + i, ws2 + i))
525                         return FALSE;
526         return TRUE;
527 }
528
529 static void set_one_cylinder(void *_data, GtkListStore *model, GtkTreeIter *iter)
530 {
531         cylinder_t *cyl = _data;
532         unsigned int start, end;
533
534         start = cyl->start.mbar ? : cyl->sample_start.mbar;
535         end = cyl->end.mbar ? : cyl->sample_end.mbar;
536         gtk_list_store_set(model, iter,
537                 CYL_DESC, cyl->type.description ? : "",
538                 CYL_SIZE, cyl->type.size.mliter,
539                 CYL_WORKP, cyl->type.workingpressure.mbar,
540                 CYL_STARTP, start,
541                 CYL_ENDP, end,
542                 CYL_O2, cyl->gasmix.o2.permille,
543                 CYL_HE, cyl->gasmix.he.permille,
544                 -1);
545 }
546
547 static void set_one_weightsystem(void *_data, GtkListStore *model, GtkTreeIter *iter)
548 {
549         weightsystem_t *ws = _data;
550
551         gtk_list_store_set(model, iter,
552                 WS_DESC, ws->description ? : "unspecified",
553                 WS_WEIGHT, ws->weight.grams,
554                 -1);
555 }
556
557 static void *cyl_ptr(struct dive *dive, int idx)
558 {
559         if (idx < 0 || idx >= MAX_CYLINDERS)
560                 return NULL;
561         return &dive->cylinder[idx];
562 }
563
564 static void *ws_ptr(struct dive *dive, int idx)
565 {
566         if (idx < 0 || idx >= MAX_WEIGHTSYSTEMS)
567                 return NULL;
568         return &dive->weightsystem[idx];
569 }
570
571 static void show_equipment(struct dive *dive, int max,
572                         struct equipment_list *equipment_list,
573                         void*(*ptr_function)(struct dive*, int),
574                         gboolean(*none_function)(void *),
575                         void(*set_one_function)(void*, GtkListStore*, GtkTreeIter *))
576 {
577         int i, used;
578         void *data;
579         GtkTreeIter iter;
580         GtkListStore *model = equipment_list->model;
581
582         gtk_list_store_clear(model);
583         used = max;
584         do {
585                 data = ptr_function(dive, used-1);
586                 if (!none_function(data))
587                         break;
588         } while (--used);
589
590         equipment_list->max_index = used;
591
592         gtk_widget_set_sensitive(equipment_list->edit, 0);
593         gtk_widget_set_sensitive(equipment_list->del, 0);
594         gtk_widget_set_sensitive(equipment_list->add, used < max);
595
596         for (i = 0; i < used; i++) {
597                 data = ptr_function(dive, i);
598                 gtk_list_store_append(model, &iter);
599                 set_one_function(data, model, &iter);
600         }
601 }
602
603 void show_dive_equipment(struct dive *dive, int w_idx)
604 {
605         show_equipment(dive, MAX_CYLINDERS, &cylinder_list[w_idx],
606                 &cyl_ptr, &cylinder_none, &set_one_cylinder);
607         show_equipment(dive, MAX_WEIGHTSYSTEMS, &weightsystem_list[w_idx],
608                 &ws_ptr, &weightsystem_none, &set_one_weightsystem);
609 }
610
611 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
612 {
613         GtkWidget *frame, *hbox, *button;
614
615         frame = gtk_frame_new(name);
616         gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
617
618         hbox = gtk_hbox_new(FALSE, 3);
619         gtk_container_add(GTK_CONTAINER(frame), hbox);
620
621         button = gtk_spin_button_new_with_range(min, max, incr);
622         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
623
624         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
625
626         return button;
627 }
628
629 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
630                 double volume, double pressure, double start, double end, int o2, int he)
631 {
632         int mbar, ml;
633
634         if (output_units.pressure == PSI) {
635                 pressure = psi_to_bar(pressure);
636                 start = psi_to_bar(start);
637                 end = psi_to_bar(end);
638         }
639
640         if (pressure && output_units.volume == CUFT) {
641                 volume = cuft_to_l(volume);
642                 volume /= bar_to_atm(pressure);
643         }
644
645         ml = volume * 1000 + 0.5;
646         mbar = pressure * 1000 + 0.5;
647
648         /* Ignore obviously crazy He values */
649         if (o2 + he > 1000)
650                 he = 0;
651
652         /* We have a rule that normal air is all zeroes */
653         if (!he && o2 > 208 && o2 < 211)
654                 o2 = 0;
655
656         cyl->type.description = desc;
657         cyl->type.size.mliter = ml;
658         cyl->type.workingpressure.mbar = mbar;
659         cyl->start.mbar = start * 1000 + 0.5;
660         cyl->end.mbar = end * 1000 + 0.5;
661         cyl->gasmix.o2.permille = o2;
662         cyl->gasmix.he.permille = he;
663
664         /*
665          * Also, insert it into the model if it doesn't already exist
666          */
667         add_cylinder(cylinder, desc, ml, mbar);
668 }
669
670 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
671 {
672         const gchar *desc;
673         GtkComboBox *box;
674         double volume, pressure, start, end;
675         int o2, he;
676
677         /* Ignore uninitialized cylinder widgets */
678         box = cylinder->description;
679         if (!box)
680                 return;
681
682         desc = gtk_combo_box_get_active_text(box);
683         volume = gtk_spin_button_get_value(cylinder->size);
684         pressure = gtk_spin_button_get_value(cylinder->pressure);
685         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button))) {
686                 start = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->start));
687                 end = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->end));
688         } else {
689                 start = end = 0;
690         }
691         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button))) {
692                 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
693                 he = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->he))*10 + 0.5;
694         } else {
695                 o2 = 0;
696                 he = 0;
697         }
698         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2, he);
699 }
700
701 static void record_weightsystem_changes(weightsystem_t *ws, struct ws_widget *weightsystem_widget)
702 {
703         const gchar *desc;
704         GtkComboBox *box;
705         int grams;
706         double value;
707         GtkTreeIter iter;
708
709         /* Ignore uninitialized cylinder widgets */
710         box = weightsystem_widget->description;
711         if (!box)
712                 return;
713
714         desc = gtk_combo_box_get_active_text(box);
715         value = gtk_spin_button_get_value(weightsystem_widget->weight);
716
717         if (output_units.weight == LBS)
718                 grams = lbs_to_grams(value);
719         else
720                 grams = value * 1000;
721         ws->weight.grams = grams;
722         ws->description = desc;
723         add_weightsystem_type(desc, grams, &iter);
724 }
725
726 /*
727  * We hardcode the most common standard cylinders,
728  * we should pick up any other names from the dive
729  * logs directly.
730  */
731 static struct tank_info {
732         const char *name;
733         int cuft, ml, psi, bar;
734 } tank_info[100] = {
735         /* Need an empty entry for the no-cylinder case */
736         { "", },
737
738         /* Size-only metric cylinders */
739         { "10.0 l", .ml = 10000 },
740         { "11.1 l", .ml = 11100 },
741
742         /* Most common AL cylinders */
743         { "AL50",  .cuft =  50, .psi = 3000 },
744         { "AL63",  .cuft =  63, .psi = 3000 },
745         { "AL72",  .cuft =  72, .psi = 3000 },
746         { "AL80",  .cuft =  80, .psi = 3000 },
747         { "AL100", .cuft = 100, .psi = 3300 },
748
749         /* Somewhat common LP steel cylinders */
750         { "LP85",  .cuft =  85, .psi = 2640 },
751         { "LP95",  .cuft =  95, .psi = 2640 },
752         { "LP108", .cuft = 108, .psi = 2640 },
753         { "LP121", .cuft = 121, .psi = 2640 },
754
755         /* Somewhat common HP steel cylinders */
756         { "HP65",  .cuft =  65, .psi = 3442 },
757         { "HP80",  .cuft =  80, .psi = 3442 },
758         { "HP100", .cuft = 100, .psi = 3442 },
759         { "HP119", .cuft = 119, .psi = 3442 },
760         { "HP130", .cuft = 130, .psi = 3442 },
761
762         /* Common European steel cylinders */
763         { "10L 300 bar",  .ml = 10000, .bar = 300 },
764         { "12L 200 bar",  .ml = 12000, .bar = 200 },
765         { "12L 232 bar",  .ml = 12000, .bar = 232 },
766         { "12L 300 bar",  .ml = 12000, .bar = 300 },
767         { "15L 200 bar",  .ml = 15000, .bar = 200 },
768         { "15L 232 bar",  .ml = 15000, .bar = 232 },
769         { "D7 300 bar",   .ml = 14000, .bar = 300 },
770         { "D8.5 232 bar", .ml = 17000, .bar = 232 },
771         { "D12 232 bar",  .ml = 24000, .bar = 232 },
772
773         /* We'll fill in more from the dive log dynamically */
774         { NULL, }
775 };
776
777 static void fill_tank_list(GtkListStore *store)
778 {
779         GtkTreeIter iter;
780         struct tank_info *info = tank_info;
781
782         for (info = tank_info ; info->name; info++) {
783                 int ml = info->ml;
784                 int cuft = info->cuft;
785                 int psi = info->psi;
786                 int mbar;
787                 double bar = info->bar;
788
789                 if (psi && bar)
790                         goto bad_tank_info;
791                 if (ml && cuft)
792                         goto bad_tank_info;
793                 if (cuft && !psi)
794                         goto bad_tank_info;
795
796                 /* Is it in cuft and psi? */
797                 if (psi)
798                         bar = psi_to_bar(psi);
799
800                 if (cuft) {
801                         double airvolume = cuft_to_l(cuft) * 1000.0;
802                         double atm = bar_to_atm(bar);
803
804                         ml = airvolume / atm + 0.5;
805                 }
806
807                 mbar = bar * 1000 + 0.5;
808
809                 gtk_list_store_append(store, &iter);
810                 gtk_list_store_set(store, &iter,
811                         0, info->name,
812                         1, ml,
813                         2, mbar,
814                         -1);
815                 continue;
816
817 bad_tank_info:
818                 fprintf(stderr, "Bad tank info for '%s'\n", info->name);
819         }
820 }
821
822 /*
823  * We hardcode the most common weight system types
824  * This is a bit odd as the weight system types don't usually encode weight
825  */
826 static struct ws_info {
827         const char *name;
828         int grams;
829 } ws_info[100] = {
830         { "integrated", 0 },
831         { "belt", 0 },
832         { "ankle", 0 },
833         { "bar", 0 },
834         { "clip-on", 0 },
835 };
836
837 static void fill_ws_list(GtkListStore *store)
838 {
839         GtkTreeIter iter;
840         struct ws_info *info = ws_info;
841
842         while (info->name) {
843                 gtk_list_store_append(store, &iter);
844                 gtk_list_store_set(store, &iter,
845                         0, info->name,
846                         1, info->grams,
847                         -1);
848                 info++;
849         }
850 }
851
852 static void gasmix_cb(GtkToggleButton *button, gpointer data)
853 {
854         struct cylinder_widget *cylinder = data;
855         int state;
856
857         state = gtk_toggle_button_get_active(button);
858         gtk_widget_set_sensitive(cylinder->o2, state);
859         gtk_widget_set_sensitive(cylinder->he, state);
860 }
861
862 static void pressure_cb(GtkToggleButton *button, gpointer data)
863 {
864         struct cylinder_widget *cylinder = data;
865         int state;
866
867         state = gtk_toggle_button_get_active(button);
868         gtk_widget_set_sensitive(cylinder->start, state);
869         gtk_widget_set_sensitive(cylinder->end, state);
870 }
871
872 static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder)
873 {
874         const char *desc;
875         unsigned int ml, mbar;
876
877         gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
878         add_cylinder(cylinder, desc, ml, mbar);
879         return TRUE;
880 }
881
882 static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data)
883 {
884         struct cylinder_widget *cylinder = data;
885         cylinder_cb(cylinder->description, data);
886 }
887
888 /* Return a frame containing a hbox inside a hbox */
889 static GtkWidget *frame_box(const char *title, GtkWidget *vbox)
890 {
891         GtkWidget *hbox, *frame;
892
893         hbox = gtk_hbox_new(FALSE, 10);
894         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
895
896         frame = gtk_frame_new(title);
897         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 0);
898
899         hbox = gtk_hbox_new(FALSE, 10);
900         gtk_container_add(GTK_CONTAINER(frame), hbox);
901
902         return hbox;
903 }
904
905 static GtkWidget *labeled_spinbutton(GtkWidget *box, const char *name, double min, double max, double incr)
906 {
907         GtkWidget *hbox, *label, *button;
908
909         hbox = gtk_hbox_new(FALSE, 0);
910         gtk_box_pack_start(GTK_BOX(box), hbox, TRUE, FALSE, 0);
911
912         label = gtk_label_new(name);
913         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
914
915         button = gtk_spin_button_new_with_range(min, max, incr);
916         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
917
918         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
919
920         return button;
921 }
922
923 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
924 {
925         GtkWidget *frame, *hbox;
926         GtkEntry *entry;
927         GtkEntryCompletion *completion;
928         GtkWidget *widget;
929
930         /*
931          * Cylinder type: description, size and
932          * working pressure
933          */
934         frame = gtk_frame_new("Cylinder");
935
936         hbox = gtk_hbox_new(FALSE, 3);
937         gtk_container_add(GTK_CONTAINER(frame), hbox);
938
939         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
940         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
941
942         cylinder->description = GTK_COMBO_BOX(widget);
943         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
944
945         entry = GTK_ENTRY(GTK_BIN(widget)->child);
946         g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder);
947
948         completion = gtk_entry_completion_new();
949         gtk_entry_completion_set_text_column(completion, 0);
950         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
951         g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder);
952         gtk_entry_set_completion(entry, completion);
953
954         hbox = gtk_hbox_new(FALSE, 3);
955         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
956         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
957
958         widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
959         cylinder->size = GTK_SPIN_BUTTON(widget);
960
961         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
962         cylinder->pressure = GTK_SPIN_BUTTON(widget);
963
964         /*
965          * Cylinder start/end pressures
966          */
967         hbox = frame_box("Pressure", vbox);
968
969         widget = labeled_spinbutton(hbox, "Start", 0, 5000, 1);
970         cylinder->start = widget;
971
972         widget = labeled_spinbutton(hbox, "End", 0, 5000, 1);
973         cylinder->end = widget;
974
975         cylinder->pressure_button = gtk_check_button_new();
976         gtk_box_pack_start(GTK_BOX(hbox), cylinder->pressure_button, FALSE, FALSE, 3);
977         g_signal_connect(cylinder->pressure_button, "toggled", G_CALLBACK(pressure_cb), cylinder);
978
979         /*
980          * Cylinder gas mix: Air, Nitrox or Trimix
981          */
982         hbox = frame_box("Gasmix", vbox);
983
984         widget = labeled_spinbutton(hbox, "O"UTF8_SUBSCRIPT_2 "%", 1, 100, 0.1);
985         cylinder->o2 = widget;
986         widget = labeled_spinbutton(hbox, "He%", 0, 100, 0.1);
987         cylinder->he = widget;
988         cylinder->gasmix_button = gtk_check_button_new();
989         gtk_box_pack_start(GTK_BOX(hbox), cylinder->gasmix_button, FALSE, FALSE, 3);
990         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(gasmix_cb), cylinder);
991 }
992
993 static gboolean weight_completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct ws_widget *ws_widget)
994 {
995         const char *desc;
996         unsigned int weight;
997
998         gtk_tree_model_get(model, iter, WS_DESC, &desc, WS_WEIGHT, &weight, -1);
999         add_weightsystem(ws_widget, desc, weight);
1000         return TRUE;
1001 }
1002
1003 static void weight_activate_cb(GtkComboBox *combo_box, gpointer data)
1004 {
1005         struct ws_widget *ws_widget = data;
1006         weight_cb(ws_widget->description, data);
1007 }
1008
1009 static void ws_widget(GtkWidget *vbox, struct ws_widget *ws_widget, GtkListStore *model)
1010 {
1011         GtkWidget *frame, *hbox;
1012         GtkEntryCompletion *completion;
1013         GtkWidget *widget;
1014         GtkEntry *entry;
1015
1016         /*
1017          * weight_system: description and weight
1018          */
1019         frame = gtk_frame_new("Weight");
1020
1021         hbox = gtk_hbox_new(FALSE, 3);
1022         gtk_container_add(GTK_CONTAINER(frame), hbox);
1023
1024         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
1025         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
1026
1027         ws_widget->description = GTK_COMBO_BOX(widget);
1028         g_signal_connect(widget, "changed", G_CALLBACK(weight_cb), ws_widget);
1029
1030         entry = GTK_ENTRY(GTK_BIN(widget)->child);
1031         g_signal_connect(entry, "activate", G_CALLBACK(weight_activate_cb), ws_widget);
1032
1033         completion = gtk_entry_completion_new();
1034         gtk_entry_completion_set_text_column(completion, 0);
1035         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
1036         g_signal_connect(completion, "match-selected", G_CALLBACK(weight_completion_cb), ws_widget);
1037         gtk_entry_set_completion(entry, completion);
1038
1039         hbox = gtk_hbox_new(FALSE, 3);
1040         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
1041         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
1042
1043         if ( output_units.weight == KG)
1044                 widget = create_spinbutton(hbox, "kg", 0, 50, 0.5);
1045         else
1046                 widget = create_spinbutton(hbox, "lbs", 0, 110, 1);
1047         ws_widget->weight = GTK_SPIN_BUTTON(widget);
1048 }
1049
1050 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
1051 {
1052         int success;
1053         GtkWidget *dialog, *vbox;
1054         struct cylinder_widget cylinder;
1055         struct dive *dive;
1056
1057         cylinder.index = index;
1058         cylinder.changed = 0;
1059
1060         dive = current_dive;
1061         if (!dive)
1062                 return 0;
1063         *cyl = dive->cylinder[index];
1064
1065         dialog = gtk_dialog_new_with_buttons("Cylinder",
1066                 GTK_WINDOW(main_window),
1067                 GTK_DIALOG_DESTROY_WITH_PARENT,
1068                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1069                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1070                 NULL);
1071
1072         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1073         cylinder_widget(vbox, &cylinder, cylinder_model);
1074
1075         show_cylinder(cyl, &cylinder);
1076
1077         gtk_widget_show_all(dialog);
1078         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
1079         if (success) {
1080                 record_cylinder_changes(cyl, &cylinder);
1081                 dive->cylinder[index] = *cyl;
1082                 mark_divelist_changed(TRUE);
1083                 update_cylinder_related_info(dive);
1084                 flush_divelist(dive);
1085         }
1086
1087         gtk_widget_destroy(dialog);
1088
1089         return success;
1090 }
1091
1092 static int edit_weightsystem_dialog(int index, weightsystem_t *ws)
1093 {
1094         int success;
1095         GtkWidget *dialog, *vbox;
1096         struct ws_widget weightsystem_widget;
1097         struct dive *dive;
1098
1099         weightsystem_widget.index = index;
1100         weightsystem_widget.changed = 0;
1101
1102         dive = current_dive;
1103         if (!dive)
1104                 return 0;
1105         *ws = dive->weightsystem[index];
1106
1107         dialog = gtk_dialog_new_with_buttons("Weight System",
1108                 GTK_WINDOW(main_window),
1109                 GTK_DIALOG_DESTROY_WITH_PARENT,
1110                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1111                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1112                 NULL);
1113
1114         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1115         ws_widget(vbox, &weightsystem_widget, weightsystem_model);
1116
1117         show_weightsystem(ws, &weightsystem_widget);
1118
1119         gtk_widget_show_all(dialog);
1120         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
1121         if (success) {
1122                 record_weightsystem_changes(ws, &weightsystem_widget);
1123                 dive->weightsystem[index] = *ws;
1124                 mark_divelist_changed(TRUE);
1125                 flush_divelist(dive);
1126         }
1127
1128         gtk_widget_destroy(dialog);
1129
1130         return success;
1131 }
1132
1133 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
1134 {
1135         int *p, index;
1136         GtkTreePath *path;
1137
1138         path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
1139         p = gtk_tree_path_get_indices(path);
1140         index = p ? *p : 0;
1141         gtk_tree_path_free(path);
1142         return index;
1143 }
1144
1145 static void edit_cb(GtkButton *button, int w_idx)
1146 {
1147         int index;
1148         GtkTreeIter iter;
1149         GtkListStore *model = cylinder_list[w_idx].model;
1150         GtkTreeView *tree_view = cylinder_list[w_idx].tree_view;
1151         GtkTreeSelection *selection;
1152         cylinder_t cyl;
1153
1154         selection = gtk_tree_view_get_selection(tree_view);
1155
1156         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1157         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1158                 return;
1159
1160         index = get_model_index(model, &iter);
1161         if (!edit_cylinder_dialog(index, &cyl))
1162                 return;
1163
1164         set_one_cylinder(&cyl, model, &iter);
1165         repaint_dive();
1166 }
1167
1168 static void add_cb(GtkButton *button, int w_idx)
1169 {
1170         int index = cylinder_list[w_idx].max_index;
1171         GtkTreeIter iter;
1172         GtkListStore *model = cylinder_list[w_idx].model;
1173         GtkTreeView *tree_view = cylinder_list[w_idx].tree_view;
1174         GtkTreeSelection *selection;
1175         cylinder_t cyl;
1176
1177         if (!edit_cylinder_dialog(index, &cyl))
1178                 return;
1179
1180         gtk_list_store_append(model, &iter);
1181         set_one_cylinder(&cyl, model, &iter);
1182
1183         selection = gtk_tree_view_get_selection(tree_view);
1184         gtk_tree_selection_select_iter(selection, &iter);
1185
1186         cylinder_list[w_idx].max_index++;
1187         gtk_widget_set_sensitive(cylinder_list[w_idx].add, cylinder_list[w_idx].max_index < MAX_CYLINDERS);
1188 }
1189
1190 static void del_cb(GtkButton *button, int w_idx)
1191 {
1192         int index, nr;
1193         GtkTreeIter iter;
1194         GtkListStore *model = cylinder_list[w_idx].model;
1195         GtkTreeView *tree_view = cylinder_list[w_idx].tree_view;
1196         GtkTreeSelection *selection;
1197         struct dive *dive;
1198         cylinder_t *cyl;
1199
1200         selection = gtk_tree_view_get_selection(tree_view);
1201
1202         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1203         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1204                 return;
1205
1206         index = get_model_index(model, &iter);
1207
1208         dive = current_dive;
1209         if (!dive)
1210                 return;
1211         cyl = dive->cylinder + index;
1212         nr = cylinder_list[w_idx].max_index - index - 1;
1213
1214         gtk_list_store_remove(model, &iter);
1215
1216         cylinder_list[w_idx].max_index--;
1217         memmove(cyl, cyl+1, nr*sizeof(*cyl));
1218         memset(cyl+nr, 0, sizeof(*cyl));
1219
1220         mark_divelist_changed(TRUE);
1221         flush_divelist(dive);
1222
1223         gtk_widget_set_sensitive(cylinder_list[w_idx].edit, 0);
1224         gtk_widget_set_sensitive(cylinder_list[w_idx].del, 0);
1225         gtk_widget_set_sensitive(cylinder_list[w_idx].add, 1);
1226 }
1227
1228 static void ws_edit_cb(GtkButton *button, int w_idx)
1229 {
1230         int index;
1231         GtkTreeIter iter;
1232         GtkListStore *model = weightsystem_list[w_idx].model;
1233         GtkTreeView *tree_view = weightsystem_list[w_idx].tree_view;
1234         GtkTreeSelection *selection;
1235         weightsystem_t ws;
1236
1237         selection = gtk_tree_view_get_selection(tree_view);
1238
1239         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1240         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1241                 return;
1242
1243         index = get_model_index(model, &iter);
1244         if (!edit_weightsystem_dialog(index, &ws))
1245                 return;
1246
1247         set_one_weightsystem(&ws, model, &iter);
1248         repaint_dive();
1249 }
1250
1251 static void ws_add_cb(GtkButton *button, int w_idx)
1252 {
1253         int index = weightsystem_list[w_idx].max_index;
1254         GtkTreeIter iter;
1255         GtkListStore *model = weightsystem_list[w_idx].model;
1256         GtkTreeView *tree_view = weightsystem_list[w_idx].tree_view;
1257         GtkTreeSelection *selection;
1258         weightsystem_t ws;
1259
1260         if (!edit_weightsystem_dialog(index, &ws))
1261                 return;
1262
1263         gtk_list_store_append(model, &iter);
1264         set_one_weightsystem(&ws, model, &iter);
1265
1266         selection = gtk_tree_view_get_selection(tree_view);
1267         gtk_tree_selection_select_iter(selection, &iter);
1268
1269         weightsystem_list[w_idx].max_index++;
1270         gtk_widget_set_sensitive(weightsystem_list[w_idx].add, weightsystem_list[w_idx].max_index < MAX_WEIGHTSYSTEMS);
1271 }
1272
1273 static void ws_del_cb(GtkButton *button, int w_idx)
1274 {
1275         int index, nr;
1276         GtkTreeIter iter;
1277         GtkListStore *model = weightsystem_list[w_idx].model;
1278         GtkTreeView *tree_view = weightsystem_list[w_idx].tree_view;
1279         GtkTreeSelection *selection;
1280         struct dive *dive;
1281         weightsystem_t *ws;
1282
1283         selection = gtk_tree_view_get_selection(tree_view);
1284
1285         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1286         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1287                 return;
1288
1289         index = get_model_index(model, &iter);
1290
1291         dive = current_dive;
1292         if (!dive)
1293                 return;
1294         ws = dive->weightsystem + index;
1295         nr = weightsystem_list[w_idx].max_index - index - 1;
1296
1297         gtk_list_store_remove(model, &iter);
1298
1299         weightsystem_list[w_idx].max_index--;
1300         memmove(ws, ws+1, nr*sizeof(*ws));
1301         memset(ws+nr, 0, sizeof(*ws));
1302
1303         mark_divelist_changed(TRUE);
1304         flush_divelist(dive);
1305
1306         gtk_widget_set_sensitive(weightsystem_list[w_idx].edit, 0);
1307         gtk_widget_set_sensitive(weightsystem_list[w_idx].del, 0);
1308         gtk_widget_set_sensitive(weightsystem_list[w_idx].add, 1);
1309 }
1310
1311 static GtkListStore *create_tank_size_model(void)
1312 {
1313         GtkListStore *model;
1314
1315         model = gtk_list_store_new(3,
1316                 G_TYPE_STRING,          /* Tank name */
1317                 G_TYPE_INT,             /* Tank size in mliter */
1318                 G_TYPE_INT,             /* Tank working pressure in mbar */
1319                 -1);
1320
1321         fill_tank_list(model);
1322         return model;
1323 }
1324
1325 static GtkListStore *create_weightsystem_model(void)
1326 {
1327         GtkListStore *model;
1328
1329         model = gtk_list_store_new(2,
1330                 G_TYPE_STRING,          /* Weightsystem description */
1331                 G_TYPE_INT,             /* Weight in grams */
1332                 -1);
1333
1334         fill_ws_list(model);
1335         return model;
1336 }
1337
1338 static void size_data_func(GtkTreeViewColumn *col,
1339                            GtkCellRenderer *renderer,
1340                            GtkTreeModel *model,
1341                            GtkTreeIter *iter,
1342                            gpointer data)
1343 {
1344         int ml, mbar;
1345         double size, pressure;
1346         char buffer[10];
1347
1348         gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
1349         convert_volume_pressure(ml, mbar, &size, &pressure);
1350         if (size)
1351                 snprintf(buffer, sizeof(buffer), "%.1f", size);
1352         else
1353                 strcpy(buffer, "unkn");
1354         g_object_set(renderer, "text", buffer, NULL);
1355 }
1356
1357 static void weight_data_func(GtkTreeViewColumn *col,
1358                            GtkCellRenderer *renderer,
1359                            GtkTreeModel *model,
1360                            GtkTreeIter *iter,
1361                            gpointer data)
1362 {
1363         int idx = (long)data;
1364         int grams, decimals;
1365         double value;
1366         char buffer[10];
1367
1368         gtk_tree_model_get(model, iter, idx, &grams, -1);
1369         decimals = convert_weight(grams, &value);
1370         if (grams)
1371                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
1372         else
1373                 strcpy(buffer, "unkn");
1374         g_object_set(renderer, "text", buffer, NULL);
1375 }
1376
1377 static void pressure_data_func(GtkTreeViewColumn *col,
1378                            GtkCellRenderer *renderer,
1379                            GtkTreeModel *model,
1380                            GtkTreeIter *iter,
1381                            gpointer data)
1382 {
1383         int index = (long)data;
1384         int mbar, decimals;
1385         double pressure;
1386         char buffer[10];
1387
1388         gtk_tree_model_get(model, iter, index, &mbar, -1);
1389         decimals = convert_pressure(mbar, &pressure);
1390         if (mbar)
1391                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
1392         else
1393                 *buffer = 0;
1394         g_object_set(renderer, "text", buffer, NULL);
1395 }
1396
1397 static void percentage_data_func(GtkTreeViewColumn *col,
1398                            GtkCellRenderer *renderer,
1399                            GtkTreeModel *model,
1400                            GtkTreeIter *iter,
1401                            gpointer data)
1402 {
1403         int index = (long)data;
1404         int permille;
1405         char buffer[10];
1406
1407         gtk_tree_model_get(model, iter, index, &permille, -1);
1408         if (permille)
1409                 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
1410         else
1411                 *buffer = 0;
1412         g_object_set(renderer, "text", buffer, NULL);
1413 }
1414
1415 static void selection_cb(GtkTreeSelection *selection, struct equipment_list *list)
1416 {
1417         GtkTreeIter iter;
1418         int selected;
1419
1420         selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
1421         gtk_widget_set_sensitive(list->edit, selected);
1422         gtk_widget_set_sensitive(list->del, selected);
1423 }
1424
1425 static void row_activated_cb(GtkTreeView *tree_view,
1426                         GtkTreePath *path,
1427                         GtkTreeViewColumn *column,
1428                         int w_idx)
1429 {
1430         edit_cb(NULL, w_idx);
1431 }
1432
1433 static void ws_row_activated_cb(GtkTreeView *tree_view,
1434                         GtkTreePath *path,
1435                         GtkTreeViewColumn *column,
1436                         int w_idx)
1437 {
1438         ws_edit_cb(NULL, w_idx);
1439 }
1440
1441 GtkWidget *cylinder_list_widget(int w_idx)
1442 {
1443         GtkListStore *model = cylinder_list[w_idx].model;
1444         GtkWidget *tree_view;
1445         GtkTreeSelection *selection;
1446
1447         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1448         gtk_widget_set_can_focus(tree_view, FALSE);
1449
1450         g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), GINT_TO_POINTER(w_idx));
1451
1452         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1453         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1454         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &cylinder_list[w_idx]);
1455
1456         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1457                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1458                                           NULL);
1459
1460         tree_view_column(tree_view, CYL_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1461         tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, ALIGN_RIGHT | UNSORTABLE);
1462         tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1463         tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1464         tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1465         tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1466         tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1467         return tree_view;
1468 }
1469
1470 GtkWidget *weightsystem_list_widget(int w_idx)
1471 {
1472         GtkListStore *model = weightsystem_list[w_idx].model;
1473         GtkWidget *tree_view;
1474         GtkTreeSelection *selection;
1475
1476         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1477         gtk_widget_set_can_focus(tree_view, FALSE);
1478         g_signal_connect(tree_view, "row-activated", G_CALLBACK(ws_row_activated_cb), GINT_TO_POINTER(w_idx));
1479
1480         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1481         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1482         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &weightsystem_list[w_idx]);
1483
1484         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1485                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1486                                           NULL);
1487
1488         tree_view_column(tree_view, WS_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1489         tree_view_column(tree_view, WS_WEIGHT, "weight",
1490                         weight_data_func, ALIGN_RIGHT | UNSORTABLE);
1491
1492         return tree_view;
1493 }
1494
1495 static GtkWidget *cylinder_list_create(int w_idx)
1496 {
1497         GtkListStore *model;
1498
1499         model = gtk_list_store_new(CYL_COLUMNS,
1500                 G_TYPE_STRING,          /* CYL_DESC: utf8 */
1501                 G_TYPE_INT,             /* CYL_SIZE: mliter */
1502                 G_TYPE_INT,             /* CYL_WORKP: mbar */
1503                 G_TYPE_INT,             /* CYL_STARTP: mbar */
1504                 G_TYPE_INT,             /* CYL_ENDP: mbar */
1505                 G_TYPE_INT,             /* CYL_O2: permille */
1506                 G_TYPE_INT              /* CYL_HE: permille */
1507                 );
1508         cylinder_list[w_idx].model = model;
1509         return cylinder_list_widget(w_idx);
1510 }
1511
1512 static GtkWidget *weightsystem_list_create(int w_idx)
1513 {
1514         GtkListStore *model;
1515
1516         model = gtk_list_store_new(WS_COLUMNS,
1517                 G_TYPE_STRING,          /* WS_DESC: utf8 */
1518                 G_TYPE_INT              /* WS_WEIGHT: grams */
1519                 );
1520         weightsystem_list[w_idx].model = model;
1521         return weightsystem_list_widget(w_idx);
1522 }
1523
1524 GtkWidget *equipment_widget(int w_idx)
1525 {
1526         GtkWidget *vbox, *hbox, *frame, *framebox, *tree_view;
1527         GtkWidget *add, *del, *edit;
1528
1529         vbox = gtk_vbox_new(FALSE, 3);
1530
1531         /*
1532          * We create the cylinder size (and weightsystem) models
1533          * at startup for the primary cylinder / weightsystem widget,
1534          * since we're going to share it across all cylinders and all
1535          * dives. So if you add a new cylinder type or weightsystem in
1536          * one dive, it will show up when you edit the cylinder types
1537          * or weightsystems for another dive.
1538          */
1539         if (w_idx == W_IDX_PRIMARY)
1540                 cylinder_model = create_tank_size_model();
1541         tree_view = cylinder_list_create(w_idx);
1542         cylinder_list[w_idx].tree_view = GTK_TREE_VIEW(tree_view);
1543
1544         hbox = gtk_hbox_new(FALSE, 3);
1545         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1546
1547         frame = gtk_frame_new("Cylinders");
1548         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1549
1550         framebox = gtk_vbox_new(FALSE, 3);
1551         gtk_container_add(GTK_CONTAINER(frame), framebox);
1552
1553         hbox = gtk_hbox_new(FALSE, 3);
1554         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1555
1556         gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1557
1558         hbox = gtk_hbox_new(TRUE, 3);
1559         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1560
1561         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1562         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1563         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1564         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1565         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1566         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1567
1568         cylinder_list[w_idx].edit = edit;
1569         cylinder_list[w_idx].add = add;
1570         cylinder_list[w_idx].del = del;
1571
1572         g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), GINT_TO_POINTER(w_idx));
1573         g_signal_connect(add, "clicked", G_CALLBACK(add_cb), GINT_TO_POINTER(w_idx));
1574         g_signal_connect(del, "clicked", G_CALLBACK(del_cb), GINT_TO_POINTER(w_idx));
1575
1576         hbox = gtk_hbox_new(FALSE, 3);
1577         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1578
1579         if (w_idx == W_IDX_PRIMARY)
1580                 weightsystem_model = create_weightsystem_model();
1581         tree_view = weightsystem_list_create(w_idx);
1582         weightsystem_list[w_idx].tree_view = GTK_TREE_VIEW(tree_view);
1583
1584         frame = gtk_frame_new("Weight");
1585         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1586
1587         framebox = gtk_vbox_new(FALSE, 3);
1588         gtk_container_add(GTK_CONTAINER(frame), framebox);
1589
1590         hbox = gtk_hbox_new(FALSE, 3);
1591         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1592
1593         gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1594
1595         hbox = gtk_hbox_new(TRUE, 3);
1596         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1597
1598         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1599         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1600         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1601         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1602         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1603         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1604
1605         weightsystem_list[w_idx].edit = edit;
1606         weightsystem_list[w_idx].add = add;
1607         weightsystem_list[w_idx].del = del;
1608
1609         g_signal_connect(edit, "clicked", G_CALLBACK(ws_edit_cb), GINT_TO_POINTER(w_idx));
1610         g_signal_connect(add, "clicked", G_CALLBACK(ws_add_cb), GINT_TO_POINTER(w_idx));
1611         g_signal_connect(del, "clicked", G_CALLBACK(ws_del_cb), GINT_TO_POINTER(w_idx));
1612
1613         return vbox;
1614 }