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)
19 #include "display-gtk.h"
22 static GtkEntry *location, *buddy, *divemaster, *rating, *suit;
23 static GtkTextView *notes;
24 static GtkListStore *location_list, *people_list, *star_list, *suit_list;
26 static char *get_text(GtkTextView *view)
28 GtkTextBuffer *buffer;
32 buffer = gtk_text_view_get_buffer(view);
33 gtk_text_buffer_get_start_iter(buffer, &start);
34 gtk_text_buffer_get_end_iter(buffer, &end);
35 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
38 /* old is NULL or a valid string, new is a valid string
39 * NOTW: NULL and "" need to be treated as "unchanged" */
40 static int text_changed(const char *old, const char *new)
42 return (old && strcmp(old,new)) ||
43 (!old && strcmp("",new));
46 static char *skip_space(char *str)
58 * Get the string from a combo box.
60 * The "master" string is the string of the current dive - we only consider it
61 * changed if the old string is either empty, or matches that master string.
63 static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp, char *master)
69 old = skip_space(old);
70 master = skip_space(master);
73 * If we had a master string, and it doesn't match our old
74 * string, we will always pick the old value (it means that
75 * we're editing another dive's info that already had a
79 if (strcmp(master, old))
82 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
83 new = gtk_entry_get_text(entry);
86 if (!text_changed(old,new))
93 #define SET_TEXT_VALUE(x) \
94 gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
96 static int divename(char *buf, size_t size, struct dive *dive)
98 struct tm *tm = gmtime(&dive->when);
99 return snprintf(buf, size, "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
101 weekday(tm->tm_wday),
102 tm->tm_mon+1, tm->tm_mday,
104 tm->tm_hour, tm->tm_min);
107 void show_dive_info(struct dive *dive)
112 /* dive number and location (or lacking that, the date) go in the window title */
113 text = dive->location;
117 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
119 divename(buffer, sizeof(buffer), dive);
123 text += 10; /* Skip the "Dive #0 - " part */
124 gtk_window_set_title(GTK_WINDOW(main_window), text);
126 SET_TEXT_VALUE(divemaster);
127 SET_TEXT_VALUE(buddy);
128 SET_TEXT_VALUE(location);
129 SET_TEXT_VALUE(suit);
130 gtk_entry_set_text(rating, star_strings[dive->rating]);
131 gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
132 dive && dive->notes ? dive->notes : "", -1);
135 static int delete_dive_info(struct dive *dive)
143 dialog = gtk_dialog_new_with_buttons("Delete Dive",
144 GTK_WINDOW(main_window),
145 GTK_DIALOG_DESTROY_WITH_PARENT,
146 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
147 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
150 gtk_widget_show_all(dialog);
151 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
154 mark_divelist_changed(TRUE);
155 dive_list_update_dives();
158 gtk_widget_destroy(dialog);
163 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
165 edit_multi_dive_info(amount_selected, selectiontracker);
168 static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data)
170 /* this needs to delete all the selected dives as well, I guess? */
171 delete_dive_info(current_dive);
174 static void add_menu_item(GtkMenu *menu, const char *label, void (*cb)(GtkMenuItem *, gpointer))
176 GtkWidget *item = gtk_menu_item_new_with_label(label);
177 g_signal_connect(item, "activate", G_CALLBACK(cb), NULL);
178 gtk_widget_show(item); /* Yes, really */
179 gtk_menu_prepend(menu, item);
182 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
184 add_menu_item(menu, "Delete", info_menu_delete_cb);
185 add_menu_item(menu, "Edit", info_menu_edit_cb);
188 static GtkEntry *text_value(GtkWidget *box, const char *label)
191 GtkWidget *frame = gtk_frame_new(label);
193 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
194 widget = gtk_entry_new();
195 gtk_widget_set_can_focus(widget, FALSE);
196 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
197 gtk_container_add(GTK_CONTAINER(frame), widget);
198 g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
199 return GTK_ENTRY(widget);
202 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
205 GtkWidget *combo_box;
206 GtkWidget *frame = gtk_frame_new(label);
207 GtkEntryCompletion *completion;
209 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
211 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
212 gtk_container_add(GTK_CONTAINER(frame), combo_box);
214 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
216 gtk_entry_set_text(entry, text);
218 completion = gtk_entry_completion_new();
219 gtk_entry_completion_set_text_column(completion, 0);
220 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
221 gtk_entry_completion_set_inline_completion(completion, TRUE);
222 gtk_entry_completion_set_inline_selection(completion, TRUE);
223 gtk_entry_completion_set_popup_single_match(completion, FALSE);
224 gtk_entry_set_completion(entry, completion);
226 return GTK_COMBO_BOX_ENTRY(combo_box);
234 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
236 GtkWidget *view, *vbox;
237 GtkWidget *frame = gtk_frame_new(label);
239 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
240 box = gtk_hbox_new(FALSE, 3);
241 gtk_container_add(GTK_CONTAINER(frame), box);
242 vbox = gtk_vbox_new(FALSE, 3);
243 gtk_container_add(GTK_CONTAINER(box), vbox);
245 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
246 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
247 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
249 view = gtk_text_view_new();
250 if (writable == READ_ONLY) {
251 gtk_widget_set_can_focus(view, FALSE);
252 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
253 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
254 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
256 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
257 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
258 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
259 return GTK_TEXT_VIEW(view);
266 } found_string_entry;
267 static GtkTreeIter string_entry_location;
269 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
271 const char *string = data;
275 gtk_tree_model_get(model, iter, 0, &entry, -1);
276 cmp = strcmp(entry, string);
280 /* Stop. The entry is bigger than the new one */
286 found_string_entry = MATCH_EXACT;
290 string_entry_location = *iter;
291 found_string_entry = MATCH_AFTER;
295 static int match_list(GtkListStore *list, const char *string)
297 found_string_entry = MATCH_PREPEND;
298 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
299 return found_string_entry;
302 static void add_string_list_entry(const char *string, GtkListStore *list)
304 GtkTreeIter *iter, loc;
306 if (!string || !*string)
309 switch (match_list(list, string)) {
316 iter = &string_entry_location;
319 gtk_list_store_insert_after(list, &loc, iter);
320 gtk_list_store_set(list, &loc, 0, string, -1);
323 void add_people(const char *string)
325 add_string_list_entry(string, people_list);
328 void add_location(const char *string)
330 add_string_list_entry(string, location_list);
333 void add_suit(const char *string)
335 add_string_list_entry(string, suit_list);
338 static int get_rating(const char *string)
343 for (i = 0; i <= 5; i++)
344 if (!strcmp(star_strings[i],string))
350 GtkComboBoxEntry *location, *divemaster, *buddy, *rating, *suit;
354 static void save_dive_info_changes(struct dive *dive, struct dive *master, struct dive_info *info)
356 char *old_text, *new_text;
360 new_text = get_combo_box_entry_text(info->location, &dive->location, master->location);
362 add_location(new_text);
366 new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster, master->divemaster);
368 add_people(new_text);
372 new_text = get_combo_box_entry_text(info->buddy, &dive->buddy, master->buddy);
374 add_people(new_text);
378 new_text = get_combo_box_entry_text(info->suit, &dive->suit, master->suit);
384 rating_string = strdup(star_strings[dive->rating]);
385 new_text = get_combo_box_entry_text(info->rating, &rating_string, star_strings[master->rating]);
387 dive->rating = get_rating(rating_string);
393 old_text = dive->notes;
394 dive->notes = get_text(info->notes);
395 if (text_changed(old_text,dive->notes))
401 mark_divelist_changed(TRUE);
406 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info, gboolean multi)
408 GtkWidget *hbox, *label, *frame, *equipment;
409 char buffer[80] = "Edit multiple dives";
412 divename(buffer, sizeof(buffer), dive);
413 label = gtk_label_new(buffer);
414 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
416 info->location = text_entry(box, "Location", location_list, dive->location);
418 hbox = gtk_hbox_new(FALSE, 3);
419 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
421 info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
422 info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
424 hbox = gtk_hbox_new(FALSE, 3);
425 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
427 info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
428 info->suit = text_entry(hbox, "Suit", suit_list, dive->suit);
430 /* only show notes if editing a single dive */
434 info->notes = text_view(box, "Notes", READ_WRITE);
435 if (dive->notes && *dive->notes)
436 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
438 hbox = gtk_hbox_new(FALSE, 3);
439 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
441 /* create a secondary Equipment widget */
442 frame = gtk_frame_new("Equipment");
443 equipment = equipment_widget(W_IDX_SECONDARY);
444 gtk_container_add(GTK_CONTAINER(frame), equipment);
445 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
448 /* we use these to find out if we edited the cylinder or weightsystem entries */
449 static cylinder_t remember_cyl[MAX_CYLINDERS];
450 static weightsystem_t remember_ws[MAX_WEIGHTSYSTEMS];
452 void save_equipment_data(struct dive *dive)
455 memcpy(remember_cyl, dive->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS);
456 memcpy(remember_ws, dive->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS);
460 void update_equipment_data(struct dive *dive, struct dive *master)
464 if (memcmp(remember_cyl, master->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS)) {
465 memcpy(dive->cylinder, master->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS);
467 if (memcmp(remember_ws, master->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS)) {
468 memcpy(dive->weightsystem, master->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS);
472 int edit_multi_dive_info(int nr, int *indices)
475 GtkWidget *dialog, *vbox;
476 struct dive_info info;
481 dialog = gtk_dialog_new_with_buttons("Dive Info",
482 GTK_WINDOW(main_window),
483 GTK_DIALOG_DESTROY_WITH_PARENT,
484 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
485 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
488 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
489 /* SCARY STUFF - IS THIS THE BEST WAY TO DO THIS???
491 * current_dive is one of our selected dives - and that is
492 * the one that is used to pre-fill the edit widget. Its
493 * data is used as the starting point for all selected dives
494 * I think it would be better to somehow collect and combine
495 * info from all the selected dives */
496 master = current_dive;
497 dive_info_widget(vbox, master, &info, (nr > 1));
498 show_dive_equipment(master, W_IDX_SECONDARY);
499 save_equipment_data(master);
500 gtk_widget_show_all(dialog);
501 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
503 /* Update the other non-current dives first */
504 for (i = 0; i < nr; i++) {
505 int idx = indices[i];
506 struct dive *dive = get_dive(idx);
508 if (!dive || dive == master)
510 /* copy all "info" fields */
511 save_dive_info_changes(dive, master, &info);
512 /* copy the cylinders / weightsystems */
513 update_equipment_data(dive, master);
514 /* this is extremely inefficient... it loops through all
515 dives to find the right one - but we KNOW the index already */
516 flush_divelist(dive);
519 /* Update the master dive last! */
520 save_dive_info_changes(master, master, &info);
521 update_equipment_data(master, master);
522 flush_divelist(master);
524 gtk_widget_destroy(dialog);
529 int edit_dive_info(struct dive *dive)
536 return edit_multi_dive_info(1, &idx);
539 static GtkWidget *frame_box(GtkWidget *vbox, const char *fmt, ...)
543 GtkWidget *frame, *hbox;
546 vsnprintf(buffer, sizeof(buffer), fmt, ap);
549 frame = gtk_frame_new(buffer);
550 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
551 hbox = gtk_hbox_new(0, 3);
552 gtk_container_add(GTK_CONTAINER(frame), hbox);
556 /* Fixme - should do at least depths too - a dive without a depth is kind of pointless */
557 static time_t dive_time_widget(struct dive *dive)
560 GtkWidget *cal, *hbox, *vbox, *box;
562 GtkWidget *duration, *depth;
564 guint yval, mval, dval;
569 double depthinterval, val;
571 dialog = gtk_dialog_new_with_buttons("Date and Time",
572 GTK_WINDOW(main_window),
573 GTK_DIALOG_DESTROY_WITH_PARENT,
574 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
575 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
578 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
581 hbox = frame_box(vbox, "Date:");
582 cal = gtk_calendar_new();
583 gtk_box_pack_start(GTK_BOX(hbox), cal, FALSE, TRUE, 0);
586 hbox = frame_box(vbox, "Time");
588 h = gtk_spin_button_new_with_range (0.0, 23.0, 1.0);
589 m = gtk_spin_button_new_with_range (0.0, 59.0, 1.0);
591 gettimeofday(&tv, NULL);
593 tmp = localtime(&time);
594 gtk_spin_button_set_value(GTK_SPIN_BUTTON(h), tmp->tm_hour);
595 gtk_spin_button_set_value(GTK_SPIN_BUTTON(m), (tmp->tm_min / 5)*5);
597 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(h), TRUE);
598 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(m), TRUE);
600 gtk_box_pack_end(GTK_BOX(hbox), m, FALSE, FALSE, 0);
601 label = gtk_label_new(":");
602 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
603 gtk_box_pack_end(GTK_BOX(hbox), h, FALSE, FALSE, 0);
605 hbox = gtk_hbox_new(TRUE, 3);
606 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
609 box = frame_box(hbox, "Duration (min)");
610 duration = gtk_spin_button_new_with_range (0.0, 1000.0, 1.0);
611 gtk_box_pack_end(GTK_BOX(box), duration, FALSE, FALSE, 0);
614 box = frame_box(hbox, "Depth (%s):", output_units.length == FEET ? "ft" : "m");
615 if (output_units.length == FEET) {
620 depth = gtk_spin_button_new_with_range (0.0, 1000.0, depthinterval);
621 gtk_box_pack_end(GTK_BOX(box), depth, FALSE, FALSE, 0);
623 /* All done, show it and wait for editing */
624 gtk_widget_show_all(dialog);
625 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
627 gtk_widget_destroy(dialog);
631 memset(&tm, 0, sizeof(tm));
632 gtk_calendar_get_date(GTK_CALENDAR(cal), &yval, &mval, &dval);
637 tm.tm_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(h));
638 tm.tm_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m));
640 val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(depth));
641 if (output_units.length == FEET) {
642 dive->maxdepth.mm = feet_to_mm(val);
644 dive->maxdepth.mm = val * 1000 + 0.5;
647 dive->duration.seconds = gtk_spin_button_get_value(GTK_SPIN_BUTTON(duration))*60;
649 gtk_widget_destroy(dialog);
650 dive->when = utc_mktime(&tm);
655 int add_new_dive(struct dive *dive)
660 if (!dive_time_widget(dive))
663 return edit_dive_info(dive);
666 GtkWidget *extended_dive_info_widget(void)
668 GtkWidget *vbox, *hbox;
669 vbox = gtk_vbox_new(FALSE, 6);
671 people_list = gtk_list_store_new(1, G_TYPE_STRING);
672 location_list = gtk_list_store_new(1, G_TYPE_STRING);
673 star_list = gtk_list_store_new(1, G_TYPE_STRING);
674 add_string_list_entry(ZERO_STARS, star_list);
675 add_string_list_entry(ONE_STARS, star_list);
676 add_string_list_entry(TWO_STARS, star_list);
677 add_string_list_entry(THREE_STARS, star_list);
678 add_string_list_entry(FOUR_STARS, star_list);
679 add_string_list_entry(FIVE_STARS, star_list);
680 suit_list = gtk_list_store_new(1, G_TYPE_STRING);
682 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
683 location = text_value(vbox, "Location");
685 hbox = gtk_hbox_new(FALSE, 3);
686 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
688 divemaster = text_value(hbox, "Divemaster");
689 buddy = text_value(hbox, "Buddy");
691 hbox = gtk_hbox_new(FALSE, 3);
692 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
694 rating = text_value(hbox, "Rating");
695 suit = text_value(hbox, "Suit");
697 notes = text_view(vbox, "Notes", READ_ONLY);