Use renderer function for divelist depth field too

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Linus Torvalds 2011-09-19 18:52:23 -07:00
parent fa2a6fb763
commit 2a45f6dbc3
2 changed files with 24 additions and 22 deletions

7
dive.h
View file

@ -86,9 +86,14 @@ typedef struct {
pressure_t start, end; pressure_t start, end;
} cylinder_t; } cylinder_t;
static inline double mm_to_feet(int mm)
{
return mm * 0.00328084;
}
static inline int to_feet(depth_t depth) static inline int to_feet(depth_t depth)
{ {
return depth.mm * 0.00328084 + 0.5; return mm_to_feet(depth.mm) + 0.5;
} }
static double mkelvin_to_C(int mkelvin) static double mkelvin_to_C(int mkelvin)

View file

@ -14,7 +14,6 @@
enum { enum {
DIVE_INDEX = 0, DIVE_INDEX = 0,
DIVE_DATE, /* time_t: dive->when */ DIVE_DATE, /* time_t: dive->when */
DIVE_DEPTHSTR, /* "67" in ft or whatever */
DIVE_DEPTH, /* int: dive->maxdepth in mm */ DIVE_DEPTH, /* int: dive->maxdepth in mm */
DIVE_DURATIONSTR, /* "47" in minutes */ DIVE_DURATIONSTR, /* "47" in minutes */
DIVE_DURATION, /* int: in seconds */ DIVE_DURATION, /* int: in seconds */
@ -85,15 +84,17 @@ static void date_data_func(GtkTreeViewColumn *col,
g_object_set(renderer, "text", buffer, NULL); g_object_set(renderer, "text", buffer, NULL);
} }
static void get_depth(struct dive *dive, int *val, char **str) static void depth_data_func(GtkTreeViewColumn *col,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{ {
int len; int depth, integer, frac, len;
int depth = dive->maxdepth.mm; char buffer[40];
int integer, frac = -1;
char buffer[10]; gtk_tree_model_get(model, iter, DIVE_DEPTH, &depth, -1);
*val = depth;
*str = "";
switch (output_units.length) { switch (output_units.length) {
case METERS: case METERS:
/* To tenths of meters */ /* To tenths of meters */
@ -106,18 +107,17 @@ static void get_depth(struct dive *dive, int *val, char **str)
/* Rounding? */ /* Rounding? */
break; break;
case FEET: case FEET:
integer = to_feet(dive->maxdepth); integer = mm_to_feet(depth) + 0.5;
frac = -1; frac = -1;
break; break;
default: default:
return; return;
} }
len = snprintf(buffer, sizeof(buffer), len = snprintf(buffer, sizeof(buffer), "%d", integer);
"%d", integer);
if (frac >= 0) if (frac >= 0)
len += snprintf(buffer+len, sizeof(buffer)-len, len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
".%d", frac);
*str = strdup(buffer); g_object_set(renderer, "text", buffer, NULL);
} }
static void get_duration(struct dive *dive, int *val, char **str) static void get_duration(struct dive *dive, int *val, char **str)
@ -242,11 +242,10 @@ static void fill_one_dive(struct dive *dive,
GtkTreeModel *model, GtkTreeModel *model,
GtkTreeIter *iter) GtkTreeIter *iter)
{ {
int depth, duration, temp, nitrox, sac; int duration, temp, nitrox, sac;
char *depthstr, *durationstr, *tempstr, *nitroxstr, *sacstr; char *durationstr, *tempstr, *nitroxstr, *sacstr;
char *location; char *location;
get_depth(dive, &depth, &depthstr);
get_duration(dive, &duration, &durationstr); get_duration(dive, &duration, &durationstr);
get_location(dive, &location); get_location(dive, &location);
get_temp(dive, &temp, &tempstr); get_temp(dive, &temp, &tempstr);
@ -258,7 +257,6 @@ static void fill_one_dive(struct dive *dive,
* The core data itself is unaffected by units * The core data itself is unaffected by units
*/ */
gtk_list_store_set(GTK_LIST_STORE(model), iter, gtk_list_store_set(GTK_LIST_STORE(model), iter,
DIVE_DEPTHSTR, depthstr,
DIVE_DURATIONSTR, durationstr, DIVE_DURATIONSTR, durationstr,
DIVE_LOCATION, location, DIVE_LOCATION, location,
DIVE_TEMPSTR, tempstr, DIVE_TEMPSTR, tempstr,
@ -330,7 +328,6 @@ static void fill_dive_list(struct DiveList *dive_list)
gtk_list_store_set(store, &iter, gtk_list_store_set(store, &iter,
DIVE_INDEX, i, DIVE_INDEX, i,
DIVE_DATE, dive->when, DIVE_DATE, dive->when,
DIVE_DEPTHSTR, "depth",
DIVE_DEPTH, dive->maxdepth, DIVE_DEPTH, dive->maxdepth,
DIVE_DURATIONSTR, "duration", DIVE_DURATIONSTR, "duration",
DIVE_DURATION, dive->duration.seconds, DIVE_DURATION, dive->duration.seconds,
@ -364,7 +361,7 @@ struct DiveList dive_list_create(void)
dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS, dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
G_TYPE_INT, /* index */ G_TYPE_INT, /* index */
G_TYPE_INT, /* Date */ G_TYPE_INT, /* Date */
G_TYPE_STRING, G_TYPE_INT, /* Depth */ G_TYPE_INT, /* Depth */
G_TYPE_STRING, G_TYPE_INT, /* Duration */ G_TYPE_STRING, G_TYPE_INT, /* Duration */
G_TYPE_STRING, /* Location */ G_TYPE_STRING, /* Location */
G_TYPE_STRING, G_TYPE_INT, /* Temperature */ G_TYPE_STRING, G_TYPE_INT, /* Temperature */
@ -391,7 +388,7 @@ struct DiveList dive_list_create(void)
gtk_tree_view_column_set_title(col, "ft"); gtk_tree_view_column_set_title(col, "ft");
gtk_tree_view_column_set_sort_column_id(col, DIVE_DEPTH); gtk_tree_view_column_set_sort_column_id(col, DIVE_DEPTH);
gtk_tree_view_column_pack_start(col, renderer, FALSE); gtk_tree_view_column_pack_start(col, renderer, FALSE);
gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DEPTHSTR); gtk_tree_view_column_set_cell_data_func(col, renderer, depth_data_func, NULL, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col); gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL); gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5); gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);