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