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