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