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