]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
Remove the ability to 'Import' .SDA files
[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
47 static void on_info_bar_response(GtkWidget *widget, gint response,
48                                  gpointer data)
49 {
50         if (response == GTK_RESPONSE_OK)
51         {
52                 gtk_widget_destroy(widget);
53                 error_info_bar = NULL;
54         }
55 }
56
57 void report_error(GError* error)
58 {
59         if (error == NULL)
60         {
61                 return;
62         }
63         
64         if (error_info_bar == NULL)
65         {
66                 error_count = 1;
67                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
68                                                                GTK_RESPONSE_OK,
69                                                                NULL);
70                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
71                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
72                                               GTK_MESSAGE_ERROR);
73                 
74                 error_label = gtk_label_new(error->message);
75                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
76                 gtk_container_add(GTK_CONTAINER(container), error_label);
77                 
78                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
79                 gtk_widget_show_all(main_vbox);
80         }
81         else
82         {
83                 error_count++;
84                 char buffer[256];
85                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
86                 gtk_label_set(GTK_LABEL(error_label), buffer);
87         }
88 }
89
90 static void file_open(GtkWidget *w, gpointer data)
91 {
92         GtkWidget *dialog;
93         GtkFileFilter *filter;
94
95         dialog = gtk_file_chooser_dialog_new("Open File",
96                 GTK_WINDOW(main_window),
97                 GTK_FILE_CHOOSER_ACTION_OPEN,
98                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
99                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
100                 NULL);
101         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
102
103         filter = gtk_file_filter_new();
104         gtk_file_filter_add_pattern(filter, "*.xml");
105         gtk_file_filter_add_pattern(filter, "*.XML");
106         gtk_file_filter_add_pattern(filter, "*.sda");
107         gtk_file_filter_add_pattern(filter, "*.SDA");
108         gtk_file_filter_add_mime_type(filter, "text/xml");
109         gtk_file_filter_set_name(filter, "XML file");
110         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
111
112         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
113                 GSList *filenames;
114                 char *filename;
115                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
116                 
117                 GError *error = NULL;
118                 while(filenames != NULL) {
119                         filename = (char *)filenames->data;
120                         parse_xml_file(filename, &error);
121                         if (error != NULL)
122                         {
123                                 report_error(error);
124                                 g_error_free(error);
125                                 error = NULL;
126                         }
127                         
128                         g_free(filename);
129                         filenames = g_slist_next(filenames);
130                 }
131                 g_slist_free(filenames);
132                 report_dives();
133                 dive_list_update_dives();
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 static void create_radio(GtkWidget *vbox, const char *name, ...)
209 {
210         va_list args;
211         GtkRadioButton *group = NULL;
212         GtkWidget *box, *label;
213
214         box = gtk_hbox_new(TRUE, 10);
215         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
216
217         label = gtk_label_new(name);
218         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
219
220         va_start(args, name);
221         for (;;) {
222                 int enabled;
223                 const char *name;
224                 GtkWidget *button;
225                 void *callback_fn;
226
227                 name = va_arg(args, char *);
228                 if (!name)
229                         break;
230                 callback_fn = va_arg(args, void *);
231                 enabled = va_arg(args, int);
232
233                 button = gtk_radio_button_new_with_label_from_widget(group, name);
234                 group = GTK_RADIO_BUTTON(button);
235                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
236                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
237                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
238         }
239         va_end(args);
240 }
241
242 #define UNITCALLBACK(name, type, value)                         \
243 static void name(GtkWidget *w, gpointer data)                   \
244 {                                                               \
245         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
246                 menu_units.type = value;                        \
247 }
248
249 static struct units menu_units;
250
251 UNITCALLBACK(set_meter, length, METERS)
252 UNITCALLBACK(set_feet, length, FEET)
253 UNITCALLBACK(set_bar, pressure, BAR)
254 UNITCALLBACK(set_psi, pressure, PSI)
255 UNITCALLBACK(set_liter, volume, LITER)
256 UNITCALLBACK(set_cuft, volume, CUFT)
257 UNITCALLBACK(set_celsius, temperature, CELSIUS)
258 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
259
260 #define OPTIONCALLBACK(name, option) \
261 static void name(GtkWidget *w, gpointer data) \
262 { \
263         option = GTK_TOGGLE_BUTTON(w)->active; \
264 }
265
266 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
267 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
268
269 static void preferences_dialog(GtkWidget *w, gpointer data)
270 {
271         int result;
272         GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
273
274         menu_units = output_units;
275
276         dialog = gtk_dialog_new_with_buttons("Preferences",
277                 GTK_WINDOW(main_window),
278                 GTK_DIALOG_DESTROY_WITH_PARENT,
279                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
280                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
281                 NULL);
282
283         frame = gtk_frame_new("Units");
284         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
285         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
286
287         box = gtk_vbox_new(FALSE, 6);
288         gtk_container_add(GTK_CONTAINER(frame), box);
289
290         create_radio(box, "Depth:",
291                 "Meter", set_meter, (output_units.length == METERS),
292                 "Feet",  set_feet, (output_units.length == FEET),
293                 NULL);
294
295         create_radio(box, "Pressure:",
296                 "Bar", set_bar, (output_units.pressure == BAR),
297                 "PSI",  set_psi, (output_units.pressure == PSI),
298                 NULL);
299
300         create_radio(box, "Volume:",
301                 "Liter",  set_liter, (output_units.volume == LITER),
302                 "CuFt", set_cuft, (output_units.volume == CUFT),
303                 NULL);
304
305         create_radio(box, "Temperature:",
306                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
307                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
308                 NULL);
309
310         frame = gtk_frame_new("Columns");
311         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
312
313         box = gtk_hbox_new(FALSE, 6);
314         gtk_container_add(GTK_CONTAINER(frame), box);
315
316         button = gtk_check_button_new_with_label("Show SAC");
317         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
318         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
319         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
320
321         button = gtk_check_button_new_with_label("Show OTU");
322         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
323         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
324         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
325
326         font = gtk_font_button_new_with_font(divelist_font);
327         gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
328
329         gtk_widget_show_all(dialog);
330         result = gtk_dialog_run(GTK_DIALOG(dialog));
331         if (result == GTK_RESPONSE_ACCEPT) {
332                 /* Make sure to flush any modified old dive data with old units */
333                 update_dive(NULL);
334
335                 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
336                 set_divelist_font(divelist_font);
337
338                 output_units = menu_units;
339                 update_dive_list_units();
340                 repaint_dive();
341                 update_dive_list_col_visibility();
342                 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
343                 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
344                 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
345                 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
346                 gconf_client_set_bool(gconf, GCONF_NAME(SAC), ! visible_cols.sac, NULL); /* inverted to get the correct default */
347                 gconf_client_set_bool(gconf, GCONF_NAME(OTU), visible_cols.otu, NULL);
348                 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
349         }
350         gtk_widget_destroy(dialog);
351 }
352
353 static void renumber_dialog(GtkWidget *w, gpointer data)
354 {
355         int result;
356         GtkWidget *dialog, *frame, *button, *vbox;
357
358         dialog = gtk_dialog_new_with_buttons("Renumber",
359                 GTK_WINDOW(main_window),
360                 GTK_DIALOG_DESTROY_WITH_PARENT,
361                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
362                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
363                 NULL);
364
365         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
366
367         frame = gtk_frame_new("New starting number");
368         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
369
370         button = gtk_spin_button_new_with_range(1, 50000, 1);
371         gtk_container_add(GTK_CONTAINER(frame), button);
372
373         gtk_widget_show_all(dialog);
374         result = gtk_dialog_run(GTK_DIALOG(dialog));
375         if (result == GTK_RESPONSE_ACCEPT) {
376                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
377                 renumber_dives(nr);
378                 repaint_dive();
379         }
380         gtk_widget_destroy(dialog);
381 }
382
383 static void about_dialog(GtkWidget *w, gpointer data)
384 {
385         const char *logo_property = NULL;
386         GdkPixbuf *logo = NULL;
387         GtkWidget *image = gtk_image_new_from_file("icon.svg");
388
389         if (image) {
390                 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
391                 logo_property = "logo";
392         }
393
394         gtk_show_about_dialog(NULL,
395                 "program-name", "SubSurface",
396                 "comments", "Half-arsed divelog software in C",
397                 "license", "GPLv2",
398                 "version", VERSION_STRING,
399                 "copyright", "Linus Torvalds 2011",
400                 /* Must be last: */
401                 logo_property, logo,
402                 NULL);
403 }
404
405 static GtkActionEntry menu_items[] = {
406         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
407         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
408         { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
409         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
410         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
411         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
412         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
413         { "Preferences",    NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
414         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
415         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
416         { "About",           GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
417 };
418 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
419
420 static const gchar* ui_string = " \
421         <ui> \
422                 <menubar name=\"MainMenu\"> \
423                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
424                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
425                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
426                                 <menuitem name=\"Print\" action=\"Print\" /> \
427                                 <separator name=\"Separator1\"/> \
428                                 <menuitem name=\"Import\" action=\"Import\" /> \
429                                 <separator name=\"Separator2\"/> \
430                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
431                                 <separator name=\"Separator3\"/> \
432                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
433                         </menu> \
434                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
435                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
436                         </menu> \
437                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
438                                 <menuitem name=\"About\" action=\"About\" /> \
439                         </menu> \
440                 </menubar> \
441         </ui> \
442 ";
443
444 static GtkWidget *get_menubar_menu(GtkWidget *window)
445 {
446         GtkActionGroup *action_group = gtk_action_group_new("Menu");
447         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
448
449         GtkUIManager *ui_manager = gtk_ui_manager_new();
450         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
451         GError* error = 0;
452         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
453
454         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
455         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
456
457         return menu;
458 }
459
460 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
461 {
462         repaint_dive();
463 }
464
465 static const char notebook_group[] = "123";
466 #define GRP_ID ((void *)notebook_group)
467 typedef struct {
468         char *name;
469         GtkWidget *widget;
470         GtkWidget *box;
471         gulong delete_handler;
472         gulong destroy_handler;
473 } notebook_data_t;
474
475 static notebook_data_t nbd[2]; /* we rip at most two notebook pages off */
476
477 static GtkNotebook *create_new_notebook_window(GtkNotebook *source,
478                 GtkWidget *page,
479                 gint x, gint y, gpointer data)
480 {
481         GtkWidget *win, *notebook, *vbox;
482         notebook_data_t *nbdp;
483
484         /* pick the right notebook page data and return if both are detached */
485         if (nbd[0].widget == NULL)
486                 nbdp = nbd;
487         else if (nbd[1].widget == NULL)
488                 nbdp = nbd + 1;
489         else
490                 return NULL;
491
492         nbdp->name = strdup(gtk_widget_get_name(page));
493         nbdp->widget = win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
494         gtk_window_set_title(GTK_WINDOW(win), nbdp->name);
495         gtk_window_move(GTK_WINDOW(win), x, y);
496
497         /* Destroying the dive list will kill the application */
498         nbdp->delete_handler = g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
499         nbdp->destroy_handler = g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
500
501         nbdp->box = vbox = gtk_vbox_new(FALSE, 0);
502         gtk_container_add(GTK_CONTAINER(win), vbox);
503
504         notebook = gtk_notebook_new();
505         gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
506         gtk_widget_set_name(notebook, nbdp->name);
507         /* disallow drop events */
508         gtk_drag_dest_set(notebook, 0, NULL, 0, 0);
509         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
510         gtk_widget_set_size_request(notebook, 450, 350);
511
512         gtk_widget_show_all(win);
513         return GTK_NOTEBOOK(notebook);
514 }
515
516 static void drag_cb(GtkWidget *widget, GdkDragContext *context,
517         gint x, gint y, guint time,
518         gpointer user_data)
519 {
520         GtkWidget *source;
521         notebook_data_t *nbdp;
522
523         source = gtk_drag_get_source_widget(context);
524         if (nbd[0].name && ! strcmp(nbd[0].name,gtk_widget_get_name(source)))
525                 nbdp = nbd;
526         else if (nbd[1].name && ! strcmp(nbd[1].name,gtk_widget_get_name(source)))
527                 nbdp = nbd + 1;
528         else
529                 /* HU? */
530                 return;
531
532         gtk_drag_finish(context, TRUE, TRUE, time);
533
534         /* we no longer need the widget - but getting rid of this is hard;
535          * remove the signal handler, remove the notebook from the box
536          * then destroy the widget (and clear out our data structure) */
537         g_signal_handler_disconnect(nbdp->widget,nbdp->delete_handler);
538         g_signal_handler_disconnect(nbdp->widget,nbdp->destroy_handler);
539         gtk_container_remove(GTK_CONTAINER(nbdp->box), source);
540         gtk_widget_destroy(nbdp->widget);
541         nbdp->widget = NULL;
542         free(nbdp->name);
543         nbdp->name = NULL;
544 }
545
546 void init_ui(int argc, char **argv)
547 {
548         GtkWidget *win;
549         GtkWidget *notebook;
550         GtkWidget *dive_info;
551         GtkWidget *dive_list;
552         GtkWidget *equipment;
553         GtkWidget *menubar;
554         GtkWidget *vbox;
555         static const GtkTargetEntry notebook_target = {
556                 "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0
557         };
558
559         gtk_init(&argc, &argv);
560
561         g_type_init();
562         gconf = gconf_client_get_default();
563
564         if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
565                 output_units.length = FEET;
566         if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
567                 output_units.pressure = PSI;
568         if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
569                 output_units.volume = CUFT;
570         if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
571                 output_units.temperature = FAHRENHEIT;
572         /* an unset key is FALSE - so in order to get the default behavior right we 
573            invert the meaning of the SAC key */
574         visible_cols.otu = gconf_client_get_bool(gconf, GCONF_NAME(OTU), NULL);
575         visible_cols.sac = ! gconf_client_get_bool(gconf, GCONF_NAME(SAC), NULL);
576                 
577         divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
578         if (!divelist_font)
579                 divelist_font = DIVELIST_DEFAULT_FONT;
580
581         error_info_bar = NULL;
582         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
583         gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
584         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
585         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
586         main_window = win;
587
588         vbox = gtk_vbox_new(FALSE, 0);
589         gtk_container_add(GTK_CONTAINER(win), vbox);
590         main_vbox = vbox;
591
592         menubar = get_menubar_menu(win);
593         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
594
595         /* Notebook for dive info vs profile vs .. */
596         notebook = gtk_notebook_new();
597         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
598         gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
599         g_signal_connect(notebook, "create-window", G_CALLBACK(create_new_notebook_window), NULL);
600         gtk_drag_dest_set(notebook, GTK_DEST_DEFAULT_ALL, &notebook_target, 1, GDK_ACTION_MOVE);
601         g_signal_connect(notebook, "drag-drop", G_CALLBACK(drag_cb), notebook);
602         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
603
604         /* Create the actual divelist */
605         dive_list = dive_list_create();
606         gtk_widget_set_name(dive_list, "Dive List");
607         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_list, gtk_label_new("Dive List"));
608         gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_list, 1);
609         gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_list, 1);
610
611         /* Frame for dive profile */
612         dive_profile = dive_profile_widget();
613         gtk_widget_set_name(dive_profile, "Dive Profile");
614         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
615         gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_profile, 1);
616         gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_profile, 1);
617
618         /* Frame for extended dive info */
619         dive_info = extended_dive_info_widget();
620         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
621
622         /* Frame for dive equipment */
623         equipment = equipment_widget();
624         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
625
626         gtk_widget_set_app_paintable(win, TRUE);
627         gtk_widget_show_all(win);
628
629         return;
630 }
631
632 void run_ui(void)
633 {
634         gtk_main();
635 }
636
637 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
638 {
639         struct dive *dive = current_dive;
640         struct graphics_context gc = { .printer = 0 };
641         int w,h;
642
643         w = widget->allocation.width;
644         h = widget->allocation.height;
645
646         gc.cr = gdk_cairo_create(widget->window);
647         set_source_rgb(&gc, 0, 0, 0);
648         cairo_paint(gc.cr);
649
650         if (dive)
651                 plot(&gc, w, h, dive);
652
653         cairo_destroy(gc.cr);
654
655         return FALSE;
656 }
657
658 GtkWidget *dive_profile_widget(void)
659 {
660         GtkWidget *da;
661
662         da = gtk_drawing_area_new();
663         gtk_widget_set_size_request(da, 350, 250);
664         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
665
666         return da;
667 }
668
669 int process_ui_events(void)
670 {
671         int ret=0;
672
673         while (gtk_events_pending()) {
674                 if (gtk_main_iteration_do(0)) {
675                         ret = 1;
676                         break;
677                 }
678         }
679         return(ret);
680 }
681
682
683 static void fill_computer_list(GtkListStore *store)
684 {
685         GtkTreeIter iter;
686         struct device_list *list = device_list;
687
688         for (list = device_list ; list->name ; list++) {
689                 gtk_list_store_append(store, &iter);
690                 gtk_list_store_set(store, &iter,
691                         0, list->name,
692                         1, list->type,
693                         -1);
694         }
695 }
696
697 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
698 {
699         GtkWidget *hbox, *combo_box, *frame;
700         GtkListStore *model;
701         GtkCellRenderer *renderer;
702
703         hbox = gtk_hbox_new(FALSE, 6);
704         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
705
706         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
707         fill_computer_list(model);
708
709         frame = gtk_frame_new("Dive computer");
710         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
711
712         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
713         gtk_container_add(GTK_CONTAINER(frame), combo_box);
714
715         renderer = gtk_cell_renderer_text_new();
716         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
717         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
718
719         return GTK_COMBO_BOX(combo_box);
720 }
721
722 static GtkEntry *dive_computer_device(GtkWidget *vbox)
723 {
724         GtkWidget *hbox, *entry, *frame;
725
726         hbox = gtk_hbox_new(FALSE, 6);
727         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
728
729         frame = gtk_frame_new("Device name");
730         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
731
732         entry = gtk_entry_new();
733         gtk_container_add(GTK_CONTAINER(frame), entry);
734         gtk_entry_set_text(GTK_ENTRY(entry), "/dev/ttyUSB0");
735
736         return GTK_ENTRY(entry);
737 }
738
739 void import_dialog(GtkWidget *w, gpointer data)
740 {
741         int result;
742         GtkWidget *dialog, *hbox, *vbox;
743         GtkComboBox *computer;
744         GtkEntry *device;
745         device_data_t devicedata = {
746                 .devname = NULL,
747         };
748
749         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
750                 GTK_WINDOW(main_window),
751                 GTK_DIALOG_DESTROY_WITH_PARENT,
752                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
753                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
754                 NULL);
755
756         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
757
758         computer = dive_computer_selector(vbox);
759         device = dive_computer_device(vbox);
760
761         hbox = gtk_hbox_new(FALSE, 6);
762         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
763         devicedata.progress.bar = gtk_progress_bar_new();
764         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
765
766         gtk_widget_show_all(dialog);
767         result = gtk_dialog_run(GTK_DIALOG(dialog));
768         switch (result) {
769                 int type;
770                 GtkTreeIter iter;
771                 GtkTreeModel *model;
772                 const char *comp;
773         case GTK_RESPONSE_ACCEPT:
774                 if (!gtk_combo_box_get_active_iter(computer, &iter))
775                         break;
776                 model = gtk_combo_box_get_model(computer);
777                 gtk_tree_model_get(model, &iter,
778                         0, &comp,
779                         1, &type,
780                         -1);
781                 devicedata.type = type;
782                 devicedata.name = comp;
783                 devicedata.devname = gtk_entry_get_text(device);
784                 do_import(&devicedata);
785                 break;
786         default:
787                 break;
788         }
789         gtk_widget_destroy(dialog);
790
791         report_dives();
792         dive_list_update_dives();
793 }
794
795 void update_progressbar(progressbar_t *progress, double value)
796 {
797         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
798 }
799
800
801 void set_filename(const char *filename)
802 {
803         if (filename)
804                 existing_filename = strdup(filename);
805         return;
806 }