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
15 #include "display-gtk.h"
17 #include "libdivecomputer.h"
19 GtkWidget *main_window;
21 GtkWidget *error_info_bar;
22 GtkWidget *error_label;
23 GtkWidget *vpane, *hpane;
26 const char *divelist_font;
28 struct units output_units;
30 static GtkWidget *dive_profile;
32 visible_cols_t visible_cols = {TRUE, FALSE};
34 static const char *default_dive_computer_vendor;
35 static const char *default_dive_computer_product;
36 static const char *default_dive_computer_device;
38 static int is_default_dive_computer(const char *vendor, const char *product)
40 return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) &&
41 default_dive_computer_product && !strcmp(product, default_dive_computer_product);
44 static int is_default_dive_computer_device(const char *name)
46 return default_dive_computer_device && !strcmp(name, default_dive_computer_device);
49 static void set_default_dive_computer(const char *vendor, const char *product)
51 if (!vendor || !*vendor)
53 if (!product || !*product)
55 if (is_default_dive_computer(vendor, product))
57 default_dive_computer_vendor = vendor;
58 default_dive_computer_product = product;
59 subsurface_set_conf("dive_computer_vendor", PREF_STRING, vendor);
60 subsurface_set_conf("dive_computer_product", PREF_STRING, product);
63 static void set_default_dive_computer_device(const char *name)
67 if (is_default_dive_computer_device(name))
69 default_dive_computer_device = name;
70 subsurface_set_conf("dive_computer_device", PREF_STRING, name);
73 void repaint_dive(void)
75 update_dive(current_dive);
77 gtk_widget_queue_draw(dive_profile);
80 static char *existing_filename;
81 static gboolean need_icon = TRUE;
83 static void on_info_bar_response(GtkWidget *widget, gint response,
86 if (response == GTK_RESPONSE_OK)
88 gtk_widget_destroy(widget);
89 error_info_bar = NULL;
93 void report_error(GError* error)
100 if (error_info_bar == NULL)
103 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
106 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
107 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
110 error_label = gtk_label_new(error->message);
111 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
112 gtk_container_add(GTK_CONTAINER(container), error_label);
114 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
115 gtk_widget_show_all(main_vbox);
121 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
122 gtk_label_set(GTK_LABEL(error_label), buffer);
126 static void file_open(GtkWidget *w, gpointer data)
129 GtkFileFilter *filter;
131 dialog = gtk_file_chooser_dialog_new("Open File",
132 GTK_WINDOW(main_window),
133 GTK_FILE_CHOOSER_ACTION_OPEN,
134 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
135 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
137 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
139 filter = gtk_file_filter_new();
140 gtk_file_filter_add_pattern(filter, "*.xml");
141 gtk_file_filter_add_pattern(filter, "*.XML");
142 gtk_file_filter_add_pattern(filter, "*.sda");
143 gtk_file_filter_add_pattern(filter, "*.SDA");
144 gtk_file_filter_add_mime_type(filter, "text/xml");
145 gtk_file_filter_set_name(filter, "XML file");
146 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
148 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
149 GSList *filenames, *fn_glist;
151 filenames = fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
153 GError *error = NULL;
154 while(filenames != NULL) {
155 filename = filenames->data;
156 parse_file(filename, &error);
165 filenames = g_slist_next(filenames);
167 g_slist_free(fn_glist);
170 gtk_widget_destroy(dialog);
173 static void file_save(GtkWidget *w, gpointer data)
176 dialog = gtk_file_chooser_dialog_new("Save File",
177 GTK_WINDOW(main_window),
178 GTK_FILE_CHOOSER_ACTION_SAVE,
179 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
180 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
182 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
183 if (!existing_filename) {
184 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
186 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
188 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
190 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
191 save_dives(filename);
193 mark_divelist_changed(FALSE);
195 gtk_widget_destroy(dialog);
198 static void ask_save_changes()
200 GtkWidget *dialog, *label, *content;
201 dialog = gtk_dialog_new_with_buttons("Save Changes?",
202 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
203 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
204 GTK_STOCK_NO, GTK_RESPONSE_NO,
206 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
207 label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
208 gtk_container_add (GTK_CONTAINER (content), label);
209 gtk_widget_show_all (dialog);
210 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
211 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
212 file_save(NULL,NULL);
214 gtk_widget_destroy(dialog);
217 static gboolean on_delete(GtkWidget* w, gpointer data)
219 /* Make sure to flush any modified dive data */
222 if (unsaved_changes())
225 return FALSE; /* go ahead, kill the program, we're good now */
228 static void on_destroy(GtkWidget* w, gpointer data)
233 static void quit(GtkWidget *w, gpointer data)
235 /* Make sure to flush any modified dive data */
238 if (unsaved_changes())
243 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
244 data_func_t data_func, unsigned int flags)
246 GtkCellRenderer *renderer;
247 GtkTreeViewColumn *col;
248 double xalign = 0.0; /* left as default */
249 PangoAlignment align;
252 align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
253 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
255 visible = !(flags & INVISIBLE);
257 renderer = gtk_cell_renderer_text_new();
258 col = gtk_tree_view_column_new();
260 gtk_tree_view_column_set_title(col, title);
261 if (!(flags & UNSORTABLE))
262 gtk_tree_view_column_set_sort_column_id(col, index);
263 gtk_tree_view_column_set_resizable(col, TRUE);
264 gtk_tree_view_column_pack_start(col, renderer, TRUE);
266 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
268 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
269 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
271 case PANGO_ALIGN_LEFT:
274 case PANGO_ALIGN_CENTER:
277 case PANGO_ALIGN_RIGHT:
281 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
282 gtk_tree_view_column_set_visible(col, visible);
283 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
287 static void create_radio(GtkWidget *vbox, const char *w_name, ...)
290 GtkRadioButton *group = NULL;
291 GtkWidget *box, *label;
293 box = gtk_hbox_new(TRUE, 10);
294 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
296 label = gtk_label_new(w_name);
297 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
299 va_start(args, w_name);
306 name = va_arg(args, char *);
309 callback_fn = va_arg(args, void *);
310 enabled = va_arg(args, int);
312 button = gtk_radio_button_new_with_label_from_widget(group, name);
313 group = GTK_RADIO_BUTTON(button);
314 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
315 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
316 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
321 #define UNITCALLBACK(name, type, value) \
322 static void name(GtkWidget *w, gpointer data) \
324 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
325 menu_units.type = value; \
328 static struct units menu_units;
330 UNITCALLBACK(set_meter, length, METERS)
331 UNITCALLBACK(set_feet, length, FEET)
332 UNITCALLBACK(set_bar, pressure, BAR)
333 UNITCALLBACK(set_psi, pressure, PSI)
334 UNITCALLBACK(set_liter, volume, LITER)
335 UNITCALLBACK(set_cuft, volume, CUFT)
336 UNITCALLBACK(set_celsius, temperature, CELSIUS)
337 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
338 UNITCALLBACK(set_kg, weight, KG)
339 UNITCALLBACK(set_lbs, weight, LBS)
341 #define OPTIONCALLBACK(name, option) \
342 static void name(GtkWidget *w, gpointer data) \
344 option = GTK_TOGGLE_BUTTON(w)->active; \
347 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
348 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
349 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
350 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
351 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
353 static void event_toggle(GtkWidget *w, gpointer _data)
355 gboolean *plot_ev = _data;
357 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
360 static void preferences_dialog(GtkWidget *w, gpointer data)
363 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
365 menu_units = output_units;
367 dialog = gtk_dialog_new_with_buttons("Preferences",
368 GTK_WINDOW(main_window),
369 GTK_DIALOG_DESTROY_WITH_PARENT,
370 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
371 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
374 frame = gtk_frame_new("Units");
375 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
376 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
378 box = gtk_vbox_new(FALSE, 6);
379 gtk_container_add(GTK_CONTAINER(frame), box);
381 create_radio(box, "Depth:",
382 "Meter", set_meter, (output_units.length == METERS),
383 "Feet", set_feet, (output_units.length == FEET),
386 create_radio(box, "Pressure:",
387 "Bar", set_bar, (output_units.pressure == BAR),
388 "PSI", set_psi, (output_units.pressure == PSI),
391 create_radio(box, "Volume:",
392 "Liter", set_liter, (output_units.volume == LITER),
393 "CuFt", set_cuft, (output_units.volume == CUFT),
396 create_radio(box, "Temperature:",
397 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
398 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
401 create_radio(box, "Weight:",
402 "kg", set_kg, (output_units.weight == KG),
403 "lbs", set_lbs, (output_units.weight == LBS),
406 frame = gtk_frame_new("Columns");
407 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
409 box = gtk_hbox_new(FALSE, 6);
410 gtk_container_add(GTK_CONTAINER(frame), box);
412 button = gtk_check_button_new_with_label("Show Temp");
413 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
414 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
415 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
417 button = gtk_check_button_new_with_label("Show Cyl");
418 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
419 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
420 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
422 button = gtk_check_button_new_with_label("Show O" UTF8_SUBSCRIPT_2 "%");
423 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
424 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
425 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
427 button = gtk_check_button_new_with_label("Show SAC");
428 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
429 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
430 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
432 button = gtk_check_button_new_with_label("Show OTU");
433 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
434 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
435 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
437 font = gtk_font_button_new_with_font(divelist_font);
438 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
440 gtk_widget_show_all(dialog);
441 result = gtk_dialog_run(GTK_DIALOG(dialog));
442 if (result == GTK_RESPONSE_ACCEPT) {
443 /* Make sure to flush any modified old dive data with old units */
446 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
447 set_divelist_font(divelist_font);
449 output_units = menu_units;
450 update_dive_list_units();
452 update_dive_list_col_visibility();
454 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
455 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
456 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
457 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
458 subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
459 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
460 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
461 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
462 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
463 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
464 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
466 /* Flush the changes out to the system */
467 subsurface_flush_conf();
469 gtk_widget_destroy(dialog);
472 static void create_toggle(const char* label, int *on, void *_data)
474 GtkWidget *button, *table = _data;
475 int rows, cols, x, y;
479 /* magic way to reset the number of toggle buttons
480 * that we have already added - call this before you
481 * create the dialog */
485 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
486 if (count > rows * cols) {
487 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
492 button = gtk_check_button_new_with_label(label);
493 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
494 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
495 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
499 static void selectevents_dialog(GtkWidget *w, gpointer data)
502 GtkWidget *dialog, *frame, *vbox, *table;
504 dialog = gtk_dialog_new_with_buttons("SelectEvents",
505 GTK_WINDOW(main_window),
506 GTK_DIALOG_DESTROY_WITH_PARENT,
507 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
508 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
510 /* initialize the function that fills the table */
511 create_toggle(NULL, NULL, NULL);
513 frame = gtk_frame_new("Enable / Disable Events");
514 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
515 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
517 table = gtk_table_new(1, 4, TRUE);
518 gtk_container_add(GTK_CONTAINER(frame), table);
520 evn_foreach(&create_toggle, table);
522 gtk_widget_show_all(dialog);
523 result = gtk_dialog_run(GTK_DIALOG(dialog));
524 if (result == GTK_RESPONSE_ACCEPT) {
527 gtk_widget_destroy(dialog);
530 static void renumber_dialog(GtkWidget *w, gpointer data)
534 GtkWidget *dialog, *frame, *button, *vbox;
536 dialog = gtk_dialog_new_with_buttons("Renumber",
537 GTK_WINDOW(main_window),
538 GTK_DIALOG_DESTROY_WITH_PARENT,
539 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
540 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
543 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
545 frame = gtk_frame_new("New starting number");
546 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
548 button = gtk_spin_button_new_with_range(1, 50000, 1);
549 gtk_container_add(GTK_CONTAINER(frame), button);
552 * Do we have a number for the first dive already? Use that
556 if (dive && dive->number)
557 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
559 gtk_widget_show_all(dialog);
560 result = gtk_dialog_run(GTK_DIALOG(dialog));
561 if (result == GTK_RESPONSE_ACCEPT) {
562 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
566 gtk_widget_destroy(dialog);
569 static void about_dialog(GtkWidget *w, gpointer data)
571 const char *logo_property = NULL;
572 GdkPixbuf *logo = NULL;
575 GtkWidget *image = gtk_image_new_from_file(subsurface_icon_name());
578 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
579 logo_property = "logo";
583 gtk_show_about_dialog(NULL,
584 "program-name", "SubSurface",
585 "comments", "Half-arsed divelog software in C",
587 "version", VERSION_STRING,
588 "copyright", "Linus Torvalds 2011",
589 "logo-icon-name", "subsurface",
595 static void view_list(GtkWidget *w, gpointer data)
597 gtk_paned_set_position(GTK_PANED(vpane), 0);
600 static void view_profile(GtkWidget *w, gpointer data)
602 gtk_paned_set_position(GTK_PANED(hpane), 0);
603 gtk_paned_set_position(GTK_PANED(vpane), 65535);
606 static void view_info(GtkWidget *w, gpointer data)
608 gtk_paned_set_position(GTK_PANED(vpane), 65535);
609 gtk_paned_set_position(GTK_PANED(hpane), 65535);
612 static void view_three(GtkWidget *w, gpointer data)
615 gtk_widget_get_allocation(hpane, &alloc);
616 gtk_paned_set_position(GTK_PANED(hpane), alloc.width/2);
617 gtk_widget_get_allocation(vpane, &alloc);
618 gtk_paned_set_position(GTK_PANED(vpane), alloc.height/2);
621 static GtkActionEntry menu_items[] = {
622 { "FileMenuAction", NULL, "File", NULL, NULL, NULL},
623 { "LogMenuAction", NULL, "Log", NULL, NULL, NULL},
624 { "ViewMenuAction", NULL, "View", NULL, NULL, NULL},
625 { "FilterMenuAction", NULL, "Filter", NULL, NULL, NULL},
626 { "HelpMenuAction", NULL, "Help", NULL, NULL, NULL},
627 { "OpenFile", GTK_STOCK_OPEN, NULL, CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
628 { "SaveFile", GTK_STOCK_SAVE, NULL, CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
629 { "Print", GTK_STOCK_PRINT, NULL, CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
630 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
631 { "AddDive", GTK_STOCK_ADD, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) },
632 { "Preferences", GTK_STOCK_PREFERENCES, "Preferences", PREFERENCE_ACCEL, NULL, G_CALLBACK(preferences_dialog) },
633 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
634 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
635 { "Quit", GTK_STOCK_QUIT, NULL, CTRLCHAR "Q", NULL, G_CALLBACK(quit) },
636 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
637 { "ViewList", NULL, "List", CTRLCHAR "1", NULL, G_CALLBACK(view_list) },
638 { "ViewProfile", NULL, "Profile", CTRLCHAR "2", NULL, G_CALLBACK(view_profile) },
639 { "ViewInfo", NULL, "Info", CTRLCHAR "3", NULL, G_CALLBACK(view_info) },
640 { "ViewThree", NULL, "Three", CTRLCHAR "4", NULL, G_CALLBACK(view_three) },
642 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
644 static const gchar* ui_string = " \
646 <menubar name=\"MainMenu\"> \
647 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
648 <menuitem name=\"Open\" action=\"OpenFile\" /> \
649 <menuitem name=\"Save\" action=\"SaveFile\" /> \
650 <menuitem name=\"Print\" action=\"Print\" /> \
651 <separator name=\"Separator1\"/> \
652 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
653 <separator name=\"Separator2\"/> \
654 <menuitem name=\"Quit\" action=\"Quit\" /> \
656 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
657 <menuitem name=\"Import\" action=\"Import\" /> \
658 <menuitem name=\"Add Dive\" action=\"AddDive\" /> \
659 <separator name=\"Separator\"/> \
660 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
661 <menu name=\"View\" action=\"ViewMenuAction\"> \
662 <menuitem name=\"List\" action=\"ViewList\" /> \
663 <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
664 <menuitem name=\"Info\" action=\"ViewInfo\" /> \
665 <menuitem name=\"Paned\" action=\"ViewThree\" /> \
668 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
669 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
671 <menu name=\"Help\" action=\"HelpMenuAction\"> \
672 <menuitem name=\"About\" action=\"About\" /> \
678 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
680 GtkActionGroup *action_group = gtk_action_group_new("Menu");
681 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
683 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
685 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
687 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
688 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
693 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
698 void init_ui(int *argcp, char ***argvp)
703 GtkWidget *dive_list;
707 GtkIconTheme *icon_theme=NULL;
708 GtkSettings *settings;
709 GtkUIManager *ui_manager;
711 gtk_init(argcp, argvp);
712 settings = gtk_settings_get_default();
713 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
717 subsurface_open_conf();
718 if (subsurface_get_conf("feet", PREF_BOOL))
719 output_units.length = FEET;
720 if (subsurface_get_conf("psi", PREF_BOOL))
721 output_units.pressure = PSI;
722 if (subsurface_get_conf("cuft", PREF_BOOL))
723 output_units.volume = CUFT;
724 if (subsurface_get_conf("fahrenheit", PREF_BOOL))
725 output_units.temperature = FAHRENHEIT;
726 if (subsurface_get_conf("lbs", PREF_BOOL))
727 output_units.weight = LBS;
728 /* an unset key is FALSE - all these are hidden by default */
729 visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
730 visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
731 visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
732 visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
733 visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
735 divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
737 default_dive_computer_vendor = subsurface_get_conf("dive_computer_vendor", PREF_STRING);
738 default_dive_computer_product = subsurface_get_conf("dive_computer_product", PREF_STRING);
739 default_dive_computer_device = subsurface_get_conf("dive_computer_device", PREF_STRING);
741 error_info_bar = NULL;
742 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
743 g_set_application_name ("subsurface");
744 /* Let's check if the subsurface icon has been installed or if
745 * we need to try to load it from the current directory */
746 screen = gdk_screen_get_default();
748 icon_theme = gtk_icon_theme_get_for_screen(screen);
750 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
752 gtk_window_set_default_icon_name ("subsurface");
756 const char *icon_name = subsurface_icon_name();
757 if (!access(icon_name, R_OK))
758 gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
760 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
761 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
764 vbox = gtk_vbox_new(FALSE, 0);
765 gtk_container_add(GTK_CONTAINER(win), vbox);
768 ui_manager = gtk_ui_manager_new();
769 menubar = get_menubar_menu(win, ui_manager);
771 subsurface_ui_setup(settings, menubar, vbox, ui_manager);
773 vpane = gtk_vpaned_new();
774 gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
776 hpane = gtk_hpaned_new();
777 gtk_paned_add1(GTK_PANED(vpane), hpane);
779 /* Notebook for dive info vs profile vs .. */
780 notebook = gtk_notebook_new();
781 gtk_paned_add1(GTK_PANED(hpane), notebook);
782 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
784 /* Create the actual divelist */
785 dive_list = dive_list_create();
786 gtk_widget_set_name(dive_list, "Dive List");
787 gtk_paned_add2(GTK_PANED(vpane), dive_list);
789 /* Frame for dive profile */
790 dive_profile = dive_profile_widget();
791 gtk_widget_set_name(dive_profile, "Dive Profile");
792 gtk_paned_add2(GTK_PANED(hpane), dive_profile);
794 /* Frame for extended dive info */
795 nb_page = extended_dive_info_widget();
796 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Notes"));
798 /* Frame for dive equipment */
799 nb_page = equipment_widget();
800 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Equipment"));
802 /* Frame for single dive statistics */
803 nb_page = single_stats_widget();
804 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
806 /* Frame for total dive statistics */
807 nb_page = total_stats_widget();
808 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Stats"));
810 gtk_widget_set_app_paintable(win, TRUE);
811 gtk_widget_show_all(win);
823 subsurface_close_conf();
827 cairo_rectangle_int_t rect;
831 static tooltip_record_t *tooltip_rects;
834 void attach_tooltip(int x, int y, int w, int h, const char *text)
836 cairo_rectangle_int_t *rect;
837 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
838 rect = &tooltip_rects[tooltips].rect;
843 tooltip_rects[tooltips].text = text;
847 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
848 (_r.y <= _y) && (_r.y + _r.height >= _y))
850 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
851 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
854 cairo_rectangle_int_t *drawing_area = user_data;
855 gint tx = x - drawing_area->x; /* get transformed coordinates */
856 gint ty = y - drawing_area->y;
858 /* are we over an event marker ? */
859 for (i = 0; i < tooltips; i++) {
860 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
861 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
862 return TRUE; /* show tooltip */
865 return FALSE; /* don't show tooltip */
868 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
870 struct dive *dive = current_dive;
871 struct graphics_context gc = { .printer = 0 };
872 static cairo_rectangle_int_t drawing_area;
874 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
875 * so effective drawing area is width-2x * height-2y */
876 drawing_area.width = widget->allocation.width;
877 drawing_area.height = widget->allocation.height;
878 drawing_area.x = drawing_area.width / 20.0;
879 drawing_area.y = drawing_area.height / 20.0;
881 gc.cr = gdk_cairo_create(widget->window);
882 g_object_set(widget, "has-tooltip", TRUE, NULL);
883 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
884 init_profile_background(&gc);
890 tooltip_rects = NULL;
893 plot(&gc, &drawing_area, dive);
895 cairo_destroy(gc.cr);
900 GtkWidget *dive_profile_widget(void)
904 da = gtk_drawing_area_new();
905 gtk_widget_set_size_request(da, 350, 250);
906 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
911 int process_ui_events(void)
915 while (gtk_events_pending()) {
916 if (gtk_main_iteration_do(0)) {
924 static int fill_computer_list(GtkListStore *store)
928 dc_iterator_t *iterator = NULL;
929 dc_descriptor_t *descriptor = NULL;
932 dc_descriptor_iterator(&iterator);
933 while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) {
934 const char *vendor = dc_descriptor_get_vendor(descriptor);
935 const char *product = dc_descriptor_get_product(descriptor);
937 gtk_list_store_append(store, &iter);
938 gtk_list_store_set(store, &iter,
941 if (is_default_dive_computer(vendor, product))
945 dc_iterator_free(iterator);
949 void render_dive_computer(GtkCellLayout *cell,
950 GtkCellRenderer *renderer,
956 dc_descriptor_t *descriptor = NULL;
957 const char *vendor, *product;
959 gtk_tree_model_get(model, iter, 0, &descriptor, -1);
960 vendor = dc_descriptor_get_vendor(descriptor);
961 product = dc_descriptor_get_product(descriptor);
962 snprintf(buffer, sizeof(buffer), "%s %s", vendor, product);
963 g_object_set(renderer, "text", buffer, NULL);
967 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
969 GtkWidget *hbox, *combo_box, *frame;
971 GtkCellRenderer *renderer;
974 hbox = gtk_hbox_new(FALSE, 6);
975 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
977 model = gtk_list_store_new(1, G_TYPE_POINTER);
978 default_index = fill_computer_list(model);
980 frame = gtk_frame_new("Dive computer");
981 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
983 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
984 gtk_container_add(GTK_CONTAINER(frame), combo_box);
986 renderer = gtk_cell_renderer_text_new();
987 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
988 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo_box), renderer, render_dive_computer, NULL, NULL);
990 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
992 return GTK_COMBO_BOX(combo_box);
995 const char *subsurface_device_name()
997 if (!default_dive_computer_device || !*default_dive_computer_device)
998 return subsurface_USB_name();
1000 return default_dive_computer_device;
1003 static GtkEntry *dive_computer_device(GtkWidget *vbox)
1005 GtkWidget *hbox, *entry, *frame;
1007 hbox = gtk_hbox_new(FALSE, 6);
1008 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1010 frame = gtk_frame_new("Device name");
1011 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1013 entry = gtk_entry_new();
1014 gtk_container_add(GTK_CONTAINER(frame), entry);
1015 gtk_entry_set_text(GTK_ENTRY(entry), subsurface_device_name());
1017 return GTK_ENTRY(entry);
1020 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
1021 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
1023 GtkDialog *main_dialog = _data;
1025 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
1028 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
1030 GtkWidget *hbox, *frame, *chooser, *dialog;
1031 GtkFileFilter *filter;
1033 hbox = gtk_hbox_new(FALSE, 6);
1034 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1036 frame = gtk_frame_new("XML file name");
1037 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1038 dialog = gtk_file_chooser_dialog_new("Open XML File",
1039 GTK_WINDOW(main_window),
1040 GTK_FILE_CHOOSER_ACTION_OPEN,
1041 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1042 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1044 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
1046 filter = gtk_file_filter_new();
1047 gtk_file_filter_add_pattern(filter, "*.xml");
1048 gtk_file_filter_add_pattern(filter, "*.XML");
1049 gtk_file_filter_add_pattern(filter, "*.sda");
1050 gtk_file_filter_add_pattern(filter, "*.SDA");
1051 gtk_file_filter_add_mime_type(filter, "text/xml");
1052 gtk_file_filter_set_name(filter, "XML file");
1053 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
1055 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
1056 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
1058 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
1059 gtk_container_add(GTK_CONTAINER(frame), chooser);
1064 static void do_import_file(gpointer data, gpointer user_data)
1066 GError *error = NULL;
1067 parse_file(data, &error);
1071 report_error(error);
1072 g_error_free(error);
1077 static GtkWidget *import_dive_computer(device_data_t *data, GtkDialog *dialog)
1080 GtkWidget *vbox, *info, *container, *label, *button;
1082 error = do_import(data);
1086 button = gtk_dialog_get_widget_for_response(dialog, GTK_RESPONSE_ACCEPT);
1087 gtk_button_set_use_stock(GTK_BUTTON(button), 0);
1088 gtk_button_set_label(GTK_BUTTON(button), "Retry");
1090 vbox = gtk_dialog_get_content_area(dialog);
1092 info = gtk_info_bar_new();
1093 container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
1094 label = gtk_label_new(error->message);
1095 gtk_container_add(GTK_CONTAINER(container), label);
1096 gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
1100 void import_dialog(GtkWidget *w, gpointer data)
1103 GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
1104 GtkComboBox *computer;
1106 GtkWidget *XMLchooser;
1107 device_data_t devicedata = {
1111 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1112 GTK_WINDOW(main_window),
1113 GTK_DIALOG_DESTROY_WITH_PARENT,
1114 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1115 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1118 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1119 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1120 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1121 XMLchooser = xml_file_selector(vbox, dialog);
1122 computer = dive_computer_selector(vbox);
1123 device = dive_computer_device(vbox);
1124 hbox = gtk_hbox_new(FALSE, 6);
1125 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1126 devicedata.progress.bar = gtk_progress_bar_new();
1127 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1130 gtk_widget_show_all(dialog);
1131 result = gtk_dialog_run(GTK_DIALOG(dialog));
1133 dc_descriptor_t *descriptor;
1135 GtkTreeModel *model;
1137 case GTK_RESPONSE_ACCEPT:
1138 /* what happened - did the user pick a file? In that case
1139 * we ignore whether a dive computer model was picked */
1141 gtk_widget_destroy(info);
1142 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1143 if (g_slist_length(list) == 0) {
1144 const char *vendor, *product;
1146 if (!gtk_combo_box_get_active_iter(computer, &iter))
1148 model = gtk_combo_box_get_model(computer);
1149 gtk_tree_model_get(model, &iter,
1153 vendor = dc_descriptor_get_vendor(descriptor);
1154 product = dc_descriptor_get_product(descriptor);
1156 devicedata.descriptor = descriptor;
1157 devicedata.vendor = vendor;
1158 devicedata.product = product;
1159 devicedata.devname = gtk_entry_get_text(device);
1160 set_default_dive_computer(vendor, product);
1161 set_default_dive_computer_device(devicedata.devname);
1162 info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
1166 g_slist_foreach(list,do_import_file,NULL);
1173 gtk_widget_destroy(dialog);
1178 void update_progressbar(progressbar_t *progress, double value)
1180 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1183 void update_progressbar_text(progressbar_t *progress, const char *text)
1185 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), text);
1188 void set_filename(const char *filename)
1190 if (!existing_filename && filename)
1191 existing_filename = strdup(filename);