diff options
-rw-r--r-- | divelist-gtk.c | 61 | ||||
-rw-r--r-- | divelist.c | 69 | ||||
-rw-r--r-- | divelist.h | 2 |
3 files changed, 77 insertions, 55 deletions
diff --git a/divelist-gtk.c b/divelist-gtk.c index 8587e11c6..dc441881c 100644 --- a/divelist-gtk.c +++ b/divelist-gtk.c @@ -295,7 +295,7 @@ static void depth_data_func(GtkTreeViewColumn *col, GtkTreeIter *iter, gpointer data) { - int depth, integer, frac, len, idx; + int depth, integer, frac, len, idx, show_decimal; char buffer[40]; gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DEPTH, &depth, -1); @@ -303,27 +303,9 @@ static void depth_data_func(GtkTreeViewColumn *col, if (idx < 0) { *buffer = '\0'; } else { - switch (prefs.units.length) { - case METERS: - /* To tenths of meters */ - depth = (depth + 49) / 100; - integer = depth / 10; - frac = depth % 10; - if (integer < 20) - break; - if (frac >= 5) - integer++; - frac = -1; - break; - case FEET: - integer = mm_to_feet(depth) + 0.5; - frac = -1; - break; - default: - return; - } + get_depth_values(depth, &integer, &frac, &show_decimal); len = snprintf(buffer, sizeof(buffer), "%d", integer); - if (frac >= 0) + if (show_decimal) len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac); } g_object_set(renderer, "text", buffer, NULL); @@ -826,49 +808,18 @@ void dive_list_update_dives(void) repaint_dive(); } -static gint dive_nr_sort(GtkTreeModel *model, +static gint gtk_dive_nr_sort(GtkTreeModel *model, GtkTreeIter *iter_a, GtkTreeIter *iter_b, gpointer user_data) { int idx_a, idx_b; timestamp_t when_a, when_b; - struct dive *a, *b; - dive_trip_t *tripa = NULL, *tripb = NULL; gtk_tree_model_get(model, iter_a, DIVE_INDEX, &idx_a, DIVE_DATE, &when_a, -1); gtk_tree_model_get(model, iter_b, DIVE_INDEX, &idx_b, DIVE_DATE, &when_b, -1); - if (idx_a < 0) { - a = NULL; - tripa = find_trip_by_idx(idx_a); - } else { - a = get_dive(idx_a); - if (a) - tripa = a->divetrip; - } - - if (idx_b < 0) { - b = NULL; - tripb = find_trip_by_idx(idx_b); - } else { - b = get_dive(idx_b); - if (b) - tripb = b->divetrip; - } - - /* - * Compare dive dates within the same trip (or when there - * are no trips involved at all). But if we have two - * different trips use the trip dates for comparison - */ - if (tripa != tripb) { - if (tripa) - when_a = tripa->when; - if (tripb) - when_b = tripb->when; - } - return when_a - when_b; + return dive_nr_sort(idx_a, idx_b, when_a, when_b); } @@ -879,7 +830,7 @@ static struct divelist_column { unsigned int flags; int *visible; } dl_column[] = { - [DIVE_NR] = { "#", nr_data_func, dive_nr_sort, ALIGN_RIGHT }, + [DIVE_NR] = { "#", nr_data_func, gtk_dive_nr_sort, ALIGN_RIGHT }, [DIVE_DATE] = { N_("Date"), date_data_func, NULL, ALIGN_LEFT }, [DIVE_RATING] = { UTF8_BLACKSTAR, star_data_func, NULL, ALIGN_LEFT }, [DIVE_DEPTH] = { N_("ft"), depth_data_func, NULL, ALIGN_RIGHT }, diff --git a/divelist.c b/divelist.c index f574a31fd..61b9116b2 100644 --- a/divelist.c +++ b/divelist.c @@ -88,6 +88,43 @@ dive_trip_t *find_trip_by_idx(int idx) return NULL; } +int dive_nr_sort(int idx_a, int idx_b, timestamp_t when_a, timestamp_t when_b) +{ + struct dive *a, *b; + dive_trip_t *tripa = NULL, *tripb = NULL; + + if (idx_a < 0) { + a = NULL; + tripa = find_trip_by_idx(idx_a); + } else { + a = get_dive(idx_a); + if (a) + tripa = a->divetrip; + } + + if (idx_b < 0) { + b = NULL; + tripb = find_trip_by_idx(idx_b); + } else { + b = get_dive(idx_b); + if (b) + tripb = b->divetrip; + } + + /* + * Compare dive dates within the same trip (or when there + * are no trips involved at all). But if we have two + * different trips use the trip dates for comparison + */ + if (tripa != tripb) { + if (tripa) + when_a = tripa->when; + if (tripb) + when_b = tripb->when; + } + return when_a - when_b; +} + int trip_has_selected_dives(dive_trip_t *trip) { struct dive *dive; @@ -98,6 +135,38 @@ int trip_has_selected_dives(dive_trip_t *trip) return 0; } +/* Get the values as we want to show them. Whole feet. But meters with one decimal for + * values less than 20m, without decimals for larger values */ +void get_depth_values(int depth, int *depth_int, int *depth_decimal, int *show_decimal) +{ + int integer, frac; + + *show_decimal = 1; + switch (prefs.units.length) { + case METERS: + /* To tenths of meters */ + depth = (depth + 49) / 100; + integer = depth / 10; + frac = depth % 10; + if (integer < 20) + break; + if (frac >= 5) + integer++; + *show_decimal = 0; + break; + case FEET: + integer = mm_to_feet(depth) + 0.5; + *show_decimal = 0; + break; + default: + /* can't happen */ + return; + } + *depth_int = integer; + if (*show_decimal) + *depth_decimal = frac; +} + /* * Get "maximal" dive gas for a dive. * Rules: diff --git a/divelist.h b/divelist.h index 8c21e9b3d..8fcd600a1 100644 --- a/divelist.h +++ b/divelist.h @@ -19,7 +19,9 @@ extern void export_all_dives_uddf_cb(); /* divelist core logic functions */ extern dive_trip_t *find_trip_by_idx(int idx); +extern int dive_nr_sort(int idx_a, int idx_b, timestamp_t when_a, timestamp_t when_b); extern int trip_has_selected_dives(dive_trip_t *trip); +extern void get_depth_values(int depth, int *depth_int, int *depth_decimal, int *show_decimal); extern void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p); extern int get_divenr(struct dive *dive); extern void get_location(struct dive *dive, char **str); |