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