10 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
15 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
18 gtk_tree_model_get_value(model, &iter, 6, &value);
19 selected_dive = g_value_get_int(&value);
23 static gboolean set_one_dive(GtkTreeModel *model,
31 char buffer[256], *datestr, *depth, *duration;
35 /* Get the dive number */
36 gtk_tree_model_get_value(model, iter, 6, &value);
37 dive = get_dive(g_value_get_int(&value));
41 tm = gmtime(&dive->when);
42 len = snprintf(buffer, sizeof(buffer),
43 "%02d.%02d.%02d %02d:%02d",
44 tm->tm_mday, tm->tm_mon+1, tm->tm_year % 100,
45 tm->tm_hour, tm->tm_min);
46 datestr = malloc(len+1);
47 memcpy(datestr, buffer, len+1);
49 switch (output_units.length) {
52 depth = (dive->maxdepth.mm + 49) / 100;
61 integer = to_feet(dive->maxdepth);
64 len = snprintf(buffer, sizeof(buffer),
67 len += snprintf(buffer+len, sizeof(buffer)-len,
69 depth = malloc(len + 1);
70 memcpy(depth, buffer, len+1);
72 len = snprintf(buffer, sizeof(buffer),
73 "%d", dive->duration.seconds / 60);
74 duration = malloc(len + 1);
75 memcpy(duration, buffer, len+1);
77 gtk_list_store_set(GTK_LIST_STORE(model), iter,
86 void update_dive_list_units(struct DiveList *dive_list)
89 GtkTreeModel *model = GTK_TREE_MODEL(dive_list->model);
91 switch (output_units.length) {
99 gtk_tree_view_column_set_title(dive_list->depth, unit);
101 gtk_tree_model_foreach(model, set_one_dive, NULL);
104 static void fill_dive_list(struct DiveList *dive_list)
110 store = GTK_LIST_STORE(dive_list->model);
112 for (i = 0; i < dive_table.nr; i++) {
113 struct dive *dive = dive_table.dives[i];
115 gtk_list_store_append(store, &iter);
116 gtk_list_store_set(store, &iter,
122 5, dive->duration.seconds,
127 update_dive_list_units(dive_list);
130 void dive_list_update_dives(struct DiveList dive_list)
132 gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
133 fill_dive_list(&dive_list);
137 struct DiveList dive_list_create(void)
139 struct DiveList dive_list;
140 GtkTreeSelection *selection;
141 GtkCellRenderer *renderer;
142 GtkTreeViewColumn *col;
144 dive_list.model = gtk_list_store_new(7,
145 G_TYPE_STRING, G_TYPE_INT,
146 G_TYPE_STRING, G_TYPE_INT,
147 G_TYPE_STRING, G_TYPE_INT,
149 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
150 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
152 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
153 gtk_widget_set_size_request(dive_list.tree_view, 200, 100);
155 renderer = gtk_cell_renderer_text_new();
156 dive_list.date = col = gtk_tree_view_column_new();
157 gtk_tree_view_column_set_title(col, "Date");
158 gtk_tree_view_column_set_sort_column_id(col, 1);
159 gtk_tree_view_column_set_resizable (col, TRUE);
160 gtk_tree_view_column_pack_start(col, renderer, TRUE);
161 gtk_tree_view_column_add_attribute(col, renderer, "text", 0);
162 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
164 renderer = gtk_cell_renderer_text_new();
165 dive_list.depth = col = gtk_tree_view_column_new();
166 gtk_tree_view_column_set_title(col, "ft");
167 gtk_tree_view_column_set_sort_column_id(col, 3);
168 gtk_tree_view_column_pack_start(col, renderer, FALSE);
169 gtk_tree_view_column_add_attribute(col, renderer, "text", 2);
170 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
171 gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
172 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
174 renderer = gtk_cell_renderer_text_new();
175 dive_list.duration = col = gtk_tree_view_column_new();
176 gtk_tree_view_column_set_title(col, "min");
177 gtk_tree_view_column_set_sort_column_id(col, 5);
178 gtk_tree_view_column_pack_start(col, renderer, FALSE);
179 gtk_tree_view_column_add_attribute(col, renderer, "text", 4);
180 gtk_tree_view_append_column(GTK_TREE_VIEW(dive_list.tree_view), col);
181 gtk_object_set(GTK_OBJECT(renderer), "alignment", PANGO_ALIGN_RIGHT, NULL);
182 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 1.0, 0.5);
184 fill_dive_list(&dive_list);
186 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
191 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
193 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
194 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
195 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
196 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);