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