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, *rating;
22 static GtkTextView *notes;
23 static GtkListStore *location_list, *people_list, *star_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_entry_set_text(rating, star_strings[dive->rating]);
99 gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
100 dive && dive->notes ? dive->notes : "", -1);
103 static int delete_dive_info(struct dive *dive)
111 dialog = gtk_dialog_new_with_buttons("Delete Dive",
112 GTK_WINDOW(main_window),
113 GTK_DIALOG_DESTROY_WITH_PARENT,
114 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
115 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
118 gtk_widget_show_all(dialog);
119 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
122 mark_divelist_changed(TRUE);
123 dive_list_update_dives();
126 gtk_widget_destroy(dialog);
131 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
133 edit_dive_info(current_dive);
136 static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data)
138 delete_dive_info(current_dive);
141 static void add_menu_item(GtkMenu *menu, const char *label, void (*cb)(GtkMenuItem *, gpointer))
143 GtkWidget *item = gtk_menu_item_new_with_label(label);
144 g_signal_connect(item, "activate", G_CALLBACK(cb), NULL);
145 gtk_widget_show(item); /* Yes, really */
146 gtk_menu_prepend(menu, item);
149 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
151 add_menu_item(menu, "Delete", info_menu_delete_cb);
152 add_menu_item(menu, "Edit", info_menu_edit_cb);
155 static GtkEntry *text_value(GtkWidget *box, const char *label)
158 GtkWidget *frame = gtk_frame_new(label);
160 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
161 widget = gtk_entry_new();
162 gtk_widget_set_can_focus(widget, FALSE);
163 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
164 gtk_container_add(GTK_CONTAINER(frame), widget);
165 g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
166 return GTK_ENTRY(widget);
169 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
172 GtkWidget *combo_box;
173 GtkWidget *frame = gtk_frame_new(label);
174 GtkEntryCompletion *completion;
176 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
178 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
179 gtk_container_add(GTK_CONTAINER(frame), combo_box);
181 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
183 gtk_entry_set_text(entry, text);
185 completion = gtk_entry_completion_new();
186 gtk_entry_completion_set_text_column(completion, 0);
187 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
188 gtk_entry_completion_set_inline_completion(completion, TRUE);
189 gtk_entry_completion_set_inline_selection(completion, TRUE);
190 gtk_entry_completion_set_popup_single_match(completion, FALSE);
191 gtk_entry_set_completion(entry, completion);
193 return GTK_COMBO_BOX_ENTRY(combo_box);
201 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
203 GtkWidget *view, *vbox;
204 GtkWidget *frame = gtk_frame_new(label);
206 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
207 box = gtk_hbox_new(FALSE, 3);
208 gtk_container_add(GTK_CONTAINER(frame), box);
209 vbox = gtk_vbox_new(FALSE, 3);
210 gtk_container_add(GTK_CONTAINER(box), vbox);
212 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
213 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
214 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
216 view = gtk_text_view_new();
217 if (writable == READ_ONLY) {
218 gtk_widget_set_can_focus(view, FALSE);
219 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
220 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
221 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
223 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
224 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
225 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
226 return GTK_TEXT_VIEW(view);
233 } found_string_entry;
234 static GtkTreeIter string_entry_location;
236 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
238 const char *string = data;
242 gtk_tree_model_get(model, iter, 0, &entry, -1);
243 cmp = strcmp(entry, string);
245 /* Stop. The entry is bigger than the new one */
251 found_string_entry = MATCH_EXACT;
255 string_entry_location = *iter;
256 found_string_entry = MATCH_AFTER;
260 static int match_list(GtkListStore *list, const char *string)
262 found_string_entry = MATCH_PREPEND;
263 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
264 return found_string_entry;
267 static void add_string_list_entry(const char *string, GtkListStore *list)
269 GtkTreeIter *iter, loc;
271 if (!string || !*string)
274 switch (match_list(list, string)) {
281 iter = &string_entry_location;
284 gtk_list_store_insert_after(list, &loc, iter);
285 gtk_list_store_set(list, &loc, 0, string, -1);
288 void add_people(const char *string)
290 add_string_list_entry(string, people_list);
293 void add_location(const char *string)
295 add_string_list_entry(string, location_list);
298 static int get_rating(const char *string)
303 for (i = 0; i <= 5; i++)
304 if (!strcmp(star_strings[i],string))
310 GtkComboBoxEntry *location, *divemaster, *buddy, *rating;
314 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
316 char *old_text, *new_text;
320 new_text = get_combo_box_entry_text(info->location, &dive->location);
322 add_location(new_text);
326 new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
328 add_people(new_text);
332 new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
334 add_people(new_text);
338 rating_string = strdup(star_strings[dive->rating]);
339 new_text = get_combo_box_entry_text(info->rating, &rating_string);
341 dive->rating = get_rating(rating_string);
346 old_text = dive->notes;
347 dive->notes = get_text(info->notes);
348 if (text_changed(old_text,dive->notes))
354 mark_divelist_changed(TRUE);
359 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
361 GtkWidget *hbox, *label, *cylinder, *frame;
364 divename(buffer, sizeof(buffer), dive);
365 label = gtk_label_new(buffer);
366 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
368 info->location = text_entry(box, "Location", location_list, dive->location);
370 hbox = gtk_hbox_new(FALSE, 3);
371 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
373 info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
374 info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
376 hbox = gtk_hbox_new(FALSE, 3);
377 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
379 info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
381 info->notes = text_view(box, "Notes", READ_WRITE);
382 if (dive->notes && *dive->notes)
383 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
385 hbox = gtk_hbox_new(FALSE, 3);
386 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
388 frame = gtk_frame_new("Cylinder");
389 cylinder = cylinder_list_widget();
390 gtk_container_add(GTK_CONTAINER(frame), cylinder);
391 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
394 int edit_dive_info(struct dive *dive)
397 GtkWidget *dialog, *vbox;
398 struct dive_info info;
403 dialog = gtk_dialog_new_with_buttons("Dive Info",
404 GTK_WINDOW(main_window),
405 GTK_DIALOG_DESTROY_WITH_PARENT,
406 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
407 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
410 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
411 dive_info_widget(vbox, dive, &info);
413 gtk_widget_show_all(dialog);
414 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
416 save_dive_info_changes(dive, &info);
418 gtk_widget_destroy(dialog);
423 GtkWidget *extended_dive_info_widget(void)
425 GtkWidget *vbox, *hbox;
426 vbox = gtk_vbox_new(FALSE, 6);
428 people_list = gtk_list_store_new(1, G_TYPE_STRING);
429 location_list = gtk_list_store_new(1, G_TYPE_STRING);
430 star_list = gtk_list_store_new(1, G_TYPE_STRING);
431 add_string_list_entry(ZERO_STARS, star_list);
432 add_string_list_entry(ONE_STARS, star_list);
433 add_string_list_entry(TWO_STARS, star_list);
434 add_string_list_entry(THREE_STARS, star_list);
435 add_string_list_entry(FOUR_STARS, star_list);
436 add_string_list_entry(FIVE_STARS, star_list);
438 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
439 location = text_value(vbox, "Location");
441 hbox = gtk_hbox_new(FALSE, 3);
442 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
444 divemaster = text_value(hbox, "Divemaster");
445 buddy = text_value(hbox, "Buddy");
447 hbox = gtk_hbox_new(FALSE, 3);
448 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
450 rating = text_value(hbox, "Rating");
452 notes = text_view(vbox, "Notes", READ_ONLY);