]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Add the "Common European Cylinders" as per Henrik
[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;
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 static struct {
35         int max_index;
36         GtkListStore *model;
37         GtkWidget *edit, *add, *del;
38 } cylinder_list;
39
40 struct cylinder_widget {
41         int index, changed;
42         const char *name;
43         GtkWidget *hbox;
44         GtkComboBox *description;
45         GtkSpinButton *size, *pressure;
46         GtkWidget *start, *end, *pressure_button;
47         GtkWidget *o2, *he, *gasmix_button;
48 };
49
50 /* we want bar - so let's not use our unit functions */
51 static int convert_pressure(int mbar, double *p)
52 {
53         int decimals = 1;
54         double pressure;
55
56         if (output_units.pressure == PSI) {
57                 pressure = mbar_to_PSI(mbar);
58                 decimals = 0;
59         } else {
60                 pressure = mbar / 1000.0;
61         }
62
63         *p = pressure;
64         return decimals;
65 }
66
67 static int convert_volume_pressure(int ml, int mbar, double *v, double *p)
68 {
69         int decimals = 1;
70         double volume, pressure;
71
72         volume = ml / 1000.0;
73         if (mbar) {
74                 if (output_units.volume == CUFT) {
75                         volume = ml_to_cuft(ml);
76                         volume *= bar_to_atm(mbar / 1000.0);
77                 }
78
79                 if (output_units.pressure == PSI) {
80                         pressure = mbar_to_PSI(mbar);
81                         decimals = 0;
82                 } else
83                         pressure = mbar / 1000.0;
84         }
85         *v = volume;
86         *p = pressure;
87         return decimals;
88 }
89
90 static void set_cylinder_type_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
91 {
92         double volume, pressure;
93
94         convert_volume_pressure(ml, mbar, &volume, &pressure);
95         gtk_spin_button_set_value(cylinder->size, volume);
96         gtk_spin_button_set_value(cylinder->pressure, pressure);
97 }
98
99 static void set_cylinder_pressure_spinbuttons(struct cylinder_widget *cylinder, cylinder_t *cyl)
100 {
101         int set;
102         unsigned int start, end;
103         double pressure;
104
105         start = cyl->start.mbar;
106         end = cyl->end.mbar;
107         set = start || end;
108         if (!set) {
109                 start = cyl->sample_start.mbar;
110                 end = cyl->sample_end.mbar;
111         }
112         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button), set);
113         gtk_widget_set_sensitive(cylinder->start, set);
114         gtk_widget_set_sensitive(cylinder->end, set);
115
116         convert_pressure(start, &pressure);
117         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->start), pressure);
118         convert_pressure(end, &pressure);
119         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->end), pressure);
120 }
121
122 /*
123  * The gtk_tree_model_foreach() interface is bad. It could have
124  * returned whether the callback ever returned true
125  */
126 static GtkTreeIter *found_match = NULL;
127 static GtkTreeIter match_iter;
128
129 static gboolean match_cylinder(GtkTreeModel *model,
130                                 GtkTreePath *path,
131                                 GtkTreeIter *iter,
132                                 gpointer data)
133 {
134         int match;
135         gchar *name;
136         const char *desc = data;
137
138         gtk_tree_model_get(model, iter, 0, &name, -1);
139         match = !strcmp(desc, name);
140         g_free(name);
141         if (match) {
142                 match_iter = *iter;
143                 found_match = &match_iter;
144         }
145         return match;
146 }
147
148 static int get_active_cylinder(GtkComboBox *combo_box, GtkTreeIter *iter)
149 {
150         char *desc;
151
152         if (gtk_combo_box_get_active_iter(combo_box, iter))
153                 return TRUE;
154
155         desc = gtk_combo_box_get_active_text(combo_box);
156
157         found_match = NULL;
158         gtk_tree_model_foreach(GTK_TREE_MODEL(cylinder_model), match_cylinder, (void *)desc);
159
160         g_free(desc);
161         if (!found_match)
162                 return FALSE;
163
164         *iter = *found_match;
165         gtk_combo_box_set_active_iter(combo_box, iter);
166         return TRUE;
167 }
168
169 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
170 {
171         GtkTreeIter iter;
172         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
173         int ml, mbar;
174         struct cylinder_widget *cylinder = data;
175         cylinder_t *cyl = current_dive->cylinder + cylinder->index;
176
177         /* Did the user set it to some non-standard value? */
178         if (!get_active_cylinder(combo_box, &iter)) {
179                 cylinder->changed = 1;
180                 return;
181         }
182
183         /*
184          * We get "change" signal callbacks just because we set
185          * the description by hand. Whatever. So ignore them if
186          * they are no-ops.
187          */
188         if (!cylinder->changed && cyl->type.description) {
189                 int same;
190                 char *desc = gtk_combo_box_get_active_text(combo_box);
191
192                 same = !strcmp(desc, cyl->type.description);
193                 g_free(desc);
194                 if (same)
195                         return;
196         }
197         cylinder->changed = 1;
198
199         gtk_tree_model_get(model, &iter,
200                 CYL_SIZE, &ml,
201                 CYL_WORKP, &mbar,
202                 -1);
203
204         set_cylinder_type_spinbuttons(cylinder, ml, mbar);
205 }
206
207 static GtkTreeIter *add_cylinder_type(const char *desc, int ml, int mbar, GtkTreeIter *iter)
208 {
209         GtkTreeModel *model;
210
211         /* Don't even bother adding stuff without a size */
212         if (!ml)
213                 return NULL;
214
215         found_match = NULL;
216         model = GTK_TREE_MODEL(cylinder_model);
217         gtk_tree_model_foreach(model, match_cylinder, (void *)desc);
218
219         if (!found_match) {
220                 GtkListStore *store = GTK_LIST_STORE(model);
221
222                 gtk_list_store_append(store, iter);
223                 gtk_list_store_set(store, iter,
224                         0, desc,
225                         1, ml,
226                         2, mbar,
227                         -1);
228                 return iter;
229         }
230         return found_match;
231 }
232
233 /*
234  * When adding a dive, we'll add all the pre-existing cylinder
235  * information from that dive to our cylinder model.
236  */
237 void add_cylinder_description(cylinder_type_t *type)
238 {
239         GtkTreeIter iter;
240         const char *desc;
241         unsigned int size, workp;
242
243         desc = type->description;
244         if (!desc)
245                 return;
246         size = type->size.mliter;
247         workp = type->workingpressure.mbar;
248         add_cylinder_type(desc, size, workp, &iter);
249 }
250
251 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
252 {
253         GtkTreeIter iter, *match;
254
255         cylinder->name = desc;
256         match = add_cylinder_type(desc, ml, mbar, &iter);
257         if (match)
258                 gtk_combo_box_set_active_iter(cylinder->description, match);
259 }
260
261 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
262 {
263         const char *desc;
264         int ml, mbar;
265         int gasmix;
266         double o2, he;
267
268         /* Don't show uninitialized cylinder widgets */
269         if (!cylinder->description)
270                 return;
271
272         desc = cyl->type.description;
273         if (!desc)
274                 desc = "";
275         ml = cyl->type.size.mliter;
276         mbar = cyl->type.workingpressure.mbar;
277         add_cylinder(cylinder, desc, ml, mbar);
278
279         set_cylinder_type_spinbuttons(cylinder,
280                 cyl->type.size.mliter, cyl->type.workingpressure.mbar);
281         set_cylinder_pressure_spinbuttons(cylinder, cyl);
282
283         gasmix = cyl->gasmix.o2.permille || cyl->gasmix.he.permille;
284         gtk_widget_set_sensitive(cylinder->o2, gasmix);
285         gtk_widget_set_sensitive(cylinder->he, gasmix);
286         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), gasmix);
287
288         o2 = cyl->gasmix.o2.permille / 10.0;
289         he = cyl->gasmix.he.permille / 10.0;
290         if (!o2)
291                 o2 = 21.0;
292         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
293         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->he), he);
294 }
295
296 int cylinder_none(cylinder_t *cyl)
297 {
298         return  !cyl->type.size.mliter &&
299                 !cyl->type.workingpressure.mbar &&
300                 !cyl->type.description &&
301                 !cyl->gasmix.o2.permille &&
302                 !cyl->gasmix.he.permille &&
303                 !cyl->sample_start.mbar &&
304                 !cyl->sample_end.mbar &&
305                 !cyl->start.mbar &&
306                 !cyl->end.mbar;
307 }
308
309 static void set_one_cylinder(int index, cylinder_t *cyl, GtkListStore *model, GtkTreeIter *iter)
310 {
311         unsigned int start, end;
312
313         start = cyl->start.mbar ? : cyl->sample_start.mbar;
314         end = cyl->end.mbar ? : cyl->sample_end.mbar;
315         gtk_list_store_set(model, iter,
316                 CYL_DESC, cyl->type.description ? : "",
317                 CYL_SIZE, cyl->type.size.mliter,
318                 CYL_WORKP, cyl->type.workingpressure.mbar,
319                 CYL_STARTP, start,
320                 CYL_ENDP, end,
321                 CYL_O2, cyl->gasmix.o2.permille,
322                 CYL_HE, cyl->gasmix.he.permille,
323                 -1);
324 }
325
326 void show_dive_equipment(struct dive *dive)
327 {
328         int i, max;
329         GtkTreeIter iter;
330         GtkListStore *model;
331
332         model = cylinder_list.model;
333         gtk_list_store_clear(model);
334         max = MAX_CYLINDERS;
335         do {
336                 cylinder_t *cyl = &dive->cylinder[max-1];
337
338                 if (!cylinder_none(cyl))
339                         break;
340         } while (--max);
341
342         cylinder_list.max_index = max;
343
344         gtk_widget_set_sensitive(cylinder_list.edit, 0);
345         gtk_widget_set_sensitive(cylinder_list.del, 0);
346         gtk_widget_set_sensitive(cylinder_list.add, max < MAX_CYLINDERS);
347
348         for (i = 0; i < max; i++) {
349                 cylinder_t *cyl = dive->cylinder+i;
350
351                 gtk_list_store_append(model, &iter);
352                 set_one_cylinder(i, cyl, model, &iter);
353         }
354 }
355
356 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
357 {
358         GtkWidget *frame, *hbox, *button;
359
360         frame = gtk_frame_new(name);
361         gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
362
363         hbox = gtk_hbox_new(FALSE, 3);
364         gtk_container_add(GTK_CONTAINER(frame), hbox);
365
366         button = gtk_spin_button_new_with_range(min, max, incr);
367         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
368
369         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
370
371         return button;
372 }
373
374 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
375                 double volume, double pressure, double start, double end, int o2, int he)
376 {
377         int mbar, ml;
378
379         if (output_units.pressure == PSI) {
380                 pressure = psi_to_bar(pressure);
381                 start = psi_to_bar(start);
382                 end = psi_to_bar(end);
383         }
384
385         if (pressure && output_units.volume == CUFT) {
386                 volume = cuft_to_l(volume);
387                 volume /= bar_to_atm(pressure);
388         }
389
390         ml = volume * 1000 + 0.5;
391         mbar = pressure * 1000 + 0.5;
392
393         /* Ignore obviously crazy He values */
394         if (o2 + he > 1000)
395                 he = 0;
396
397         /* We have a rule that normal air is all zeroes */
398         if (!he && o2 > 208 && o2 < 211)
399                 o2 = 0;
400
401         cyl->type.description = desc;
402         cyl->type.size.mliter = ml;
403         cyl->type.workingpressure.mbar = mbar;
404         cyl->start.mbar = start * 1000 + 0.5;
405         cyl->end.mbar = end * 1000 + 0.5;
406         cyl->gasmix.o2.permille = o2;
407         cyl->gasmix.he.permille = he;
408
409         /*
410          * Also, insert it into the model if it doesn't already exist
411          */
412         add_cylinder(cylinder, desc, ml, mbar);
413 }
414
415 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
416 {
417         const gchar *desc;
418         GtkComboBox *box;
419         double volume, pressure, start, end;
420         int o2, he;
421
422         /* Ignore uninitialized cylinder widgets */
423         box = cylinder->description;
424         if (!box)
425                 return;
426
427         desc = gtk_combo_box_get_active_text(box);
428         volume = gtk_spin_button_get_value(cylinder->size);
429         pressure = gtk_spin_button_get_value(cylinder->pressure);
430         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button))) {
431                 start = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->start));
432                 end = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->end));
433         } else {
434                 start = end = 0;
435         }
436         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button))) {
437                 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
438                 he = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->he))*10 + 0.5;
439         } else {
440                 o2 = 0;
441                 he = 0;
442         }
443         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2, he);
444 }
445
446 /*
447  * We hardcode the most common standard cylinders,
448  * we should pick up any other names from the dive
449  * logs directly.
450  */
451 static struct tank_info {
452         const char *name;
453         int cuft, ml, psi, bar;
454 } tank_info[100] = {
455         /* Need an empty entry for the no-cylinder case */
456         { "", },
457
458         /* Size-only metric cylinders */
459         { "10.0 l", .ml = 10000 },
460         { "11.1 l", .ml = 11100 },
461
462         /* Most common AL cylinders */
463         { "AL50",  .cuft =  50, .psi = 3000 },
464         { "AL63",  .cuft =  63, .psi = 3000 },
465         { "AL72",  .cuft =  72, .psi = 3000 },
466         { "AL80",  .cuft =  80, .psi = 3000 },
467         { "AL100", .cuft = 100, .psi = 3300 },
468
469         /* Somewhat common LP steel cylinders */
470         { "LP85",  .cuft =  85, 2640 },
471         { "LP95",  .cuft =  95, 2640 },
472         { "LP108", .cuft = 108, 2640 },
473         { "LP121", .cuft = 121, 2640 },
474
475         /* Somewhat common HP steel cylinders */
476         { "HP65",  .cuft =  65, .psi = 3442 },
477         { "HP80",  .cuft =  80, .psi = 3442 },
478         { "HP100", .cuft = 100, .psi = 3442 },
479         { "HP119", .cuft = 119, .psi = 3442 },
480         { "HP130", .cuft = 130, .psi = 3442 },
481
482         /* Common European steel cylinders */
483         { "10L 300 bar",  .ml = 10000, .bar = 300 },
484         { "12L 200 bar",  .ml = 12000, .bar = 200 },
485         { "12L 232 bar",  .ml = 12000, .bar = 232 },
486         { "12L 300 bar",  .ml = 12000, .bar = 300 },
487         { "15L 200 bar",  .ml = 15000, .bar = 200 },
488         { "15L 232 bar",  .ml = 15000, .bar = 232 },
489         { "D7 300 bar",   .ml = 14000, .bar = 300 },
490         { "D8.5 232 bar", .ml = 17000, .bar = 232 },
491         { "D12 232 bar",  .ml = 24000, .bar = 232 },
492
493         /* We'll fill in more from the dive log dynamically */
494         { NULL, }
495 };
496
497 static void fill_tank_list(GtkListStore *store)
498 {
499         GtkTreeIter iter;
500         struct tank_info *info = tank_info;
501
502         while (info->name) {
503                 int ml = info->ml;
504                 int cuft = info->cuft;
505                 int psi = info->psi;
506                 int mbar;
507                 double bar = info->bar;
508
509                 /* Is it in cuft and psi? */
510                 if (psi) {
511                         bar = psi_to_bar(psi);
512
513                         if (cuft) {
514                                 double airvolume = cuft_to_l(cuft) * 1000.0;
515                                 double atm = bar_to_atm(bar);
516
517                                 ml = airvolume / atm + 0.5;
518                         }
519                 }
520
521                 mbar = bar * 1000 + 0.5;
522
523                 gtk_list_store_append(store, &iter);
524                 gtk_list_store_set(store, &iter,
525                         0, info->name,
526                         1, ml,
527                         2, mbar,
528                         -1);
529                 info++;
530         }
531 }
532
533 static void gasmix_cb(GtkToggleButton *button, gpointer data)
534 {
535         struct cylinder_widget *cylinder = data;
536         int state;
537
538         state = gtk_toggle_button_get_active(button);
539         gtk_widget_set_sensitive(cylinder->o2, state);
540         gtk_widget_set_sensitive(cylinder->he, state);
541 }
542
543 static void pressure_cb(GtkToggleButton *button, gpointer data)
544 {
545         struct cylinder_widget *cylinder = data;
546         int state;
547
548         state = gtk_toggle_button_get_active(button);
549         gtk_widget_set_sensitive(cylinder->start, state);
550         gtk_widget_set_sensitive(cylinder->end, state);
551 }
552
553 static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder)
554 {
555         const char *desc;
556         unsigned int ml, mbar;
557
558         gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
559         add_cylinder(cylinder, desc, ml, mbar);
560         return TRUE;
561 }
562
563 static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data)
564 {
565         struct cylinder_widget *cylinder = data;
566         cylinder_cb(cylinder->description, data);
567 }
568
569 /* Return a frame containing a hbox inside a hbox */
570 static GtkWidget *frame_box(const char *title, GtkWidget *vbox)
571 {
572         GtkWidget *hbox, *frame;
573
574         hbox = gtk_hbox_new(FALSE, 10);
575         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
576
577         frame = gtk_frame_new(title);
578         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 0);
579
580         hbox = gtk_hbox_new(FALSE, 10);
581         gtk_container_add(GTK_CONTAINER(frame), hbox);
582
583         return hbox;
584 }
585
586 static GtkWidget *labeled_spinbutton(GtkWidget *box, const char *name, double min, double max, double incr)
587 {
588         GtkWidget *hbox, *label, *button;
589
590         hbox = gtk_hbox_new(FALSE, 0);
591         gtk_box_pack_start(GTK_BOX(box), hbox, TRUE, FALSE, 0);
592
593         label = gtk_label_new(name);
594         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
595
596         button = gtk_spin_button_new_with_range(min, max, incr);
597         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
598
599         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
600
601         return button;
602 }
603
604 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
605 {
606         GtkWidget *frame, *hbox;
607         GtkEntry *entry;
608         GtkEntryCompletion *completion;
609         GtkWidget *widget;
610
611         /*
612          * Cylinder type: description, size and
613          * working pressure
614          */
615         frame = gtk_frame_new("Cylinder");
616
617         hbox = gtk_hbox_new(FALSE, 3);
618         gtk_container_add(GTK_CONTAINER(frame), hbox);
619
620         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
621         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
622
623         cylinder->description = GTK_COMBO_BOX(widget);
624         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
625
626         entry = GTK_ENTRY(GTK_BIN(widget)->child);
627         g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder);
628
629         completion = gtk_entry_completion_new();
630         gtk_entry_completion_set_text_column(completion, 0);
631         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
632         g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder);
633         gtk_entry_set_completion(entry, completion);
634
635         hbox = gtk_hbox_new(FALSE, 3);
636         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
637         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
638
639         widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
640         cylinder->size = GTK_SPIN_BUTTON(widget);
641
642         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
643         cylinder->pressure = GTK_SPIN_BUTTON(widget);
644
645         /*
646          * Cylinder start/end pressures
647          */
648         hbox = frame_box("Pressure", vbox);
649
650         widget = labeled_spinbutton(hbox, "Start", 0, 5000, 1);
651         cylinder->start = widget;
652
653         widget = labeled_spinbutton(hbox, "End", 0, 5000, 1);
654         cylinder->end = widget;
655
656         cylinder->pressure_button = gtk_check_button_new();
657         gtk_box_pack_start(GTK_BOX(hbox), cylinder->pressure_button, FALSE, FALSE, 3);
658         g_signal_connect(cylinder->pressure_button, "toggled", G_CALLBACK(pressure_cb), cylinder);
659
660         /*
661          * Cylinder gas mix: Air, Nitrox or Trimix
662          */
663         hbox = frame_box("Gasmix", vbox);
664
665         widget = labeled_spinbutton(hbox, "O"UTF8_SUBSCRIPT_2 "%", 1, 100, 0.1);
666         cylinder->o2 = widget;
667         widget = labeled_spinbutton(hbox, "He%", 0, 100, 0.1);
668         cylinder->he = widget;
669         cylinder->gasmix_button = gtk_check_button_new();
670         gtk_box_pack_start(GTK_BOX(hbox), cylinder->gasmix_button, FALSE, FALSE, 3);
671         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(gasmix_cb), cylinder);
672 }
673
674 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
675 {
676         int success;
677         GtkWidget *dialog, *vbox;
678         struct cylinder_widget cylinder;
679         struct dive *dive;
680
681         cylinder.index = index;
682         cylinder.changed = 0;
683
684         dive = current_dive;
685         if (!dive)
686                 return 0;
687         *cyl = dive->cylinder[index];
688
689         dialog = gtk_dialog_new_with_buttons("Cylinder",
690                 GTK_WINDOW(main_window),
691                 GTK_DIALOG_DESTROY_WITH_PARENT,
692                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
693                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
694                 NULL);
695
696         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
697         cylinder_widget(vbox, &cylinder, cylinder_model);
698
699         show_cylinder(cyl, &cylinder);
700
701         gtk_widget_show_all(dialog);
702         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
703         if (success) {
704                 record_cylinder_changes(cyl, &cylinder);
705                 dive->cylinder[index] = *cyl;
706                 mark_divelist_changed(TRUE);
707                 update_cylinder_related_info(dive);
708                 flush_divelist(dive);
709         }
710
711         gtk_widget_destroy(dialog);
712
713         return success;
714 }
715
716 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
717 {
718         int *p, index;
719         GtkTreePath *path;
720
721         path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
722         p = gtk_tree_path_get_indices(path);
723         index = p ? *p : 0;
724         gtk_tree_path_free(path);
725         return index;
726 }
727
728 static void edit_cb(GtkButton *button, GtkTreeView *tree_view)
729 {
730         int index;
731         GtkTreeIter iter;
732         GtkListStore *model = cylinder_list.model;
733         GtkTreeSelection *selection;
734         cylinder_t cyl;
735
736         selection = gtk_tree_view_get_selection(tree_view);
737
738         /* Nothing selected? This shouldn't happen, since the button should be inactive */
739         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
740                 return;
741
742         index = get_model_index(model, &iter);
743         if (!edit_cylinder_dialog(index, &cyl))
744                 return;
745
746         set_one_cylinder(index, &cyl, model, &iter);
747         repaint_dive();
748 }
749
750 static void add_cb(GtkButton *button, GtkTreeView *tree_view)
751 {
752         int index = cylinder_list.max_index;
753         GtkTreeIter iter;
754         GtkListStore *model = cylinder_list.model;
755         GtkTreeSelection *selection;
756         cylinder_t cyl;
757
758         if (!edit_cylinder_dialog(index, &cyl))
759                 return;
760
761         gtk_list_store_append(model, &iter);
762         set_one_cylinder(index, &cyl, model, &iter);
763
764         selection = gtk_tree_view_get_selection(tree_view);
765         gtk_tree_selection_select_iter(selection, &iter);
766
767         cylinder_list.max_index++;
768         gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
769 }
770
771 static void del_cb(GtkButton *button, GtkTreeView *tree_view)
772 {
773         int index, nr;
774         GtkTreeIter iter;
775         GtkListStore *model = cylinder_list.model;
776         GtkTreeSelection *selection;
777         struct dive *dive;
778         cylinder_t *cyl;
779
780         selection = gtk_tree_view_get_selection(tree_view);
781
782         /* Nothing selected? This shouldn't happen, since the button should be inactive */
783         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
784                 return;
785
786         index = get_model_index(model, &iter);
787
788         dive = current_dive;
789         if (!dive)
790                 return;
791         cyl = dive->cylinder + index;
792         nr = cylinder_list.max_index - index - 1;
793
794         gtk_list_store_remove(model, &iter);
795
796         cylinder_list.max_index--;
797         memmove(cyl, cyl+1, nr*sizeof(*cyl));
798         memset(cyl+nr, 0, sizeof(*cyl));
799
800         mark_divelist_changed(TRUE);
801         flush_divelist(dive);
802
803         gtk_widget_set_sensitive(cylinder_list.edit, 0);
804         gtk_widget_set_sensitive(cylinder_list.del, 0);
805         gtk_widget_set_sensitive(cylinder_list.add, 1);
806 }
807
808 static GtkListStore *create_tank_size_model(void)
809 {
810         GtkListStore *model;
811
812         model = gtk_list_store_new(3,
813                 G_TYPE_STRING,          /* Tank name */
814                 G_TYPE_INT,             /* Tank size in mliter */
815                 G_TYPE_INT,             /* Tank working pressure in mbar */
816                 -1);
817
818         fill_tank_list(model);
819         return model;
820 }
821
822 static void size_data_func(GtkTreeViewColumn *col,
823                            GtkCellRenderer *renderer,
824                            GtkTreeModel *model,
825                            GtkTreeIter *iter,
826                            gpointer data)
827 {
828         int ml, mbar;
829         double size, pressure;
830         char buffer[10];
831
832         gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
833         convert_volume_pressure(ml, mbar, &size, &pressure);
834         if (size)
835                 snprintf(buffer, sizeof(buffer), "%.1f", size);
836         else
837                 strcpy(buffer, "unkn");
838         g_object_set(renderer, "text", buffer, NULL);
839 }
840
841 static void pressure_data_func(GtkTreeViewColumn *col,
842                            GtkCellRenderer *renderer,
843                            GtkTreeModel *model,
844                            GtkTreeIter *iter,
845                            gpointer data)
846 {
847         int index = (long)data;
848         int mbar, decimals;
849         double pressure;
850         char buffer[10];
851
852         gtk_tree_model_get(model, iter, index, &mbar, -1);
853         decimals = convert_pressure(mbar, &pressure);
854         if (mbar)
855                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
856         else
857                 *buffer = 0;
858         g_object_set(renderer, "text", buffer, NULL);
859 }
860
861 static void percentage_data_func(GtkTreeViewColumn *col,
862                            GtkCellRenderer *renderer,
863                            GtkTreeModel *model,
864                            GtkTreeIter *iter,
865                            gpointer data)
866 {
867         int index = (long)data;
868         int permille;
869         char buffer[10];
870
871         gtk_tree_model_get(model, iter, index, &permille, -1);
872         if (permille)
873                 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
874         else
875                 *buffer = 0;
876         g_object_set(renderer, "text", buffer, NULL);
877 }
878
879 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
880 {
881         GtkTreeIter iter;
882         int selected;
883
884         selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
885         gtk_widget_set_sensitive(cylinder_list.edit, selected);
886         gtk_widget_set_sensitive(cylinder_list.del, selected);
887 }
888
889 static void row_activated_cb(GtkTreeView *tree_view,
890                         GtkTreePath *path,
891                         GtkTreeViewColumn *column,
892                         GtkTreeModel *model)
893 {
894         edit_cb(NULL, tree_view);
895 }
896
897 GtkWidget *cylinder_list_widget(void)
898 {
899         GtkListStore *model = cylinder_list.model;
900         GtkWidget *tree_view;
901         GtkTreeSelection *selection;
902
903         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
904         gtk_widget_set_can_focus(tree_view, FALSE);
905
906         g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
907
908         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
909         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
910         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
911
912         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
913                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
914                                           NULL);
915
916         tree_view_column(tree_view, CYL_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
917         tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, ALIGN_RIGHT | UNSORTABLE);
918         tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
919         tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
920         tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
921         tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
922         tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
923         return tree_view;
924 }
925
926 static GtkWidget *cylinder_list_create(void)
927 {
928         GtkListStore *model;
929
930         model = gtk_list_store_new(CYL_COLUMNS,
931                 G_TYPE_STRING,          /* CYL_DESC: utf8 */
932                 G_TYPE_INT,             /* CYL_SIZE: mliter */
933                 G_TYPE_INT,             /* CYL_WORKP: mbar */
934                 G_TYPE_INT,             /* CYL_STARTP: mbar */
935                 G_TYPE_INT,             /* CYL_ENDP: mbar */
936                 G_TYPE_INT,             /* CYL_O2: permille */
937                 G_TYPE_INT              /* CYL_HE: permille */
938                 );
939         cylinder_list.model = model;
940         return cylinder_list_widget();
941 }
942
943 GtkWidget *equipment_widget(void)
944 {
945         GtkWidget *vbox, *hbox, *frame, *framebox, *tree_view;
946         GtkWidget *add, *del, *edit;
947
948         vbox = gtk_vbox_new(FALSE, 3);
949
950         /*
951          * We create the cylinder size model at startup, since
952          * we're going to share it across all cylinders and all
953          * dives. So if you add a new cylinder type in one dive,
954          * it will show up when you edit the cylinder types for
955          * another dive.
956          */
957         cylinder_model = create_tank_size_model();
958
959         tree_view = cylinder_list_create();
960
961         hbox = gtk_hbox_new(FALSE, 3);
962         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
963
964         frame = gtk_frame_new("Cylinders");
965         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
966
967         framebox = gtk_vbox_new(FALSE, 3);
968         gtk_container_add(GTK_CONTAINER(frame), framebox);
969
970         hbox = gtk_hbox_new(FALSE, 3);
971         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
972
973         gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
974
975         hbox = gtk_hbox_new(TRUE, 3);
976         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
977
978         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
979         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
980         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
981         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
982         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
983         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
984
985         cylinder_list.edit = edit;
986         cylinder_list.add = add;
987         cylinder_list.del = del;
988
989         g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), tree_view);
990         g_signal_connect(add, "clicked", G_CALLBACK(add_cb), tree_view);
991         g_signal_connect(del, "clicked", G_CALLBACK(del_cb), tree_view);
992
993         return vbox;
994 }