]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Save and parse notes and locations
[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 GtkTextBuffer *location, *notes;
10 static int location_changed = 1, notes_changed = 1;
11 static struct dive *buffered_dive;
12
13 static const char *weekday(int wday)
14 {
15         static const char wday_array[7][4] = {
16                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
17         };
18         return wday_array[wday];
19 }
20
21 static char *get_text(GtkTextBuffer *buffer)
22 {
23         GtkTextIter start;
24         GtkTextIter end;
25
26         gtk_text_buffer_get_start_iter(buffer, &start);
27         gtk_text_buffer_get_end_iter(buffer, &end);
28         return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
29 }
30
31 void flush_dive_info_changes(void)
32 {
33         struct dive *dive = buffered_dive;
34
35         if (!dive)
36                 return;
37
38         if (location_changed) {
39                 g_free(dive->location);
40                 dive->location = get_text(location);
41         }
42
43         if (notes_changed) {
44                 g_free(dive->notes);
45                 dive->notes = get_text(notes);
46         }
47 }
48
49 void update_dive_info(struct dive *dive)
50 {
51         struct tm *tm;
52         char buffer[80];
53         char *text;
54
55         flush_dive_info_changes();
56         buffered_dive = dive;
57
58         if (!dive) {
59                 gtk_label_set_text(GTK_LABEL(divedate), "no dive");
60                 gtk_label_set_text(GTK_LABEL(divetime), "");
61                 gtk_label_set_text(GTK_LABEL(depth), "");
62                 gtk_label_set_text(GTK_LABEL(duration), "");
63                 return;
64         }
65
66         tm = gmtime(&dive->when);
67         snprintf(buffer, sizeof(buffer),
68                 "%s %02d/%02d/%04d",
69                 weekday(tm->tm_wday),
70                 tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900);
71         gtk_label_set_text(GTK_LABEL(divedate), buffer);
72
73         snprintf(buffer, sizeof(buffer),
74                 "%02d:%02d:%02d",
75                 tm->tm_hour, tm->tm_min, tm->tm_sec);
76         gtk_label_set_text(GTK_LABEL(divetime), buffer);
77
78         snprintf(buffer, sizeof(buffer),
79                 "%d ft",
80                 to_feet(dive->maxdepth));
81         gtk_label_set_text(GTK_LABEL(depth), buffer);
82
83         snprintf(buffer, sizeof(buffer),
84                 "%d min",
85                 dive->duration.seconds / 60);
86         gtk_label_set_text(GTK_LABEL(duration), buffer);
87
88         text = dive->location ? : "";
89         gtk_text_buffer_set_text(location, text, -1);
90         text = dive->notes ? : "";
91         gtk_text_buffer_set_text(notes, text, -1);
92 }
93
94 static GtkWidget *info_label(GtkWidget *box, const char *str)
95 {
96         GtkWidget *label = gtk_label_new(str);
97         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
98         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
99         return label;
100 }
101
102 GtkWidget *dive_info_frame(void)
103 {
104         GtkWidget *frame;
105         GtkWidget *hbox;
106
107         frame = gtk_frame_new("Dive info");
108         gtk_widget_show(frame);
109
110         hbox = gtk_hbox_new(TRUE, 5);
111         gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
112         gtk_container_add(GTK_CONTAINER(frame), hbox);
113
114         divedate = info_label(hbox, "date");
115         divetime = info_label(hbox, "time");
116         depth = info_label(hbox, "depth");
117         duration = info_label(hbox, "duration");
118
119         return frame;
120 }
121
122 static GtkTextBuffer *text_entry(GtkWidget *box, const char *label, gboolean expand)
123 {
124         GtkWidget *view;
125         GtkTextBuffer *buffer;
126
127         GtkWidget *frame = gtk_frame_new(label);
128
129         gtk_box_pack_start(GTK_BOX(box), frame, expand, expand, 0);
130
131         view = gtk_text_view_new ();
132         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
133
134         gtk_container_add(GTK_CONTAINER(frame), view);
135         return buffer;
136 }
137
138 GtkWidget *extended_dive_info_frame(void)
139 {
140         GtkWidget *frame;
141         GtkWidget *vbox;
142
143         frame = gtk_frame_new("Extended dive info");
144         gtk_widget_show(frame);
145
146         vbox = gtk_vbox_new(FALSE, 5);
147         gtk_container_add(GTK_CONTAINER(frame), vbox);
148
149         location = text_entry(vbox, "Location", FALSE);
150         notes = text_entry(vbox, "Notes", TRUE);
151
152         /* Add extended info here: name, description, yadda yadda */
153         update_dive_info(current_dive);
154         return frame;
155 }