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 the master string didn't change, don't change other dives either! */
87 if (!text_changed(master,new))
89 if (!text_changed(old,new))
96 #define SET_TEXT_VALUE(x) \
97 gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
99 static int divename(char *buf, size_t size, struct dive *dive)
101 struct tm *tm = gmtime(&dive->when);
102 return snprintf(buf, size, "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
104 weekday(tm->tm_wday),
105 tm->tm_mon+1, tm->tm_mday,
107 tm->tm_hour, tm->tm_min);
110 void show_dive_info(struct dive *dive)
115 /* dive number and location (or lacking that, the date) go in the window title */
116 text = dive->location;
120 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
122 divename(buffer, sizeof(buffer), dive);
126 text += 10; /* Skip the "Dive #0 - " part */
127 gtk_window_set_title(GTK_WINDOW(main_window), text);
129 SET_TEXT_VALUE(divemaster);
130 SET_TEXT_VALUE(buddy);
131 SET_TEXT_VALUE(location);
132 SET_TEXT_VALUE(suit);
133 gtk_entry_set_text(rating, star_strings[dive->rating]);
134 gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
135 dive && dive->notes ? dive->notes : "", -1);
138 static int delete_dive_info(struct dive *dive)
146 dialog = gtk_dialog_new_with_buttons("Delete Dive",
147 GTK_WINDOW(main_window),
148 GTK_DIALOG_DESTROY_WITH_PARENT,
149 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
150 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
153 gtk_widget_show_all(dialog);
154 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
157 mark_divelist_changed(TRUE);
158 dive_list_update_dives();
161 gtk_widget_destroy(dialog);
166 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
168 edit_multi_dive_info(amount_selected, selectiontracker);
171 static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data)
173 /* this needs to delete all the selected dives as well, I guess? */
174 delete_dive_info(current_dive);
177 static void add_menu_item(GtkMenu *menu, const char *label, void (*cb)(GtkMenuItem *, gpointer))
179 GtkWidget *item = gtk_menu_item_new_with_label(label);
180 g_signal_connect(item, "activate", G_CALLBACK(cb), NULL);
181 gtk_widget_show(item); /* Yes, really */
182 gtk_menu_prepend(menu, item);
185 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
187 add_menu_item(menu, "Delete", info_menu_delete_cb);
188 add_menu_item(menu, "Edit", info_menu_edit_cb);
191 static GtkEntry *text_value(GtkWidget *box, const char *label)
194 GtkWidget *frame = gtk_frame_new(label);
196 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
197 widget = gtk_entry_new();
198 gtk_widget_set_can_focus(widget, FALSE);
199 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
200 gtk_container_add(GTK_CONTAINER(frame), widget);
201 g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
202 return GTK_ENTRY(widget);
205 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
208 GtkWidget *combo_box;
209 GtkWidget *frame = gtk_frame_new(label);
210 GtkEntryCompletion *completion;
212 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
214 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
215 gtk_container_add(GTK_CONTAINER(frame), combo_box);
217 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
219 gtk_entry_set_text(entry, text);
221 completion = gtk_entry_completion_new();
222 gtk_entry_completion_set_text_column(completion, 0);
223 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
224 gtk_entry_completion_set_inline_completion(completion, TRUE);
225 gtk_entry_completion_set_inline_selection(completion, TRUE);
226 gtk_entry_completion_set_popup_single_match(completion, FALSE);
227 gtk_entry_set_completion(entry, completion);
229 return GTK_COMBO_BOX_ENTRY(combo_box);
237 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
239 GtkWidget *view, *vbox;
240 GtkWidget *frame = gtk_frame_new(label);
242 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
243 box = gtk_hbox_new(FALSE, 3);
244 gtk_container_add(GTK_CONTAINER(frame), box);
245 vbox = gtk_vbox_new(FALSE, 3);
246 gtk_container_add(GTK_CONTAINER(box), vbox);
248 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
249 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
250 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
252 view = gtk_text_view_new();
253 if (writable == READ_ONLY) {
254 gtk_widget_set_can_focus(view, FALSE);
255 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
256 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
257 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
259 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
260 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
261 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
262 return GTK_TEXT_VIEW(view);
269 } found_string_entry;
270 static GtkTreeIter string_entry_location;
272 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
274 const char *string = data;
278 gtk_tree_model_get(model, iter, 0, &entry, -1);
279 cmp = strcmp(entry, string);
283 /* Stop. The entry is bigger than the new one */
289 found_string_entry = MATCH_EXACT;
293 string_entry_location = *iter;
294 found_string_entry = MATCH_AFTER;
298 static int match_list(GtkListStore *list, const char *string)
300 found_string_entry = MATCH_PREPEND;
301 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
302 return found_string_entry;
305 static void add_string_list_entry(const char *string, GtkListStore *list)
307 GtkTreeIter *iter, loc;
309 if (!string || !*string)
312 switch (match_list(list, string)) {
319 iter = &string_entry_location;
322 gtk_list_store_insert_after(list, &loc, iter);
323 gtk_list_store_set(list, &loc, 0, string, -1);
326 void add_people(const char *string)
328 add_string_list_entry(string, people_list);
331 void add_location(const char *string)
333 add_string_list_entry(string, location_list);
336 void add_suit(const char *string)
338 add_string_list_entry(string, suit_list);
341 static int get_rating(const char *string)
346 for (i = 0; i <= 5; i++)
347 if (!strcmp(star_strings[i],string))
353 GtkComboBoxEntry *location, *divemaster, *buddy, *rating, *suit;
357 static void save_dive_info_changes(struct dive *dive, struct dive *master, struct dive_info *info)
359 char *old_text, *new_text;
363 new_text = get_combo_box_entry_text(info->location, &dive->location, master->location);
365 add_location(new_text);
369 new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster, master->divemaster);
371 add_people(new_text);
375 new_text = get_combo_box_entry_text(info->buddy, &dive->buddy, master->buddy);
377 add_people(new_text);
381 new_text = get_combo_box_entry_text(info->suit, &dive->suit, master->suit);
387 rating_string = strdup(star_strings[dive->rating]);
388 new_text = get_combo_box_entry_text(info->rating, &rating_string, star_strings[master->rating]);
390 dive->rating = get_rating(rating_string);
396 old_text = dive->notes;
397 dive->notes = get_text(info->notes);
398 if (text_changed(old_text,dive->notes))
404 mark_divelist_changed(TRUE);
409 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info, gboolean multi)
411 GtkWidget *hbox, *label, *frame, *equipment;
412 char buffer[80] = "Edit multiple dives";
415 divename(buffer, sizeof(buffer), dive);
416 label = gtk_label_new(buffer);
417 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
419 info->location = text_entry(box, "Location", location_list, dive->location);
421 hbox = gtk_hbox_new(FALSE, 3);
422 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
424 info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
425 info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
427 hbox = gtk_hbox_new(FALSE, 3);
428 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
430 info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
431 info->suit = text_entry(hbox, "Suit", suit_list, dive->suit);
433 /* only show notes if editing a single dive */
437 info->notes = text_view(box, "Notes", READ_WRITE);
438 if (dive->notes && *dive->notes)
439 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
441 hbox = gtk_hbox_new(FALSE, 3);
442 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
444 /* create a secondary Equipment widget */
445 frame = gtk_frame_new("Equipment");
446 equipment = equipment_widget(W_IDX_SECONDARY);
447 gtk_container_add(GTK_CONTAINER(frame), equipment);
448 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
451 /* we use these to find out if we edited the cylinder or weightsystem entries */
452 static cylinder_t remember_cyl[MAX_CYLINDERS];
453 static weightsystem_t remember_ws[MAX_WEIGHTSYSTEMS];
455 void save_equipment_data(struct dive *dive)
458 memcpy(remember_cyl, dive->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS);
459 memcpy(remember_ws, dive->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS);
463 void update_equipment_data(struct dive *dive, struct dive *master)
467 if (memcmp(remember_cyl, master->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS)) {
468 memcpy(dive->cylinder, master->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS);
470 if (memcmp(remember_ws, master->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS)) {
471 memcpy(dive->weightsystem, master->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS);
475 int edit_multi_dive_info(int nr, int *indices)
478 GtkWidget *dialog, *vbox;
479 struct dive_info info;
484 dialog = gtk_dialog_new_with_buttons("Dive Info",
485 GTK_WINDOW(main_window),
486 GTK_DIALOG_DESTROY_WITH_PARENT,
487 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
488 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
491 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
492 /* SCARY STUFF - IS THIS THE BEST WAY TO DO THIS???
494 * current_dive is one of our selected dives - and that is
495 * the one that is used to pre-fill the edit widget. Its
496 * data is used as the starting point for all selected dives
497 * I think it would be better to somehow collect and combine
498 * info from all the selected dives */
499 master = current_dive;
500 dive_info_widget(vbox, master, &info, (nr > 1));
501 show_dive_equipment(master, W_IDX_SECONDARY);
502 save_equipment_data(master);
503 gtk_widget_show_all(dialog);
504 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
506 /* Update the other non-current dives first */
507 for (i = 0; i < nr; i++) {
508 int idx = indices[i];
509 struct dive *dive = get_dive(idx);
511 if (!dive || dive == master)
513 /* copy all "info" fields */
514 save_dive_info_changes(dive, master, &info);
515 /* copy the cylinders / weightsystems */
516 update_equipment_data(dive, master);
517 /* this is extremely inefficient... it loops through all
518 dives to find the right one - but we KNOW the index already */
519 flush_divelist(dive);
522 /* Update the master dive last! */
523 save_dive_info_changes(master, master, &info);
524 update_equipment_data(master, master);
525 flush_divelist(master);
527 gtk_widget_destroy(dialog);
532 int edit_dive_info(struct dive *dive)
539 return edit_multi_dive_info(1, &idx);
542 static GtkWidget *frame_box(GtkWidget *vbox, const char *fmt, ...)
546 GtkWidget *frame, *hbox;
549 vsnprintf(buffer, sizeof(buffer), fmt, ap);
552 frame = gtk_frame_new(buffer);
553 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
554 hbox = gtk_hbox_new(0, 3);
555 gtk_container_add(GTK_CONTAINER(frame), hbox);
559 /* Fixme - should do at least depths too - a dive without a depth is kind of pointless */
560 static time_t dive_time_widget(struct dive *dive)
563 GtkWidget *cal, *hbox, *vbox, *box;
565 GtkWidget *duration, *depth;
567 guint yval, mval, dval;
572 double depthinterval, val;
574 dialog = gtk_dialog_new_with_buttons("Date and Time",
575 GTK_WINDOW(main_window),
576 GTK_DIALOG_DESTROY_WITH_PARENT,
577 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
578 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
581 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
584 hbox = frame_box(vbox, "Date:");
585 cal = gtk_calendar_new();
586 gtk_box_pack_start(GTK_BOX(hbox), cal, FALSE, TRUE, 0);
589 hbox = frame_box(vbox, "Time");
591 h = gtk_spin_button_new_with_range (0.0, 23.0, 1.0);
592 m = gtk_spin_button_new_with_range (0.0, 59.0, 1.0);
594 gettimeofday(&tv, NULL);
596 tmp = localtime(&time);
597 gtk_spin_button_set_value(GTK_SPIN_BUTTON(h), tmp->tm_hour);
598 gtk_spin_button_set_value(GTK_SPIN_BUTTON(m), (tmp->tm_min / 5)*5);
600 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(h), TRUE);
601 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(m), TRUE);
603 gtk_box_pack_end(GTK_BOX(hbox), m, FALSE, FALSE, 0);
604 label = gtk_label_new(":");
605 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
606 gtk_box_pack_end(GTK_BOX(hbox), h, FALSE, FALSE, 0);
608 hbox = gtk_hbox_new(TRUE, 3);
609 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
612 box = frame_box(hbox, "Duration (min)");
613 duration = gtk_spin_button_new_with_range (0.0, 1000.0, 1.0);
614 gtk_box_pack_end(GTK_BOX(box), duration, FALSE, FALSE, 0);
617 box = frame_box(hbox, "Depth (%s):", output_units.length == FEET ? "ft" : "m");
618 if (output_units.length == FEET) {
623 depth = gtk_spin_button_new_with_range (0.0, 1000.0, depthinterval);
624 gtk_box_pack_end(GTK_BOX(box), depth, FALSE, FALSE, 0);
626 /* All done, show it and wait for editing */
627 gtk_widget_show_all(dialog);
628 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
630 gtk_widget_destroy(dialog);
634 memset(&tm, 0, sizeof(tm));
635 gtk_calendar_get_date(GTK_CALENDAR(cal), &yval, &mval, &dval);
640 tm.tm_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(h));
641 tm.tm_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m));
643 val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(depth));
644 if (output_units.length == FEET) {
645 dive->maxdepth.mm = feet_to_mm(val);
647 dive->maxdepth.mm = val * 1000 + 0.5;
650 dive->duration.seconds = gtk_spin_button_get_value(GTK_SPIN_BUTTON(duration))*60;
652 gtk_widget_destroy(dialog);
653 dive->when = utc_mktime(&tm);
658 int add_new_dive(struct dive *dive)
663 if (!dive_time_widget(dive))
666 return edit_dive_info(dive);
669 GtkWidget *extended_dive_info_widget(void)
671 GtkWidget *vbox, *hbox;
672 vbox = gtk_vbox_new(FALSE, 6);
674 people_list = gtk_list_store_new(1, G_TYPE_STRING);
675 location_list = gtk_list_store_new(1, G_TYPE_STRING);
676 star_list = gtk_list_store_new(1, G_TYPE_STRING);
677 add_string_list_entry(ZERO_STARS, star_list);
678 add_string_list_entry(ONE_STARS, star_list);
679 add_string_list_entry(TWO_STARS, star_list);
680 add_string_list_entry(THREE_STARS, star_list);
681 add_string_list_entry(FOUR_STARS, star_list);
682 add_string_list_entry(FIVE_STARS, star_list);
683 suit_list = gtk_list_store_new(1, G_TYPE_STRING);
685 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
686 location = text_value(vbox, "Location");
688 hbox = gtk_hbox_new(FALSE, 3);
689 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
691 divemaster = text_value(hbox, "Divemaster");
692 buddy = text_value(hbox, "Buddy");
694 hbox = gtk_hbox_new(FALSE, 3);
695 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
697 rating = text_value(hbox, "Rating");
698 suit = text_value(hbox, "Suit");
700 notes = text_view(vbox, "Notes", READ_ONLY);