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 void repaint_dive(void)
36 update_dive(current_dive);
38 gtk_widget_queue_draw(dive_profile);
41 static char *existing_filename;
42 static gboolean need_icon = TRUE;
44 static void on_info_bar_response(GtkWidget *widget, gint response,
47 if (response == GTK_RESPONSE_OK)
49 gtk_widget_destroy(widget);
50 error_info_bar = NULL;
54 void report_error(GError* error)
61 if (error_info_bar == NULL)
64 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
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),
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);
75 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
76 gtk_widget_show_all(main_vbox);
82 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
83 gtk_label_set(GTK_LABEL(error_label), buffer);
87 static void file_open(GtkWidget *w, gpointer data)
90 GtkFileFilter *filter;
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,
98 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
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);
109 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
110 GSList *filenames, *fn_glist;
112 filenames = fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
114 GError *error = NULL;
115 while(filenames != NULL) {
116 filename = filenames->data;
117 parse_xml_file(filename, &error);
126 filenames = g_slist_next(filenames);
128 g_slist_free(fn_glist);
131 gtk_widget_destroy(dialog);
134 static void file_save(GtkWidget *w, gpointer data)
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,
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");
147 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
149 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
151 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
152 save_dives(filename);
154 mark_divelist_changed(FALSE);
156 gtk_widget_destroy(dialog);
159 static void ask_save_changes()
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,
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);
175 gtk_widget_destroy(dialog);
178 static gboolean on_delete(GtkWidget* w, gpointer data)
180 /* Make sure to flush any modified dive data */
183 if (unsaved_changes())
186 return FALSE; /* go ahead, kill the program, we're good now */
189 static void on_destroy(GtkWidget* w, gpointer data)
194 static void quit(GtkWidget *w, gpointer data)
196 /* Make sure to flush any modified dive data */
199 if (unsaved_changes())
204 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
205 data_func_t data_func, unsigned int flags)
207 GtkCellRenderer *renderer;
208 GtkTreeViewColumn *col;
209 double xalign = 0.0; /* left as default */
210 PangoAlignment align;
213 align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
214 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
216 visible = !(flags & INVISIBLE);
218 renderer = gtk_cell_renderer_text_new();
219 col = gtk_tree_view_column_new();
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);
227 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
229 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
230 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
232 case PANGO_ALIGN_LEFT:
235 case PANGO_ALIGN_CENTER:
238 case PANGO_ALIGN_RIGHT:
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);
248 static void create_radio(GtkWidget *vbox, const char *name, ...)
251 GtkRadioButton *group = NULL;
252 GtkWidget *box, *label;
254 box = gtk_hbox_new(TRUE, 10);
255 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
257 label = gtk_label_new(name);
258 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
260 va_start(args, name);
267 name = va_arg(args, char *);
270 callback_fn = va_arg(args, void *);
271 enabled = va_arg(args, int);
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);
282 #define UNITCALLBACK(name, type, value) \
283 static void name(GtkWidget *w, gpointer data) \
285 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
286 menu_units.type = value; \
289 static struct units menu_units;
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)
300 #define OPTIONCALLBACK(name, option) \
301 static void name(GtkWidget *w, gpointer data) \
303 option = GTK_TOGGLE_BUTTON(w)->active; \
306 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
307 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
308 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
309 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
310 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
312 static void event_toggle(GtkWidget *w, gpointer _data)
314 gboolean *plot_ev = _data;
316 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
319 static void preferences_dialog(GtkWidget *w, gpointer data)
322 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
324 menu_units = output_units;
326 dialog = gtk_dialog_new_with_buttons("Preferences",
327 GTK_WINDOW(main_window),
328 GTK_DIALOG_DESTROY_WITH_PARENT,
329 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
330 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
333 frame = gtk_frame_new("Units");
334 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
335 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
337 box = gtk_vbox_new(FALSE, 6);
338 gtk_container_add(GTK_CONTAINER(frame), box);
340 create_radio(box, "Depth:",
341 "Meter", set_meter, (output_units.length == METERS),
342 "Feet", set_feet, (output_units.length == FEET),
345 create_radio(box, "Pressure:",
346 "Bar", set_bar, (output_units.pressure == BAR),
347 "PSI", set_psi, (output_units.pressure == PSI),
350 create_radio(box, "Volume:",
351 "Liter", set_liter, (output_units.volume == LITER),
352 "CuFt", set_cuft, (output_units.volume == CUFT),
355 create_radio(box, "Temperature:",
356 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
357 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
360 frame = gtk_frame_new("Columns");
361 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
363 box = gtk_hbox_new(FALSE, 6);
364 gtk_container_add(GTK_CONTAINER(frame), box);
366 button = gtk_check_button_new_with_label("Show Temp");
367 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
368 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
369 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
371 button = gtk_check_button_new_with_label("Show Cyl");
372 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
373 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
374 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
376 button = gtk_check_button_new_with_label("Show O" UTF8_SUBSCRIPT_2 "%");
377 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
378 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
379 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
381 button = gtk_check_button_new_with_label("Show SAC");
382 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
383 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
384 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
386 button = gtk_check_button_new_with_label("Show OTU");
387 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
388 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
389 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
391 font = gtk_font_button_new_with_font(divelist_font);
392 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
394 gtk_widget_show_all(dialog);
395 result = gtk_dialog_run(GTK_DIALOG(dialog));
396 if (result == GTK_RESPONSE_ACCEPT) {
397 /* Make sure to flush any modified old dive data with old units */
400 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
401 set_divelist_font(divelist_font);
403 output_units = menu_units;
404 update_dive_list_units();
406 update_dive_list_col_visibility();
408 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
409 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
410 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
411 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
412 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
413 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
414 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
415 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
416 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
417 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
418 subsurface_close_conf();
420 gtk_widget_destroy(dialog);
423 static void create_toggle(const char* label, int *on, void *_data)
425 GtkWidget *button, *table = _data;
426 int rows, cols, x, y;
430 /* magic way to reset the number of toggle buttons
431 * that we have already added - call this before you
432 * create the dialog */
436 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
437 if (count > rows * cols) {
438 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
443 button = gtk_check_button_new_with_label(label);
444 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
445 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
446 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
450 static void selectevents_dialog(GtkWidget *w, gpointer data)
453 GtkWidget *dialog, *frame, *vbox, *table;
455 dialog = gtk_dialog_new_with_buttons("SelectEvents",
456 GTK_WINDOW(main_window),
457 GTK_DIALOG_DESTROY_WITH_PARENT,
458 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
459 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
461 /* initialize the function that fills the table */
462 create_toggle(NULL, NULL, NULL);
464 frame = gtk_frame_new("Enable / Disable Events");
465 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
466 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
468 table = gtk_table_new(1, 4, TRUE);
469 gtk_container_add(GTK_CONTAINER(frame), table);
471 evn_foreach(&create_toggle, table);
473 gtk_widget_show_all(dialog);
474 result = gtk_dialog_run(GTK_DIALOG(dialog));
475 if (result == GTK_RESPONSE_ACCEPT) {
478 gtk_widget_destroy(dialog);
481 static void renumber_dialog(GtkWidget *w, gpointer data)
485 GtkWidget *dialog, *frame, *button, *vbox;
487 dialog = gtk_dialog_new_with_buttons("Renumber",
488 GTK_WINDOW(main_window),
489 GTK_DIALOG_DESTROY_WITH_PARENT,
490 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
491 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
494 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
496 frame = gtk_frame_new("New starting number");
497 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
499 button = gtk_spin_button_new_with_range(1, 50000, 1);
500 gtk_container_add(GTK_CONTAINER(frame), button);
503 * Do we have a number for the first dive already? Use that
507 if (dive && dive->number)
508 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
510 gtk_widget_show_all(dialog);
511 result = gtk_dialog_run(GTK_DIALOG(dialog));
512 if (result == GTK_RESPONSE_ACCEPT) {
513 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
517 gtk_widget_destroy(dialog);
520 static void about_dialog(GtkWidget *w, gpointer data)
522 const char *logo_property = NULL;
523 GdkPixbuf *logo = NULL;
526 GtkWidget *image = gtk_image_new_from_file(subsurface_icon_name());
529 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
530 logo_property = "logo";
534 gtk_show_about_dialog(NULL,
535 "program-name", "SubSurface",
536 "comments", "Half-arsed divelog software in C",
538 "version", VERSION_STRING,
539 "copyright", "Linus Torvalds 2011",
540 "logo-icon-name", "subsurface",
546 static void view_list(GtkWidget *w, gpointer data)
548 gtk_paned_set_position(GTK_PANED(vpane), 0);
551 static void view_profile(GtkWidget *w, gpointer data)
553 gtk_paned_set_position(GTK_PANED(hpane), 0);
554 gtk_paned_set_position(GTK_PANED(vpane), 65535);
557 static void view_info(GtkWidget *w, gpointer data)
559 gtk_paned_set_position(GTK_PANED(vpane), 65535);
560 gtk_paned_set_position(GTK_PANED(hpane), 65535);
563 /* Ooh. I don't know how to get the half-way size. So I'm just using random numbers */
564 static void view_three(GtkWidget *w, gpointer data)
566 gtk_paned_set_position(GTK_PANED(hpane), 400);
567 gtk_paned_set_position(GTK_PANED(vpane), 200);
570 static GtkActionEntry menu_items[] = {
571 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
572 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
573 { "ViewMenuAction", GTK_STOCK_FILE, "View", NULL, NULL, NULL},
574 { "FilterMenuAction", GTK_STOCK_FILE, "Filter", NULL, NULL, NULL},
575 { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
576 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
577 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
578 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
579 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
580 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
581 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
582 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
583 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
584 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
585 { "ViewList", NULL, "List", "<control>1", NULL, G_CALLBACK(view_list) },
586 { "ViewProfile", NULL, "Profile", "<control>2", NULL, G_CALLBACK(view_profile) },
587 { "ViewInfo", NULL, "Info", "<control>3", NULL, G_CALLBACK(view_info) },
588 { "ViewThree", NULL, "Three", "<control>4", NULL, G_CALLBACK(view_three) },
590 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
592 static const gchar* ui_string = " \
594 <menubar name=\"MainMenu\"> \
595 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
596 <menuitem name=\"Open\" action=\"OpenFile\" /> \
597 <menuitem name=\"Save\" action=\"SaveFile\" /> \
598 <menuitem name=\"Print\" action=\"Print\" /> \
599 <separator name=\"Separator1\"/> \
600 <menuitem name=\"Import\" action=\"Import\" /> \
601 <separator name=\"Separator2\"/> \
602 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
603 <separator name=\"Separator3\"/> \
604 <menuitem name=\"Quit\" action=\"Quit\" /> \
606 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
607 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
608 <menu name=\"View\" action=\"ViewMenuAction\"> \
609 <menuitem name=\"List\" action=\"ViewList\" /> \
610 <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
611 <menuitem name=\"Info\" action=\"ViewInfo\" /> \
612 <menuitem name=\"Paned\" action=\"ViewThree\" /> \
615 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
616 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
618 <menu name=\"Help\" action=\"HelpMenuAction\"> \
619 <menuitem name=\"About\" action=\"About\" /> \
625 static GtkWidget *get_menubar_menu(GtkWidget *window)
627 GtkActionGroup *action_group = gtk_action_group_new("Menu");
628 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
630 GtkUIManager *ui_manager = gtk_ui_manager_new();
631 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
633 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
635 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
636 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
641 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
646 void init_ui(int *argcp, char ***argvp)
650 GtkWidget *dive_info;
651 GtkWidget *dive_list;
652 GtkWidget *equipment;
657 GtkIconTheme *icon_theme=NULL;
658 GtkSettings *settings;
660 gtk_init(argcp, argvp);
661 settings = gtk_settings_get_default();
662 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
666 subsurface_open_conf();
667 if (subsurface_get_conf("feet", PREF_BOOL))
668 output_units.length = FEET;
669 if (subsurface_get_conf("psi", PREF_BOOL))
670 output_units.pressure = PSI;
671 if (subsurface_get_conf("cuft", PREF_BOOL))
672 output_units.volume = CUFT;
673 if (subsurface_get_conf("fahrenheit", PREF_BOOL))
674 output_units.temperature = FAHRENHEIT;
675 /* an unset key is FALSE - all these are hidden by default */
676 visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
677 visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
678 visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
679 visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
680 visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
682 divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
684 error_info_bar = NULL;
685 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
686 g_set_application_name ("subsurface");
687 /* Let's check if the subsurface icon has been installed or if
688 * we need to try to load it from the current directory */
689 screen = gdk_screen_get_default();
691 icon_theme = gtk_icon_theme_get_for_screen(screen);
693 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
695 gtk_window_set_default_icon_name ("subsurface");
699 const char *icon_name = subsurface_icon_name();
700 if (!access(icon_name, R_OK))
701 gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
703 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
704 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
707 vbox = gtk_vbox_new(FALSE, 0);
708 gtk_container_add(GTK_CONTAINER(win), vbox);
711 menubar = get_menubar_menu(win);
713 subsurface_ui_setup(settings, menubar, vbox);
715 vpane = gtk_vpaned_new();
716 gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
718 hpane = gtk_hpaned_new();
719 gtk_paned_add1(GTK_PANED(vpane), hpane);
721 /* Notebook for dive info vs profile vs .. */
722 notebook = gtk_notebook_new();
723 gtk_paned_add1(GTK_PANED(hpane), notebook);
724 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
726 /* Create the actual divelist */
727 dive_list = dive_list_create();
728 gtk_widget_set_name(dive_list, "Dive List");
729 gtk_paned_add2(GTK_PANED(vpane), dive_list);
731 /* Frame for dive profile */
732 dive_profile = dive_profile_widget();
733 gtk_widget_set_name(dive_profile, "Dive Profile");
734 gtk_paned_add2(GTK_PANED(hpane), dive_profile);
736 /* Frame for extended dive info */
737 dive_info = extended_dive_info_widget();
738 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
740 /* Frame for dive equipment */
741 equipment = equipment_widget();
742 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
744 /* Frame for dive statistics */
745 stats = stats_widget();
746 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), stats, gtk_label_new("Info & Stats"));
748 gtk_widget_set_app_paintable(win, TRUE);
749 gtk_widget_show_all(win);
760 cairo_rectangle_int_t rect;
764 static tooltip_record_t *tooltip_rects;
767 void attach_tooltip(int x, int y, int w, int h, const char *text)
769 cairo_rectangle_int_t *rect;
770 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
771 rect = &tooltip_rects[tooltips].rect;
776 tooltip_rects[tooltips].text = text;
780 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
781 (_r.y <= _y) && (_r.y + _r.height >= _y))
783 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
784 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
787 cairo_rectangle_int_t *drawing_area = user_data;
788 gint tx = x - drawing_area->x; /* get transformed coordinates */
789 gint ty = y - drawing_area->y;
791 /* are we over an event marker ? */
792 for (i = 0; i < tooltips; i++) {
793 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
794 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
795 return TRUE; /* show tooltip */
798 return FALSE; /* don't show tooltip */
801 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
803 struct dive *dive = current_dive;
804 struct graphics_context gc = { .printer = 0 };
805 static cairo_rectangle_int_t drawing_area;
807 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
808 * so effective drawing area is width-2x * height-2y */
809 drawing_area.width = widget->allocation.width;
810 drawing_area.height = widget->allocation.height;
811 drawing_area.x = drawing_area.width / 20.0;
812 drawing_area.y = drawing_area.height / 20.0;
814 gc.cr = gdk_cairo_create(widget->window);
815 g_object_set(widget, "has-tooltip", TRUE, NULL);
816 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
817 init_profile_background(&gc);
823 tooltip_rects = NULL;
826 plot(&gc, &drawing_area, dive);
828 cairo_destroy(gc.cr);
833 GtkWidget *dive_profile_widget(void)
837 da = gtk_drawing_area_new();
838 gtk_widget_set_size_request(da, 350, 250);
839 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
844 int process_ui_events(void)
848 while (gtk_events_pending()) {
849 if (gtk_main_iteration_do(0)) {
858 static void fill_computer_list(GtkListStore *store)
861 struct device_list *list = device_list;
863 for (list = device_list ; list->name ; list++) {
864 gtk_list_store_append(store, &iter);
865 gtk_list_store_set(store, &iter,
872 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
874 GtkWidget *hbox, *combo_box, *frame;
876 GtkCellRenderer *renderer;
878 hbox = gtk_hbox_new(FALSE, 6);
879 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
881 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
882 fill_computer_list(model);
884 frame = gtk_frame_new("Dive computer");
885 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
887 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
888 gtk_container_add(GTK_CONTAINER(frame), combo_box);
890 renderer = gtk_cell_renderer_text_new();
891 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
892 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
894 return GTK_COMBO_BOX(combo_box);
897 static GtkEntry *dive_computer_device(GtkWidget *vbox)
899 GtkWidget *hbox, *entry, *frame;
901 hbox = gtk_hbox_new(FALSE, 6);
902 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
904 frame = gtk_frame_new("Device name");
905 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
907 entry = gtk_entry_new();
908 gtk_container_add(GTK_CONTAINER(frame), entry);
909 gtk_entry_set_text(GTK_ENTRY(entry), subsurface_USB_name());
911 return GTK_ENTRY(entry);
914 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
915 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
917 GtkDialog *main_dialog = _data;
919 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
922 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
924 GtkWidget *hbox, *frame, *chooser, *dialog;
925 GtkFileFilter *filter;
927 hbox = gtk_hbox_new(FALSE, 6);
928 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
930 frame = gtk_frame_new("XML file name");
931 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
932 dialog = gtk_file_chooser_dialog_new("Open XML File",
933 GTK_WINDOW(main_window),
934 GTK_FILE_CHOOSER_ACTION_OPEN,
935 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
936 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
938 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
940 filter = gtk_file_filter_new();
941 gtk_file_filter_add_pattern(filter, "*.xml");
942 gtk_file_filter_add_pattern(filter, "*.XML");
943 gtk_file_filter_add_pattern(filter, "*.sda");
944 gtk_file_filter_add_pattern(filter, "*.SDA");
945 gtk_file_filter_add_mime_type(filter, "text/xml");
946 gtk_file_filter_set_name(filter, "XML file");
947 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
949 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
950 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
952 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
953 gtk_container_add(GTK_CONTAINER(frame), chooser);
958 static void do_import_file(gpointer data, gpointer user_data)
960 GError *error = NULL;
961 parse_xml_file(data, &error);
971 void import_dialog(GtkWidget *w, gpointer data)
974 GtkWidget *dialog, *hbox, *vbox, *label;
975 GtkComboBox *computer;
977 GtkWidget *XMLchooser;
978 device_data_t devicedata = {
982 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
983 GTK_WINDOW(main_window),
984 GTK_DIALOG_DESTROY_WITH_PARENT,
985 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
986 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
989 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
990 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
991 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
992 XMLchooser = xml_file_selector(vbox, dialog);
993 computer = dive_computer_selector(vbox);
994 device = dive_computer_device(vbox);
995 hbox = gtk_hbox_new(FALSE, 6);
996 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
997 devicedata.progress.bar = gtk_progress_bar_new();
998 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1000 gtk_widget_show_all(dialog);
1001 result = gtk_dialog_run(GTK_DIALOG(dialog));
1005 GtkTreeModel *model;
1008 case GTK_RESPONSE_ACCEPT:
1009 /* what happened - did the user pick a file? In that case
1010 * we ignore whether a dive computer model was picked */
1011 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1012 if (g_slist_length(list) == 0) {
1013 if (!gtk_combo_box_get_active_iter(computer, &iter))
1015 model = gtk_combo_box_get_model(computer);
1016 gtk_tree_model_get(model, &iter,
1020 devicedata.type = type;
1021 devicedata.name = comp;
1022 devicedata.devname = gtk_entry_get_text(device);
1023 do_import(&devicedata);
1025 g_slist_foreach(list,do_import_file,NULL);
1032 gtk_widget_destroy(dialog);
1037 void update_progressbar(progressbar_t *progress, double value)
1039 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1043 void set_filename(const char *filename)
1045 if (!existing_filename && filename)
1046 existing_filename = strdup(filename);