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