2 /* creates the UI for the info frame -
3 * controlled through the following interfaces:
5 * void flush_dive_info_changes(struct dive *dive)
6 * void show_dive_info(struct dive *dive)
9 * GtkWidget *extended_dive_info_widget(void)
18 #include "display-gtk.h"
21 static GtkEntry *location, *buddy, *divemaster;
22 static GtkTextBuffer *notes;
23 static int location_changed = 1, notes_changed = 1;
24 static int divemaster_changed = 1, buddy_changed = 1;
26 static char *get_text(GtkTextBuffer *buffer)
31 gtk_text_buffer_get_start_iter(buffer, &start);
32 gtk_text_buffer_get_end_iter(buffer, &end);
33 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
36 /* old is NULL or a valid string, new is a valid string
37 * NOTW: NULL and "" need to be treated as "unchanged" */
38 static int text_changed(char *old, char *new)
40 return ((old && strcmp(old,new)) ||
41 (!old && strcmp("",new)));
44 void flush_dive_info_changes(struct dive *dive)
52 if (location_changed) {
53 old_text = dive->location;
54 dive->location = gtk_editable_get_chars(GTK_EDITABLE(location), 0, -1);
55 if (text_changed(old_text,dive->location))
61 if (divemaster_changed) {
62 old_text = dive->divemaster;
63 dive->divemaster = gtk_editable_get_chars(GTK_EDITABLE(divemaster), 0, -1);
64 if (text_changed(old_text,dive->divemaster))
71 old_text = dive->buddy;
72 dive->buddy = gtk_editable_get_chars(GTK_EDITABLE(buddy), 0, -1);
73 if (text_changed(old_text,dive->buddy))
80 old_text = dive->notes;
81 dive->notes = get_text(notes);
82 if (text_changed(old_text,dive->notes))
88 mark_divelist_changed(TRUE);
91 #define SET_TEXT_ENTRY(x) \
92 gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
94 void show_dive_info(struct dive *dive)
100 /* dive number and location (or lacking that, the date) go in the window title */
101 tm = gmtime(&dive->when);
102 text = dive->location;
106 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
108 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
110 weekday(tm->tm_wday),
111 tm->tm_mon+1, tm->tm_mday,
113 tm->tm_hour, tm->tm_min);
117 text += 10; /* Skip the "Dive #0 - " part */
118 gtk_window_set_title(GTK_WINDOW(main_window), text);
120 SET_TEXT_ENTRY(divemaster);
121 SET_TEXT_ENTRY(buddy);
122 SET_TEXT_ENTRY(location);
123 gtk_text_buffer_set_text(notes, dive && dive->notes ? dive->notes : "", -1);
126 static GtkEntry *text_entry(GtkWidget *box, const char *label)
129 GtkWidget *frame = gtk_frame_new(label);
131 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
133 entry = gtk_entry_new();
134 gtk_container_add(GTK_CONTAINER(frame), entry);
136 return GTK_ENTRY(entry);
139 static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
141 GtkWidget *view, *vbox;
142 GtkTextBuffer *buffer;
143 GtkWidget *frame = gtk_frame_new(label);
145 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
146 box = gtk_hbox_new(FALSE, 3);
147 gtk_container_add(GTK_CONTAINER(frame), box);
148 vbox = gtk_vbox_new(FALSE, 3);
149 gtk_container_add(GTK_CONTAINER(box), vbox);
151 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
152 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
153 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
155 view = gtk_text_view_new();
156 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
157 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
159 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
161 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
165 GtkWidget *extended_dive_info_widget(void)
167 GtkWidget *vbox, *hbox;
168 vbox = gtk_vbox_new(FALSE, 6);
170 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
171 location = text_entry(vbox, "Location");
173 hbox = gtk_hbox_new(FALSE, 3);
174 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
176 divemaster = text_entry(hbox, "Divemaster");
177 buddy = text_entry(hbox, "Buddy");
179 notes = text_view(vbox, "Notes");