]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
Add drag-n-drop support to be able to re-integrate the dive list
[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, *divelist_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_mime_type(filter, "text/xml");
107         gtk_file_filter_set_name(filter, "XML file");
108         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
109
110         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
111                 GSList *filenames;
112                 char *filename;
113                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
114                 
115                 GError *error = NULL;
116                 while(filenames != NULL) {
117                         filename = (char *)filenames->data;
118                         parse_xml_file(filename, &error);
119                         if (error != NULL)
120                         {
121                                 report_error(error);
122                                 g_error_free(error);
123                                 error = NULL;
124                         }
125                         
126                         g_free(filename);
127                         filenames = g_slist_next(filenames);
128                 }
129                 g_slist_free(filenames);
130                 report_dives();
131                 dive_list_update_dives();
132         }
133         gtk_widget_destroy(dialog);
134 }
135
136 static void file_save(GtkWidget *w, gpointer data)
137 {
138         GtkWidget *dialog;
139         dialog = gtk_file_chooser_dialog_new("Save File",
140                 GTK_WINDOW(main_window),
141                 GTK_FILE_CHOOSER_ACTION_SAVE,
142                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
143                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
144                 NULL);
145         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
146         if (!existing_filename) {
147                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
148         } else
149                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
150
151         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
152                 char *filename;
153                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
154                 save_dives(filename);
155                 g_free(filename);
156                 mark_divelist_changed(FALSE);
157         }
158         gtk_widget_destroy(dialog);
159 }
160
161 static void ask_save_changes()
162 {
163         GtkWidget *dialog, *label, *content;
164         dialog = gtk_dialog_new_with_buttons("Save Changes?",
165                 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
166                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
167                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
168                 NULL);
169         content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
170         label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
171         gtk_container_add (GTK_CONTAINER (content), label);
172         gtk_widget_show_all (dialog);
173         gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
174         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
175                 file_save(NULL,NULL);
176         }
177         gtk_widget_destroy(dialog);
178 }
179
180 static gboolean on_delete(GtkWidget* w, gpointer data)
181 {
182         /* Make sure to flush any modified dive data */
183         update_dive(NULL);
184
185         if (unsaved_changes())
186                 ask_save_changes();
187
188         return FALSE; /* go ahead, kill the program, we're good now */
189 }
190
191 static void on_destroy(GtkWidget* w, gpointer data)
192 {
193         gtk_main_quit();
194 }
195
196 static void quit(GtkWidget *w, gpointer data)
197 {
198         /* Make sure to flush any modified dive data */
199         update_dive(NULL);
200
201         if (unsaved_changes())
202                 ask_save_changes();
203         gtk_main_quit();
204 }
205
206 static void create_radio(GtkWidget *vbox, const char *name, ...)
207 {
208         va_list args;
209         GtkRadioButton *group = NULL;
210         GtkWidget *box, *label;
211
212         box = gtk_hbox_new(TRUE, 10);
213         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
214
215         label = gtk_label_new(name);
216         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
217
218         va_start(args, name);
219         for (;;) {
220                 int enabled;
221                 const char *name;
222                 GtkWidget *button;
223                 void *callback_fn;
224
225                 name = va_arg(args, char *);
226                 if (!name)
227                         break;
228                 callback_fn = va_arg(args, void *);
229                 enabled = va_arg(args, int);
230
231                 button = gtk_radio_button_new_with_label_from_widget(group, name);
232                 group = GTK_RADIO_BUTTON(button);
233                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
234                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
235                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
236         }
237         va_end(args);
238 }
239
240 #define UNITCALLBACK(name, type, value)                         \
241 static void name(GtkWidget *w, gpointer data)                   \
242 {                                                               \
243         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
244                 menu_units.type = value;                        \
245 }
246
247 static struct units menu_units;
248
249 UNITCALLBACK(set_meter, length, METERS)
250 UNITCALLBACK(set_feet, length, FEET)
251 UNITCALLBACK(set_bar, pressure, BAR)
252 UNITCALLBACK(set_psi, pressure, PSI)
253 UNITCALLBACK(set_liter, volume, LITER)
254 UNITCALLBACK(set_cuft, volume, CUFT)
255 UNITCALLBACK(set_celsius, temperature, CELSIUS)
256 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
257
258 #define OPTIONCALLBACK(name, option) \
259 static void name(GtkWidget *w, gpointer data) \
260 { \
261         option = GTK_TOGGLE_BUTTON(w)->active; \
262 }
263
264 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
265 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
266
267 static void preferences_dialog(GtkWidget *w, gpointer data)
268 {
269         int result;
270         GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
271
272         menu_units = output_units;
273
274         dialog = gtk_dialog_new_with_buttons("Preferences",
275                 GTK_WINDOW(main_window),
276                 GTK_DIALOG_DESTROY_WITH_PARENT,
277                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
278                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
279                 NULL);
280
281         frame = gtk_frame_new("Units");
282         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
283         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
284
285         box = gtk_vbox_new(FALSE, 6);
286         gtk_container_add(GTK_CONTAINER(frame), box);
287
288         create_radio(box, "Depth:",
289                 "Meter", set_meter, (output_units.length == METERS),
290                 "Feet",  set_feet, (output_units.length == FEET),
291                 NULL);
292
293         create_radio(box, "Pressure:",
294                 "Bar", set_bar, (output_units.pressure == BAR),
295                 "PSI",  set_psi, (output_units.pressure == PSI),
296                 NULL);
297
298         create_radio(box, "Volume:",
299                 "Liter",  set_liter, (output_units.volume == LITER),
300                 "CuFt", set_cuft, (output_units.volume == CUFT),
301                 NULL);
302
303         create_radio(box, "Temperature:",
304                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
305                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
306                 NULL);
307
308         frame = gtk_frame_new("Columns");
309         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
310
311         box = gtk_hbox_new(FALSE, 6);
312         gtk_container_add(GTK_CONTAINER(frame), box);
313
314         button = gtk_check_button_new_with_label("Show SAC");
315         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
316         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
317         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
318
319         button = gtk_check_button_new_with_label("Show OTU");
320         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
321         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
322         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
323
324         font = gtk_font_button_new_with_font(divelist_font);
325         gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
326
327         gtk_widget_show_all(dialog);
328         result = gtk_dialog_run(GTK_DIALOG(dialog));
329         if (result == GTK_RESPONSE_ACCEPT) {
330                 /* Make sure to flush any modified old dive data with old units */
331                 update_dive(NULL);
332
333                 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
334                 set_divelist_font(divelist_font);
335
336                 output_units = menu_units;
337                 update_dive_list_units();
338                 repaint_dive();
339                 update_dive_list_col_visibility();
340                 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
341                 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
342                 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
343                 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
344                 gconf_client_set_bool(gconf, GCONF_NAME(SAC), ! visible_cols.sac, NULL); /* inverted to get the correct default */
345                 gconf_client_set_bool(gconf, GCONF_NAME(OTU), visible_cols.otu, NULL);
346                 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
347         }
348         gtk_widget_destroy(dialog);
349 }
350
351 static void renumber_dialog(GtkWidget *w, gpointer data)
352 {
353         int result;
354         GtkWidget *dialog, *frame, *button, *vbox;
355
356         dialog = gtk_dialog_new_with_buttons("Renumber",
357                 GTK_WINDOW(main_window),
358                 GTK_DIALOG_DESTROY_WITH_PARENT,
359                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
360                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
361                 NULL);
362
363         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
364
365         frame = gtk_frame_new("New starting number");
366         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
367
368         button = gtk_spin_button_new_with_range(1, 50000, 1);
369         gtk_container_add(GTK_CONTAINER(frame), button);
370
371         gtk_widget_show_all(dialog);
372         result = gtk_dialog_run(GTK_DIALOG(dialog));
373         if (result == GTK_RESPONSE_ACCEPT) {
374                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
375                 renumber_dives(nr);
376                 repaint_dive();
377         }
378         gtk_widget_destroy(dialog);
379 }
380
381 static void about_dialog(GtkWidget *w, gpointer data)
382 {
383         const char *logo_property = NULL;
384         GdkPixbuf *logo = NULL;
385         GtkWidget *image = gtk_image_new_from_file("icon.svg");
386
387         if (image) {
388                 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
389                 logo_property = "logo";
390         }
391
392         gtk_show_about_dialog(NULL,
393                 "program-name", "SubSurface",
394                 "comments", "Half-arsed divelog software in C",
395                 "license", "GPLv2",
396                 "version", VERSION_STRING,
397                 "copyright", "Linus Torvalds 2011",
398                 /* Must be last: */
399                 logo_property, logo,
400                 NULL);
401 }
402
403 static GtkActionEntry menu_items[] = {
404         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
405         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
406         { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
407         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
408         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
409         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
410         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
411         { "Preferences",    NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
412         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
413         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
414         { "About",           GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
415 };
416 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
417
418 static const gchar* ui_string = " \
419         <ui> \
420                 <menubar name=\"MainMenu\"> \
421                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
422                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
423                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
424                                 <menuitem name=\"Print\" action=\"Print\" /> \
425                                 <separator name=\"Separator1\"/> \
426                                 <menuitem name=\"Import\" action=\"Import\" /> \
427                                 <separator name=\"Separator2\"/> \
428                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
429                                 <separator name=\"Separator3\"/> \
430                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
431                         </menu> \
432                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
433                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
434                         </menu> \
435                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
436                                 <menuitem name=\"About\" action=\"About\" /> \
437                         </menu> \
438                 </menubar> \
439         </ui> \
440 ";
441
442 static GtkWidget *get_menubar_menu(GtkWidget *window)
443 {
444         GtkActionGroup *action_group = gtk_action_group_new("Menu");
445         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
446
447         GtkUIManager *ui_manager = gtk_ui_manager_new();
448         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
449         GError* error = 0;
450         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
451
452         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
453         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
454
455         return menu;
456 }
457
458 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
459 {
460         repaint_dive();
461 }
462
463 static const char notebook_group[] = "123";
464 #define GRP_ID ((void *)notebook_group)
465
466 static GtkNotebook *create_new_notebook_window(GtkNotebook *source,
467                 GtkWidget *page,
468                 gint x, gint y, gpointer data)
469 {
470         GtkWidget *win, *notebook, *vbox;
471
472         /* We don't detatch twice */
473         if (divelist_window)
474                 return NULL;
475
476         divelist_window = win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
477         gtk_window_set_title(GTK_WINDOW(win), "Dive List");
478         gtk_window_move(GTK_WINDOW(win), x, y);
479
480         /* Destroying the dive list will kill the application */
481         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
482         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
483
484         vbox = gtk_vbox_new(FALSE, 0);
485         gtk_container_add(GTK_CONTAINER(win), vbox);
486
487         notebook = gtk_notebook_new();
488         gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
489         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
490         gtk_widget_set_size_request(notebook, 350, 250);
491
492         gtk_widget_show_all(win);
493         return GTK_NOTEBOOK(notebook);
494 }
495
496 static void drag_cb(GtkWidget *widget, GdkDragContext *context,
497         gint x, gint y,
498         GtkSelectionData *selection_data,
499         guint info, guint time,
500         gpointer user_data)
501 {
502         GtkWidget *source, *target;
503         GtkWidget *tab;
504
505         /*
506          * We don't actually really *use* this yet, but Dirk wants to
507          * do all the tabs as detatched tabs, and we'd need to use
508          * this all to figure out which window we're talking about.
509          */
510         source = gtk_drag_get_source_widget(context);
511         target = (GtkWidget *) user_data;
512         tab = *(GtkWidget **)selection_data->data;
513
514         gtk_drag_finish(context, TRUE, TRUE, time);
515
516         /*
517          * Horrible, horrible hack. We hide the old divelist window, and
518          * set it to NULL. So if we drag the thing back out, we'll create
519          * a new window. Ugh.
520          *
521          * Actually destroying the divelist window triggers the whole
522          * destroy callback, which we don't want.
523          */
524         if (source != target) {
525                 gtk_widget_hide(divelist_window);
526                 divelist_window = NULL;
527         }
528 }
529
530 void init_ui(int argc, char **argv)
531 {
532         GtkWidget *win;
533         GtkWidget *notebook;
534         GtkWidget *dive_info;
535         GtkWidget *dive_list;
536         GtkWidget *equipment;
537         GtkWidget *menubar;
538         GtkWidget *vbox;
539         static const GtkTargetEntry notebook_target = {
540                 "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0
541         };
542
543         gtk_init(&argc, &argv);
544
545         g_type_init();
546         gconf = gconf_client_get_default();
547
548         if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
549                 output_units.length = FEET;
550         if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
551                 output_units.pressure = PSI;
552         if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
553                 output_units.volume = CUFT;
554         if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
555                 output_units.temperature = FAHRENHEIT;
556         /* an unset key is FALSE - so in order to get the default behavior right we 
557            invert the meaning of the SAC key */
558         visible_cols.otu = gconf_client_get_bool(gconf, GCONF_NAME(OTU), NULL);
559         visible_cols.sac = ! gconf_client_get_bool(gconf, GCONF_NAME(SAC), NULL);
560                 
561         divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
562         if (!divelist_font)
563                 divelist_font = DIVELIST_DEFAULT_FONT;
564
565         error_info_bar = NULL;
566         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
567         gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
568         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
569         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
570         main_window = win;
571
572         vbox = gtk_vbox_new(FALSE, 0);
573         gtk_container_add(GTK_CONTAINER(win), vbox);
574         main_vbox = vbox;
575
576         menubar = get_menubar_menu(win);
577         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
578
579         /* Notebook for dive info vs profile vs .. */
580         notebook = gtk_notebook_new();
581         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
582         gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
583         gtk_notebook_set_window_creation_hook(create_new_notebook_window, NULL, NULL);
584         gtk_drag_dest_set(notebook, GTK_DEST_DEFAULT_ALL, &notebook_target, 1, GDK_ACTION_MOVE);
585         g_signal_connect(notebook, "drag-data-received", G_CALLBACK(drag_cb), notebook);
586         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
587
588         /* Create the actual divelist */
589         dive_list = dive_list_create();
590         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_list, gtk_label_new("Dive List"));
591         gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_list, 1);
592         gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_list, 1);
593
594         /* Frame for dive profile */
595         dive_profile = dive_profile_widget();
596         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
597
598         /* Frame for extended dive info */
599         dive_info = extended_dive_info_widget();
600         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
601
602         /* Frame for dive equipment */
603         equipment = equipment_widget();
604         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
605
606         gtk_widget_set_app_paintable(win, TRUE);
607         gtk_widget_show_all(win);
608
609         return;
610 }
611
612 void run_ui(void)
613 {
614         gtk_main();
615 }
616
617 /* get the filenames the user selects and call the parsing function
618  * on them
619  * return 0 if the user cancelled the dialog
620  */
621 int open_import_file_dialog(char *filterpattern, char *filtertext, 
622                         void(* parse_function)(char *))
623 {
624         int ret=0;
625
626         GtkWidget *dialog;
627         GtkFileFilter *filter = gtk_file_filter_new ();
628         gtk_file_filter_add_pattern (filter, filterpattern);
629         gtk_file_filter_set_name(filter, filtertext);
630         dialog = gtk_file_chooser_dialog_new("Open File",
631                                         GTK_WINDOW(main_window),
632                                         GTK_FILE_CHOOSER_ACTION_OPEN,
633                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
634                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
635                                         NULL);
636         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
637         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
638
639         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
640                 GSList *filenames;
641                 char *filename;
642                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
643                 while(filenames != NULL) {
644                         filename = (char *)filenames->data;
645                         parse_function(filename);
646                         g_free(filename);
647                         filenames = g_slist_next(filenames);
648                 }
649                 g_slist_free(filenames);
650                 ret = 1;
651         }
652         gtk_widget_destroy(dialog);
653
654         return ret;
655 }
656
657 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
658 {
659         struct dive *dive = current_dive;
660         struct graphics_context gc = { .printer = 0 };
661         int w,h;
662
663         w = widget->allocation.width;
664         h = widget->allocation.height;
665
666         gc.cr = gdk_cairo_create(widget->window);
667         set_source_rgb(&gc, 0, 0, 0);
668         cairo_paint(gc.cr);
669
670         if (dive)
671                 plot(&gc, w, h, dive);
672
673         cairo_destroy(gc.cr);
674
675         return FALSE;
676 }
677
678 GtkWidget *dive_profile_widget(void)
679 {
680         GtkWidget *da;
681
682         da = gtk_drawing_area_new();
683         gtk_widget_set_size_request(da, 350, 250);
684         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
685
686         return da;
687 }
688
689 int process_ui_events(void)
690 {
691         int ret=0;
692
693         while (gtk_events_pending()) {
694                 if (gtk_main_iteration_do(0)) {
695                         ret = 1;
696                         break;
697                 }
698         }
699         return(ret);
700 }
701
702
703 static void fill_computer_list(GtkListStore *store)
704 {
705         GtkTreeIter iter;
706         struct device_list *list = device_list;
707
708         for (list = device_list ; list->name ; list++) {
709                 gtk_list_store_append(store, &iter);
710                 gtk_list_store_set(store, &iter,
711                         0, list->name,
712                         1, list->type,
713                         -1);
714         }
715 }
716
717 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
718 {
719         GtkWidget *hbox, *combo_box, *frame;
720         GtkListStore *model;
721         GtkCellRenderer *renderer;
722
723         hbox = gtk_hbox_new(FALSE, 6);
724         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
725
726         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
727         fill_computer_list(model);
728
729         frame = gtk_frame_new("Dive computer");
730         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
731
732         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
733         gtk_container_add(GTK_CONTAINER(frame), combo_box);
734
735         renderer = gtk_cell_renderer_text_new();
736         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
737         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
738
739         return GTK_COMBO_BOX(combo_box);
740 }
741
742 static GtkEntry *dive_computer_device(GtkWidget *vbox)
743 {
744         GtkWidget *hbox, *entry, *frame;
745
746         hbox = gtk_hbox_new(FALSE, 6);
747         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
748
749         frame = gtk_frame_new("Device name");
750         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
751
752         entry = gtk_entry_new();
753         gtk_container_add(GTK_CONTAINER(frame), entry);
754         gtk_entry_set_text(GTK_ENTRY(entry), "/dev/ttyUSB0");
755
756         return GTK_ENTRY(entry);
757 }
758
759 void import_dialog(GtkWidget *w, gpointer data)
760 {
761         int result;
762         GtkWidget *dialog, *hbox, *vbox;
763         GtkComboBox *computer;
764         GtkEntry *device;
765         device_data_t devicedata = {
766                 .devname = NULL,
767         };
768
769         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
770                 GTK_WINDOW(main_window),
771                 GTK_DIALOG_DESTROY_WITH_PARENT,
772                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
773                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
774                 NULL);
775
776         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
777
778         computer = dive_computer_selector(vbox);
779         device = dive_computer_device(vbox);
780
781         hbox = gtk_hbox_new(FALSE, 6);
782         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
783         devicedata.progress.bar = gtk_progress_bar_new();
784         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
785
786         gtk_widget_show_all(dialog);
787         result = gtk_dialog_run(GTK_DIALOG(dialog));
788         switch (result) {
789                 int type;
790                 GtkTreeIter iter;
791                 GtkTreeModel *model;
792                 const char *comp;
793         case GTK_RESPONSE_ACCEPT:
794                 if (!gtk_combo_box_get_active_iter(computer, &iter))
795                         break;
796                 model = gtk_combo_box_get_model(computer);
797                 gtk_tree_model_get(model, &iter,
798                         0, &comp,
799                         1, &type,
800                         -1);
801                 devicedata.type = type;
802                 devicedata.name = comp;
803                 devicedata.devname = gtk_entry_get_text(device);
804                 do_import(&devicedata);
805                 break;
806         default:
807                 break;
808         }
809         gtk_widget_destroy(dialog);
810
811         report_dives();
812         dive_list_update_dives();
813 }
814
815 void update_progressbar(progressbar_t *progress, double value)
816 {
817         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
818 }
819
820
821 void set_filename(const char *filename)
822 {
823         if (filename)
824                 existing_filename = strdup(filename);
825         return;
826 }