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