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