]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
Move 'dive_list_update_dives()' call into 'report_dives()'
[ext/subsurface.git] / gtk-gui.c
1 /* gtk-gui.c */
2 /* gtk UI implementation */
3 /* creates the window and overall layout
4  * divelist, dive info, equipment and printing are handled in their own source files
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <time.h>
10
11 #include <gconf/gconf-client.h>
12
13 #include "dive.h"
14 #include "divelist.h"
15 #include "display.h"
16 #include "display-gtk.h"
17
18 #include "libdivecomputer.h"
19
20 GtkWidget *main_window;
21 GtkWidget *main_vbox;
22 GtkWidget *error_info_bar;
23 GtkWidget *error_label;
24 int        error_count;
25
26 #define DIVELIST_DEFAULT_FONT "Sans 8"
27 const char *divelist_font;
28
29 GConfClient *gconf;
30 struct units output_units;
31
32 #define GCONF_NAME(x) "/apps/subsurface/" #x
33
34 static GtkWidget *dive_profile;
35
36 visible_cols_t visible_cols = {TRUE, FALSE};
37
38 void repaint_dive(void)
39 {
40         update_dive(current_dive);
41         if (dive_profile)
42                 gtk_widget_queue_draw(dive_profile);
43 }
44
45 static char *existing_filename;
46
47 static void on_info_bar_response(GtkWidget *widget, gint response,
48                                  gpointer data)
49 {
50         if (response == GTK_RESPONSE_OK)
51         {
52                 gtk_widget_destroy(widget);
53                 error_info_bar = NULL;
54         }
55 }
56
57 void report_error(GError* error)
58 {
59         if (error == NULL)
60         {
61                 return;
62         }
63         
64         if (error_info_bar == NULL)
65         {
66                 error_count = 1;
67                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
68                                                                GTK_RESPONSE_OK,
69                                                                NULL);
70                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
71                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
72                                               GTK_MESSAGE_ERROR);
73                 
74                 error_label = gtk_label_new(error->message);
75                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
76                 gtk_container_add(GTK_CONTAINER(container), error_label);
77                 
78                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
79                 gtk_widget_show_all(main_vbox);
80         }
81         else
82         {
83                 error_count++;
84                 char buffer[256];
85                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
86                 gtk_label_set(GTK_LABEL(error_label), buffer);
87         }
88 }
89
90 static void file_open(GtkWidget *w, gpointer data)
91 {
92         GtkWidget *dialog;
93         GtkFileFilter *filter;
94
95         dialog = gtk_file_chooser_dialog_new("Open File",
96                 GTK_WINDOW(main_window),
97                 GTK_FILE_CHOOSER_ACTION_OPEN,
98                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
99                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
100                 NULL);
101         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
102
103         filter = gtk_file_filter_new();
104         gtk_file_filter_add_pattern(filter, "*.xml");
105         gtk_file_filter_add_pattern(filter, "*.XML");
106         gtk_file_filter_add_pattern(filter, "*.sda");
107         gtk_file_filter_add_pattern(filter, "*.SDA");
108         gtk_file_filter_add_mime_type(filter, "text/xml");
109         gtk_file_filter_set_name(filter, "XML file");
110         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
111
112         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
113                 GSList *filenames;
114                 char *filename;
115                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
116                 
117                 GError *error = NULL;
118                 while(filenames != NULL) {
119                         filename = (char *)filenames->data;
120                         parse_xml_file(filename, &error);
121                         if (error != NULL)
122                         {
123                                 report_error(error);
124                                 g_error_free(error);
125                                 error = NULL;
126                         }
127                         
128                         g_free(filename);
129                         filenames = g_slist_next(filenames);
130                 }
131                 g_slist_free(filenames);
132                 report_dives();
133         }
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         GtkWidget *dialog, *frame, *button, *vbox;
392
393         dialog = gtk_dialog_new_with_buttons("Renumber",
394                 GTK_WINDOW(main_window),
395                 GTK_DIALOG_DESTROY_WITH_PARENT,
396                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
397                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
398                 NULL);
399
400         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
401
402         frame = gtk_frame_new("New starting number");
403         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
404
405         button = gtk_spin_button_new_with_range(1, 50000, 1);
406         gtk_container_add(GTK_CONTAINER(frame), button);
407
408         gtk_widget_show_all(dialog);
409         result = gtk_dialog_run(GTK_DIALOG(dialog));
410         if (result == GTK_RESPONSE_ACCEPT) {
411                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
412                 renumber_dives(nr);
413                 repaint_dive();
414         }
415         gtk_widget_destroy(dialog);
416 }
417
418 static void about_dialog(GtkWidget *w, gpointer data)
419 {
420         const char *logo_property = NULL;
421         GdkPixbuf *logo = NULL;
422         GtkWidget *image = gtk_image_new_from_file("icon.svg");
423
424         if (image) {
425                 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
426                 logo_property = "logo";
427         }
428
429         gtk_show_about_dialog(NULL,
430                 "program-name", "SubSurface",
431                 "comments", "Half-arsed divelog software in C",
432                 "license", "GPLv2",
433                 "version", VERSION_STRING,
434                 "copyright", "Linus Torvalds 2011",
435                 /* Must be last: */
436                 logo_property, logo,
437                 NULL);
438 }
439
440 static GtkActionEntry menu_items[] = {
441         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
442         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
443         { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
444         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
445         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
446         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
447         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
448         { "Preferences",    NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
449         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
450         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
451         { "About",           GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
452 };
453 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
454
455 static const gchar* ui_string = " \
456         <ui> \
457                 <menubar name=\"MainMenu\"> \
458                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
459                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
460                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
461                                 <menuitem name=\"Print\" action=\"Print\" /> \
462                                 <separator name=\"Separator1\"/> \
463                                 <menuitem name=\"Import\" action=\"Import\" /> \
464                                 <separator name=\"Separator2\"/> \
465                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
466                                 <separator name=\"Separator3\"/> \
467                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
468                         </menu> \
469                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
470                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
471                         </menu> \
472                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
473                                 <menuitem name=\"About\" action=\"About\" /> \
474                         </menu> \
475                 </menubar> \
476         </ui> \
477 ";
478
479 static GtkWidget *get_menubar_menu(GtkWidget *window)
480 {
481         GtkActionGroup *action_group = gtk_action_group_new("Menu");
482         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
483
484         GtkUIManager *ui_manager = gtk_ui_manager_new();
485         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
486         GError* error = 0;
487         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
488
489         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
490         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
491
492         return menu;
493 }
494
495 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
496 {
497         repaint_dive();
498 }
499
500 static const char notebook_group[] = "123";
501 #define GRP_ID ((void *)notebook_group)
502 typedef struct {
503         char *name;
504         GtkWidget *widget;
505         GtkWidget *box;
506         gulong delete_handler;
507         gulong destroy_handler;
508 } notebook_data_t;
509
510 static notebook_data_t nbd[2]; /* we rip at most two notebook pages off */
511
512 static GtkNotebook *create_new_notebook_window(GtkNotebook *source,
513                 GtkWidget *page,
514                 gint x, gint y, gpointer data)
515 {
516         GtkWidget *win, *notebook, *vbox;
517         notebook_data_t *nbdp;
518
519         /* pick the right notebook page data and return if both are detached */
520         if (nbd[0].widget == NULL)
521                 nbdp = nbd;
522         else if (nbd[1].widget == NULL)
523                 nbdp = nbd + 1;
524         else
525                 return NULL;
526
527         nbdp->name = strdup(gtk_widget_get_name(page));
528         nbdp->widget = win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
529         gtk_window_set_title(GTK_WINDOW(win), nbdp->name);
530         gtk_window_move(GTK_WINDOW(win), x, y);
531
532         /* Destroying the dive list will kill the application */
533         nbdp->delete_handler = g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
534         nbdp->destroy_handler = g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
535
536         nbdp->box = vbox = gtk_vbox_new(FALSE, 0);
537         gtk_container_add(GTK_CONTAINER(win), vbox);
538
539         notebook = gtk_notebook_new();
540         gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
541         gtk_widget_set_name(notebook, nbdp->name);
542         /* disallow drop events */
543         gtk_drag_dest_set(notebook, 0, NULL, 0, 0);
544         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
545         gtk_widget_set_size_request(notebook, 450, 350);
546
547         gtk_widget_show_all(win);
548         return GTK_NOTEBOOK(notebook);
549 }
550
551 static void drag_cb(GtkWidget *widget, GdkDragContext *context,
552         gint x, gint y, guint time,
553         gpointer user_data)
554 {
555         GtkWidget *source;
556         notebook_data_t *nbdp;
557
558         source = gtk_drag_get_source_widget(context);
559         if (nbd[0].name && ! strcmp(nbd[0].name,gtk_widget_get_name(source)))
560                 nbdp = nbd;
561         else if (nbd[1].name && ! strcmp(nbd[1].name,gtk_widget_get_name(source)))
562                 nbdp = nbd + 1;
563         else
564                 /* HU? */
565                 return;
566
567         gtk_drag_finish(context, TRUE, TRUE, time);
568
569         /* we no longer need the widget - but getting rid of this is hard;
570          * remove the signal handler, remove the notebook from the box
571          * then destroy the widget (and clear out our data structure) */
572         g_signal_handler_disconnect(nbdp->widget,nbdp->delete_handler);
573         g_signal_handler_disconnect(nbdp->widget,nbdp->destroy_handler);
574         gtk_container_remove(GTK_CONTAINER(nbdp->box), source);
575         gtk_widget_destroy(nbdp->widget);
576         nbdp->widget = NULL;
577         free(nbdp->name);
578         nbdp->name = NULL;
579 }
580
581 void init_ui(int argc, char **argv)
582 {
583         GtkWidget *win;
584         GtkWidget *notebook;
585         GtkWidget *dive_info;
586         GtkWidget *dive_list;
587         GtkWidget *equipment;
588         GtkWidget *menubar;
589         GtkWidget *vbox;
590         GtkSettings *settings;
591         static const GtkTargetEntry notebook_target = {
592                 "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0
593         };
594
595         gtk_init(&argc, &argv);
596         settings = gtk_settings_get_default();
597         gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
598
599         g_type_init();
600         gconf = gconf_client_get_default();
601
602         if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
603                 output_units.length = FEET;
604         if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
605                 output_units.pressure = PSI;
606         if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
607                 output_units.volume = CUFT;
608         if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
609                 output_units.temperature = FAHRENHEIT;
610         /* an unset key is FALSE - so in order to get the default behavior right we 
611            invert the meaning of the SAC key */
612         visible_cols.otu = gconf_client_get_bool(gconf, GCONF_NAME(OTU), NULL);
613         visible_cols.sac = ! gconf_client_get_bool(gconf, GCONF_NAME(SAC), NULL);
614                 
615         divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
616         if (!divelist_font)
617                 divelist_font = DIVELIST_DEFAULT_FONT;
618
619         error_info_bar = NULL;
620         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
621         gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
622         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
623         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
624         main_window = win;
625
626         vbox = gtk_vbox_new(FALSE, 0);
627         gtk_container_add(GTK_CONTAINER(win), vbox);
628         main_vbox = vbox;
629
630         menubar = get_menubar_menu(win);
631         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
632
633         /* Notebook for dive info vs profile vs .. */
634         notebook = gtk_notebook_new();
635         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
636         gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
637         g_signal_connect(notebook, "create-window", G_CALLBACK(create_new_notebook_window), NULL);
638         gtk_drag_dest_set(notebook, GTK_DEST_DEFAULT_ALL, &notebook_target, 1, GDK_ACTION_MOVE);
639         g_signal_connect(notebook, "drag-drop", G_CALLBACK(drag_cb), notebook);
640         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
641
642         /* Create the actual divelist */
643         dive_list = dive_list_create();
644         gtk_widget_set_name(dive_list, "Dive List");
645         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_list, gtk_label_new("Dive List"));
646         gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_list, 1);
647         gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_list, 1);
648
649         /* Frame for dive profile */
650         dive_profile = dive_profile_widget();
651         gtk_widget_set_name(dive_profile, "Dive Profile");
652         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
653         gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_profile, 1);
654         gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_profile, 1);
655
656         /* Frame for extended dive info */
657         dive_info = extended_dive_info_widget();
658         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
659
660         /* Frame for dive equipment */
661         equipment = equipment_widget();
662         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
663
664         gtk_widget_set_app_paintable(win, TRUE);
665         gtk_widget_show_all(win);
666
667         return;
668 }
669
670 void run_ui(void)
671 {
672         gtk_main();
673 }
674
675 typedef struct {
676         cairo_rectangle_int_t rect;
677         const char *text;
678 } tooltip_record_t;
679
680 static tooltip_record_t *tooltip_rects;
681 static int tooltips;
682
683 void attach_tooltip(int x, int y, int w, int h, const char *text)
684 {
685         cairo_rectangle_int_t *rect;
686         tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
687         rect = &tooltip_rects[tooltips].rect;
688         rect->x = x;
689         rect->y = y;
690         rect->width = w;
691         rect->height = h;
692         tooltip_rects[tooltips].text = text;
693         tooltips++;
694 }
695
696 #define INSIDE_RECT(_r,_x,_y)   ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
697                                 (_r.y <= _y) && (_r.y + _r.height >= _y))
698
699 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
700                         gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
701 {
702         int i;
703         cairo_rectangle_int_t *drawing_area = user_data;
704         gint tx = x - drawing_area->x; /* get transformed coordinates */
705         gint ty = y - drawing_area->y;
706
707         /* are we over an event marker ? */
708         for (i = 0; i < tooltips; i++) {
709                 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
710                         gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
711                         return TRUE; /* show tooltip */
712                 }
713         }
714         return FALSE; /* don't show tooltip */
715 }
716
717 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
718 {
719         struct dive *dive = current_dive;
720         struct graphics_context gc = { .printer = 0 };
721         static cairo_rectangle_int_t drawing_area;
722
723         /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
724          * so effective drawing area is width-2x * height-2y */
725         drawing_area.width = widget->allocation.width;
726         drawing_area.height = widget->allocation.height;
727         drawing_area.x = drawing_area.width / 20.0;
728         drawing_area.y = drawing_area.height / 20.0;
729
730         gc.cr = gdk_cairo_create(widget->window);
731         g_object_set(widget, "has-tooltip", TRUE, NULL);
732         g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
733         set_source_rgb(&gc, 0, 0, 0);
734         cairo_paint(gc.cr);
735
736         if (dive) {
737                 if (tooltip_rects) {
738                         free(tooltip_rects);
739                         tooltip_rects = NULL;
740                 }
741                 tooltips = 0;
742                 plot(&gc, &drawing_area, dive);
743         }
744         cairo_destroy(gc.cr);
745
746         return FALSE;
747 }
748
749 GtkWidget *dive_profile_widget(void)
750 {
751         GtkWidget *da;
752
753         da = gtk_drawing_area_new();
754         gtk_widget_set_size_request(da, 350, 250);
755         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
756
757         return da;
758 }
759
760 int process_ui_events(void)
761 {
762         int ret=0;
763
764         while (gtk_events_pending()) {
765                 if (gtk_main_iteration_do(0)) {
766                         ret = 1;
767                         break;
768                 }
769         }
770         return(ret);
771 }
772
773
774 static void fill_computer_list(GtkListStore *store)
775 {
776         GtkTreeIter iter;
777         struct device_list *list = device_list;
778
779         for (list = device_list ; list->name ; list++) {
780                 gtk_list_store_append(store, &iter);
781                 gtk_list_store_set(store, &iter,
782                         0, list->name,
783                         1, list->type,
784                         -1);
785         }
786 }
787
788 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
789 {
790         GtkWidget *hbox, *combo_box, *frame;
791         GtkListStore *model;
792         GtkCellRenderer *renderer;
793
794         hbox = gtk_hbox_new(FALSE, 6);
795         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
796
797         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
798         fill_computer_list(model);
799
800         frame = gtk_frame_new("Dive computer");
801         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
802
803         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
804         gtk_container_add(GTK_CONTAINER(frame), combo_box);
805
806         renderer = gtk_cell_renderer_text_new();
807         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
808         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
809
810         return GTK_COMBO_BOX(combo_box);
811 }
812
813 static GtkEntry *dive_computer_device(GtkWidget *vbox)
814 {
815         GtkWidget *hbox, *entry, *frame;
816
817         hbox = gtk_hbox_new(FALSE, 6);
818         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
819
820         frame = gtk_frame_new("Device name");
821         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
822
823         entry = gtk_entry_new();
824         gtk_container_add(GTK_CONTAINER(frame), entry);
825         gtk_entry_set_text(GTK_ENTRY(entry), "/dev/ttyUSB0");
826
827         return GTK_ENTRY(entry);
828 }
829
830 void import_dialog(GtkWidget *w, gpointer data)
831 {
832         int result;
833         GtkWidget *dialog, *hbox, *vbox;
834         GtkComboBox *computer;
835         GtkEntry *device;
836         device_data_t devicedata = {
837                 .devname = NULL,
838         };
839
840         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
841                 GTK_WINDOW(main_window),
842                 GTK_DIALOG_DESTROY_WITH_PARENT,
843                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
844                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
845                 NULL);
846
847         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
848
849         computer = dive_computer_selector(vbox);
850         device = dive_computer_device(vbox);
851
852         hbox = gtk_hbox_new(FALSE, 6);
853         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
854         devicedata.progress.bar = gtk_progress_bar_new();
855         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
856
857         gtk_widget_show_all(dialog);
858         result = gtk_dialog_run(GTK_DIALOG(dialog));
859         switch (result) {
860                 int type;
861                 GtkTreeIter iter;
862                 GtkTreeModel *model;
863                 const char *comp;
864         case GTK_RESPONSE_ACCEPT:
865                 if (!gtk_combo_box_get_active_iter(computer, &iter))
866                         break;
867                 model = gtk_combo_box_get_model(computer);
868                 gtk_tree_model_get(model, &iter,
869                         0, &comp,
870                         1, &type,
871                         -1);
872                 devicedata.type = type;
873                 devicedata.name = comp;
874                 devicedata.devname = gtk_entry_get_text(device);
875                 do_import(&devicedata);
876                 break;
877         default:
878                 break;
879         }
880         gtk_widget_destroy(dialog);
881
882         report_dives();
883 }
884
885 void update_progressbar(progressbar_t *progress, double value)
886 {
887         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
888 }
889
890
891 void set_filename(const char *filename)
892 {
893         if (filename)
894                 existing_filename = strdup(filename);
895         return;
896 }