2 /* this creates the UI for the dive list -
3 * controlled through the following interfaces:
5 * void flush_divelist(struct dive *dive)
6 * GtkWidget dive_list_create(void)
7 * void dive_list_update_dives(void)
8 * void update_dive_list_units(void)
9 * void set_divelist_font(const char *font)
10 * void mark_divelist_changed(int changed)
11 * int unsaved_changes()
22 #include "display-gtk.h"
26 GtkWidget *container_widget;
28 GtkTreeViewColumn *date, *depth, *duration, *location;
29 GtkTreeViewColumn *temperature, *cylinder, *nitrox, *sac, *otu;
33 static struct DiveList dive_list;
36 * The dive list has the dive data in both string format (for showing)
37 * and in "raw" format (for sorting purposes)
41 DIVE_DATE, /* time_t: dive->when */
42 DIVE_DEPTH, /* int: dive->maxdepth in mm */
43 DIVE_DURATION, /* int: in seconds */
44 DIVE_TEMPERATURE, /* int: in mkelvin */
46 DIVE_NITROX, /* int: in permille */
47 DIVE_SAC, /* int: in ml/min */
48 DIVE_OTU, /* int: in OTUs */
49 DIVE_LOCATION, /* "2nd Cathedral, Lanai" */
53 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
58 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
61 gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
62 selected_dive = g_value_get_int(&value);
66 static void date_data_func(GtkTreeViewColumn *col,
67 GtkCellRenderer *renderer,
77 gtk_tree_model_get(model, iter, DIVE_DATE, &val, -1);
83 snprintf(buffer, sizeof(buffer),
84 "%s, %s %d, %d %02d:%02d",
86 monthname(tm->tm_mon),
87 tm->tm_mday, tm->tm_year + 1900,
88 tm->tm_hour, tm->tm_min);
89 g_object_set(renderer, "text", buffer, NULL);
92 static void depth_data_func(GtkTreeViewColumn *col,
93 GtkCellRenderer *renderer,
98 int depth, integer, frac, len;
101 gtk_tree_model_get(model, iter, DIVE_DEPTH, &depth, -1);
103 switch (output_units.length) {
105 /* To tenths of meters */
106 depth = (depth + 49) / 100;
107 integer = depth / 10;
115 integer = mm_to_feet(depth) + 0.5;
121 len = snprintf(buffer, sizeof(buffer), "%d", integer);
123 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
125 g_object_set(renderer, "text", buffer, NULL);
128 static void duration_data_func(GtkTreeViewColumn *col,
129 GtkCellRenderer *renderer,
137 gtk_tree_model_get(model, iter, DIVE_DURATION, &sec, -1);
138 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
140 g_object_set(renderer, "text", buffer, NULL);
143 static void temperature_data_func(GtkTreeViewColumn *col,
144 GtkCellRenderer *renderer,
152 gtk_tree_model_get(model, iter, DIVE_TEMPERATURE, &value, -1);
157 switch (output_units.temperature) {
159 deg = mkelvin_to_C(value);
162 deg = mkelvin_to_F(value);
167 snprintf(buffer, sizeof(buffer), "%.1f", deg);
170 g_object_set(renderer, "text", buffer, NULL);
173 static void nitrox_data_func(GtkTreeViewColumn *col,
174 GtkCellRenderer *renderer,
182 gtk_tree_model_get(model, iter, DIVE_NITROX, &value, -1);
185 snprintf(buffer, sizeof(buffer), "%.1f", value/10.0);
187 strcpy(buffer, "air");
189 g_object_set(renderer, "text", buffer, NULL);
192 /* Render the SAC data (integer value of "ml / min") */
193 static void sac_data_func(GtkTreeViewColumn *col,
194 GtkCellRenderer *renderer,
200 const double liters_per_cuft = 28.317;
205 gtk_tree_model_get(model, iter, DIVE_SAC, &value, -1);
208 g_object_set(renderer, "text", "", NULL);
212 sac = value / 1000.0;
213 switch (output_units.volume) {
219 sac /= liters_per_cuft;
222 snprintf(buffer, sizeof(buffer), fmt, sac);
224 g_object_set(renderer, "text", buffer, NULL);
227 /* Render the OTU data (integer value of "OTU") */
228 static void otu_data_func(GtkTreeViewColumn *col,
229 GtkCellRenderer *renderer,
237 gtk_tree_model_get(model, iter, DIVE_OTU, &value, -1);
240 g_object_set(renderer, "text", "", NULL);
244 snprintf(buffer, sizeof(buffer), "%d", value);
246 g_object_set(renderer, "text", buffer, NULL);
249 /* calculate OTU for a dive */
250 static int calculate_otu(struct dive *dive)
255 for (i = 1; i < dive->samples; i++) {
258 struct sample *sample = dive->sample + i;
259 struct sample *psample = sample - 1;
260 t = sample->time.seconds - psample->time.seconds;
261 po2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille / 1000.0 *
262 (sample->depth.mm + 10000) / 10000.0;
264 otu += pow(po2 - 0.5, 0.83) * t / 30.0;
269 * Return air usage (in liters).
271 static double calculate_airuse(struct dive *dive)
276 for (i = 0; i < MAX_CYLINDERS; i++) {
277 cylinder_t *cyl = dive->cylinder + i;
278 int size = cyl->type.size.mliter;
284 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
286 /* Liters of air at 1 atm == milliliters at 1k atm*/
287 airuse += kilo_atm * size;
292 static void get_sac(struct dive *dive, int *val)
294 double airuse, pressure, sac;
297 airuse = calculate_airuse(dive);
300 if (!dive->duration.seconds)
303 /* Mean pressure in atm: 1 atm per 10m */
304 pressure = 1 + (dive->meandepth.mm / 10000.0);
305 sac = airuse / pressure * 60 / dive->duration.seconds;
307 /* milliliters per minute.. */
311 static void get_string(char **str, const char *s)
327 static void get_location(struct dive *dive, char **str)
329 get_string(str, dive->location);
332 static void get_cylinder(struct dive *dive, char **str)
334 get_string(str, dive->cylinder[0].type.description);
337 static void fill_one_dive(struct dive *dive,
342 char *location, *cylinder;
344 get_cylinder(dive, &cylinder);
345 get_location(dive, &location);
349 * We only set the fields that changed: the strings.
350 * The core data itself is unaffected by units
352 gtk_list_store_set(GTK_LIST_STORE(model), iter,
353 DIVE_LOCATION, location,
354 DIVE_CYLINDER, cylinder,
360 static gboolean set_one_dive(GtkTreeModel *model,
365 GValue value = {0, };
368 /* Get the dive number */
369 gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
370 dive = get_dive(g_value_get_int(&value));
373 if (data && dive != data)
376 fill_one_dive(dive, model, iter);
380 void flush_divelist(struct dive *dive)
382 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
384 gtk_tree_model_foreach(model, set_one_dive, dive);
387 void set_divelist_font(const char *font)
389 PangoFontDescription *font_desc = pango_font_description_from_string(font);
390 gtk_widget_modify_font(dive_list.tree_view, font_desc);
391 pango_font_description_free(font_desc);
394 void update_dive_list_units(void)
397 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
399 switch (output_units.length) {
407 gtk_tree_view_column_set_title(dive_list.depth, unit);
409 switch (output_units.temperature) {
411 unit = UTF8_DEGREE "C";
414 unit = UTF8_DEGREE "F";
420 gtk_tree_view_column_set_title(dive_list.temperature, unit);
422 gtk_tree_model_foreach(model, set_one_dive, NULL);
425 void update_dive_list_col_visibility(void)
427 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
428 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
432 static void fill_dive_list(void)
438 store = GTK_LIST_STORE(dive_list.model);
440 for (i = 0; i < dive_table.nr; i++) {
441 struct dive *dive = dive_table.dives[i];
443 dive->otu = calculate_otu(dive);
444 gtk_list_store_append(store, &iter);
445 gtk_list_store_set(store, &iter,
447 DIVE_DATE, dive->when,
448 DIVE_DEPTH, dive->maxdepth,
449 DIVE_DURATION, dive->duration.seconds,
450 DIVE_LOCATION, "location",
451 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
452 DIVE_NITROX, dive->cylinder[0].gasmix.o2,
457 update_dive_list_units();
458 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
459 GtkTreeSelection *selection;
460 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
461 gtk_tree_selection_select_iter(selection, &iter);
465 void dive_list_update_dives(void)
467 gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
472 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, int index, const char *title,
473 data_func_t data_func, PangoAlignment align, gboolean visible)
475 return tree_view_column(dl->tree_view, index, title, data_func, align, visible);
479 * This is some crazy crap. The only way to get default focus seems
480 * to be to grab focus as the widget is being shown the first time.
482 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
484 gtk_widget_grab_focus(tree_view);
487 GtkWidget *dive_list_create(void)
489 GtkTreeSelection *selection;
491 dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
492 G_TYPE_INT, /* index */
493 G_TYPE_INT, /* Date */
494 G_TYPE_INT, /* Depth */
495 G_TYPE_INT, /* Duration */
496 G_TYPE_INT, /* Temperature */
497 G_TYPE_STRING, /* Cylinder */
498 G_TYPE_INT, /* Nitrox */
499 G_TYPE_INT, /* SAC */
500 G_TYPE_INT, /* OTU */
501 G_TYPE_STRING /* Location */
503 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
504 set_divelist_font(divelist_font);
506 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
508 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
509 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
511 dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE);
512 dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE);
513 dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE);
514 dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, TRUE);
515 dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, TRUE);
516 dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, TRUE);
517 dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, visible_cols.sac);
518 dive_list.otu = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, visible_cols.otu);
519 dive_list.location = divelist_column(&dive_list, DIVE_LOCATION, "Location", NULL, PANGO_ALIGN_LEFT, TRUE);
523 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
524 "search-column", DIVE_LOCATION,
528 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
529 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
531 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
532 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
533 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
534 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
536 dive_list.changed = 0;
538 return dive_list.container_widget;
541 void mark_divelist_changed(int changed)
543 dive_list.changed = changed;
546 int unsaved_changes()
548 return dive_list.changed;