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