]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
bef03a04b0898818c69844660f0d66a9c8aa72a1
[ext/subsurface.git] / gtk-gui.c
1 /* gtk-gui.c */
2 /* gtk UI implementation */
3 /* creates the window and overall layout
4  * divelist, dive info, equipment and printing are handled in their own source files
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <time.h>
10 #include <unistd.h>
11
12 #include "dive.h"
13 #include "divelist.h"
14 #include "display.h"
15 #include "display-gtk.h"
16
17 #include "libdivecomputer.h"
18
19 GtkWidget *main_window;
20 GtkWidget *main_vbox;
21 GtkWidget *error_info_bar;
22 GtkWidget *error_label;
23 GtkWidget *vpane, *hpane;
24 int        error_count;
25
26 const char *divelist_font;
27
28 struct units output_units;
29
30 static GtkWidget *dive_profile;
31
32 visible_cols_t visible_cols = {TRUE, FALSE};
33
34 static const char *default_dive_computer_vendor;
35 static const char *default_dive_computer_product;
36 static const char *default_dive_computer_device;
37
38 static int is_default_dive_computer(const char *vendor, const char *product)
39 {
40         return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) &&
41                 default_dive_computer_product && !strcmp(product, default_dive_computer_product);
42 }
43
44 static int is_default_dive_computer_device(const char *name)
45 {
46         return default_dive_computer_device && !strcmp(name, default_dive_computer_device);
47 }
48
49 static void set_default_dive_computer(const char *vendor, const char *product)
50 {
51         if (!vendor || !*vendor)
52                 return;
53         if (!product || !*product)
54                 return;
55         if (is_default_dive_computer(vendor, product))
56                 return;
57         default_dive_computer_vendor = vendor;
58         default_dive_computer_product = product;
59         subsurface_set_conf("dive_computer_vendor", PREF_STRING, vendor);
60         subsurface_set_conf("dive_computer_product", PREF_STRING, product);
61 }
62
63 static void set_default_dive_computer_device(const char *name)
64 {
65         if (!name || !*name)
66                 return;
67         if (is_default_dive_computer_device(name))
68                 return;
69         default_dive_computer_device = name;
70         subsurface_set_conf("dive_computer_device", PREF_STRING, name);
71 }
72
73 void repaint_dive(void)
74 {
75         update_dive(current_dive);
76         if (dive_profile)
77                 gtk_widget_queue_draw(dive_profile);
78 }
79
80 static char *existing_filename;
81 static gboolean need_icon = TRUE;
82
83 static void on_info_bar_response(GtkWidget *widget, gint response,
84                                  gpointer data)
85 {
86         if (response == GTK_RESPONSE_OK)
87         {
88                 gtk_widget_destroy(widget);
89                 error_info_bar = NULL;
90         }
91 }
92
93 void report_error(GError* error)
94 {
95         if (error == NULL)
96         {
97                 return;
98         }
99         
100         if (error_info_bar == NULL)
101         {
102                 error_count = 1;
103                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
104                                                                GTK_RESPONSE_OK,
105                                                                NULL);
106                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
107                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
108                                               GTK_MESSAGE_ERROR);
109                 
110                 error_label = gtk_label_new(error->message);
111                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
112                 gtk_container_add(GTK_CONTAINER(container), error_label);
113                 
114                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
115                 gtk_widget_show_all(main_vbox);
116         }
117         else
118         {
119                 error_count++;
120                 char buffer[256];
121                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
122                 gtk_label_set(GTK_LABEL(error_label), buffer);
123         }
124 }
125
126 static void file_open(GtkWidget *w, gpointer data)
127 {
128         GtkWidget *dialog;
129         GtkFileFilter *filter;
130
131         dialog = gtk_file_chooser_dialog_new("Open File",
132                 GTK_WINDOW(main_window),
133                 GTK_FILE_CHOOSER_ACTION_OPEN,
134                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
135                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
136                 NULL);
137         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
138
139         filter = gtk_file_filter_new();
140         gtk_file_filter_add_pattern(filter, "*.xml");
141         gtk_file_filter_add_pattern(filter, "*.XML");
142         gtk_file_filter_add_pattern(filter, "*.sda");
143         gtk_file_filter_add_pattern(filter, "*.SDA");
144         gtk_file_filter_add_mime_type(filter, "text/xml");
145         gtk_file_filter_set_name(filter, "XML file");
146         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
147
148         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
149                 GSList *filenames, *fn_glist;
150                 char *filename;
151                 filenames = fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
152                 
153                 GError *error = NULL;
154                 while(filenames != NULL) {
155                         filename = filenames->data;
156                         parse_file(filename, &error);
157                         if (error != NULL)
158                         {
159                                 report_error(error);
160                                 g_error_free(error);
161                                 error = NULL;
162                         }
163                         
164                         g_free(filename);
165                         filenames = g_slist_next(filenames);
166                 }
167                 g_slist_free(fn_glist);
168                 report_dives(FALSE);
169         }
170         gtk_widget_destroy(dialog);
171 }
172
173 static void file_save_as(GtkWidget *w, gpointer data)
174 {
175         GtkWidget *dialog;
176         dialog = gtk_file_chooser_dialog_new("Save File",
177                 GTK_WINDOW(main_window),
178                 GTK_FILE_CHOOSER_ACTION_SAVE,
179                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
180                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
181                 NULL);
182         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
183         if (!existing_filename) {
184                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
185         } else
186                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
187
188         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
189                 char *filename;
190                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
191                 save_dives(filename);
192                 set_filename(filename);
193                 g_free(filename);
194                 mark_divelist_changed(FALSE);
195         }
196         gtk_widget_destroy(dialog);
197 }
198
199 static void file_save(GtkWidget *w, gpointer data)
200 {
201         if (!existing_filename)
202                 return file_save_as(w, data);
203
204         save_dives(existing_filename);
205 }
206
207 static void ask_save_changes()
208 {
209         GtkWidget *dialog, *label, *content;
210         dialog = gtk_dialog_new_with_buttons("Save Changes?",
211                 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
212                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
213                 GTK_STOCK_NO, GTK_RESPONSE_NO,
214                 NULL);
215         content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
216         label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
217         gtk_container_add (GTK_CONTAINER (content), label);
218         gtk_widget_show_all (dialog);
219         gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
220         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
221                 file_save(NULL,NULL);
222         }
223         gtk_widget_destroy(dialog);
224 }
225
226 static gboolean on_delete(GtkWidget* w, gpointer data)
227 {
228         /* Make sure to flush any modified dive data */
229         update_dive(NULL);
230
231         if (unsaved_changes())
232                 ask_save_changes();
233
234         return FALSE; /* go ahead, kill the program, we're good now */
235 }
236
237 static void on_destroy(GtkWidget* w, gpointer data)
238 {
239         gtk_main_quit();
240 }
241
242 static void quit(GtkWidget *w, gpointer data)
243 {
244         /* Make sure to flush any modified dive data */
245         update_dive(NULL);
246
247         if (unsaved_changes())
248                 ask_save_changes();
249         gtk_main_quit();
250 }
251
252 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
253                                 data_func_t data_func, unsigned int flags)
254 {
255         GtkCellRenderer *renderer;
256         GtkTreeViewColumn *col;
257         double xalign = 0.0; /* left as default */
258         PangoAlignment align;
259         gboolean visible;
260
261         align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
262                 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
263                 PANGO_ALIGN_CENTER;
264         visible = !(flags & INVISIBLE);
265
266         renderer = gtk_cell_renderer_text_new();
267         col = gtk_tree_view_column_new();
268
269         gtk_tree_view_column_set_title(col, title);
270         if (!(flags & UNSORTABLE))
271                 gtk_tree_view_column_set_sort_column_id(col, index);
272         gtk_tree_view_column_set_resizable(col, TRUE);
273         gtk_tree_view_column_pack_start(col, renderer, TRUE);
274         if (data_func)
275                 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
276         else
277                 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
278         gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
279         switch (align) {
280         case PANGO_ALIGN_LEFT:
281                 xalign = 0.0;
282                 break;
283         case PANGO_ALIGN_CENTER:
284                 xalign = 0.5;
285                 break;
286         case PANGO_ALIGN_RIGHT:
287                 xalign = 1.0;
288                 break;
289         }
290         gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
291         gtk_tree_view_column_set_visible(col, visible);
292         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
293         return col;
294 }
295
296 static void create_radio(GtkWidget *vbox, const char *w_name, ...)
297 {
298         va_list args;
299         GtkRadioButton *group = NULL;
300         GtkWidget *box, *label;
301
302         box = gtk_hbox_new(TRUE, 10);
303         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
304
305         label = gtk_label_new(w_name);
306         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
307
308         va_start(args, w_name);
309         for (;;) {
310                 int enabled;
311                 const char *name;
312                 GtkWidget *button;
313                 void *callback_fn;
314
315                 name = va_arg(args, char *);
316                 if (!name)
317                         break;
318                 callback_fn = va_arg(args, void *);
319                 enabled = va_arg(args, int);
320
321                 button = gtk_radio_button_new_with_label_from_widget(group, name);
322                 group = GTK_RADIO_BUTTON(button);
323                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
324                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
325                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
326         }
327         va_end(args);
328 }
329
330 #define UNITCALLBACK(name, type, value)                         \
331 static void name(GtkWidget *w, gpointer data)                   \
332 {                                                               \
333         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
334                 menu_units.type = value;                        \
335 }
336
337 static struct units menu_units;
338
339 UNITCALLBACK(set_meter, length, METERS)
340 UNITCALLBACK(set_feet, length, FEET)
341 UNITCALLBACK(set_bar, pressure, BAR)
342 UNITCALLBACK(set_psi, pressure, PSI)
343 UNITCALLBACK(set_liter, volume, LITER)
344 UNITCALLBACK(set_cuft, volume, CUFT)
345 UNITCALLBACK(set_celsius, temperature, CELSIUS)
346 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
347 UNITCALLBACK(set_kg, weight, KG)
348 UNITCALLBACK(set_lbs, weight, LBS)
349
350 #define OPTIONCALLBACK(name, option) \
351 static void name(GtkWidget *w, gpointer data) \
352 { \
353         option = GTK_TOGGLE_BUTTON(w)->active; \
354 }
355
356 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
357 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
358 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
359 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
360 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
361
362 static void event_toggle(GtkWidget *w, gpointer _data)
363 {
364         gboolean *plot_ev = _data;
365
366         *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
367 }
368
369 static void preferences_dialog(GtkWidget *w, gpointer data)
370 {
371         int result;
372         GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
373
374         menu_units = output_units;
375
376         dialog = gtk_dialog_new_with_buttons("Preferences",
377                 GTK_WINDOW(main_window),
378                 GTK_DIALOG_DESTROY_WITH_PARENT,
379                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
380                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
381                 NULL);
382
383         frame = gtk_frame_new("Units");
384         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
385         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
386
387         box = gtk_vbox_new(FALSE, 6);
388         gtk_container_add(GTK_CONTAINER(frame), box);
389
390         create_radio(box, "Depth:",
391                 "Meter", set_meter, (output_units.length == METERS),
392                 "Feet",  set_feet, (output_units.length == FEET),
393                 NULL);
394
395         create_radio(box, "Pressure:",
396                 "Bar", set_bar, (output_units.pressure == BAR),
397                 "PSI",  set_psi, (output_units.pressure == PSI),
398                 NULL);
399
400         create_radio(box, "Volume:",
401                 "Liter",  set_liter, (output_units.volume == LITER),
402                 "CuFt", set_cuft, (output_units.volume == CUFT),
403                 NULL);
404
405         create_radio(box, "Temperature:",
406                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
407                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
408                 NULL);
409
410         create_radio(box, "Weight:",
411                 "kg", set_kg, (output_units.weight == KG),
412                 "lbs",  set_lbs, (output_units.weight == LBS),
413                 NULL);
414
415         frame = gtk_frame_new("Columns");
416         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
417
418         box = gtk_hbox_new(FALSE, 6);
419         gtk_container_add(GTK_CONTAINER(frame), box);
420
421         button = gtk_check_button_new_with_label("Show Temp");
422         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
423         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
424         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
425
426         button = gtk_check_button_new_with_label("Show Cyl");
427         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
428         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
429         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
430
431         button = gtk_check_button_new_with_label("Show O" UTF8_SUBSCRIPT_2 "%");
432         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
433         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
434         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
435
436         button = gtk_check_button_new_with_label("Show SAC");
437         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
438         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
439         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
440
441         button = gtk_check_button_new_with_label("Show OTU");
442         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
443         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
444         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
445
446         font = gtk_font_button_new_with_font(divelist_font);
447         gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
448
449         gtk_widget_show_all(dialog);
450         result = gtk_dialog_run(GTK_DIALOG(dialog));
451         if (result == GTK_RESPONSE_ACCEPT) {
452                 /* Make sure to flush any modified old dive data with old units */
453                 update_dive(NULL);
454
455                 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
456                 set_divelist_font(divelist_font);
457
458                 output_units = menu_units;
459                 update_dive_list_units();
460                 repaint_dive();
461                 update_dive_list_col_visibility();
462
463                 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
464                 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
465                 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
466                 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
467                 subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
468                 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
469                 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
470                 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
471                 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
472                 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
473                 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
474
475                 /* Flush the changes out to the system */
476                 subsurface_flush_conf();
477         }
478         gtk_widget_destroy(dialog);
479 }
480
481 static void create_toggle(const char* label, int *on, void *_data)
482 {
483         GtkWidget *button, *table = _data;
484         int rows, cols, x, y;
485         static int count;
486
487         if (table == NULL) {
488                 /* magic way to reset the number of toggle buttons
489                  * that we have already added - call this before you
490                  * create the dialog */
491                 count = 0;
492                 return;
493         }
494         g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
495         if (count > rows * cols) {
496                 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
497                 rows++;
498         }
499         x = count % cols;
500         y = count / cols;
501         button = gtk_check_button_new_with_label(label);
502         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
503         gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
504         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
505         count++;
506 }
507
508 static void selectevents_dialog(GtkWidget *w, gpointer data)
509 {
510         int result;
511         GtkWidget *dialog, *frame, *vbox, *table;
512
513         dialog = gtk_dialog_new_with_buttons("SelectEvents",
514                 GTK_WINDOW(main_window),
515                 GTK_DIALOG_DESTROY_WITH_PARENT,
516                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
517                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
518                 NULL);
519         /* initialize the function that fills the table */
520         create_toggle(NULL, NULL, NULL);
521
522         frame = gtk_frame_new("Enable / Disable Events");
523         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
524         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
525
526         table = gtk_table_new(1, 4, TRUE);
527         gtk_container_add(GTK_CONTAINER(frame), table);
528
529         evn_foreach(&create_toggle, table);
530
531         gtk_widget_show_all(dialog);
532         result = gtk_dialog_run(GTK_DIALOG(dialog));
533         if (result == GTK_RESPONSE_ACCEPT) {
534                 repaint_dive();
535         }
536         gtk_widget_destroy(dialog);
537 }
538
539 static void renumber_dialog(GtkWidget *w, gpointer data)
540 {
541         int result;
542         struct dive *dive;
543         GtkWidget *dialog, *frame, *button, *vbox;
544
545         dialog = gtk_dialog_new_with_buttons("Renumber",
546                 GTK_WINDOW(main_window),
547                 GTK_DIALOG_DESTROY_WITH_PARENT,
548                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
549                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
550                 NULL);
551
552         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
553
554         frame = gtk_frame_new("New starting number");
555         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
556
557         button = gtk_spin_button_new_with_range(1, 50000, 1);
558         gtk_container_add(GTK_CONTAINER(frame), button);
559
560         /*
561          * Do we have a number for the first dive already? Use that
562          * as the default.
563          */
564         dive = get_dive(0);
565         if (dive && dive->number)
566                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
567
568         gtk_widget_show_all(dialog);
569         result = gtk_dialog_run(GTK_DIALOG(dialog));
570         if (result == GTK_RESPONSE_ACCEPT) {
571                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
572                 renumber_dives(nr);
573                 repaint_dive();
574         }
575         gtk_widget_destroy(dialog);
576 }
577
578 static void about_dialog(GtkWidget *w, gpointer data)
579 {
580         const char *logo_property = NULL;
581         GdkPixbuf *logo = NULL;
582
583         if (need_icon) {
584                 GtkWidget *image = gtk_image_new_from_file(subsurface_icon_name());
585
586                 if (image) {
587                         logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
588                         logo_property = "logo";
589                 }
590         }
591
592         gtk_show_about_dialog(NULL,
593                 "program-name", "SubSurface",
594                 "comments", "Half-arsed divelog software in C",
595                 "license", "GPLv2",
596                 "version", VERSION_STRING,
597                 "copyright", "Linus Torvalds 2011",
598                 "logo-icon-name", "subsurface",
599                 /* Must be last: */
600                 logo_property, logo,
601                 NULL);
602 }
603
604 static void view_list(GtkWidget *w, gpointer data)
605 {
606         gtk_paned_set_position(GTK_PANED(vpane), 0);
607 }
608
609 static void view_profile(GtkWidget *w, gpointer data)
610 {
611         gtk_paned_set_position(GTK_PANED(hpane), 0);
612         gtk_paned_set_position(GTK_PANED(vpane), 65535);
613 }
614
615 static void view_info(GtkWidget *w, gpointer data)
616 {
617         gtk_paned_set_position(GTK_PANED(vpane), 65535);
618         gtk_paned_set_position(GTK_PANED(hpane), 65535);
619 }
620
621 static void view_three(GtkWidget *w, gpointer data)
622 {
623         GtkAllocation alloc;
624         gtk_widget_get_allocation(hpane, &alloc);
625         gtk_paned_set_position(GTK_PANED(hpane), alloc.width/2);
626         gtk_widget_get_allocation(vpane, &alloc);
627         gtk_paned_set_position(GTK_PANED(vpane), alloc.height/2);
628 }
629
630 static GtkActionEntry menu_items[] = {
631         { "FileMenuAction", NULL, "File", NULL, NULL, NULL},
632         { "LogMenuAction",  NULL, "Log", NULL, NULL, NULL},
633         { "ViewMenuAction",  NULL, "View", NULL, NULL, NULL},
634         { "FilterMenuAction",  NULL, "Filter", NULL, NULL, NULL},
635         { "HelpMenuAction", NULL, "Help", NULL, NULL, NULL},
636         { "OpenFile",       GTK_STOCK_OPEN, NULL,   CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
637         { "SaveFile",       GTK_STOCK_SAVE, NULL,   CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
638         { "SaveFileAs",     GTK_STOCK_SAVE_AS, NULL,   CTRLCHAR "<Shift>S", NULL, G_CALLBACK(file_save_as) },
639         { "Print",          GTK_STOCK_PRINT, NULL,  CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
640         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
641         { "AddDive",        GTK_STOCK_ADD, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) },
642         { "Preferences",    GTK_STOCK_PREFERENCES, "Preferences", PREFERENCE_ACCEL, NULL, G_CALLBACK(preferences_dialog) },
643         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
644         { "SelectEvents",   NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
645         { "Quit",           GTK_STOCK_QUIT, NULL,   CTRLCHAR "Q", NULL, G_CALLBACK(quit) },
646         { "About",          GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
647         { "ViewList",       NULL, "List",  CTRLCHAR "1", NULL, G_CALLBACK(view_list) },
648         { "ViewProfile",    NULL, "Profile", CTRLCHAR "2", NULL, G_CALLBACK(view_profile) },
649         { "ViewInfo",       NULL, "Info", CTRLCHAR "3", NULL, G_CALLBACK(view_info) },
650         { "ViewThree",       NULL, "Three", CTRLCHAR "4", NULL, G_CALLBACK(view_three) },
651 };
652 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
653
654 static const gchar* ui_string = " \
655         <ui> \
656                 <menubar name=\"MainMenu\"> \
657                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
658                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
659                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
660                                 <menuitem name=\"Save as\" action=\"SaveFileAs\" /> \
661                                 <menuitem name=\"Print\" action=\"Print\" /> \
662                                 <separator name=\"Separator1\"/> \
663                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
664                                 <separator name=\"Separator2\"/> \
665                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
666                         </menu> \
667                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
668                                 <menuitem name=\"Import\" action=\"Import\" /> \
669                                 <menuitem name=\"Add Dive\" action=\"AddDive\" /> \
670                                 <separator name=\"Separator\"/> \
671                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
672                                 <menu name=\"View\" action=\"ViewMenuAction\"> \
673                                         <menuitem name=\"List\" action=\"ViewList\" /> \
674                                         <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
675                                         <menuitem name=\"Info\" action=\"ViewInfo\" /> \
676                                         <menuitem name=\"Paned\" action=\"ViewThree\" /> \
677                                 </menu> \
678                         </menu> \
679                         <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
680                                 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
681                         </menu> \
682                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
683                                 <menuitem name=\"About\" action=\"About\" /> \
684                         </menu> \
685                 </menubar> \
686         </ui> \
687 ";
688
689 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
690 {
691         GtkActionGroup *action_group = gtk_action_group_new("Menu");
692         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
693
694         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
695         GError* error = 0;
696         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
697
698         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
699         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
700
701         return menu;
702 }
703
704 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
705 {
706         repaint_dive();
707 }
708
709 void init_ui(int *argcp, char ***argvp)
710 {
711         GtkWidget *win;
712         GtkWidget *notebook;
713         GtkWidget *nb_page;
714         GtkWidget *dive_list;
715         GtkWidget *menubar;
716         GtkWidget *vbox;
717         GdkScreen *screen;
718         GtkIconTheme *icon_theme=NULL;
719         GtkSettings *settings;
720         GtkUIManager *ui_manager;
721
722         gtk_init(argcp, argvp);
723         settings = gtk_settings_get_default();
724         gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
725
726         g_type_init();
727
728         subsurface_open_conf();
729         if (subsurface_get_conf("feet", PREF_BOOL))
730                 output_units.length = FEET;
731         if (subsurface_get_conf("psi", PREF_BOOL))
732                 output_units.pressure = PSI;
733         if (subsurface_get_conf("cuft", PREF_BOOL))
734                 output_units.volume = CUFT;
735         if (subsurface_get_conf("fahrenheit", PREF_BOOL))
736                 output_units.temperature = FAHRENHEIT;
737         if (subsurface_get_conf("lbs", PREF_BOOL))
738                 output_units.weight = LBS;
739         /* an unset key is FALSE - all these are hidden by default */
740         visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
741         visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
742         visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
743         visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
744         visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
745
746         divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
747
748         default_dive_computer_vendor = subsurface_get_conf("dive_computer_vendor", PREF_STRING);
749         default_dive_computer_product = subsurface_get_conf("dive_computer_product", PREF_STRING);
750         default_dive_computer_device = subsurface_get_conf("dive_computer_device", PREF_STRING);
751
752         error_info_bar = NULL;
753         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
754         g_set_application_name ("subsurface");
755         /* Let's check if the subsurface icon has been installed or if
756          * we need to try to load it from the current directory */
757         screen = gdk_screen_get_default();
758         if (screen)
759                 icon_theme = gtk_icon_theme_get_for_screen(screen);
760         if (icon_theme) {
761                 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
762                         need_icon = FALSE;
763                         gtk_window_set_default_icon_name ("subsurface");
764                 }
765         }
766         if (need_icon) {
767                 const char *icon_name = subsurface_icon_name();
768                 if (!access(icon_name, R_OK))
769                         gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
770         }
771         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
772         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
773         main_window = win;
774
775         vbox = gtk_vbox_new(FALSE, 0);
776         gtk_container_add(GTK_CONTAINER(win), vbox);
777         main_vbox = vbox;
778
779         ui_manager = gtk_ui_manager_new();
780         menubar = get_menubar_menu(win, ui_manager);
781
782         subsurface_ui_setup(settings, menubar, vbox, ui_manager);
783
784         vpane = gtk_vpaned_new();
785         gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
786
787         hpane = gtk_hpaned_new();
788         gtk_paned_add1(GTK_PANED(vpane), hpane);
789
790         /* Notebook for dive info vs profile vs .. */
791         notebook = gtk_notebook_new();
792         gtk_paned_add1(GTK_PANED(hpane), notebook);
793         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
794
795         /* Create the actual divelist */
796         dive_list = dive_list_create();
797         gtk_widget_set_name(dive_list, "Dive List");
798         gtk_paned_add2(GTK_PANED(vpane), dive_list);
799
800         /* Frame for dive profile */
801         dive_profile = dive_profile_widget();
802         gtk_widget_set_name(dive_profile, "Dive Profile");
803         gtk_paned_add2(GTK_PANED(hpane), dive_profile);
804
805         /* Frame for extended dive info */
806         nb_page = extended_dive_info_widget();
807         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Notes"));
808
809         /* Frame for dive equipment */
810         nb_page = equipment_widget();
811         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Equipment"));
812
813         /* Frame for single dive statistics */
814         nb_page = single_stats_widget();
815         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
816
817         /* Frame for total dive statistics */
818         nb_page = total_stats_widget();
819         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Stats"));
820
821         gtk_widget_set_app_paintable(win, TRUE);
822         gtk_widget_show_all(win);
823
824         return;
825 }
826
827 void run_ui(void)
828 {
829         gtk_main();
830 }
831
832 void exit_ui(void)
833 {
834         subsurface_close_conf();
835 }
836
837 typedef struct {
838         cairo_rectangle_int_t rect;
839         const char *text;
840 } tooltip_record_t;
841
842 static tooltip_record_t *tooltip_rects;
843 static int tooltips;
844
845 void attach_tooltip(int x, int y, int w, int h, const char *text)
846 {
847         cairo_rectangle_int_t *rect;
848         tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
849         rect = &tooltip_rects[tooltips].rect;
850         rect->x = x;
851         rect->y = y;
852         rect->width = w;
853         rect->height = h;
854         tooltip_rects[tooltips].text = text;
855         tooltips++;
856 }
857
858 #define INSIDE_RECT(_r,_x,_y)   ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
859                                 (_r.y <= _y) && (_r.y + _r.height >= _y))
860
861 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
862                         gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
863 {
864         int i;
865         cairo_rectangle_int_t *drawing_area = user_data;
866         gint tx = x - drawing_area->x; /* get transformed coordinates */
867         gint ty = y - drawing_area->y;
868
869         /* are we over an event marker ? */
870         for (i = 0; i < tooltips; i++) {
871                 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
872                         gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
873                         return TRUE; /* show tooltip */
874                 }
875         }
876         return FALSE; /* don't show tooltip */
877 }
878
879 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
880 {
881         struct dive *dive = current_dive;
882         struct graphics_context gc = { .printer = 0 };
883         static cairo_rectangle_int_t drawing_area;
884
885         /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
886          * so effective drawing area is width-2x * height-2y */
887         drawing_area.width = widget->allocation.width;
888         drawing_area.height = widget->allocation.height;
889         drawing_area.x = drawing_area.width / 20.0;
890         drawing_area.y = drawing_area.height / 20.0;
891
892         gc.cr = gdk_cairo_create(widget->window);
893         g_object_set(widget, "has-tooltip", TRUE, NULL);
894         g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
895         init_profile_background(&gc);
896         cairo_paint(gc.cr);
897
898         if (dive) {
899                 if (tooltip_rects) {
900                         free(tooltip_rects);
901                         tooltip_rects = NULL;
902                 }
903                 tooltips = 0;
904                 plot(&gc, &drawing_area, dive);
905         }
906         cairo_destroy(gc.cr);
907
908         return FALSE;
909 }
910
911 GtkWidget *dive_profile_widget(void)
912 {
913         GtkWidget *da;
914
915         da = gtk_drawing_area_new();
916         gtk_widget_set_size_request(da, 350, 250);
917         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
918
919         return da;
920 }
921
922 int process_ui_events(void)
923 {
924         int ret=0;
925
926         while (gtk_events_pending()) {
927                 if (gtk_main_iteration_do(0)) {
928                         ret = 1;
929                         break;
930                 }
931         }
932         return ret;
933 }
934
935 static int fill_computer_list(GtkListStore *store)
936 {
937         int index = -1, i;
938         GtkTreeIter iter;
939         dc_iterator_t *iterator = NULL;
940         dc_descriptor_t *descriptor = NULL;
941
942         i = 0;
943         dc_descriptor_iterator(&iterator);
944         while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) {
945                 const char *vendor = dc_descriptor_get_vendor(descriptor);
946                 const char *product = dc_descriptor_get_product(descriptor);
947
948                 gtk_list_store_append(store, &iter);
949                 gtk_list_store_set(store, &iter,
950                         0, descriptor,
951                         -1);
952                 if (is_default_dive_computer(vendor, product))
953                         index = i;
954                 i++;
955         }
956         dc_iterator_free(iterator);
957         return index;
958 }
959
960 void render_dive_computer(GtkCellLayout *cell,
961                 GtkCellRenderer *renderer,
962                 GtkTreeModel *model,
963                 GtkTreeIter *iter,
964                 gpointer data)
965 {
966         char buffer[40];
967         dc_descriptor_t *descriptor = NULL;
968         const char *vendor, *product;
969
970         gtk_tree_model_get(model, iter, 0, &descriptor, -1);
971         vendor = dc_descriptor_get_vendor(descriptor);
972         product = dc_descriptor_get_product(descriptor);
973         snprintf(buffer, sizeof(buffer), "%s %s", vendor, product);
974         g_object_set(renderer, "text", buffer, NULL);
975 }
976
977
978 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
979 {
980         GtkWidget *hbox, *combo_box, *frame;
981         GtkListStore *model;
982         GtkCellRenderer *renderer;
983         int default_index;
984
985         hbox = gtk_hbox_new(FALSE, 6);
986         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
987
988         model = gtk_list_store_new(1, G_TYPE_POINTER);
989         default_index = fill_computer_list(model);
990
991         frame = gtk_frame_new("Dive computer");
992         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
993
994         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
995         gtk_container_add(GTK_CONTAINER(frame), combo_box);
996
997         renderer = gtk_cell_renderer_text_new();
998         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
999         gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo_box), renderer, render_dive_computer, NULL, NULL);
1000
1001         gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
1002
1003         return GTK_COMBO_BOX(combo_box);
1004 }
1005
1006 const char *subsurface_device_name()
1007 {
1008         if (!default_dive_computer_device || !*default_dive_computer_device)
1009                 return subsurface_USB_name();
1010         else
1011                 return default_dive_computer_device;
1012 }
1013
1014 static GtkEntry *dive_computer_device(GtkWidget *vbox)
1015 {
1016         GtkWidget *hbox, *entry, *frame;
1017
1018         hbox = gtk_hbox_new(FALSE, 6);
1019         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1020
1021         frame = gtk_frame_new("Device name");
1022         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1023
1024         entry = gtk_entry_new();
1025         gtk_container_add(GTK_CONTAINER(frame), entry);
1026         gtk_entry_set_text(GTK_ENTRY(entry), subsurface_device_name());
1027
1028         return GTK_ENTRY(entry);
1029 }
1030
1031 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
1032 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
1033 {
1034         GtkDialog *main_dialog = _data;
1035
1036         gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
1037 }
1038
1039 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
1040 {
1041         GtkWidget *hbox, *frame, *chooser, *dialog;
1042         GtkFileFilter *filter;
1043
1044         hbox = gtk_hbox_new(FALSE, 6);
1045         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1046
1047         frame = gtk_frame_new("XML file name");
1048         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1049         dialog = gtk_file_chooser_dialog_new("Open XML File",
1050                 GTK_WINDOW(main_window),
1051                 GTK_FILE_CHOOSER_ACTION_OPEN,
1052                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1053                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1054                 NULL);
1055         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
1056
1057         filter = gtk_file_filter_new();
1058         gtk_file_filter_add_pattern(filter, "*.xml");
1059         gtk_file_filter_add_pattern(filter, "*.XML");
1060         gtk_file_filter_add_pattern(filter, "*.sda");
1061         gtk_file_filter_add_pattern(filter, "*.SDA");
1062         gtk_file_filter_add_mime_type(filter, "text/xml");
1063         gtk_file_filter_set_name(filter, "XML file");
1064         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
1065
1066         chooser = gtk_file_chooser_button_new_with_dialog(dialog);
1067         g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
1068
1069         gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
1070         gtk_container_add(GTK_CONTAINER(frame), chooser);
1071
1072         return chooser;
1073 }
1074
1075 static void do_import_file(gpointer data, gpointer user_data)
1076 {
1077         GError *error = NULL;
1078         parse_file(data, &error);
1079
1080         if (error != NULL)
1081         {
1082                 report_error(error);
1083                 g_error_free(error);
1084                 error = NULL;
1085         }
1086 }
1087
1088 static GtkWidget *import_dive_computer(device_data_t *data, GtkDialog *dialog)
1089 {
1090         GError *error;
1091         GtkWidget *vbox, *info, *container, *label, *button;
1092
1093         error = do_import(data);
1094         if (!error)
1095                 return NULL;
1096
1097         button = gtk_dialog_get_widget_for_response(dialog, GTK_RESPONSE_ACCEPT);
1098         gtk_button_set_use_stock(GTK_BUTTON(button), 0);
1099         gtk_button_set_label(GTK_BUTTON(button), "Retry");
1100
1101         vbox = gtk_dialog_get_content_area(dialog);
1102
1103         info = gtk_info_bar_new();
1104         container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
1105         label = gtk_label_new(error->message);
1106         gtk_container_add(GTK_CONTAINER(container), label);
1107         gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
1108         return info;
1109 }
1110
1111 void import_dialog(GtkWidget *w, gpointer data)
1112 {
1113         int result;
1114         GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
1115         GtkComboBox *computer;
1116         GtkEntry *device;
1117         GtkWidget *XMLchooser;
1118         device_data_t devicedata = {
1119                 .devname = NULL,
1120         };
1121
1122         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1123                 GTK_WINDOW(main_window),
1124                 GTK_DIALOG_DESTROY_WITH_PARENT,
1125                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1126                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1127                 NULL);
1128
1129         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1130         label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1131         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1132         XMLchooser = xml_file_selector(vbox, dialog);
1133         computer = dive_computer_selector(vbox);
1134         device = dive_computer_device(vbox);
1135         hbox = gtk_hbox_new(FALSE, 6);
1136         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1137         devicedata.progress.bar = gtk_progress_bar_new();
1138         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1139
1140 repeat:
1141         gtk_widget_show_all(dialog);
1142         result = gtk_dialog_run(GTK_DIALOG(dialog));
1143         switch (result) {
1144                 dc_descriptor_t *descriptor;
1145                 GtkTreeIter iter;
1146                 GtkTreeModel *model;
1147                 GSList *list;
1148         case GTK_RESPONSE_ACCEPT:
1149                 /* what happened - did the user pick a file? In that case
1150                  * we ignore whether a dive computer model was picked */
1151                 if (info)
1152                         gtk_widget_destroy(info);
1153                 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1154                 if (g_slist_length(list) == 0) {
1155                         const char *vendor, *product;
1156
1157                         if (!gtk_combo_box_get_active_iter(computer, &iter))
1158                                 break;
1159                         model = gtk_combo_box_get_model(computer);
1160                         gtk_tree_model_get(model, &iter,
1161                                         0, &descriptor,
1162                                         -1);
1163
1164                         vendor = dc_descriptor_get_vendor(descriptor);
1165                         product = dc_descriptor_get_product(descriptor);
1166
1167                         devicedata.descriptor = descriptor;
1168                         devicedata.vendor = vendor;
1169                         devicedata.product = product;
1170                         devicedata.devname = gtk_entry_get_text(device);
1171                         set_default_dive_computer(vendor, product);
1172                         set_default_dive_computer_device(devicedata.devname);
1173                         info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
1174                         if (info)
1175                                 goto repeat;
1176                 } else {
1177                         g_slist_foreach(list,do_import_file,NULL);
1178                         g_slist_free(list);
1179                 }
1180                 break;
1181         default:
1182                 break;
1183         }
1184         gtk_widget_destroy(dialog);
1185
1186         report_dives(TRUE);
1187 }
1188
1189 void update_progressbar(progressbar_t *progress, double value)
1190 {
1191         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1192 }
1193
1194 void update_progressbar_text(progressbar_t *progress, const char *text)
1195 {
1196         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), text);
1197 }
1198
1199 void set_filename(const char *filename)
1200 {
1201         if (existing_filename)
1202                 free(existing_filename);
1203         existing_filename = NULL;
1204         if (filename)
1205                 existing_filename = strdup(filename);
1206 }