]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Do something half-way sane (no SIGSEGV) when there are no dives
[ext/subsurface.git] / info.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 #include "dive.h"
6 #include "display.h"
7
8 static GtkWidget *datetime, *depth, *duration;
9
10 void update_dive_info(struct dive *dive)
11 {
12         struct tm *tm;
13         char buffer[80];
14
15         if (!dive) {
16                 gtk_entry_set_text(GTK_ENTRY(datetime), "no dive");
17                 gtk_entry_set_text(GTK_ENTRY(depth), "");
18                 gtk_entry_set_text(GTK_ENTRY(duration), "");
19                 return;
20         }
21
22         tm = gmtime(&dive->when);
23         snprintf(buffer, sizeof(buffer),
24                 "%04d-%02d-%02d "
25                 "%02d:%02d:%02d",
26                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
27                 tm->tm_hour, tm->tm_min, tm->tm_sec);
28         gtk_entry_set_text(GTK_ENTRY(datetime), buffer);
29
30         snprintf(buffer, sizeof(buffer),
31                 "%d ft",
32                 to_feet(dive->maxdepth));
33         gtk_entry_set_text(GTK_ENTRY(depth), buffer);
34
35         snprintf(buffer, sizeof(buffer),
36                 "%d min",
37                 dive->duration.seconds / 60);
38         gtk_entry_set_text(GTK_ENTRY(duration), buffer);
39 }
40
41 GtkWidget *dive_info_frame(void)
42 {
43         GtkWidget *frame;
44         GtkWidget *hbox;
45
46         frame = gtk_frame_new("Dive info");
47         gtk_widget_show(frame);
48
49         hbox = gtk_hbox_new(FALSE, 5);
50         gtk_container_add(GTK_CONTAINER(frame), hbox);
51
52         datetime = gtk_entry_new();
53         gtk_editable_set_editable(GTK_EDITABLE(datetime), FALSE);
54
55         gtk_box_pack_start(GTK_BOX(hbox), datetime, FALSE, FALSE, 0);
56
57         depth = gtk_entry_new();
58         gtk_editable_set_editable(GTK_EDITABLE(depth), FALSE);
59
60         gtk_box_pack_start(GTK_BOX(hbox), depth, FALSE, FALSE, 0);
61
62         duration = gtk_entry_new();
63         gtk_editable_set_editable(GTK_EDITABLE(duration), FALSE);
64
65         gtk_box_pack_start(GTK_BOX(hbox), duration, FALSE, FALSE, 0);
66
67         update_dive_info(current_dive);
68         return frame;
69 }