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