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