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,
226 const double liters_per_cuft = 28.317;
231 gtk_tree_model_get(model, iter, DIVE_SAC, &value, -1);
234 g_object_set(renderer, "text", "", NULL);
238 sac = value / 1000.0;
239 switch (output_units.volume) {
245 sac /= liters_per_cuft;
248 snprintf(buffer, sizeof(buffer), fmt, sac);
250 g_object_set(renderer, "text", buffer, NULL);
253 /* Render the OTU data (integer value of "OTU") */
254 static void otu_data_func(GtkTreeViewColumn *col,
255 GtkCellRenderer *renderer,
263 gtk_tree_model_get(model, iter, DIVE_OTU, &value, -1);
266 g_object_set(renderer, "text", "", NULL);
270 snprintf(buffer, sizeof(buffer), "%d", value);
272 g_object_set(renderer, "text", buffer, NULL);
275 /* calculate OTU for a dive */
276 static int calculate_otu(struct dive *dive)
281 for (i = 1; i < dive->samples; i++) {
284 struct sample *sample = dive->sample + i;
285 struct sample *psample = sample - 1;
286 t = sample->time.seconds - psample->time.seconds;
287 po2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille / 1000.0 *
288 (sample->depth.mm + 10000) / 10000.0;
290 otu += pow(po2 - 0.5, 0.83) * t / 30.0;
295 * Return air usage (in liters).
297 static double calculate_airuse(struct dive *dive)
302 for (i = 0; i < MAX_CYLINDERS; i++) {
303 cylinder_t *cyl = dive->cylinder + i;
304 int size = cyl->type.size.mliter;
310 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
312 /* Liters of air at 1 atm == milliliters at 1k atm*/
313 airuse += kilo_atm * size;
318 static int calculate_sac(struct dive *dive)
320 double airuse, pressure, sac;
322 airuse = calculate_airuse(dive);
325 if (!dive->duration.seconds)
328 /* Mean pressure in atm: 1 atm per 10m */
329 pressure = 1 + (dive->meandepth.mm / 10000.0);
330 sac = airuse / pressure * 60 / dive->duration.seconds;
332 /* milliliters per minute.. */
336 static void get_string(char **str, const char *s)
352 static void get_location(struct dive *dive, char **str)
354 get_string(str, dive->location);
357 static void get_cylinder(struct dive *dive, char **str)
359 get_string(str, dive->cylinder[0].type.description);
362 static void fill_one_dive(struct dive *dive,
366 char *location, *cylinder;
368 get_cylinder(dive, &cylinder);
369 get_location(dive, &location);
372 * We only set the fields that changed: the strings.
373 * The core data itself is unaffected by units
375 gtk_list_store_set(GTK_LIST_STORE(model), iter,
376 DIVE_NR, dive->number,
377 DIVE_LOCATION, location,
378 DIVE_CYLINDER, cylinder,
384 static gboolean set_one_dive(GtkTreeModel *model,
389 GValue value = {0, };
392 /* Get the dive number */
393 gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
394 dive = get_dive(g_value_get_int(&value));
397 if (data && dive != data)
400 fill_one_dive(dive, model, iter);
404 void flush_divelist(struct dive *dive)
406 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
408 gtk_tree_model_foreach(model, set_one_dive, dive);
411 void set_divelist_font(const char *font)
413 PangoFontDescription *font_desc = pango_font_description_from_string(font);
414 gtk_widget_modify_font(dive_list.tree_view, font_desc);
415 pango_font_description_free(font_desc);
418 void update_dive_list_units(void)
421 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
423 (void) get_depth_units(0, NULL, &unit);
424 gtk_tree_view_column_set_title(dive_list.depth, unit);
426 (void) get_temp_units(0, &unit);
427 gtk_tree_view_column_set_title(dive_list.temperature, unit);
429 gtk_tree_model_foreach(model, set_one_dive, NULL);
432 void update_dive_list_col_visibility(void)
434 gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
435 gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
436 gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
437 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
438 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
442 static void fill_dive_list(void)
448 store = GTK_LIST_STORE(dive_list.model);
450 for (i = 0; i < dive_table.nr; i++) {
451 struct dive *dive = dive_table.dives[i];
453 dive->otu = calculate_otu(dive);
454 dive->sac = calculate_sac(dive);
455 gtk_list_store_append(store, &iter);
456 gtk_list_store_set(store, &iter,
458 DIVE_NR, dive->number,
459 DIVE_DATE, dive->when,
460 DIVE_DEPTH, dive->maxdepth,
461 DIVE_DURATION, dive->duration.seconds,
462 DIVE_LOCATION, "location",
463 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
464 DIVE_NITROX, dive->cylinder[0].gasmix.o2,
469 update_dive_list_units();
470 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
471 GtkTreeSelection *selection;
472 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
473 gtk_tree_selection_select_iter(selection, &iter);
477 void dive_list_update_dives(void)
479 gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
484 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, int index, const char *title,
485 data_func_t data_func, PangoAlignment align, gboolean visible)
487 return tree_view_column(dl->tree_view, index, title, data_func, align, visible);
491 * This is some crazy crap. The only way to get default focus seems
492 * to be to grab focus as the widget is being shown the first time.
494 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
496 gtk_widget_grab_focus(tree_view);
499 GtkWidget *dive_list_create(void)
501 GtkTreeSelection *selection;
503 dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
504 G_TYPE_INT, /* index */
506 G_TYPE_INT, /* Date */
507 G_TYPE_INT, /* Depth */
508 G_TYPE_INT, /* Duration */
509 G_TYPE_INT, /* Temperature */
510 G_TYPE_STRING, /* Cylinder */
511 G_TYPE_INT, /* Nitrox */
512 G_TYPE_INT, /* SAC */
513 G_TYPE_INT, /* OTU */
514 G_TYPE_STRING /* Location */
516 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
517 set_divelist_font(divelist_font);
519 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
521 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
522 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
524 dive_list.nr = divelist_column(&dive_list, DIVE_NR, "#", NULL, PANGO_ALIGN_RIGHT, TRUE);
525 gtk_tree_view_column_set_sort_column_id(dive_list.nr, -1);
526 dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE);
527 dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE);
528 dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE);
529 dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, visible_cols.temperature);
530 dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, visible_cols.cylinder);
531 dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, visible_cols.nitrox);
532 dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, visible_cols.sac);
533 dive_list.otu = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, visible_cols.otu);
534 dive_list.location = divelist_column(&dive_list, DIVE_LOCATION, "Location", NULL, PANGO_ALIGN_LEFT, TRUE);
538 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
539 "search-column", DIVE_LOCATION,
543 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
544 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
546 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
547 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
548 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
549 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
551 dive_list.changed = 0;
553 return dive_list.container_widget;
556 void mark_divelist_changed(int changed)
558 dive_list.changed = changed;
561 int unsaved_changes()
563 return dive_list.changed;