]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Merge branch 'forlinus' 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         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button))) {
420                 start = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->start));
421                 end = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->end));
422         } else
423                 start = end = 0;
424         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
425                 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
426         else
427                 o2 = 0;
428         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2);
429 }
430
431 void flush_dive_equipment_changes(struct dive *dive)
432 {
433         /* We do nothing: we require the "Ok" button press */
434 }
435
436 /*
437  * We hardcode the most common standard cylinders,
438  * we should pick up any other names from the dive
439  * logs directly.
440  */
441 static struct tank_info {
442         const char *name;
443         int size;       /* cuft if < 1000, otherwise mliter */
444         int psi;        /* If zero, size is in mliter */
445 } tank_info[100] = {
446         /* Need an empty entry for the no-cylinder case */
447         { "", 0, 0 },
448
449         /* Size-only metric cylinders */
450         { "10.0 l", 10000 },
451         { "11.1 l", 11100 },
452
453         /* Most common AL cylinders */
454         { "AL50",   50, 3000 },
455         { "AL63",   63, 3000 },
456         { "AL72",   72, 3000 },
457         { "AL80",   80, 3000 },
458         { "AL100", 100, 3300 },
459
460         /* Somewhat common LP steel cylinders */
461         { "LP85",   85, 2640 },
462         { "LP95",   95, 2640 },
463         { "LP108", 108, 2640 },
464         { "LP121", 121, 2640 },
465
466         /* Somewhat common HP steel cylinders */
467         { "HP65",   65, 3442 },
468         { "HP80",   80, 3442 },
469         { "HP100", 100, 3442 },
470         { "HP119", 119, 3442 },
471         { "HP130", 130, 3442 },
472
473         /* We'll fill in more from the dive log dynamically */
474         { NULL, }
475 };
476
477 static void fill_tank_list(GtkListStore *store)
478 {
479         GtkTreeIter iter;
480         struct tank_info *info = tank_info;
481
482         while (info->name) {
483                 int size = info->size;
484                 int psi = info->psi;
485                 int mbar = 0, ml = size;
486
487                 /* Is it in cuft and psi? */
488                 if (psi) {
489                         double bar = psi_to_bar(psi);
490                         double airvolume = cuft_to_l(size) * 1000.0;
491                         double atm = bar_to_atm(bar);
492
493                         ml = airvolume / atm + 0.5;
494                         mbar = bar*1000 + 0.5;
495                 }
496
497                 gtk_list_store_append(store, &iter);
498                 gtk_list_store_set(store, &iter,
499                         0, info->name,
500                         1, ml,
501                         2, mbar,
502                         -1);
503                 info++;
504         }
505 }
506
507 static void nitrox_cb(GtkToggleButton *button, gpointer data)
508 {
509         struct cylinder_widget *cylinder = data;
510         int state;
511
512         state = gtk_toggle_button_get_active(button);
513         gtk_widget_set_sensitive(cylinder->o2, state);
514 }
515
516 static void pressure_cb(GtkToggleButton *button, gpointer data)
517 {
518         struct cylinder_widget *cylinder = data;
519         int state;
520
521         state = gtk_toggle_button_get_active(button);
522         gtk_widget_set_sensitive(cylinder->start, state);
523         gtk_widget_set_sensitive(cylinder->end, state);
524 }
525
526 static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder)
527 {
528         const char *desc;
529         unsigned int ml, mbar;
530
531         gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
532         add_cylinder(cylinder, desc, ml, mbar);
533         return TRUE;
534 }
535
536 static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data)
537 {
538         struct cylinder_widget *cylinder = data;
539         cylinder_cb(cylinder->description, data);
540 }
541
542 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
543 {
544         GtkWidget *frame, *hbox;
545         GtkEntry *entry;
546         GtkEntryCompletion *completion;
547         GtkWidget *widget;
548
549         /*
550          * Cylinder type: description, size and
551          * working pressure
552          */
553         frame = gtk_frame_new("Cylinder");
554
555         hbox = gtk_hbox_new(FALSE, 3);
556         gtk_container_add(GTK_CONTAINER(frame), hbox);
557
558         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
559         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
560
561         cylinder->description = GTK_COMBO_BOX(widget);
562         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
563
564         entry = GTK_ENTRY(GTK_BIN(widget)->child);
565         g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder);
566
567         completion = gtk_entry_completion_new();
568         gtk_entry_completion_set_text_column(completion, 0);
569         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
570         g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder);
571         gtk_entry_set_completion(entry, completion);
572
573         hbox = gtk_hbox_new(FALSE, 3);
574         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
575         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
576
577         widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
578         cylinder->size = GTK_SPIN_BUTTON(widget);
579
580         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
581         cylinder->pressure = GTK_SPIN_BUTTON(widget);
582
583         /*
584          * Cylinder start/end pressures
585          */
586         hbox = gtk_hbox_new(FALSE, 3);
587         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
588
589         cylinder->pressure_button = gtk_check_button_new();
590         gtk_box_pack_start(GTK_BOX(hbox), cylinder->pressure_button, FALSE, FALSE, 3);
591         g_signal_connect(cylinder->pressure_button, "toggled", G_CALLBACK(pressure_cb), cylinder);
592
593         widget = create_spinbutton(hbox, "Start Pressure", 0, 5000, 1);
594         cylinder->start = widget;
595
596         widget = create_spinbutton(hbox, "End Pressure", 0, 5000, 1);
597         cylinder->end = widget;
598
599         /*
600          * Cylinder gas mix: Air, Nitrox or Trimix
601          */
602         hbox = gtk_hbox_new(FALSE, 3);
603         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
604
605         widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
606         cylinder->o2 = widget;
607         cylinder->gasmix_button = gtk_check_button_new();
608         gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
609                 cylinder->gasmix_button, FALSE, FALSE, 3);
610         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
611
612         gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
613 }
614
615 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
616 {
617         int success;
618         GtkWidget *dialog, *vbox;
619         struct cylinder_widget cylinder;
620         struct dive *dive;
621
622         cylinder.index = index;
623         cylinder.changed = 0;
624
625         dive = current_dive;
626         if (!dive)
627                 return 0;
628         *cyl = dive->cylinder[index];
629
630         dialog = gtk_dialog_new_with_buttons("Cylinder",
631                 GTK_WINDOW(main_window),
632                 GTK_DIALOG_DESTROY_WITH_PARENT,
633                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
634                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
635                 NULL);
636
637         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
638         cylinder_widget(vbox, &cylinder, cylinder_model);
639
640         show_cylinder(cyl, &cylinder);
641
642         gtk_widget_show_all(dialog);
643         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
644         if (success) {
645                 record_cylinder_changes(cyl, &cylinder);
646                 dive->cylinder[index] = *cyl;
647                 mark_divelist_changed(TRUE);
648                 update_cylinder_related_info(dive);
649                 flush_divelist(dive);
650         }
651
652         gtk_widget_destroy(dialog);
653
654         return success;
655 }
656
657 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
658 {
659         int *p, index;
660         GtkTreePath *path;
661
662         path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
663         p = gtk_tree_path_get_indices(path);
664         index = p ? *p : 0;
665         gtk_tree_path_free(path);
666         return index;
667 }
668
669 static void edit_cb(GtkButton *button, gpointer data)
670 {
671         int index;
672         GtkTreeIter iter;
673         GtkListStore *model = cylinder_list.model;
674         GtkTreeSelection *selection;
675         cylinder_t cyl;
676
677         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
678
679         /* Nothing selected? This shouldn't happen, since the button should be inactive */
680         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
681                 return;
682
683         index = get_model_index(model, &iter);
684         if (!edit_cylinder_dialog(index, &cyl))
685                 return;
686
687         set_one_cylinder(index, &cyl, model, &iter);
688         repaint_dive();
689 }
690
691 static void add_cb(GtkButton *button, gpointer data)
692 {
693         int index = cylinder_list.max_index;
694         GtkTreeIter iter;
695         GtkListStore *model = cylinder_list.model;
696         GtkTreeSelection *selection;
697         cylinder_t cyl;
698
699         if (!edit_cylinder_dialog(index, &cyl))
700                 return;
701
702         gtk_list_store_append(model, &iter);
703         set_one_cylinder(index, &cyl, model, &iter);
704
705         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
706         gtk_tree_selection_select_iter(selection, &iter);
707
708         cylinder_list.max_index++;
709         gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
710 }
711
712 static void del_cb(GtkButton *button, gpointer data)
713 {
714         int index, nr;
715         GtkTreeIter iter;
716         GtkListStore *model = cylinder_list.model;
717         GtkTreeSelection *selection;
718         struct dive *dive;
719         cylinder_t *cyl;
720
721         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
722
723         /* Nothing selected? This shouldn't happen, since the button should be inactive */
724         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
725                 return;
726
727         index = get_model_index(model, &iter);
728
729         dive = current_dive;
730         if (!dive)
731                 return;
732         cyl = dive->cylinder + index;
733         nr = cylinder_list.max_index - index - 1;
734
735         gtk_list_store_remove(model, &iter);
736
737         cylinder_list.max_index--;
738         memmove(cyl, cyl+1, nr*sizeof(*cyl));
739         memset(cyl+nr, 0, sizeof(*cyl));
740
741         mark_divelist_changed(TRUE);
742         flush_divelist(dive);
743
744         gtk_widget_set_sensitive(cylinder_list.edit, 0);
745         gtk_widget_set_sensitive(cylinder_list.del, 0);
746         gtk_widget_set_sensitive(cylinder_list.add, 1);
747 }
748
749 static GtkListStore *create_tank_size_model(void)
750 {
751         GtkListStore *model;
752
753         model = gtk_list_store_new(3,
754                 G_TYPE_STRING,          /* Tank name */
755                 G_TYPE_INT,             /* Tank size in mliter */
756                 G_TYPE_INT,             /* Tank working pressure in mbar */
757                 -1);
758
759         fill_tank_list(model);
760         return model;
761 }
762
763 static void size_data_func(GtkTreeViewColumn *col,
764                            GtkCellRenderer *renderer,
765                            GtkTreeModel *model,
766                            GtkTreeIter *iter,
767                            gpointer data)
768 {
769         int ml, mbar;
770         double size, pressure;
771         char buffer[10];
772
773         gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
774         convert_volume_pressure(ml, mbar, &size, &pressure);
775         if (size)
776                 snprintf(buffer, sizeof(buffer), "%.1f", size);
777         else
778                 strcpy(buffer, "unkn");
779         g_object_set(renderer, "text", buffer, NULL);
780 }
781
782 static void pressure_data_func(GtkTreeViewColumn *col,
783                            GtkCellRenderer *renderer,
784                            GtkTreeModel *model,
785                            GtkTreeIter *iter,
786                            gpointer data)
787 {
788         int index = (long)data;
789         int mbar, decimals;
790         double pressure;
791         char buffer[10];
792
793         gtk_tree_model_get(model, iter, index, &mbar, -1);
794         decimals = convert_pressure(mbar, &pressure);
795         if (mbar)
796                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
797         else
798                 *buffer = 0;
799         g_object_set(renderer, "text", buffer, NULL);
800 }
801
802 static void percentage_data_func(GtkTreeViewColumn *col,
803                            GtkCellRenderer *renderer,
804                            GtkTreeModel *model,
805                            GtkTreeIter *iter,
806                            gpointer data)
807 {
808         int index = (long)data;
809         int permille;
810         char buffer[10];
811
812         gtk_tree_model_get(model, iter, index, &permille, -1);
813         if (permille)
814                 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
815         else
816                 *buffer = 0;
817         g_object_set(renderer, "text", buffer, NULL);
818 }
819
820 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
821 {
822         GtkTreeIter iter;
823         int selected;
824
825         selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
826         gtk_widget_set_sensitive(cylinder_list.edit, selected);
827         gtk_widget_set_sensitive(cylinder_list.del, selected);
828 }
829
830 static void row_activated_cb(GtkTreeView *tree_view,
831                         GtkTreePath *path,
832                         GtkTreeViewColumn *column,
833                         GtkTreeModel *model)
834 {
835         edit_cb(NULL, NULL);
836 }
837
838 static GtkWidget *cylinder_list_create(void)
839 {
840         GtkWidget *tree_view;
841         GtkTreeSelection *selection;
842         GtkListStore *model;
843
844         model = gtk_list_store_new(CYL_COLUMNS,
845                 G_TYPE_STRING,          /* CYL_DESC: utf8 */
846                 G_TYPE_INT,             /* CYL_SIZE: mliter */
847                 G_TYPE_INT,             /* CYL_WORKP: mbar */
848                 G_TYPE_INT,             /* CYL_STARTP: mbar */
849                 G_TYPE_INT,             /* CYL_ENDP: mbar */
850                 G_TYPE_INT,             /* CYL_O2: permille */
851                 G_TYPE_INT              /* CYL_HE: permille */
852                 );
853         cylinder_list.model = model;
854         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
855         g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
856
857         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
858         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
859         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
860
861         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
862                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
863                                           NULL);
864
865         cylinder_list.desc = tree_view_column(tree_view, CYL_DESC, "Type", NULL, PANGO_ALIGN_LEFT, TRUE);
866         cylinder_list.size = tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, PANGO_ALIGN_RIGHT, TRUE);
867         cylinder_list.workp = tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
868         cylinder_list.startp = tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
869         cylinder_list.endp = tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
870         cylinder_list.o2 = tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
871         cylinder_list.he = tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
872         return tree_view;
873 }
874
875 GtkWidget *equipment_widget(void)
876 {
877         GtkWidget *vbox, *hbox, *frame, *framebox;
878         GtkWidget *add, *del, *edit;
879
880         vbox = gtk_vbox_new(FALSE, 3);
881
882         /*
883          * We create the cylinder size model at startup, since
884          * we're going to share it across all cylinders and all
885          * dives. So if you add a new cylinder type in one dive,
886          * it will show up when you edit the cylinder types for
887          * another dive.
888          */
889         cylinder_model = create_tank_size_model();
890
891         cylinder_list.tree_view = cylinder_list_create();
892
893         hbox = gtk_hbox_new(FALSE, 3);
894         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
895
896         frame = gtk_frame_new("Cylinders");
897         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
898
899         framebox = gtk_vbox_new(FALSE, 3);
900         gtk_container_add(GTK_CONTAINER(frame), framebox);
901
902         hbox = gtk_hbox_new(FALSE, 3);
903         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
904
905         gtk_box_pack_start(GTK_BOX(hbox), cylinder_list.tree_view, TRUE, FALSE, 3);
906
907         hbox = gtk_hbox_new(TRUE, 3);
908         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
909
910         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
911         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
912         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
913         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
914         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
915         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
916
917         cylinder_list.edit = edit;
918         cylinder_list.add = add;
919         cylinder_list.del = del;
920
921         g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), NULL);
922         g_signal_connect(add, "clicked", G_CALLBACK(add_cb), NULL);
923         g_signal_connect(del, "clicked", G_CALLBACK(del_cb), NULL);
924
925         return vbox;
926 }