2 /* creates the UI for the info frame -
3 * controlled through the following interfaces:
5 * void show_dive_info(struct dive *dive)
8 * GtkWidget *extended_dive_info_widget(void)
18 #include "display-gtk.h"
21 static GtkEntry *location, *buddy, *divemaster;
22 static GtkTextView *notes;
23 static GtkListStore *location_list, *people_list;
25 static char *get_text(GtkTextView *view)
27 GtkTextBuffer *buffer;
31 buffer = gtk_text_view_get_buffer(view);
32 gtk_text_buffer_get_start_iter(buffer, &start);
33 gtk_text_buffer_get_end_iter(buffer, &end);
34 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
37 /* old is NULL or a valid string, new is a valid string
38 * NOTW: NULL and "" need to be treated as "unchanged" */
39 static int text_changed(const char *old, const char *new)
41 return ((old && strcmp(old,new)) ||
42 (!old && strcmp("",new)));
45 static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp)
51 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
52 new = gtk_entry_get_text(entry);
55 if (!text_changed(old,new))
62 #define SET_TEXT_VALUE(x) \
63 gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
65 void show_dive_info(struct dive *dive)
71 /* dive number and location (or lacking that, the date) go in the window title */
72 tm = gmtime(&dive->when);
73 text = dive->location;
77 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
79 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
82 tm->tm_mon+1, tm->tm_mday,
84 tm->tm_hour, tm->tm_min);
88 text += 10; /* Skip the "Dive #0 - " part */
89 gtk_window_set_title(GTK_WINDOW(main_window), text);
91 SET_TEXT_VALUE(divemaster);
92 SET_TEXT_VALUE(buddy);
93 SET_TEXT_VALUE(location);
94 gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
95 dive && dive->notes ? dive->notes : "", -1);
98 static GtkEntry *text_value(GtkWidget *box, const char *label)
101 GtkWidget *frame = gtk_frame_new(label);
103 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
104 widget = gtk_entry_new();
105 gtk_widget_set_can_focus(widget, FALSE);
106 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
107 gtk_container_add(GTK_CONTAINER(frame), widget);
108 return GTK_ENTRY(widget);
111 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
114 GtkWidget *combo_box;
115 GtkWidget *frame = gtk_frame_new(label);
116 GtkEntryCompletion *completion;
118 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
120 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
121 gtk_container_add(GTK_CONTAINER(frame), combo_box);
123 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
125 gtk_entry_set_text(entry, text);
127 completion = gtk_entry_completion_new();
128 gtk_entry_completion_set_text_column(completion, 0);
129 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
130 gtk_entry_completion_set_inline_completion(completion, TRUE);
131 gtk_entry_completion_set_inline_selection(completion, TRUE);
132 gtk_entry_completion_set_popup_single_match(completion, FALSE);
133 gtk_entry_set_completion(entry, completion);
135 return GTK_COMBO_BOX_ENTRY(combo_box);
143 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
145 GtkWidget *view, *vbox;
146 GtkWidget *frame = gtk_frame_new(label);
148 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
149 box = gtk_hbox_new(FALSE, 3);
150 gtk_container_add(GTK_CONTAINER(frame), box);
151 vbox = gtk_vbox_new(FALSE, 3);
152 gtk_container_add(GTK_CONTAINER(box), vbox);
154 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
155 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
156 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
158 view = gtk_text_view_new();
159 if (writable == READ_ONLY) {
160 gtk_widget_set_can_focus(view, FALSE);
161 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
162 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
164 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
165 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
166 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
167 return GTK_TEXT_VIEW(view);
174 } found_string_entry;
175 static GtkTreeIter string_entry_location;
177 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
179 const char *string = data;
183 gtk_tree_model_get(model, iter, 0, &entry, -1);
184 cmp = strcmp(entry, string);
186 /* Stop. The entry is bigger than the new one */
192 found_string_entry = MATCH_EXACT;
196 string_entry_location = *iter;
197 found_string_entry = MATCH_AFTER;
201 static int match_list(GtkListStore *list, const char *string)
203 found_string_entry = MATCH_PREPEND;
204 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
205 return found_string_entry;
208 static void add_string_list_entry(const char *string, GtkListStore *list)
210 GtkTreeIter *iter, loc;
212 if (!string || !*string)
215 switch (match_list(list, string)) {
222 iter = &string_entry_location;
225 gtk_list_store_insert_after(list, &loc, iter);
226 gtk_list_store_set(list, &loc, 0, string, -1);
229 void add_people(const char *string)
231 add_string_list_entry(string, people_list);
234 void add_location(const char *string)
236 add_string_list_entry(string, location_list);
240 GtkComboBoxEntry *location, *divemaster, *buddy;
244 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
246 char *old_text, *new_text;
249 new_text = get_combo_box_entry_text(info->location, &dive->location);
251 add_location(new_text);
255 new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
257 add_people(new_text);
261 new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
263 add_people(new_text);
267 old_text = dive->notes;
268 dive->notes = get_text(info->notes);
269 if (text_changed(old_text,dive->notes))
275 mark_divelist_changed(TRUE);
276 flush_divelist(dive);
280 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
284 info->location = text_entry(box, "Location", location_list, dive->location);
286 hbox = gtk_hbox_new(FALSE, 3);
287 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
289 info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
290 info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
292 info->notes = text_view(box, "Notes", READ_WRITE);
293 if (dive->notes && *dive->notes)
294 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
297 int edit_dive_info(struct dive *dive)
300 GtkWidget *dialog, *vbox;
301 struct dive_info info;
306 dialog = gtk_dialog_new_with_buttons("Dive Info",
307 GTK_WINDOW(main_window),
308 GTK_DIALOG_DESTROY_WITH_PARENT,
309 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
310 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
313 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
314 dive_info_widget(vbox, dive, &info);
316 gtk_widget_show_all(dialog);
317 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
319 save_dive_info_changes(dive, &info);
321 gtk_widget_destroy(dialog);
326 GtkWidget *extended_dive_info_widget(void)
328 GtkWidget *vbox, *hbox;
329 vbox = gtk_vbox_new(FALSE, 6);
331 people_list = gtk_list_store_new(1, G_TYPE_STRING);
332 location_list = gtk_list_store_new(1, G_TYPE_STRING);
334 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
335 location = text_value(vbox, "Location");
337 hbox = gtk_hbox_new(FALSE, 3);
338 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
340 divemaster = text_value(hbox, "Divemaster");
341 buddy = text_value(hbox, "Buddy");
343 notes = text_view(vbox, "Notes", READ_ONLY);