]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Fix crash when editing weight system info
[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 int 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 int weightsystem_none(void *_data)
446 {
447         weightsystem_t *ws = _data;
448         return !ws->weight.grams && !ws->description;
449 }
450
451 static void set_one_cylinder(void *_data, GtkListStore *model, GtkTreeIter *iter)
452 {
453         cylinder_t *cyl = _data;
454         unsigned int start, end;
455
456         start = cyl->start.mbar ? : cyl->sample_start.mbar;
457         end = cyl->end.mbar ? : cyl->sample_end.mbar;
458         gtk_list_store_set(model, iter,
459                 CYL_DESC, cyl->type.description ? : "",
460                 CYL_SIZE, cyl->type.size.mliter,
461                 CYL_WORKP, cyl->type.workingpressure.mbar,
462                 CYL_STARTP, start,
463                 CYL_ENDP, end,
464                 CYL_O2, cyl->gasmix.o2.permille,
465                 CYL_HE, cyl->gasmix.he.permille,
466                 -1);
467 }
468
469 static void set_one_weightsystem(void *_data, GtkListStore *model, GtkTreeIter *iter)
470 {
471         weightsystem_t *ws = _data;
472
473         gtk_list_store_set(model, iter,
474                 WS_DESC, ws->description ? : "unspecified",
475                 WS_WEIGHT, ws->weight.grams,
476                 -1);
477 }
478
479 static void *cyl_ptr(struct dive *dive, int idx)
480 {
481         if (idx < 0 || idx >= MAX_CYLINDERS)
482                 return NULL;
483         return &dive->cylinder[idx];
484 }
485
486 static void *ws_ptr(struct dive *dive, int idx)
487 {
488         if (idx < 0 || idx >= MAX_WEIGHTSYSTEMS)
489                 return NULL;
490         return &dive->weightsystem[idx];
491 }
492
493 static void show_equipment(struct dive *dive, int max,
494                         struct equipment_list *equipment_list,
495                         void*(*ptr_function)(struct dive*, int),
496                         int(*none_function)(void *),
497                         void(*set_one_function)(void*, GtkListStore*, GtkTreeIter *))
498 {
499         int i, used;
500         void *data;
501         GtkTreeIter iter;
502         GtkListStore *model = equipment_list->model;
503
504         gtk_list_store_clear(model);
505         used = max;
506         do {
507                 data = ptr_function(dive, used-1);
508                 if (!none_function(data))
509                         break;
510         } while (--used);
511
512         equipment_list->max_index = used;
513
514         gtk_widget_set_sensitive(equipment_list->edit, 0);
515         gtk_widget_set_sensitive(equipment_list->del, 0);
516         gtk_widget_set_sensitive(equipment_list->add, used < max);
517
518         for (i = 0; i < used; i++) {
519                 data = ptr_function(dive, i);
520                 gtk_list_store_append(model, &iter);
521                 set_one_function(data, model, &iter);
522         }
523 }
524
525 void show_dive_equipment(struct dive *dive, int w_idx)
526 {
527         show_equipment(dive, MAX_CYLINDERS, &cylinder_list[w_idx],
528                 &cyl_ptr, &cylinder_none, &set_one_cylinder);
529         show_equipment(dive, MAX_WEIGHTSYSTEMS, &weightsystem_list[w_idx],
530                 &ws_ptr, &weightsystem_none, &set_one_weightsystem);
531 }
532
533 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
534 {
535         GtkWidget *frame, *hbox, *button;
536
537         frame = gtk_frame_new(name);
538         gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
539
540         hbox = gtk_hbox_new(FALSE, 3);
541         gtk_container_add(GTK_CONTAINER(frame), hbox);
542
543         button = gtk_spin_button_new_with_range(min, max, incr);
544         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
545
546         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
547
548         return button;
549 }
550
551 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
552                 double volume, double pressure, double start, double end, int o2, int he)
553 {
554         int mbar, ml;
555
556         if (output_units.pressure == PSI) {
557                 pressure = psi_to_bar(pressure);
558                 start = psi_to_bar(start);
559                 end = psi_to_bar(end);
560         }
561
562         if (pressure && output_units.volume == CUFT) {
563                 volume = cuft_to_l(volume);
564                 volume /= bar_to_atm(pressure);
565         }
566
567         ml = volume * 1000 + 0.5;
568         mbar = pressure * 1000 + 0.5;
569
570         /* Ignore obviously crazy He values */
571         if (o2 + he > 1000)
572                 he = 0;
573
574         /* We have a rule that normal air is all zeroes */
575         if (!he && o2 > 208 && o2 < 211)
576                 o2 = 0;
577
578         cyl->type.description = desc;
579         cyl->type.size.mliter = ml;
580         cyl->type.workingpressure.mbar = mbar;
581         cyl->start.mbar = start * 1000 + 0.5;
582         cyl->end.mbar = end * 1000 + 0.5;
583         cyl->gasmix.o2.permille = o2;
584         cyl->gasmix.he.permille = he;
585
586         /*
587          * Also, insert it into the model if it doesn't already exist
588          */
589         add_cylinder(cylinder, desc, ml, mbar);
590 }
591
592 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
593 {
594         const gchar *desc;
595         GtkComboBox *box;
596         double volume, pressure, start, end;
597         int o2, he;
598
599         /* Ignore uninitialized cylinder widgets */
600         box = cylinder->description;
601         if (!box)
602                 return;
603
604         desc = gtk_combo_box_get_active_text(box);
605         volume = gtk_spin_button_get_value(cylinder->size);
606         pressure = gtk_spin_button_get_value(cylinder->pressure);
607         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button))) {
608                 start = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->start));
609                 end = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->end));
610         } else {
611                 start = end = 0;
612         }
613         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button))) {
614                 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
615                 he = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->he))*10 + 0.5;
616         } else {
617                 o2 = 0;
618                 he = 0;
619         }
620         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2, he);
621 }
622
623 static void record_weightsystem_changes(weightsystem_t *ws, struct ws_widget *weightsystem_widget)
624 {
625         const gchar *desc;
626         GtkComboBox *box;
627         int grams;
628         double value;
629         GtkTreeIter iter;
630
631         /* Ignore uninitialized cylinder widgets */
632         box = weightsystem_widget->description;
633         if (!box)
634                 return;
635
636         desc = gtk_combo_box_get_active_text(box);
637         value = gtk_spin_button_get_value(weightsystem_widget->weight);
638
639         if (output_units.weight == LBS)
640                 grams = lbs_to_grams(value);
641         else
642                 grams = value * 1000;
643         ws->weight.grams = grams;
644         ws->description = desc;
645         add_weightsystem_type(desc, grams, &iter);
646 }
647
648 /*
649  * We hardcode the most common standard cylinders,
650  * we should pick up any other names from the dive
651  * logs directly.
652  */
653 static struct tank_info {
654         const char *name;
655         int cuft, ml, psi, bar;
656 } tank_info[100] = {
657         /* Need an empty entry for the no-cylinder case */
658         { "", },
659
660         /* Size-only metric cylinders */
661         { "10.0 l", .ml = 10000 },
662         { "11.1 l", .ml = 11100 },
663
664         /* Most common AL cylinders */
665         { "AL50",  .cuft =  50, .psi = 3000 },
666         { "AL63",  .cuft =  63, .psi = 3000 },
667         { "AL72",  .cuft =  72, .psi = 3000 },
668         { "AL80",  .cuft =  80, .psi = 3000 },
669         { "AL100", .cuft = 100, .psi = 3300 },
670
671         /* Somewhat common LP steel cylinders */
672         { "LP85",  .cuft =  85, .psi = 2640 },
673         { "LP95",  .cuft =  95, .psi = 2640 },
674         { "LP108", .cuft = 108, .psi = 2640 },
675         { "LP121", .cuft = 121, .psi = 2640 },
676
677         /* Somewhat common HP steel cylinders */
678         { "HP65",  .cuft =  65, .psi = 3442 },
679         { "HP80",  .cuft =  80, .psi = 3442 },
680         { "HP100", .cuft = 100, .psi = 3442 },
681         { "HP119", .cuft = 119, .psi = 3442 },
682         { "HP130", .cuft = 130, .psi = 3442 },
683
684         /* Common European steel cylinders */
685         { "10L 300 bar",  .ml = 10000, .bar = 300 },
686         { "12L 200 bar",  .ml = 12000, .bar = 200 },
687         { "12L 232 bar",  .ml = 12000, .bar = 232 },
688         { "12L 300 bar",  .ml = 12000, .bar = 300 },
689         { "15L 200 bar",  .ml = 15000, .bar = 200 },
690         { "15L 232 bar",  .ml = 15000, .bar = 232 },
691         { "D7 300 bar",   .ml = 14000, .bar = 300 },
692         { "D8.5 232 bar", .ml = 17000, .bar = 232 },
693         { "D12 232 bar",  .ml = 24000, .bar = 232 },
694
695         /* We'll fill in more from the dive log dynamically */
696         { NULL, }
697 };
698
699 static void fill_tank_list(GtkListStore *store)
700 {
701         GtkTreeIter iter;
702         struct tank_info *info = tank_info;
703
704         for (info = tank_info ; info->name; info++) {
705                 int ml = info->ml;
706                 int cuft = info->cuft;
707                 int psi = info->psi;
708                 int mbar;
709                 double bar = info->bar;
710
711                 if (psi && bar)
712                         goto bad_tank_info;
713                 if (ml && cuft)
714                         goto bad_tank_info;
715                 if (cuft && !psi)
716                         goto bad_tank_info;
717
718                 /* Is it in cuft and psi? */
719                 if (psi)
720                         bar = psi_to_bar(psi);
721
722                 if (cuft) {
723                         double airvolume = cuft_to_l(cuft) * 1000.0;
724                         double atm = bar_to_atm(bar);
725
726                         ml = airvolume / atm + 0.5;
727                 }
728
729                 mbar = bar * 1000 + 0.5;
730
731                 gtk_list_store_append(store, &iter);
732                 gtk_list_store_set(store, &iter,
733                         0, info->name,
734                         1, ml,
735                         2, mbar,
736                         -1);
737                 continue;
738
739 bad_tank_info:
740                 fprintf(stderr, "Bad tank info for '%s'\n", info->name);
741         }
742 }
743
744 /*
745  * We hardcode the most common weight system types
746  * This is a bit odd as the weight system types don't usually encode weight
747  */
748 static struct ws_info {
749         const char *name;
750         int grams;
751 } ws_info[100] = {
752         { "integrated", 0 },
753         { "belt", 0 },
754         { "ankle", 0 },
755         { "bar", 0 },
756         { "clip-on", 0 },
757 };
758
759 static void fill_ws_list(GtkListStore *store)
760 {
761         GtkTreeIter iter;
762         struct ws_info *info = ws_info;
763
764         while (info->name) {
765                 gtk_list_store_append(store, &iter);
766                 gtk_list_store_set(store, &iter,
767                         0, info->name,
768                         1, info->grams,
769                         -1);
770                 info++;
771         }
772 }
773
774 static void gasmix_cb(GtkToggleButton *button, gpointer data)
775 {
776         struct cylinder_widget *cylinder = data;
777         int state;
778
779         state = gtk_toggle_button_get_active(button);
780         gtk_widget_set_sensitive(cylinder->o2, state);
781         gtk_widget_set_sensitive(cylinder->he, state);
782 }
783
784 static void pressure_cb(GtkToggleButton *button, gpointer data)
785 {
786         struct cylinder_widget *cylinder = data;
787         int state;
788
789         state = gtk_toggle_button_get_active(button);
790         gtk_widget_set_sensitive(cylinder->start, state);
791         gtk_widget_set_sensitive(cylinder->end, state);
792 }
793
794 static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder)
795 {
796         const char *desc;
797         unsigned int ml, mbar;
798
799         gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
800         add_cylinder(cylinder, desc, ml, mbar);
801         return TRUE;
802 }
803
804 static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data)
805 {
806         struct cylinder_widget *cylinder = data;
807         cylinder_cb(cylinder->description, data);
808 }
809
810 /* Return a frame containing a hbox inside a hbox */
811 static GtkWidget *frame_box(const char *title, GtkWidget *vbox)
812 {
813         GtkWidget *hbox, *frame;
814
815         hbox = gtk_hbox_new(FALSE, 10);
816         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
817
818         frame = gtk_frame_new(title);
819         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 0);
820
821         hbox = gtk_hbox_new(FALSE, 10);
822         gtk_container_add(GTK_CONTAINER(frame), hbox);
823
824         return hbox;
825 }
826
827 static GtkWidget *labeled_spinbutton(GtkWidget *box, const char *name, double min, double max, double incr)
828 {
829         GtkWidget *hbox, *label, *button;
830
831         hbox = gtk_hbox_new(FALSE, 0);
832         gtk_box_pack_start(GTK_BOX(box), hbox, TRUE, FALSE, 0);
833
834         label = gtk_label_new(name);
835         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
836
837         button = gtk_spin_button_new_with_range(min, max, incr);
838         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
839
840         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
841
842         return button;
843 }
844
845 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
846 {
847         GtkWidget *frame, *hbox;
848         GtkEntry *entry;
849         GtkEntryCompletion *completion;
850         GtkWidget *widget;
851
852         /*
853          * Cylinder type: description, size and
854          * working pressure
855          */
856         frame = gtk_frame_new("Cylinder");
857
858         hbox = gtk_hbox_new(FALSE, 3);
859         gtk_container_add(GTK_CONTAINER(frame), hbox);
860
861         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
862         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
863
864         cylinder->description = GTK_COMBO_BOX(widget);
865         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
866
867         entry = GTK_ENTRY(GTK_BIN(widget)->child);
868         g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder);
869
870         completion = gtk_entry_completion_new();
871         gtk_entry_completion_set_text_column(completion, 0);
872         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
873         g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder);
874         gtk_entry_set_completion(entry, completion);
875
876         hbox = gtk_hbox_new(FALSE, 3);
877         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
878         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
879
880         widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
881         cylinder->size = GTK_SPIN_BUTTON(widget);
882
883         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
884         cylinder->pressure = GTK_SPIN_BUTTON(widget);
885
886         /*
887          * Cylinder start/end pressures
888          */
889         hbox = frame_box("Pressure", vbox);
890
891         widget = labeled_spinbutton(hbox, "Start", 0, 5000, 1);
892         cylinder->start = widget;
893
894         widget = labeled_spinbutton(hbox, "End", 0, 5000, 1);
895         cylinder->end = widget;
896
897         cylinder->pressure_button = gtk_check_button_new();
898         gtk_box_pack_start(GTK_BOX(hbox), cylinder->pressure_button, FALSE, FALSE, 3);
899         g_signal_connect(cylinder->pressure_button, "toggled", G_CALLBACK(pressure_cb), cylinder);
900
901         /*
902          * Cylinder gas mix: Air, Nitrox or Trimix
903          */
904         hbox = frame_box("Gasmix", vbox);
905
906         widget = labeled_spinbutton(hbox, "O"UTF8_SUBSCRIPT_2 "%", 1, 100, 0.1);
907         cylinder->o2 = widget;
908         widget = labeled_spinbutton(hbox, "He%", 0, 100, 0.1);
909         cylinder->he = widget;
910         cylinder->gasmix_button = gtk_check_button_new();
911         gtk_box_pack_start(GTK_BOX(hbox), cylinder->gasmix_button, FALSE, FALSE, 3);
912         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(gasmix_cb), cylinder);
913 }
914
915 static gboolean weight_completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct ws_widget *ws_widget)
916 {
917         const char *desc;
918         unsigned int weight;
919
920         gtk_tree_model_get(model, iter, WS_DESC, &desc, WS_WEIGHT, &weight, -1);
921         add_weightsystem(ws_widget, desc, weight);
922         return TRUE;
923 }
924
925 static void weight_activate_cb(GtkComboBox *combo_box, gpointer data)
926 {
927         struct ws_widget *ws_widget = data;
928         weight_cb(ws_widget->description, data);
929 }
930
931 static void ws_widget(GtkWidget *vbox, struct ws_widget *ws_widget, GtkListStore *model)
932 {
933         GtkWidget *frame, *hbox;
934         GtkEntryCompletion *completion;
935         GtkWidget *widget;
936         GtkEntry *entry;
937
938         /*
939          * weight_system: description and weight
940          */
941         frame = gtk_frame_new("Weight");
942
943         hbox = gtk_hbox_new(FALSE, 3);
944         gtk_container_add(GTK_CONTAINER(frame), hbox);
945
946         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
947         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
948
949         ws_widget->description = GTK_COMBO_BOX(widget);
950         g_signal_connect(widget, "changed", G_CALLBACK(weight_cb), ws_widget);
951
952         entry = GTK_ENTRY(GTK_BIN(widget)->child);
953         g_signal_connect(entry, "activate", G_CALLBACK(weight_activate_cb), ws_widget);
954
955         completion = gtk_entry_completion_new();
956         gtk_entry_completion_set_text_column(completion, 0);
957         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
958         g_signal_connect(completion, "match-selected", G_CALLBACK(weight_completion_cb), ws_widget);
959         gtk_entry_set_completion(entry, completion);
960
961         hbox = gtk_hbox_new(FALSE, 3);
962         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
963         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
964
965         if ( output_units.weight == KG)
966                 widget = create_spinbutton(hbox, "kg", 0, 50, 0.5);
967         else
968                 widget = create_spinbutton(hbox, "lbs", 0, 110, 1);
969         ws_widget->weight = GTK_SPIN_BUTTON(widget);
970 }
971
972 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
973 {
974         int success;
975         GtkWidget *dialog, *vbox;
976         struct cylinder_widget cylinder;
977         struct dive *dive;
978
979         cylinder.index = index;
980         cylinder.changed = 0;
981
982         dive = current_dive;
983         if (!dive)
984                 return 0;
985         *cyl = dive->cylinder[index];
986
987         dialog = gtk_dialog_new_with_buttons("Cylinder",
988                 GTK_WINDOW(main_window),
989                 GTK_DIALOG_DESTROY_WITH_PARENT,
990                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
991                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
992                 NULL);
993
994         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
995         cylinder_widget(vbox, &cylinder, cylinder_model);
996
997         show_cylinder(cyl, &cylinder);
998
999         gtk_widget_show_all(dialog);
1000         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
1001         if (success) {
1002                 record_cylinder_changes(cyl, &cylinder);
1003                 dive->cylinder[index] = *cyl;
1004                 mark_divelist_changed(TRUE);
1005                 update_cylinder_related_info(dive);
1006                 flush_divelist(dive);
1007         }
1008
1009         gtk_widget_destroy(dialog);
1010
1011         return success;
1012 }
1013
1014 static int edit_weightsystem_dialog(int index, weightsystem_t *ws)
1015 {
1016         int success;
1017         GtkWidget *dialog, *vbox;
1018         struct ws_widget weightsystem_widget;
1019         struct dive *dive;
1020
1021         weightsystem_widget.index = index;
1022         weightsystem_widget.changed = 0;
1023
1024         dive = current_dive;
1025         if (!dive)
1026                 return 0;
1027         *ws = dive->weightsystem[index];
1028
1029         dialog = gtk_dialog_new_with_buttons("Weight System",
1030                 GTK_WINDOW(main_window),
1031                 GTK_DIALOG_DESTROY_WITH_PARENT,
1032                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1033                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1034                 NULL);
1035
1036         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1037         ws_widget(vbox, &weightsystem_widget, weightsystem_model);
1038
1039         show_weightsystem(ws, &weightsystem_widget);
1040
1041         gtk_widget_show_all(dialog);
1042         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
1043         if (success) {
1044                 record_weightsystem_changes(ws, &weightsystem_widget);
1045                 dive->weightsystem[index] = *ws;
1046                 mark_divelist_changed(TRUE);
1047                 flush_divelist(dive);
1048         }
1049
1050         gtk_widget_destroy(dialog);
1051
1052         return success;
1053 }
1054
1055 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
1056 {
1057         int *p, index;
1058         GtkTreePath *path;
1059
1060         path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
1061         p = gtk_tree_path_get_indices(path);
1062         index = p ? *p : 0;
1063         gtk_tree_path_free(path);
1064         return index;
1065 }
1066
1067 static void edit_cb(GtkButton *button, int w_idx)
1068 {
1069         int index;
1070         GtkTreeIter iter;
1071         GtkListStore *model = cylinder_list[w_idx].model;
1072         GtkTreeView *tree_view = cylinder_list[w_idx].tree_view;
1073         GtkTreeSelection *selection;
1074         cylinder_t cyl;
1075
1076         selection = gtk_tree_view_get_selection(tree_view);
1077
1078         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1079         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1080                 return;
1081
1082         index = get_model_index(model, &iter);
1083         if (!edit_cylinder_dialog(index, &cyl))
1084                 return;
1085
1086         set_one_cylinder(&cyl, model, &iter);
1087         repaint_dive();
1088 }
1089
1090 static void add_cb(GtkButton *button, int w_idx)
1091 {
1092         int index = cylinder_list[w_idx].max_index;
1093         GtkTreeIter iter;
1094         GtkListStore *model = cylinder_list[w_idx].model;
1095         GtkTreeView *tree_view = cylinder_list[w_idx].tree_view;
1096         GtkTreeSelection *selection;
1097         cylinder_t cyl;
1098
1099         if (!edit_cylinder_dialog(index, &cyl))
1100                 return;
1101
1102         gtk_list_store_append(model, &iter);
1103         set_one_cylinder(&cyl, model, &iter);
1104
1105         selection = gtk_tree_view_get_selection(tree_view);
1106         gtk_tree_selection_select_iter(selection, &iter);
1107
1108         cylinder_list[w_idx].max_index++;
1109         gtk_widget_set_sensitive(cylinder_list[w_idx].add, cylinder_list[w_idx].max_index < MAX_CYLINDERS);
1110 }
1111
1112 static void del_cb(GtkButton *button, int w_idx)
1113 {
1114         int index, nr;
1115         GtkTreeIter iter;
1116         GtkListStore *model = cylinder_list[w_idx].model;
1117         GtkTreeView *tree_view = cylinder_list[w_idx].tree_view;
1118         GtkTreeSelection *selection;
1119         struct dive *dive;
1120         cylinder_t *cyl;
1121
1122         selection = gtk_tree_view_get_selection(tree_view);
1123
1124         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1125         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1126                 return;
1127
1128         index = get_model_index(model, &iter);
1129
1130         dive = current_dive;
1131         if (!dive)
1132                 return;
1133         cyl = dive->cylinder + index;
1134         nr = cylinder_list[w_idx].max_index - index - 1;
1135
1136         gtk_list_store_remove(model, &iter);
1137
1138         cylinder_list[w_idx].max_index--;
1139         memmove(cyl, cyl+1, nr*sizeof(*cyl));
1140         memset(cyl+nr, 0, sizeof(*cyl));
1141
1142         mark_divelist_changed(TRUE);
1143         flush_divelist(dive);
1144
1145         gtk_widget_set_sensitive(cylinder_list[w_idx].edit, 0);
1146         gtk_widget_set_sensitive(cylinder_list[w_idx].del, 0);
1147         gtk_widget_set_sensitive(cylinder_list[w_idx].add, 1);
1148 }
1149
1150 static void ws_edit_cb(GtkButton *button, int w_idx)
1151 {
1152         int index;
1153         GtkTreeIter iter;
1154         GtkListStore *model = weightsystem_list[w_idx].model;
1155         GtkTreeView *tree_view = weightsystem_list[w_idx].tree_view;
1156         GtkTreeSelection *selection;
1157         weightsystem_t ws;
1158
1159         selection = gtk_tree_view_get_selection(tree_view);
1160
1161         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1162         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1163                 return;
1164
1165         index = get_model_index(model, &iter);
1166         if (!edit_weightsystem_dialog(index, &ws))
1167                 return;
1168
1169         set_one_weightsystem(&ws, model, &iter);
1170         repaint_dive();
1171 }
1172
1173 static void ws_add_cb(GtkButton *button, int w_idx)
1174 {
1175         int index = weightsystem_list[w_idx].max_index;
1176         GtkTreeIter iter;
1177         GtkListStore *model = weightsystem_list[w_idx].model;
1178         GtkTreeView *tree_view = weightsystem_list[w_idx].tree_view;
1179         GtkTreeSelection *selection;
1180         weightsystem_t ws;
1181
1182         if (!edit_weightsystem_dialog(index, &ws))
1183                 return;
1184
1185         gtk_list_store_append(model, &iter);
1186         set_one_weightsystem(&ws, model, &iter);
1187
1188         selection = gtk_tree_view_get_selection(tree_view);
1189         gtk_tree_selection_select_iter(selection, &iter);
1190
1191         weightsystem_list[w_idx].max_index++;
1192         gtk_widget_set_sensitive(weightsystem_list[w_idx].add, weightsystem_list[w_idx].max_index < MAX_WEIGHTSYSTEMS);
1193 }
1194
1195 static void ws_del_cb(GtkButton *button, int w_idx)
1196 {
1197         int index, nr;
1198         GtkTreeIter iter;
1199         GtkListStore *model = weightsystem_list[w_idx].model;
1200         GtkTreeView *tree_view = weightsystem_list[w_idx].tree_view;
1201         GtkTreeSelection *selection;
1202         struct dive *dive;
1203         weightsystem_t *ws;
1204
1205         selection = gtk_tree_view_get_selection(tree_view);
1206
1207         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1208         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1209                 return;
1210
1211         index = get_model_index(model, &iter);
1212
1213         dive = current_dive;
1214         if (!dive)
1215                 return;
1216         ws = dive->weightsystem + index;
1217         nr = weightsystem_list[w_idx].max_index - index - 1;
1218
1219         gtk_list_store_remove(model, &iter);
1220
1221         weightsystem_list[w_idx].max_index--;
1222         memmove(ws, ws+1, nr*sizeof(*ws));
1223         memset(ws+nr, 0, sizeof(*ws));
1224
1225         mark_divelist_changed(TRUE);
1226         flush_divelist(dive);
1227
1228         gtk_widget_set_sensitive(weightsystem_list[w_idx].edit, 0);
1229         gtk_widget_set_sensitive(weightsystem_list[w_idx].del, 0);
1230         gtk_widget_set_sensitive(weightsystem_list[w_idx].add, 1);
1231 }
1232
1233 static GtkListStore *create_tank_size_model(void)
1234 {
1235         GtkListStore *model;
1236
1237         model = gtk_list_store_new(3,
1238                 G_TYPE_STRING,          /* Tank name */
1239                 G_TYPE_INT,             /* Tank size in mliter */
1240                 G_TYPE_INT,             /* Tank working pressure in mbar */
1241                 -1);
1242
1243         fill_tank_list(model);
1244         return model;
1245 }
1246
1247 static GtkListStore *create_weightsystem_model(void)
1248 {
1249         GtkListStore *model;
1250
1251         model = gtk_list_store_new(2,
1252                 G_TYPE_STRING,          /* Weightsystem description */
1253                 G_TYPE_INT,             /* Weight in grams */
1254                 -1);
1255
1256         fill_ws_list(model);
1257         return model;
1258 }
1259
1260 static void size_data_func(GtkTreeViewColumn *col,
1261                            GtkCellRenderer *renderer,
1262                            GtkTreeModel *model,
1263                            GtkTreeIter *iter,
1264                            gpointer data)
1265 {
1266         int ml, mbar;
1267         double size, pressure;
1268         char buffer[10];
1269
1270         gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
1271         convert_volume_pressure(ml, mbar, &size, &pressure);
1272         if (size)
1273                 snprintf(buffer, sizeof(buffer), "%.1f", size);
1274         else
1275                 strcpy(buffer, "unkn");
1276         g_object_set(renderer, "text", buffer, NULL);
1277 }
1278
1279 static void weight_data_func(GtkTreeViewColumn *col,
1280                            GtkCellRenderer *renderer,
1281                            GtkTreeModel *model,
1282                            GtkTreeIter *iter,
1283                            gpointer data)
1284 {
1285         int idx = (long)data;
1286         int grams, decimals;
1287         double value;
1288         char buffer[10];
1289
1290         gtk_tree_model_get(model, iter, idx, &grams, -1);
1291         decimals = convert_weight(grams, &value);
1292         if (grams)
1293                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
1294         else
1295                 strcpy(buffer, "unkn");
1296         g_object_set(renderer, "text", buffer, NULL);
1297 }
1298
1299 static void pressure_data_func(GtkTreeViewColumn *col,
1300                            GtkCellRenderer *renderer,
1301                            GtkTreeModel *model,
1302                            GtkTreeIter *iter,
1303                            gpointer data)
1304 {
1305         int index = (long)data;
1306         int mbar, decimals;
1307         double pressure;
1308         char buffer[10];
1309
1310         gtk_tree_model_get(model, iter, index, &mbar, -1);
1311         decimals = convert_pressure(mbar, &pressure);
1312         if (mbar)
1313                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
1314         else
1315                 *buffer = 0;
1316         g_object_set(renderer, "text", buffer, NULL);
1317 }
1318
1319 static void percentage_data_func(GtkTreeViewColumn *col,
1320                            GtkCellRenderer *renderer,
1321                            GtkTreeModel *model,
1322                            GtkTreeIter *iter,
1323                            gpointer data)
1324 {
1325         int index = (long)data;
1326         int permille;
1327         char buffer[10];
1328
1329         gtk_tree_model_get(model, iter, index, &permille, -1);
1330         if (permille)
1331                 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
1332         else
1333                 *buffer = 0;
1334         g_object_set(renderer, "text", buffer, NULL);
1335 }
1336
1337 static void selection_cb(GtkTreeSelection *selection, struct equipment_list *list)
1338 {
1339         GtkTreeIter iter;
1340         int selected;
1341
1342         selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
1343         gtk_widget_set_sensitive(list->edit, selected);
1344         gtk_widget_set_sensitive(list->del, selected);
1345 }
1346
1347 static void row_activated_cb(GtkTreeView *tree_view,
1348                         GtkTreePath *path,
1349                         GtkTreeViewColumn *column,
1350                         int w_idx)
1351 {
1352         edit_cb(NULL, w_idx);
1353 }
1354
1355 static void ws_row_activated_cb(GtkTreeView *tree_view,
1356                         GtkTreePath *path,
1357                         GtkTreeViewColumn *column,
1358                         int w_idx)
1359 {
1360         ws_edit_cb(NULL, w_idx);
1361 }
1362
1363 GtkWidget *cylinder_list_widget(int w_idx)
1364 {
1365         GtkListStore *model = cylinder_list[w_idx].model;
1366         GtkWidget *tree_view;
1367         GtkTreeSelection *selection;
1368
1369         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1370         gtk_widget_set_can_focus(tree_view, FALSE);
1371
1372         g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), GINT_TO_POINTER(w_idx));
1373
1374         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1375         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1376         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &cylinder_list[w_idx]);
1377
1378         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1379                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1380                                           NULL);
1381
1382         tree_view_column(tree_view, CYL_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1383         tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, ALIGN_RIGHT | UNSORTABLE);
1384         tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1385         tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1386         tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1387         tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1388         tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1389         return tree_view;
1390 }
1391
1392 GtkWidget *weightsystem_list_widget(int w_idx)
1393 {
1394         GtkListStore *model = weightsystem_list[w_idx].model;
1395         GtkWidget *tree_view;
1396         GtkTreeSelection *selection;
1397
1398         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1399         gtk_widget_set_can_focus(tree_view, FALSE);
1400         g_signal_connect(tree_view, "row-activated", G_CALLBACK(ws_row_activated_cb), GINT_TO_POINTER(w_idx));
1401
1402         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1403         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1404         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &weightsystem_list[w_idx]);
1405
1406         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1407                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1408                                           NULL);
1409
1410         tree_view_column(tree_view, WS_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1411         tree_view_column(tree_view, WS_WEIGHT, "weight",
1412                         weight_data_func, ALIGN_RIGHT | UNSORTABLE);
1413
1414         return tree_view;
1415 }
1416
1417 static GtkWidget *cylinder_list_create(int w_idx)
1418 {
1419         GtkListStore *model;
1420
1421         model = gtk_list_store_new(CYL_COLUMNS,
1422                 G_TYPE_STRING,          /* CYL_DESC: utf8 */
1423                 G_TYPE_INT,             /* CYL_SIZE: mliter */
1424                 G_TYPE_INT,             /* CYL_WORKP: mbar */
1425                 G_TYPE_INT,             /* CYL_STARTP: mbar */
1426                 G_TYPE_INT,             /* CYL_ENDP: mbar */
1427                 G_TYPE_INT,             /* CYL_O2: permille */
1428                 G_TYPE_INT              /* CYL_HE: permille */
1429                 );
1430         cylinder_list[w_idx].model = model;
1431         return cylinder_list_widget(w_idx);
1432 }
1433
1434 static GtkWidget *weightsystem_list_create(int w_idx)
1435 {
1436         GtkListStore *model;
1437
1438         model = gtk_list_store_new(WS_COLUMNS,
1439                 G_TYPE_STRING,          /* WS_DESC: utf8 */
1440                 G_TYPE_INT              /* WS_WEIGHT: grams */
1441                 );
1442         weightsystem_list[w_idx].model = model;
1443         return weightsystem_list_widget(w_idx);
1444 }
1445
1446 GtkWidget *equipment_widget(int w_idx)
1447 {
1448         GtkWidget *vbox, *hbox, *frame, *framebox, *tree_view;
1449         GtkWidget *add, *del, *edit;
1450
1451         vbox = gtk_vbox_new(FALSE, 3);
1452
1453         /*
1454          * We create the cylinder size (and weightsystem) models
1455          * at startup for the primary cylinder / weightsystem widget,
1456          * since we're going to share it across all cylinders and all
1457          * dives. So if you add a new cylinder type or weightsystem in
1458          * one dive, it will show up when you edit the cylinder types
1459          * or weightsystems for another dive.
1460          */
1461         if (w_idx == W_IDX_PRIMARY)
1462                 cylinder_model = create_tank_size_model();
1463         tree_view = cylinder_list_create(w_idx);
1464         cylinder_list[w_idx].tree_view = GTK_TREE_VIEW(tree_view);
1465
1466         hbox = gtk_hbox_new(FALSE, 3);
1467         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1468
1469         frame = gtk_frame_new("Cylinders");
1470         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1471
1472         framebox = gtk_vbox_new(FALSE, 3);
1473         gtk_container_add(GTK_CONTAINER(frame), framebox);
1474
1475         hbox = gtk_hbox_new(FALSE, 3);
1476         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1477
1478         gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1479
1480         hbox = gtk_hbox_new(TRUE, 3);
1481         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1482
1483         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1484         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1485         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1486         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1487         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1488         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1489
1490         cylinder_list[w_idx].edit = edit;
1491         cylinder_list[w_idx].add = add;
1492         cylinder_list[w_idx].del = del;
1493
1494         g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), GINT_TO_POINTER(w_idx));
1495         g_signal_connect(add, "clicked", G_CALLBACK(add_cb), GINT_TO_POINTER(w_idx));
1496         g_signal_connect(del, "clicked", G_CALLBACK(del_cb), GINT_TO_POINTER(w_idx));
1497
1498         hbox = gtk_hbox_new(FALSE, 3);
1499         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1500
1501         if (w_idx == W_IDX_PRIMARY)
1502                 weightsystem_model = create_weightsystem_model();
1503         tree_view = weightsystem_list_create(w_idx);
1504         weightsystem_list[w_idx].tree_view = GTK_TREE_VIEW(tree_view);
1505
1506         frame = gtk_frame_new("Weight");
1507         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1508
1509         framebox = gtk_vbox_new(FALSE, 3);
1510         gtk_container_add(GTK_CONTAINER(frame), framebox);
1511
1512         hbox = gtk_hbox_new(FALSE, 3);
1513         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1514
1515         gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1516
1517         hbox = gtk_hbox_new(TRUE, 3);
1518         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1519
1520         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1521         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1522         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1523         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1524         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1525         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1526
1527         weightsystem_list[w_idx].edit = edit;
1528         weightsystem_list[w_idx].add = add;
1529         weightsystem_list[w_idx].del = del;
1530
1531         g_signal_connect(edit, "clicked", G_CALLBACK(ws_edit_cb), GINT_TO_POINTER(w_idx));
1532         g_signal_connect(add, "clicked", G_CALLBACK(ws_add_cb), GINT_TO_POINTER(w_idx));
1533         g_signal_connect(del, "clicked", G_CALLBACK(ws_del_cb), GINT_TO_POINTER(w_idx));
1534
1535         return vbox;
1536 }