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