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