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 static void file_save_as(GtkWidget *w, gpointer data)
178 char *filename = NULL;
179 dialog = gtk_file_chooser_dialog_new("Save File As",
180 GTK_WINDOW(main_window),
181 GTK_FILE_CHOOSER_ACTION_SAVE,
182 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
183 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
185 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
187 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), existing_filename);
188 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
189 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
191 gtk_widget_destroy(dialog);
194 save_dives(filename);
195 set_filename(filename);
197 mark_divelist_changed(FALSE);
201 static void file_save(GtkWidget *w, gpointer data)
203 if (!existing_filename)
204 return file_save_as(w, data);
206 save_dives(existing_filename);
207 mark_divelist_changed(FALSE);
210 static gboolean ask_save_changes()
212 GtkWidget *dialog, *label, *content;
213 gboolean quit = TRUE;
214 dialog = gtk_dialog_new_with_buttons("Save Changes?",
215 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
216 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
217 GTK_STOCK_NO, GTK_RESPONSE_NO,
218 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
220 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
222 if (!existing_filename){
223 label = gtk_label_new (
224 "You have unsaved changes\nWould you like to save those before exiting the program?");
226 char *label_text = (char*) malloc(sizeof(char) * (92 + strlen(existing_filename)));
228 "You have unsaved changes to file: %s \nWould you like to save those before exiting the program?",
230 label = gtk_label_new (label_text);
233 gtk_container_add (GTK_CONTAINER (content), label);
234 gtk_widget_show_all (dialog);
235 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
236 gint outcode = gtk_dialog_run(GTK_DIALOG(dialog));
237 if (outcode == GTK_RESPONSE_ACCEPT) {
238 file_save(NULL,NULL);
239 } else if (outcode == GTK_RESPONSE_CANCEL) {
242 gtk_widget_destroy(dialog);
246 static gboolean on_delete(GtkWidget* w, gpointer data)
248 /* Make sure to flush any modified dive data */
251 gboolean quit = TRUE;
252 if (unsaved_changes())
253 quit = ask_save_changes();
256 return FALSE; /* go ahead, kill the program, we're good now */
258 return TRUE; /* We are not leaving */
262 static void on_destroy(GtkWidget* w, gpointer data)
267 static void quit(GtkWidget *w, gpointer data)
269 /* Make sure to flush any modified dive data */
272 gboolean quit = TRUE;
273 if (unsaved_changes())
274 quit = ask_save_changes();
281 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
282 data_func_t data_func, unsigned int flags)
284 GtkCellRenderer *renderer;
285 GtkTreeViewColumn *col;
286 double xalign = 0.0; /* left as default */
287 PangoAlignment align;
290 align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
291 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
293 visible = !(flags & INVISIBLE);
295 renderer = gtk_cell_renderer_text_new();
296 col = gtk_tree_view_column_new();
298 gtk_tree_view_column_set_title(col, title);
299 if (!(flags & UNSORTABLE))
300 gtk_tree_view_column_set_sort_column_id(col, index);
301 gtk_tree_view_column_set_resizable(col, TRUE);
302 gtk_tree_view_column_pack_start(col, renderer, TRUE);
304 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
306 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
307 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
309 case PANGO_ALIGN_LEFT:
312 case PANGO_ALIGN_CENTER:
315 case PANGO_ALIGN_RIGHT:
319 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
320 gtk_tree_view_column_set_visible(col, visible);
321 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
325 static void create_radio(GtkWidget *vbox, const char *w_name, ...)
328 GtkRadioButton *group = NULL;
329 GtkWidget *box, *label;
331 box = gtk_hbox_new(TRUE, 10);
332 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
334 label = gtk_label_new(w_name);
335 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
337 va_start(args, w_name);
344 name = va_arg(args, char *);
347 callback_fn = va_arg(args, void *);
348 enabled = va_arg(args, int);
350 button = gtk_radio_button_new_with_label_from_widget(group, name);
351 group = GTK_RADIO_BUTTON(button);
352 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
353 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
354 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
359 #define UNITCALLBACK(name, type, value) \
360 static void name(GtkWidget *w, gpointer data) \
362 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
363 menu_units.type = value; \
366 static struct units menu_units;
368 UNITCALLBACK(set_meter, length, METERS)
369 UNITCALLBACK(set_feet, length, FEET)
370 UNITCALLBACK(set_bar, pressure, BAR)
371 UNITCALLBACK(set_psi, pressure, PSI)
372 UNITCALLBACK(set_liter, volume, LITER)
373 UNITCALLBACK(set_cuft, volume, CUFT)
374 UNITCALLBACK(set_celsius, temperature, CELSIUS)
375 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
376 UNITCALLBACK(set_kg, weight, KG)
377 UNITCALLBACK(set_lbs, weight, LBS)
379 #define OPTIONCALLBACK(name, option) \
380 static void name(GtkWidget *w, gpointer data) \
382 option = GTK_TOGGLE_BUTTON(w)->active; \
385 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
386 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
387 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
388 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
389 OPTIONCALLBACK(totalweight_toggle, visible_cols.totalweight)
390 OPTIONCALLBACK(suit_toggle, visible_cols.suit)
391 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
392 OPTIONCALLBACK(autogroup_toggle, autogroup)
394 static void event_toggle(GtkWidget *w, gpointer _data)
396 gboolean *plot_ev = _data;
398 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
401 static void preferences_dialog(GtkWidget *w, gpointer data)
404 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
406 menu_units = output_units;
408 dialog = gtk_dialog_new_with_buttons("Preferences",
409 GTK_WINDOW(main_window),
410 GTK_DIALOG_DESTROY_WITH_PARENT,
411 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
412 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
415 frame = gtk_frame_new("Units");
416 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
417 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
419 box = gtk_vbox_new(FALSE, 6);
420 gtk_container_add(GTK_CONTAINER(frame), box);
422 create_radio(box, "Depth:",
423 "Meter", set_meter, (output_units.length == METERS),
424 "Feet", set_feet, (output_units.length == FEET),
427 create_radio(box, "Pressure:",
428 "Bar", set_bar, (output_units.pressure == BAR),
429 "PSI", set_psi, (output_units.pressure == PSI),
432 create_radio(box, "Volume:",
433 "Liter", set_liter, (output_units.volume == LITER),
434 "CuFt", set_cuft, (output_units.volume == CUFT),
437 create_radio(box, "Temperature:",
438 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
439 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
442 create_radio(box, "Weight:",
443 "kg", set_kg, (output_units.weight == KG),
444 "lbs", set_lbs, (output_units.weight == LBS),
447 frame = gtk_frame_new("Show Columns");
448 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
450 box = gtk_hbox_new(FALSE, 6);
451 gtk_container_add(GTK_CONTAINER(frame), box);
453 button = gtk_check_button_new_with_label("Temp");
454 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
455 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
456 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
458 button = gtk_check_button_new_with_label("Cyl");
459 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
460 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
461 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
463 button = gtk_check_button_new_with_label("O" UTF8_SUBSCRIPT_2 "%");
464 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
465 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
466 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
468 button = gtk_check_button_new_with_label("SAC");
469 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
470 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
471 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
473 button = gtk_check_button_new_with_label("OTU");
474 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
475 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
476 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
478 button = gtk_check_button_new_with_label("Weight");
479 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.totalweight);
480 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
481 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(totalweight_toggle), NULL);
483 button = gtk_check_button_new_with_label("Suit");
484 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.suit);
485 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
486 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(suit_toggle), NULL);
488 frame = gtk_frame_new("Divelist Font");
489 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
491 font = gtk_font_button_new_with_font(divelist_font);
492 gtk_container_add(GTK_CONTAINER(frame),font);
494 frame = gtk_frame_new("Misc. Options");
495 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
497 box = gtk_hbox_new(FALSE, 6);
498 gtk_container_add(GTK_CONTAINER(frame), box);
500 button = gtk_check_button_new_with_label("Automatically group dives in trips");
501 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), autogroup);
502 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
503 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(autogroup_toggle), NULL);
505 gtk_widget_show_all(dialog);
506 result = gtk_dialog_run(GTK_DIALOG(dialog));
507 if (result == GTK_RESPONSE_ACCEPT) {
508 /* Make sure to flush any modified old dive data with old units */
511 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
512 set_divelist_font(divelist_font);
514 output_units = menu_units;
515 update_dive_list_units();
517 update_dive_list_col_visibility();
519 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
520 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
521 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
522 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
523 subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
524 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
525 subsurface_set_conf("TOTALWEIGHT", PREF_BOOL, BOOL_TO_PTR(visible_cols.totalweight));
526 subsurface_set_conf("SUIT", PREF_BOOL, BOOL_TO_PTR(visible_cols.suit));
527 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
528 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
529 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
530 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
531 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
532 subsurface_set_conf("autogroup", PREF_BOOL, BOOL_TO_PTR(autogroup));
534 /* Flush the changes out to the system */
535 subsurface_flush_conf();
537 gtk_widget_destroy(dialog);
540 static void create_toggle(const char* label, int *on, void *_data)
542 GtkWidget *button, *table = _data;
543 int rows, cols, x, y;
547 /* magic way to reset the number of toggle buttons
548 * that we have already added - call this before you
549 * create the dialog */
553 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
554 if (count > rows * cols) {
555 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
560 button = gtk_check_button_new_with_label(label);
561 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
562 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
563 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
567 static void selectevents_dialog(GtkWidget *w, gpointer data)
570 GtkWidget *dialog, *frame, *vbox, *table;
572 dialog = gtk_dialog_new_with_buttons("SelectEvents",
573 GTK_WINDOW(main_window),
574 GTK_DIALOG_DESTROY_WITH_PARENT,
575 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
576 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
578 /* initialize the function that fills the table */
579 create_toggle(NULL, NULL, NULL);
581 frame = gtk_frame_new("Enable / Disable Events");
582 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
583 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
585 table = gtk_table_new(1, 4, TRUE);
586 gtk_container_add(GTK_CONTAINER(frame), table);
588 evn_foreach(&create_toggle, table);
590 gtk_widget_show_all(dialog);
591 result = gtk_dialog_run(GTK_DIALOG(dialog));
592 if (result == GTK_RESPONSE_ACCEPT) {
595 gtk_widget_destroy(dialog);
598 static void renumber_dialog(GtkWidget *w, gpointer data)
602 GtkWidget *dialog, *frame, *button, *vbox;
604 dialog = gtk_dialog_new_with_buttons("Renumber",
605 GTK_WINDOW(main_window),
606 GTK_DIALOG_DESTROY_WITH_PARENT,
607 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
608 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
611 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
613 frame = gtk_frame_new("New starting number");
614 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
616 button = gtk_spin_button_new_with_range(1, 50000, 1);
617 gtk_container_add(GTK_CONTAINER(frame), button);
620 * Do we have a number for the first dive already? Use that
624 if (dive && dive->number)
625 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
627 gtk_widget_show_all(dialog);
628 result = gtk_dialog_run(GTK_DIALOG(dialog));
629 if (result == GTK_RESPONSE_ACCEPT) {
630 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
634 gtk_widget_destroy(dialog);
637 static void about_dialog(GtkWidget *w, gpointer data)
639 const char *logo_property = NULL;
640 GdkPixbuf *logo = NULL;
643 GtkWidget *image = gtk_image_new_from_file(subsurface_icon_name());
646 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
647 logo_property = "logo";
651 gtk_show_about_dialog(NULL,
652 "program-name", "SubSurface",
653 "comments", "Half-arsed divelog software in C",
655 "version", VERSION_STRING,
656 "copyright", "Linus Torvalds 2011",
657 "logo-icon-name", "subsurface",
663 static void view_list(GtkWidget *w, gpointer data)
665 gtk_paned_set_position(GTK_PANED(vpane), 0);
668 static void view_profile(GtkWidget *w, gpointer data)
670 gtk_paned_set_position(GTK_PANED(hpane), 0);
671 gtk_paned_set_position(GTK_PANED(vpane), 65535);
674 static void view_info(GtkWidget *w, gpointer data)
676 gtk_paned_set_position(GTK_PANED(vpane), 65535);
677 gtk_paned_set_position(GTK_PANED(hpane), 65535);
680 static void view_three(GtkWidget *w, gpointer data)
683 GtkRequisition requisition;
685 gtk_widget_get_allocation(hpane, &alloc);
686 gtk_paned_set_position(GTK_PANED(hpane), alloc.width/2);
687 gtk_widget_get_allocation(vpane, &alloc);
688 gtk_widget_size_request(notebook, &requisition);
689 /* pick the requested size for the notebook plus 6 pixels for frame */
690 gtk_paned_set_position(GTK_PANED(vpane), requisition.height + 6);
693 static GtkActionEntry menu_items[] = {
694 { "FileMenuAction", NULL, "File", NULL, NULL, NULL},
695 { "LogMenuAction", NULL, "Log", NULL, NULL, NULL},
696 { "ViewMenuAction", NULL, "View", NULL, NULL, NULL},
697 { "FilterMenuAction", NULL, "Filter", NULL, NULL, NULL},
698 { "HelpMenuAction", NULL, "Help", NULL, NULL, NULL},
699 { "OpenFile", GTK_STOCK_OPEN, NULL, CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
700 { "SaveFile", GTK_STOCK_SAVE, NULL, CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
701 { "SaveAsFile", GTK_STOCK_SAVE_AS, NULL, SHIFTCHAR CTRLCHAR "S", NULL, G_CALLBACK(file_save_as) },
702 { "Print", GTK_STOCK_PRINT, NULL, CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
703 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
704 { "AddDive", GTK_STOCK_ADD, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) },
705 { "Preferences", GTK_STOCK_PREFERENCES, "Preferences", PREFERENCE_ACCEL, NULL, G_CALLBACK(preferences_dialog) },
706 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
707 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
708 { "Quit", GTK_STOCK_QUIT, NULL, CTRLCHAR "Q", NULL, G_CALLBACK(quit) },
709 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
710 { "ViewList", NULL, "List", CTRLCHAR "1", NULL, G_CALLBACK(view_list) },
711 { "ViewProfile", NULL, "Profile", CTRLCHAR "2", NULL, G_CALLBACK(view_profile) },
712 { "ViewInfo", NULL, "Info", CTRLCHAR "3", NULL, G_CALLBACK(view_info) },
713 { "ViewThree", NULL, "Three", CTRLCHAR "4", NULL, G_CALLBACK(view_three) },
715 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
717 static const gchar* ui_string = " \
719 <menubar name=\"MainMenu\"> \
720 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
721 <menuitem name=\"Open\" action=\"OpenFile\" /> \
722 <menuitem name=\"Save\" action=\"SaveFile\" /> \
723 <menuitem name=\"Save As\" action=\"SaveAsFile\" /> \
724 <menuitem name=\"Print\" action=\"Print\" /> \
725 <separator name=\"Separator1\"/> \
726 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
727 <separator name=\"Separator2\"/> \
728 <menuitem name=\"Quit\" action=\"Quit\" /> \
730 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
731 <menuitem name=\"Import\" action=\"Import\" /> \
732 <menuitem name=\"Add Dive\" action=\"AddDive\" /> \
733 <separator name=\"Separator\"/> \
734 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
735 <menu name=\"View\" action=\"ViewMenuAction\"> \
736 <menuitem name=\"List\" action=\"ViewList\" /> \
737 <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
738 <menuitem name=\"Info\" action=\"ViewInfo\" /> \
739 <menuitem name=\"Paned\" action=\"ViewThree\" /> \
742 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
743 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
745 <menu name=\"Help\" action=\"HelpMenuAction\"> \
746 <menuitem name=\"About\" action=\"About\" /> \
752 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
754 GtkActionGroup *action_group = gtk_action_group_new("Menu");
755 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
757 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
759 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
761 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
762 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
767 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
772 void init_ui(int *argcp, char ***argvp)
776 GtkWidget *dive_list;
781 GtkIconTheme *icon_theme=NULL;
782 GtkSettings *settings;
783 GtkUIManager *ui_manager;
785 gtk_init(argcp, argvp);
786 settings = gtk_settings_get_default();
787 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
791 subsurface_open_conf();
792 if (subsurface_get_conf("feet", PREF_BOOL))
793 output_units.length = FEET;
794 if (subsurface_get_conf("psi", PREF_BOOL))
795 output_units.pressure = PSI;
796 if (subsurface_get_conf("cuft", PREF_BOOL))
797 output_units.volume = CUFT;
798 if (subsurface_get_conf("fahrenheit", PREF_BOOL))
799 output_units.temperature = FAHRENHEIT;
800 if (subsurface_get_conf("lbs", PREF_BOOL))
801 output_units.weight = LBS;
802 /* an unset key is FALSE - all these are hidden by default */
803 visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
804 visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
805 visible_cols.totalweight = PTR_TO_BOOL(subsurface_get_conf("TOTALWEIGHT", PREF_BOOL));
806 visible_cols.suit = PTR_TO_BOOL(subsurface_get_conf("SUIT", PREF_BOOL));
807 visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
808 visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
809 visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
811 divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
813 autogroup = PTR_TO_BOOL(subsurface_get_conf("autogroup", PREF_BOOL));
815 default_dive_computer_vendor = subsurface_get_conf("dive_computer_vendor", PREF_STRING);
816 default_dive_computer_product = subsurface_get_conf("dive_computer_product", PREF_STRING);
817 default_dive_computer_device = subsurface_get_conf("dive_computer_device", PREF_STRING);
819 error_info_bar = NULL;
820 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
821 g_set_application_name ("subsurface");
822 /* Let's check if the subsurface icon has been installed or if
823 * we need to try to load it from the current directory */
824 screen = gdk_screen_get_default();
826 icon_theme = gtk_icon_theme_get_for_screen(screen);
828 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
830 gtk_window_set_default_icon_name ("subsurface");
834 const char *icon_name = subsurface_icon_name();
835 if (!access(icon_name, R_OK))
836 gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
838 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
839 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
842 vbox = gtk_vbox_new(FALSE, 0);
843 gtk_container_add(GTK_CONTAINER(win), vbox);
846 ui_manager = gtk_ui_manager_new();
847 menubar = get_menubar_menu(win, ui_manager);
849 subsurface_ui_setup(settings, menubar, vbox, ui_manager);
851 vpane = gtk_vpaned_new();
852 gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
853 hpane = gtk_hpaned_new();
854 gtk_paned_add1(GTK_PANED(vpane), hpane);
855 g_signal_connect_after(G_OBJECT(vbox), "realize", G_CALLBACK(view_three), NULL);
857 /* Notebook for dive info vs profile vs .. */
858 notebook = gtk_notebook_new();
859 scrolled = gtk_scrolled_window_new(NULL, NULL);
860 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
861 gtk_paned_add1(GTK_PANED(hpane), scrolled);
862 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), notebook);
863 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
865 /* Create the actual divelist */
866 dive_list = dive_list_create();
867 gtk_widget_set_name(dive_list, "Dive List");
868 gtk_paned_add2(GTK_PANED(vpane), dive_list);
870 /* Frame for dive profile */
871 dive_profile = dive_profile_widget();
872 gtk_widget_set_name(dive_profile, "Dive Profile");
873 gtk_paned_add2(GTK_PANED(hpane), dive_profile);
875 /* Frame for extended dive info */
876 nb_page = extended_dive_info_widget();
877 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Notes"));
879 /* Frame for dive equipment */
880 nb_page = equipment_widget(W_IDX_PRIMARY);
881 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Equipment"));
883 /* Frame for single dive statistics */
884 nb_page = single_stats_widget();
885 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
887 /* Frame for total dive statistics */
888 nb_page = total_stats_widget();
889 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Stats"));
891 gtk_widget_set_app_paintable(win, TRUE);
892 gtk_widget_show_all(win);
904 subsurface_close_conf();
908 cairo_rectangle_int_t rect;
912 static tooltip_record_t *tooltip_rects;
915 void attach_tooltip(int x, int y, int w, int h, const char *text)
917 cairo_rectangle_int_t *rect;
918 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
919 rect = &tooltip_rects[tooltips].rect;
924 tooltip_rects[tooltips].text = text;
928 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
929 (_r.y <= _y) && (_r.y + _r.height >= _y))
931 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
932 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
935 cairo_rectangle_int_t *drawing_area = user_data;
936 gint tx = x - drawing_area->x; /* get transformed coordinates */
937 gint ty = y - drawing_area->y;
939 /* are we over an event marker ? */
940 for (i = 0; i < tooltips; i++) {
941 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
942 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
943 return TRUE; /* show tooltip */
946 return FALSE; /* don't show tooltip */
949 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
951 struct dive *dive = current_dive;
952 struct graphics_context gc = { .printer = 0 };
953 static cairo_rectangle_int_t drawing_area;
955 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
956 * so effective drawing area is width-2x * height-2y */
957 drawing_area.width = widget->allocation.width;
958 drawing_area.height = widget->allocation.height;
959 drawing_area.x = drawing_area.width / 20.0;
960 drawing_area.y = drawing_area.height / 20.0;
962 gc.cr = gdk_cairo_create(widget->window);
963 g_object_set(widget, "has-tooltip", TRUE, NULL);
964 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
965 init_profile_background(&gc);
971 tooltip_rects = NULL;
974 plot(&gc, &drawing_area, dive);
976 cairo_destroy(gc.cr);
981 GtkWidget *dive_profile_widget(void)
985 da = gtk_drawing_area_new();
986 gtk_widget_set_size_request(da, 350, 250);
987 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
992 int process_ui_events(void)
996 while (gtk_events_pending()) {
997 if (gtk_main_iteration_do(0)) {
1005 static int fill_computer_list(GtkListStore *store)
1009 dc_iterator_t *iterator = NULL;
1010 dc_descriptor_t *descriptor = NULL;
1013 dc_descriptor_iterator(&iterator);
1014 while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) {
1015 const char *vendor = dc_descriptor_get_vendor(descriptor);
1016 const char *product = dc_descriptor_get_product(descriptor);
1018 gtk_list_store_append(store, &iter);
1019 gtk_list_store_set(store, &iter,
1022 if (is_default_dive_computer(vendor, product))
1026 dc_iterator_free(iterator);
1030 void render_dive_computer(GtkCellLayout *cell,
1031 GtkCellRenderer *renderer,
1032 GtkTreeModel *model,
1037 dc_descriptor_t *descriptor = NULL;
1038 const char *vendor, *product;
1040 gtk_tree_model_get(model, iter, 0, &descriptor, -1);
1041 vendor = dc_descriptor_get_vendor(descriptor);
1042 product = dc_descriptor_get_product(descriptor);
1043 snprintf(buffer, sizeof(buffer), "%s %s", vendor, product);
1044 g_object_set(renderer, "text", buffer, NULL);
1048 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
1050 GtkWidget *hbox, *combo_box, *frame;
1051 GtkListStore *model;
1052 GtkCellRenderer *renderer;
1055 hbox = gtk_hbox_new(FALSE, 6);
1056 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1058 model = gtk_list_store_new(1, G_TYPE_POINTER);
1059 default_index = fill_computer_list(model);
1061 frame = gtk_frame_new("Dive computer");
1062 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1064 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
1065 gtk_container_add(GTK_CONTAINER(frame), combo_box);
1067 renderer = gtk_cell_renderer_text_new();
1068 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
1069 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo_box), renderer, render_dive_computer, NULL, NULL);
1071 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
1073 return GTK_COMBO_BOX(combo_box);
1076 const char *subsurface_device_name()
1078 if (!default_dive_computer_device || !*default_dive_computer_device)
1079 return subsurface_USB_name();
1081 return default_dive_computer_device;
1084 static GtkEntry *dive_computer_device(GtkWidget *vbox)
1086 GtkWidget *hbox, *entry, *frame;
1088 hbox = gtk_hbox_new(FALSE, 6);
1089 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1091 frame = gtk_frame_new("Device name");
1092 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1094 entry = gtk_entry_new();
1095 gtk_container_add(GTK_CONTAINER(frame), entry);
1096 gtk_entry_set_text(GTK_ENTRY(entry), subsurface_device_name());
1098 return GTK_ENTRY(entry);
1101 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
1102 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
1104 GtkDialog *main_dialog = _data;
1106 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
1109 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
1111 GtkWidget *hbox, *frame, *chooser, *dialog;
1112 GtkFileFilter *filter;
1114 hbox = gtk_hbox_new(FALSE, 6);
1115 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1117 frame = gtk_frame_new("XML file name");
1118 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1119 dialog = gtk_file_chooser_dialog_new("Open XML File",
1120 GTK_WINDOW(main_window),
1121 GTK_FILE_CHOOSER_ACTION_OPEN,
1122 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1123 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1125 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
1127 filter = gtk_file_filter_new();
1128 gtk_file_filter_add_pattern(filter, "*.xml");
1129 gtk_file_filter_add_pattern(filter, "*.XML");
1130 gtk_file_filter_add_pattern(filter, "*.sda");
1131 gtk_file_filter_add_pattern(filter, "*.SDA");
1132 gtk_file_filter_add_mime_type(filter, "text/xml");
1133 gtk_file_filter_set_name(filter, "XML file");
1134 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
1136 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
1137 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
1139 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
1140 gtk_container_add(GTK_CONTAINER(frame), chooser);
1145 static void do_import_file(gpointer data, gpointer user_data)
1147 GError *error = NULL;
1148 parse_file(data, &error);
1152 report_error(error);
1153 g_error_free(error);
1158 static GtkWidget *import_dive_computer(device_data_t *data, GtkDialog *dialog)
1161 GtkWidget *vbox, *info, *container, *label, *button;
1163 error = do_import(data);
1167 button = gtk_dialog_get_widget_for_response(dialog, GTK_RESPONSE_ACCEPT);
1168 gtk_button_set_use_stock(GTK_BUTTON(button), 0);
1169 gtk_button_set_label(GTK_BUTTON(button), "Retry");
1171 vbox = gtk_dialog_get_content_area(dialog);
1173 info = gtk_info_bar_new();
1174 container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
1175 label = gtk_label_new(error->message);
1176 gtk_container_add(GTK_CONTAINER(container), label);
1177 gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
1181 void import_dialog(GtkWidget *w, gpointer data)
1184 GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
1185 GtkComboBox *computer;
1187 GtkWidget *XMLchooser;
1188 device_data_t devicedata = {
1192 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1193 GTK_WINDOW(main_window),
1194 GTK_DIALOG_DESTROY_WITH_PARENT,
1195 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1196 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1199 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1200 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1201 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1202 XMLchooser = xml_file_selector(vbox, dialog);
1203 computer = dive_computer_selector(vbox);
1204 device = dive_computer_device(vbox);
1205 hbox = gtk_hbox_new(FALSE, 6);
1206 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1207 devicedata.progress.bar = gtk_progress_bar_new();
1208 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1211 gtk_widget_show_all(dialog);
1212 result = gtk_dialog_run(GTK_DIALOG(dialog));
1214 dc_descriptor_t *descriptor;
1216 GtkTreeModel *model;
1218 case GTK_RESPONSE_ACCEPT:
1219 /* what happened - did the user pick a file? In that case
1220 * we ignore whether a dive computer model was picked */
1222 gtk_widget_destroy(info);
1223 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1224 if (g_slist_length(list) == 0) {
1225 const char *vendor, *product;
1227 if (!gtk_combo_box_get_active_iter(computer, &iter))
1229 model = gtk_combo_box_get_model(computer);
1230 gtk_tree_model_get(model, &iter,
1234 vendor = dc_descriptor_get_vendor(descriptor);
1235 product = dc_descriptor_get_product(descriptor);
1237 devicedata.descriptor = descriptor;
1238 devicedata.vendor = vendor;
1239 devicedata.product = product;
1240 devicedata.devname = gtk_entry_get_text(device);
1241 set_default_dive_computer(vendor, product);
1242 set_default_dive_computer_device(devicedata.devname);
1243 info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
1247 g_slist_foreach(list,do_import_file,NULL);
1254 gtk_widget_destroy(dialog);
1259 void update_progressbar(progressbar_t *progress, double value)
1261 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1264 void update_progressbar_text(progressbar_t *progress, const char *text)
1266 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), text);
1269 void set_filename(const char *filename)
1271 if (existing_filename)
1272 free(existing_filename);
1273 existing_filename = NULL;
1275 existing_filename = strdup(filename);