]> git.tdb.fi Git - ext/subsurface.git/blob - gtk-gui.c
Add weight system tracking
[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 "dive.h"
12 #include "divelist.h"
13 #include "display.h"
14 #include "display-gtk.h"
15
16 #include "libdivecomputer.h"
17
18 GtkWidget *main_window;
19 GtkWidget *main_vbox;
20 GtkWidget *error_info_bar;
21 GtkWidget *error_label;
22 GtkWidget *vpane, *hpane;
23 int        error_count;
24
25 #define DIVELIST_DEFAULT_FONT "Sans 8"
26 const char *divelist_font;
27
28 struct units output_units;
29
30 static GtkWidget *dive_profile;
31
32 visible_cols_t visible_cols = {TRUE, FALSE};
33
34 void repaint_dive(void)
35 {
36         update_dive(current_dive);
37         if (dive_profile)
38                 gtk_widget_queue_draw(dive_profile);
39 }
40
41 static char *existing_filename;
42 static gboolean need_icon = TRUE;
43
44 static void on_info_bar_response(GtkWidget *widget, gint response,
45                                  gpointer data)
46 {
47         if (response == GTK_RESPONSE_OK)
48         {
49                 gtk_widget_destroy(widget);
50                 error_info_bar = NULL;
51         }
52 }
53
54 void report_error(GError* error)
55 {
56         if (error == NULL)
57         {
58                 return;
59         }
60         
61         if (error_info_bar == NULL)
62         {
63                 error_count = 1;
64                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
65                                                                GTK_RESPONSE_OK,
66                                                                NULL);
67                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
68                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
69                                               GTK_MESSAGE_ERROR);
70                 
71                 error_label = gtk_label_new(error->message);
72                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
73                 gtk_container_add(GTK_CONTAINER(container), error_label);
74                 
75                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
76                 gtk_widget_show_all(main_vbox);
77         }
78         else
79         {
80                 error_count++;
81                 char buffer[256];
82                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
83                 gtk_label_set(GTK_LABEL(error_label), buffer);
84         }
85 }
86
87 static void file_open(GtkWidget *w, gpointer data)
88 {
89         GtkWidget *dialog;
90         GtkFileFilter *filter;
91
92         dialog = gtk_file_chooser_dialog_new("Open File",
93                 GTK_WINDOW(main_window),
94                 GTK_FILE_CHOOSER_ACTION_OPEN,
95                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
96                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
97                 NULL);
98         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
99
100         filter = gtk_file_filter_new();
101         gtk_file_filter_add_pattern(filter, "*.xml");
102         gtk_file_filter_add_pattern(filter, "*.XML");
103         gtk_file_filter_add_pattern(filter, "*.sda");
104         gtk_file_filter_add_pattern(filter, "*.SDA");
105         gtk_file_filter_add_mime_type(filter, "text/xml");
106         gtk_file_filter_set_name(filter, "XML file");
107         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
108
109         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
110                 GSList *filenames;
111                 char *filename;
112                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
113                 
114                 GError *error = NULL;
115                 while(filenames != NULL) {
116                         filename = filenames->data;
117                         parse_xml_file(filename, &error);
118                         if (error != NULL)
119                         {
120                                 report_error(error);
121                                 g_error_free(error);
122                                 error = NULL;
123                         }
124                         
125                         g_free(filename);
126                         filenames = g_slist_next(filenames);
127                 }
128                 g_slist_free(filenames);
129                 report_dives(FALSE);
130         }
131         gtk_widget_destroy(dialog);
132 }
133
134 static void file_save(GtkWidget *w, gpointer data)
135 {
136         GtkWidget *dialog;
137         dialog = gtk_file_chooser_dialog_new("Save File",
138                 GTK_WINDOW(main_window),
139                 GTK_FILE_CHOOSER_ACTION_SAVE,
140                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
141                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
142                 NULL);
143         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
144         if (!existing_filename) {
145                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
146         } else
147                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
148
149         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
150                 char *filename;
151                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
152                 save_dives(filename);
153                 g_free(filename);
154                 mark_divelist_changed(FALSE);
155         }
156         gtk_widget_destroy(dialog);
157 }
158
159 static void ask_save_changes()
160 {
161         GtkWidget *dialog, *label, *content;
162         dialog = gtk_dialog_new_with_buttons("Save Changes?",
163                 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
164                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
165                 GTK_STOCK_NO, GTK_RESPONSE_NO,
166                 NULL);
167         content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
168         label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
169         gtk_container_add (GTK_CONTAINER (content), label);
170         gtk_widget_show_all (dialog);
171         gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
172         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
173                 file_save(NULL,NULL);
174         }
175         gtk_widget_destroy(dialog);
176 }
177
178 static gboolean on_delete(GtkWidget* w, gpointer data)
179 {
180         /* Make sure to flush any modified dive data */
181         update_dive(NULL);
182
183         if (unsaved_changes())
184                 ask_save_changes();
185
186         return FALSE; /* go ahead, kill the program, we're good now */
187 }
188
189 static void on_destroy(GtkWidget* w, gpointer data)
190 {
191         gtk_main_quit();
192 }
193
194 static void quit(GtkWidget *w, gpointer data)
195 {
196         /* Make sure to flush any modified dive data */
197         update_dive(NULL);
198
199         if (unsaved_changes())
200                 ask_save_changes();
201         gtk_main_quit();
202 }
203
204 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
205                                 data_func_t data_func, unsigned int flags)
206 {
207         GtkCellRenderer *renderer;
208         GtkTreeViewColumn *col;
209         double xalign = 0.0; /* left as default */
210         PangoAlignment align;
211         gboolean visible;
212
213         align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
214                 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
215                 PANGO_ALIGN_CENTER;
216         visible = !(flags & INVISIBLE);
217
218         renderer = gtk_cell_renderer_text_new();
219         col = gtk_tree_view_column_new();
220
221         gtk_tree_view_column_set_title(col, title);
222         if (!(flags & UNSORTABLE))
223                 gtk_tree_view_column_set_sort_column_id(col, index);
224         gtk_tree_view_column_set_resizable(col, TRUE);
225         gtk_tree_view_column_pack_start(col, renderer, TRUE);
226         if (data_func)
227                 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
228         else
229                 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
230         gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
231         switch (align) {
232         case PANGO_ALIGN_LEFT:
233                 xalign = 0.0;
234                 break;
235         case PANGO_ALIGN_CENTER:
236                 xalign = 0.5;
237                 break;
238         case PANGO_ALIGN_RIGHT:
239                 xalign = 1.0;
240                 break;
241         }
242         gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
243         gtk_tree_view_column_set_visible(col, visible);
244         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
245         return col;
246 }
247
248 static void create_radio(GtkWidget *vbox, const char *name, ...)
249 {
250         va_list args;
251         GtkRadioButton *group = NULL;
252         GtkWidget *box, *label;
253
254         box = gtk_hbox_new(TRUE, 10);
255         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
256
257         label = gtk_label_new(name);
258         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
259
260         va_start(args, name);
261         for (;;) {
262                 int enabled;
263                 const char *name;
264                 GtkWidget *button;
265                 void *callback_fn;
266
267                 name = va_arg(args, char *);
268                 if (!name)
269                         break;
270                 callback_fn = va_arg(args, void *);
271                 enabled = va_arg(args, int);
272
273                 button = gtk_radio_button_new_with_label_from_widget(group, name);
274                 group = GTK_RADIO_BUTTON(button);
275                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
276                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
277                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
278         }
279         va_end(args);
280 }
281
282 #define UNITCALLBACK(name, type, value)                         \
283 static void name(GtkWidget *w, gpointer data)                   \
284 {                                                               \
285         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
286                 menu_units.type = value;                        \
287 }
288
289 static struct units menu_units;
290
291 UNITCALLBACK(set_meter, length, METERS)
292 UNITCALLBACK(set_feet, length, FEET)
293 UNITCALLBACK(set_bar, pressure, BAR)
294 UNITCALLBACK(set_psi, pressure, PSI)
295 UNITCALLBACK(set_liter, volume, LITER)
296 UNITCALLBACK(set_cuft, volume, CUFT)
297 UNITCALLBACK(set_celsius, temperature, CELSIUS)
298 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
299 UNITCALLBACK(set_kg, weight, KG)
300 UNITCALLBACK(set_lbs, weight, LBS)
301
302 #define OPTIONCALLBACK(name, option) \
303 static void name(GtkWidget *w, gpointer data) \
304 { \
305         option = GTK_TOGGLE_BUTTON(w)->active; \
306 }
307
308 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
309 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
310 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
311 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
312 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
313
314 static void event_toggle(GtkWidget *w, gpointer _data)
315 {
316         gboolean *plot_ev = _data;
317
318         *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
319 }
320
321 static void preferences_dialog(GtkWidget *w, gpointer data)
322 {
323         int result;
324         GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
325
326         menu_units = output_units;
327
328         dialog = gtk_dialog_new_with_buttons("Preferences",
329                 GTK_WINDOW(main_window),
330                 GTK_DIALOG_DESTROY_WITH_PARENT,
331                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
332                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
333                 NULL);
334
335         frame = gtk_frame_new("Units");
336         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
337         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
338
339         box = gtk_vbox_new(FALSE, 6);
340         gtk_container_add(GTK_CONTAINER(frame), box);
341
342         create_radio(box, "Depth:",
343                 "Meter", set_meter, (output_units.length == METERS),
344                 "Feet",  set_feet, (output_units.length == FEET),
345                 NULL);
346
347         create_radio(box, "Pressure:",
348                 "Bar", set_bar, (output_units.pressure == BAR),
349                 "PSI",  set_psi, (output_units.pressure == PSI),
350                 NULL);
351
352         create_radio(box, "Volume:",
353                 "Liter",  set_liter, (output_units.volume == LITER),
354                 "CuFt", set_cuft, (output_units.volume == CUFT),
355                 NULL);
356
357         create_radio(box, "Temperature:",
358                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
359                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
360                 NULL);
361
362         create_radio(box, "Weight:",
363                 "kg", set_kg, (output_units.weight == KG),
364                 "lbs",  set_lbs, (output_units.weight == LBS),
365                 NULL);
366
367         frame = gtk_frame_new("Columns");
368         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
369
370         box = gtk_hbox_new(FALSE, 6);
371         gtk_container_add(GTK_CONTAINER(frame), box);
372
373         button = gtk_check_button_new_with_label("Show Temp");
374         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
375         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
376         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
377
378         button = gtk_check_button_new_with_label("Show Cyl");
379         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
380         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
381         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
382
383         button = gtk_check_button_new_with_label("Show O" UTF8_SUBSCRIPT_2 "%");
384         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
385         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
386         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
387
388         button = gtk_check_button_new_with_label("Show SAC");
389         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
390         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
391         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
392
393         button = gtk_check_button_new_with_label("Show OTU");
394         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
395         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
396         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
397
398         font = gtk_font_button_new_with_font(divelist_font);
399         gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
400
401         gtk_widget_show_all(dialog);
402         result = gtk_dialog_run(GTK_DIALOG(dialog));
403         if (result == GTK_RESPONSE_ACCEPT) {
404                 /* Make sure to flush any modified old dive data with old units */
405                 update_dive(NULL);
406
407                 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
408                 set_divelist_font(divelist_font);
409
410                 output_units = menu_units;
411                 update_dive_list_units();
412                 repaint_dive();
413                 update_dive_list_col_visibility();
414
415                 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
416                 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
417                 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
418                 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
419                 subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
420                 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
421                 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
422                 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
423                 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
424                 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
425                 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
426                 subsurface_close_conf();
427         }
428         gtk_widget_destroy(dialog);
429 }
430
431 static void create_toggle(const char* label, int *on, void *_data)
432 {
433         GtkWidget *button, *table = _data;
434         int rows, cols, x, y;
435         static int count;
436
437         if (table == NULL) {
438                 /* magic way to reset the number of toggle buttons
439                  * that we have already added - call this before you
440                  * create the dialog */
441                 count = 0;
442                 return;
443         }
444         g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
445         if (count > rows * cols) {
446                 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
447                 rows++;
448         }
449         x = count % cols;
450         y = count / cols;
451         button = gtk_check_button_new_with_label(label);
452         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
453         gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
454         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
455         count++;
456 }
457
458 static void selectevents_dialog(GtkWidget *w, gpointer data)
459 {
460         int result;
461         GtkWidget *dialog, *frame, *vbox, *table;
462
463         dialog = gtk_dialog_new_with_buttons("SelectEvents",
464                 GTK_WINDOW(main_window),
465                 GTK_DIALOG_DESTROY_WITH_PARENT,
466                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
467                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
468                 NULL);
469         /* initialize the function that fills the table */
470         create_toggle(NULL, NULL, NULL);
471
472         frame = gtk_frame_new("Enable / Disable Events");
473         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
474         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
475
476         table = gtk_table_new(1, 4, TRUE);
477         gtk_container_add(GTK_CONTAINER(frame), table);
478
479         evn_foreach(&create_toggle, table);
480
481         gtk_widget_show_all(dialog);
482         result = gtk_dialog_run(GTK_DIALOG(dialog));
483         if (result == GTK_RESPONSE_ACCEPT) {
484                 repaint_dive();
485         }
486         gtk_widget_destroy(dialog);
487 }
488
489 static void renumber_dialog(GtkWidget *w, gpointer data)
490 {
491         int result;
492         struct dive *dive;
493         GtkWidget *dialog, *frame, *button, *vbox;
494
495         dialog = gtk_dialog_new_with_buttons("Renumber",
496                 GTK_WINDOW(main_window),
497                 GTK_DIALOG_DESTROY_WITH_PARENT,
498                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
499                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
500                 NULL);
501
502         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
503
504         frame = gtk_frame_new("New starting number");
505         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
506
507         button = gtk_spin_button_new_with_range(1, 50000, 1);
508         gtk_container_add(GTK_CONTAINER(frame), button);
509
510         /*
511          * Do we have a number for the first dive already? Use that
512          * as the default.
513          */
514         dive = get_dive(0);
515         if (dive && dive->number)
516                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
517
518         gtk_widget_show_all(dialog);
519         result = gtk_dialog_run(GTK_DIALOG(dialog));
520         if (result == GTK_RESPONSE_ACCEPT) {
521                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
522                 renumber_dives(nr);
523                 repaint_dive();
524         }
525         gtk_widget_destroy(dialog);
526 }
527
528 static void about_dialog(GtkWidget *w, gpointer data)
529 {
530         const char *logo_property = NULL;
531         GdkPixbuf *logo = NULL;
532
533         if (need_icon) {
534 #if defined __linux__ || defined __APPLE__
535                 GtkWidget *image = gtk_image_new_from_file("subsurface.svg");
536 #elif defined WIN32
537                 GtkWidget *image = gtk_image_new_from_file("subsurface.ico");
538 #endif
539
540                 if (image) {
541                         logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
542                         logo_property = "logo";
543                 }
544         }
545
546         gtk_show_about_dialog(NULL,
547                 "program-name", "SubSurface",
548                 "comments", "Half-arsed divelog software in C",
549                 "license", "GPLv2",
550                 "version", VERSION_STRING,
551                 "copyright", "Linus Torvalds 2011",
552                 "logo-icon-name", "subsurface",
553                 /* Must be last: */
554                 logo_property, logo,
555                 NULL);
556 }
557
558 static void view_list(GtkWidget *w, gpointer data)
559 {
560         gtk_paned_set_position(GTK_PANED(vpane), 0);
561 }
562
563 static void view_profile(GtkWidget *w, gpointer data)
564 {
565         gtk_paned_set_position(GTK_PANED(hpane), 0);
566         gtk_paned_set_position(GTK_PANED(vpane), 65535);
567 }
568
569 static void view_info(GtkWidget *w, gpointer data)
570 {
571         gtk_paned_set_position(GTK_PANED(vpane), 65535);
572         gtk_paned_set_position(GTK_PANED(hpane), 65535);
573 }
574
575 /* Ooh. I don't know how to get the half-way size. So I'm just using random numbers */
576 static void view_three(GtkWidget *w, gpointer data)
577 {
578         gtk_paned_set_position(GTK_PANED(hpane), 400);
579         gtk_paned_set_position(GTK_PANED(vpane), 200);
580 }
581
582 static GtkActionEntry menu_items[] = {
583         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
584         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
585         { "ViewMenuAction",  GTK_STOCK_FILE, "View", NULL, NULL, NULL},
586         { "FilterMenuAction",  GTK_STOCK_FILE, "Filter", NULL, NULL, NULL},
587         { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
588         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
589         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
590         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
591         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
592         { "Preferences",    NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
593         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
594         { "SelectEvents",   NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
595         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
596         { "About",          GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
597         { "ViewList",       NULL, "List",  "<control>1", NULL, G_CALLBACK(view_list) },
598         { "ViewProfile",    NULL, "Profile", "<control>2", NULL, G_CALLBACK(view_profile) },
599         { "ViewInfo",       NULL, "Info", "<control>3", NULL, G_CALLBACK(view_info) },
600         { "ViewThree",       NULL, "Three", "<control>4", NULL, G_CALLBACK(view_three) },
601 };
602 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
603
604 static const gchar* ui_string = " \
605         <ui> \
606                 <menubar name=\"MainMenu\"> \
607                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
608                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
609                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
610                                 <menuitem name=\"Print\" action=\"Print\" /> \
611                                 <separator name=\"Separator1\"/> \
612                                 <menuitem name=\"Import\" action=\"Import\" /> \
613                                 <separator name=\"Separator2\"/> \
614                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
615                                 <separator name=\"Separator3\"/> \
616                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
617                         </menu> \
618                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
619                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
620                                 <menu name=\"View\" action=\"ViewMenuAction\"> \
621                                         <menuitem name=\"List\" action=\"ViewList\" /> \
622                                         <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
623                                         <menuitem name=\"Info\" action=\"ViewInfo\" /> \
624                                         <menuitem name=\"Paned\" action=\"ViewThree\" /> \
625                                 </menu> \
626                         </menu> \
627                         <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
628                                 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
629                         </menu> \
630                         <menu name=\"Help\" action=\"HelpMenuAction\"> \
631                                 <menuitem name=\"About\" action=\"About\" /> \
632                         </menu> \
633                 </menubar> \
634         </ui> \
635 ";
636
637 static GtkWidget *get_menubar_menu(GtkWidget *window)
638 {
639         GtkActionGroup *action_group = gtk_action_group_new("Menu");
640         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
641
642         GtkUIManager *ui_manager = gtk_ui_manager_new();
643         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
644         GError* error = 0;
645         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
646
647         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
648         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
649
650         return menu;
651 }
652
653 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
654 {
655         repaint_dive();
656 }
657
658 void init_ui(int *argcp, char ***argvp)
659 {
660         GtkWidget *win;
661         GtkWidget *notebook;
662         GtkWidget *dive_info;
663         GtkWidget *dive_list;
664         GtkWidget *equipment;
665         GtkWidget *stats;
666         GtkWidget *menubar;
667         GtkWidget *vbox;
668         GdkScreen *screen;
669         GtkIconTheme *icon_theme=NULL;
670         GtkSettings *settings;
671
672         gtk_init(argcp, argvp);
673         settings = gtk_settings_get_default();
674         gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
675
676         g_type_init();
677
678         subsurface_open_conf();
679         if (subsurface_get_conf("feet", PREF_BOOL))
680                 output_units.length = FEET;
681         if (subsurface_get_conf("psi", PREF_BOOL))
682                 output_units.pressure = PSI;
683         if (subsurface_get_conf("cuft", PREF_BOOL))
684                 output_units.volume = CUFT;
685         if (subsurface_get_conf("fahrenheit", PREF_BOOL))
686                 output_units.temperature = FAHRENHEIT;
687         if (subsurface_get_conf("lbs", PREF_BOOL))
688                 output_units.weight = LBS;
689         /* an unset key is FALSE - all these are hidden by default */
690         visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
691         visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
692         visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
693         visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
694         visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
695
696         divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
697
698         if (!divelist_font)
699                 divelist_font = DIVELIST_DEFAULT_FONT;
700
701         error_info_bar = NULL;
702         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
703         g_set_application_name ("subsurface");
704         /* Let's check if the subsurface icon has been installed or if
705          * we need to try to load it from the current directory */
706         screen = gdk_screen_get_default();
707         if (screen)
708                 icon_theme = gtk_icon_theme_get_for_screen(screen);
709         if (icon_theme) {
710                 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
711                         need_icon = FALSE;
712                         gtk_window_set_default_icon_name ("subsurface");
713                 }
714         }
715         if (need_icon)
716 #if defined __linux__ || defined __APPLE__
717                 gtk_window_set_icon_from_file(GTK_WINDOW(win), "subsurface.svg", NULL);
718 #elif defined WIN32
719                 gtk_window_set_icon_from_file(GTK_WINDOW(win), "subsurface.ico", NULL);
720 #endif
721         g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
722         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
723         main_window = win;
724
725         vbox = gtk_vbox_new(FALSE, 0);
726         gtk_container_add(GTK_CONTAINER(win), vbox);
727         main_vbox = vbox;
728
729         menubar = get_menubar_menu(win);
730         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
731
732         vpane = gtk_vpaned_new();
733         gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
734
735         hpane = gtk_hpaned_new();
736         gtk_paned_add1(GTK_PANED(vpane), hpane);
737
738         /* Notebook for dive info vs profile vs .. */
739         notebook = gtk_notebook_new();
740         gtk_paned_add1(GTK_PANED(hpane), notebook);
741         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
742
743         /* Create the actual divelist */
744         dive_list = dive_list_create();
745         gtk_widget_set_name(dive_list, "Dive List");
746         gtk_paned_add2(GTK_PANED(vpane), dive_list);
747
748         /* Frame for dive profile */
749         dive_profile = dive_profile_widget();
750         gtk_widget_set_name(dive_profile, "Dive Profile");
751         gtk_paned_add2(GTK_PANED(hpane), dive_profile);
752
753         /* Frame for extended dive info */
754         dive_info = extended_dive_info_widget();
755         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
756
757         /* Frame for dive equipment */
758         equipment = equipment_widget();
759         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
760
761         /* Frame for dive statistics */
762         stats = stats_widget();
763         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), stats, gtk_label_new("Info & Stats"));
764
765         gtk_widget_set_app_paintable(win, TRUE);
766         gtk_widget_show_all(win);
767
768         return;
769 }
770
771 void run_ui(void)
772 {
773         gtk_main();
774 }
775
776 typedef struct {
777         cairo_rectangle_int_t rect;
778         const char *text;
779 } tooltip_record_t;
780
781 static tooltip_record_t *tooltip_rects;
782 static int tooltips;
783
784 void attach_tooltip(int x, int y, int w, int h, const char *text)
785 {
786         cairo_rectangle_int_t *rect;
787         tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
788         rect = &tooltip_rects[tooltips].rect;
789         rect->x = x;
790         rect->y = y;
791         rect->width = w;
792         rect->height = h;
793         tooltip_rects[tooltips].text = text;
794         tooltips++;
795 }
796
797 #define INSIDE_RECT(_r,_x,_y)   ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
798                                 (_r.y <= _y) && (_r.y + _r.height >= _y))
799
800 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
801                         gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
802 {
803         int i;
804         cairo_rectangle_int_t *drawing_area = user_data;
805         gint tx = x - drawing_area->x; /* get transformed coordinates */
806         gint ty = y - drawing_area->y;
807
808         /* are we over an event marker ? */
809         for (i = 0; i < tooltips; i++) {
810                 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
811                         gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
812                         return TRUE; /* show tooltip */
813                 }
814         }
815         return FALSE; /* don't show tooltip */
816 }
817
818 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
819 {
820         struct dive *dive = current_dive;
821         struct graphics_context gc = { .printer = 0 };
822         static cairo_rectangle_int_t drawing_area;
823
824         /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
825          * so effective drawing area is width-2x * height-2y */
826         drawing_area.width = widget->allocation.width;
827         drawing_area.height = widget->allocation.height;
828         drawing_area.x = drawing_area.width / 20.0;
829         drawing_area.y = drawing_area.height / 20.0;
830
831         gc.cr = gdk_cairo_create(widget->window);
832         g_object_set(widget, "has-tooltip", TRUE, NULL);
833         g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
834         init_profile_background(&gc);
835         cairo_paint(gc.cr);
836
837         if (dive) {
838                 if (tooltip_rects) {
839                         free(tooltip_rects);
840                         tooltip_rects = NULL;
841                 }
842                 tooltips = 0;
843                 plot(&gc, &drawing_area, dive);
844         }
845         cairo_destroy(gc.cr);
846
847         return FALSE;
848 }
849
850 GtkWidget *dive_profile_widget(void)
851 {
852         GtkWidget *da;
853
854         da = gtk_drawing_area_new();
855         gtk_widget_set_size_request(da, 350, 250);
856         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
857
858         return da;
859 }
860
861 int process_ui_events(void)
862 {
863         int ret=0;
864
865         while (gtk_events_pending()) {
866                 if (gtk_main_iteration_do(0)) {
867                         ret = 1;
868                         break;
869                 }
870         }
871         return ret;
872 }
873
874
875 static void fill_computer_list(GtkListStore *store)
876 {
877         GtkTreeIter iter;
878         struct device_list *list = device_list;
879
880         for (list = device_list ; list->name ; list++) {
881                 gtk_list_store_append(store, &iter);
882                 gtk_list_store_set(store, &iter,
883                         0, list->name,
884                         1, list->type,
885                         -1);
886         }
887 }
888
889 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
890 {
891         GtkWidget *hbox, *combo_box, *frame;
892         GtkListStore *model;
893         GtkCellRenderer *renderer;
894
895         hbox = gtk_hbox_new(FALSE, 6);
896         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
897
898         model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
899         fill_computer_list(model);
900
901         frame = gtk_frame_new("Dive computer");
902         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
903
904         combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
905         gtk_container_add(GTK_CONTAINER(frame), combo_box);
906
907         renderer = gtk_cell_renderer_text_new();
908         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
909         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
910
911         return GTK_COMBO_BOX(combo_box);
912 }
913
914 static GtkEntry *dive_computer_device(GtkWidget *vbox)
915 {
916         GtkWidget *hbox, *entry, *frame;
917
918         hbox = gtk_hbox_new(FALSE, 6);
919         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
920
921         frame = gtk_frame_new("Device name");
922         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
923
924         entry = gtk_entry_new();
925         gtk_container_add(GTK_CONTAINER(frame), entry);
926         gtk_entry_set_text(GTK_ENTRY(entry), subsurface_USB_name());
927
928         return GTK_ENTRY(entry);
929 }
930
931 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
932 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
933 {
934         GtkDialog *main_dialog = _data;
935
936         gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
937 }
938
939 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
940 {
941         GtkWidget *hbox, *frame, *chooser, *dialog;
942         GtkFileFilter *filter;
943
944         hbox = gtk_hbox_new(FALSE, 6);
945         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
946
947         frame = gtk_frame_new("XML file name");
948         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
949         dialog = gtk_file_chooser_dialog_new("Open XML File",
950                 GTK_WINDOW(main_window),
951                 GTK_FILE_CHOOSER_ACTION_OPEN,
952                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
953                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
954                 NULL);
955         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
956
957         filter = gtk_file_filter_new();
958         gtk_file_filter_add_pattern(filter, "*.xml");
959         gtk_file_filter_add_pattern(filter, "*.XML");
960         gtk_file_filter_add_pattern(filter, "*.sda");
961         gtk_file_filter_add_pattern(filter, "*.SDA");
962         gtk_file_filter_add_mime_type(filter, "text/xml");
963         gtk_file_filter_set_name(filter, "XML file");
964         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
965
966         chooser = gtk_file_chooser_button_new_with_dialog(dialog);
967         g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
968
969         gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
970         gtk_container_add(GTK_CONTAINER(frame), chooser);
971
972         return chooser;
973 }
974
975 static void do_import_file(gpointer data, gpointer user_data)
976 {
977         GError *error = NULL;
978         parse_xml_file(data, &error);
979
980         if (error != NULL)
981         {
982                 report_error(error);
983                 g_error_free(error);
984                 error = NULL;
985         }
986 }
987
988 void import_dialog(GtkWidget *w, gpointer data)
989 {
990         int result;
991         GtkWidget *dialog, *hbox, *vbox, *label;
992         GtkComboBox *computer;
993         GtkEntry *device;
994         GtkWidget *XMLchooser;
995         device_data_t devicedata = {
996                 .devname = NULL,
997         };
998
999         dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1000                 GTK_WINDOW(main_window),
1001                 GTK_DIALOG_DESTROY_WITH_PARENT,
1002                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1003                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1004                 NULL);
1005
1006         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1007         label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1008         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1009         XMLchooser = xml_file_selector(vbox, dialog);
1010         computer = dive_computer_selector(vbox);
1011         device = dive_computer_device(vbox);
1012         hbox = gtk_hbox_new(FALSE, 6);
1013         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1014         devicedata.progress.bar = gtk_progress_bar_new();
1015         gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1016
1017         gtk_widget_show_all(dialog);
1018         result = gtk_dialog_run(GTK_DIALOG(dialog));
1019         switch (result) {
1020                 int type;
1021                 GtkTreeIter iter;
1022                 GtkTreeModel *model;
1023                 const char *comp;
1024                 GSList *list;
1025         case GTK_RESPONSE_ACCEPT:
1026                 /* what happened - did the user pick a file? In that case
1027                  * we ignore whether a dive computer model was picked */
1028                 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1029                 if (g_slist_length(list) == 0) {
1030                         if (!gtk_combo_box_get_active_iter(computer, &iter))
1031                                 break;
1032                         model = gtk_combo_box_get_model(computer);
1033                         gtk_tree_model_get(model, &iter,
1034                                         0, &comp,
1035                                         1, &type,
1036                                         -1);
1037                         devicedata.type = type;
1038                         devicedata.name = comp;
1039                         devicedata.devname = gtk_entry_get_text(device);
1040                         do_import(&devicedata);
1041                 } else {
1042                         g_slist_foreach(list,do_import_file,NULL);
1043                         g_slist_free(list);
1044                 }
1045                 break;
1046         default:
1047                 break;
1048         }
1049         gtk_widget_destroy(dialog);
1050
1051         report_dives(TRUE);
1052 }
1053
1054 void update_progressbar(progressbar_t *progress, double value)
1055 {
1056         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1057 }
1058
1059
1060 void set_filename(const char *filename)
1061 {
1062         if (!existing_filename && filename)
1063                 existing_filename = strdup(filename);
1064         return;
1065 }