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