]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
10a31f55813dd57160d5c796b02aace0323e73d6
[ext/subsurface.git] / info.c
1 /* info.c */
2 /* creates the UI for the info frame - 
3  * controlled through the following interfaces:
4  * 
5  * void show_dive_info(struct dive *dive)
6  *
7  * called from gtk-ui:
8  * GtkWidget *extended_dive_info_widget(void)
9  */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <ctype.h>
15
16 #include "dive.h"
17 #include "display.h"
18 #include "display-gtk.h"
19 #include "divelist.h"
20
21 static GtkEntry *location, *buddy, *divemaster, *rating;
22 static GtkTextView *notes;
23 static GtkListStore *location_list, *people_list, *star_list;
24
25 static char *get_text(GtkTextView *view)
26 {
27         GtkTextBuffer *buffer;
28         GtkTextIter start;
29         GtkTextIter end;
30
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);
35 }
36
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)
40 {
41         return (old && strcmp(old,new)) ||
42                 (!old && strcmp("",new));
43 }
44
45 static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp)
46 {
47         char *old = *textp;
48         const gchar *new;
49         GtkEntry *entry;
50
51         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
52         new = gtk_entry_get_text(entry);
53         while (isspace(*new))
54                 new++;
55         if (!text_changed(old,new))
56                 return NULL;
57         free(old);
58         *textp = strdup(new);
59         return *textp;
60 }
61
62 #define SET_TEXT_VALUE(x) \
63         gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
64
65 static int divename(char *buf, size_t size, struct dive *dive)
66 {
67         struct tm *tm = gmtime(&dive->when);
68         return snprintf(buf, size, "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
69                 dive->number,
70                 weekday(tm->tm_wday),
71                 tm->tm_mon+1, tm->tm_mday,
72                 tm->tm_year+1900,
73                 tm->tm_hour, tm->tm_min);
74 }
75
76 void show_dive_info(struct dive *dive)
77 {
78         const char *text;
79         char buffer[80];
80
81         /* dive number and location (or lacking that, the date) go in the window title */
82         text = dive->location;
83         if (!text)
84                 text = "";
85         if (*text) {
86                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
87         } else {
88                 divename(buffer, sizeof(buffer), dive);
89         }
90         text = buffer;
91         if (!dive->number)
92                 text += 10;     /* Skip the "Dive #0 - " part */
93         gtk_window_set_title(GTK_WINDOW(main_window), text);
94
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);
101 }
102
103 static int delete_dive_info(struct dive *dive)
104 {
105         int success;
106         GtkWidget *dialog;
107
108         if (!dive)
109                 return 0;
110
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,
116                 NULL);
117
118         gtk_widget_show_all(dialog);
119         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
120         if (success) {
121                 delete_dive(dive);
122                 mark_divelist_changed(TRUE);
123                 dive_list_update_dives();
124         }
125
126         gtk_widget_destroy(dialog);
127
128         return success;
129 }
130
131 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
132 {
133         edit_dive_info(current_dive);
134 }
135
136 static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data)
137 {
138         delete_dive_info(current_dive);
139 }
140
141 static void add_menu_item(GtkMenu *menu, const char *label, void (*cb)(GtkMenuItem *, gpointer))
142 {
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);
147 }
148
149 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
150 {
151         add_menu_item(menu, "Delete", info_menu_delete_cb);
152         add_menu_item(menu, "Edit", info_menu_edit_cb);
153 }
154
155 static GtkEntry *text_value(GtkWidget *box, const char *label)
156 {
157         GtkWidget *widget;
158         GtkWidget *frame = gtk_frame_new(label);
159
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);
167 }
168
169 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
170 {
171         GtkEntry *entry;
172         GtkWidget *combo_box;
173         GtkWidget *frame = gtk_frame_new(label);
174         GtkEntryCompletion *completion;
175
176         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
177
178         combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
179         gtk_container_add(GTK_CONTAINER(frame), combo_box);
180
181         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
182         if (text && *text)
183                 gtk_entry_set_text(entry, text);
184
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);
192
193         return GTK_COMBO_BOX_ENTRY(combo_box);
194 }
195
196 enum writable {
197         READ_ONLY,
198         READ_WRITE
199 };
200
201 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
202 {
203         GtkWidget *view, *vbox;
204         GtkWidget *frame = gtk_frame_new(label);
205
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);
211
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);
215
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);
222         }
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);
227 }
228
229 static enum {
230         MATCH_EXACT,
231         MATCH_PREPEND,
232         MATCH_AFTER
233 } found_string_entry;
234 static GtkTreeIter string_entry_location;
235
236 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
237 {
238         const char *string = data;
239         char *entry;
240         int cmp;
241
242         gtk_tree_model_get(model, iter, 0, &entry, -1);
243         cmp = strcmp(entry, string);
244
245         /* Stop. The entry is bigger than the new one */
246         if (cmp > 0)
247                 return TRUE;
248
249         /* Exact match */
250         if (!cmp) {
251                 found_string_entry = MATCH_EXACT;
252                 return TRUE;
253         }
254
255         string_entry_location = *iter;
256         found_string_entry = MATCH_AFTER;
257         return FALSE;
258 }
259
260 static int match_list(GtkListStore *list, const char *string)
261 {
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;
265 }
266
267 static void add_string_list_entry(const char *string, GtkListStore *list)
268 {
269         GtkTreeIter *iter, loc;
270
271         if (!string || !*string)
272                 return;
273
274         switch (match_list(list, string)) {
275         case MATCH_EXACT:
276                 return;
277         case MATCH_PREPEND:
278                 iter = NULL;
279                 break;
280         case MATCH_AFTER:
281                 iter = &string_entry_location;
282                 break;
283         }
284         gtk_list_store_insert_after(list, &loc, iter);
285         gtk_list_store_set(list, &loc, 0, string, -1);
286 }
287
288 void add_people(const char *string)
289 {
290         add_string_list_entry(string, people_list);
291 }
292
293 void add_location(const char *string)
294 {
295         add_string_list_entry(string, location_list);
296 }
297
298 static int get_rating(const char *string)
299 {
300         int rating_val = 0;
301         int i;
302
303         for (i = 0; i <= 5; i++)
304                 if (!strcmp(star_strings[i],string))
305                         rating_val = i;
306         return rating_val;
307 }
308
309 struct dive_info {
310         GtkComboBoxEntry *location, *divemaster, *buddy, *rating;
311         GtkTextView *notes;
312 };
313
314 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
315 {
316         char *old_text, *new_text;
317         char *rating_string;
318         int changed = 0;
319
320         new_text = get_combo_box_entry_text(info->location, &dive->location);
321         if (new_text) {
322                 add_location(new_text);
323                 changed = 1;
324         }
325
326         new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
327         if (new_text) {
328                 add_people(new_text);
329                 changed = 1;
330         }
331
332         new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
333         if (new_text) {
334                 add_people(new_text);
335                 changed = 1;
336         }
337
338         rating_string = strdup(star_strings[dive->rating]);
339         new_text = get_combo_box_entry_text(info->rating, &rating_string);
340         if (new_text) {
341                 dive->rating = get_rating(rating_string);
342                 free(rating_string);
343                 changed =1;
344         }
345
346         old_text = dive->notes;
347         dive->notes = get_text(info->notes);
348         if (text_changed(old_text,dive->notes))
349                 changed = 1;
350         if (old_text)
351                 g_free(old_text);
352
353         if (changed) {
354                 mark_divelist_changed(TRUE);
355                 update_dive(dive);
356         }
357 }
358
359 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
360 {
361         GtkWidget *hbox, *label, *cylinder, *frame;
362         char buffer[80];
363
364         divename(buffer, sizeof(buffer), dive);
365         label = gtk_label_new(buffer);
366         gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
367
368         info->location = text_entry(box, "Location", location_list, dive->location);
369
370         hbox = gtk_hbox_new(FALSE, 3);
371         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
372
373         info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
374         info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
375
376         hbox = gtk_hbox_new(FALSE, 3);
377         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
378
379         info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
380
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);
384
385         hbox = gtk_hbox_new(FALSE, 3);
386         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
387
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);
392 }
393
394 int edit_dive_info(struct dive *dive)
395 {
396         int success;
397         GtkWidget *dialog, *vbox;
398         struct dive_info info;
399
400         if (!dive)
401                 return 0;
402
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,
408                 NULL);
409
410         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
411         dive_info_widget(vbox, dive, &info);
412
413         gtk_widget_show_all(dialog);
414         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
415         if (success)
416                 save_dive_info_changes(dive, &info);
417
418         gtk_widget_destroy(dialog);
419
420         return success;
421 }
422
423 /* Fixme - should do at least depths too - a dive without a depth is kind of pointless */
424 static time_t dive_time_widget(struct dive *dive)
425 {
426         GtkWidget *dialog;
427         GtkWidget *cal, *hbox, *vbox;
428         GtkWidget *h, *m;
429         GtkWidget *duration;
430         GtkWidget *label;
431         guint yval, mval, dval;
432         struct tm tm;
433         int success;
434
435         dialog = gtk_dialog_new_with_buttons("Date and Time",
436                 GTK_WINDOW(main_window),
437                 GTK_DIALOG_DESTROY_WITH_PARENT,
438                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
439                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
440                 NULL);
441
442         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
443
444         /* Calendar hbox */
445         hbox = gtk_hbox_new(0, 3);
446         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
447
448         cal = gtk_calendar_new();
449         gtk_box_pack_start(GTK_BOX(hbox), cal, FALSE, TRUE, 0);
450
451         /* Time/duration hbox */
452         hbox = gtk_hbox_new(0, 3);
453         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
454
455         h = gtk_spin_button_new_with_range (0.0, 23.0, 1.0);
456         m = gtk_spin_button_new_with_range (0.0, 59.0, 1.0);
457
458         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(h), TRUE);
459         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(m), TRUE);
460
461         duration = gtk_spin_button_new_with_range (0.0, 1000.0, 1.0);
462
463         gtk_box_pack_start(GTK_BOX(hbox), h, FALSE, FALSE, 0);
464         label = gtk_label_new(":");
465         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
466         gtk_box_pack_start(GTK_BOX(hbox), m, FALSE, FALSE, 0);
467
468         label = gtk_label_new("   Duration:");
469         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
470         gtk_box_pack_start(GTK_BOX(hbox), duration, FALSE, FALSE, 0);
471
472         gtk_widget_show_all(dialog);
473         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
474         if (!success) {
475                 gtk_widget_destroy(dialog);
476                 return 0;
477         }
478
479         memset(&tm, 0, sizeof(tm));
480         gtk_calendar_get_date(GTK_CALENDAR(cal), &yval, &mval, &dval);
481         tm.tm_year = yval;
482         tm.tm_mon = mval;
483         tm.tm_mday = dval;
484
485         tm.tm_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(h));
486         tm.tm_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m));
487
488         dive->duration.seconds = gtk_spin_button_get_value(GTK_SPIN_BUTTON(duration))*60;
489
490         gtk_widget_destroy(dialog);
491         dive->when = utc_mktime(&tm);
492
493         return 1;
494 }
495
496 int add_new_dive(struct dive *dive)
497 {
498         if (!dive)
499                 return 0;
500
501         if (!dive_time_widget(dive))
502                 return 0;
503
504         return edit_dive_info(dive);
505 }
506
507 GtkWidget *extended_dive_info_widget(void)
508 {
509         GtkWidget *vbox, *hbox;
510         vbox = gtk_vbox_new(FALSE, 6);
511
512         people_list = gtk_list_store_new(1, G_TYPE_STRING);
513         location_list = gtk_list_store_new(1, G_TYPE_STRING);
514         star_list = gtk_list_store_new(1, G_TYPE_STRING);
515         add_string_list_entry(ZERO_STARS, star_list);
516         add_string_list_entry(ONE_STARS, star_list);
517         add_string_list_entry(TWO_STARS, star_list);
518         add_string_list_entry(THREE_STARS, star_list);
519         add_string_list_entry(FOUR_STARS, star_list);
520         add_string_list_entry(FIVE_STARS, star_list);
521
522         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
523         location = text_value(vbox, "Location");
524
525         hbox = gtk_hbox_new(FALSE, 3);
526         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
527
528         divemaster = text_value(hbox, "Divemaster");
529         buddy = text_value(hbox, "Buddy");
530
531         hbox = gtk_hbox_new(FALSE, 3);
532         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
533
534         rating = text_value(hbox, "Rating");
535
536         notes = text_view(vbox, "Notes", READ_ONLY);
537         return vbox;
538 }