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