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