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