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;
28 const char *divelist_font;
30 struct units output_units;
32 static GtkWidget *dive_profile;
34 visible_cols_t visible_cols = {TRUE, FALSE};
36 static const char *default_dive_computer_vendor;
37 static const char *default_dive_computer_product;
38 static const char *default_dive_computer_device;
40 static int is_default_dive_computer(const char *vendor, const char *product)
42 return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) &&
43 default_dive_computer_product && !strcmp(product, default_dive_computer_product);
46 static int is_default_dive_computer_device(const char *name)
48 return default_dive_computer_device && !strcmp(name, default_dive_computer_device);
51 static void set_default_dive_computer(const char *vendor, const char *product)
53 if (!vendor || !*vendor)
55 if (!product || !*product)
57 if (is_default_dive_computer(vendor, product))
59 default_dive_computer_vendor = vendor;
60 default_dive_computer_product = product;
61 subsurface_set_conf("dive_computer_vendor", PREF_STRING, vendor);
62 subsurface_set_conf("dive_computer_product", PREF_STRING, product);
65 static void set_default_dive_computer_device(const char *name)
69 if (is_default_dive_computer_device(name))
71 default_dive_computer_device = name;
72 subsurface_set_conf("dive_computer_device", PREF_STRING, name);
75 void repaint_dive(void)
77 update_dive(current_dive);
79 gtk_widget_queue_draw(dive_profile);
82 static char *existing_filename;
83 static gboolean need_icon = TRUE;
85 static void on_info_bar_response(GtkWidget *widget, gint response,
88 if (response == GTK_RESPONSE_OK)
90 gtk_widget_destroy(widget);
91 error_info_bar = NULL;
95 void report_error(GError* error)
102 if (error_info_bar == NULL)
105 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
108 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
109 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
112 error_label = gtk_label_new(error->message);
113 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
114 gtk_container_add(GTK_CONTAINER(container), error_label);
116 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
117 gtk_widget_show_all(main_vbox);
123 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
124 gtk_label_set(GTK_LABEL(error_label), buffer);
128 static void file_open(GtkWidget *w, gpointer data)
131 GtkFileFilter *filter;
133 dialog = gtk_file_chooser_dialog_new("Open File",
134 GTK_WINDOW(main_window),
135 GTK_FILE_CHOOSER_ACTION_OPEN,
136 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
137 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
139 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
141 filter = gtk_file_filter_new();
142 gtk_file_filter_add_pattern(filter, "*.xml");
143 gtk_file_filter_add_pattern(filter, "*.XML");
144 gtk_file_filter_add_pattern(filter, "*.sda");
145 gtk_file_filter_add_pattern(filter, "*.SDA");
146 gtk_file_filter_add_mime_type(filter, "text/xml");
147 gtk_file_filter_set_name(filter, "XML file");
148 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
150 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
151 GSList *filenames, *fn_glist;
153 filenames = fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
155 GError *error = NULL;
156 while(filenames != NULL) {
157 filename = filenames->data;
158 parse_file(filename, &error);
167 filenames = g_slist_next(filenames);
169 g_slist_free(fn_glist);
172 gtk_widget_destroy(dialog);
175 /* return the path and the file component contained in the full path */
176 static char *path_and_file(char *pathin, char **fileout) {
177 char *slash = pathin, *next;
182 *fileout = strdup("");
185 while ((next = strpbrk(slash + 1, "\\/")))
189 *fileout = strdup(slash);
191 /* strndup(pathin, slash - pathin) */
193 len = strlen(pathin);
197 result = (char *)malloc(len + 1);
202 return (char *)memcpy(result, pathin, len);
205 static void file_save_as(GtkWidget *w, gpointer data)
208 char *filename = NULL;
212 dialog = gtk_file_chooser_dialog_new("Save File As",
213 GTK_WINDOW(main_window),
214 GTK_FILE_CHOOSER_ACTION_SAVE,
215 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
216 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
218 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
220 current_dir = path_and_file(existing_filename, ¤t_file);
221 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), current_dir);
222 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), current_file);
226 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
227 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
229 gtk_widget_destroy(dialog);
232 save_dives(filename);
233 set_filename(filename);
235 mark_divelist_changed(FALSE);
239 static void file_save(GtkWidget *w, gpointer data)
241 if (!existing_filename)
242 return file_save_as(w, data);
244 save_dives(existing_filename);
245 mark_divelist_changed(FALSE);
248 static gboolean ask_save_changes()
250 GtkWidget *dialog, *label, *content;
251 gboolean quit = TRUE;
252 dialog = gtk_dialog_new_with_buttons("Save Changes?",
253 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
254 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
255 GTK_STOCK_NO, GTK_RESPONSE_NO,
256 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
258 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
260 if (!existing_filename){
261 label = gtk_label_new (
262 "You have unsaved changes\nWould you like to save those before exiting the program?");
264 char *label_text = (char*) malloc(sizeof(char) * (92 + strlen(existing_filename)));
266 "You have unsaved changes to file: %s \nWould you like to save those before exiting the program?",
268 label = gtk_label_new (label_text);
271 gtk_container_add (GTK_CONTAINER (content), label);
272 gtk_widget_show_all (dialog);
273 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
274 gint outcode = gtk_dialog_run(GTK_DIALOG(dialog));
275 if (outcode == GTK_RESPONSE_ACCEPT) {
276 file_save(NULL,NULL);
277 } else if (outcode == GTK_RESPONSE_CANCEL) {
280 gtk_widget_destroy(dialog);
284 static gboolean on_delete(GtkWidget* w, gpointer data)
286 /* Make sure to flush any modified dive data */
289 gboolean quit = TRUE;
290 if (unsaved_changes())
291 quit = ask_save_changes();
294 return FALSE; /* go ahead, kill the program, we're good now */
296 return TRUE; /* We are not leaving */
300 static void on_destroy(GtkWidget* w, gpointer data)
305 static void quit(GtkWidget *w, gpointer data)
307 /* Make sure to flush any modified dive data */
310 gboolean quit = TRUE;
311 if (unsaved_changes())
312 quit = ask_save_changes();
319 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
320 data_func_t data_func, unsigned int flags)
322 GtkCellRenderer *renderer;
323 GtkTreeViewColumn *col;
324 double xalign = 0.0; /* left as default */
325 PangoAlignment align;
328 align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
329 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
331 visible = !(flags & INVISIBLE);
333 renderer = gtk_cell_renderer_text_new();
334 col = gtk_tree_view_column_new();
336 gtk_tree_view_column_set_title(col, title);
337 if (!(flags & UNSORTABLE))
338 gtk_tree_view_column_set_sort_column_id(col, index);
339 gtk_tree_view_column_set_resizable(col, TRUE);
340 gtk_tree_view_column_pack_start(col, renderer, TRUE);
342 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
344 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
345 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
347 case PANGO_ALIGN_LEFT:
350 case PANGO_ALIGN_CENTER:
353 case PANGO_ALIGN_RIGHT:
357 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
358 gtk_tree_view_column_set_visible(col, visible);
359 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
363 static void create_radio(GtkWidget *vbox, const char *w_name, ...)
366 GtkRadioButton *group = NULL;
367 GtkWidget *box, *label;
369 box = gtk_hbox_new(TRUE, 10);
370 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
372 label = gtk_label_new(w_name);
373 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
375 va_start(args, w_name);
382 name = va_arg(args, char *);
385 callback_fn = va_arg(args, void *);
386 enabled = va_arg(args, int);
388 button = gtk_radio_button_new_with_label_from_widget(group, name);
389 group = GTK_RADIO_BUTTON(button);
390 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
391 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
392 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
397 #define UNITCALLBACK(name, type, value) \
398 static void name(GtkWidget *w, gpointer data) \
400 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
401 menu_units.type = value; \
404 static struct units menu_units;
406 UNITCALLBACK(set_meter, length, METERS)
407 UNITCALLBACK(set_feet, length, FEET)
408 UNITCALLBACK(set_bar, pressure, BAR)
409 UNITCALLBACK(set_psi, pressure, PSI)
410 UNITCALLBACK(set_liter, volume, LITER)
411 UNITCALLBACK(set_cuft, volume, CUFT)
412 UNITCALLBACK(set_celsius, temperature, CELSIUS)
413 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
414 UNITCALLBACK(set_kg, weight, KG)
415 UNITCALLBACK(set_lbs, weight, LBS)
417 #define OPTIONCALLBACK(name, option) \
418 static void name(GtkWidget *w, gpointer data) \
420 option = GTK_TOGGLE_BUTTON(w)->active; \
423 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
424 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
425 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
426 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
427 OPTIONCALLBACK(totalweight_toggle, visible_cols.totalweight)
428 OPTIONCALLBACK(suit_toggle, visible_cols.suit)
429 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
431 static void event_toggle(GtkWidget *w, gpointer _data)
433 gboolean *plot_ev = _data;
435 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
438 static void preferences_dialog(GtkWidget *w, gpointer data)
441 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
443 menu_units = output_units;
445 dialog = gtk_dialog_new_with_buttons("Preferences",
446 GTK_WINDOW(main_window),
447 GTK_DIALOG_DESTROY_WITH_PARENT,
448 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
449 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
452 frame = gtk_frame_new("Units");
453 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
454 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
456 box = gtk_vbox_new(FALSE, 6);
457 gtk_container_add(GTK_CONTAINER(frame), box);
459 create_radio(box, "Depth:",
460 "Meter", set_meter, (output_units.length == METERS),
461 "Feet", set_feet, (output_units.length == FEET),
464 create_radio(box, "Pressure:",
465 "Bar", set_bar, (output_units.pressure == BAR),
466 "PSI", set_psi, (output_units.pressure == PSI),
469 create_radio(box, "Volume:",
470 "Liter", set_liter, (output_units.volume == LITER),
471 "CuFt", set_cuft, (output_units.volume == CUFT),
474 create_radio(box, "Temperature:",
475 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
476 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
479 create_radio(box, "Weight:",
480 "kg", set_kg, (output_units.weight == KG),
481 "lbs", set_lbs, (output_units.weight == LBS),
484 frame = gtk_frame_new("Show Columns");
485 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
487 box = gtk_hbox_new(FALSE, 6);
488 gtk_container_add(GTK_CONTAINER(frame), box);
490 button = gtk_check_button_new_with_label("Temp");
491 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
492 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
493 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
495 button = gtk_check_button_new_with_label("Cyl");
496 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
497 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
498 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
500 button = gtk_check_button_new_with_label("O" UTF8_SUBSCRIPT_2 "%");
501 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
502 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
503 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
505 button = gtk_check_button_new_with_label("SAC");
506 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
507 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
508 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
510 button = gtk_check_button_new_with_label("OTU");
511 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
512 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
513 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
515 button = gtk_check_button_new_with_label("Weight");
516 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.totalweight);
517 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
518 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(totalweight_toggle), NULL);
520 button = gtk_check_button_new_with_label("Suit");
521 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.suit);
522 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
523 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(suit_toggle), NULL);
525 font = gtk_font_button_new_with_font(divelist_font);
526 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
528 gtk_widget_show_all(dialog);
529 result = gtk_dialog_run(GTK_DIALOG(dialog));
530 if (result == GTK_RESPONSE_ACCEPT) {
531 /* Make sure to flush any modified old dive data with old units */
534 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
535 set_divelist_font(divelist_font);
537 output_units = menu_units;
538 update_dive_list_units();
540 update_dive_list_col_visibility();
542 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
543 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
544 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
545 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
546 subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
547 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
548 subsurface_set_conf("TOTALWEIGHT", PREF_BOOL, BOOL_TO_PTR(visible_cols.totalweight));
549 subsurface_set_conf("SUIT", PREF_BOOL, BOOL_TO_PTR(visible_cols.suit));
550 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
551 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
552 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
553 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
554 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
556 /* Flush the changes out to the system */
557 subsurface_flush_conf();
559 gtk_widget_destroy(dialog);
562 static void create_toggle(const char* label, int *on, void *_data)
564 GtkWidget *button, *table = _data;
565 int rows, cols, x, y;
569 /* magic way to reset the number of toggle buttons
570 * that we have already added - call this before you
571 * create the dialog */
575 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
576 if (count > rows * cols) {
577 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
582 button = gtk_check_button_new_with_label(label);
583 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
584 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
585 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
589 static void selectevents_dialog(GtkWidget *w, gpointer data)
592 GtkWidget *dialog, *frame, *vbox, *table;
594 dialog = gtk_dialog_new_with_buttons("SelectEvents",
595 GTK_WINDOW(main_window),
596 GTK_DIALOG_DESTROY_WITH_PARENT,
597 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
598 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
600 /* initialize the function that fills the table */
601 create_toggle(NULL, NULL, NULL);
603 frame = gtk_frame_new("Enable / Disable Events");
604 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
605 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
607 table = gtk_table_new(1, 4, TRUE);
608 gtk_container_add(GTK_CONTAINER(frame), table);
610 evn_foreach(&create_toggle, table);
612 gtk_widget_show_all(dialog);
613 result = gtk_dialog_run(GTK_DIALOG(dialog));
614 if (result == GTK_RESPONSE_ACCEPT) {
617 gtk_widget_destroy(dialog);
620 static void renumber_dialog(GtkWidget *w, gpointer data)
624 GtkWidget *dialog, *frame, *button, *vbox;
626 dialog = gtk_dialog_new_with_buttons("Renumber",
627 GTK_WINDOW(main_window),
628 GTK_DIALOG_DESTROY_WITH_PARENT,
629 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
630 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
633 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
635 frame = gtk_frame_new("New starting number");
636 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
638 button = gtk_spin_button_new_with_range(1, 50000, 1);
639 gtk_container_add(GTK_CONTAINER(frame), button);
642 * Do we have a number for the first dive already? Use that
646 if (dive && dive->number)
647 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
649 gtk_widget_show_all(dialog);
650 result = gtk_dialog_run(GTK_DIALOG(dialog));
651 if (result == GTK_RESPONSE_ACCEPT) {
652 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
656 gtk_widget_destroy(dialog);
659 static void about_dialog(GtkWidget *w, gpointer data)
661 const char *logo_property = NULL;
662 GdkPixbuf *logo = NULL;
665 GtkWidget *image = gtk_image_new_from_file(subsurface_icon_name());
668 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
669 logo_property = "logo";
673 gtk_show_about_dialog(NULL,
674 "program-name", "SubSurface",
675 "comments", "Half-arsed divelog software in C",
677 "version", VERSION_STRING,
678 "copyright", "Linus Torvalds 2011",
679 "logo-icon-name", "subsurface",
685 static void view_list(GtkWidget *w, gpointer data)
687 gtk_paned_set_position(GTK_PANED(vpane), 0);
690 static void view_profile(GtkWidget *w, gpointer data)
692 gtk_paned_set_position(GTK_PANED(hpane), 0);
693 gtk_paned_set_position(GTK_PANED(vpane), 65535);
696 static void view_info(GtkWidget *w, gpointer data)
698 gtk_paned_set_position(GTK_PANED(vpane), 65535);
699 gtk_paned_set_position(GTK_PANED(hpane), 65535);
702 static void view_three(GtkWidget *w, gpointer data)
705 GtkRequisition requisition;
707 gtk_widget_get_allocation(hpane, &alloc);
708 gtk_paned_set_position(GTK_PANED(hpane), alloc.width/2);
709 gtk_widget_get_allocation(vpane, &alloc);
710 gtk_widget_size_request(notebook, &requisition);
711 /* pick the requested size for the notebook plus 6 pixels for frame */
712 gtk_paned_set_position(GTK_PANED(vpane), requisition.height + 6);
715 static GtkActionEntry menu_items[] = {
716 { "FileMenuAction", NULL, "File", NULL, NULL, NULL},
717 { "LogMenuAction", NULL, "Log", NULL, NULL, NULL},
718 { "ViewMenuAction", NULL, "View", NULL, NULL, NULL},
719 { "FilterMenuAction", NULL, "Filter", NULL, NULL, NULL},
720 { "HelpMenuAction", NULL, "Help", NULL, NULL, NULL},
721 { "OpenFile", GTK_STOCK_OPEN, NULL, CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
722 { "SaveFile", GTK_STOCK_SAVE, NULL, CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
723 { "SaveAsFile", GTK_STOCK_SAVE_AS, NULL, SHIFTCHAR CTRLCHAR "S", NULL, G_CALLBACK(file_save_as) },
724 { "Print", GTK_STOCK_PRINT, NULL, CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
725 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
726 { "AddDive", GTK_STOCK_ADD, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) },
727 { "Preferences", GTK_STOCK_PREFERENCES, "Preferences", PREFERENCE_ACCEL, NULL, G_CALLBACK(preferences_dialog) },
728 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
729 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
730 { "Quit", GTK_STOCK_QUIT, NULL, CTRLCHAR "Q", NULL, G_CALLBACK(quit) },
731 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
732 { "ViewList", NULL, "List", CTRLCHAR "1", NULL, G_CALLBACK(view_list) },
733 { "ViewProfile", NULL, "Profile", CTRLCHAR "2", NULL, G_CALLBACK(view_profile) },
734 { "ViewInfo", NULL, "Info", CTRLCHAR "3", NULL, G_CALLBACK(view_info) },
735 { "ViewThree", NULL, "Three", CTRLCHAR "4", NULL, G_CALLBACK(view_three) },
737 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
739 static const gchar* ui_string = " \
741 <menubar name=\"MainMenu\"> \
742 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
743 <menuitem name=\"Open\" action=\"OpenFile\" /> \
744 <menuitem name=\"Save\" action=\"SaveFile\" /> \
745 <menuitem name=\"Save As\" action=\"SaveAsFile\" /> \
746 <menuitem name=\"Print\" action=\"Print\" /> \
747 <separator name=\"Separator1\"/> \
748 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
749 <separator name=\"Separator2\"/> \
750 <menuitem name=\"Quit\" action=\"Quit\" /> \
752 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
753 <menuitem name=\"Import\" action=\"Import\" /> \
754 <menuitem name=\"Add Dive\" action=\"AddDive\" /> \
755 <separator name=\"Separator\"/> \
756 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
757 <menu name=\"View\" action=\"ViewMenuAction\"> \
758 <menuitem name=\"List\" action=\"ViewList\" /> \
759 <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
760 <menuitem name=\"Info\" action=\"ViewInfo\" /> \
761 <menuitem name=\"Paned\" action=\"ViewThree\" /> \
764 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
765 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
767 <menu name=\"Help\" action=\"HelpMenuAction\"> \
768 <menuitem name=\"About\" action=\"About\" /> \
774 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
776 GtkActionGroup *action_group = gtk_action_group_new("Menu");
777 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
779 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
781 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
783 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
784 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
789 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
794 void init_ui(int *argcp, char ***argvp)
798 GtkWidget *dive_list;
803 GtkIconTheme *icon_theme=NULL;
804 GtkSettings *settings;
805 GtkUIManager *ui_manager;
807 gtk_init(argcp, argvp);
808 settings = gtk_settings_get_default();
809 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
813 subsurface_open_conf();
814 if (subsurface_get_conf("feet", PREF_BOOL))
815 output_units.length = FEET;
816 if (subsurface_get_conf("psi", PREF_BOOL))
817 output_units.pressure = PSI;
818 if (subsurface_get_conf("cuft", PREF_BOOL))
819 output_units.volume = CUFT;
820 if (subsurface_get_conf("fahrenheit", PREF_BOOL))
821 output_units.temperature = FAHRENHEIT;
822 if (subsurface_get_conf("lbs", PREF_BOOL))
823 output_units.weight = LBS;
824 /* an unset key is FALSE - all these are hidden by default */
825 visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
826 visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
827 visible_cols.totalweight = PTR_TO_BOOL(subsurface_get_conf("TOTALWEIGHT", PREF_BOOL));
828 visible_cols.suit = PTR_TO_BOOL(subsurface_get_conf("SUIT", PREF_BOOL));
829 visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
830 visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
831 visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
833 divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
835 default_dive_computer_vendor = subsurface_get_conf("dive_computer_vendor", PREF_STRING);
836 default_dive_computer_product = subsurface_get_conf("dive_computer_product", PREF_STRING);
837 default_dive_computer_device = subsurface_get_conf("dive_computer_device", PREF_STRING);
839 error_info_bar = NULL;
840 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
841 g_set_application_name ("subsurface");
842 /* Let's check if the subsurface icon has been installed or if
843 * we need to try to load it from the current directory */
844 screen = gdk_screen_get_default();
846 icon_theme = gtk_icon_theme_get_for_screen(screen);
848 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
850 gtk_window_set_default_icon_name ("subsurface");
854 const char *icon_name = subsurface_icon_name();
855 if (!access(icon_name, R_OK))
856 gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
858 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
859 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
862 vbox = gtk_vbox_new(FALSE, 0);
863 gtk_container_add(GTK_CONTAINER(win), vbox);
866 ui_manager = gtk_ui_manager_new();
867 menubar = get_menubar_menu(win, ui_manager);
869 subsurface_ui_setup(settings, menubar, vbox, ui_manager);
871 vpane = gtk_vpaned_new();
872 gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
873 hpane = gtk_hpaned_new();
874 gtk_paned_add1(GTK_PANED(vpane), hpane);
875 g_signal_connect_after(G_OBJECT(vbox), "realize", G_CALLBACK(view_three), NULL);
877 /* Notebook for dive info vs profile vs .. */
878 notebook = gtk_notebook_new();
879 scrolled = gtk_scrolled_window_new(NULL, NULL);
880 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
881 gtk_paned_add1(GTK_PANED(hpane), scrolled);
882 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), notebook);
883 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
885 /* Create the actual divelist */
886 dive_list = dive_list_create();
887 gtk_widget_set_name(dive_list, "Dive List");
888 gtk_paned_add2(GTK_PANED(vpane), dive_list);
890 /* Frame for dive profile */
891 dive_profile = dive_profile_widget();
892 gtk_widget_set_name(dive_profile, "Dive Profile");
893 gtk_paned_add2(GTK_PANED(hpane), dive_profile);
895 /* Frame for extended dive info */
896 nb_page = extended_dive_info_widget();
897 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Notes"));
899 /* Frame for dive equipment */
900 nb_page = equipment_widget(W_IDX_PRIMARY);
901 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Equipment"));
903 /* Frame for single dive statistics */
904 nb_page = single_stats_widget();
905 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
907 /* Frame for total dive statistics */
908 nb_page = total_stats_widget();
909 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Stats"));
911 gtk_widget_set_app_paintable(win, TRUE);
912 gtk_widget_show_all(win);
924 subsurface_close_conf();
928 cairo_rectangle_int_t rect;
932 static tooltip_record_t *tooltip_rects;
935 void attach_tooltip(int x, int y, int w, int h, const char *text)
937 cairo_rectangle_int_t *rect;
938 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
939 rect = &tooltip_rects[tooltips].rect;
944 tooltip_rects[tooltips].text = text;
948 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
949 (_r.y <= _y) && (_r.y + _r.height >= _y))
951 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
952 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
955 cairo_rectangle_int_t *drawing_area = user_data;
956 gint tx = x - drawing_area->x; /* get transformed coordinates */
957 gint ty = y - drawing_area->y;
959 /* are we over an event marker ? */
960 for (i = 0; i < tooltips; i++) {
961 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
962 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
963 return TRUE; /* show tooltip */
966 return FALSE; /* don't show tooltip */
969 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
971 struct dive *dive = current_dive;
972 struct graphics_context gc = { .printer = 0 };
973 static cairo_rectangle_int_t drawing_area;
975 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
976 * so effective drawing area is width-2x * height-2y */
977 drawing_area.width = widget->allocation.width;
978 drawing_area.height = widget->allocation.height;
979 drawing_area.x = drawing_area.width / 20.0;
980 drawing_area.y = drawing_area.height / 20.0;
982 gc.cr = gdk_cairo_create(widget->window);
983 g_object_set(widget, "has-tooltip", TRUE, NULL);
984 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
985 init_profile_background(&gc);
991 tooltip_rects = NULL;
994 plot(&gc, &drawing_area, dive);
996 cairo_destroy(gc.cr);
1001 GtkWidget *dive_profile_widget(void)
1005 da = gtk_drawing_area_new();
1006 gtk_widget_set_size_request(da, 350, 250);
1007 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
1012 int process_ui_events(void)
1016 while (gtk_events_pending()) {
1017 if (gtk_main_iteration_do(0)) {
1025 static int fill_computer_list(GtkListStore *store)
1029 dc_iterator_t *iterator = NULL;
1030 dc_descriptor_t *descriptor = NULL;
1033 dc_descriptor_iterator(&iterator);
1034 while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) {
1035 const char *vendor = dc_descriptor_get_vendor(descriptor);
1036 const char *product = dc_descriptor_get_product(descriptor);
1038 gtk_list_store_append(store, &iter);
1039 gtk_list_store_set(store, &iter,
1042 if (is_default_dive_computer(vendor, product))
1046 dc_iterator_free(iterator);
1050 void render_dive_computer(GtkCellLayout *cell,
1051 GtkCellRenderer *renderer,
1052 GtkTreeModel *model,
1057 dc_descriptor_t *descriptor = NULL;
1058 const char *vendor, *product;
1060 gtk_tree_model_get(model, iter, 0, &descriptor, -1);
1061 vendor = dc_descriptor_get_vendor(descriptor);
1062 product = dc_descriptor_get_product(descriptor);
1063 snprintf(buffer, sizeof(buffer), "%s %s", vendor, product);
1064 g_object_set(renderer, "text", buffer, NULL);
1068 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
1070 GtkWidget *hbox, *combo_box, *frame;
1071 GtkListStore *model;
1072 GtkCellRenderer *renderer;
1075 hbox = gtk_hbox_new(FALSE, 6);
1076 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1078 model = gtk_list_store_new(1, G_TYPE_POINTER);
1079 default_index = fill_computer_list(model);
1081 frame = gtk_frame_new("Dive computer");
1082 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1084 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
1085 gtk_container_add(GTK_CONTAINER(frame), combo_box);
1087 renderer = gtk_cell_renderer_text_new();
1088 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
1089 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo_box), renderer, render_dive_computer, NULL, NULL);
1091 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
1093 return GTK_COMBO_BOX(combo_box);
1096 const char *subsurface_device_name()
1098 if (!default_dive_computer_device || !*default_dive_computer_device)
1099 return subsurface_USB_name();
1101 return default_dive_computer_device;
1104 static GtkEntry *dive_computer_device(GtkWidget *vbox)
1106 GtkWidget *hbox, *entry, *frame;
1108 hbox = gtk_hbox_new(FALSE, 6);
1109 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1111 frame = gtk_frame_new("Device name");
1112 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1114 entry = gtk_entry_new();
1115 gtk_container_add(GTK_CONTAINER(frame), entry);
1116 gtk_entry_set_text(GTK_ENTRY(entry), subsurface_device_name());
1118 return GTK_ENTRY(entry);
1121 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
1122 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
1124 GtkDialog *main_dialog = _data;
1126 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
1129 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
1131 GtkWidget *hbox, *frame, *chooser, *dialog;
1132 GtkFileFilter *filter;
1134 hbox = gtk_hbox_new(FALSE, 6);
1135 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1137 frame = gtk_frame_new("XML file name");
1138 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1139 dialog = gtk_file_chooser_dialog_new("Open XML File",
1140 GTK_WINDOW(main_window),
1141 GTK_FILE_CHOOSER_ACTION_OPEN,
1142 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1143 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1145 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
1147 filter = gtk_file_filter_new();
1148 gtk_file_filter_add_pattern(filter, "*.xml");
1149 gtk_file_filter_add_pattern(filter, "*.XML");
1150 gtk_file_filter_add_pattern(filter, "*.sda");
1151 gtk_file_filter_add_pattern(filter, "*.SDA");
1152 gtk_file_filter_add_mime_type(filter, "text/xml");
1153 gtk_file_filter_set_name(filter, "XML file");
1154 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
1156 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
1157 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
1159 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
1160 gtk_container_add(GTK_CONTAINER(frame), chooser);
1165 static void do_import_file(gpointer data, gpointer user_data)
1167 GError *error = NULL;
1168 parse_file(data, &error);
1172 report_error(error);
1173 g_error_free(error);
1178 static GtkWidget *import_dive_computer(device_data_t *data, GtkDialog *dialog)
1181 GtkWidget *vbox, *info, *container, *label, *button;
1183 error = do_import(data);
1187 button = gtk_dialog_get_widget_for_response(dialog, GTK_RESPONSE_ACCEPT);
1188 gtk_button_set_use_stock(GTK_BUTTON(button), 0);
1189 gtk_button_set_label(GTK_BUTTON(button), "Retry");
1191 vbox = gtk_dialog_get_content_area(dialog);
1193 info = gtk_info_bar_new();
1194 container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
1195 label = gtk_label_new(error->message);
1196 gtk_container_add(GTK_CONTAINER(container), label);
1197 gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
1201 void import_dialog(GtkWidget *w, gpointer data)
1204 GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
1205 GtkComboBox *computer;
1207 GtkWidget *XMLchooser;
1208 device_data_t devicedata = {
1212 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1213 GTK_WINDOW(main_window),
1214 GTK_DIALOG_DESTROY_WITH_PARENT,
1215 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1216 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1219 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1220 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1221 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1222 XMLchooser = xml_file_selector(vbox, dialog);
1223 computer = dive_computer_selector(vbox);
1224 device = dive_computer_device(vbox);
1225 hbox = gtk_hbox_new(FALSE, 6);
1226 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1227 devicedata.progress.bar = gtk_progress_bar_new();
1228 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1231 gtk_widget_show_all(dialog);
1232 result = gtk_dialog_run(GTK_DIALOG(dialog));
1234 dc_descriptor_t *descriptor;
1236 GtkTreeModel *model;
1238 case GTK_RESPONSE_ACCEPT:
1239 /* what happened - did the user pick a file? In that case
1240 * we ignore whether a dive computer model was picked */
1242 gtk_widget_destroy(info);
1243 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1244 if (g_slist_length(list) == 0) {
1245 const char *vendor, *product;
1247 if (!gtk_combo_box_get_active_iter(computer, &iter))
1249 model = gtk_combo_box_get_model(computer);
1250 gtk_tree_model_get(model, &iter,
1254 vendor = dc_descriptor_get_vendor(descriptor);
1255 product = dc_descriptor_get_product(descriptor);
1257 devicedata.descriptor = descriptor;
1258 devicedata.vendor = vendor;
1259 devicedata.product = product;
1260 devicedata.devname = gtk_entry_get_text(device);
1261 set_default_dive_computer(vendor, product);
1262 set_default_dive_computer_device(devicedata.devname);
1263 info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
1267 g_slist_foreach(list,do_import_file,NULL);
1274 gtk_widget_destroy(dialog);
1279 void update_progressbar(progressbar_t *progress, double value)
1281 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1284 void update_progressbar_text(progressbar_t *progress, const char *text)
1286 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), text);
1289 void set_filename(const char *filename)
1291 if (existing_filename)
1292 free(existing_filename);
1293 existing_filename = NULL;
1295 existing_filename = strdup(filename);