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