11 struct cylinder_widget {
15 GtkComboBox *description;
16 GtkSpinButton *size, *pressure;
17 GtkWidget *o2, *gasmix_button;
20 static struct cylinder_widget gtk_cylinder[MAX_CYLINDERS];
22 static void set_cylinder_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
24 double volume, pressure;
27 pressure = mbar / 1000.0;
29 if (output_units.volume == CUFT) {
30 volume /= 28.3168466; /* Liters to cuft */
31 volume *= pressure / 1.01325;
33 if (output_units.pressure == PSI) {
34 pressure *= 14.5037738; /* Bar to PSI */
38 gtk_spin_button_set_value(cylinder->size, volume);
39 gtk_spin_button_set_value(cylinder->pressure, pressure);
42 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
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;
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;
57 * We get "change" signal callbacks just because we set
58 * the description by hand. Whatever. So ignore them if
61 if (!cylinder->changed && cyl->type.description) {
63 char *desc = gtk_combo_box_get_active_text(combo_box);
65 same = !strcmp(desc, cyl->type.description);
70 cylinder->changed = 1;
72 gtk_tree_model_get_value(model, &iter, 1, &value1);
73 gtk_tree_model_get_value(model, &iter, 2, &value2);
75 set_cylinder_spinbuttons(cylinder, g_value_get_int(&value1), g_value_get_int(&value2));
79 * The gtk_tree_model_foreach() interface is bad. It could have
80 * returned whether the callback ever returned true
82 static int found_match = 0;
84 static gboolean match_cylinder(GtkTreeModel *model,
90 struct cylinder_widget *cylinder = data;
93 gtk_tree_model_get_value(model, iter, 0, &value);
94 name = g_value_get_string(&value);
95 if (strcmp(cylinder->name, name))
97 gtk_combo_box_set_active_iter(cylinder->description, iter);
102 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
107 model = gtk_combo_box_get_model(cylinder->description);
108 cylinder->name = desc;
109 gtk_tree_model_foreach(model, match_cylinder, cylinder);
112 GtkListStore *store = GTK_LIST_STORE(model);
115 gtk_list_store_append(store, &iter);
116 gtk_list_store_set(store, &iter,
121 gtk_combo_box_set_active_iter(cylinder->description, &iter);
125 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
131 /* Don't show uninitialized cylinder widgets */
132 if (!cylinder->description)
135 desc = cyl->type.description;
138 ml = cyl->type.size.mliter;
139 mbar = cyl->type.workingpressure.mbar;
140 add_cylinder(cylinder, desc, ml, mbar);
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);
148 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
151 void show_dive_equipment(struct dive *dive)
155 for (i = 0; i < MAX_CYLINDERS; i++)
156 show_cylinder(dive->cylinder + i, gtk_cylinder+i);
159 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
161 GtkWidget *frame, *hbox, *button;
163 frame = gtk_frame_new(name);
164 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
166 hbox = gtk_hbox_new(FALSE, 3);
167 gtk_container_add(GTK_CONTAINER(frame), hbox);
169 button = gtk_spin_button_new_with_range(min, max, incr);
170 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
172 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
177 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
181 if (output_units.pressure == PSI)
182 pressure /= 14.5037738;
184 if (pressure && output_units.volume == CUFT) {
185 volume *= 28.3168466; /* CUFT to liter */
186 volume /= pressure / 1.01325;
189 ml = volume * 1000 + 0.5;
190 mbar = pressure * 1000 + 0.5;
194 cyl->type.description = desc;
195 cyl->type.size.mliter = ml;
196 cyl->type.workingpressure.mbar = mbar;
197 cyl->gasmix.o2.permille = o2;
200 * Also, insert it into the model if it doesn't already exist
202 add_cylinder(cylinder, desc, ml, mbar);
205 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
209 double volume, pressure;
212 /* Ignore uninitialized cylinder widgets */
213 box = cylinder->description;
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)))
223 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, o2);
226 void flush_dive_equipment_changes(struct dive *dive)
230 for (i = 0; i < MAX_CYLINDERS; i++)
231 record_cylinder_changes(dive->cylinder+i, gtk_cylinder+i);
235 * We hardcode the most common standard cylinders,
236 * we should pick up any other names from the dive
239 static struct tank_info {
241 int size; /* cuft if < 1000, otherwise mliter */
242 int psi; /* If zero, size is in mliter */
244 /* Need an empty entry for the no-cylinder case */
247 /* Size-only metric cylinders */
251 /* Most common AL cylinders */
252 { "AL50", 50, 3000 },
253 { "AL63", 63, 3000 },
254 { "AL72", 72, 3000 },
255 { "AL80", 80, 3000 },
256 { "AL100", 100, 3300 },
258 /* Somewhat common LP steel cylinders */
259 { "LP85", 85, 2640 },
260 { "LP95", 95, 2640 },
261 { "LP108", 108, 2640 },
262 { "LP121", 121, 2640 },
264 /* Somewhat common HP steel cylinders */
265 { "HP65", 65, 3442 },
266 { "HP80", 80, 3442 },
267 { "HP100", 100, 3442 },
268 { "HP119", 119, 3442 },
269 { "HP130", 130, 3442 },
271 /* We'll fill in more from the dive log dynamically */
275 static void fill_tank_list(GtkListStore *store)
278 struct tank_info *info = tank_info;
281 int size = info->size;
283 int mbar = 0, ml = size;
285 /* Is it in cuft and psi? */
287 double bar = 0.0689475729 * psi;
288 double airvolume = 28316.8466 * size;
289 double atm = bar / 1.01325;
291 ml = airvolume / atm + 0.5;
292 mbar = bar*1000 + 0.5;
295 gtk_list_store_append(store, &iter);
296 gtk_list_store_set(store, &iter,
305 static void nitrox_cb(GtkToggleButton *button, gpointer data)
307 struct cylinder_widget *cylinder = data;
310 state = gtk_toggle_button_get_active(button);
311 gtk_widget_set_sensitive(cylinder->o2, state);
314 static void cylinder_widget(int nr, GtkListStore *model)
316 struct cylinder_widget *cylinder;
317 GtkWidget *frame, *hbox, *hbox2;
321 cylinder = gtk_cylinder + nr;
322 cylinder->index = nr;
324 hbox = gtk_hbox_new(FALSE, 3);
325 cylinder->hbox = hbox;
327 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
328 frame = gtk_frame_new(buffer);
329 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
331 hbox2 = gtk_hbox_new(FALSE, 3);
332 gtk_container_add(GTK_CONTAINER(frame), hbox2);
334 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
335 gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
337 cylinder->description = GTK_COMBO_BOX(widget);
338 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
340 widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
341 cylinder->size = GTK_SPIN_BUTTON(widget);
343 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
344 cylinder->pressure = GTK_SPIN_BUTTON(widget);
346 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
347 cylinder->o2 = widget;
348 cylinder->gasmix_button = gtk_check_button_new();
349 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
350 cylinder->gasmix_button, FALSE, FALSE, 3);
351 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
353 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
356 static GtkListStore *create_tank_size_model(void)
360 model = gtk_list_store_new(3,
361 G_TYPE_STRING, /* Tank name */
362 G_TYPE_INT, /* Tank size in mliter */
363 G_TYPE_INT, /* Tank working pressure in mbar */
366 fill_tank_list(model);
370 GtkWidget *equipment_widget(void)
376 vbox = gtk_vbox_new(FALSE, 3);
378 model = create_tank_size_model();
380 /* Create all MAX_CYLINDER gtk widgets */
381 for (i = 0; i < MAX_CYLINDERS; i++)
382 cylinder_widget(i, model);
384 /* But only connect two of them right now to the frame vbox */
385 for (i = 0; i < 2; i++) {
386 struct cylinder_widget *cylinder = gtk_cylinder+i;
387 gtk_box_pack_start(GTK_BOX(vbox), cylinder->hbox, FALSE, TRUE, 0);