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