]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
Use 'gtk_dialog_get_content_area()' instead of accessing dialog directly
[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(FALSE);
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 static gboolean on_delete(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
176         return FALSE; /* go ahead, kill the program, we're good now */
177 }
178
179 static void on_destroy(GtkWidget* w, gpointer data)
180 {
181         gtk_main_quit();
182 }
183
184 static void quit(GtkWidget *w, gpointer data)
185 {
186         /* Make sure to flush any modified dive data */
187         update_dive(NULL);
188
189         if (unsaved_changes())
190                 ask_save_changes();
191         gtk_main_quit();
192 }
193
194 static void create_radio(GtkWidget *vbox, const char *name, ...)
195 {
196         va_list args;
197         GtkRadioButton *group = NULL;
198         GtkWidget *box, *label;
199
200         box = gtk_hbox_new(TRUE, 10);
201         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
202
203         label = gtk_label_new(name);
204         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
205
206         va_start(args, name);
207         for (;;) {
208                 int enabled;
209                 const char *name;
210                 GtkWidget *button;
211                 void *callback_fn;
212
213                 name = va_arg(args, char *);
214                 if (!name)
215                         break;
216                 callback_fn = va_arg(args, void *);
217                 enabled = va_arg(args, int);
218
219                 button = gtk_radio_button_new_with_label_from_widget(group, name);
220                 group = GTK_RADIO_BUTTON(button);
221                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
222                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
223                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
224         }
225         va_end(args);
226 }
227
228 #define UNITCALLBACK(name, type, value)                         \
229 static void name(GtkWidget *w, gpointer data)                   \
230 {                                                               \
231         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
232                 menu_units.type = value;                        \
233 }
234
235 static struct units menu_units;
236
237 UNITCALLBACK(set_meter, length, METERS)
238 UNITCALLBACK(set_feet, length, FEET)
239 UNITCALLBACK(set_bar, pressure, BAR)
240 UNITCALLBACK(set_psi, pressure, PSI)
241 UNITCALLBACK(set_liter, volume, LITER)
242 UNITCALLBACK(set_cuft, volume, CUFT)
243 UNITCALLBACK(set_celsius, temperature, CELSIUS)
244 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
245
246 static void preferences_dialog(GtkWidget *w, gpointer data)
247 {
248         int result;
249         GtkWidget *dialog, *font, *frame, *box, *vbox;
250
251         menu_units = output_units;
252
253         dialog = gtk_dialog_new_with_buttons("Preferences",
254                 GTK_WINDOW(main_window),
255                 GTK_DIALOG_DESTROY_WITH_PARENT,
256                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
257                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
258                 NULL);
259
260         frame = gtk_frame_new("Units");
261         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
262         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
263
264         box = gtk_vbox_new(FALSE, 6);
265         gtk_container_add(GTK_CONTAINER(frame), box);
266
267         create_radio(box, "Depth:",
268                 "Meter", set_meter, (output_units.length == METERS),
269                 "Feet",  set_feet, (output_units.length == FEET),
270                 NULL);
271
272         create_radio(box, "Pressure:",
273                 "Bar", set_bar, (output_units.pressure == BAR),
274                 "PSI",  set_psi, (output_units.pressure == PSI),
275                 NULL);
276
277         create_radio(box, "Volume:",
278                 "Liter",  set_liter, (output_units.volume == LITER),
279                 "CuFt", set_cuft, (output_units.volume == CUFT),
280                 NULL);
281
282         create_radio(box, "Temperature:",
283                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
284                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
285                 NULL);
286
287         font = gtk_font_button_new_with_font(divelist_font);
288         gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
289
290         gtk_widget_show_all(dialog);
291         result = gtk_dialog_run(GTK_DIALOG(dialog));
292         if (result == GTK_RESPONSE_ACCEPT) {
293                 /* Make sure to flush any modified old dive data with old units */
294                 update_dive(NULL);
295
296                 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
297                 set_divelist_font(divelist_font);
298
299                 output_units = menu_units;
300                 update_dive_list_units();
301                 repaint_dive();
302                 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
303                 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
304                 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
305                 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
306                 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
307         }
308         gtk_widget_destroy(dialog);
309 }
310
311 static void renumber_dialog(GtkWidget *w, gpointer data)
312 {
313         int result;
314         GtkWidget *dialog, *frame, *button, *vbox;
315
316         dialog = gtk_dialog_new_with_buttons("Renumber",
317                 GTK_WINDOW(main_window),
318                 GTK_DIALOG_DESTROY_WITH_PARENT,
319                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
320                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
321                 NULL);
322
323         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
324
325         frame = gtk_frame_new("New starting number");
326         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
327
328         button = gtk_spin_button_new_with_range(1, 50000, 1);
329         gtk_container_add(GTK_CONTAINER(frame), button);
330
331         gtk_widget_show_all(dialog);
332         result = gtk_dialog_run(GTK_DIALOG(dialog));
333         if (result == GTK_RESPONSE_ACCEPT) {
334                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
335                 renumber_dives(nr);
336                 repaint_dive();
337         }
338         gtk_widget_destroy(dialog);
339 }
340
341 static GtkActionEntry menu_items[] = {
342         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
343         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
344         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
345         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
346         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
347         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
348         { "Preferences",    NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
349         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
350         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
351 };
352 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
353
354 static const gchar* ui_string = " \
355         <ui> \
356                 <menubar name=\"MainMenu\"> \
357                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
358                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
359                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
360                                 <menuitem name=\"Print\" action=\"Print\" /> \
361                                 <separator name=\"Separator1\"/> \
362                                 <menuitem name=\"Import\" action=\"Import\" /> \
363                                 <separator name=\"Separator2\"/> \
364                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
365                                 <separator name=\"Separator3\"/> \
366                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
367                         </menu> \
368                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
369                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
370                         </menu> \
371                 </menubar> \
372         </ui> \
373 ";
374
375 static GtkWidget *get_menubar_menu(GtkWidget *window)
376 {
377         GtkActionGroup *action_group = gtk_action_group_new("Menu");
378         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
379
380         GtkUIManager *ui_manager = gtk_ui_manager_new();
381         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
382         GError* error = 0;
383         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
384
385         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
386         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
387
388         return menu;
389 }
390
391 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
392 {
393         repaint_dive();
394 }
395
396 void init_ui(int argc, char **argv)
397 {
398         GtkWidget *win;
399         GtkWidget *paned;
400         GtkWidget *info_box;
401         GtkWidget *notebook;
402         GtkWidget *dive_info;
403         GtkWidget *dive_list;
404         GtkWidget *equipment;
405         GtkWidget *menubar;
406         GtkWidget *vbox;
407
408         gtk_init(&argc, &argv);
409
410         g_type_init();
411         gconf = gconf_client_get_default();
412
413         if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
414                 output_units.length = FEET;
415         if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
416                 output_units.pressure = PSI;
417         if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
418                 output_units.volume = CUFT;
419         if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
420                 output_units.temperature = FAHRENHEIT;
421
422         divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
423         if (!divelist_font)
424                 divelist_font = DIVELIST_DEFAULT_FONT;
425
426         error_info_bar = NULL;
427         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
428         gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
429         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK (on_delete), NULL);
430         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
431         main_window = win;
432
433         vbox = gtk_vbox_new(FALSE, 0);
434         gtk_container_add(GTK_CONTAINER(win), vbox);
435         main_vbox = vbox;
436
437         menubar = get_menubar_menu(win);
438         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
439
440         /* HPane for left the dive list, and right the dive info */
441         paned = gtk_vpaned_new();
442         gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
443
444         /* Create the actual divelist */
445         dive_list = dive_list_create();
446         gtk_paned_add2(GTK_PANED(paned), dive_list);
447
448         /* VBox for dive info, and tabs */
449         info_box = gtk_vbox_new(FALSE, 6);
450         gtk_paned_add1(GTK_PANED(paned), info_box);
451
452         /* Notebook for dive info vs profile vs .. */
453         notebook = gtk_notebook_new();
454         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
455         gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
456
457         /* Frame for dive profile */
458         dive_profile = dive_profile_widget();
459         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
460
461         /* Frame for extended dive info */
462         dive_info = extended_dive_info_widget();
463         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
464
465         /* Frame for dive equipment */
466         equipment = equipment_widget();
467         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
468
469         gtk_widget_set_app_paintable(win, TRUE);
470         gtk_widget_show_all(win);
471
472         return;
473 }
474
475 void run_ui(void)
476 {
477         gtk_main();
478 }
479
480 /* get the filenames the user selects and call the parsing function
481  * on them
482  * return 0 if the user cancelled the dialog
483  */
484 int open_import_file_dialog(char *filterpattern, char *filtertext, 
485                         void(* parse_function)(char *))
486 {
487         int ret=0;
488
489         GtkWidget *dialog;
490         GtkFileFilter *filter = gtk_file_filter_new ();
491         gtk_file_filter_add_pattern (filter, filterpattern);
492         gtk_file_filter_set_name(filter, filtertext);
493         dialog = gtk_file_chooser_dialog_new("Open File",
494                                         GTK_WINDOW(main_window),
495                                         GTK_FILE_CHOOSER_ACTION_OPEN,
496                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
497                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
498                                         NULL);
499         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
500         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
501
502         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
503                 GSList *filenames;
504                 char *filename;
505                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
506                 while(filenames != NULL) {
507                         filename = (char *)filenames->data;
508                         parse_function(filename);
509                         g_free(filename);
510                         filenames = g_slist_next(filenames);
511                 }
512                 g_slist_free(filenames);
513                 ret = 1;
514         }
515         gtk_widget_destroy(dialog);
516
517         return ret;
518 }
519
520 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
521 {
522         struct dive *dive = current_dive;
523         struct graphics_context gc = { .printer = 0 };
524         int w,h;
525
526         w = widget->allocation.width;
527         h = widget->allocation.height;
528
529         gc.cr = gdk_cairo_create(widget->window);
530         set_source_rgb(&gc, 0, 0, 0);
531         cairo_paint(gc.cr);
532
533         if (dive)
534                 plot(&gc, w, h, dive);
535
536         cairo_destroy(gc.cr);
537
538         return FALSE;
539 }
540
541 GtkWidget *dive_profile_widget(void)
542 {
543         GtkWidget *da;
544
545         da = gtk_drawing_area_new();
546         gtk_widget_set_size_request(da, 350, 250);
547         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
548
549         return da;
550 }
551
552 int process_ui_events(void)
553 {
554         int ret=0;
555
556         while (gtk_events_pending()) {
557                 if (gtk_main_iteration_do(0)) {
558                         ret = 1;
559                         break;
560                 }
561         }
562         return(ret);
563 }
564
565
566 static void fill_computer_list(GtkListStore *store)
567 {
568         GtkTreeIter iter;
569         struct device_list *list = device_list;
570
571         for (list = device_list ; list->name ; list++) {
572                 gtk_list_store_append(store, &iter);
573                 gtk_list_store_set(store, &iter,
574                         0, list->name,
575                         1, list->type,
576                         -1);
577         }
578 }
579
580 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
581 {
582         GtkWidget *hbox, *combo_box, *vbox;
583         GtkListStore *model;
584         GtkCellRenderer *renderer;
585
586         hbox = gtk_hbox_new(FALSE, 6);
587         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
588         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
589
590         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
591         fill_computer_list(model);
592
593         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
594         gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3);
595
596         renderer = gtk_cell_renderer_text_new();
597         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
598         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
599
600         return GTK_COMBO_BOX(combo_box);
601 }
602
603 void import_dialog(GtkWidget *w, gpointer data)
604 {
605         int result;
606         GtkWidget *dialog, *hbox, *vbox;
607         GtkComboBox *computer;
608         device_data_t devicedata = {
609                 .devname = "/dev/ttyUSB0",
610         };
611
612         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
613                 GTK_WINDOW(main_window),
614                 GTK_DIALOG_DESTROY_WITH_PARENT,
615                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
616                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
617                 NULL);
618
619         computer = dive_computer_selector(dialog);
620
621         hbox = gtk_hbox_new(FALSE, 6);
622         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
623         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
624         devicedata.progress.bar = gtk_progress_bar_new();
625         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
626
627         gtk_widget_show_all(dialog);
628         result = gtk_dialog_run(GTK_DIALOG(dialog));
629         switch (result) {
630                 int type;
631                 GtkTreeIter iter;
632                 GtkTreeModel *model;
633                 const char *comp;
634         case GTK_RESPONSE_ACCEPT:
635                 if (!gtk_combo_box_get_active_iter(computer, &iter))
636                         break;
637                 model = gtk_combo_box_get_model(computer);
638                 gtk_tree_model_get(model, &iter,
639                         0, &comp,
640                         1, &type,
641                         -1);
642                 devicedata.type = type;
643                 devicedata.name = comp;
644                 do_import(&devicedata);
645                 break;
646         default:
647                 break;
648         }
649         gtk_widget_destroy(dialog);
650
651         report_dives();
652         dive_list_update_dives();
653 }
654
655 void update_progressbar(progressbar_t *progress, double value)
656 {
657         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
658 }
659
660
661 void set_filename(const char *filename)
662 {
663         if (filename)
664                 existing_filename = strdup(filename);
665         return;
666 }