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 *nr, *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_NR, /* int: dive->nr */
42 DIVE_DATE, /* time_t: dive->when */
43 DIVE_DEPTH, /* int: dive->maxdepth in mm */
44 DIVE_DURATION, /* int: in seconds */
45 DIVE_TEMPERATURE, /* int: in mkelvin */
47 DIVE_NITROX, /* int: in permille */
48 DIVE_SAC, /* int: in ml/min */
49 DIVE_OTU, /* int: in OTUs */
50 DIVE_LOCATION, /* "2nd Cathedral, Lanai" */
54 static GList *selected_dives;
56 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
62 int nr_selected = gtk_tree_selection_count_selected_rows(selection);
65 g_list_foreach (selected_dives, (GFunc) gtk_tree_path_free, NULL);
66 g_list_free (selected_dives);
68 selected_dives = gtk_tree_selection_get_selected_rows(selection, NULL);
70 switch (nr_selected) {
71 case 0: /* keep showing the last selected dive */
74 /* just pick that dive as selected */
75 path = g_list_nth_data(selected_dives, 0);
76 if (gtk_tree_model_get_iter(model, &iter, path)) {
77 gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
78 selected_dive = g_value_get_int(&value);
82 default: /* multiple selections - what now? At this point I
83 * don't want to change the selected dive unless
84 * there is exactly one dive selected; not sure this
85 * is the most intuitive solution.
86 * I do however want to keep around which dives have
92 static void date_data_func(GtkTreeViewColumn *col,
93 GtkCellRenderer *renderer,
103 gtk_tree_model_get(model, iter, DIVE_DATE, &val, -1);
109 snprintf(buffer, sizeof(buffer),
110 "%s, %s %d, %d %02d:%02d",
111 weekday(tm->tm_wday),
112 monthname(tm->tm_mon),
113 tm->tm_mday, tm->tm_year + 1900,
114 tm->tm_hour, tm->tm_min);
115 g_object_set(renderer, "text", buffer, NULL);
118 static void depth_data_func(GtkTreeViewColumn *col,
119 GtkCellRenderer *renderer,
124 int depth, integer, frac, len;
127 gtk_tree_model_get(model, iter, DIVE_DEPTH, &depth, -1);
129 switch (output_units.length) {
131 /* To tenths of meters */
132 depth = (depth + 49) / 100;
133 integer = depth / 10;
141 integer = mm_to_feet(depth) + 0.5;
147 len = snprintf(buffer, sizeof(buffer), "%d", integer);
149 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
151 g_object_set(renderer, "text", buffer, NULL);
154 static void duration_data_func(GtkTreeViewColumn *col,
155 GtkCellRenderer *renderer,
163 gtk_tree_model_get(model, iter, DIVE_DURATION, &sec, -1);
164 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
166 g_object_set(renderer, "text", buffer, NULL);
169 static void temperature_data_func(GtkTreeViewColumn *col,
170 GtkCellRenderer *renderer,
178 gtk_tree_model_get(model, iter, DIVE_TEMPERATURE, &value, -1);
183 switch (output_units.temperature) {
185 deg = mkelvin_to_C(value);
188 deg = mkelvin_to_F(value);
193 snprintf(buffer, sizeof(buffer), "%.1f", deg);
196 g_object_set(renderer, "text", buffer, NULL);
199 static void nitrox_data_func(GtkTreeViewColumn *col,
200 GtkCellRenderer *renderer,
208 gtk_tree_model_get(model, iter, DIVE_NITROX, &value, -1);
211 snprintf(buffer, sizeof(buffer), "%.1f", value/10.0);
213 strcpy(buffer, "air");
215 g_object_set(renderer, "text", buffer, NULL);
218 /* Render the SAC data (integer value of "ml / min") */
219 static void sac_data_func(GtkTreeViewColumn *col,
220 GtkCellRenderer *renderer,
230 gtk_tree_model_get(model, iter, DIVE_SAC, &value, -1);
233 g_object_set(renderer, "text", "", NULL);
237 sac = value / 1000.0;
238 switch (output_units.volume) {
244 sac = ml_to_cuft(sac * 1000);
247 snprintf(buffer, sizeof(buffer), fmt, sac);
249 g_object_set(renderer, "text", buffer, NULL);
252 /* Render the OTU data (integer value of "OTU") */
253 static void otu_data_func(GtkTreeViewColumn *col,
254 GtkCellRenderer *renderer,
262 gtk_tree_model_get(model, iter, DIVE_OTU, &value, -1);
265 g_object_set(renderer, "text", "", NULL);
269 snprintf(buffer, sizeof(buffer), "%d", value);
271 g_object_set(renderer, "text", buffer, NULL);
274 /* calculate OTU for a dive */
275 static int calculate_otu(struct dive *dive)
280 for (i = 1; i < dive->samples; i++) {
283 struct sample *sample = dive->sample + i;
284 struct sample *psample = sample - 1;
285 t = sample->time.seconds - psample->time.seconds;
286 po2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille / 1000.0 *
287 (sample->depth.mm + 10000) / 10000.0;
289 otu += pow(po2 - 0.5, 0.83) * t / 30.0;
294 * Return air usage (in liters).
296 static double calculate_airuse(struct dive *dive)
301 for (i = 0; i < MAX_CYLINDERS; i++) {
302 pressure_t start, end;
303 cylinder_t *cyl = dive->cylinder + i;
304 int size = cyl->type.size.mliter;
310 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
311 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
312 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
314 /* Liters of air at 1 atm == milliliters at 1k atm*/
315 airuse += kilo_atm * size;
320 static int calculate_sac(struct dive *dive)
322 double airuse, pressure, sac;
324 airuse = calculate_airuse(dive);
327 if (!dive->duration.seconds)
330 /* Mean pressure in atm: 1 atm per 10m */
331 pressure = 1 + (dive->meandepth.mm / 10000.0);
332 sac = airuse / pressure * 60 / dive->duration.seconds;
334 /* milliliters per minute.. */
338 static void get_string(char **str, const char *s)
354 static void get_location(struct dive *dive, char **str)
356 get_string(str, dive->location);
359 static void get_cylinder(struct dive *dive, char **str)
361 get_string(str, dive->cylinder[0].type.description);
364 static void fill_one_dive(struct dive *dive,
368 char *location, *cylinder;
370 get_cylinder(dive, &cylinder);
371 get_location(dive, &location);
374 * We only set the fields that changed: the strings.
375 * The core data itself is unaffected by units
377 gtk_list_store_set(GTK_LIST_STORE(model), iter,
378 DIVE_NR, dive->number,
379 DIVE_LOCATION, location,
380 DIVE_CYLINDER, cylinder,
386 static gboolean set_one_dive(GtkTreeModel *model,
391 GValue value = {0, };
394 /* Get the dive number */
395 gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
396 dive = get_dive(g_value_get_int(&value));
399 if (data && dive != data)
402 fill_one_dive(dive, model, iter);
406 void flush_divelist(struct dive *dive)
408 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
410 gtk_tree_model_foreach(model, set_one_dive, dive);
413 void set_divelist_font(const char *font)
415 PangoFontDescription *font_desc = pango_font_description_from_string(font);
416 gtk_widget_modify_font(dive_list.tree_view, font_desc);
417 pango_font_description_free(font_desc);
420 void update_dive_list_units(void)
423 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
425 (void) get_depth_units(0, NULL, &unit);
426 gtk_tree_view_column_set_title(dive_list.depth, unit);
428 (void) get_temp_units(0, &unit);
429 gtk_tree_view_column_set_title(dive_list.temperature, unit);
431 gtk_tree_model_foreach(model, set_one_dive, NULL);
434 void update_dive_list_col_visibility(void)
436 gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
437 gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
438 gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
439 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
440 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
444 static void fill_dive_list(void)
450 store = GTK_LIST_STORE(dive_list.model);
452 for (i = 0; i < dive_table.nr; i++) {
453 struct dive *dive = dive_table.dives[i];
455 dive->otu = calculate_otu(dive);
456 dive->sac = calculate_sac(dive);
457 gtk_list_store_append(store, &iter);
458 gtk_list_store_set(store, &iter,
460 DIVE_NR, dive->number,
461 DIVE_DATE, dive->when,
462 DIVE_DEPTH, dive->maxdepth,
463 DIVE_DURATION, dive->duration.seconds,
464 DIVE_LOCATION, "location",
465 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
466 DIVE_NITROX, dive->cylinder[0].gasmix.o2,
471 update_dive_list_units();
472 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
473 GtkTreeSelection *selection;
474 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
475 gtk_tree_selection_select_iter(selection, &iter);
479 void dive_list_update_dives(void)
481 gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
486 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, int index, const char *title,
487 data_func_t data_func, PangoAlignment align, gboolean visible)
489 return tree_view_column(dl->tree_view, index, title, data_func, align, visible);
493 * This is some crazy crap. The only way to get default focus seems
494 * to be to grab focus as the widget is being shown the first time.
496 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
498 gtk_widget_grab_focus(tree_view);
501 GtkWidget *dive_list_create(void)
503 GtkTreeSelection *selection;
505 dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
506 G_TYPE_INT, /* index */
508 G_TYPE_INT, /* Date */
509 G_TYPE_INT, /* Depth */
510 G_TYPE_INT, /* Duration */
511 G_TYPE_INT, /* Temperature */
512 G_TYPE_STRING, /* Cylinder */
513 G_TYPE_INT, /* Nitrox */
514 G_TYPE_INT, /* SAC */
515 G_TYPE_INT, /* OTU */
516 G_TYPE_STRING /* Location */
518 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
519 set_divelist_font(divelist_font);
521 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
523 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
524 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
526 dive_list.nr = divelist_column(&dive_list, DIVE_NR, "#", NULL, PANGO_ALIGN_RIGHT, TRUE);
527 gtk_tree_view_column_set_sort_column_id(dive_list.nr, -1);
528 dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE);
529 dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE);
530 dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE);
531 dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, visible_cols.temperature);
532 dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, visible_cols.cylinder);
533 dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, visible_cols.nitrox);
534 dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, visible_cols.sac);
535 dive_list.otu = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, visible_cols.otu);
536 dive_list.location = divelist_column(&dive_list, DIVE_LOCATION, "Location", NULL, PANGO_ALIGN_LEFT, TRUE);
540 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
541 "search-column", DIVE_LOCATION,
545 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
546 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
548 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
549 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
550 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
551 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
553 dive_list.changed = 0;
555 return dive_list.container_widget;
558 void mark_divelist_changed(int changed)
560 dive_list.changed = changed;
563 int unsaved_changes()
565 return dive_list.changed;