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