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 *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp)
52 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
53 new = gtk_entry_get_text(entry);
56 if (!text_changed(old,new))
63 #define SET_TEXT_VALUE(x) \
64 gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
66 static int divename(char *buf, size_t size, struct dive *dive)
68 struct tm *tm = gmtime(&dive->when);
69 return snprintf(buf, size, "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
72 tm->tm_mon+1, tm->tm_mday,
74 tm->tm_hour, tm->tm_min);
77 void show_dive_info(struct dive *dive)
82 /* dive number and location (or lacking that, the date) go in the window title */
83 text = dive->location;
87 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
89 divename(buffer, sizeof(buffer), dive);
93 text += 10; /* Skip the "Dive #0 - " part */
94 gtk_window_set_title(GTK_WINDOW(main_window), text);
96 SET_TEXT_VALUE(divemaster);
97 SET_TEXT_VALUE(buddy);
98 SET_TEXT_VALUE(location);
100 gtk_entry_set_text(rating, star_strings[dive->rating]);
101 gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
102 dive && dive->notes ? dive->notes : "", -1);
105 static int delete_dive_info(struct dive *dive)
113 dialog = gtk_dialog_new_with_buttons("Delete Dive",
114 GTK_WINDOW(main_window),
115 GTK_DIALOG_DESTROY_WITH_PARENT,
116 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
117 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
120 gtk_widget_show_all(dialog);
121 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
124 mark_divelist_changed(TRUE);
125 dive_list_update_dives();
128 gtk_widget_destroy(dialog);
133 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
135 edit_dive_info(current_dive);
138 static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data)
140 delete_dive_info(current_dive);
143 static void add_menu_item(GtkMenu *menu, const char *label, void (*cb)(GtkMenuItem *, gpointer))
145 GtkWidget *item = gtk_menu_item_new_with_label(label);
146 g_signal_connect(item, "activate", G_CALLBACK(cb), NULL);
147 gtk_widget_show(item); /* Yes, really */
148 gtk_menu_prepend(menu, item);
151 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
153 add_menu_item(menu, "Delete", info_menu_delete_cb);
154 add_menu_item(menu, "Edit", info_menu_edit_cb);
157 static GtkEntry *text_value(GtkWidget *box, const char *label)
160 GtkWidget *frame = gtk_frame_new(label);
162 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
163 widget = gtk_entry_new();
164 gtk_widget_set_can_focus(widget, FALSE);
165 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
166 gtk_container_add(GTK_CONTAINER(frame), widget);
167 g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
168 return GTK_ENTRY(widget);
171 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
174 GtkWidget *combo_box;
175 GtkWidget *frame = gtk_frame_new(label);
176 GtkEntryCompletion *completion;
178 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
180 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
181 gtk_container_add(GTK_CONTAINER(frame), combo_box);
183 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
185 gtk_entry_set_text(entry, text);
187 completion = gtk_entry_completion_new();
188 gtk_entry_completion_set_text_column(completion, 0);
189 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
190 gtk_entry_completion_set_inline_completion(completion, TRUE);
191 gtk_entry_completion_set_inline_selection(completion, TRUE);
192 gtk_entry_completion_set_popup_single_match(completion, FALSE);
193 gtk_entry_set_completion(entry, completion);
195 return GTK_COMBO_BOX_ENTRY(combo_box);
203 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
205 GtkWidget *view, *vbox;
206 GtkWidget *frame = gtk_frame_new(label);
208 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
209 box = gtk_hbox_new(FALSE, 3);
210 gtk_container_add(GTK_CONTAINER(frame), box);
211 vbox = gtk_vbox_new(FALSE, 3);
212 gtk_container_add(GTK_CONTAINER(box), vbox);
214 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
215 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
216 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
218 view = gtk_text_view_new();
219 if (writable == READ_ONLY) {
220 gtk_widget_set_can_focus(view, FALSE);
221 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
222 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
223 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
225 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
226 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
227 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
228 return GTK_TEXT_VIEW(view);
235 } found_string_entry;
236 static GtkTreeIter string_entry_location;
238 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
240 const char *string = data;
244 gtk_tree_model_get(model, iter, 0, &entry, -1);
245 cmp = strcmp(entry, string);
249 /* Stop. The entry is bigger than the new one */
255 found_string_entry = MATCH_EXACT;
259 string_entry_location = *iter;
260 found_string_entry = MATCH_AFTER;
264 static int match_list(GtkListStore *list, const char *string)
266 found_string_entry = MATCH_PREPEND;
267 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
268 return found_string_entry;
271 static void add_string_list_entry(const char *string, GtkListStore *list)
273 GtkTreeIter *iter, loc;
275 if (!string || !*string)
278 switch (match_list(list, string)) {
285 iter = &string_entry_location;
288 gtk_list_store_insert_after(list, &loc, iter);
289 gtk_list_store_set(list, &loc, 0, string, -1);
292 void add_people(const char *string)
294 add_string_list_entry(string, people_list);
297 void add_location(const char *string)
299 add_string_list_entry(string, location_list);
302 void add_suit(const char *string)
304 add_string_list_entry(string, suit_list);
307 static int get_rating(const char *string)
312 for (i = 0; i <= 5; i++)
313 if (!strcmp(star_strings[i],string))
319 GtkComboBoxEntry *location, *divemaster, *buddy, *rating, *suit;
323 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
325 char *old_text, *new_text;
329 new_text = get_combo_box_entry_text(info->location, &dive->location);
331 add_location(new_text);
335 new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
337 add_people(new_text);
341 new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
343 add_people(new_text);
347 new_text = get_combo_box_entry_text(info->suit, &dive->suit);
353 rating_string = strdup(star_strings[dive->rating]);
354 new_text = get_combo_box_entry_text(info->rating, &rating_string);
356 dive->rating = get_rating(rating_string);
361 old_text = dive->notes;
362 dive->notes = get_text(info->notes);
363 if (text_changed(old_text,dive->notes))
369 mark_divelist_changed(TRUE);
374 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
376 GtkWidget *hbox, *label, *cylinder, *frame;
379 divename(buffer, sizeof(buffer), dive);
380 label = gtk_label_new(buffer);
381 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
383 info->location = text_entry(box, "Location", location_list, dive->location);
385 hbox = gtk_hbox_new(FALSE, 3);
386 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
388 info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
389 info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
391 hbox = gtk_hbox_new(FALSE, 3);
392 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
394 info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
395 info->suit = text_entry(hbox, "Suit", suit_list, dive->suit);
397 info->notes = text_view(box, "Notes", READ_WRITE);
398 if (dive->notes && *dive->notes)
399 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
401 hbox = gtk_hbox_new(FALSE, 3);
402 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
404 frame = gtk_frame_new("Cylinder");
405 cylinder = cylinder_list_widget();
406 gtk_container_add(GTK_CONTAINER(frame), cylinder);
407 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
410 int edit_dive_info(struct dive *dive)
413 GtkWidget *dialog, *vbox;
414 struct dive_info info;
419 dialog = gtk_dialog_new_with_buttons("Dive Info",
420 GTK_WINDOW(main_window),
421 GTK_DIALOG_DESTROY_WITH_PARENT,
422 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
423 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
426 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
427 dive_info_widget(vbox, dive, &info);
429 gtk_widget_show_all(dialog);
430 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
432 save_dive_info_changes(dive, &info);
434 gtk_widget_destroy(dialog);
439 static GtkWidget *frame_box(GtkWidget *vbox, const char *fmt, ...)
443 GtkWidget *frame, *hbox;
446 vsnprintf(buffer, sizeof(buffer), fmt, ap);
449 frame = gtk_frame_new(buffer);
450 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
451 hbox = gtk_hbox_new(0, 3);
452 gtk_container_add(GTK_CONTAINER(frame), hbox);
456 /* Fixme - should do at least depths too - a dive without a depth is kind of pointless */
457 static time_t dive_time_widget(struct dive *dive)
460 GtkWidget *cal, *hbox, *vbox, *box;
462 GtkWidget *duration, *depth;
464 guint yval, mval, dval;
469 double depthinterval, val;
471 dialog = gtk_dialog_new_with_buttons("Date and Time",
472 GTK_WINDOW(main_window),
473 GTK_DIALOG_DESTROY_WITH_PARENT,
474 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
475 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
478 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
481 hbox = frame_box(vbox, "Date:");
482 cal = gtk_calendar_new();
483 gtk_box_pack_start(GTK_BOX(hbox), cal, FALSE, TRUE, 0);
486 hbox = frame_box(vbox, "Time");
488 h = gtk_spin_button_new_with_range (0.0, 23.0, 1.0);
489 m = gtk_spin_button_new_with_range (0.0, 59.0, 1.0);
491 gettimeofday(&tv, NULL);
493 tmp = localtime(&time);
494 gtk_spin_button_set_value(GTK_SPIN_BUTTON(h), tmp->tm_hour);
495 gtk_spin_button_set_value(GTK_SPIN_BUTTON(m), (tmp->tm_min / 5)*5);
497 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(h), TRUE);
498 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(m), TRUE);
500 gtk_box_pack_end(GTK_BOX(hbox), m, FALSE, FALSE, 0);
501 label = gtk_label_new(":");
502 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
503 gtk_box_pack_end(GTK_BOX(hbox), h, FALSE, FALSE, 0);
505 hbox = gtk_hbox_new(TRUE, 3);
506 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
509 box = frame_box(hbox, "Duration (min)");
510 duration = gtk_spin_button_new_with_range (0.0, 1000.0, 1.0);
511 gtk_box_pack_end(GTK_BOX(box), duration, FALSE, FALSE, 0);
514 box = frame_box(hbox, "Depth (%s):", output_units.length == FEET ? "ft" : "m");
515 if (output_units.length == FEET) {
520 depth = gtk_spin_button_new_with_range (0.0, 1000.0, depthinterval);
521 gtk_box_pack_end(GTK_BOX(box), depth, FALSE, FALSE, 0);
523 /* All done, show it and wait for editing */
524 gtk_widget_show_all(dialog);
525 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
527 gtk_widget_destroy(dialog);
531 memset(&tm, 0, sizeof(tm));
532 gtk_calendar_get_date(GTK_CALENDAR(cal), &yval, &mval, &dval);
537 tm.tm_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(h));
538 tm.tm_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m));
540 val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(depth));
541 if (output_units.length == FEET) {
542 dive->maxdepth.mm = feet_to_mm(val);
544 dive->maxdepth.mm = val * 1000 + 0.5;
547 dive->duration.seconds = gtk_spin_button_get_value(GTK_SPIN_BUTTON(duration))*60;
549 gtk_widget_destroy(dialog);
550 dive->when = utc_mktime(&tm);
555 int add_new_dive(struct dive *dive)
560 if (!dive_time_widget(dive))
563 return edit_dive_info(dive);
566 GtkWidget *extended_dive_info_widget(void)
568 GtkWidget *vbox, *hbox;
569 vbox = gtk_vbox_new(FALSE, 6);
571 people_list = gtk_list_store_new(1, G_TYPE_STRING);
572 location_list = gtk_list_store_new(1, G_TYPE_STRING);
573 star_list = gtk_list_store_new(1, G_TYPE_STRING);
574 add_string_list_entry(ZERO_STARS, star_list);
575 add_string_list_entry(ONE_STARS, star_list);
576 add_string_list_entry(TWO_STARS, star_list);
577 add_string_list_entry(THREE_STARS, star_list);
578 add_string_list_entry(FOUR_STARS, star_list);
579 add_string_list_entry(FIVE_STARS, star_list);
580 suit_list = gtk_list_store_new(1, G_TYPE_STRING);
582 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
583 location = text_value(vbox, "Location");
585 hbox = gtk_hbox_new(FALSE, 3);
586 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
588 divemaster = text_value(hbox, "Divemaster");
589 buddy = text_value(hbox, "Buddy");
591 hbox = gtk_hbox_new(FALSE, 3);
592 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
594 rating = text_value(hbox, "Rating");
595 suit = text_value(hbox, "Suit");
597 notes = text_view(vbox, "Notes", READ_ONLY);