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 GList *selected_dives;
55 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
61 int nr_selected = gtk_tree_selection_count_selected_rows(selection);
64 g_list_foreach (selected_dives, (GFunc) gtk_tree_path_free, NULL);
65 g_list_free (selected_dives);
67 selected_dives = gtk_tree_selection_get_selected_rows(selection, NULL);
69 switch (nr_selected) {
70 case 0: /* keep showing the last selected dive */
73 /* just pick that dive as selected */
74 path = g_list_nth_data(selected_dives, 0);
75 if (gtk_tree_model_get_iter(model, &iter, path)) {
76 gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
77 selected_dive = g_value_get_int(&value);
81 default: /* multiple selections - what now? At this point I
82 * don't want to change the selected dive unless
83 * there is exactly one dive selected; not sure this
84 * is the most intuitive solution.
85 * I do however want to keep around which dives have
91 static void date_data_func(GtkTreeViewColumn *col,
92 GtkCellRenderer *renderer,
102 gtk_tree_model_get(model, iter, DIVE_DATE, &val, -1);
108 snprintf(buffer, sizeof(buffer),
109 "%s, %s %d, %d %02d:%02d",
110 weekday(tm->tm_wday),
111 monthname(tm->tm_mon),
112 tm->tm_mday, tm->tm_year + 1900,
113 tm->tm_hour, tm->tm_min);
114 g_object_set(renderer, "text", buffer, NULL);
117 static void depth_data_func(GtkTreeViewColumn *col,
118 GtkCellRenderer *renderer,
123 int depth, integer, frac, len;
126 gtk_tree_model_get(model, iter, DIVE_DEPTH, &depth, -1);
128 switch (output_units.length) {
130 /* To tenths of meters */
131 depth = (depth + 49) / 100;
132 integer = depth / 10;
140 integer = mm_to_feet(depth) + 0.5;
146 len = snprintf(buffer, sizeof(buffer), "%d", integer);
148 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
150 g_object_set(renderer, "text", buffer, NULL);
153 static void duration_data_func(GtkTreeViewColumn *col,
154 GtkCellRenderer *renderer,
162 gtk_tree_model_get(model, iter, DIVE_DURATION, &sec, -1);
163 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
165 g_object_set(renderer, "text", buffer, NULL);
168 static void temperature_data_func(GtkTreeViewColumn *col,
169 GtkCellRenderer *renderer,
177 gtk_tree_model_get(model, iter, DIVE_TEMPERATURE, &value, -1);
182 switch (output_units.temperature) {
184 deg = mkelvin_to_C(value);
187 deg = mkelvin_to_F(value);
192 snprintf(buffer, sizeof(buffer), "%.1f", deg);
195 g_object_set(renderer, "text", buffer, NULL);
198 static void nitrox_data_func(GtkTreeViewColumn *col,
199 GtkCellRenderer *renderer,
207 gtk_tree_model_get(model, iter, DIVE_NITROX, &value, -1);
210 snprintf(buffer, sizeof(buffer), "%.1f", value/10.0);
212 strcpy(buffer, "air");
214 g_object_set(renderer, "text", buffer, NULL);
217 /* Render the SAC data (integer value of "ml / min") */
218 static void sac_data_func(GtkTreeViewColumn *col,
219 GtkCellRenderer *renderer,
225 const double liters_per_cuft = 28.317;
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 /= liters_per_cuft;
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 cylinder_t *cyl = dive->cylinder + i;
303 int size = cyl->type.size.mliter;
309 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
311 /* Liters of air at 1 atm == milliliters at 1k atm*/
312 airuse += kilo_atm * size;
317 static void get_sac(struct dive *dive, int *val)
319 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,
367 char *location, *cylinder;
369 get_cylinder(dive, &cylinder);
370 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_LOCATION, location,
379 DIVE_CYLINDER, cylinder,
385 static gboolean set_one_dive(GtkTreeModel *model,
390 GValue value = {0, };
393 /* Get the dive number */
394 gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
395 dive = get_dive(g_value_get_int(&value));
398 if (data && dive != data)
401 fill_one_dive(dive, model, iter);
405 void flush_divelist(struct dive *dive)
407 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
409 gtk_tree_model_foreach(model, set_one_dive, dive);
412 void set_divelist_font(const char *font)
414 PangoFontDescription *font_desc = pango_font_description_from_string(font);
415 gtk_widget_modify_font(dive_list.tree_view, font_desc);
416 pango_font_description_free(font_desc);
419 void update_dive_list_units(void)
422 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
424 switch (output_units.length) {
432 gtk_tree_view_column_set_title(dive_list.depth, unit);
434 switch (output_units.temperature) {
436 unit = UTF8_DEGREE "C";
439 unit = UTF8_DEGREE "F";
445 gtk_tree_view_column_set_title(dive_list.temperature, unit);
447 gtk_tree_model_foreach(model, set_one_dive, NULL);
450 void update_dive_list_col_visibility(void)
452 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
453 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
457 static void fill_dive_list(void)
463 store = GTK_LIST_STORE(dive_list.model);
465 for (i = 0; i < dive_table.nr; i++) {
466 struct dive *dive = dive_table.dives[i];
468 dive->otu = calculate_otu(dive);
469 gtk_list_store_append(store, &iter);
470 gtk_list_store_set(store, &iter,
472 DIVE_DATE, dive->when,
473 DIVE_DEPTH, dive->maxdepth,
474 DIVE_DURATION, dive->duration.seconds,
475 DIVE_LOCATION, "location",
476 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
477 DIVE_NITROX, dive->cylinder[0].gasmix.o2,
482 update_dive_list_units();
483 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
484 GtkTreeSelection *selection;
485 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
486 gtk_tree_selection_select_iter(selection, &iter);
490 void dive_list_update_dives(void)
492 gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
497 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, int index, const char *title,
498 data_func_t data_func, PangoAlignment align, gboolean visible)
500 return tree_view_column(dl->tree_view, index, title, data_func, align, visible);
504 * This is some crazy crap. The only way to get default focus seems
505 * to be to grab focus as the widget is being shown the first time.
507 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
509 gtk_widget_grab_focus(tree_view);
512 GtkWidget *dive_list_create(void)
514 GtkTreeSelection *selection;
516 dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
517 G_TYPE_INT, /* index */
518 G_TYPE_INT, /* Date */
519 G_TYPE_INT, /* Depth */
520 G_TYPE_INT, /* Duration */
521 G_TYPE_INT, /* Temperature */
522 G_TYPE_STRING, /* Cylinder */
523 G_TYPE_INT, /* Nitrox */
524 G_TYPE_INT, /* SAC */
525 G_TYPE_INT, /* OTU */
526 G_TYPE_STRING /* Location */
528 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
529 set_divelist_font(divelist_font);
531 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
533 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
534 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
536 dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE);
537 dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE);
538 dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE);
539 dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, TRUE);
540 dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, TRUE);
541 dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, TRUE);
542 dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, visible_cols.sac);
543 dive_list.otu = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, visible_cols.otu);
544 dive_list.location = divelist_column(&dive_list, DIVE_LOCATION, "Location", NULL, PANGO_ALIGN_LEFT, TRUE);
548 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
549 "search-column", DIVE_LOCATION,
553 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
554 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
556 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
557 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
558 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
559 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
561 dive_list.changed = 0;
563 return dive_list.container_widget;
566 void mark_divelist_changed(int changed)
568 dive_list.changed = changed;
571 int unsaved_changes()
573 return dive_list.changed;