]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Changes to menu icons
[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;
23 static GtkTextView *notes;
24 static GtkListStore *location_list, *people_list, *star_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         gtk_entry_set_text(rating, star_strings[dive->rating]);
100         gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
101                 dive && dive->notes ? dive->notes : "", -1);
102 }
103
104 static int delete_dive_info(struct dive *dive)
105 {
106         int success;
107         GtkWidget *dialog;
108
109         if (!dive)
110                 return 0;
111
112         dialog = gtk_dialog_new_with_buttons("Delete Dive",
113                 GTK_WINDOW(main_window),
114                 GTK_DIALOG_DESTROY_WITH_PARENT,
115                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
116                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
117                 NULL);
118
119         gtk_widget_show_all(dialog);
120         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
121         if (success) {
122                 delete_dive(dive);
123                 mark_divelist_changed(TRUE);
124                 dive_list_update_dives();
125         }
126
127         gtk_widget_destroy(dialog);
128
129         return success;
130 }
131
132 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
133 {
134         edit_dive_info(current_dive);
135 }
136
137 static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data)
138 {
139         delete_dive_info(current_dive);
140 }
141
142 static void add_menu_item(GtkMenu *menu, const char *label, const char *icon, void (*cb)(GtkMenuItem *, gpointer))
143 {
144         GtkWidget *item;
145         if (icon) {
146                 GtkWidget *image;
147                 item = gtk_image_menu_item_new_with_label(label);
148                 image = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU);
149                 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
150         } else {
151                 item = gtk_menu_item_new_with_label(label);
152         }
153         g_signal_connect(item, "activate", G_CALLBACK(cb), NULL);
154         gtk_widget_show(item); /* Yes, really */
155         gtk_menu_prepend(menu, item);
156 }
157
158 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
159 {
160         add_menu_item(menu, "Delete", GTK_STOCK_DELETE, info_menu_delete_cb);
161         add_menu_item(menu, "Edit", GTK_STOCK_EDIT, info_menu_edit_cb);
162 }
163
164 static GtkEntry *text_value(GtkWidget *box, const char *label)
165 {
166         GtkWidget *widget;
167         GtkWidget *frame = gtk_frame_new(label);
168
169         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
170         widget = gtk_entry_new();
171         gtk_widget_set_can_focus(widget, FALSE);
172         gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
173         gtk_container_add(GTK_CONTAINER(frame), widget);
174         g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
175         return GTK_ENTRY(widget);
176 }
177
178 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
179 {
180         GtkEntry *entry;
181         GtkWidget *combo_box;
182         GtkWidget *frame = gtk_frame_new(label);
183         GtkEntryCompletion *completion;
184
185         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
186
187         combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
188         gtk_container_add(GTK_CONTAINER(frame), combo_box);
189
190         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
191         if (text && *text)
192                 gtk_entry_set_text(entry, text);
193
194         completion = gtk_entry_completion_new();
195         gtk_entry_completion_set_text_column(completion, 0);
196         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
197         gtk_entry_completion_set_inline_completion(completion, TRUE);
198         gtk_entry_completion_set_inline_selection(completion, TRUE);
199         gtk_entry_completion_set_popup_single_match(completion, FALSE);
200         gtk_entry_set_completion(entry, completion);
201
202         return GTK_COMBO_BOX_ENTRY(combo_box);
203 }
204
205 enum writable {
206         READ_ONLY,
207         READ_WRITE
208 };
209
210 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
211 {
212         GtkWidget *view, *vbox;
213         GtkWidget *frame = gtk_frame_new(label);
214
215         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
216         box = gtk_hbox_new(FALSE, 3);
217         gtk_container_add(GTK_CONTAINER(frame), box);
218         vbox = gtk_vbox_new(FALSE, 3);
219         gtk_container_add(GTK_CONTAINER(box), vbox);
220
221         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
222         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
223         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
224
225         view = gtk_text_view_new();
226         if (writable == READ_ONLY) {
227                 gtk_widget_set_can_focus(view, FALSE);
228                 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
229                 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
230                 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
231         }
232         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
233         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
234         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
235         return GTK_TEXT_VIEW(view);
236 }
237
238 static enum {
239         MATCH_EXACT,
240         MATCH_PREPEND,
241         MATCH_AFTER
242 } found_string_entry;
243 static GtkTreeIter string_entry_location;
244
245 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
246 {
247         const char *string = data;
248         char *entry;
249         int cmp;
250
251         gtk_tree_model_get(model, iter, 0, &entry, -1);
252         cmp = strcmp(entry, string);
253
254         /* Stop. The entry is bigger than the new one */
255         if (cmp > 0)
256                 return TRUE;
257
258         /* Exact match */
259         if (!cmp) {
260                 found_string_entry = MATCH_EXACT;
261                 return TRUE;
262         }
263
264         string_entry_location = *iter;
265         found_string_entry = MATCH_AFTER;
266         return FALSE;
267 }
268
269 static int match_list(GtkListStore *list, const char *string)
270 {
271         found_string_entry = MATCH_PREPEND;
272         gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
273         return found_string_entry;
274 }
275
276 static void add_string_list_entry(const char *string, GtkListStore *list)
277 {
278         GtkTreeIter *iter, loc;
279
280         if (!string || !*string)
281                 return;
282
283         switch (match_list(list, string)) {
284         case MATCH_EXACT:
285                 return;
286         case MATCH_PREPEND:
287                 iter = NULL;
288                 break;
289         case MATCH_AFTER:
290                 iter = &string_entry_location;
291                 break;
292         }
293         gtk_list_store_insert_after(list, &loc, iter);
294         gtk_list_store_set(list, &loc, 0, string, -1);
295 }
296
297 void add_people(const char *string)
298 {
299         add_string_list_entry(string, people_list);
300 }
301
302 void add_location(const char *string)
303 {
304         add_string_list_entry(string, location_list);
305 }
306
307 static int get_rating(const char *string)
308 {
309         int rating_val = 0;
310         int i;
311
312         for (i = 0; i <= 5; i++)
313                 if (!strcmp(star_strings[i],string))
314                         rating_val = i;
315         return rating_val;
316 }
317
318 struct dive_info {
319         GtkComboBoxEntry *location, *divemaster, *buddy, *rating;
320         GtkTextView *notes;
321 };
322
323 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
324 {
325         char *old_text, *new_text;
326         char *rating_string;
327         int changed = 0;
328
329         new_text = get_combo_box_entry_text(info->location, &dive->location);
330         if (new_text) {
331                 add_location(new_text);
332                 changed = 1;
333         }
334
335         new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
336         if (new_text) {
337                 add_people(new_text);
338                 changed = 1;
339         }
340
341         new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
342         if (new_text) {
343                 add_people(new_text);
344                 changed = 1;
345         }
346
347         rating_string = strdup(star_strings[dive->rating]);
348         new_text = get_combo_box_entry_text(info->rating, &rating_string);
349         if (new_text) {
350                 dive->rating = get_rating(rating_string);
351                 free(rating_string);
352                 changed =1;
353         }
354
355         old_text = dive->notes;
356         dive->notes = get_text(info->notes);
357         if (text_changed(old_text,dive->notes))
358                 changed = 1;
359         if (old_text)
360                 g_free(old_text);
361
362         if (changed) {
363                 mark_divelist_changed(TRUE);
364                 update_dive(dive);
365         }
366 }
367
368 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
369 {
370         GtkWidget *hbox, *label, *cylinder, *frame;
371         char buffer[80];
372
373         divename(buffer, sizeof(buffer), dive);
374         label = gtk_label_new(buffer);
375         gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
376
377         info->location = text_entry(box, "Location", location_list, dive->location);
378
379         hbox = gtk_hbox_new(FALSE, 3);
380         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
381
382         info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
383         info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
384
385         hbox = gtk_hbox_new(FALSE, 3);
386         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
387
388         info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
389
390         info->notes = text_view(box, "Notes", READ_WRITE);
391         if (dive->notes && *dive->notes)
392                 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
393
394         hbox = gtk_hbox_new(FALSE, 3);
395         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
396
397         frame = gtk_frame_new("Cylinder");
398         cylinder = cylinder_list_widget();
399         gtk_container_add(GTK_CONTAINER(frame), cylinder);
400         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
401 }
402
403 int edit_dive_info(struct dive *dive)
404 {
405         int success;
406         GtkWidget *dialog, *vbox;
407         struct dive_info info;
408
409         if (!dive)
410                 return 0;
411
412         dialog = gtk_dialog_new_with_buttons("Dive Info",
413                 GTK_WINDOW(main_window),
414                 GTK_DIALOG_DESTROY_WITH_PARENT,
415                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
416                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
417                 NULL);
418
419         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
420         dive_info_widget(vbox, dive, &info);
421
422         gtk_widget_show_all(dialog);
423         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
424         if (success)
425                 save_dive_info_changes(dive, &info);
426
427         gtk_widget_destroy(dialog);
428
429         return success;
430 }
431
432 static GtkWidget *frame_box(GtkWidget *vbox, const char *fmt, ...)
433 {
434         va_list ap;
435         char buffer[64];
436         GtkWidget *frame, *hbox;
437
438         va_start(ap, fmt);
439         vsnprintf(buffer, sizeof(buffer), fmt, ap);
440         va_end(ap);
441
442         frame = gtk_frame_new(buffer);
443         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
444         hbox = gtk_hbox_new(0, 3);
445         gtk_container_add(GTK_CONTAINER(frame), hbox);
446         return hbox;
447 }
448
449 /* Fixme - should do at least depths too - a dive without a depth is kind of pointless */
450 static time_t dive_time_widget(struct dive *dive)
451 {
452         GtkWidget *dialog;
453         GtkWidget *cal, *hbox, *vbox, *box;
454         GtkWidget *h, *m;
455         GtkWidget *duration, *depth;
456         GtkWidget *label;
457         guint yval, mval, dval;
458         struct tm tm, *tmp;
459         struct timeval tv;
460         time_t time;
461         int success;
462         double depthinterval, val;
463
464         dialog = gtk_dialog_new_with_buttons("Date and Time",
465                 GTK_WINDOW(main_window),
466                 GTK_DIALOG_DESTROY_WITH_PARENT,
467                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
468                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
469                 NULL);
470
471         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
472
473         /* Calendar hbox */
474         hbox = frame_box(vbox, "Date:");
475         cal = gtk_calendar_new();
476         gtk_box_pack_start(GTK_BOX(hbox), cal, FALSE, TRUE, 0);
477
478         /* Time hbox */
479         hbox = frame_box(vbox, "Time");
480
481         h = gtk_spin_button_new_with_range (0.0, 23.0, 1.0);
482         m = gtk_spin_button_new_with_range (0.0, 59.0, 1.0);
483
484         gettimeofday(&tv, NULL);
485         time = tv.tv_sec;
486         tmp = localtime(&time);
487         gtk_spin_button_set_value(GTK_SPIN_BUTTON(h), tmp->tm_hour);
488         gtk_spin_button_set_value(GTK_SPIN_BUTTON(m), (tmp->tm_min / 5)*5);
489
490         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(h), TRUE);
491         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(m), TRUE);
492
493         gtk_box_pack_end(GTK_BOX(hbox), m, FALSE, FALSE, 0);
494         label = gtk_label_new(":");
495         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
496         gtk_box_pack_end(GTK_BOX(hbox), h, FALSE, FALSE, 0);
497
498         hbox = gtk_hbox_new(TRUE, 3);
499         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
500
501         /* Duration hbox */
502         box = frame_box(hbox, "Duration (min)");
503         duration = gtk_spin_button_new_with_range (0.0, 1000.0, 1.0);
504         gtk_box_pack_end(GTK_BOX(box), duration, FALSE, FALSE, 0);
505
506         /* Depth box */
507         box = frame_box(hbox, "Depth (%s):", output_units.length == FEET ? "ft" : "m");
508         if (output_units.length == FEET) {
509                 depthinterval = 1.0;
510         } else {
511                 depthinterval = 0.1;
512         }
513         depth = gtk_spin_button_new_with_range (0.0, 1000.0, depthinterval);
514         gtk_box_pack_end(GTK_BOX(box), depth, FALSE, FALSE, 0);
515
516         /* All done, show it and wait for editing */
517         gtk_widget_show_all(dialog);
518         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
519         if (!success) {
520                 gtk_widget_destroy(dialog);
521                 return 0;
522         }
523
524         memset(&tm, 0, sizeof(tm));
525         gtk_calendar_get_date(GTK_CALENDAR(cal), &yval, &mval, &dval);
526         tm.tm_year = yval;
527         tm.tm_mon = mval;
528         tm.tm_mday = dval;
529
530         tm.tm_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(h));
531         tm.tm_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m));
532
533         val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(depth));
534         if (output_units.length == FEET) {
535                 dive->maxdepth.mm = feet_to_mm(val);
536         } else {
537                 dive->maxdepth.mm = val * 1000 + 0.5;
538         }
539
540         dive->duration.seconds = gtk_spin_button_get_value(GTK_SPIN_BUTTON(duration))*60;
541
542         gtk_widget_destroy(dialog);
543         dive->when = utc_mktime(&tm);
544
545         return 1;
546 }
547
548 int add_new_dive(struct dive *dive)
549 {
550         if (!dive)
551                 return 0;
552
553         if (!dive_time_widget(dive))
554                 return 0;
555
556         return edit_dive_info(dive);
557 }
558
559 GtkWidget *extended_dive_info_widget(void)
560 {
561         GtkWidget *vbox, *hbox;
562         vbox = gtk_vbox_new(FALSE, 6);
563
564         people_list = gtk_list_store_new(1, G_TYPE_STRING);
565         location_list = gtk_list_store_new(1, G_TYPE_STRING);
566         star_list = gtk_list_store_new(1, G_TYPE_STRING);
567         add_string_list_entry(ZERO_STARS, star_list);
568         add_string_list_entry(ONE_STARS, star_list);
569         add_string_list_entry(TWO_STARS, star_list);
570         add_string_list_entry(THREE_STARS, star_list);
571         add_string_list_entry(FOUR_STARS, star_list);
572         add_string_list_entry(FIVE_STARS, star_list);
573
574         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
575         location = text_value(vbox, "Location");
576
577         hbox = gtk_hbox_new(FALSE, 3);
578         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
579
580         divemaster = text_value(hbox, "Divemaster");
581         buddy = text_value(hbox, "Buddy");
582
583         hbox = gtk_hbox_new(FALSE, 3);
584         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
585
586         rating = text_value(hbox, "Rating");
587
588         notes = text_view(vbox, "Notes", READ_ONLY);
589         return vbox;
590 }