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