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 static int divename(char *buf, size_t size, struct dive *dive)
67 struct tm *tm = gmtime(&dive->when);
68 return snprintf(buf, size, "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
71 tm->tm_mon+1, tm->tm_mday,
73 tm->tm_hour, tm->tm_min);
76 void show_dive_info(struct dive *dive)
81 /* dive number and location (or lacking that, the date) go in the window title */
82 text = dive->location;
86 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
88 divename(buffer, sizeof(buffer), dive);
92 text += 10; /* Skip the "Dive #0 - " part */
93 gtk_window_set_title(GTK_WINDOW(main_window), text);
95 SET_TEXT_VALUE(divemaster);
96 SET_TEXT_VALUE(buddy);
97 SET_TEXT_VALUE(location);
98 gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
99 dive && dive->notes ? dive->notes : "", -1);
102 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
104 edit_dive_info(current_dive);
107 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
109 GtkWidget *item = gtk_menu_item_new_with_label("Edit");
110 g_signal_connect(item, "activate", G_CALLBACK(info_menu_edit_cb), NULL);
111 gtk_widget_show(item); /* Yes, really */
112 gtk_menu_prepend(menu, item);
115 static GtkEntry *text_value(GtkWidget *box, const char *label)
118 GtkWidget *frame = gtk_frame_new(label);
120 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
121 widget = gtk_entry_new();
122 gtk_widget_set_can_focus(widget, FALSE);
123 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
124 gtk_container_add(GTK_CONTAINER(frame), widget);
125 g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
126 return GTK_ENTRY(widget);
129 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
132 GtkWidget *combo_box;
133 GtkWidget *frame = gtk_frame_new(label);
134 GtkEntryCompletion *completion;
136 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
138 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
139 gtk_container_add(GTK_CONTAINER(frame), combo_box);
141 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
143 gtk_entry_set_text(entry, text);
145 completion = gtk_entry_completion_new();
146 gtk_entry_completion_set_text_column(completion, 0);
147 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
148 gtk_entry_completion_set_inline_completion(completion, TRUE);
149 gtk_entry_completion_set_inline_selection(completion, TRUE);
150 gtk_entry_completion_set_popup_single_match(completion, FALSE);
151 gtk_entry_set_completion(entry, completion);
153 return GTK_COMBO_BOX_ENTRY(combo_box);
161 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
163 GtkWidget *view, *vbox;
164 GtkWidget *frame = gtk_frame_new(label);
166 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
167 box = gtk_hbox_new(FALSE, 3);
168 gtk_container_add(GTK_CONTAINER(frame), box);
169 vbox = gtk_vbox_new(FALSE, 3);
170 gtk_container_add(GTK_CONTAINER(box), vbox);
172 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
173 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
174 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
176 view = gtk_text_view_new();
177 if (writable == READ_ONLY) {
178 gtk_widget_set_can_focus(view, FALSE);
179 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
180 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
181 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
183 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
184 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
185 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
186 return GTK_TEXT_VIEW(view);
193 } found_string_entry;
194 static GtkTreeIter string_entry_location;
196 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
198 const char *string = data;
202 gtk_tree_model_get(model, iter, 0, &entry, -1);
203 cmp = strcmp(entry, string);
205 /* Stop. The entry is bigger than the new one */
211 found_string_entry = MATCH_EXACT;
215 string_entry_location = *iter;
216 found_string_entry = MATCH_AFTER;
220 static int match_list(GtkListStore *list, const char *string)
222 found_string_entry = MATCH_PREPEND;
223 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
224 return found_string_entry;
227 static void add_string_list_entry(const char *string, GtkListStore *list)
229 GtkTreeIter *iter, loc;
231 if (!string || !*string)
234 switch (match_list(list, string)) {
241 iter = &string_entry_location;
244 gtk_list_store_insert_after(list, &loc, iter);
245 gtk_list_store_set(list, &loc, 0, string, -1);
248 void add_people(const char *string)
250 add_string_list_entry(string, people_list);
253 void add_location(const char *string)
255 add_string_list_entry(string, location_list);
259 GtkComboBoxEntry *location, *divemaster, *buddy;
263 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
265 char *old_text, *new_text;
268 new_text = get_combo_box_entry_text(info->location, &dive->location);
270 add_location(new_text);
274 new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
276 add_people(new_text);
280 new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
282 add_people(new_text);
286 old_text = dive->notes;
287 dive->notes = get_text(info->notes);
288 if (text_changed(old_text,dive->notes))
294 mark_divelist_changed(TRUE);
295 flush_divelist(dive);
299 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
301 GtkWidget *hbox, *label, *cylinder, *frame;
304 divename(buffer, sizeof(buffer), dive);
305 label = gtk_label_new(buffer);
306 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
308 info->location = text_entry(box, "Location", location_list, dive->location);
310 hbox = gtk_hbox_new(FALSE, 3);
311 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
313 info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
314 info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
316 info->notes = text_view(box, "Notes", READ_WRITE);
317 if (dive->notes && *dive->notes)
318 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
320 frame = gtk_frame_new("Cylinder");
321 cylinder = cylinder_list_widget();
322 gtk_container_add(GTK_CONTAINER(frame), cylinder);
323 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
326 int edit_dive_info(struct dive *dive)
329 GtkWidget *dialog, *vbox;
330 struct dive_info info;
335 dialog = gtk_dialog_new_with_buttons("Dive Info",
336 GTK_WINDOW(main_window),
337 GTK_DIALOG_DESTROY_WITH_PARENT,
338 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
339 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
342 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
343 dive_info_widget(vbox, dive, &info);
345 gtk_widget_show_all(dialog);
346 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
348 save_dive_info_changes(dive, &info);
350 gtk_widget_destroy(dialog);
355 GtkWidget *extended_dive_info_widget(void)
357 GtkWidget *vbox, *hbox;
358 vbox = gtk_vbox_new(FALSE, 6);
360 people_list = gtk_list_store_new(1, G_TYPE_STRING);
361 location_list = gtk_list_store_new(1, G_TYPE_STRING);
363 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
364 location = text_value(vbox, "Location");
366 hbox = gtk_hbox_new(FALSE, 3);
367 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
369 divemaster = text_value(hbox, "Divemaster");
370 buddy = text_value(hbox, "Buddy");
372 notes = text_view(vbox, "Notes", READ_ONLY);