]> git.tdb.fi Git - ext/subsurface.git/blob - equipment.c
Merge branch 'for-linus' of git://github.com/dirkhh/subsurface
[ext/subsurface.git] / equipment.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <time.h>
6
7 #include "dive.h"
8 #include "display.h"
9 #include "divelist.h"
10
11 struct cylinder_widget {
12         int index, changed;
13         const char *name;
14         GtkWidget *hbox;
15         GtkComboBox *description;
16         GtkSpinButton *size, *pressure;
17         GtkWidget *o2, *gasmix_button;
18 };
19
20 static struct cylinder_widget gtk_cylinder[MAX_CYLINDERS];
21
22 static void set_cylinder_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
23 {
24         double volume, pressure;
25
26         volume = ml / 1000.0;
27         pressure = mbar / 1000.0;
28         if (mbar) {
29                 if (output_units.volume == CUFT) {
30                         volume /= 28.3168466;   /* Liters to cuft */
31                         volume *= pressure / 1.01325;
32                 }
33                 if (output_units.pressure == PSI) {
34                         pressure *= 14.5037738; /* Bar to PSI */
35                 }
36         }
37
38         gtk_spin_button_set_value(cylinder->size, volume);
39         gtk_spin_button_set_value(cylinder->pressure, pressure);
40 }
41
42 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
43 {
44         GtkTreeIter iter;
45         GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
46         GValue value1 = {0, }, value2 = {0,};
47         struct cylinder_widget *cylinder = data;
48         cylinder_t *cyl = current_dive->cylinder + cylinder->index;
49
50         /* Did the user set it to some non-standard value? */
51         if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
52                 cylinder->changed = 1;
53                 return;
54         }
55
56         /*
57          * We get "change" signal callbacks just because we set
58          * the description by hand. Whatever. So ignore them if
59          * they are no-ops.
60          */
61         if (!cylinder->changed && cyl->type.description) {
62                 int same;
63                 char *desc = gtk_combo_box_get_active_text(combo_box);
64
65                 same = !strcmp(desc, cyl->type.description);
66                 g_free(desc);
67                 if (same)
68                         return;
69         }
70         cylinder->changed = 1;
71
72         gtk_tree_model_get_value(model, &iter, 1, &value1);
73         gtk_tree_model_get_value(model, &iter, 2, &value2);
74
75         set_cylinder_spinbuttons(cylinder, g_value_get_int(&value1), g_value_get_int(&value2));
76 }
77
78 /*
79  * The gtk_tree_model_foreach() interface is bad. It could have
80  * returned whether the callback ever returned true
81  */
82 static int found_match = 0;
83
84 static gboolean match_cylinder(GtkTreeModel *model,
85                                 GtkTreePath *path,
86                                 GtkTreeIter *iter,
87                                 gpointer data)
88 {
89         const char *name;
90         struct cylinder_widget *cylinder = data;
91         GValue value = {0, };
92
93         gtk_tree_model_get_value(model, iter, 0, &value);
94         name = g_value_get_string(&value);
95         if (strcmp(cylinder->name, name))
96                 return FALSE;
97         gtk_combo_box_set_active_iter(cylinder->description, iter);
98         found_match = 1;
99         return TRUE;
100 }
101
102 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
103 {
104         GtkTreeModel *model;
105
106         found_match = 0;
107         model = gtk_combo_box_get_model(cylinder->description);
108         cylinder->name = desc;
109         gtk_tree_model_foreach(model, match_cylinder, cylinder);
110
111         if (!found_match) {
112                 GtkListStore *store = GTK_LIST_STORE(model);
113                 GtkTreeIter iter;
114
115                 gtk_list_store_append(store, &iter);
116                 gtk_list_store_set(store, &iter,
117                         0, desc,
118                         1, ml,
119                         2, mbar,
120                         -1);
121                 gtk_combo_box_set_active_iter(cylinder->description, &iter);
122         }
123 }
124
125 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
126 {
127         const char *desc;
128         int ml, mbar;
129         double o2;
130
131         /* Don't show uninitialized cylinder widgets */
132         if (!cylinder->description)
133                 return;
134
135         desc = cyl->type.description;
136         if (!desc)
137                 desc = "";
138         ml = cyl->type.size.mliter;
139         mbar = cyl->type.workingpressure.mbar;
140         add_cylinder(cylinder, desc, ml, mbar);
141
142         set_cylinder_spinbuttons(cylinder, cyl->type.size.mliter, cyl->type.workingpressure.mbar);
143         o2 = cyl->gasmix.o2.permille / 10.0;
144         gtk_widget_set_sensitive(cylinder->o2, !!o2);
145         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
146         if (!o2)
147                 o2 = 21.0;
148         gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
149 }
150
151 void show_dive_equipment(struct dive *dive)
152 {
153         int i;
154
155         for (i = 0; i < MAX_CYLINDERS; i++)
156                 show_cylinder(dive->cylinder + i, gtk_cylinder+i);
157 }
158
159 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
160 {
161         GtkWidget *frame, *hbox, *button;
162
163         frame = gtk_frame_new(name);
164         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
165
166         hbox = gtk_hbox_new(FALSE, 3);
167         gtk_container_add(GTK_CONTAINER(frame), hbox);
168
169         button = gtk_spin_button_new_with_range(min, max, incr);
170         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
171
172         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
173
174         return button;
175 }
176
177 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
178 {
179         int mbar, ml;
180
181         if (output_units.pressure == PSI)
182                 pressure /= 14.5037738;
183
184         if (pressure && output_units.volume == CUFT) {
185                 volume *= 28.3168466;   /* CUFT to liter */
186                 volume /= pressure / 1.01325;
187         }
188
189         ml = volume * 1000 + 0.5;
190         mbar = pressure * 1000 + 0.5;
191
192         if (o2 < 211)
193                 o2 = 0;
194         cyl->type.description = desc;
195         cyl->type.size.mliter = ml;
196         cyl->type.workingpressure.mbar = mbar;
197         cyl->gasmix.o2.permille = o2;
198
199         /*
200          * Also, insert it into the model if it doesn't already exist
201          */
202         add_cylinder(cylinder, desc, ml, mbar);
203 }
204
205 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
206 {
207         const gchar *desc;
208         GtkComboBox *box;
209         double volume, pressure;
210         int o2;
211
212         /* Ignore uninitialized cylinder widgets */
213         box = cylinder->description;
214         if (!box)
215                 return;
216
217         desc = gtk_combo_box_get_active_text(box);
218         volume = gtk_spin_button_get_value(cylinder->size);
219         pressure = gtk_spin_button_get_value(cylinder->pressure);
220         o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
221         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
222                 o2 = 0;
223         fill_cylinder_info(cylinder, cyl, desc, volume, pressure, o2);
224 }
225
226 void flush_dive_equipment_changes(struct dive *dive)
227 {
228         /* We do nothing: we require the "Ok" button press */
229 }
230
231 static void apply_cb(GtkButton *button, gpointer data)
232 {
233         int i;
234         struct dive *dive = current_dive;
235
236         if (!dive)
237                 return;
238
239         for (i = 0; i < MAX_CYLINDERS; i++)
240                 record_cylinder_changes(dive->cylinder+i, gtk_cylinder+i);
241         flush_divelist(dive);
242 }
243
244 static void cancel_cb(GtkButton *button, gpointer data)
245 {
246         struct dive *dive = current_dive;
247
248         if (!dive)
249                 return;
250
251         show_dive_equipment(current_dive);
252 }
253
254 /*
255  * We hardcode the most common standard cylinders,
256  * we should pick up any other names from the dive
257  * logs directly.
258  */
259 static struct tank_info {
260         const char *name;
261         int size;       /* cuft if < 1000, otherwise mliter */
262         int psi;        /* If zero, size is in mliter */
263 } tank_info[100] = {
264         /* Need an empty entry for the no-cylinder case */
265         { "", 0, 0 },
266
267         /* Size-only metric cylinders */
268         { "10.0 l", 10000 },
269         { "11.1 l", 11100 },
270
271         /* Most common AL cylinders */
272         { "AL50",   50, 3000 },
273         { "AL63",   63, 3000 },
274         { "AL72",   72, 3000 },
275         { "AL80",   80, 3000 },
276         { "AL100", 100, 3300 },
277
278         /* Somewhat common LP steel cylinders */
279         { "LP85",   85, 2640 },
280         { "LP95",   95, 2640 },
281         { "LP108", 108, 2640 },
282         { "LP121", 121, 2640 },
283
284         /* Somewhat common HP steel cylinders */
285         { "HP65",   65, 3442 },
286         { "HP80",   80, 3442 },
287         { "HP100", 100, 3442 },
288         { "HP119", 119, 3442 },
289         { "HP130", 130, 3442 },
290
291         /* We'll fill in more from the dive log dynamically */
292         { NULL, }
293 };
294
295 static void fill_tank_list(GtkListStore *store)
296 {
297         GtkTreeIter iter;
298         struct tank_info *info = tank_info;
299
300         while (info->name) {
301                 int size = info->size;
302                 int psi = info->psi;
303                 int mbar = 0, ml = size;
304
305                 /* Is it in cuft and psi? */
306                 if (psi) {
307                         double bar = 0.0689475729 * psi;
308                         double airvolume = 28316.8466 * size;
309                         double atm = bar / 1.01325;
310
311                         ml = airvolume / atm + 0.5;
312                         mbar = bar*1000 + 0.5;
313                 }
314
315                 gtk_list_store_append(store, &iter);
316                 gtk_list_store_set(store, &iter,
317                         0, info->name,
318                         1, ml,
319                         2, mbar,
320                         -1);
321                 info++;
322         }
323 }
324
325 static void nitrox_cb(GtkToggleButton *button, gpointer data)
326 {
327         struct cylinder_widget *cylinder = data;
328         int state;
329
330         state = gtk_toggle_button_get_active(button);
331         gtk_widget_set_sensitive(cylinder->o2, state);
332 }
333
334 static void cylinder_widget(int nr, GtkListStore *model)
335 {
336         struct cylinder_widget *cylinder;
337         GtkWidget *frame, *hbox, *hbox2;
338         GtkWidget *widget;
339         char buffer[80];
340
341         cylinder = gtk_cylinder + nr;
342         cylinder->index = nr;
343
344         hbox = gtk_hbox_new(FALSE, 3);
345         cylinder->hbox = hbox;
346
347         snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
348         frame = gtk_frame_new(buffer);
349         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
350
351         hbox2 = gtk_hbox_new(FALSE, 3);
352         gtk_container_add(GTK_CONTAINER(frame), hbox2);
353
354         widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
355         gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
356
357         cylinder->description = GTK_COMBO_BOX(widget);
358         g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
359
360         widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
361         cylinder->size = GTK_SPIN_BUTTON(widget);
362
363         widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
364         cylinder->pressure = GTK_SPIN_BUTTON(widget);
365
366         widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
367         cylinder->o2 = widget;
368         cylinder->gasmix_button = gtk_check_button_new();
369         gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
370                 cylinder->gasmix_button, FALSE, FALSE, 3);
371         g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
372
373         gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
374 }
375
376 static GtkListStore *create_tank_size_model(void)
377 {
378         GtkListStore *model;
379
380         model = gtk_list_store_new(3,
381                 G_TYPE_STRING,          /* Tank name */
382                 G_TYPE_INT,             /* Tank size in mliter */
383                 G_TYPE_INT,             /* Tank working pressure in mbar */
384                 -1);
385
386         fill_tank_list(model);
387         return model;
388 }
389
390 GtkWidget *equipment_widget(void)
391 {
392         int i;
393         GtkWidget *vbox, *hbox;
394         GtkWidget *apply, *cancel;
395         GtkListStore *model;
396
397         vbox = gtk_vbox_new(FALSE, 3);
398
399         model = create_tank_size_model();
400
401         /* Create all MAX_CYLINDER gtk widgets */
402         for (i = 0; i < MAX_CYLINDERS; i++)
403                 cylinder_widget(i, model);
404
405         /* But only connect two of them right now to the frame vbox */
406         for (i = 0; i < 2; i++) {
407                 struct cylinder_widget *cylinder = gtk_cylinder+i;
408                 gtk_box_pack_start(GTK_BOX(vbox), cylinder->hbox, FALSE, TRUE, 0);
409         }
410
411         hbox = gtk_hbox_new(TRUE, 3);
412         gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
413
414         apply = gtk_button_new_from_stock(GTK_STOCK_APPLY);
415         cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
416         gtk_box_pack_start(GTK_BOX(hbox), apply, FALSE, FALSE, 0);
417         gtk_box_pack_start(GTK_BOX(hbox), cancel, FALSE, FALSE, 0);
418
419         g_signal_connect(apply, "clicked", G_CALLBACK(apply_cb), NULL);
420         g_signal_connect(cancel, "clicked", G_CALLBACK(cancel_cb), NULL);
421
422         return vbox;
423 }