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