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