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