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