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