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