]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
Check for changes at regular 'quit' events as well
[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
11 #include <gconf/gconf-client.h>
12
13 #include "dive.h"
14 #include "divelist.h"
15 #include "display.h"
16 #include "display-gtk.h"
17
18 #include "libdivecomputer.h"
19
20 GtkWidget *main_window;
21 GtkWidget *main_vbox;
22 GtkWidget *error_info_bar;
23 GtkWidget *error_label;
24 int        error_count;
25
26 #define DIVELIST_DEFAULT_FONT "Sans 8"
27 const char *divelist_font;
28
29 GConfClient *gconf;
30 struct units output_units;
31
32 #define GCONF_NAME(x) "/apps/subsurface/" #x
33
34 static GtkWidget *dive_profile;
35
36 void repaint_dive(void)
37 {
38         update_dive(current_dive);
39         gtk_widget_queue_draw(dive_profile);
40 }
41
42 static char *existing_filename;
43
44 static void on_info_bar_response(GtkWidget *widget, gint response,
45                                  gpointer data)
46 {
47         if (response == GTK_RESPONSE_OK)
48         {
49                 gtk_widget_destroy(widget);
50                 error_info_bar = NULL;
51         }
52 }
53
54 void report_error(GError* error)
55 {
56         if (error == NULL)
57         {
58                 return;
59         }
60         
61         if (error_info_bar == NULL)
62         {
63                 error_count = 1;
64                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
65                                                                GTK_RESPONSE_OK,
66                                                                NULL);
67                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
68                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
69                                               GTK_MESSAGE_ERROR);
70                 
71                 error_label = gtk_label_new(error->message);
72                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
73                 gtk_container_add(GTK_CONTAINER(container), error_label);
74                 
75                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
76                 gtk_widget_show_all(main_vbox);
77         }
78         else
79         {
80                 error_count++;
81                 char buffer[256];
82                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
83                 gtk_label_set(GTK_LABEL(error_label), buffer);
84         }
85 }
86
87 static void file_open(GtkWidget *w, gpointer data)
88 {
89         GtkWidget *dialog;
90         dialog = gtk_file_chooser_dialog_new("Open File",
91                 GTK_WINDOW(main_window),
92                 GTK_FILE_CHOOSER_ACTION_OPEN,
93                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
94                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
95                 NULL);
96         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
97
98         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
99                 GSList *filenames;
100                 char *filename;
101                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
102                 
103                 GError *error = NULL;
104                 while(filenames != NULL) {
105                         filename = (char *)filenames->data;
106                         parse_xml_file(filename, &error);
107                         if (error != NULL)
108                         {
109                                 report_error(error);
110                                 g_error_free(error);
111                                 error = NULL;
112                         }
113                         
114                         g_free(filename);
115                         filenames = g_slist_next(filenames);
116                 }
117                 g_slist_free(filenames);
118                 report_dives();
119                 dive_list_update_dives();
120         }
121         gtk_widget_destroy(dialog);
122 }
123
124 static void file_save(GtkWidget *w, gpointer data)
125 {
126         GtkWidget *dialog;
127         dialog = gtk_file_chooser_dialog_new("Save File",
128                 GTK_WINDOW(main_window),
129                 GTK_FILE_CHOOSER_ACTION_SAVE,
130                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
131                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
132                 NULL);
133         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
134         if (!existing_filename) {
135                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
136         } else
137                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
138
139         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
140                 char *filename;
141                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
142                 save_dives(filename);
143                 g_free(filename);
144                 mark_divelist_changed(TRUE);
145         }
146         gtk_widget_destroy(dialog);
147 }
148
149 static void ask_save_changes()
150 {
151         GtkWidget *dialog, *label, *content;
152         dialog = gtk_dialog_new_with_buttons("Save Changes?",
153                 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
154                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
155                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
156                 NULL);
157         content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
158         label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
159         gtk_container_add (GTK_CONTAINER (content), label);
160         gtk_widget_show_all (dialog);
161         gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
162         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
163                 file_save(NULL,NULL);
164         }
165         gtk_widget_destroy(dialog);
166 }
167
168 void on_destroy(GtkWidget* w, gpointer data)
169 {
170         /* Make sure to flush any modified dive data */
171         update_dive(NULL);
172
173         if (unsaved_changes())
174                 ask_save_changes();
175         gtk_main_quit();
176 }
177
178 static void quit(GtkWidget *w, gpointer data)
179 {
180         /* Make sure to flush any modified dive data */
181         update_dive(NULL);
182
183         if (unsaved_changes())
184                 ask_save_changes();
185         gtk_main_quit();
186 }
187
188 static void create_radio(GtkWidget *vbox, const char *name, ...)
189 {
190         va_list args;
191         GtkRadioButton *group = NULL;
192         GtkWidget *box, *label;
193
194         box = gtk_hbox_new(TRUE, 10);
195         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
196
197         label = gtk_label_new(name);
198         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
199
200         va_start(args, name);
201         for (;;) {
202                 int enabled;
203                 const char *name;
204                 GtkWidget *button;
205                 void *callback_fn;
206
207                 name = va_arg(args, char *);
208                 if (!name)
209                         break;
210                 callback_fn = va_arg(args, void *);
211                 enabled = va_arg(args, int);
212
213                 button = gtk_radio_button_new_with_label_from_widget(group, name);
214                 group = GTK_RADIO_BUTTON(button);
215                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
216                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
217                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
218         }
219         va_end(args);
220 }
221
222 #define UNITCALLBACK(name, type, value)                         \
223 static void name(GtkWidget *w, gpointer data)                   \
224 {                                                               \
225         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
226                 menu_units.type = value;                        \
227 }
228
229 static struct units menu_units;
230
231 UNITCALLBACK(set_meter, length, METERS)
232 UNITCALLBACK(set_feet, length, FEET)
233 UNITCALLBACK(set_bar, pressure, BAR)
234 UNITCALLBACK(set_psi, pressure, PSI)
235 UNITCALLBACK(set_liter, volume, LITER)
236 UNITCALLBACK(set_cuft, volume, CUFT)
237 UNITCALLBACK(set_celsius, temperature, CELSIUS)
238 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
239
240 static void preferences_dialog(GtkWidget *w, gpointer data)
241 {
242         int result;
243         GtkWidget *dialog, *font, *frame, *box;
244
245         menu_units = output_units;
246
247         dialog = gtk_dialog_new_with_buttons("Preferences",
248                 GTK_WINDOW(main_window),
249                 GTK_DIALOG_DESTROY_WITH_PARENT,
250                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
251                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
252                 NULL);
253
254         frame = gtk_frame_new("Units");
255         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
256
257         box = gtk_vbox_new(FALSE, 6);
258         gtk_container_add(GTK_CONTAINER(frame), box);
259
260         create_radio(box, "Depth:",
261                 "Meter", set_meter, (output_units.length == METERS),
262                 "Feet",  set_feet, (output_units.length == FEET),
263                 NULL);
264
265         create_radio(box, "Pressure:",
266                 "Bar", set_bar, (output_units.pressure == BAR),
267                 "PSI",  set_psi, (output_units.pressure == PSI),
268                 NULL);
269
270         create_radio(box, "Volume:",
271                 "Liter",  set_liter, (output_units.volume == LITER),
272                 "CuFt", set_cuft, (output_units.volume == CUFT),
273                 NULL);
274
275         create_radio(box, "Temperature:",
276                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
277                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
278                 NULL);
279
280         font = gtk_font_button_new_with_font(divelist_font);
281         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), font, FALSE, FALSE, 5);
282
283         gtk_widget_show_all(dialog);
284         result = gtk_dialog_run(GTK_DIALOG(dialog));
285         if (result == GTK_RESPONSE_ACCEPT) {
286                 /* Make sure to flush any modified old dive data with old units */
287                 update_dive(NULL);
288
289                 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
290                 set_divelist_font(divelist_font);
291
292                 output_units = menu_units;
293                 update_dive_list_units();
294                 repaint_dive();
295                 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
296                 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
297                 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
298                 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
299                 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
300         }
301         gtk_widget_destroy(dialog);
302 }
303
304 static void renumber_dialog(GtkWidget *w, gpointer data)
305 {
306         int result;
307         GtkWidget *dialog, *frame, *button;
308
309         dialog = gtk_dialog_new_with_buttons("Renumber",
310                 GTK_WINDOW(main_window),
311                 GTK_DIALOG_DESTROY_WITH_PARENT,
312                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
313                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
314                 NULL);
315
316         frame = gtk_frame_new("New starting number");
317         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame);
318
319         button = gtk_spin_button_new_with_range(1, 50000, 1);
320         gtk_container_add(GTK_CONTAINER(frame), button);
321
322         gtk_widget_show_all(dialog);
323         result = gtk_dialog_run(GTK_DIALOG(dialog));
324         if (result == GTK_RESPONSE_ACCEPT) {
325                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
326                 renumber_dives(nr);
327                 repaint_dive();
328         }
329         gtk_widget_destroy(dialog);
330 }
331
332 static GtkActionEntry menu_items[] = {
333         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
334         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
335         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
336         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
337         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
338         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
339         { "Preferences",    NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
340         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
341         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
342 };
343 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
344
345 static const gchar* ui_string = " \
346         <ui> \
347                 <menubar name=\"MainMenu\"> \
348                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
349                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
350                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
351                                 <menuitem name=\"Print\" action=\"Print\" /> \
352                                 <separator name=\"Separator1\"/> \
353                                 <menuitem name=\"Import\" action=\"Import\" /> \
354                                 <separator name=\"Separator2\"/> \
355                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
356                                 <separator name=\"Separator3\"/> \
357                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
358                         </menu> \
359                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
360                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
361                         </menu> \
362                 </menubar> \
363         </ui> \
364 ";
365
366 static GtkWidget *get_menubar_menu(GtkWidget *window)
367 {
368         GtkActionGroup *action_group = gtk_action_group_new("Menu");
369         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
370
371         GtkUIManager *ui_manager = gtk_ui_manager_new();
372         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
373         GError* error = 0;
374         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
375
376         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
377         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
378
379         return menu;
380 }
381
382 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
383 {
384         repaint_dive();
385 }
386
387 void init_ui(int argc, char **argv)
388 {
389         GtkWidget *win;
390         GtkWidget *paned;
391         GtkWidget *info_box;
392         GtkWidget *notebook;
393         GtkWidget *dive_info;
394         GtkWidget *dive_list;
395         GtkWidget *equipment;
396         GtkWidget *menubar;
397         GtkWidget *vbox;
398
399         gtk_init(&argc, &argv);
400
401         g_type_init();
402         gconf = gconf_client_get_default();
403
404         if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
405                 output_units.length = FEET;
406         if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
407                 output_units.pressure = PSI;
408         if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
409                 output_units.volume = CUFT;
410         if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
411                 output_units.temperature = FAHRENHEIT;
412
413         divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
414         if (!divelist_font)
415                 divelist_font = DIVELIST_DEFAULT_FONT;
416
417         error_info_bar = NULL;
418         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
419         gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
420         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
421         main_window = win;
422
423         vbox = gtk_vbox_new(FALSE, 0);
424         gtk_container_add(GTK_CONTAINER(win), vbox);
425         main_vbox = vbox;
426
427         menubar = get_menubar_menu(win);
428         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
429
430         /* HPane for left the dive list, and right the dive info */
431         paned = gtk_vpaned_new();
432         gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
433
434         /* Create the actual divelist */
435         dive_list = dive_list_create();
436         gtk_paned_add2(GTK_PANED(paned), dive_list);
437
438         /* VBox for dive info, and tabs */
439         info_box = gtk_vbox_new(FALSE, 6);
440         gtk_paned_add1(GTK_PANED(paned), info_box);
441
442         /* Notebook for dive info vs profile vs .. */
443         notebook = gtk_notebook_new();
444         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
445         gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
446
447         /* Frame for dive profile */
448         dive_profile = dive_profile_widget();
449         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
450
451         /* Frame for extended dive info */
452         dive_info = extended_dive_info_widget();
453         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
454
455         /* Frame for dive equipment */
456         equipment = equipment_widget();
457         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
458
459         gtk_widget_set_app_paintable(win, TRUE);
460         gtk_widget_show_all(win);
461
462         return;
463 }
464
465 void run_ui(void)
466 {
467         gtk_main();
468 }
469
470 /* get the filenames the user selects and call the parsing function
471  * on them
472  * return 0 if the user cancelled the dialog
473  */
474 int open_import_file_dialog(char *filterpattern, char *filtertext, 
475                         void(* parse_function)(char *))
476 {
477         int ret=0;
478
479         GtkWidget *dialog;
480         GtkFileFilter *filter = gtk_file_filter_new ();
481         gtk_file_filter_add_pattern (filter, filterpattern);
482         gtk_file_filter_set_name(filter, filtertext);
483         dialog = gtk_file_chooser_dialog_new("Open File",
484                                         GTK_WINDOW(main_window),
485                                         GTK_FILE_CHOOSER_ACTION_OPEN,
486                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
487                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
488                                         NULL);
489         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
490         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
491
492         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
493                 GSList *filenames;
494                 char *filename;
495                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
496                 while(filenames != NULL) {
497                         filename = (char *)filenames->data;
498                         parse_function(filename);
499                         g_free(filename);
500                         filenames = g_slist_next(filenames);
501                 }
502                 g_slist_free(filenames);
503                 ret = 1;
504         }
505         gtk_widget_destroy(dialog);
506
507         return ret;
508 }
509
510 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
511 {
512         struct dive *dive = current_dive;
513         struct graphics_context gc = { .printer = 0 };
514         int w,h;
515
516         w = widget->allocation.width;
517         h = widget->allocation.height;
518
519         gc.cr = gdk_cairo_create(widget->window);
520         set_source_rgb(&gc, 0, 0, 0);
521         cairo_paint(gc.cr);
522
523         if (dive)
524                 plot(&gc, w, h, dive);
525
526         cairo_destroy(gc.cr);
527
528         return FALSE;
529 }
530
531 GtkWidget *dive_profile_widget(void)
532 {
533         GtkWidget *da;
534
535         da = gtk_drawing_area_new();
536         gtk_widget_set_size_request(da, 350, 250);
537         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
538
539         return da;
540 }
541
542 int process_ui_events(void)
543 {
544         int ret=0;
545
546         while (gtk_events_pending()) {
547                 if (gtk_main_iteration_do(0)) {
548                         ret = 1;
549                         break;
550                 }
551         }
552         return(ret);
553 }
554
555
556 static void fill_computer_list(GtkListStore *store)
557 {
558         GtkTreeIter iter;
559         struct device_list *list = device_list;
560
561         for (list = device_list ; list->name ; list++) {
562                 gtk_list_store_append(store, &iter);
563                 gtk_list_store_set(store, &iter,
564                         0, list->name,
565                         1, list->type,
566                         -1);
567         }
568 }
569
570 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
571 {
572         GtkWidget *hbox, *combo_box;
573         GtkListStore *model;
574         GtkCellRenderer *renderer;
575
576         hbox = gtk_hbox_new(FALSE, 6);
577         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 3);
578
579         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
580         fill_computer_list(model);
581
582         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
583         gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3);
584
585         renderer = gtk_cell_renderer_text_new();
586         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
587         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
588
589         return GTK_COMBO_BOX(combo_box);
590 }
591
592 void import_dialog(GtkWidget *w, gpointer data)
593 {
594         int result;
595         GtkWidget *dialog, *hbox;
596         GtkComboBox *computer;
597         device_data_t devicedata = {
598                 .devname = "/dev/ttyUSB0",
599         };
600
601         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
602                 GTK_WINDOW(main_window),
603                 GTK_DIALOG_DESTROY_WITH_PARENT,
604                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
605                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
606                 NULL);
607
608         computer = dive_computer_selector(dialog);
609
610         hbox = gtk_hbox_new(FALSE, 6);
611         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, TRUE, 3);
612         devicedata.progress->bar = gtk_progress_bar_new();
613         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress->bar);
614
615         gtk_widget_show_all(dialog);
616         result = gtk_dialog_run(GTK_DIALOG(dialog));
617         switch (result) {
618                 int type;
619                 GtkTreeIter iter;
620                 GtkTreeModel *model;
621                 const char *comp;
622         case GTK_RESPONSE_ACCEPT:
623                 if (!gtk_combo_box_get_active_iter(computer, &iter))
624                         break;
625                 model = gtk_combo_box_get_model(computer);
626                 gtk_tree_model_get(model, &iter,
627                         0, &comp,
628                         1, &type,
629                         -1);
630                 devicedata.type = type;
631                 devicedata.name = comp;
632                 do_import(&devicedata);
633                 break;
634         default:
635                 break;
636         }
637         gtk_widget_destroy(dialog);
638
639         report_dives();
640         dive_list_update_dives();
641 }
642
643 void update_progressbar(progressbar_t *progress, double value)
644 {
645         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
646 }