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