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)
19 #include "display-gtk.h"
22 static GtkComboBoxEntry *location, *buddy, *divemaster;
23 static GtkTextBuffer *notes;
25 static char *get_text(GtkTextBuffer *buffer)
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);
35 /* old is NULL or a valid string, new is a valid string
36 * NOTW: NULL and "" need to be treated as "unchanged" */
37 static int text_changed(const char *old, const char *new)
39 return ((old && strcmp(old,new)) ||
40 (!old && strcmp("",new)));
43 static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp)
49 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
50 new = gtk_entry_get_text(entry);
53 if (!text_changed(old,new))
61 void flush_dive_info_changes(struct dive *dive)
63 char *old_text, *new_text;
69 new_text = get_combo_box_entry_text(location, &dive->location);
71 add_location(new_text);
75 new_text = get_combo_box_entry_text(divemaster, &dive->divemaster);
81 new_text = get_combo_box_entry_text(buddy, &dive->buddy);
87 old_text = dive->notes;
88 dive->notes = get_text(notes);
89 if (text_changed(old_text,dive->notes))
95 mark_divelist_changed(TRUE);
98 static void set_combo_box_entry_text(GtkComboBoxEntry *combo_box, const char *text)
100 GtkEntry *entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
101 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
104 gtk_entry_set_text(entry, text);
107 #define SET_TEXT_ENTRY(x) \
108 set_combo_box_entry_text(x, dive && dive->x ? dive->x : "")
110 void show_dive_info(struct dive *dive)
116 /* dive number and location (or lacking that, the date) go in the window title */
117 tm = gmtime(&dive->when);
118 text = dive->location;
122 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
124 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
126 weekday(tm->tm_wday),
127 tm->tm_mon+1, tm->tm_mday,
129 tm->tm_hour, tm->tm_min);
133 text += 10; /* Skip the "Dive #0 - " part */
134 gtk_window_set_title(GTK_WINDOW(main_window), text);
136 SET_TEXT_ENTRY(divemaster);
137 SET_TEXT_ENTRY(buddy);
138 SET_TEXT_ENTRY(location);
139 gtk_text_buffer_set_text(notes, dive && dive->notes ? dive->notes : "", -1);
142 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions)
145 GtkWidget *combo_box;
146 GtkWidget *frame = gtk_frame_new(label);
147 GtkEntryCompletion *completion;
149 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
151 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
152 gtk_container_add(GTK_CONTAINER(frame), combo_box);
154 entry = GTK_ENTRY(GTK_BIN(combo_box)->child);
156 completion = gtk_entry_completion_new();
157 gtk_entry_completion_set_text_column(completion, 0);
158 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
159 gtk_entry_completion_set_inline_completion(completion, TRUE);
160 gtk_entry_completion_set_inline_selection(completion, TRUE);
161 gtk_entry_completion_set_popup_single_match(completion, FALSE);
162 gtk_entry_set_completion(entry, completion);
164 return GTK_COMBO_BOX_ENTRY(combo_box);
167 static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
169 GtkWidget *view, *vbox;
170 GtkTextBuffer *buffer;
171 GtkWidget *frame = gtk_frame_new(label);
173 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
174 box = gtk_hbox_new(FALSE, 3);
175 gtk_container_add(GTK_CONTAINER(frame), box);
176 vbox = gtk_vbox_new(FALSE, 3);
177 gtk_container_add(GTK_CONTAINER(box), vbox);
179 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
180 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
181 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
183 view = gtk_text_view_new();
184 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
185 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
187 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
189 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
197 } found_string_entry;
198 static GtkTreeIter string_entry_location;
200 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
202 const char *string = data;
206 gtk_tree_model_get(model, iter, 0, &entry, -1);
207 cmp = strcmp(entry, string);
209 /* Stop. The entry is bigger than the new one */
215 found_string_entry = MATCH_EXACT;
219 string_entry_location = *iter;
220 found_string_entry = MATCH_AFTER;
224 static int match_list(GtkListStore *list, const char *string)
226 found_string_entry = MATCH_PREPEND;
227 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
228 return found_string_entry;
231 static GtkListStore *location_list, *people_list;
233 static void add_string_list_entry(const char *string, GtkListStore *list)
235 GtkTreeIter *iter, loc;
237 if (!string || !*string)
240 switch (match_list(list, string)) {
247 iter = &string_entry_location;
250 gtk_list_store_insert_after(list, &loc, iter);
251 gtk_list_store_set(list, &loc, 0, string, -1);
254 void add_people(const char *string)
256 add_string_list_entry(string, people_list);
259 void add_location(const char *string)
261 add_string_list_entry(string, location_list);
264 GtkWidget *extended_dive_info_widget(void)
266 GtkWidget *vbox, *hbox;
267 vbox = gtk_vbox_new(FALSE, 6);
269 people_list = gtk_list_store_new(1, G_TYPE_STRING);
270 location_list = gtk_list_store_new(1, G_TYPE_STRING);
272 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
273 location = text_entry(vbox, "Location", location_list);
275 hbox = gtk_hbox_new(FALSE, 3);
276 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
278 divemaster = text_entry(hbox, "Divemaster", people_list);
279 buddy = text_entry(hbox, "Buddy", people_list);
281 notes = text_view(vbox, "Notes");