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
14 #include "display-gtk.h"
16 #include "libdivecomputer.h"
18 GtkWidget *main_window;
20 GtkWidget *error_info_bar;
21 GtkWidget *error_label;
24 #define DIVELIST_DEFAULT_FONT "Sans 8"
25 const char *divelist_font;
27 struct units output_units;
29 static GtkWidget *dive_profile;
31 visible_cols_t visible_cols = {TRUE, FALSE};
33 void repaint_dive(void)
35 update_dive(current_dive);
37 gtk_widget_queue_draw(dive_profile);
40 static char *existing_filename;
41 static gboolean need_icon = TRUE;
43 static void on_info_bar_response(GtkWidget *widget, gint response,
46 if (response == GTK_RESPONSE_OK)
48 gtk_widget_destroy(widget);
49 error_info_bar = NULL;
53 void report_error(GError* error)
60 if (error_info_bar == NULL)
63 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
66 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
67 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
70 error_label = gtk_label_new(error->message);
71 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
72 gtk_container_add(GTK_CONTAINER(container), error_label);
74 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
75 gtk_widget_show_all(main_vbox);
81 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
82 gtk_label_set(GTK_LABEL(error_label), buffer);
86 static void file_open(GtkWidget *w, gpointer data)
89 GtkFileFilter *filter;
91 dialog = gtk_file_chooser_dialog_new("Open File",
92 GTK_WINDOW(main_window),
93 GTK_FILE_CHOOSER_ACTION_OPEN,
94 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
95 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
97 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
99 filter = gtk_file_filter_new();
100 gtk_file_filter_add_pattern(filter, "*.xml");
101 gtk_file_filter_add_pattern(filter, "*.XML");
102 gtk_file_filter_add_pattern(filter, "*.sda");
103 gtk_file_filter_add_pattern(filter, "*.SDA");
104 gtk_file_filter_add_mime_type(filter, "text/xml");
105 gtk_file_filter_set_name(filter, "XML file");
106 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
108 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
111 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
113 GError *error = NULL;
114 while(filenames != NULL) {
115 filename = filenames->data;
116 parse_xml_file(filename, &error);
125 filenames = g_slist_next(filenames);
127 g_slist_free(filenames);
130 gtk_widget_destroy(dialog);
133 static void file_save(GtkWidget *w, gpointer data)
136 dialog = gtk_file_chooser_dialog_new("Save File",
137 GTK_WINDOW(main_window),
138 GTK_FILE_CHOOSER_ACTION_SAVE,
139 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
140 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
142 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
143 if (!existing_filename) {
144 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
146 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
148 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
150 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
151 save_dives(filename);
153 mark_divelist_changed(FALSE);
155 gtk_widget_destroy(dialog);
158 static void ask_save_changes()
160 GtkWidget *dialog, *label, *content;
161 dialog = gtk_dialog_new_with_buttons("Save Changes?",
162 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
163 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
164 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
166 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
167 label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
168 gtk_container_add (GTK_CONTAINER (content), label);
169 gtk_widget_show_all (dialog);
170 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
171 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
172 file_save(NULL,NULL);
174 gtk_widget_destroy(dialog);
177 static gboolean on_delete(GtkWidget* w, gpointer data)
179 /* Make sure to flush any modified dive data */
182 if (unsaved_changes())
185 return FALSE; /* go ahead, kill the program, we're good now */
188 static void on_destroy(GtkWidget* w, gpointer data)
193 static void quit(GtkWidget *w, gpointer data)
195 /* Make sure to flush any modified dive data */
198 if (unsaved_changes())
203 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
204 data_func_t data_func, PangoAlignment align, gboolean visible)
206 GtkCellRenderer *renderer;
207 GtkTreeViewColumn *col;
208 double xalign = 0.0; /* left as default */
210 renderer = gtk_cell_renderer_text_new();
211 col = gtk_tree_view_column_new();
213 gtk_tree_view_column_set_title(col, title);
214 gtk_tree_view_column_set_sort_column_id(col, index);
215 gtk_tree_view_column_set_resizable(col, TRUE);
216 gtk_tree_view_column_pack_start(col, renderer, TRUE);
218 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
220 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
221 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
223 case PANGO_ALIGN_LEFT:
226 case PANGO_ALIGN_CENTER:
229 case PANGO_ALIGN_RIGHT:
233 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
234 gtk_tree_view_column_set_visible(col, visible);
235 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
239 static void create_radio(GtkWidget *vbox, const char *name, ...)
242 GtkRadioButton *group = NULL;
243 GtkWidget *box, *label;
245 box = gtk_hbox_new(TRUE, 10);
246 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
248 label = gtk_label_new(name);
249 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
251 va_start(args, name);
258 name = va_arg(args, char *);
261 callback_fn = va_arg(args, void *);
262 enabled = va_arg(args, int);
264 button = gtk_radio_button_new_with_label_from_widget(group, name);
265 group = GTK_RADIO_BUTTON(button);
266 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
267 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
268 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
273 #define UNITCALLBACK(name, type, value) \
274 static void name(GtkWidget *w, gpointer data) \
276 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
277 menu_units.type = value; \
280 static struct units menu_units;
282 UNITCALLBACK(set_meter, length, METERS)
283 UNITCALLBACK(set_feet, length, FEET)
284 UNITCALLBACK(set_bar, pressure, BAR)
285 UNITCALLBACK(set_psi, pressure, PSI)
286 UNITCALLBACK(set_liter, volume, LITER)
287 UNITCALLBACK(set_cuft, volume, CUFT)
288 UNITCALLBACK(set_celsius, temperature, CELSIUS)
289 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
291 #define OPTIONCALLBACK(name, option) \
292 static void name(GtkWidget *w, gpointer data) \
294 option = GTK_TOGGLE_BUTTON(w)->active; \
297 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
298 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
299 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
300 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
301 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
303 static void event_toggle(GtkWidget *w, gpointer _data)
305 gboolean *plot_ev = _data;
307 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
310 static void preferences_dialog(GtkWidget *w, gpointer data)
313 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
315 menu_units = output_units;
317 dialog = gtk_dialog_new_with_buttons("Preferences",
318 GTK_WINDOW(main_window),
319 GTK_DIALOG_DESTROY_WITH_PARENT,
320 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
321 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
324 frame = gtk_frame_new("Units");
325 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
326 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
328 box = gtk_vbox_new(FALSE, 6);
329 gtk_container_add(GTK_CONTAINER(frame), box);
331 create_radio(box, "Depth:",
332 "Meter", set_meter, (output_units.length == METERS),
333 "Feet", set_feet, (output_units.length == FEET),
336 create_radio(box, "Pressure:",
337 "Bar", set_bar, (output_units.pressure == BAR),
338 "PSI", set_psi, (output_units.pressure == PSI),
341 create_radio(box, "Volume:",
342 "Liter", set_liter, (output_units.volume == LITER),
343 "CuFt", set_cuft, (output_units.volume == CUFT),
346 create_radio(box, "Temperature:",
347 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
348 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
351 frame = gtk_frame_new("Columns");
352 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
354 box = gtk_hbox_new(FALSE, 6);
355 gtk_container_add(GTK_CONTAINER(frame), box);
357 button = gtk_check_button_new_with_label("Show Temp");
358 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
359 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
360 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
362 button = gtk_check_button_new_with_label("Show Cyl");
363 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
364 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
365 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
367 button = gtk_check_button_new_with_label("Show O" UTF8_SUBSCRIPT_2 "%");
368 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
369 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
370 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
372 button = gtk_check_button_new_with_label("Show SAC");
373 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
374 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
375 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
377 button = gtk_check_button_new_with_label("Show OTU");
378 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
379 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
380 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
382 font = gtk_font_button_new_with_font(divelist_font);
383 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
385 gtk_widget_show_all(dialog);
386 result = gtk_dialog_run(GTK_DIALOG(dialog));
387 if (result == GTK_RESPONSE_ACCEPT) {
388 /* Make sure to flush any modified old dive data with old units */
391 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
392 set_divelist_font(divelist_font);
394 output_units = menu_units;
395 update_dive_list_units();
397 update_dive_list_col_visibility();
399 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
400 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
401 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
402 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
403 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
404 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
405 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
406 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
407 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
408 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
409 subsurface_close_conf();
411 gtk_widget_destroy(dialog);
414 static void create_toggle(const char* label, int *on, void *_data)
416 GtkWidget *button, *table = _data;
417 int rows, cols, x, y;
421 /* magic way to reset the number of toggle buttons
422 * that we have already added - call this before you
423 * create the dialog */
427 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
428 if (count > rows * cols) {
429 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
434 button = gtk_check_button_new_with_label(label);
435 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
436 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
437 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
441 static void selectevents_dialog(GtkWidget *w, gpointer data)
444 GtkWidget *dialog, *frame, *vbox, *table;
446 dialog = gtk_dialog_new_with_buttons("SelectEvents",
447 GTK_WINDOW(main_window),
448 GTK_DIALOG_DESTROY_WITH_PARENT,
449 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
450 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
452 /* initialize the function that fills the table */
453 create_toggle(NULL, NULL, NULL);
455 frame = gtk_frame_new("Enable / Disable Events");
456 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
457 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
459 table = gtk_table_new(1, 4, TRUE);
460 gtk_container_add(GTK_CONTAINER(frame), table);
462 evn_foreach(&create_toggle, table);
464 gtk_widget_show_all(dialog);
465 result = gtk_dialog_run(GTK_DIALOG(dialog));
466 if (result == GTK_RESPONSE_ACCEPT) {
469 gtk_widget_destroy(dialog);
472 static void renumber_dialog(GtkWidget *w, gpointer data)
476 GtkWidget *dialog, *frame, *button, *vbox;
478 dialog = gtk_dialog_new_with_buttons("Renumber",
479 GTK_WINDOW(main_window),
480 GTK_DIALOG_DESTROY_WITH_PARENT,
481 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
482 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
485 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
487 frame = gtk_frame_new("New starting number");
488 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
490 button = gtk_spin_button_new_with_range(1, 50000, 1);
491 gtk_container_add(GTK_CONTAINER(frame), button);
494 * Do we have a number for the first dive already? Use that
498 if (dive && dive->number)
499 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
501 gtk_widget_show_all(dialog);
502 result = gtk_dialog_run(GTK_DIALOG(dialog));
503 if (result == GTK_RESPONSE_ACCEPT) {
504 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
508 gtk_widget_destroy(dialog);
511 static void about_dialog(GtkWidget *w, gpointer data)
513 const char *logo_property = NULL;
514 GdkPixbuf *logo = NULL;
517 #if defined __linux__ || defined __APPLE__
518 GtkWidget *image = gtk_image_new_from_file("subsurface.svg");
520 GtkWidget *image = gtk_image_new_from_file("subsurface.ico");
524 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
525 logo_property = "logo";
529 gtk_show_about_dialog(NULL,
530 "program-name", "SubSurface",
531 "comments", "Half-arsed divelog software in C",
533 "version", VERSION_STRING,
534 "copyright", "Linus Torvalds 2011",
535 "logo-icon-name", "subsurface",
541 static GtkActionEntry menu_items[] = {
542 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
543 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
544 { "FilterMenuAction", GTK_STOCK_FILE, "Filter", NULL, NULL, NULL},
545 { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
546 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
547 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
548 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
549 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
550 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
551 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
552 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
553 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
554 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
556 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
558 static const gchar* ui_string = " \
560 <menubar name=\"MainMenu\"> \
561 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
562 <menuitem name=\"Open\" action=\"OpenFile\" /> \
563 <menuitem name=\"Save\" action=\"SaveFile\" /> \
564 <menuitem name=\"Print\" action=\"Print\" /> \
565 <separator name=\"Separator1\"/> \
566 <menuitem name=\"Import\" action=\"Import\" /> \
567 <separator name=\"Separator2\"/> \
568 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
569 <separator name=\"Separator3\"/> \
570 <menuitem name=\"Quit\" action=\"Quit\" /> \
572 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
573 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
575 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
576 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
578 <menu name=\"Help\" action=\"HelpMenuAction\"> \
579 <menuitem name=\"About\" action=\"About\" /> \
585 static GtkWidget *get_menubar_menu(GtkWidget *window)
587 GtkActionGroup *action_group = gtk_action_group_new("Menu");
588 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
590 GtkUIManager *ui_manager = gtk_ui_manager_new();
591 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
593 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
595 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
596 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
601 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
606 void init_ui(int *argcp, char ***argvp)
610 GtkWidget *dive_info;
611 GtkWidget *dive_list;
612 GtkWidget *equipment;
616 GtkWidget *hpane, *vpane;
618 GtkIconTheme *icon_theme=NULL;
619 GtkSettings *settings;
621 gtk_init(argcp, argvp);
622 settings = gtk_settings_get_default();
623 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
627 subsurface_open_conf();
628 if (subsurface_get_conf("feet", PREF_BOOL))
629 output_units.length = FEET;
630 if (subsurface_get_conf("psi", PREF_BOOL))
631 output_units.pressure = PSI;
632 if (subsurface_get_conf("cuft", PREF_BOOL))
633 output_units.volume = CUFT;
634 if (subsurface_get_conf("fahrenheit", PREF_BOOL))
635 output_units.temperature = FAHRENHEIT;
636 /* an unset key is FALSE - all these are hidden by default */
637 visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
638 visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
639 visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
640 visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
641 visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
643 divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
646 divelist_font = DIVELIST_DEFAULT_FONT;
648 error_info_bar = NULL;
649 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
650 g_set_application_name ("subsurface");
651 /* Let's check if the subsurface icon has been installed or if
652 * we need to try to load it from the current directory */
653 screen = gdk_screen_get_default();
655 icon_theme = gtk_icon_theme_get_for_screen(screen);
657 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
659 gtk_window_set_default_icon_name ("subsurface");
663 #if defined __linux__ || defined __APPLE__
664 gtk_window_set_icon_from_file(GTK_WINDOW(win), "subsurface.svg", NULL);
666 gtk_window_set_icon_from_file(GTK_WINDOW(win), "subsurface.ico", NULL);
668 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
669 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
672 vbox = gtk_vbox_new(FALSE, 0);
673 gtk_container_add(GTK_CONTAINER(win), vbox);
676 menubar = get_menubar_menu(win);
677 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
679 vpane = gtk_vpaned_new();
680 gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
682 hpane = gtk_hpaned_new();
683 gtk_paned_add1(GTK_PANED(vpane), hpane);
685 /* Notebook for dive info vs profile vs .. */
686 notebook = gtk_notebook_new();
687 gtk_paned_add1(GTK_PANED(hpane), notebook);
688 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
690 /* Create the actual divelist */
691 dive_list = dive_list_create();
692 gtk_widget_set_name(dive_list, "Dive List");
693 gtk_paned_add2(GTK_PANED(vpane), dive_list);
695 /* Frame for dive profile */
696 dive_profile = dive_profile_widget();
697 gtk_widget_set_name(dive_profile, "Dive Profile");
698 gtk_paned_add2(GTK_PANED(hpane), dive_profile);
700 /* Frame for extended dive info */
701 dive_info = extended_dive_info_widget();
702 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
704 /* Frame for dive equipment */
705 equipment = equipment_widget();
706 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
708 /* Frame for dive statistics */
709 stats = stats_widget();
710 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), stats, gtk_label_new("Info & Stats"));
712 gtk_widget_set_app_paintable(win, TRUE);
713 gtk_widget_show_all(win);
724 cairo_rectangle_int_t rect;
728 static tooltip_record_t *tooltip_rects;
731 void attach_tooltip(int x, int y, int w, int h, const char *text)
733 cairo_rectangle_int_t *rect;
734 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
735 rect = &tooltip_rects[tooltips].rect;
740 tooltip_rects[tooltips].text = text;
744 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
745 (_r.y <= _y) && (_r.y + _r.height >= _y))
747 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
748 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
751 cairo_rectangle_int_t *drawing_area = user_data;
752 gint tx = x - drawing_area->x; /* get transformed coordinates */
753 gint ty = y - drawing_area->y;
755 /* are we over an event marker ? */
756 for (i = 0; i < tooltips; i++) {
757 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
758 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
759 return TRUE; /* show tooltip */
762 return FALSE; /* don't show tooltip */
765 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
767 struct dive *dive = current_dive;
768 struct graphics_context gc = { .printer = 0 };
769 static cairo_rectangle_int_t drawing_area;
771 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
772 * so effective drawing area is width-2x * height-2y */
773 drawing_area.width = widget->allocation.width;
774 drawing_area.height = widget->allocation.height;
775 drawing_area.x = drawing_area.width / 20.0;
776 drawing_area.y = drawing_area.height / 20.0;
778 gc.cr = gdk_cairo_create(widget->window);
779 g_object_set(widget, "has-tooltip", TRUE, NULL);
780 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
781 set_source_rgb(&gc, 0, 0, 0);
787 tooltip_rects = NULL;
790 plot(&gc, &drawing_area, dive);
792 cairo_destroy(gc.cr);
797 GtkWidget *dive_profile_widget(void)
801 da = gtk_drawing_area_new();
802 gtk_widget_set_size_request(da, 350, 250);
803 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
808 int process_ui_events(void)
812 while (gtk_events_pending()) {
813 if (gtk_main_iteration_do(0)) {
822 static void fill_computer_list(GtkListStore *store)
825 struct device_list *list = device_list;
827 for (list = device_list ; list->name ; list++) {
828 gtk_list_store_append(store, &iter);
829 gtk_list_store_set(store, &iter,
836 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
838 GtkWidget *hbox, *combo_box, *frame;
840 GtkCellRenderer *renderer;
842 hbox = gtk_hbox_new(FALSE, 6);
843 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
845 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
846 fill_computer_list(model);
848 frame = gtk_frame_new("Dive computer");
849 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
851 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
852 gtk_container_add(GTK_CONTAINER(frame), combo_box);
854 renderer = gtk_cell_renderer_text_new();
855 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
856 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
858 return GTK_COMBO_BOX(combo_box);
861 static GtkEntry *dive_computer_device(GtkWidget *vbox)
863 GtkWidget *hbox, *entry, *frame;
865 hbox = gtk_hbox_new(FALSE, 6);
866 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
868 frame = gtk_frame_new("Device name");
869 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
871 entry = gtk_entry_new();
872 gtk_container_add(GTK_CONTAINER(frame), entry);
873 gtk_entry_set_text(GTK_ENTRY(entry), "/dev/ttyUSB0");
875 return GTK_ENTRY(entry);
878 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
879 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
881 GtkDialog *main_dialog = _data;
883 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
886 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
888 GtkWidget *hbox, *frame, *chooser, *dialog;
889 GtkFileFilter *filter;
891 hbox = gtk_hbox_new(FALSE, 6);
892 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
894 frame = gtk_frame_new("XML file name");
895 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
896 dialog = gtk_file_chooser_dialog_new("Open XML File",
897 GTK_WINDOW(main_window),
898 GTK_FILE_CHOOSER_ACTION_OPEN,
899 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
900 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
902 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
904 filter = gtk_file_filter_new();
905 gtk_file_filter_add_pattern(filter, "*.xml");
906 gtk_file_filter_add_pattern(filter, "*.XML");
907 gtk_file_filter_add_pattern(filter, "*.sda");
908 gtk_file_filter_add_pattern(filter, "*.SDA");
909 gtk_file_filter_add_mime_type(filter, "text/xml");
910 gtk_file_filter_set_name(filter, "XML file");
911 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
913 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
914 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
916 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
917 gtk_container_add(GTK_CONTAINER(frame), chooser);
922 static void do_import_file(gpointer data, gpointer user_data)
924 GError *error = NULL;
925 parse_xml_file(data, &error);
935 void import_dialog(GtkWidget *w, gpointer data)
938 GtkWidget *dialog, *hbox, *vbox, *label;
939 GtkComboBox *computer;
941 GtkWidget *XMLchooser;
942 device_data_t devicedata = {
946 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
947 GTK_WINDOW(main_window),
948 GTK_DIALOG_DESTROY_WITH_PARENT,
949 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
950 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
953 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
954 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
955 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
956 XMLchooser = xml_file_selector(vbox, dialog);
957 computer = dive_computer_selector(vbox);
958 device = dive_computer_device(vbox);
959 hbox = gtk_hbox_new(FALSE, 6);
960 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
961 devicedata.progress.bar = gtk_progress_bar_new();
962 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
964 gtk_widget_show_all(dialog);
965 result = gtk_dialog_run(GTK_DIALOG(dialog));
972 case GTK_RESPONSE_ACCEPT:
973 /* what happened - did the user pick a file? In that case
974 * we ignore whether a dive computer model was picked */
975 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
976 if (g_slist_length(list) == 0) {
977 if (!gtk_combo_box_get_active_iter(computer, &iter))
979 model = gtk_combo_box_get_model(computer);
980 gtk_tree_model_get(model, &iter,
984 devicedata.type = type;
985 devicedata.name = comp;
986 devicedata.devname = gtk_entry_get_text(device);
987 do_import(&devicedata);
989 g_slist_foreach(list,do_import_file,NULL);
996 gtk_widget_destroy(dialog);
1001 void update_progressbar(progressbar_t *progress, double value)
1003 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1007 void set_filename(const char *filename)
1009 if (!existing_filename && filename)
1010 existing_filename = strdup(filename);