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