]> git.tdb.fi Git - ext/subsurface.git/blob - divelist.c
Label the temperature graph
[ext/subsurface.git] / divelist.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5
6 #include "divelist.h"
7 #include "dive.h"
8 #include "display.h"
9
10 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
11 {
12         GtkTreeIter iter;
13         GValue value = {0, };
14
15         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
16                 return;
17
18         gtk_tree_model_get_value(model, &iter, 6, &value);
19         selected_dive = g_value_get_int(&value);
20         repaint_dive();
21 }
22
23 static gboolean set_one_dive(GtkTreeModel *model,
24                              GtkTreePath *path,
25                              GtkTreeIter *iter,
26                              gpointer data)
27 {
28         int len;
29         GValue value = {0, };
30         struct dive *dive;
31         char buffer[256], *datestr, *depth, *duration;
32         struct tm *tm;
33         int integer, frac;
34
35         /* Get the dive number */
36         gtk_tree_model_get_value(model, iter, 6, &value);
37         dive = get_dive(g_value_get_int(&value));
38         if (!dive)
39                 return TRUE;
40
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);
48
49         switch (output_units.length) {
50         unsigned int depth;
51         case METERS:
52                 depth = (dive->maxdepth.mm + 49) / 100;
53                 integer = depth / 10;
54                 frac = depth % 10;
55                 if (integer < 20)
56                         break;
57                 frac = -1;
58                 /* Rounding? */
59                 break;
60         case FEET:
61                 integer = to_feet(dive->maxdepth);
62                 frac = -1;
63         }
64         len = snprintf(buffer, sizeof(buffer),
65                        "%d", integer);
66         if (frac >= 0)
67                 len += snprintf(buffer+len, sizeof(buffer)-len,
68                         ".%d", frac);
69         depth = malloc(len + 1);
70         memcpy(depth, buffer, len+1);
71
72         len = snprintf(buffer, sizeof(buffer),
73                        "%d", dive->duration.seconds / 60);
74         duration = malloc(len + 1);
75         memcpy(duration, buffer, len+1);
76
77         gtk_list_store_set(GTK_LIST_STORE(model), iter,
78                 0, datestr,
79                 2, depth,
80                 4, duration,
81                 -1);
82
83         return FALSE;
84 }
85
86 void update_dive_list_units(struct DiveList *dive_list)
87 {
88         const char *unit;
89         GtkTreeModel *model = GTK_TREE_MODEL(dive_list->model);
90
91         switch (output_units.length) {
92         case METERS:
93                 unit = "m";
94                 break;
95         case FEET:
96                 unit = "ft";
97                 break;
98         }
99         gtk_tree_view_column_set_title(dive_list->depth, unit);
100
101         gtk_tree_model_foreach(model, set_one_dive, NULL);
102 }
103
104 static void fill_dive_list(struct DiveList *dive_list)
105 {
106         int i;
107         GtkTreeIter iter;
108         GtkListStore *store;
109
110         store = GTK_LIST_STORE(dive_list->model);
111
112         for (i = 0; i < dive_table.nr; i++) {
113                 struct dive *dive = dive_table.dives[i];
114
115                 gtk_list_store_append(store, &iter);
116                 gtk_list_store_set(store, &iter,
117                         0, "date",
118                         1, dive->when,
119                         2, "depth",
120                         3, dive->maxdepth,
121                         4, "duration",
122                         5, dive->duration.seconds,
123                         6, i,
124                         -1);
125         }
126
127         update_dive_list_units(dive_list);
128 }
129
130 void dive_list_update_dives(struct DiveList dive_list)
131 {
132         gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
133         fill_dive_list(&dive_list);
134         repaint_dive();
135 }
136
137 struct DiveList dive_list_create(void)
138 {
139         struct DiveList    dive_list;
140         GtkTreeSelection  *selection;
141         GtkCellRenderer   *renderer;
142         GtkTreeViewColumn *col;
143
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,
148                                    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));
151
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);
154
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);
163
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);
173
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);
183
184         fill_dive_list(&dive_list);
185
186         g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
187                                           "search-column", 0,
188                                           "rules-hint", TRUE,
189                                           NULL);
190
191         g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
192
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);
197
198         return dive_list;
199 }