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