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