]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
Merge branch 'trips' of git://git.hohndel.org/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) * (92 + 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 GtkActionEntry menu_items[] = {
733         { "FileMenuAction", NULL, "File", NULL, NULL, NULL},
734         { "LogMenuAction",  NULL, "Log", NULL, NULL, NULL},
735         { "ViewMenuAction",  NULL, "View", NULL, NULL, NULL},
736         { "FilterMenuAction",  NULL, "Filter", NULL, NULL, NULL},
737         { "HelpMenuAction", NULL, "Help", NULL, NULL, NULL},
738         { "OpenFile",       GTK_STOCK_OPEN, NULL,   CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
739         { "SaveFile",       GTK_STOCK_SAVE, NULL,   CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
740         { "SaveAsFile",     GTK_STOCK_SAVE_AS, NULL,   SHIFTCHAR CTRLCHAR "S", NULL, G_CALLBACK(file_save_as) },
741         { "Print",          GTK_STOCK_PRINT, NULL,  CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
742         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
743         { "AddDive",        GTK_STOCK_ADD, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) },
744         { "Preferences",    GTK_STOCK_PREFERENCES, "Preferences", PREFERENCE_ACCEL, NULL, G_CALLBACK(preferences_dialog) },
745         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
746         { "SelectEvents",   NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
747         { "Quit",           GTK_STOCK_QUIT, NULL,   CTRLCHAR "Q", NULL, G_CALLBACK(quit) },
748         { "About",          GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
749         { "ViewList",       NULL, "List",  CTRLCHAR "1", NULL, G_CALLBACK(view_list) },
750         { "ViewProfile",    NULL, "Profile", CTRLCHAR "2", NULL, G_CALLBACK(view_profile) },
751         { "ViewInfo",       NULL, "Info", CTRLCHAR "3", NULL, G_CALLBACK(view_info) },
752         { "ViewThree",       NULL, "Three", CTRLCHAR "4", NULL, G_CALLBACK(view_three) },
753 };
754 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
755
756 static const gchar* ui_string = " \
757         <ui> \
758                 <menubar name=\"MainMenu\"> \
759                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
760                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
761                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
762                                 <menuitem name=\"Save As\" action=\"SaveAsFile\" /> \
763                                 <menuitem name=\"Print\" action=\"Print\" /> \
764                                 <separator name=\"Separator1\"/> \
765                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
766                                 <separator name=\"Separator2\"/> \
767                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
768                         </menu> \
769                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
770                                 <menuitem name=\"Import\" action=\"Import\" /> \
771                                 <menuitem name=\"Add Dive\" action=\"AddDive\" /> \
772                                 <separator name=\"Separator\"/> \
773                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
774                                 <menu name=\"View\" action=\"ViewMenuAction\"> \
775                                         <menuitem name=\"List\" action=\"ViewList\" /> \
776                                         <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
777                                         <menuitem name=\"Info\" action=\"ViewInfo\" /> \
778                                         <menuitem name=\"Paned\" action=\"ViewThree\" /> \
779                                 </menu> \
780                         </menu> \
781                         <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
782                                 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
783                         </menu> \
784                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
785                                 <menuitem name=\"About\" action=\"About\" /> \
786                         </menu> \
787                 </menubar> \
788         </ui> \
789 ";
790
791 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
792 {
793         GtkActionGroup *action_group = gtk_action_group_new("Menu");
794         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
795
796         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
797         GError* error = 0;
798         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
799
800         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
801         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
802
803         return menu;
804 }
805
806 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
807 {
808         repaint_dive();
809 }
810
811 void init_ui(int *argcp, char ***argvp)
812 {
813         GtkWidget *win;
814         GtkWidget *nb_page;
815         GtkWidget *dive_list;
816         GtkWidget *menubar;
817         GtkWidget *vbox;
818         GtkWidget *scrolled;
819         GdkScreen *screen;
820         GtkIconTheme *icon_theme=NULL;
821         GtkSettings *settings;
822         GtkUIManager *ui_manager;
823
824         gtk_init(argcp, argvp);
825         settings = gtk_settings_get_default();
826         gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
827
828         g_type_init();
829
830         subsurface_open_conf();
831         if (subsurface_get_conf("feet", PREF_BOOL))
832                 output_units.length = FEET;
833         if (subsurface_get_conf("psi", PREF_BOOL))
834                 output_units.pressure = PSI;
835         if (subsurface_get_conf("cuft", PREF_BOOL))
836                 output_units.volume = CUFT;
837         if (subsurface_get_conf("fahrenheit", PREF_BOOL))
838                 output_units.temperature = FAHRENHEIT;
839         if (subsurface_get_conf("lbs", PREF_BOOL))
840                 output_units.weight = LBS;
841         /* an unset key is FALSE - all these are hidden by default */
842         visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
843         visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
844         visible_cols.totalweight = PTR_TO_BOOL(subsurface_get_conf("TOTALWEIGHT", PREF_BOOL));
845         visible_cols.suit = PTR_TO_BOOL(subsurface_get_conf("SUIT", PREF_BOOL));
846         visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
847         visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
848         visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
849
850         divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
851
852         autogroup = PTR_TO_BOOL(subsurface_get_conf("autogroup", PREF_BOOL));
853
854         default_dive_computer_vendor = subsurface_get_conf("dive_computer_vendor", PREF_STRING);
855         default_dive_computer_product = subsurface_get_conf("dive_computer_product", PREF_STRING);
856         default_dive_computer_device = subsurface_get_conf("dive_computer_device", PREF_STRING);
857
858         error_info_bar = NULL;
859         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
860         g_set_application_name ("subsurface");
861         /* Let's check if the subsurface icon has been installed or if
862          * we need to try to load it from the current directory */
863         screen = gdk_screen_get_default();
864         if (screen)
865                 icon_theme = gtk_icon_theme_get_for_screen(screen);
866         if (icon_theme) {
867                 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
868                         need_icon = FALSE;
869                         gtk_window_set_default_icon_name ("subsurface");
870                 }
871         }
872         if (need_icon) {
873                 const char *icon_name = subsurface_icon_name();
874                 if (!access(icon_name, R_OK))
875                         gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
876         }
877         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
878         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
879         main_window = win;
880
881         vbox = gtk_vbox_new(FALSE, 0);
882         gtk_container_add(GTK_CONTAINER(win), vbox);
883         main_vbox = vbox;
884
885         ui_manager = gtk_ui_manager_new();
886         menubar = get_menubar_menu(win, ui_manager);
887
888         subsurface_ui_setup(settings, menubar, vbox, ui_manager);
889
890         vpane = gtk_vpaned_new();
891         gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
892         hpane = gtk_hpaned_new();
893         gtk_paned_add1(GTK_PANED(vpane), hpane);
894         g_signal_connect_after(G_OBJECT(vbox), "realize", G_CALLBACK(view_three), NULL);
895
896         /* Notebook for dive info vs profile vs .. */
897         notebook = gtk_notebook_new();
898         scrolled = gtk_scrolled_window_new(NULL, NULL);
899         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
900         gtk_paned_add1(GTK_PANED(hpane), scrolled);
901         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), notebook);
902         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
903
904         /* Create the actual divelist */
905         dive_list = dive_list_create();
906         gtk_widget_set_name(dive_list, "Dive List");
907         gtk_paned_add2(GTK_PANED(vpane), dive_list);
908
909         /* Frame for dive profile */
910         dive_profile = dive_profile_widget();
911         gtk_widget_set_name(dive_profile, "Dive Profile");
912         gtk_paned_add2(GTK_PANED(hpane), dive_profile);
913
914         /* Frame for extended dive info */
915         nb_page = extended_dive_info_widget();
916         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Notes"));
917
918         /* Frame for dive equipment */
919         nb_page = equipment_widget(W_IDX_PRIMARY);
920         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Equipment"));
921
922         /* Frame for single dive statistics */
923         nb_page = single_stats_widget();
924         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
925
926         /* Frame for total dive statistics */
927         nb_page = total_stats_widget();
928         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Stats"));
929
930         gtk_widget_set_app_paintable(win, TRUE);
931         gtk_widget_show_all(win);
932
933         return;
934 }
935
936 void run_ui(void)
937 {
938         gtk_main();
939 }
940
941 void exit_ui(void)
942 {
943         subsurface_close_conf();
944 }
945
946 typedef struct {
947         cairo_rectangle_int_t rect;
948         const char *text;
949 } tooltip_record_t;
950
951 static tooltip_record_t *tooltip_rects;
952 static int tooltips;
953
954 void attach_tooltip(int x, int y, int w, int h, const char *text)
955 {
956         cairo_rectangle_int_t *rect;
957         tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
958         rect = &tooltip_rects[tooltips].rect;
959         rect->x = x;
960         rect->y = y;
961         rect->width = w;
962         rect->height = h;
963         tooltip_rects[tooltips].text = text;
964         tooltips++;
965 }
966
967 #define INSIDE_RECT(_r,_x,_y)   ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
968                                 (_r.y <= _y) && (_r.y + _r.height >= _y))
969
970 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
971                         gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
972 {
973         int i;
974         cairo_rectangle_int_t *drawing_area = user_data;
975         gint tx = x - drawing_area->x; /* get transformed coordinates */
976         gint ty = y - drawing_area->y;
977
978         /* are we over an event marker ? */
979         for (i = 0; i < tooltips; i++) {
980                 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
981                         gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
982                         return TRUE; /* show tooltip */
983                 }
984         }
985         return FALSE; /* don't show tooltip */
986 }
987
988 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
989 {
990         struct dive *dive = current_dive;
991         struct graphics_context gc = { .printer = 0 };
992         static cairo_rectangle_int_t drawing_area;
993
994         /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
995          * so effective drawing area is width-2x * height-2y */
996         drawing_area.width = widget->allocation.width;
997         drawing_area.height = widget->allocation.height;
998         drawing_area.x = drawing_area.width / 20.0;
999         drawing_area.y = drawing_area.height / 20.0;
1000
1001         gc.cr = gdk_cairo_create(widget->window);
1002         g_object_set(widget, "has-tooltip", TRUE, NULL);
1003         g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
1004         init_profile_background(&gc);
1005         cairo_paint(gc.cr);
1006
1007         if (dive) {
1008                 if (tooltip_rects) {
1009                         free(tooltip_rects);
1010                         tooltip_rects = NULL;
1011                 }
1012                 tooltips = 0;
1013                 plot(&gc, &drawing_area, dive);
1014         }
1015         cairo_destroy(gc.cr);
1016
1017         return FALSE;
1018 }
1019
1020 GtkWidget *dive_profile_widget(void)
1021 {
1022         GtkWidget *da;
1023
1024         da = gtk_drawing_area_new();
1025         gtk_widget_set_size_request(da, 350, 250);
1026         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
1027
1028         return da;
1029 }
1030
1031 int process_ui_events(void)
1032 {
1033         int ret=0;
1034
1035         while (gtk_events_pending()) {
1036                 if (gtk_main_iteration_do(0)) {
1037                         ret = 1;
1038                         break;
1039                 }
1040         }
1041         return ret;
1042 }
1043
1044 static int fill_computer_list(GtkListStore *store)
1045 {
1046         int index = -1, i;
1047         GtkTreeIter iter;
1048         dc_iterator_t *iterator = NULL;
1049         dc_descriptor_t *descriptor = NULL;
1050
1051         i = 0;
1052         dc_descriptor_iterator(&iterator);
1053         while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) {
1054                 const char *vendor = dc_descriptor_get_vendor(descriptor);
1055                 const char *product = dc_descriptor_get_product(descriptor);
1056
1057                 gtk_list_store_append(store, &iter);
1058                 gtk_list_store_set(store, &iter,
1059                         0, descriptor,
1060                         -1);
1061                 if (is_default_dive_computer(vendor, product))
1062                         index = i;
1063                 i++;
1064         }
1065         dc_iterator_free(iterator);
1066         return index;
1067 }
1068
1069 void render_dive_computer(GtkCellLayout *cell,
1070                 GtkCellRenderer *renderer,
1071                 GtkTreeModel *model,
1072                 GtkTreeIter *iter,
1073                 gpointer data)
1074 {
1075         char buffer[40];
1076         dc_descriptor_t *descriptor = NULL;
1077         const char *vendor, *product;
1078
1079         gtk_tree_model_get(model, iter, 0, &descriptor, -1);
1080         vendor = dc_descriptor_get_vendor(descriptor);
1081         product = dc_descriptor_get_product(descriptor);
1082         snprintf(buffer, sizeof(buffer), "%s %s", vendor, product);
1083         g_object_set(renderer, "text", buffer, NULL);
1084 }
1085
1086
1087 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
1088 {
1089         GtkWidget *hbox, *combo_box, *frame;
1090         GtkListStore *model;
1091         GtkCellRenderer *renderer;
1092         int default_index;
1093
1094         hbox = gtk_hbox_new(FALSE, 6);
1095         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1096
1097         model = gtk_list_store_new(1, G_TYPE_POINTER);
1098         default_index = fill_computer_list(model);
1099
1100         frame = gtk_frame_new("Dive computer");
1101         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1102
1103         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
1104         gtk_container_add(GTK_CONTAINER(frame), combo_box);
1105
1106         renderer = gtk_cell_renderer_text_new();
1107         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
1108         gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo_box), renderer, render_dive_computer, NULL, NULL);
1109
1110         gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
1111
1112         return GTK_COMBO_BOX(combo_box);
1113 }
1114
1115 const char *subsurface_device_name()
1116 {
1117         if (!default_dive_computer_device || !*default_dive_computer_device)
1118                 return subsurface_USB_name();
1119         else
1120                 return default_dive_computer_device;
1121 }
1122
1123 static GtkEntry *dive_computer_device(GtkWidget *vbox)
1124 {
1125         GtkWidget *hbox, *entry, *frame;
1126
1127         hbox = gtk_hbox_new(FALSE, 6);
1128         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1129
1130         frame = gtk_frame_new("Device name");
1131         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1132
1133         entry = gtk_entry_new();
1134         gtk_container_add(GTK_CONTAINER(frame), entry);
1135         gtk_entry_set_text(GTK_ENTRY(entry), subsurface_device_name());
1136
1137         return GTK_ENTRY(entry);
1138 }
1139
1140 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
1141 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
1142 {
1143         GtkDialog *main_dialog = _data;
1144
1145         gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
1146 }
1147
1148 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
1149 {
1150         GtkWidget *hbox, *frame, *chooser, *dialog;
1151         GtkFileFilter *filter;
1152
1153         hbox = gtk_hbox_new(FALSE, 6);
1154         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1155
1156         frame = gtk_frame_new("XML file name");
1157         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1158         dialog = gtk_file_chooser_dialog_new("Open XML File",
1159                 GTK_WINDOW(main_window),
1160                 GTK_FILE_CHOOSER_ACTION_OPEN,
1161                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1162                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1163                 NULL);
1164         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
1165
1166         filter = gtk_file_filter_new();
1167         gtk_file_filter_add_pattern(filter, "*.xml");
1168         gtk_file_filter_add_pattern(filter, "*.XML");
1169         gtk_file_filter_add_pattern(filter, "*.sda");
1170         gtk_file_filter_add_pattern(filter, "*.SDA");
1171         gtk_file_filter_add_mime_type(filter, "text/xml");
1172         gtk_file_filter_set_name(filter, "XML file");
1173         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
1174
1175         chooser = gtk_file_chooser_button_new_with_dialog(dialog);
1176         g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
1177
1178         gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
1179         gtk_container_add(GTK_CONTAINER(frame), chooser);
1180
1181         return chooser;
1182 }
1183
1184 static void do_import_file(gpointer data, gpointer user_data)
1185 {
1186         GError *error = NULL;
1187         parse_file(data, &error);
1188
1189         if (error != NULL)
1190         {
1191                 report_error(error);
1192                 g_error_free(error);
1193                 error = NULL;
1194         }
1195 }
1196
1197 static GtkWidget *import_dive_computer(device_data_t *data, GtkDialog *dialog)
1198 {
1199         GError *error;
1200         GtkWidget *vbox, *info, *container, *label, *button;
1201
1202         error = do_import(data);
1203         if (!error)
1204                 return NULL;
1205
1206         button = gtk_dialog_get_widget_for_response(dialog, GTK_RESPONSE_ACCEPT);
1207         gtk_button_set_use_stock(GTK_BUTTON(button), 0);
1208         gtk_button_set_label(GTK_BUTTON(button), "Retry");
1209
1210         vbox = gtk_dialog_get_content_area(dialog);
1211
1212         info = gtk_info_bar_new();
1213         container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
1214         label = gtk_label_new(error->message);
1215         gtk_container_add(GTK_CONTAINER(container), label);
1216         gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
1217         return info;
1218 }
1219
1220 void import_dialog(GtkWidget *w, gpointer data)
1221 {
1222         int result;
1223         GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
1224         GtkComboBox *computer;
1225         GtkEntry *device;
1226         GtkWidget *XMLchooser;
1227         device_data_t devicedata = {
1228                 .devname = NULL,
1229         };
1230
1231         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1232                 GTK_WINDOW(main_window),
1233                 GTK_DIALOG_DESTROY_WITH_PARENT,
1234                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1235                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1236                 NULL);
1237
1238         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1239         label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1240         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1241         XMLchooser = xml_file_selector(vbox, dialog);
1242         computer = dive_computer_selector(vbox);
1243         device = dive_computer_device(vbox);
1244         hbox = gtk_hbox_new(FALSE, 6);
1245         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1246         devicedata.progress.bar = gtk_progress_bar_new();
1247         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1248
1249 repeat:
1250         gtk_widget_show_all(dialog);
1251         result = gtk_dialog_run(GTK_DIALOG(dialog));
1252         switch (result) {
1253                 dc_descriptor_t *descriptor;
1254                 GtkTreeIter iter;
1255                 GtkTreeModel *model;
1256                 GSList *list;
1257         case GTK_RESPONSE_ACCEPT:
1258                 /* what happened - did the user pick a file? In that case
1259                  * we ignore whether a dive computer model was picked */
1260                 if (info)
1261                         gtk_widget_destroy(info);
1262                 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1263                 if (g_slist_length(list) == 0) {
1264                         const char *vendor, *product;
1265
1266                         if (!gtk_combo_box_get_active_iter(computer, &iter))
1267                                 break;
1268                         model = gtk_combo_box_get_model(computer);
1269                         gtk_tree_model_get(model, &iter,
1270                                         0, &descriptor,
1271                                         -1);
1272
1273                         vendor = dc_descriptor_get_vendor(descriptor);
1274                         product = dc_descriptor_get_product(descriptor);
1275
1276                         devicedata.descriptor = descriptor;
1277                         devicedata.vendor = vendor;
1278                         devicedata.product = product;
1279                         devicedata.devname = gtk_entry_get_text(device);
1280                         set_default_dive_computer(vendor, product);
1281                         set_default_dive_computer_device(devicedata.devname);
1282                         info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
1283                         if (info)
1284                                 goto repeat;
1285                 } else {
1286                         g_slist_foreach(list,do_import_file,NULL);
1287                         g_slist_free(list);
1288                 }
1289                 break;
1290         default:
1291                 break;
1292         }
1293         gtk_widget_destroy(dialog);
1294
1295         report_dives(TRUE);
1296 }
1297
1298 void update_progressbar(progressbar_t *progress, double value)
1299 {
1300         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1301 }
1302
1303 void update_progressbar_text(progressbar_t *progress, const char *text)
1304 {
1305         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), text);
1306 }
1307
1308 void set_filename(const char *filename)
1309 {
1310         if (existing_filename)
1311                 free(existing_filename);
1312         existing_filename = NULL;
1313         if (filename)
1314                 existing_filename = strdup(filename);
1315 }