]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
equipment: clean up cylinder pressure spinbutton setup
[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, cylinder_t *cyl)
102 {
103         double pressure;
104
105         convert_pressure(cyl->start.mbar, &pressure);
106         gtk_spin_button_set_value(cylinder->start, pressure);
107         convert_pressure(cyl->end.mbar, &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, cyl);
270         o2 = cyl->gasmix.o2.permille / 10.0;
271         gtk_widget_set_sensitive(cylinder->o2, !!o2);
272         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
273         if (!o2)
274                 o2 = 21.0;
275         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
276 }
277
278 static int cyl_nothing(cylinder_t *cyl)
279 {
280         return  !cyl->type.size.mliter &&
281                 !cyl->type.workingpressure.mbar &&
282                 !cyl->type.description &&
283                 !cyl->gasmix.o2.permille &&
284                 !cyl->gasmix.he.permille &&
285                 !cyl->sample_start.mbar &&
286                 !cyl->sample_end.mbar &&
287                 !cyl->start.mbar &&
288                 !cyl->end.mbar;
289 }
290
291 static void set_one_cylinder(int index, cylinder_t *cyl, GtkListStore *model, GtkTreeIter *iter)
292 {
293         unsigned int start, end;
294
295         start = cyl->start.mbar ? : cyl->sample_start.mbar;
296         end = cyl->end.mbar ? : cyl->sample_end.mbar;
297         gtk_list_store_set(model, iter,
298                 CYL_DESC, cyl->type.description ? : "",
299                 CYL_SIZE, cyl->type.size.mliter,
300                 CYL_WORKP, cyl->type.workingpressure.mbar,
301                 CYL_STARTP, start,
302                 CYL_ENDP, end,
303                 CYL_O2, cyl->gasmix.o2.permille,
304                 CYL_HE, cyl->gasmix.he.permille,
305                 -1);
306 }
307
308 void show_dive_equipment(struct dive *dive)
309 {
310         int i, max;
311         GtkTreeIter iter;
312         GtkListStore *model;
313
314         model = cylinder_list.model;
315         gtk_list_store_clear(model);
316         max = MAX_CYLINDERS;
317         do {
318                 cylinder_t *cyl = &dive->cylinder[max-1];
319
320                 if (!cyl_nothing(cyl))
321                         break;
322         } while (--max);
323
324         cylinder_list.max_index = max;
325
326         gtk_widget_set_sensitive(cylinder_list.edit, 0);
327         gtk_widget_set_sensitive(cylinder_list.del, 0);
328         gtk_widget_set_sensitive(cylinder_list.add, max < MAX_CYLINDERS);
329
330         for (i = 0; i < max; i++) {
331                 cylinder_t *cyl = dive->cylinder+i;
332
333                 gtk_list_store_append(model, &iter);
334                 set_one_cylinder(i, cyl, model, &iter);
335         }
336 }
337
338 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
339 {
340         GtkWidget *frame, *hbox, *button;
341
342         frame = gtk_frame_new(name);
343         gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
344
345         hbox = gtk_hbox_new(FALSE, 3);
346         gtk_container_add(GTK_CONTAINER(frame), hbox);
347
348         button = gtk_spin_button_new_with_range(min, max, incr);
349         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
350
351         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
352
353         return button;
354 }
355
356 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
357                 double volume, double pressure, double start, double end, int o2)
358 {
359         int mbar, ml;
360
361         if (output_units.pressure == PSI) {
362                 pressure = psi_to_bar(pressure);
363                 start = psi_to_bar(start);
364                 end = psi_to_bar(end);
365         }
366
367         if (pressure && output_units.volume == CUFT) {
368                 volume = cuft_to_l(volume);
369                 volume /= bar_to_atm(pressure);
370         }
371
372         ml = volume * 1000 + 0.5;
373         mbar = pressure * 1000 + 0.5;
374
375         if (o2 < 211)
376                 o2 = 0;
377         cyl->type.description = desc;
378         cyl->type.size.mliter = ml;
379         cyl->type.workingpressure.mbar = mbar;
380         cyl->start.mbar = start * 1000 + 0.5;
381         cyl->end.mbar = end * 1000 + 0.5;
382         cyl->gasmix.o2.permille = o2;
383
384         /*
385          * Also, insert it into the model if it doesn't already exist
386          */
387         add_cylinder(cylinder, desc, ml, mbar);
388 }
389
390 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
391 {
392         const gchar *desc;
393         GtkComboBox *box;
394         double volume, pressure, start, end;
395         int o2;
396
397         /* Ignore uninitialized cylinder widgets */
398         box = cylinder->description;
399         if (!box)
400                 return;
401
402         desc = gtk_combo_box_get_active_text(box);
403         volume = gtk_spin_button_get_value(cylinder->size);
404         pressure = gtk_spin_button_get_value(cylinder->pressure);
405         start = gtk_spin_button_get_value(cylinder->start);
406         end = gtk_spin_button_get_value(cylinder->end);
407         o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
408         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
409                 o2 = 0;
410         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2);
411 }
412
413 void flush_dive_equipment_changes(struct dive *dive)
414 {
415         /* We do nothing: we require the "Ok" button press */
416 }
417
418 /*
419  * We hardcode the most common standard cylinders,
420  * we should pick up any other names from the dive
421  * logs directly.
422  */
423 static struct tank_info {
424         const char *name;
425         int size;       /* cuft if < 1000, otherwise mliter */
426         int psi;        /* If zero, size is in mliter */
427 } tank_info[100] = {
428         /* Need an empty entry for the no-cylinder case */
429         { "", 0, 0 },
430
431         /* Size-only metric cylinders */
432         { "10.0 l", 10000 },
433         { "11.1 l", 11100 },
434
435         /* Most common AL cylinders */
436         { "AL50",   50, 3000 },
437         { "AL63",   63, 3000 },
438         { "AL72",   72, 3000 },
439         { "AL80",   80, 3000 },
440         { "AL100", 100, 3300 },
441
442         /* Somewhat common LP steel cylinders */
443         { "LP85",   85, 2640 },
444         { "LP95",   95, 2640 },
445         { "LP108", 108, 2640 },
446         { "LP121", 121, 2640 },
447
448         /* Somewhat common HP steel cylinders */
449         { "HP65",   65, 3442 },
450         { "HP80",   80, 3442 },
451         { "HP100", 100, 3442 },
452         { "HP119", 119, 3442 },
453         { "HP130", 130, 3442 },
454
455         /* We'll fill in more from the dive log dynamically */
456         { NULL, }
457 };
458
459 static void fill_tank_list(GtkListStore *store)
460 {
461         GtkTreeIter iter;
462         struct tank_info *info = tank_info;
463
464         while (info->name) {
465                 int size = info->size;
466                 int psi = info->psi;
467                 int mbar = 0, ml = size;
468
469                 /* Is it in cuft and psi? */
470                 if (psi) {
471                         double bar = psi_to_bar(psi);
472                         double airvolume = cuft_to_l(size) * 1000.0;
473                         double atm = bar_to_atm(bar);
474
475                         ml = airvolume / atm + 0.5;
476                         mbar = bar*1000 + 0.5;
477                 }
478
479                 gtk_list_store_append(store, &iter);
480                 gtk_list_store_set(store, &iter,
481                         0, info->name,
482                         1, ml,
483                         2, mbar,
484                         -1);
485                 info++;
486         }
487 }
488
489 static void nitrox_cb(GtkToggleButton *button, gpointer data)
490 {
491         struct cylinder_widget *cylinder = data;
492         int state;
493
494         state = gtk_toggle_button_get_active(button);
495         gtk_widget_set_sensitive(cylinder->o2, state);
496 }
497
498 static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder)
499 {
500         const char *desc;
501         unsigned int ml, mbar;
502
503         gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
504         add_cylinder(cylinder, desc, ml, mbar);
505         return TRUE;
506 }
507
508 static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data)
509 {
510         struct cylinder_widget *cylinder = data;
511         cylinder_cb(cylinder->description, data);
512 }
513
514 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
515 {
516         GtkWidget *frame, *hbox;
517         GtkEntry *entry;
518         GtkEntryCompletion *completion;
519         GtkWidget *widget;
520
521         /*
522          * Cylinder type: description, size and
523          * working pressure
524          */
525         frame = gtk_frame_new("Cylinder");
526
527         hbox = gtk_hbox_new(FALSE, 3);
528         gtk_container_add(GTK_CONTAINER(frame), hbox);
529
530         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
531         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
532
533         cylinder->description = GTK_COMBO_BOX(widget);
534         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
535
536         entry = GTK_ENTRY(GTK_BIN(widget)->child);
537         g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder);
538
539         completion = gtk_entry_completion_new();
540         gtk_entry_completion_set_text_column(completion, 0);
541         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
542         g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder);
543         gtk_entry_set_completion(entry, completion);
544
545         hbox = gtk_hbox_new(FALSE, 3);
546         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
547         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
548
549         widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
550         cylinder->size = GTK_SPIN_BUTTON(widget);
551
552         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
553         cylinder->pressure = GTK_SPIN_BUTTON(widget);
554
555         /*
556          * Cylinder start/end pressures
557          */
558         hbox = gtk_hbox_new(FALSE, 3);
559         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
560
561         widget = create_spinbutton(hbox, "Start Pressure", 0, 5000, 1);
562         cylinder->start = GTK_SPIN_BUTTON(widget);
563
564         widget = create_spinbutton(hbox, "End Pressure", 0, 5000, 1);
565         cylinder->end = GTK_SPIN_BUTTON(widget);
566
567         /*
568          * Cylinder gas mix: Air, Nitrox or Trimix
569          */
570         hbox = gtk_hbox_new(FALSE, 3);
571         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
572
573         widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
574         cylinder->o2 = widget;
575         cylinder->gasmix_button = gtk_check_button_new();
576         gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
577                 cylinder->gasmix_button, FALSE, FALSE, 3);
578         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
579
580         gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
581 }
582
583 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
584 {
585         int success;
586         GtkWidget *dialog, *vbox;
587         struct cylinder_widget cylinder;
588         struct dive *dive;
589
590         cylinder.index = index;
591         cylinder.changed = 0;
592
593         dive = current_dive;
594         if (!dive)
595                 return 0;
596         *cyl = dive->cylinder[index];
597
598         dialog = gtk_dialog_new_with_buttons("Cylinder",
599                 GTK_WINDOW(main_window),
600                 GTK_DIALOG_DESTROY_WITH_PARENT,
601                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
602                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
603                 NULL);
604
605         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
606         cylinder_widget(vbox, &cylinder, cylinder_model);
607
608         show_cylinder(cyl, &cylinder);
609
610         gtk_widget_show_all(dialog);
611         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
612         if (success) {
613                 record_cylinder_changes(cyl, &cylinder);
614                 dive->cylinder[index] = *cyl;
615                 mark_divelist_changed(TRUE);
616                 flush_divelist(dive);
617         }
618
619         gtk_widget_destroy(dialog);
620
621         return success;
622 }
623
624 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
625 {
626         int *p, index;
627         GtkTreePath *path;
628
629         path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
630         p = gtk_tree_path_get_indices(path);
631         index = p ? *p : 0;
632         gtk_tree_path_free(path);
633         return index;
634 }
635
636 static void edit_cb(GtkButton *button, gpointer data)
637 {
638         int index;
639         GtkTreeIter iter;
640         GtkListStore *model = cylinder_list.model;
641         GtkTreeSelection *selection;
642         cylinder_t cyl;
643
644         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
645
646         /* Nothing selected? This shouldn't happen, since the button should be inactive */
647         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
648                 return;
649
650         index = get_model_index(model, &iter);
651         if (!edit_cylinder_dialog(index, &cyl))
652                 return;
653
654         set_one_cylinder(index, &cyl, model, &iter);
655         repaint_dive();
656 }
657
658 static void add_cb(GtkButton *button, gpointer data)
659 {
660         int index = cylinder_list.max_index;
661         GtkTreeIter iter;
662         GtkListStore *model = cylinder_list.model;
663         GtkTreeSelection *selection;
664         cylinder_t cyl;
665
666         if (!edit_cylinder_dialog(index, &cyl))
667                 return;
668
669         gtk_list_store_append(model, &iter);
670         set_one_cylinder(index, &cyl, model, &iter);
671
672         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
673         gtk_tree_selection_select_iter(selection, &iter);
674
675         cylinder_list.max_index++;
676         gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
677 }
678
679 static void del_cb(GtkButton *button, gpointer data)
680 {
681         int index, nr;
682         GtkTreeIter iter;
683         GtkListStore *model = cylinder_list.model;
684         GtkTreeSelection *selection;
685         struct dive *dive;
686         cylinder_t *cyl;
687
688         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
689
690         /* Nothing selected? This shouldn't happen, since the button should be inactive */
691         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
692                 return;
693
694         index = get_model_index(model, &iter);
695
696         dive = current_dive;
697         if (!dive)
698                 return;
699         cyl = dive->cylinder + index;
700         nr = cylinder_list.max_index - index - 1;
701
702         gtk_list_store_remove(model, &iter);
703
704         cylinder_list.max_index--;
705         memmove(cyl, cyl+1, nr*sizeof(*cyl));
706         memset(cyl+nr, 0, sizeof(*cyl));
707
708         mark_divelist_changed(TRUE);
709         flush_divelist(dive);
710
711         gtk_widget_set_sensitive(cylinder_list.edit, 0);
712         gtk_widget_set_sensitive(cylinder_list.del, 0);
713         gtk_widget_set_sensitive(cylinder_list.add, 1);
714 }
715
716 static GtkListStore *create_tank_size_model(void)
717 {
718         GtkListStore *model;
719
720         model = gtk_list_store_new(3,
721                 G_TYPE_STRING,          /* Tank name */
722                 G_TYPE_INT,             /* Tank size in mliter */
723                 G_TYPE_INT,             /* Tank working pressure in mbar */
724                 -1);
725
726         fill_tank_list(model);
727         return model;
728 }
729
730 static void size_data_func(GtkTreeViewColumn *col,
731                            GtkCellRenderer *renderer,
732                            GtkTreeModel *model,
733                            GtkTreeIter *iter,
734                            gpointer data)
735 {
736         int ml, mbar;
737         double size, pressure;
738         char buffer[10];
739
740         gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
741         convert_volume_pressure(ml, mbar, &size, &pressure);
742         if (size)
743                 snprintf(buffer, sizeof(buffer), "%.1f", size);
744         else
745                 strcpy(buffer, "unkn");
746         g_object_set(renderer, "text", buffer, NULL);
747 }
748
749 static void pressure_data_func(GtkTreeViewColumn *col,
750                            GtkCellRenderer *renderer,
751                            GtkTreeModel *model,
752                            GtkTreeIter *iter,
753                            gpointer data)
754 {
755         int index = (long)data;
756         int mbar, decimals;
757         double pressure;
758         char buffer[10];
759
760         gtk_tree_model_get(model, iter, index, &mbar, -1);
761         decimals = convert_pressure(mbar, &pressure);
762         if (mbar)
763                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
764         else
765                 *buffer = 0;
766         g_object_set(renderer, "text", buffer, NULL);
767 }
768
769 static void percentage_data_func(GtkTreeViewColumn *col,
770                            GtkCellRenderer *renderer,
771                            GtkTreeModel *model,
772                            GtkTreeIter *iter,
773                            gpointer data)
774 {
775         int index = (long)data;
776         int permille;
777         char buffer[10];
778
779         gtk_tree_model_get(model, iter, index, &permille, -1);
780         if (permille)
781                 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
782         else
783                 *buffer = 0;
784         g_object_set(renderer, "text", buffer, NULL);
785 }
786
787 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
788 {
789         GtkTreeIter iter;
790         int selected;
791
792         selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
793         gtk_widget_set_sensitive(cylinder_list.edit, selected);
794         gtk_widget_set_sensitive(cylinder_list.del, selected);
795 }
796
797 static void row_activated_cb(GtkTreeView *tree_view,
798                         GtkTreePath *path,
799                         GtkTreeViewColumn *column,
800                         GtkTreeModel *model)
801 {
802         edit_cb(NULL, NULL);
803 }
804
805 static GtkWidget *cylinder_list_create(void)
806 {
807         GtkWidget *tree_view;
808         GtkTreeSelection *selection;
809         GtkListStore *model;
810
811         model = gtk_list_store_new(CYL_COLUMNS,
812                 G_TYPE_STRING,          /* CYL_DESC: utf8 */
813                 G_TYPE_INT,             /* CYL_SIZE: mliter */
814                 G_TYPE_INT,             /* CYL_WORKP: mbar */
815                 G_TYPE_INT,             /* CYL_STARTP: mbar */
816                 G_TYPE_INT,             /* CYL_ENDP: mbar */
817                 G_TYPE_INT,             /* CYL_O2: permille */
818                 G_TYPE_INT              /* CYL_HE: permille */
819                 );
820         cylinder_list.model = model;
821         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
822         g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
823
824         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
825         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
826         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
827
828         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
829                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
830                                           NULL);
831
832         cylinder_list.desc = tree_view_column(tree_view, CYL_DESC, "Type", NULL, PANGO_ALIGN_LEFT, TRUE);
833         cylinder_list.size = tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, PANGO_ALIGN_RIGHT, TRUE);
834         cylinder_list.workp = tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
835         cylinder_list.startp = tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
836         cylinder_list.endp = tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
837         cylinder_list.o2 = tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
838         cylinder_list.he = tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
839         return tree_view;
840 }
841
842 GtkWidget *equipment_widget(void)
843 {
844         GtkWidget *vbox, *hbox, *frame, *framebox;
845         GtkWidget *add, *del, *edit;
846
847         vbox = gtk_vbox_new(FALSE, 3);
848
849         /*
850          * We create the cylinder size model at startup, since
851          * we're going to share it across all cylinders and all
852          * dives. So if you add a new cylinder type in one dive,
853          * it will show up when you edit the cylinder types for
854          * another dive.
855          */
856         cylinder_model = create_tank_size_model();
857
858         cylinder_list.tree_view = cylinder_list_create();
859
860         hbox = gtk_hbox_new(FALSE, 3);
861         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
862
863         frame = gtk_frame_new("Cylinders");
864         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
865
866         framebox = gtk_vbox_new(FALSE, 3);
867         gtk_container_add(GTK_CONTAINER(frame), framebox);
868
869         hbox = gtk_hbox_new(FALSE, 3);
870         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
871
872         gtk_box_pack_start(GTK_BOX(hbox), cylinder_list.tree_view, TRUE, FALSE, 3);
873
874         hbox = gtk_hbox_new(TRUE, 3);
875         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
876
877         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
878         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
879         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
880         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
881         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
882         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
883
884         cylinder_list.edit = edit;
885         cylinder_list.add = add;
886         cylinder_list.del = del;
887
888         g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), NULL);
889         g_signal_connect(add, "clicked", G_CALLBACK(add_cb), NULL);
890         g_signal_connect(del, "clicked", G_CALLBACK(del_cb), NULL);
891
892         return vbox;
893 }