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 void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
105 edit_dive_info(current_dive);
108 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
110 GtkWidget *item = gtk_menu_item_new_with_label("Edit");
111 g_signal_connect(item, "activate", G_CALLBACK(info_menu_edit_cb), NULL);
112 gtk_widget_show(item); /* Yes, really */
113 gtk_menu_prepend(menu, item);
116 static GtkEntry *text_value(GtkWidget *box, const char *label)
119 GtkWidget *frame = gtk_frame_new(label);
121 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
122 widget = gtk_entry_new();
123 gtk_widget_set_can_focus(widget, FALSE);
124 gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
125 gtk_container_add(GTK_CONTAINER(frame), widget);
126 g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
127 return GTK_ENTRY(widget);
130 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
133 GtkWidget *combo_box;
134 GtkWidget *frame = gtk_frame_new(label);
135 GtkEntryCompletion *completion;
137 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
139 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
140 gtk_container_add(GTK_CONTAINER(frame), combo_box);
142 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
144 gtk_entry_set_text(entry, text);
146 completion = gtk_entry_completion_new();
147 gtk_entry_completion_set_text_column(completion, 0);
148 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
149 gtk_entry_completion_set_inline_completion(completion, TRUE);
150 gtk_entry_completion_set_inline_selection(completion, TRUE);
151 gtk_entry_completion_set_popup_single_match(completion, FALSE);
152 gtk_entry_set_completion(entry, completion);
154 return GTK_COMBO_BOX_ENTRY(combo_box);
162 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
164 GtkWidget *view, *vbox;
165 GtkWidget *frame = gtk_frame_new(label);
167 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
168 box = gtk_hbox_new(FALSE, 3);
169 gtk_container_add(GTK_CONTAINER(frame), box);
170 vbox = gtk_vbox_new(FALSE, 3);
171 gtk_container_add(GTK_CONTAINER(box), vbox);
173 GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
174 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
175 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
177 view = gtk_text_view_new();
178 if (writable == READ_ONLY) {
179 gtk_widget_set_can_focus(view, FALSE);
180 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
181 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
182 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
184 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
185 gtk_container_add(GTK_CONTAINER(scrolled_window), view);
186 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
187 return GTK_TEXT_VIEW(view);
194 } found_string_entry;
195 static GtkTreeIter string_entry_location;
197 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
199 const char *string = data;
203 gtk_tree_model_get(model, iter, 0, &entry, -1);
204 cmp = strcmp(entry, string);
206 /* Stop. The entry is bigger than the new one */
212 found_string_entry = MATCH_EXACT;
216 string_entry_location = *iter;
217 found_string_entry = MATCH_AFTER;
221 static int match_list(GtkListStore *list, const char *string)
223 found_string_entry = MATCH_PREPEND;
224 gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
225 return found_string_entry;
228 static void add_string_list_entry(const char *string, GtkListStore *list)
230 GtkTreeIter *iter, loc;
232 if (!string || !*string)
235 switch (match_list(list, string)) {
242 iter = &string_entry_location;
245 gtk_list_store_insert_after(list, &loc, iter);
246 gtk_list_store_set(list, &loc, 0, string, -1);
249 void add_people(const char *string)
251 add_string_list_entry(string, people_list);
254 void add_location(const char *string)
256 add_string_list_entry(string, location_list);
259 static int get_rating(const char *string)
264 for (i = 0; i <= 5; i++)
265 if (!strcmp(star_strings[i],string))
271 GtkComboBoxEntry *location, *divemaster, *buddy, *rating;
275 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
277 char *old_text, *new_text;
281 new_text = get_combo_box_entry_text(info->location, &dive->location);
283 add_location(new_text);
287 new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
289 add_people(new_text);
293 new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
295 add_people(new_text);
299 rating_string = strdup(star_strings[dive->rating]);
300 new_text = get_combo_box_entry_text(info->rating, &rating_string);
302 dive->rating = get_rating(rating_string);
307 old_text = dive->notes;
308 dive->notes = get_text(info->notes);
309 if (text_changed(old_text,dive->notes))
315 mark_divelist_changed(TRUE);
316 flush_divelist(dive);
320 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
322 GtkWidget *hbox, *label, *cylinder, *frame;
325 divename(buffer, sizeof(buffer), dive);
326 label = gtk_label_new(buffer);
327 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
329 info->location = text_entry(box, "Location", location_list, dive->location);
331 hbox = gtk_hbox_new(FALSE, 3);
332 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
334 info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
335 info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
337 hbox = gtk_hbox_new(FALSE, 3);
338 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
340 info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
342 info->notes = text_view(box, "Notes", READ_WRITE);
343 if (dive->notes && *dive->notes)
344 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
346 hbox = gtk_hbox_new(FALSE, 3);
347 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
349 frame = gtk_frame_new("Cylinder");
350 cylinder = cylinder_list_widget();
351 gtk_container_add(GTK_CONTAINER(frame), cylinder);
352 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
355 int edit_dive_info(struct dive *dive)
358 GtkWidget *dialog, *vbox;
359 struct dive_info info;
364 dialog = gtk_dialog_new_with_buttons("Dive Info",
365 GTK_WINDOW(main_window),
366 GTK_DIALOG_DESTROY_WITH_PARENT,
367 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
368 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
371 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
372 dive_info_widget(vbox, dive, &info);
374 gtk_widget_show_all(dialog);
375 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
377 save_dive_info_changes(dive, &info);
379 gtk_widget_destroy(dialog);
384 GtkWidget *extended_dive_info_widget(void)
386 GtkWidget *vbox, *hbox;
387 vbox = gtk_vbox_new(FALSE, 6);
389 people_list = gtk_list_store_new(1, G_TYPE_STRING);
390 location_list = gtk_list_store_new(1, G_TYPE_STRING);
391 star_list = gtk_list_store_new(1, G_TYPE_STRING);
392 add_string_list_entry(ZERO_STARS, star_list);
393 add_string_list_entry(ONE_STARS, star_list);
394 add_string_list_entry(TWO_STARS, star_list);
395 add_string_list_entry(THREE_STARS, star_list);
396 add_string_list_entry(FOUR_STARS, star_list);
397 add_string_list_entry(FIVE_STARS, star_list);
399 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
400 location = text_value(vbox, "Location");
402 hbox = gtk_hbox_new(FALSE, 3);
403 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
405 divemaster = text_value(hbox, "Divemaster");
406 buddy = text_value(hbox, "Buddy");
408 hbox = gtk_hbox_new(FALSE, 3);
409 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
411 rating = text_value(hbox, "Rating");
413 notes = text_view(vbox, "Notes", READ_ONLY);