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