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;
15 static const char *weekday(int wday)
17 static const char wday_array[7][4] = {
18 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
20 return wday_array[wday];
23 static char *get_text(GtkTextBuffer *buffer)
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);
33 void flush_dive_info_changes(void)
35 struct dive *dive = buffered_dive;
40 if (location_changed) {
41 g_free(dive->location);
42 dive->location = gtk_editable_get_chars(GTK_EDITABLE(location), 0, -1);
47 dive->notes = get_text(notes);
51 void update_dive_info(struct dive *dive)
57 flush_dive_info_changes();
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), "");
68 tm = gmtime(&dive->when);
69 snprintf(buffer, sizeof(buffer),
72 tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900);
73 gtk_label_set_text(GTK_LABEL(divedate), buffer);
75 snprintf(buffer, sizeof(buffer),
77 tm->tm_hour, tm->tm_min, tm->tm_sec);
78 gtk_label_set_text(GTK_LABEL(divetime), buffer);
80 snprintf(buffer, sizeof(buffer),
82 to_feet(dive->maxdepth));
83 gtk_label_set_text(GTK_LABEL(depth), buffer);
85 snprintf(buffer, sizeof(buffer),
87 dive->duration.seconds / 60);
88 gtk_label_set_text(GTK_LABEL(duration), buffer);
91 if (dive->watertemp.mkelvin)
92 snprintf(buffer, sizeof(buffer),
94 to_C(dive->watertemp));
95 gtk_label_set_text(GTK_LABEL(temperature), buffer);
97 text = dive->location ? : "";
98 gtk_entry_set_text(location, text);
99 gtk_label_set_text(GTK_LABEL(locationnote), text);
101 text = dive->notes ? : "";
102 gtk_text_buffer_set_text(notes, text, -1);
105 static GtkWidget *info_label(GtkWidget *box, const char *str, GtkJustification jtype)
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);
113 GtkWidget *dive_info_frame(void)
116 GtkWidget *hbox, *hbox2;
119 frame = gtk_frame_new("Dive info");
120 gtk_widget_show(frame);
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);
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);
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);
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);
140 locationnote = info_label(hbox2, "location", GTK_JUSTIFY_LEFT);
141 gtk_label_set_width_chars(GTK_LABEL(locationnote), 80);
146 static GtkEntry *text_entry(GtkWidget *box, const char *label)
150 GtkWidget *frame = gtk_frame_new(label);
152 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
154 entry = gtk_entry_new ();
155 gtk_container_add(GTK_CONTAINER(frame), entry);
157 return GTK_ENTRY(entry);
160 static GtkTextBuffer *text_view(GtkWidget *box, const char *label, gboolean expand)
163 GtkTextBuffer *buffer;
165 GtkWidget *frame = gtk_frame_new(label);
167 gtk_box_pack_start(GTK_BOX(box), frame, expand, expand, 0);
169 GtkWidget* scrolled_window = gtk_scrolled_window_new (0, 0);
170 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
171 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
172 gtk_widget_show(scrolled_window);
174 view = gtk_text_view_new ();
175 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
176 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
178 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
180 gtk_container_add(GTK_CONTAINER(frame), scrolled_window);
184 GtkWidget *extended_dive_info_widget(void)
188 vbox = gtk_vbox_new(FALSE, 5);
190 location = text_entry(vbox, "Location");
191 notes = text_view(vbox, "Notes", TRUE);
193 /* Add extended info here: name, description, yadda yadda */
194 update_dive_info(current_dive);