]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Fix string handling in get_combo_box_entry_text
[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 const char *skip_space(const char *str)
47 {
48         if (str) {
49                 while (isspace(*str))
50                         str++;
51                 if (!*str)
52                         str = NULL;
53         }
54         return str;
55 }
56
57 /*
58  * Get the string from a combo box.
59  *
60  * The "master" string is the string of the current dive - we only consider it
61  * changed if the old string is either empty, or matches that master string.
62  */
63 static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp, const char *master)
64 {
65         char *old = *textp;
66         const char *old_text;
67         const gchar *new;
68         GtkEntry *entry;
69
70         old_text = skip_space(old);
71         master = skip_space(master);
72
73         /*
74          * If we had a master string, and it doesn't match our old
75          * string, we will always pick the old value (it means that
76          * we're editing another dive's info that already had a
77          * valid value).
78          */
79         if (master && old_text)
80                 if (strcmp(master, old_text))
81                         return NULL;
82
83         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
84         new = gtk_entry_get_text(entry);
85         while (isspace(*new))
86                 new++;
87         /* If the master string didn't change, don't change other dives either! */
88         if (!text_changed(master,new))
89                 return NULL;
90         if (!text_changed(old,new))
91                 return NULL;
92         free(old);
93         *textp = strdup(new);
94         return *textp;
95 }
96
97 #define SET_TEXT_VALUE(x) \
98         gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
99
100 static int divename(char *buf, size_t size, struct dive *dive)
101 {
102         struct tm *tm = gmtime(&dive->when);
103         return snprintf(buf, size, "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
104                 dive->number,
105                 weekday(tm->tm_wday),
106                 tm->tm_mon+1, tm->tm_mday,
107                 tm->tm_year+1900,
108                 tm->tm_hour, tm->tm_min);
109 }
110
111 void show_dive_info(struct dive *dive)
112 {
113         const char *text;
114         char buffer[80];
115
116         /* dive number and location (or lacking that, the date) go in the window title */
117         text = dive->location;
118         if (!text)
119                 text = "";
120         if (*text) {
121                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
122         } else {
123                 divename(buffer, sizeof(buffer), dive);
124         }
125         text = buffer;
126         if (!dive->number)
127                 text += 10;     /* Skip the "Dive #0 - " part */
128         gtk_window_set_title(GTK_WINDOW(main_window), text);
129
130         SET_TEXT_VALUE(divemaster);
131         SET_TEXT_VALUE(buddy);
132         SET_TEXT_VALUE(location);
133         SET_TEXT_VALUE(suit);
134         gtk_entry_set_text(rating, star_strings[dive->rating]);
135         gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
136                 dive && dive->notes ? dive->notes : "", -1);
137 }
138
139 static int delete_dive_info(struct dive *dive)
140 {
141         int success;
142         GtkWidget *dialog;
143
144         if (!dive)
145                 return 0;
146
147         dialog = gtk_dialog_new_with_buttons("Delete Dive",
148                 GTK_WINDOW(main_window),
149                 GTK_DIALOG_DESTROY_WITH_PARENT,
150                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
151                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
152                 NULL);
153
154         gtk_widget_show_all(dialog);
155         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
156         if (success) {
157                 delete_dive(dive);
158                 mark_divelist_changed(TRUE);
159                 dive_list_update_dives();
160         }
161
162         gtk_widget_destroy(dialog);
163
164         return success;
165 }
166
167 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
168 {
169         edit_multi_dive_info(amount_selected, selectiontracker);
170 }
171
172 static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data)
173 {
174         /* this needs to delete all the selected dives as well, I guess? */
175         delete_dive_info(current_dive);
176 }
177
178 static void add_menu_item(GtkMenu *menu, const char *label, void (*cb)(GtkMenuItem *, gpointer))
179 {
180         GtkWidget *item = gtk_menu_item_new_with_label(label);
181         g_signal_connect(item, "activate", G_CALLBACK(cb), NULL);
182         gtk_widget_show(item); /* Yes, really */
183         gtk_menu_prepend(menu, item);
184 }
185
186 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
187 {
188         add_menu_item(menu, "Delete", info_menu_delete_cb);
189         add_menu_item(menu, "Edit", info_menu_edit_cb);
190 }
191
192 static GtkEntry *text_value(GtkWidget *box, const char *label)
193 {
194         GtkWidget *widget;
195         GtkWidget *frame = gtk_frame_new(label);
196
197         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
198         widget = gtk_entry_new();
199         gtk_widget_set_can_focus(widget, FALSE);
200         gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
201         gtk_container_add(GTK_CONTAINER(frame), widget);
202         g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
203         return GTK_ENTRY(widget);
204 }
205
206 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
207 {
208         GtkEntry *entry;
209         GtkWidget *combo_box;
210         GtkWidget *frame = gtk_frame_new(label);
211         GtkEntryCompletion *completion;
212
213         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
214
215         combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
216         gtk_container_add(GTK_CONTAINER(frame), combo_box);
217
218         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
219         if (text && *text)
220                 gtk_entry_set_text(entry, text);
221
222         completion = gtk_entry_completion_new();
223         gtk_entry_completion_set_text_column(completion, 0);
224         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
225         gtk_entry_completion_set_inline_completion(completion, TRUE);
226         gtk_entry_completion_set_inline_selection(completion, TRUE);
227         gtk_entry_completion_set_popup_single_match(completion, FALSE);
228         gtk_entry_set_completion(entry, completion);
229
230         return GTK_COMBO_BOX_ENTRY(combo_box);
231 }
232
233 enum writable {
234         READ_ONLY,
235         READ_WRITE
236 };
237
238 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
239 {
240         GtkWidget *view, *vbox;
241         GtkWidget *frame = gtk_frame_new(label);
242
243         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
244         box = gtk_hbox_new(FALSE, 3);
245         gtk_container_add(GTK_CONTAINER(frame), box);
246         vbox = gtk_vbox_new(FALSE, 3);
247         gtk_container_add(GTK_CONTAINER(box), vbox);
248
249         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
250         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
251         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
252
253         view = gtk_text_view_new();
254         if (writable == READ_ONLY) {
255                 gtk_widget_set_can_focus(view, FALSE);
256                 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
257                 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
258                 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
259         }
260         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
261         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
262         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
263         return GTK_TEXT_VIEW(view);
264 }
265
266 static enum {
267         MATCH_EXACT,
268         MATCH_PREPEND,
269         MATCH_AFTER
270 } found_string_entry;
271 static GtkTreeIter string_entry_location;
272
273 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
274 {
275         const char *string = data;
276         char *entry;
277         int cmp;
278
279         gtk_tree_model_get(model, iter, 0, &entry, -1);
280         cmp = strcmp(entry, string);
281         if (entry)
282                 free(entry);
283
284         /* Stop. The entry is bigger than the new one */
285         if (cmp > 0)
286                 return TRUE;
287
288         /* Exact match */
289         if (!cmp) {
290                 found_string_entry = MATCH_EXACT;
291                 return TRUE;
292         }
293
294         string_entry_location = *iter;
295         found_string_entry = MATCH_AFTER;
296         return FALSE;
297 }
298
299 static int match_list(GtkListStore *list, const char *string)
300 {
301         found_string_entry = MATCH_PREPEND;
302         gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
303         return found_string_entry;
304 }
305
306 static void add_string_list_entry(const char *string, GtkListStore *list)
307 {
308         GtkTreeIter *iter, loc;
309
310         if (!string || !*string)
311                 return;
312
313         switch (match_list(list, string)) {
314         case MATCH_EXACT:
315                 return;
316         case MATCH_PREPEND:
317                 iter = NULL;
318                 break;
319         case MATCH_AFTER:
320                 iter = &string_entry_location;
321                 break;
322         }
323         gtk_list_store_insert_after(list, &loc, iter);
324         gtk_list_store_set(list, &loc, 0, string, -1);
325 }
326
327 void add_people(const char *string)
328 {
329         add_string_list_entry(string, people_list);
330 }
331
332 void add_location(const char *string)
333 {
334         add_string_list_entry(string, location_list);
335 }
336
337 void add_suit(const char *string)
338 {
339         add_string_list_entry(string, suit_list);
340 }
341
342 static int get_rating(const char *string)
343 {
344         int rating_val = 0;
345         int i;
346
347         for (i = 0; i <= 5; i++)
348                 if (!strcmp(star_strings[i],string))
349                         rating_val = i;
350         return rating_val;
351 }
352
353 struct dive_info {
354         GtkComboBoxEntry *location, *divemaster, *buddy, *rating, *suit;
355         GtkTextView *notes;
356 };
357
358 static void save_dive_info_changes(struct dive *dive, struct dive *master, struct dive_info *info)
359 {
360         char *old_text, *new_text;
361         char *rating_string;
362         int changed = 0;
363
364         new_text = get_combo_box_entry_text(info->location, &dive->location, master->location);
365         if (new_text) {
366                 add_location(new_text);
367                 changed = 1;
368         }
369
370         new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster, master->divemaster);
371         if (new_text) {
372                 add_people(new_text);
373                 changed = 1;
374         }
375
376         new_text = get_combo_box_entry_text(info->buddy, &dive->buddy, master->buddy);
377         if (new_text) {
378                 add_people(new_text);
379                 changed = 1;
380         }
381
382         new_text = get_combo_box_entry_text(info->suit, &dive->suit, master->suit);
383         if (new_text) {
384                 add_suit(new_text);
385                 changed = 1;
386         }
387
388         rating_string = strdup(star_strings[dive->rating]);
389         new_text = get_combo_box_entry_text(info->rating, &rating_string, star_strings[master->rating]);
390         if (new_text) {
391                 dive->rating = get_rating(rating_string);
392                 free(rating_string);
393                 changed =1;
394         }
395
396         if (info->notes) {
397                 old_text = dive->notes;
398                 dive->notes = get_text(info->notes);
399                 if (text_changed(old_text,dive->notes))
400                         changed = 1;
401                 if (old_text)
402                         g_free(old_text);
403         }
404         if (changed) {
405                 mark_divelist_changed(TRUE);
406                 update_dive(dive);
407         }
408 }
409
410 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info, gboolean multi)
411 {
412         GtkWidget *hbox, *label, *frame, *equipment;
413         char buffer[80] = "Edit multiple dives";
414
415         if (!multi)
416                 divename(buffer, sizeof(buffer), dive);
417         label = gtk_label_new(buffer);
418         gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
419
420         info->location = text_entry(box, "Location", location_list, dive->location);
421
422         hbox = gtk_hbox_new(FALSE, 3);
423         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
424
425         info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
426         info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
427
428         hbox = gtk_hbox_new(FALSE, 3);
429         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
430
431         info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
432         info->suit = text_entry(hbox, "Suit", suit_list, dive->suit);
433
434         /* only show notes if editing a single dive */
435         if (multi) {
436                 info->notes = NULL;
437         } else {
438                 info->notes = text_view(box, "Notes", READ_WRITE);
439                 if (dive->notes && *dive->notes)
440                         gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
441         }
442         hbox = gtk_hbox_new(FALSE, 3);
443         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
444
445         /* create a secondary Equipment widget */
446         frame = gtk_frame_new("Equipment");
447         equipment = equipment_widget(W_IDX_SECONDARY);
448         gtk_container_add(GTK_CONTAINER(frame), equipment);
449         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
450 }
451
452 /* we use these to find out if we edited the cylinder or weightsystem entries */
453 static cylinder_t remember_cyl[MAX_CYLINDERS];
454 static weightsystem_t remember_ws[MAX_WEIGHTSYSTEMS];
455
456 void save_equipment_data(struct dive *dive)
457 {
458         if (dive) {
459                 memcpy(remember_cyl, dive->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS);
460                 memcpy(remember_ws, dive->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS);
461         }
462 }
463
464 void update_equipment_data(struct dive *dive, struct dive *master)
465 {
466         if (dive == master)
467                 return;
468         if (memcmp(remember_cyl, master->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS)) {
469                 memcpy(dive->cylinder, master->cylinder, sizeof(cylinder_t) * MAX_CYLINDERS);
470         }
471         if (memcmp(remember_ws, master->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS)) {
472                 memcpy(dive->weightsystem, master->weightsystem, sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS);
473         }
474 }
475
476 int edit_multi_dive_info(int nr, int *indices)
477 {
478         int success, i;
479         GtkWidget *dialog, *vbox;
480         struct dive_info info;
481         struct dive *master;
482
483         if (!nr)
484                 return 0;
485         dialog = gtk_dialog_new_with_buttons("Dive Info",
486                 GTK_WINDOW(main_window),
487                 GTK_DIALOG_DESTROY_WITH_PARENT,
488                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
489                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
490                 NULL);
491
492         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
493         /* SCARY STUFF - IS THIS THE BEST WAY TO DO THIS???
494          *
495          * current_dive is one of our selected dives - and that is
496          * the one that is used to pre-fill the edit widget. Its
497          * data is used as the starting point for all selected dives
498          * I think it would be better to somehow collect and combine
499          * info from all the selected dives */
500         master = current_dive;
501         dive_info_widget(vbox, master, &info, (nr > 1));
502         show_dive_equipment(master, W_IDX_SECONDARY);
503         save_equipment_data(master);
504         gtk_widget_show_all(dialog);
505         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
506         if (success) {
507                 /* Update the other non-current dives first */
508                 for (i = 0; i < nr; i++) {
509                         int idx = indices[i];
510                         struct dive *dive = get_dive(idx);
511
512                         if (!dive || dive == master)
513                                 continue;
514                         /* copy all "info" fields */
515                         save_dive_info_changes(dive, master, &info);
516                         /* copy the cylinders / weightsystems */
517                         update_equipment_data(dive, master);
518                         /* this is extremely inefficient... it loops through all
519                            dives to find the right one - but we KNOW the index already */
520                         flush_divelist(dive);
521                 }
522
523                 /* Update the master dive last! */
524                 save_dive_info_changes(master, master, &info);
525                 update_equipment_data(master, master);
526                 flush_divelist(master);
527         }
528         gtk_widget_destroy(dialog);
529
530         return success;
531 }
532
533 int edit_dive_info(struct dive *dive)
534 {
535         int idx;
536
537         if (!dive)
538                 return 0;
539         idx = dive->number;
540         return edit_multi_dive_info(1, &idx);
541 }
542
543 static GtkWidget *frame_box(GtkWidget *vbox, const char *fmt, ...)
544 {
545         va_list ap;
546         char buffer[64];
547         GtkWidget *frame, *hbox;
548
549         va_start(ap, fmt);
550         vsnprintf(buffer, sizeof(buffer), fmt, ap);
551         va_end(ap);
552
553         frame = gtk_frame_new(buffer);
554         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
555         hbox = gtk_hbox_new(0, 3);
556         gtk_container_add(GTK_CONTAINER(frame), hbox);
557         return hbox;
558 }
559
560 /* Fixme - should do at least depths too - a dive without a depth is kind of pointless */
561 static time_t dive_time_widget(struct dive *dive)
562 {
563         GtkWidget *dialog;
564         GtkWidget *cal, *hbox, *vbox, *box;
565         GtkWidget *h, *m;
566         GtkWidget *duration, *depth;
567         GtkWidget *label;
568         guint yval, mval, dval;
569         struct tm tm, *tmp;
570         struct timeval tv;
571         time_t time;
572         int success;
573         double depthinterval, val;
574
575         dialog = gtk_dialog_new_with_buttons("Date and Time",
576                 GTK_WINDOW(main_window),
577                 GTK_DIALOG_DESTROY_WITH_PARENT,
578                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
579                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
580                 NULL);
581
582         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
583
584         /* Calendar hbox */
585         hbox = frame_box(vbox, "Date:");
586         cal = gtk_calendar_new();
587         gtk_box_pack_start(GTK_BOX(hbox), cal, FALSE, TRUE, 0);
588
589         /* Time hbox */
590         hbox = frame_box(vbox, "Time");
591
592         h = gtk_spin_button_new_with_range (0.0, 23.0, 1.0);
593         m = gtk_spin_button_new_with_range (0.0, 59.0, 1.0);
594
595         gettimeofday(&tv, NULL);
596         time = tv.tv_sec;
597         tmp = localtime(&time);
598         gtk_spin_button_set_value(GTK_SPIN_BUTTON(h), tmp->tm_hour);
599         gtk_spin_button_set_value(GTK_SPIN_BUTTON(m), (tmp->tm_min / 5)*5);
600
601         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(h), TRUE);
602         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(m), TRUE);
603
604         gtk_box_pack_end(GTK_BOX(hbox), m, FALSE, FALSE, 0);
605         label = gtk_label_new(":");
606         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
607         gtk_box_pack_end(GTK_BOX(hbox), h, FALSE, FALSE, 0);
608
609         hbox = gtk_hbox_new(TRUE, 3);
610         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
611
612         /* Duration hbox */
613         box = frame_box(hbox, "Duration (min)");
614         duration = gtk_spin_button_new_with_range (0.0, 1000.0, 1.0);
615         gtk_box_pack_end(GTK_BOX(box), duration, FALSE, FALSE, 0);
616
617         /* Depth box */
618         box = frame_box(hbox, "Depth (%s):", output_units.length == FEET ? "ft" : "m");
619         if (output_units.length == FEET) {
620                 depthinterval = 1.0;
621         } else {
622                 depthinterval = 0.1;
623         }
624         depth = gtk_spin_button_new_with_range (0.0, 1000.0, depthinterval);
625         gtk_box_pack_end(GTK_BOX(box), depth, FALSE, FALSE, 0);
626
627         /* All done, show it and wait for editing */
628         gtk_widget_show_all(dialog);
629         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
630         if (!success) {
631                 gtk_widget_destroy(dialog);
632                 return 0;
633         }
634
635         memset(&tm, 0, sizeof(tm));
636         gtk_calendar_get_date(GTK_CALENDAR(cal), &yval, &mval, &dval);
637         tm.tm_year = yval;
638         tm.tm_mon = mval;
639         tm.tm_mday = dval;
640
641         tm.tm_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(h));
642         tm.tm_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m));
643
644         val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(depth));
645         if (output_units.length == FEET) {
646                 dive->maxdepth.mm = feet_to_mm(val);
647         } else {
648                 dive->maxdepth.mm = val * 1000 + 0.5;
649         }
650
651         dive->duration.seconds = gtk_spin_button_get_value(GTK_SPIN_BUTTON(duration))*60;
652
653         gtk_widget_destroy(dialog);
654         dive->when = utc_mktime(&tm);
655
656         return 1;
657 }
658
659 int add_new_dive(struct dive *dive)
660 {
661         if (!dive)
662                 return 0;
663
664         if (!dive_time_widget(dive))
665                 return 0;
666
667         return edit_dive_info(dive);
668 }
669
670 GtkWidget *extended_dive_info_widget(void)
671 {
672         GtkWidget *vbox, *hbox;
673         vbox = gtk_vbox_new(FALSE, 6);
674
675         people_list = gtk_list_store_new(1, G_TYPE_STRING);
676         location_list = gtk_list_store_new(1, G_TYPE_STRING);
677         star_list = gtk_list_store_new(1, G_TYPE_STRING);
678         add_string_list_entry(ZERO_STARS, star_list);
679         add_string_list_entry(ONE_STARS, star_list);
680         add_string_list_entry(TWO_STARS, star_list);
681         add_string_list_entry(THREE_STARS, star_list);
682         add_string_list_entry(FOUR_STARS, star_list);
683         add_string_list_entry(FIVE_STARS, star_list);
684         suit_list = gtk_list_store_new(1, G_TYPE_STRING);
685
686         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
687         location = text_value(vbox, "Location");
688
689         hbox = gtk_hbox_new(FALSE, 3);
690         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
691
692         divemaster = text_value(hbox, "Divemaster");
693         buddy = text_value(hbox, "Buddy");
694
695         hbox = gtk_hbox_new(FALSE, 3);
696         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
697
698         rating = text_value(hbox, "Rating");
699         suit = text_value(hbox, "Suit");
700
701         notes = text_view(vbox, "Notes", READ_ONLY);
702         return vbox;
703 }