]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Clean up dive info box too
[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 *divedate, *divetime, *depth, *duration, *temperature;
12 static GtkEntry *location;
13 static GtkTextBuffer *notes;
14 static int location_changed = 1, notes_changed = 1;
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(struct dive *dive)
35 {
36         if (!dive)
37                 return;
38
39         if (location_changed) {
40                 g_free(dive->location);
41                 dive->location = gtk_editable_get_chars(GTK_EDITABLE(location), 0, -1);
42         }
43
44         if (notes_changed) {
45                 g_free(dive->notes);
46                 dive->notes = get_text(notes);
47         }
48 }
49
50 void show_dive_info(struct dive *dive)
51 {
52         struct tm *tm;
53         char buffer[80];
54         char *text;
55         int len;
56
57         if (!dive) {
58                 gtk_label_set_text(GTK_LABEL(divedate), "no dive");
59                 gtk_label_set_text(GTK_LABEL(divetime), "");
60                 gtk_label_set_text(GTK_LABEL(depth), "");
61                 gtk_label_set_text(GTK_LABEL(duration), "");
62                 return;
63         }
64
65         tm = gmtime(&dive->when);
66         snprintf(buffer, sizeof(buffer),
67                 "%s %02d/%02d/%04d",
68                 weekday(tm->tm_wday),
69                 tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900);
70         gtk_label_set_text(GTK_LABEL(divedate), buffer);
71
72         snprintf(buffer, sizeof(buffer),
73                 "%02d:%02d:%02d",
74                 tm->tm_hour, tm->tm_min, tm->tm_sec);
75         gtk_label_set_text(GTK_LABEL(divetime), buffer);
76
77         switch (output_units.length) {
78         case METERS:
79                 snprintf(buffer, sizeof(buffer),
80                         "%.1f m",
81                         dive->maxdepth.mm / 1000.0);
82                 break;
83         case FEET:
84                 snprintf(buffer, sizeof(buffer),
85                         "%d ft",
86                         to_feet(dive->maxdepth));
87                 break;
88         }
89         gtk_label_set_text(GTK_LABEL(depth), buffer);
90
91         snprintf(buffer, sizeof(buffer),
92                 "%d min",
93                 dive->duration.seconds / 60);
94         gtk_label_set_text(GTK_LABEL(duration), buffer);
95
96         *buffer = 0;
97         if (dive->watertemp.mkelvin) {
98                 switch (output_units.temperature) {
99                 case CELSIUS:
100                         snprintf(buffer, sizeof(buffer),
101                                 "%d C",
102                                 to_C(dive->watertemp));
103                         break;
104                 case FAHRENHEIT:
105                         snprintf(buffer, sizeof(buffer),
106                                 "%d F",
107                                 to_F(dive->watertemp));
108                         break;
109                 case KELVIN:
110                         snprintf(buffer, sizeof(buffer),
111                                 "%d K",
112                                 to_K(dive->watertemp));
113                         break;
114                 }
115         }
116         gtk_label_set_text(GTK_LABEL(temperature), buffer);
117
118         text = dive->location ? : "";
119         gtk_entry_set_text(location, text);
120
121         text = "Dive Info";
122         if (dive->location && *dive->location)
123                 text = dive->location;
124         len = 0;
125         if (dive->number)
126                 len = snprintf(buffer, sizeof(buffer), "%d. ", dive->number);
127         snprintf(buffer+len, sizeof(buffer)-len, "%s", text);
128         gtk_frame_set_label(GTK_FRAME(info_frame), buffer);
129
130         text = dive->notes ? : "";
131         gtk_text_buffer_set_text(notes, text, -1);
132 }
133
134 static GtkWidget *info_label(GtkWidget *box, const char *str, GtkJustification jtype)
135 {
136         GtkWidget *label = gtk_label_new(str);
137         gtk_label_set_justify(GTK_LABEL(label), jtype);
138         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
139         return label;
140 }
141
142 GtkWidget *dive_info_frame(void)
143 {
144         GtkWidget *frame;
145         GtkWidget *hbox;
146         GtkWidget *vbox;
147
148         frame = gtk_frame_new("Dive info");
149         info_frame = frame;
150         gtk_widget_show(frame);
151
152         vbox = gtk_vbox_new(FALSE, 6);
153         gtk_container_set_border_width(GTK_CONTAINER(vbox), 3);
154         gtk_container_add(GTK_CONTAINER(frame), vbox);
155
156         hbox = gtk_hbox_new(FALSE, 16);
157         gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
158         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
159
160         divedate = info_label(hbox, "date", GTK_JUSTIFY_RIGHT);
161         divetime = info_label(hbox, "time", GTK_JUSTIFY_RIGHT);
162         depth = info_label(hbox, "depth", GTK_JUSTIFY_RIGHT);
163         duration = info_label(hbox, "duration", GTK_JUSTIFY_RIGHT);
164         temperature = info_label(hbox, "temperature", GTK_JUSTIFY_RIGHT);
165
166         return frame;
167 }
168
169 static GtkEntry *text_entry(GtkWidget *box, const char *label)
170 {
171         GtkWidget *entry;
172         GtkWidget *frame = gtk_frame_new(label);
173
174         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
175
176         entry = gtk_entry_new();
177         gtk_container_add(GTK_CONTAINER(frame), entry);
178
179         return GTK_ENTRY(entry);
180 }
181
182 static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
183 {
184         GtkWidget *view, *vbox;
185         GtkTextBuffer *buffer;
186         GtkWidget *frame = gtk_frame_new(label);
187
188         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
189         box = gtk_hbox_new(FALSE, 3);
190         gtk_container_add(GTK_CONTAINER(frame), box);
191         vbox = gtk_vbox_new(FALSE, 3);
192         gtk_container_add(GTK_CONTAINER(box), vbox);
193
194         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
195         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
196         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
197         gtk_widget_show(scrolled_window);
198
199         view = gtk_text_view_new();
200         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
201         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
202
203         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
204
205         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
206         return buffer;
207 }
208
209 GtkWidget *extended_dive_info_widget(void)
210 {
211         GtkWidget *vbox;
212         vbox = gtk_vbox_new(FALSE, 6);
213
214         location = text_entry(vbox, "Location");
215         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
216         notes = text_view(vbox, "Notes");
217
218         /* Add extended info here: name, description, yadda yadda */
219         show_dive_info(current_dive);
220         return vbox;
221 }