]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Add weight system tracking
[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  *
7  * called from gtk-ui:
8  * GtkWidget *equipment_widget(void)
9  */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include <time.h>
15
16 #include "dive.h"
17 #include "display.h"
18 #include "display-gtk.h"
19 #include "divelist.h"
20
21 static GtkListStore *cylinder_model, *weightsystem_model;
22
23 enum {
24         CYL_DESC,
25         CYL_SIZE,
26         CYL_WORKP,
27         CYL_STARTP,
28         CYL_ENDP,
29         CYL_O2,
30         CYL_HE,
31         CYL_COLUMNS
32 };
33
34 enum {
35         WS_DESC,
36         WS_WEIGHT,
37         WS_COLUMNS
38 };
39
40 struct equipment_list {
41         int max_index;
42         GtkListStore *model;
43         GtkWidget *edit, *add, *del;
44 };
45
46 static struct equipment_list cylinder_list, weightsystem_list;
47
48
49 struct cylinder_widget {
50         int index, changed;
51         const char *name;
52         GtkWidget *hbox;
53         GtkComboBox *description;
54         GtkSpinButton *size, *pressure;
55         GtkWidget *start, *end, *pressure_button;
56         GtkWidget *o2, *he, *gasmix_button;
57 };
58
59 struct ws_widget {
60         int index, changed;
61         const char *name;
62         GtkWidget *hbox;
63         GtkComboBox *description;
64         GtkSpinButton *weight;
65 };
66
67 /* we want bar - so let's not use our unit functions */
68 static int convert_pressure(int mbar, double *p)
69 {
70         int decimals = 1;
71         double pressure;
72
73         if (output_units.pressure == PSI) {
74                 pressure = mbar_to_PSI(mbar);
75                 decimals = 0;
76         } else {
77                 pressure = mbar / 1000.0;
78         }
79
80         *p = pressure;
81         return decimals;
82 }
83
84 static int convert_volume_pressure(int ml, int mbar, double *v, double *p)
85 {
86         int decimals = 1;
87         double volume, pressure;
88
89         volume = ml / 1000.0;
90         if (mbar) {
91                 if (output_units.volume == CUFT) {
92                         volume = ml_to_cuft(ml);
93                         volume *= bar_to_atm(mbar / 1000.0);
94                 }
95
96                 if (output_units.pressure == PSI) {
97                         pressure = mbar_to_PSI(mbar);
98                         decimals = 0;
99                 } else
100                         pressure = mbar / 1000.0;
101         }
102         *v = volume;
103         *p = pressure;
104         return decimals;
105 }
106
107 static int convert_weight(int grams, double *m)
108 {
109         int decimals = 1; /* not sure - do people do less than whole lbs/kg ? */
110         double weight;
111
112         if (output_units.weight == LBS)
113                 weight = grams_to_lbs(grams);
114         else
115                 weight = grams / 1000.0;
116         *m = weight;
117         return decimals;
118 }
119
120 static void set_cylinder_type_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
121 {
122         double volume, pressure;
123
124         convert_volume_pressure(ml, mbar, &volume, &pressure);
125         gtk_spin_button_set_value(cylinder->size, volume);
126         gtk_spin_button_set_value(cylinder->pressure, pressure);
127 }
128
129 static void set_cylinder_pressure_spinbuttons(struct cylinder_widget *cylinder, cylinder_t *cyl)
130 {
131         int set;
132         unsigned int start, end;
133         double pressure;
134
135         start = cyl->start.mbar;
136         end = cyl->end.mbar;
137         set = start || end;
138         if (!set) {
139                 start = cyl->sample_start.mbar;
140                 end = cyl->sample_end.mbar;
141         }
142         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button), set);
143         gtk_widget_set_sensitive(cylinder->start, set);
144         gtk_widget_set_sensitive(cylinder->end, set);
145
146         convert_pressure(start, &pressure);
147         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->start), pressure);
148         convert_pressure(end, &pressure);
149         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->end), pressure);
150 }
151
152 static void set_weight_weight_spinbutton(struct ws_widget *ws_widget, int grams)
153 {
154         double weight;
155
156         convert_weight(grams, &weight);
157         gtk_spin_button_set_value(ws_widget->weight, weight);
158 }
159
160 /*
161  * The gtk_tree_model_foreach() interface is bad. It could have
162  * returned whether the callback ever returned true
163  */
164 static GtkTreeIter *found_match = NULL;
165 static GtkTreeIter match_iter;
166
167 static gboolean match_desc(GtkTreeModel *model, GtkTreePath *path,
168                         GtkTreeIter *iter, gpointer data)
169 {
170         int match;
171         gchar *name;
172         const char *desc = data;
173
174         gtk_tree_model_get(model, iter, 0, &name, -1);
175         match = !strcmp(desc, name);
176         g_free(name);
177         if (match) {
178                 match_iter = *iter;
179                 found_match = &match_iter;
180         }
181         return match;
182 }
183
184 static int get_active_item(GtkComboBox *combo_box, GtkTreeIter *iter, GtkListStore *model)
185 {
186         char *desc;
187
188         if (gtk_combo_box_get_active_iter(combo_box, iter))
189                 return TRUE;
190
191         desc = gtk_combo_box_get_active_text(combo_box);
192
193         found_match = NULL;
194         gtk_tree_model_foreach(GTK_TREE_MODEL(model), match_desc, (void *)desc);
195
196         g_free(desc);
197         if (!found_match)
198                 return FALSE;
199
200         *iter = *found_match;
201         gtk_combo_box_set_active_iter(combo_box, iter);
202         return TRUE;
203 }
204
205 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
206 {
207         GtkTreeIter iter;
208         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
209         int ml, mbar;
210         struct cylinder_widget *cylinder = data;
211         cylinder_t *cyl = current_dive->cylinder + cylinder->index;
212
213         /* Did the user set it to some non-standard value? */
214         if (!get_active_item(combo_box, &iter, cylinder_model)) {
215                 cylinder->changed = 1;
216                 return;
217         }
218
219         /*
220          * We get "change" signal callbacks just because we set
221          * the description by hand. Whatever. So ignore them if
222          * they are no-ops.
223          */
224         if (!cylinder->changed && cyl->type.description) {
225                 int same;
226                 char *desc = gtk_combo_box_get_active_text(combo_box);
227
228                 same = !strcmp(desc, cyl->type.description);
229                 g_free(desc);
230                 if (same)
231                         return;
232         }
233         cylinder->changed = 1;
234
235         gtk_tree_model_get(model, &iter,
236                 CYL_SIZE, &ml,
237                 CYL_WORKP, &mbar,
238                 -1);
239
240         set_cylinder_type_spinbuttons(cylinder, ml, mbar);
241 }
242
243 static void weight_cb(GtkComboBox *combo_box, gpointer data)
244 {
245         GtkTreeIter iter;
246         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
247         int weight;
248         struct ws_widget *ws_widget = data;
249         weightsystem_t *ws = current_dive->weightsystem + ws_widget->index;
250
251         /* Did the user set it to some non-standard value? */
252         if (!get_active_item(combo_box, &iter, weightsystem_model)) {
253                 ws_widget->changed = 1;
254                 return;
255         }
256
257         /*
258          * We get "change" signal callbacks just because we set
259          * the description by hand. Whatever. So ignore them if
260          * they are no-ops.
261          */
262         if (!ws_widget->changed && ws->description) {
263                 int same;
264                 char *desc = gtk_combo_box_get_active_text(combo_box);
265
266                 same = !strcmp(desc, ws->description);
267                 g_free(desc);
268                 if (same)
269                         return;
270         }
271         ws_widget->changed = 1;
272
273         gtk_tree_model_get(model, &iter,
274                 WS_WEIGHT, &weight,
275                 -1);
276
277         set_weight_weight_spinbutton(ws_widget, weight);
278 }
279
280 static GtkTreeIter *add_cylinder_type(const char *desc, int ml, int mbar, GtkTreeIter *iter)
281 {
282         GtkTreeModel *model;
283
284         /* Don't even bother adding stuff without a size */
285         if (!ml)
286                 return NULL;
287
288         found_match = NULL;
289         model = GTK_TREE_MODEL(cylinder_model);
290         gtk_tree_model_foreach(model, match_desc, (void *)desc);
291
292         if (!found_match) {
293                 GtkListStore *store = GTK_LIST_STORE(model);
294
295                 gtk_list_store_append(store, iter);
296                 gtk_list_store_set(store, iter,
297                         0, desc,
298                         1, ml,
299                         2, mbar,
300                         -1);
301                 return iter;
302         }
303         return found_match;
304 }
305
306 static GtkTreeIter *add_weightsystem_type(const char *desc, int weight, GtkTreeIter *iter)
307 {
308         GtkTreeModel *model;
309
310         found_match = NULL;
311         model = GTK_TREE_MODEL(weightsystem_model);
312         gtk_tree_model_foreach(model, match_desc, (void *)desc);
313
314         if (!found_match) {
315                 GtkListStore *store = GTK_LIST_STORE(model);
316
317                 gtk_list_store_append(store, iter);
318                 gtk_list_store_set(store, iter,
319                         0, desc,
320                         1, weight,
321                         -1);
322                 return iter;
323         }
324         return found_match;
325 }
326
327 /*
328  * When adding a dive, we'll add all the pre-existing cylinder
329  * information from that dive to our cylinder model.
330  */
331 void add_cylinder_description(cylinder_type_t *type)
332 {
333         GtkTreeIter iter;
334         const char *desc;
335         unsigned int size, workp;
336
337         desc = type->description;
338         if (!desc)
339                 return;
340         size = type->size.mliter;
341         workp = type->workingpressure.mbar;
342         add_cylinder_type(desc, size, workp, &iter);
343 }
344
345 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
346 {
347         GtkTreeIter iter, *match;
348
349         cylinder->name = desc;
350         match = add_cylinder_type(desc, ml, mbar, &iter);
351         if (match)
352                 gtk_combo_box_set_active_iter(cylinder->description, match);
353 }
354
355 void add_weightsystem_description(weightsystem_t *weightsystem)
356 {
357         GtkTreeIter iter;
358         const char *desc;
359         unsigned int weight;
360
361         desc = weightsystem->description;
362         if (!desc)
363                 return;
364         weight = weightsystem->weight.grams;
365         add_weightsystem_type(desc, weight, &iter);
366 }
367
368 static void add_weightsystem(struct ws_widget *ws_widget, const char *desc, int weight)
369 {
370         GtkTreeIter iter, *match;
371
372         ws_widget->name = desc;
373         match = add_weightsystem_type(desc, weight, &iter);
374         if (match)
375                 gtk_combo_box_set_active_iter(ws_widget->description, match);
376 }
377
378 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
379 {
380         const char *desc;
381         int ml, mbar;
382         int gasmix;
383         double o2, he;
384
385         /* Don't show uninitialized cylinder widgets */
386         if (!cylinder->description)
387                 return;
388
389         desc = cyl->type.description;
390         if (!desc)
391                 desc = "";
392         ml = cyl->type.size.mliter;
393         mbar = cyl->type.workingpressure.mbar;
394         add_cylinder(cylinder, desc, ml, mbar);
395
396         set_cylinder_type_spinbuttons(cylinder,
397                 cyl->type.size.mliter, cyl->type.workingpressure.mbar);
398         set_cylinder_pressure_spinbuttons(cylinder, cyl);
399
400         gasmix = cyl->gasmix.o2.permille || cyl->gasmix.he.permille;
401         gtk_widget_set_sensitive(cylinder->o2, gasmix);
402         gtk_widget_set_sensitive(cylinder->he, gasmix);
403         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), gasmix);
404
405         o2 = cyl->gasmix.o2.permille / 10.0;
406         he = cyl->gasmix.he.permille / 10.0;
407         if (!o2)
408                 o2 = 21.0;
409         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
410         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->he), he);
411 }
412
413 static void show_weightsystem(weightsystem_t *ws, struct ws_widget *weightsystem_widget)
414 {
415         const char *desc;
416         int grams;
417
418         /* Don't show uninitialized widgets */
419         if (!weightsystem_widget->description)
420                 return;
421
422         desc = ws->description;
423         if (!desc)
424                 desc = "";
425         grams = ws->weight.grams;
426         add_weightsystem(weightsystem_widget, desc, grams);
427
428         set_weight_weight_spinbutton(weightsystem_widget, ws->weight.grams);
429 }
430
431 int cylinder_none(void *_data)
432 {
433         cylinder_t *cyl = _data;
434         return  !cyl->type.size.mliter &&
435                 !cyl->type.workingpressure.mbar &&
436                 !cyl->type.description &&
437                 !cyl->gasmix.o2.permille &&
438                 !cyl->gasmix.he.permille &&
439                 !cyl->sample_start.mbar &&
440                 !cyl->sample_end.mbar &&
441                 !cyl->start.mbar &&
442                 !cyl->end.mbar;
443 }
444
445 int weightsystem_none(void *_data)
446 {
447         weightsystem_t *ws = _data;
448         return !ws->weight.grams && !ws->description;
449 }
450
451 static void set_one_cylinder(int index, void *_data, GtkListStore *model, GtkTreeIter *iter)
452 {
453         cylinder_t *cyl = _data;
454         unsigned int start, end;
455
456         start = cyl->start.mbar ? : cyl->sample_start.mbar;
457         end = cyl->end.mbar ? : cyl->sample_end.mbar;
458         gtk_list_store_set(model, iter,
459                 CYL_DESC, cyl->type.description ? : "",
460                 CYL_SIZE, cyl->type.size.mliter,
461                 CYL_WORKP, cyl->type.workingpressure.mbar,
462                 CYL_STARTP, start,
463                 CYL_ENDP, end,
464                 CYL_O2, cyl->gasmix.o2.permille,
465                 CYL_HE, cyl->gasmix.he.permille,
466                 -1);
467 }
468
469 static void set_one_weightsystem(int index, void *_data, GtkListStore *model, GtkTreeIter *iter)
470 {
471         weightsystem_t *ws = _data;
472
473         gtk_list_store_set(model, iter,
474                 WS_DESC, ws->description ? : "unspecified",
475                 WS_WEIGHT, ws->weight.grams,
476                 -1);
477 }
478
479 static void *cyl_ptr(struct dive *dive, int idx)
480 {
481         if (idx < 0 || idx >= MAX_CYLINDERS)
482                 return NULL;
483         return &dive->cylinder[idx];
484 }
485
486 static void *ws_ptr(struct dive *dive, int idx)
487 {
488         if (idx < 0 || idx >= MAX_WEIGHTSYSTEMS)
489                 return NULL;
490         return &dive->weightsystem[idx];
491 }
492
493 static void show_equipment(struct dive *dive, int max,
494                         struct equipment_list *equipment_list,
495                         void*(*ptr_function)(struct dive*, int),
496                         int(*none_function)(void *),
497                         void(*set_one_function)(int, void*, GtkListStore*, GtkTreeIter *))
498 {
499         int i, used;
500         void *data;
501         GtkTreeIter iter;
502         GtkListStore *model = equipment_list->model;
503
504         gtk_list_store_clear(model);
505         used = max;
506         do {
507                 data = ptr_function(dive, used-1);
508                 if (!none_function(data))
509                         break;
510         } while (--used);
511
512         equipment_list->max_index = used;
513
514         gtk_widget_set_sensitive(equipment_list->edit, 0);
515         gtk_widget_set_sensitive(equipment_list->del, 0);
516         gtk_widget_set_sensitive(equipment_list->add, used < max);
517
518         for (i = 0; i < used; i++) {
519                 data = ptr_function(dive, i);
520                 gtk_list_store_append(model, &iter);
521                 set_one_function(i, data, model, &iter);
522         }
523 }
524
525 void show_dive_equipment(struct dive *dive)
526 {
527         show_equipment(dive, MAX_CYLINDERS, &cylinder_list,
528                 &cyl_ptr, &cylinder_none, &set_one_cylinder);
529         show_equipment(dive, MAX_WEIGHTSYSTEMS, &weightsystem_list,
530                 &ws_ptr, &weightsystem_none, &set_one_weightsystem);
531 }
532
533 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
534 {
535         GtkWidget *frame, *hbox, *button;
536
537         frame = gtk_frame_new(name);
538         gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
539
540         hbox = gtk_hbox_new(FALSE, 3);
541         gtk_container_add(GTK_CONTAINER(frame), hbox);
542
543         button = gtk_spin_button_new_with_range(min, max, incr);
544         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
545
546         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
547
548         return button;
549 }
550
551 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
552                 double volume, double pressure, double start, double end, int o2, int he)
553 {
554         int mbar, ml;
555
556         if (output_units.pressure == PSI) {
557                 pressure = psi_to_bar(pressure);
558                 start = psi_to_bar(start);
559                 end = psi_to_bar(end);
560         }
561
562         if (pressure && output_units.volume == CUFT) {
563                 volume = cuft_to_l(volume);
564                 volume /= bar_to_atm(pressure);
565         }
566
567         ml = volume * 1000 + 0.5;
568         mbar = pressure * 1000 + 0.5;
569
570         /* Ignore obviously crazy He values */
571         if (o2 + he > 1000)
572                 he = 0;
573
574         /* We have a rule that normal air is all zeroes */
575         if (!he && o2 > 208 && o2 < 211)
576                 o2 = 0;
577
578         cyl->type.description = desc;
579         cyl->type.size.mliter = ml;
580         cyl->type.workingpressure.mbar = mbar;
581         cyl->start.mbar = start * 1000 + 0.5;
582         cyl->end.mbar = end * 1000 + 0.5;
583         cyl->gasmix.o2.permille = o2;
584         cyl->gasmix.he.permille = he;
585
586         /*
587          * Also, insert it into the model if it doesn't already exist
588          */
589         add_cylinder(cylinder, desc, ml, mbar);
590 }
591
592 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
593 {
594         const gchar *desc;
595         GtkComboBox *box;
596         double volume, pressure, start, end;
597         int o2, he;
598
599         /* Ignore uninitialized cylinder widgets */
600         box = cylinder->description;
601         if (!box)
602                 return;
603
604         desc = gtk_combo_box_get_active_text(box);
605         volume = gtk_spin_button_get_value(cylinder->size);
606         pressure = gtk_spin_button_get_value(cylinder->pressure);
607         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button))) {
608                 start = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->start));
609                 end = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->end));
610         } else {
611                 start = end = 0;
612         }
613         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button))) {
614                 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
615                 he = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->he))*10 + 0.5;
616         } else {
617                 o2 = 0;
618                 he = 0;
619         }
620         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2, he);
621 }
622
623 static void record_weightsystem_changes(weightsystem_t *ws, struct ws_widget *weightsystem_widget)
624 {
625         const gchar *desc;
626         GtkComboBox *box;
627         int grams;
628         double value;
629
630         /* Ignore uninitialized cylinder widgets */
631         box = weightsystem_widget->description;
632         if (!box)
633                 return;
634
635         desc = gtk_combo_box_get_active_text(box);
636         value = gtk_spin_button_get_value(weightsystem_widget->weight);
637
638         if (output_units.weight == LBS)
639                 grams = lbs_to_grams(value);
640         else
641                 grams = value * 1000;
642         ws->weight.grams = grams;
643         ws->description = desc;
644 }
645
646 /*
647  * We hardcode the most common standard cylinders,
648  * we should pick up any other names from the dive
649  * logs directly.
650  */
651 static struct tank_info {
652         const char *name;
653         int cuft, ml, psi, bar;
654 } tank_info[100] = {
655         /* Need an empty entry for the no-cylinder case */
656         { "", },
657
658         /* Size-only metric cylinders */
659         { "10.0 l", .ml = 10000 },
660         { "11.1 l", .ml = 11100 },
661
662         /* Most common AL cylinders */
663         { "AL50",  .cuft =  50, .psi = 3000 },
664         { "AL63",  .cuft =  63, .psi = 3000 },
665         { "AL72",  .cuft =  72, .psi = 3000 },
666         { "AL80",  .cuft =  80, .psi = 3000 },
667         { "AL100", .cuft = 100, .psi = 3300 },
668
669         /* Somewhat common LP steel cylinders */
670         { "LP85",  .cuft =  85, 2640 },
671         { "LP95",  .cuft =  95, 2640 },
672         { "LP108", .cuft = 108, 2640 },
673         { "LP121", .cuft = 121, 2640 },
674
675         /* Somewhat common HP steel cylinders */
676         { "HP65",  .cuft =  65, .psi = 3442 },
677         { "HP80",  .cuft =  80, .psi = 3442 },
678         { "HP100", .cuft = 100, .psi = 3442 },
679         { "HP119", .cuft = 119, .psi = 3442 },
680         { "HP130", .cuft = 130, .psi = 3442 },
681
682         /* Common European steel cylinders */
683         { "10L 300 bar",  .ml = 10000, .bar = 300 },
684         { "12L 200 bar",  .ml = 12000, .bar = 200 },
685         { "12L 232 bar",  .ml = 12000, .bar = 232 },
686         { "12L 300 bar",  .ml = 12000, .bar = 300 },
687         { "15L 200 bar",  .ml = 15000, .bar = 200 },
688         { "15L 232 bar",  .ml = 15000, .bar = 232 },
689         { "D7 300 bar",   .ml = 14000, .bar = 300 },
690         { "D8.5 232 bar", .ml = 17000, .bar = 232 },
691         { "D12 232 bar",  .ml = 24000, .bar = 232 },
692
693         /* We'll fill in more from the dive log dynamically */
694         { NULL, }
695 };
696
697 static void fill_tank_list(GtkListStore *store)
698 {
699         GtkTreeIter iter;
700         struct tank_info *info = tank_info;
701
702         while (info->name) {
703                 int ml = info->ml;
704                 int cuft = info->cuft;
705                 int psi = info->psi;
706                 int mbar;
707                 double bar = info->bar;
708
709                 /* Is it in cuft and psi? */
710                 if (psi) {
711                         bar = psi_to_bar(psi);
712
713                         if (cuft) {
714                                 double airvolume = cuft_to_l(cuft) * 1000.0;
715                                 double atm = bar_to_atm(bar);
716
717                                 ml = airvolume / atm + 0.5;
718                         }
719                 }
720
721                 mbar = bar * 1000 + 0.5;
722
723                 gtk_list_store_append(store, &iter);
724                 gtk_list_store_set(store, &iter,
725                         0, info->name,
726                         1, ml,
727                         2, mbar,
728                         -1);
729                 info++;
730         }
731 }
732
733 /*
734  * We hardcode the most common weight system types
735  * This is a bit odd as the weight system types don't usually encode weight
736  */
737 static struct ws_info {
738         const char *name;
739         int grams;
740 } ws_info[100] = {
741         /* Need an empty entry for the no weight system case */
742         { "", },
743         { "integrated", 0 },
744         { "belt", 0 },
745         { "ankle", 0 },
746         { "bar", 0 },
747         { "clip-on", 0 },
748 };
749
750 static void fill_ws_list(GtkListStore *store)
751 {
752         GtkTreeIter iter;
753         struct ws_info *info = ws_info;
754
755         while (info->name) {
756                 gtk_list_store_append(store, &iter);
757                 gtk_list_store_set(store, &iter,
758                         0, info->name,
759                         1, info->grams,
760                         -1);
761                 info++;
762         }
763 }
764
765 static void gasmix_cb(GtkToggleButton *button, gpointer data)
766 {
767         struct cylinder_widget *cylinder = data;
768         int state;
769
770         state = gtk_toggle_button_get_active(button);
771         gtk_widget_set_sensitive(cylinder->o2, state);
772         gtk_widget_set_sensitive(cylinder->he, state);
773 }
774
775 static void pressure_cb(GtkToggleButton *button, gpointer data)
776 {
777         struct cylinder_widget *cylinder = data;
778         int state;
779
780         state = gtk_toggle_button_get_active(button);
781         gtk_widget_set_sensitive(cylinder->start, state);
782         gtk_widget_set_sensitive(cylinder->end, state);
783 }
784
785 static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder)
786 {
787         const char *desc;
788         unsigned int ml, mbar;
789
790         gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
791         add_cylinder(cylinder, desc, ml, mbar);
792         return TRUE;
793 }
794
795 static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data)
796 {
797         struct cylinder_widget *cylinder = data;
798         cylinder_cb(cylinder->description, data);
799 }
800
801 /* Return a frame containing a hbox inside a hbox */
802 static GtkWidget *frame_box(const char *title, GtkWidget *vbox)
803 {
804         GtkWidget *hbox, *frame;
805
806         hbox = gtk_hbox_new(FALSE, 10);
807         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
808
809         frame = gtk_frame_new(title);
810         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 0);
811
812         hbox = gtk_hbox_new(FALSE, 10);
813         gtk_container_add(GTK_CONTAINER(frame), hbox);
814
815         return hbox;
816 }
817
818 static GtkWidget *labeled_spinbutton(GtkWidget *box, const char *name, double min, double max, double incr)
819 {
820         GtkWidget *hbox, *label, *button;
821
822         hbox = gtk_hbox_new(FALSE, 0);
823         gtk_box_pack_start(GTK_BOX(box), hbox, TRUE, FALSE, 0);
824
825         label = gtk_label_new(name);
826         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
827
828         button = gtk_spin_button_new_with_range(min, max, incr);
829         gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
830
831         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
832
833         return button;
834 }
835
836 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
837 {
838         GtkWidget *frame, *hbox;
839         GtkEntry *entry;
840         GtkEntryCompletion *completion;
841         GtkWidget *widget;
842
843         /*
844          * Cylinder type: description, size and
845          * working pressure
846          */
847         frame = gtk_frame_new("Cylinder");
848
849         hbox = gtk_hbox_new(FALSE, 3);
850         gtk_container_add(GTK_CONTAINER(frame), hbox);
851
852         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
853         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
854
855         cylinder->description = GTK_COMBO_BOX(widget);
856         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
857
858         entry = GTK_ENTRY(GTK_BIN(widget)->child);
859         g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder);
860
861         completion = gtk_entry_completion_new();
862         gtk_entry_completion_set_text_column(completion, 0);
863         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
864         g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder);
865         gtk_entry_set_completion(entry, completion);
866
867         hbox = gtk_hbox_new(FALSE, 3);
868         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
869         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
870
871         widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
872         cylinder->size = GTK_SPIN_BUTTON(widget);
873
874         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
875         cylinder->pressure = GTK_SPIN_BUTTON(widget);
876
877         /*
878          * Cylinder start/end pressures
879          */
880         hbox = frame_box("Pressure", vbox);
881
882         widget = labeled_spinbutton(hbox, "Start", 0, 5000, 1);
883         cylinder->start = widget;
884
885         widget = labeled_spinbutton(hbox, "End", 0, 5000, 1);
886         cylinder->end = widget;
887
888         cylinder->pressure_button = gtk_check_button_new();
889         gtk_box_pack_start(GTK_BOX(hbox), cylinder->pressure_button, FALSE, FALSE, 3);
890         g_signal_connect(cylinder->pressure_button, "toggled", G_CALLBACK(pressure_cb), cylinder);
891
892         /*
893          * Cylinder gas mix: Air, Nitrox or Trimix
894          */
895         hbox = frame_box("Gasmix", vbox);
896
897         widget = labeled_spinbutton(hbox, "O"UTF8_SUBSCRIPT_2 "%", 1, 100, 0.1);
898         cylinder->o2 = widget;
899         widget = labeled_spinbutton(hbox, "He%", 0, 100, 0.1);
900         cylinder->he = widget;
901         cylinder->gasmix_button = gtk_check_button_new();
902         gtk_box_pack_start(GTK_BOX(hbox), cylinder->gasmix_button, FALSE, FALSE, 3);
903         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(gasmix_cb), cylinder);
904 }
905
906 static gboolean weight_completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct ws_widget *ws_widget)
907 {
908         const char *desc;
909         unsigned int weight;
910
911         gtk_tree_model_get(model, iter, WS_DESC, &desc, WS_WEIGHT, &weight, -1);
912         add_weightsystem(ws_widget, desc, weight);
913         return TRUE;
914 }
915
916 static void weight_activate_cb(GtkComboBox *combo_box, gpointer data)
917 {
918         struct ws_widget *ws_widget = data;
919         weight_cb(ws_widget->description, data);
920 }
921
922 static void ws_widget(GtkWidget *vbox, struct ws_widget *ws_widget, GtkListStore *model)
923 {
924         GtkWidget *frame, *hbox;
925         GtkEntryCompletion *completion;
926         GtkWidget *widget;
927         GtkEntry *entry;
928
929         /*
930          * weight_system: description and weight
931          */
932         frame = gtk_frame_new("Weight");
933
934         hbox = gtk_hbox_new(FALSE, 3);
935         gtk_container_add(GTK_CONTAINER(frame), hbox);
936
937         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
938         gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
939
940         ws_widget->description = GTK_COMBO_BOX(widget);
941         g_signal_connect(widget, "changed", G_CALLBACK(weight_cb), ws_widget);
942
943         entry = GTK_ENTRY(GTK_BIN(widget)->child);
944         g_signal_connect(entry, "activate", G_CALLBACK(weight_activate_cb), ws_widget);
945
946         completion = gtk_entry_completion_new();
947         gtk_entry_completion_set_text_column(completion, 0);
948         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
949         g_signal_connect(completion, "match-selected", G_CALLBACK(weight_completion_cb), ws_widget);
950         gtk_entry_set_completion(entry, completion);
951
952         hbox = gtk_hbox_new(FALSE, 3);
953         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
954         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
955
956         if ( output_units.weight == KG)
957                 widget = create_spinbutton(hbox, "kg", 0, 50, 0.5);
958         else
959                 widget = create_spinbutton(hbox, "lbs", 0, 110, 1);
960         ws_widget->weight = GTK_SPIN_BUTTON(widget);
961 }
962
963 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
964 {
965         int success;
966         GtkWidget *dialog, *vbox;
967         struct cylinder_widget cylinder;
968         struct dive *dive;
969
970         cylinder.index = index;
971         cylinder.changed = 0;
972
973         dive = current_dive;
974         if (!dive)
975                 return 0;
976         *cyl = dive->cylinder[index];
977
978         dialog = gtk_dialog_new_with_buttons("Cylinder",
979                 GTK_WINDOW(main_window),
980                 GTK_DIALOG_DESTROY_WITH_PARENT,
981                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
982                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
983                 NULL);
984
985         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
986         cylinder_widget(vbox, &cylinder, cylinder_model);
987
988         show_cylinder(cyl, &cylinder);
989
990         gtk_widget_show_all(dialog);
991         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
992         if (success) {
993                 record_cylinder_changes(cyl, &cylinder);
994                 dive->cylinder[index] = *cyl;
995                 mark_divelist_changed(TRUE);
996                 update_cylinder_related_info(dive);
997                 flush_divelist(dive);
998         }
999
1000         gtk_widget_destroy(dialog);
1001
1002         return success;
1003 }
1004
1005 static int edit_weightsystem_dialog(int index, weightsystem_t *ws)
1006 {
1007         int success;
1008         GtkWidget *dialog, *vbox;
1009         struct ws_widget weightsystem_widget;
1010         struct dive *dive;
1011
1012         weightsystem_widget.index = index;
1013         weightsystem_widget.changed = 0;
1014
1015         dive = current_dive;
1016         if (!dive)
1017                 return 0;
1018         *ws = dive->weightsystem[index];
1019
1020         dialog = gtk_dialog_new_with_buttons("Weight System",
1021                 GTK_WINDOW(main_window),
1022                 GTK_DIALOG_DESTROY_WITH_PARENT,
1023                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1024                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1025                 NULL);
1026
1027         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1028         ws_widget(vbox, &weightsystem_widget, weightsystem_model);
1029
1030         show_weightsystem(ws, &weightsystem_widget);
1031
1032         gtk_widget_show_all(dialog);
1033         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
1034         if (success) {
1035                 record_weightsystem_changes(ws, &weightsystem_widget);
1036                 dive->weightsystem[index] = *ws;
1037                 mark_divelist_changed(TRUE);
1038                 flush_divelist(dive);
1039         }
1040
1041         gtk_widget_destroy(dialog);
1042
1043         return success;
1044 }
1045
1046 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
1047 {
1048         int *p, index;
1049         GtkTreePath *path;
1050
1051         path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
1052         p = gtk_tree_path_get_indices(path);
1053         index = p ? *p : 0;
1054         gtk_tree_path_free(path);
1055         return index;
1056 }
1057
1058 static void edit_cb(GtkButton *button, GtkTreeView *tree_view)
1059 {
1060         int index;
1061         GtkTreeIter iter;
1062         GtkListStore *model = cylinder_list.model;
1063         GtkTreeSelection *selection;
1064         cylinder_t cyl;
1065
1066         selection = gtk_tree_view_get_selection(tree_view);
1067
1068         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1069         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1070                 return;
1071
1072         index = get_model_index(model, &iter);
1073         if (!edit_cylinder_dialog(index, &cyl))
1074                 return;
1075
1076         set_one_cylinder(index, &cyl, model, &iter);
1077         repaint_dive();
1078 }
1079
1080 static void add_cb(GtkButton *button, GtkTreeView *tree_view)
1081 {
1082         int index = cylinder_list.max_index;
1083         GtkTreeIter iter;
1084         GtkListStore *model = cylinder_list.model;
1085         GtkTreeSelection *selection;
1086         cylinder_t cyl;
1087
1088         if (!edit_cylinder_dialog(index, &cyl))
1089                 return;
1090
1091         gtk_list_store_append(model, &iter);
1092         set_one_cylinder(index, &cyl, model, &iter);
1093
1094         selection = gtk_tree_view_get_selection(tree_view);
1095         gtk_tree_selection_select_iter(selection, &iter);
1096
1097         cylinder_list.max_index++;
1098         gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
1099 }
1100
1101 static void del_cb(GtkButton *button, GtkTreeView *tree_view)
1102 {
1103         int index, nr;
1104         GtkTreeIter iter;
1105         GtkListStore *model = cylinder_list.model;
1106         GtkTreeSelection *selection;
1107         struct dive *dive;
1108         cylinder_t *cyl;
1109
1110         selection = gtk_tree_view_get_selection(tree_view);
1111
1112         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1113         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1114                 return;
1115
1116         index = get_model_index(model, &iter);
1117
1118         dive = current_dive;
1119         if (!dive)
1120                 return;
1121         cyl = dive->cylinder + index;
1122         nr = cylinder_list.max_index - index - 1;
1123
1124         gtk_list_store_remove(model, &iter);
1125
1126         cylinder_list.max_index--;
1127         memmove(cyl, cyl+1, nr*sizeof(*cyl));
1128         memset(cyl+nr, 0, sizeof(*cyl));
1129
1130         mark_divelist_changed(TRUE);
1131         flush_divelist(dive);
1132
1133         gtk_widget_set_sensitive(cylinder_list.edit, 0);
1134         gtk_widget_set_sensitive(cylinder_list.del, 0);
1135         gtk_widget_set_sensitive(cylinder_list.add, 1);
1136 }
1137
1138 static void ws_edit_cb(GtkButton *button, GtkTreeView *tree_view)
1139 {
1140         int index;
1141         GtkTreeIter iter;
1142         GtkListStore *model = weightsystem_list.model;
1143         GtkTreeSelection *selection;
1144         weightsystem_t ws;
1145
1146         selection = gtk_tree_view_get_selection(tree_view);
1147
1148         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1149         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1150                 return;
1151
1152         index = get_model_index(model, &iter);
1153         if (!edit_weightsystem_dialog(index, &ws))
1154                 return;
1155
1156         set_one_weightsystem(index, &ws, model, &iter);
1157         repaint_dive();
1158 }
1159
1160 static void ws_add_cb(GtkButton *button, GtkTreeView *tree_view)
1161 {
1162         int index = weightsystem_list.max_index;
1163         GtkTreeIter iter;
1164         GtkListStore *model = weightsystem_list.model;
1165         GtkTreeSelection *selection;
1166         weightsystem_t ws;
1167
1168         if (!edit_weightsystem_dialog(index, &ws))
1169                 return;
1170
1171         gtk_list_store_append(model, &iter);
1172         set_one_weightsystem(index, &ws, model, &iter);
1173
1174         selection = gtk_tree_view_get_selection(tree_view);
1175         gtk_tree_selection_select_iter(selection, &iter);
1176
1177         weightsystem_list.max_index++;
1178         gtk_widget_set_sensitive(weightsystem_list.add, weightsystem_list.max_index < MAX_WEIGHTSYSTEMS);
1179 }
1180
1181 static void ws_del_cb(GtkButton *button, GtkTreeView *tree_view)
1182 {
1183         int index, nr;
1184         GtkTreeIter iter;
1185         GtkListStore *model = weightsystem_list.model;
1186         GtkTreeSelection *selection;
1187         struct dive *dive;
1188         weightsystem_t *ws;
1189
1190         selection = gtk_tree_view_get_selection(tree_view);
1191
1192         /* Nothing selected? This shouldn't happen, since the button should be inactive */
1193         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1194                 return;
1195
1196         index = get_model_index(model, &iter);
1197
1198         dive = current_dive;
1199         if (!dive)
1200                 return;
1201         ws = dive->weightsystem + index;
1202         nr = weightsystem_list.max_index - index - 1;
1203
1204         gtk_list_store_remove(model, &iter);
1205
1206         weightsystem_list.max_index--;
1207         memmove(ws, ws+1, nr*sizeof(*ws));
1208         memset(ws+nr, 0, sizeof(*ws));
1209
1210         mark_divelist_changed(TRUE);
1211         flush_divelist(dive);
1212
1213         gtk_widget_set_sensitive(weightsystem_list.edit, 0);
1214         gtk_widget_set_sensitive(weightsystem_list.del, 0);
1215         gtk_widget_set_sensitive(weightsystem_list.add, 1);
1216 }
1217
1218 static GtkListStore *create_tank_size_model(void)
1219 {
1220         GtkListStore *model;
1221
1222         model = gtk_list_store_new(3,
1223                 G_TYPE_STRING,          /* Tank name */
1224                 G_TYPE_INT,             /* Tank size in mliter */
1225                 G_TYPE_INT,             /* Tank working pressure in mbar */
1226                 -1);
1227
1228         fill_tank_list(model);
1229         return model;
1230 }
1231
1232 static GtkListStore *create_weightsystem_model(void)
1233 {
1234         GtkListStore *model;
1235
1236         model = gtk_list_store_new(2,
1237                 G_TYPE_STRING,          /* Weightsystem description */
1238                 G_TYPE_INT,             /* Weight in grams */
1239                 -1);
1240
1241         fill_ws_list(model);
1242         return model;
1243 }
1244
1245 static void size_data_func(GtkTreeViewColumn *col,
1246                            GtkCellRenderer *renderer,
1247                            GtkTreeModel *model,
1248                            GtkTreeIter *iter,
1249                            gpointer data)
1250 {
1251         int ml, mbar;
1252         double size, pressure;
1253         char buffer[10];
1254
1255         gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
1256         convert_volume_pressure(ml, mbar, &size, &pressure);
1257         if (size)
1258                 snprintf(buffer, sizeof(buffer), "%.1f", size);
1259         else
1260                 strcpy(buffer, "unkn");
1261         g_object_set(renderer, "text", buffer, NULL);
1262 }
1263
1264 static void weight_data_func(GtkTreeViewColumn *col,
1265                            GtkCellRenderer *renderer,
1266                            GtkTreeModel *model,
1267                            GtkTreeIter *iter,
1268                            gpointer data)
1269 {
1270         int idx = (long)data;
1271         int grams, decimals;
1272         double value;
1273         char buffer[10];
1274
1275         gtk_tree_model_get(model, iter, idx, &grams, -1);
1276         decimals = convert_weight(grams, &value);
1277         if (grams)
1278                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
1279         else
1280                 strcpy(buffer, "unkn");
1281         g_object_set(renderer, "text", buffer, NULL);
1282 }
1283
1284 static void pressure_data_func(GtkTreeViewColumn *col,
1285                            GtkCellRenderer *renderer,
1286                            GtkTreeModel *model,
1287                            GtkTreeIter *iter,
1288                            gpointer data)
1289 {
1290         int index = (long)data;
1291         int mbar, decimals;
1292         double pressure;
1293         char buffer[10];
1294
1295         gtk_tree_model_get(model, iter, index, &mbar, -1);
1296         decimals = convert_pressure(mbar, &pressure);
1297         if (mbar)
1298                 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
1299         else
1300                 *buffer = 0;
1301         g_object_set(renderer, "text", buffer, NULL);
1302 }
1303
1304 static void percentage_data_func(GtkTreeViewColumn *col,
1305                            GtkCellRenderer *renderer,
1306                            GtkTreeModel *model,
1307                            GtkTreeIter *iter,
1308                            gpointer data)
1309 {
1310         int index = (long)data;
1311         int permille;
1312         char buffer[10];
1313
1314         gtk_tree_model_get(model, iter, index, &permille, -1);
1315         if (permille)
1316                 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
1317         else
1318                 *buffer = 0;
1319         g_object_set(renderer, "text", buffer, NULL);
1320 }
1321
1322 static void selection_cb(GtkTreeSelection *selection, struct equipment_list *list)
1323 {
1324         GtkTreeIter iter;
1325         int selected;
1326
1327         selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
1328         gtk_widget_set_sensitive(list->edit, selected);
1329         gtk_widget_set_sensitive(list->del, selected);
1330 }
1331
1332 static void row_activated_cb(GtkTreeView *tree_view,
1333                         GtkTreePath *path,
1334                         GtkTreeViewColumn *column,
1335                         GtkTreeModel *model)
1336 {
1337         edit_cb(NULL, tree_view);
1338 }
1339
1340 GtkWidget *cylinder_list_widget(void)
1341 {
1342         GtkListStore *model = cylinder_list.model;
1343         GtkWidget *tree_view;
1344         GtkTreeSelection *selection;
1345
1346         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1347         gtk_widget_set_can_focus(tree_view, FALSE);
1348
1349         g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
1350
1351         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1352         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1353         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &cylinder_list);
1354
1355         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1356                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1357                                           NULL);
1358
1359         tree_view_column(tree_view, CYL_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1360         tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, ALIGN_RIGHT | UNSORTABLE);
1361         tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1362         tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1363         tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1364         tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1365         tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1366         return tree_view;
1367 }
1368
1369 GtkWidget *weightsystem_list_widget(void)
1370 {
1371         GtkListStore *model = weightsystem_list.model;
1372         GtkWidget *tree_view;
1373         GtkTreeSelection *selection;
1374
1375         tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1376         gtk_widget_set_can_focus(tree_view, FALSE);
1377         g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
1378
1379         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1380         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1381         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &weightsystem_list);
1382
1383         g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1384                                           "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1385                                           NULL);
1386
1387         tree_view_column(tree_view, WS_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1388         tree_view_column(tree_view, WS_WEIGHT, "weight",
1389                         weight_data_func, ALIGN_RIGHT | UNSORTABLE);
1390
1391         return tree_view;
1392 }
1393
1394 static GtkWidget *cylinder_list_create(void)
1395 {
1396         GtkListStore *model;
1397
1398         model = gtk_list_store_new(CYL_COLUMNS,
1399                 G_TYPE_STRING,          /* CYL_DESC: utf8 */
1400                 G_TYPE_INT,             /* CYL_SIZE: mliter */
1401                 G_TYPE_INT,             /* CYL_WORKP: mbar */
1402                 G_TYPE_INT,             /* CYL_STARTP: mbar */
1403                 G_TYPE_INT,             /* CYL_ENDP: mbar */
1404                 G_TYPE_INT,             /* CYL_O2: permille */
1405                 G_TYPE_INT              /* CYL_HE: permille */
1406                 );
1407         cylinder_list.model = model;
1408         return cylinder_list_widget();
1409 }
1410
1411 static GtkWidget *weightsystem_list_create(void)
1412 {
1413         GtkListStore *model;
1414
1415         model = gtk_list_store_new(WS_COLUMNS,
1416                 G_TYPE_STRING,          /* WS_DESC: utf8 */
1417                 G_TYPE_INT              /* WS_WEIGHT: grams */
1418                 );
1419         weightsystem_list.model = model;
1420         return weightsystem_list_widget();
1421 }
1422
1423 GtkWidget *equipment_widget(void)
1424 {
1425         GtkWidget *vbox, *hbox, *frame, *framebox, *tree_view;
1426         GtkWidget *add, *del, *edit;
1427
1428         vbox = gtk_vbox_new(FALSE, 3);
1429
1430         /*
1431          * We create the cylinder size model at startup, since
1432          * we're going to share it across all cylinders and all
1433          * dives. So if you add a new cylinder type in one dive,
1434          * it will show up when you edit the cylinder types for
1435          * another dive.
1436          */
1437         cylinder_model = create_tank_size_model();
1438         tree_view = cylinder_list_create();
1439
1440         hbox = gtk_hbox_new(FALSE, 3);
1441         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1442
1443         frame = gtk_frame_new("Cylinders");
1444         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1445
1446         framebox = gtk_vbox_new(FALSE, 3);
1447         gtk_container_add(GTK_CONTAINER(frame), framebox);
1448
1449         hbox = gtk_hbox_new(FALSE, 3);
1450         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1451
1452         gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1453
1454         hbox = gtk_hbox_new(TRUE, 3);
1455         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1456
1457         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1458         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1459         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1460         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1461         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1462         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1463
1464         cylinder_list.edit = edit;
1465         cylinder_list.add = add;
1466         cylinder_list.del = del;
1467
1468         g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), tree_view);
1469         g_signal_connect(add, "clicked", G_CALLBACK(add_cb), tree_view);
1470         g_signal_connect(del, "clicked", G_CALLBACK(del_cb), tree_view);
1471
1472         hbox = gtk_hbox_new(FALSE, 3);
1473         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1474
1475         weightsystem_model = create_weightsystem_model();
1476         tree_view = weightsystem_list_create();
1477
1478         frame = gtk_frame_new("Weight");
1479         gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1480
1481         framebox = gtk_vbox_new(FALSE, 3);
1482         gtk_container_add(GTK_CONTAINER(frame), framebox);
1483
1484         hbox = gtk_hbox_new(FALSE, 3);
1485         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1486
1487         gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1488
1489         hbox = gtk_hbox_new(TRUE, 3);
1490         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1491
1492         edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1493         add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1494         del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1495         gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1496         gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1497         gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1498
1499         weightsystem_list.edit = edit;
1500         weightsystem_list.add = add;
1501         weightsystem_list.del = del;
1502
1503         g_signal_connect(edit, "clicked", G_CALLBACK(ws_edit_cb), tree_view);
1504         g_signal_connect(add, "clicked", G_CALLBACK(ws_add_cb), tree_view);
1505         g_signal_connect(del, "clicked", G_CALLBACK(ws_del_cb), tree_view);
1506
1507         return vbox;
1508 }