]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Merge branch 'open-files' of git://github.com/nathansamson/diveclog
[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 *divedate, *divetime, *depth, *duration, *temperature, *locationnote;
11 static GtkEntry *location;
12 static GtkTextBuffer *notes;
13 static int location_changed = 1, notes_changed = 1;
14 static struct dive *buffered_dive;
15
16 static const char *weekday(int wday)
17 {
18         static const char wday_array[7][4] = {
19                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
20         };
21         return wday_array[wday];
22 }
23
24 static char *get_text(GtkTextBuffer *buffer)
25 {
26         GtkTextIter start;
27         GtkTextIter end;
28
29         gtk_text_buffer_get_start_iter(buffer, &start);
30         gtk_text_buffer_get_end_iter(buffer, &end);
31         return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
32 }
33
34 void flush_dive_info_changes(void)
35 {
36         struct dive *dive = buffered_dive;
37
38         if (!dive)
39                 return;
40
41         if (location_changed) {
42                 g_free(dive->location);
43                 dive->location = gtk_editable_get_chars(GTK_EDITABLE(location), 0, -1);
44         }
45
46         if (notes_changed) {
47                 g_free(dive->notes);
48                 dive->notes = get_text(notes);
49         }
50 }
51
52 void update_dive_info(struct dive *dive)
53 {
54         struct tm *tm;
55         char buffer[80];
56         char *text;
57
58         flush_dive_info_changes();
59         buffered_dive = dive;
60
61         if (!dive) {
62                 gtk_label_set_text(GTK_LABEL(divedate), "no dive");
63                 gtk_label_set_text(GTK_LABEL(divetime), "");
64                 gtk_label_set_text(GTK_LABEL(depth), "");
65                 gtk_label_set_text(GTK_LABEL(duration), "");
66                 return;
67         }
68
69         tm = gmtime(&dive->when);
70         snprintf(buffer, sizeof(buffer),
71                 "%s %02d/%02d/%04d",
72                 weekday(tm->tm_wday),
73                 tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900);
74         gtk_label_set_text(GTK_LABEL(divedate), buffer);
75
76         snprintf(buffer, sizeof(buffer),
77                 "%02d:%02d:%02d",
78                 tm->tm_hour, tm->tm_min, tm->tm_sec);
79         gtk_label_set_text(GTK_LABEL(divetime), buffer);
80
81         snprintf(buffer, sizeof(buffer),
82                 "%d ft",
83                 to_feet(dive->maxdepth));
84         gtk_label_set_text(GTK_LABEL(depth), buffer);
85
86         snprintf(buffer, sizeof(buffer),
87                 "%d min",
88                 dive->duration.seconds / 60);
89         gtk_label_set_text(GTK_LABEL(duration), buffer);
90
91         *buffer = 0;
92         if (dive->watertemp.mkelvin)
93                 snprintf(buffer, sizeof(buffer),
94                         "%d C",
95                         to_C(dive->watertemp));
96         gtk_label_set_text(GTK_LABEL(temperature), buffer);
97
98         text = dive->location ? : "";
99         gtk_entry_set_text(location, text);
100         gtk_label_set_text(GTK_LABEL(locationnote), text);
101
102         text = dive->notes ? : "";
103         gtk_text_buffer_set_text(notes, text, -1);
104 }
105
106 static GtkWidget *info_label(GtkWidget *box, const char *str, GtkJustification jtype)
107 {
108         GtkWidget *label = gtk_label_new(str);
109         gtk_label_set_justify(GTK_LABEL(label), jtype);
110         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
111         return label;
112 }
113
114 GtkWidget *dive_info_frame(void)
115 {
116         GtkWidget *frame;
117         GtkWidget *hbox, *hbox2;
118         GtkWidget *vbox;
119
120         frame = gtk_frame_new("Dive info");
121         gtk_widget_show(frame);
122
123         vbox = gtk_vbox_new(TRUE, 6);
124         gtk_container_set_border_width(GTK_CONTAINER(vbox), 3);
125         gtk_container_add(GTK_CONTAINER(frame), vbox);
126
127         hbox = gtk_hbox_new(TRUE, 6);
128         gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
129         gtk_container_add(GTK_CONTAINER(vbox), hbox);
130
131         hbox2 = gtk_hbox_new(FALSE, 6);
132         gtk_container_set_border_width(GTK_CONTAINER(hbox2), 3);
133         gtk_container_add(GTK_CONTAINER(vbox), hbox2);
134
135         divedate = info_label(hbox, "date", GTK_JUSTIFY_RIGHT);
136         divetime = info_label(hbox, "time", GTK_JUSTIFY_RIGHT);
137         depth = info_label(hbox, "depth", GTK_JUSTIFY_RIGHT);
138         duration = info_label(hbox, "duration", GTK_JUSTIFY_RIGHT);
139         temperature = info_label(hbox, "temperature", GTK_JUSTIFY_RIGHT);
140
141         locationnote = info_label(hbox2, "location", GTK_JUSTIFY_LEFT);
142         gtk_label_set_width_chars(GTK_LABEL(locationnote), 80);
143
144         return frame;
145 }
146
147 static GtkEntry *text_entry(GtkWidget *box, const char *label)
148 {
149         GtkWidget *entry;
150
151         GtkWidget *frame = gtk_frame_new(label);
152
153         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
154
155         entry = gtk_entry_new ();
156         gtk_container_add(GTK_CONTAINER(frame), entry);
157
158         return GTK_ENTRY(entry);
159 }
160
161 static GtkTextBuffer *text_view(GtkWidget *box, const char *label, gboolean expand)
162 {
163         GtkWidget *view;
164         GtkTextBuffer *buffer;
165
166         GtkWidget *frame = gtk_frame_new(label);
167
168         gtk_box_pack_start(GTK_BOX(box), frame, expand, expand, 0);
169
170         GtkWidget* scrolled_window = gtk_scrolled_window_new (0, 0);
171         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
172         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
173         gtk_widget_show(scrolled_window);
174
175         view = gtk_text_view_new ();
176         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
177         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
178
179         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
180
181         gtk_container_add(GTK_CONTAINER(frame), scrolled_window);
182         return buffer;
183 }
184
185 GtkWidget *extended_dive_info_widget(void)
186 {
187         GtkWidget *vbox;
188
189         vbox = gtk_vbox_new(FALSE, 6);
190
191         location = text_entry(vbox, "Location");
192         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
193         notes = text_view(vbox, "Notes", TRUE);
194
195         /* Add extended info here: name, description, yadda yadda */
196         update_dive_info(current_dive);
197         return vbox;
198 }