]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Show tank / nitrox / air consumption information in the info_frame
[ext/subsurface.git] / info.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 #include "dive.h"
7 #include "display.h"
8 #include "divelist.h"
9
10 static GtkWidget *info_frame;
11 static GtkWidget *depth, *duration, *temperature, *airconsumption;
12 static GtkEntry *location, *buddy, *divemaster;
13 static GtkTextBuffer *notes;
14 static int location_changed = 1, notes_changed = 1;
15 static int divemaster_changed = 1, buddy_changed = 1;
16
17 static const char *weekday(int wday)
18 {
19         static const char wday_array[7][4] = {
20                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
21         };
22         return wday_array[wday];
23 }
24
25 static char *get_text(GtkTextBuffer *buffer)
26 {
27         GtkTextIter start;
28         GtkTextIter end;
29
30         gtk_text_buffer_get_start_iter(buffer, &start);
31         gtk_text_buffer_get_end_iter(buffer, &end);
32         return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
33 }
34
35 void flush_dive_info_changes(struct dive *dive)
36 {
37         if (!dive)
38                 return;
39
40         if (location_changed) {
41                 g_free(dive->location);
42                 dive->location = gtk_editable_get_chars(GTK_EDITABLE(location), 0, -1);
43         }
44
45         if (divemaster_changed) {
46                 g_free(dive->divemaster);
47                 dive->divemaster = gtk_editable_get_chars(GTK_EDITABLE(divemaster), 0, -1);
48         }
49
50         if (buddy_changed) {
51                 g_free(dive->buddy);
52                 dive->buddy = gtk_editable_get_chars(GTK_EDITABLE(buddy), 0, -1);
53         }
54
55         if (notes_changed) {
56                 g_free(dive->notes);
57                 dive->notes = get_text(notes);
58         }
59 }
60
61 void show_dive_info(struct dive *dive)
62 {
63         struct tm *tm;
64         char buffer[80];
65         char *text;
66
67         if (!dive) {
68                 gtk_label_set_text(GTK_LABEL(depth), "");
69                 gtk_label_set_text(GTK_LABEL(duration), "");
70                 gtk_label_set_text(GTK_LABEL(airconsumption), "");
71                 return;
72         }
73         /* dive number and location (or lacking that, the date) go in the window title */
74         tm = gmtime(&dive->when);
75         text = dive->location;
76         if (!text)
77                 text = "";
78         if (*text) {
79                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
80         } else {
81                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
82                         dive->number,
83                         weekday(tm->tm_wday),
84                         tm->tm_mon+1, tm->tm_mday,
85                         tm->tm_year+1900,
86                         tm->tm_hour, tm->tm_min);
87         }
88         text = buffer;
89         if (!dive->number)
90                 text += 10;     /* Skip the "Dive #0 - " part */
91         gtk_window_set_title(GTK_WINDOW(main_window), text);
92
93         /* the date goes in the frame label */
94         snprintf(buffer, sizeof(buffer), "%s %02d/%02d/%04d at %d:%02d",
95                 weekday(tm->tm_wday),
96                 tm->tm_mon+1, tm->tm_mday,
97                 tm->tm_year+1900,
98                 tm->tm_hour, tm->tm_min);
99         gtk_frame_set_label(GTK_FRAME(info_frame), buffer);
100
101
102         switch (output_units.length) {
103         case METERS:
104                 snprintf(buffer, sizeof(buffer),
105                         "%.1f m",
106                         dive->maxdepth.mm / 1000.0);
107                 break;
108         case FEET:
109                 snprintf(buffer, sizeof(buffer),
110                         "%d ft",
111                         to_feet(dive->maxdepth));
112                 break;
113         }
114         gtk_label_set_text(GTK_LABEL(depth), buffer);
115
116         snprintf(buffer, sizeof(buffer),
117                 "%d min",
118                 dive->duration.seconds / 60);
119         gtk_label_set_text(GTK_LABEL(duration), buffer);
120
121         *buffer = 0;
122         if (dive->watertemp.mkelvin) {
123                 switch (output_units.temperature) {
124                 case CELSIUS:
125                         snprintf(buffer, sizeof(buffer),
126                                 "%d C",
127                                 to_C(dive->watertemp));
128                         break;
129                 case FAHRENHEIT:
130                         snprintf(buffer, sizeof(buffer),
131                                 "%d F",
132                                 to_F(dive->watertemp));
133                         break;
134                 case KELVIN:
135                         snprintf(buffer, sizeof(buffer),
136                                 "%d K",
137                                 to_K(dive->watertemp));
138                         break;
139                 }
140         }
141         gtk_label_set_text(GTK_LABEL(temperature), buffer);
142
143         text = dive->location ? : "";
144         gtk_entry_set_text(location, text);
145
146         text = dive->divemaster ? : "";
147         gtk_entry_set_text(divemaster, text);
148
149         text = dive->buddy ? : "";
150         gtk_entry_set_text(buddy, text);
151
152         text = dive->notes ? : "";
153         gtk_text_buffer_set_text(notes, text, -1);
154 }
155
156 static GtkWidget *info_label(GtkWidget *box, const char *str, GtkJustification jtype)
157 {
158         GtkWidget *label = gtk_label_new(str);
159         gtk_label_set_justify(GTK_LABEL(label), jtype);
160         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
161         return label;
162 }
163
164 GtkWidget *dive_info_frame(void)
165 {
166         GtkWidget *frame;
167         GtkWidget *hbox;
168         GtkWidget *vbox;
169
170         frame = gtk_frame_new("Dive info");
171         info_frame = frame;
172         gtk_widget_show(frame);
173
174         vbox = gtk_vbox_new(FALSE, 6);
175         gtk_container_set_border_width(GTK_CONTAINER(vbox), 3);
176         gtk_container_add(GTK_CONTAINER(frame), vbox);
177
178         hbox = gtk_hbox_new(FALSE, 16);
179         gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
180         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
181
182         depth = info_label(hbox, "depth", GTK_JUSTIFY_RIGHT);
183         duration = info_label(hbox, "duration", GTK_JUSTIFY_RIGHT);
184         temperature = info_label(hbox, "temperature", GTK_JUSTIFY_RIGHT);
185         airconsumption = info_label(hbox, "air", GTK_JUSTIFY_RIGHT);
186
187         return frame;
188 }
189
190 void update_air_info(char *buffer)
191 {
192         char markup[120];
193         
194         snprintf(markup, sizeof(markup), "<span font=\"8\">%s</span>",buffer);
195         gtk_label_set_markup(GTK_LABEL(airconsumption), markup);
196 }
197
198 static GtkEntry *text_entry(GtkWidget *box, const char *label)
199 {
200         GtkWidget *entry;
201         GtkWidget *frame = gtk_frame_new(label);
202
203         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
204
205         entry = gtk_entry_new();
206         gtk_container_add(GTK_CONTAINER(frame), entry);
207
208         return GTK_ENTRY(entry);
209 }
210
211 static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
212 {
213         GtkWidget *view, *vbox;
214         GtkTextBuffer *buffer;
215         GtkWidget *frame = gtk_frame_new(label);
216
217         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
218         box = gtk_hbox_new(FALSE, 3);
219         gtk_container_add(GTK_CONTAINER(frame), box);
220         vbox = gtk_vbox_new(FALSE, 3);
221         gtk_container_add(GTK_CONTAINER(box), vbox);
222
223         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
224         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
225         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
226         gtk_widget_show(scrolled_window);
227
228         view = gtk_text_view_new();
229         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
230         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
231
232         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
233
234         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
235         return buffer;
236 }
237
238 GtkWidget *extended_dive_info_widget(void)
239 {
240         GtkWidget *vbox, *hbox;
241         vbox = gtk_vbox_new(FALSE, 6);
242
243         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
244         location = text_entry(vbox, "Location");
245
246         hbox = gtk_hbox_new(FALSE, 3);
247         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
248
249         divemaster = text_entry(hbox, "Divemaster");
250         buddy = text_entry(hbox, "Buddy");
251
252         notes = text_view(vbox, "Notes");
253
254         /* Add extended info here: name, description, yadda yadda */
255         show_dive_info(current_dive);
256         return vbox;
257 }