11 * The dive list has the dive data in both string format (for showing)
12 * and in "raw" format (for sorting purposes)
16 DIVE_DATESTR, /* "Thu Jun 17th, 2011" or whatever */
17 DIVE_DATE, /* time_t: dive->when */
18 DIVE_DEPTHSTR, /* "67" in ft or whatever */
19 DIVE_DEPTH, /* int: dive->maxdepth in mm */
20 DIVE_DURATIONSTR, /* "47" in minutes */
21 DIVE_DURATION, /* int: in seconds */
22 DIVE_TEMPSTR, /* "78" in fahrenheit or whatever */
23 DIVE_TEMP, /* int: in mkelvin */
24 DIVE_NITROXSTR, /* "32.5" in percent */
25 DIVE_NITROX, /* int: in permille */
26 DIVE_SACSTR, /* "0.49" in cuft/min */
27 DIVE_SAC, /* int: in ml/min or something */
32 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
37 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
40 gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
41 selected_dive = g_value_get_int(&value);
45 static const char *weekday(int wday)
47 static const char wday_array[7][4] = {
48 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
50 return wday_array[wday];
53 static const char *monthname(int mon)
55 static const char month_array[12][4] = {
56 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
57 "Jul", "Aug", "Oct", "Sep", "Nov", "Dec",
59 return month_array[mon];
62 static void get_date(struct dive *dive, int *val, char **str)
65 time_t when = dive->when;
71 tm = gmtime(&dive->when);
72 snprintf(buffer, sizeof(buffer),
73 "%s, %s %d, %d %02d:%02d",
75 monthname(tm->tm_mon),
76 tm->tm_mday, tm->tm_year + 1900,
77 tm->tm_hour, tm->tm_min);
78 *str = strdup(buffer);
81 static void get_depth(struct dive *dive, int *val, char **str)
84 int depth = dive->maxdepth.mm;
85 int integer, frac = -1;
90 switch (output_units.length) {
92 /* To tenths of meters */
93 depth = (depth + 49) / 100;
102 integer = to_feet(dive->maxdepth);
108 len = snprintf(buffer, sizeof(buffer),
111 len += snprintf(buffer+len, sizeof(buffer)-len,
113 *str = strdup(buffer);
116 static void get_duration(struct dive *dive, int *val, char **str)
118 unsigned int sec = dive->duration.seconds;
122 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
123 *str = strdup(buffer);
126 static void get_temp(struct dive *dive, int *val, char **str)
128 int value = dive->watertemp.mkelvin;
135 switch (output_units.temperature) {
137 deg = mkelvin_to_C(value);
140 deg = mkelvin_to_F(value);
145 snprintf(buffer, sizeof(buffer), "%.1f", deg);
146 *str = strdup(buffer);
150 static void get_nitrox(struct dive *dive, int *val, char **str)
152 int value = dive->cylinder[0].gasmix.o2.permille;
158 snprintf(buffer, sizeof(buffer), "%.1f", value/10.0);
159 *str = strdup(buffer);
163 static void get_sac(struct dive *dive, int *val, char **str)
169 static gboolean set_one_dive(GtkTreeModel *model,
174 GValue value = {0, };
176 int date, depth, duration, temp, nitrox, sac;
177 char *datestr, *depthstr, *durationstr, *tempstr, *nitroxstr, *sacstr;
179 /* Get the dive number */
180 gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
181 dive = get_dive(g_value_get_int(&value));
185 get_date(dive, &date, &datestr);
186 get_depth(dive, &depth, &depthstr);
187 get_duration(dive, &duration, &durationstr);
188 get_temp(dive, &temp, &tempstr);
189 get_nitrox(dive, &nitrox, &nitroxstr);
190 get_sac(dive, &sac, &sacstr);
193 * We only set the fields that changed: the strings.
194 * The core data itself is unaffected by units
196 gtk_list_store_set(GTK_LIST_STORE(model), iter,
197 DIVE_DATESTR, datestr,
198 DIVE_DEPTHSTR, depthstr,
199 DIVE_DURATIONSTR, durationstr,
200 DIVE_TEMPSTR, tempstr,
202 DIVE_NITROXSTR, nitroxstr,
211 void update_dive_list_units(struct DiveList *dive_list)
214 GtkTreeModel *model = GTK_TREE_MODEL(dive_list->model);
216 switch (output_units.length) {
224 gtk_tree_view_column_set_title(dive_list->depth, unit);
226 gtk_tree_model_foreach(model, set_one_dive, NULL);
229 static void fill_dive_list(struct DiveList *dive_list)
235 store = GTK_LIST_STORE(dive_list->model);
237 for (i = 0; i < dive_table.nr; i++) {
238 struct dive *dive = dive_table.dives[i];
240 gtk_list_store_append(store, &iter);
241 gtk_list_store_set(store, &iter,
243 DIVE_DATESTR, "date",
244 DIVE_DATE, dive->when,
245 DIVE_DEPTHSTR, "depth",
246 DIVE_DEPTH, dive->maxdepth,
247 DIVE_DURATIONSTR, "duration",
248 DIVE_DURATION, dive->duration.seconds,
249 DIVE_TEMPSTR, "temp",
250 DIVE_TEMP, dive->watertemp.mkelvin,
251 DIVE_NITROXSTR, "21.0",
252 DIVE_NITROX, dive->cylinder[0].gasmix.o2,
258 update_dive_list_units(dive_list);
261 void dive_list_update_dives(struct DiveList dive_list)
263 gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
264 fill_dive_list(&dive_list);
268 struct DiveList dive_list_create(void)
270 struct DiveList dive_list;
271 GtkTreeSelection *selection;
272 GtkCellRenderer *renderer;
273 GtkTreeViewColumn *col;
275 dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
276 G_TYPE_INT, /* index */
277 G_TYPE_STRING, G_TYPE_INT, /* Date */
278 G_TYPE_STRING, G_TYPE_INT, /* Depth */
279 G_TYPE_STRING, G_TYPE_INT, /* Duration */
280 G_TYPE_STRING, G_TYPE_INT, /* Temperature */
281 G_TYPE_STRING, G_TYPE_INT, /* Nitrox */
282 G_TYPE_STRING, G_TYPE_INT /* SAC */
284 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
285 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
287 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
288 gtk_widget_set_size_request(dive_list.tree_view, 200, 100);
290 renderer = gtk_cell_renderer_text_new();
291 dive_list.date = col = gtk_tree_view_column_new();
292 gtk_tree_view_column_set_title(col, "Date");
293 gtk_tree_view_column_set_sort_column_id(col, DIVE_DATE);
294 gtk_tree_view_column_set_resizable (col, TRUE);
295 gtk_tree_view_column_pack_start(col, renderer, TRUE);
296 gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DATESTR);
297 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
299 renderer = gtk_cell_renderer_text_new();
300 dive_list.depth = col = gtk_tree_view_column_new();
301 gtk_tree_view_column_set_title(col, "ft");
302 gtk_tree_view_column_set_sort_column_id(col, DIVE_DEPTH);
303 gtk_tree_view_column_pack_start(col, renderer, FALSE);
304 gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DEPTHSTR);
305 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
306 gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
307 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
309 renderer = gtk_cell_renderer_text_new();
310 dive_list.duration = col = gtk_tree_view_column_new();
311 gtk_tree_view_column_set_title(col, "min");
312 gtk_tree_view_column_set_sort_column_id(col, DIVE_DURATION);
313 gtk_tree_view_column_pack_start(col, renderer, FALSE);
314 gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_DURATIONSTR);
315 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
316 gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
317 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
319 renderer = gtk_cell_renderer_text_new();
320 dive_list.temperature = col = gtk_tree_view_column_new();
321 gtk_tree_view_column_set_title(col, "deg");
322 gtk_tree_view_column_set_sort_column_id(col, DIVE_TEMP);
323 gtk_tree_view_column_pack_start(col, renderer, FALSE);
324 gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_TEMPSTR);
325 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
326 gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
327 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
329 renderer = gtk_cell_renderer_text_new();
330 dive_list.nitrox = col = gtk_tree_view_column_new();
331 gtk_tree_view_column_set_title(col, "EAD");
332 gtk_tree_view_column_set_sort_column_id(col, DIVE_NITROX);
333 gtk_tree_view_column_pack_start(col, renderer, FALSE);
334 gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_NITROXSTR);
335 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
336 gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
337 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
339 renderer = gtk_cell_renderer_text_new();
340 dive_list.sac = col = gtk_tree_view_column_new();
341 gtk_tree_view_column_set_title(col, "SAC");
342 gtk_tree_view_column_set_sort_column_id(col, DIVE_SAC);
343 gtk_tree_view_column_pack_start(col, renderer, FALSE);
344 gtk_tree_view_column_add_attribute(col, renderer, "text", DIVE_SACSTR);
345 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
346 gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
347 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
349 fill_dive_list(&dive_list);
351 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
356 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
358 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
359 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
360 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
361 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);