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