]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Add some extended dive info fields
[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 *divedate, *divetime, *depth, *duration;
9 static GtkWidget *location, *notes;
10
11 static const char *weekday(int wday)
12 {
13         static const char wday_array[7][4] = {
14                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
15         };
16         return wday_array[wday];
17 }
18
19 void update_dive_info(struct dive *dive)
20 {
21         struct tm *tm;
22         char buffer[80];
23
24         if (!dive) {
25                 gtk_label_set_text(GTK_LABEL(divedate), "no dive");
26                 gtk_label_set_text(GTK_LABEL(divetime), "");
27                 gtk_label_set_text(GTK_LABEL(depth), "");
28                 gtk_label_set_text(GTK_LABEL(duration), "");
29                 return;
30         }
31
32         tm = gmtime(&dive->when);
33         snprintf(buffer, sizeof(buffer),
34                 "%s %02d/%02d/%04d",
35                 weekday(tm->tm_wday),
36                 tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900);
37         gtk_label_set_text(GTK_LABEL(divedate), buffer);
38
39         snprintf(buffer, sizeof(buffer),
40                 "%02d:%02d:%02d",
41                 tm->tm_hour, tm->tm_min, tm->tm_sec);
42         gtk_label_set_text(GTK_LABEL(divetime), buffer);
43
44         snprintf(buffer, sizeof(buffer),
45                 "%d ft",
46                 to_feet(dive->maxdepth));
47         gtk_label_set_text(GTK_LABEL(depth), buffer);
48
49         snprintf(buffer, sizeof(buffer),
50                 "%d min",
51                 dive->duration.seconds / 60);
52         gtk_label_set_text(GTK_LABEL(duration), buffer);
53 }
54
55 static GtkWidget *info_label(GtkWidget *box, const char *str)
56 {
57         GtkWidget *label = gtk_label_new(str);
58         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
59         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
60         return label;
61 }
62
63 GtkWidget *dive_info_frame(void)
64 {
65         GtkWidget *frame;
66         GtkWidget *hbox;
67
68         frame = gtk_frame_new("Dive info");
69         gtk_widget_show(frame);
70
71         hbox = gtk_hbox_new(TRUE, 5);
72         gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
73         gtk_container_add(GTK_CONTAINER(frame), hbox);
74
75         divedate = info_label(hbox, "date");
76         divetime = info_label(hbox, "time");
77         depth = info_label(hbox, "depth");
78         duration = info_label(hbox, "duration");
79
80         return frame;
81 }
82
83 static GtkWidget *text_entry(GtkWidget *box, const char *label)
84 {
85         GtkWidget *entry;
86         GtkWidget *frame = gtk_frame_new(label);
87         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0);
88         entry = gtk_entry_new();
89         gtk_container_add(GTK_CONTAINER(frame), entry);
90         return entry;
91 }
92
93 GtkWidget *extended_dive_info_frame(void)
94 {
95         GtkWidget *frame;
96         GtkWidget *vbox;
97
98         frame = gtk_frame_new("Extended dive info");
99         gtk_widget_show(frame);
100
101         vbox = gtk_vbox_new(FALSE, 5);
102         gtk_container_add(GTK_CONTAINER(frame), vbox);
103
104         location = text_entry(vbox, "Location");
105         notes = text_entry(vbox, "Notes");
106         location = gtk_entry_new();
107
108         /* Add extended info here: name, description, yadda yadda */
109         update_dive_info(current_dive);
110         return frame;
111 }