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) * (93 + 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)
393 static void event_toggle(GtkWidget *w, gpointer _data)
395 gboolean *plot_ev = _data;
397 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
400 static void preferences_dialog(GtkWidget *w, gpointer data)
403 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
405 menu_units = output_units;
407 dialog = gtk_dialog_new_with_buttons("Preferences",
408 GTK_WINDOW(main_window),
409 GTK_DIALOG_DESTROY_WITH_PARENT,
410 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
411 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
414 frame = gtk_frame_new("Units");
415 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
416 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
418 box = gtk_vbox_new(FALSE, 6);
419 gtk_container_add(GTK_CONTAINER(frame), box);
421 create_radio(box, "Depth:",
422 "Meter", set_meter, (output_units.length == METERS),
423 "Feet", set_feet, (output_units.length == FEET),
426 create_radio(box, "Pressure:",
427 "Bar", set_bar, (output_units.pressure == BAR),
428 "PSI", set_psi, (output_units.pressure == PSI),
431 create_radio(box, "Volume:",
432 "Liter", set_liter, (output_units.volume == LITER),
433 "CuFt", set_cuft, (output_units.volume == CUFT),
436 create_radio(box, "Temperature:",
437 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
438 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
441 create_radio(box, "Weight:",
442 "kg", set_kg, (output_units.weight == KG),
443 "lbs", set_lbs, (output_units.weight == LBS),
446 frame = gtk_frame_new("Show Columns");
447 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
449 box = gtk_hbox_new(FALSE, 6);
450 gtk_container_add(GTK_CONTAINER(frame), box);
452 button = gtk_check_button_new_with_label("Temp");
453 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
454 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
455 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
457 button = gtk_check_button_new_with_label("Cyl");
458 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
459 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
460 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
462 button = gtk_check_button_new_with_label("O" UTF8_SUBSCRIPT_2 "%");
463 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
464 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
465 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
467 button = gtk_check_button_new_with_label("SAC");
468 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
469 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
470 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
472 button = gtk_check_button_new_with_label("OTU");
473 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
474 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
475 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
477 button = gtk_check_button_new_with_label("Weight");
478 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.totalweight);
479 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
480 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(totalweight_toggle), NULL);
482 button = gtk_check_button_new_with_label("Suit");
483 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.suit);
484 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
485 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(suit_toggle), NULL);
487 font = gtk_font_button_new_with_font(divelist_font);
488 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
490 gtk_widget_show_all(dialog);
491 result = gtk_dialog_run(GTK_DIALOG(dialog));
492 if (result == GTK_RESPONSE_ACCEPT) {
493 /* Make sure to flush any modified old dive data with old units */
496 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
497 set_divelist_font(divelist_font);
499 output_units = menu_units;
500 update_dive_list_units();
502 update_dive_list_col_visibility();
504 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
505 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
506 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
507 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
508 subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
509 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
510 subsurface_set_conf("TOTALWEIGHT", PREF_BOOL, BOOL_TO_PTR(visible_cols.totalweight));
511 subsurface_set_conf("SUIT", PREF_BOOL, BOOL_TO_PTR(visible_cols.suit));
512 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
513 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
514 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
515 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
516 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
518 /* Flush the changes out to the system */
519 subsurface_flush_conf();
521 gtk_widget_destroy(dialog);
524 static void create_toggle(const char* label, int *on, void *_data)
526 GtkWidget *button, *table = _data;
527 int rows, cols, x, y;
531 /* magic way to reset the number of toggle buttons
532 * that we have already added - call this before you
533 * create the dialog */
537 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
538 if (count > rows * cols) {
539 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
544 button = gtk_check_button_new_with_label(label);
545 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
546 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
547 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
551 static void selectevents_dialog(GtkWidget *w, gpointer data)
554 GtkWidget *dialog, *frame, *vbox, *table;
556 dialog = gtk_dialog_new_with_buttons("SelectEvents",
557 GTK_WINDOW(main_window),
558 GTK_DIALOG_DESTROY_WITH_PARENT,
559 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
560 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
562 /* initialize the function that fills the table */
563 create_toggle(NULL, NULL, NULL);
565 frame = gtk_frame_new("Enable / Disable Events");
566 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
567 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
569 table = gtk_table_new(1, 4, TRUE);
570 gtk_container_add(GTK_CONTAINER(frame), table);
572 evn_foreach(&create_toggle, table);
574 gtk_widget_show_all(dialog);
575 result = gtk_dialog_run(GTK_DIALOG(dialog));
576 if (result == GTK_RESPONSE_ACCEPT) {
579 gtk_widget_destroy(dialog);
582 static void renumber_dialog(GtkWidget *w, gpointer data)
586 GtkWidget *dialog, *frame, *button, *vbox;
588 dialog = gtk_dialog_new_with_buttons("Renumber",
589 GTK_WINDOW(main_window),
590 GTK_DIALOG_DESTROY_WITH_PARENT,
591 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
592 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
595 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
597 frame = gtk_frame_new("New starting number");
598 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
600 button = gtk_spin_button_new_with_range(1, 50000, 1);
601 gtk_container_add(GTK_CONTAINER(frame), button);
604 * Do we have a number for the first dive already? Use that
608 if (dive && dive->number)
609 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
611 gtk_widget_show_all(dialog);
612 result = gtk_dialog_run(GTK_DIALOG(dialog));
613 if (result == GTK_RESPONSE_ACCEPT) {
614 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
618 gtk_widget_destroy(dialog);
621 static void about_dialog(GtkWidget *w, gpointer data)
623 const char *logo_property = NULL;
624 GdkPixbuf *logo = NULL;
627 GtkWidget *image = gtk_image_new_from_file(subsurface_icon_name());
630 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
631 logo_property = "logo";
635 gtk_show_about_dialog(NULL,
636 "program-name", "SubSurface",
637 "comments", "Half-arsed divelog software in C",
639 "version", VERSION_STRING,
640 "copyright", "Linus Torvalds 2011",
641 "logo-icon-name", "subsurface",
647 static void view_list(GtkWidget *w, gpointer data)
649 gtk_paned_set_position(GTK_PANED(vpane), 0);
652 static void view_profile(GtkWidget *w, gpointer data)
654 gtk_paned_set_position(GTK_PANED(hpane), 0);
655 gtk_paned_set_position(GTK_PANED(vpane), 65535);
658 static void view_info(GtkWidget *w, gpointer data)
660 gtk_paned_set_position(GTK_PANED(vpane), 65535);
661 gtk_paned_set_position(GTK_PANED(hpane), 65535);
664 static void view_three(GtkWidget *w, gpointer data)
667 GtkRequisition requisition;
669 gtk_widget_get_allocation(hpane, &alloc);
670 gtk_paned_set_position(GTK_PANED(hpane), alloc.width/2);
671 gtk_widget_get_allocation(vpane, &alloc);
672 gtk_widget_size_request(notebook, &requisition);
673 /* pick the requested size for the notebook plus 6 pixels for frame */
674 gtk_paned_set_position(GTK_PANED(vpane), requisition.height + 6);
677 static GtkActionEntry menu_items[] = {
678 { "FileMenuAction", NULL, "File", NULL, NULL, NULL},
679 { "LogMenuAction", NULL, "Log", NULL, NULL, NULL},
680 { "ViewMenuAction", NULL, "View", NULL, NULL, NULL},
681 { "FilterMenuAction", NULL, "Filter", NULL, NULL, NULL},
682 { "HelpMenuAction", NULL, "Help", NULL, NULL, NULL},
683 { "OpenFile", GTK_STOCK_OPEN, NULL, CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
684 { "SaveFile", GTK_STOCK_SAVE, NULL, CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
685 { "SaveAsFile", GTK_STOCK_SAVE_AS, NULL, SHIFTCHAR CTRLCHAR "S", NULL, G_CALLBACK(file_save_as) },
686 { "Print", GTK_STOCK_PRINT, NULL, CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
687 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
688 { "AddDive", GTK_STOCK_ADD, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) },
689 { "Preferences", GTK_STOCK_PREFERENCES, "Preferences", PREFERENCE_ACCEL, NULL, G_CALLBACK(preferences_dialog) },
690 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
691 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
692 { "Quit", GTK_STOCK_QUIT, NULL, CTRLCHAR "Q", NULL, G_CALLBACK(quit) },
693 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
694 { "ViewList", NULL, "List", CTRLCHAR "1", NULL, G_CALLBACK(view_list) },
695 { "ViewProfile", NULL, "Profile", CTRLCHAR "2", NULL, G_CALLBACK(view_profile) },
696 { "ViewInfo", NULL, "Info", CTRLCHAR "3", NULL, G_CALLBACK(view_info) },
697 { "ViewThree", NULL, "Three", CTRLCHAR "4", NULL, G_CALLBACK(view_three) },
699 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
701 static const gchar* ui_string = " \
703 <menubar name=\"MainMenu\"> \
704 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
705 <menuitem name=\"Open\" action=\"OpenFile\" /> \
706 <menuitem name=\"Save\" action=\"SaveFile\" /> \
707 <menuitem name=\"Save As\" action=\"SaveAsFile\" /> \
708 <menuitem name=\"Print\" action=\"Print\" /> \
709 <separator name=\"Separator1\"/> \
710 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
711 <separator name=\"Separator2\"/> \
712 <menuitem name=\"Quit\" action=\"Quit\" /> \
714 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
715 <menuitem name=\"Import\" action=\"Import\" /> \
716 <menuitem name=\"Add Dive\" action=\"AddDive\" /> \
717 <separator name=\"Separator\"/> \
718 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
719 <menu name=\"View\" action=\"ViewMenuAction\"> \
720 <menuitem name=\"List\" action=\"ViewList\" /> \
721 <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
722 <menuitem name=\"Info\" action=\"ViewInfo\" /> \
723 <menuitem name=\"Paned\" action=\"ViewThree\" /> \
726 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
727 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
729 <menu name=\"Help\" action=\"HelpMenuAction\"> \
730 <menuitem name=\"About\" action=\"About\" /> \
736 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
738 GtkActionGroup *action_group = gtk_action_group_new("Menu");
739 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
741 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
743 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
745 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
746 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
751 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
756 void init_ui(int *argcp, char ***argvp)
760 GtkWidget *dive_list;
765 GtkIconTheme *icon_theme=NULL;
766 GtkSettings *settings;
767 GtkUIManager *ui_manager;
769 gtk_init(argcp, argvp);
770 settings = gtk_settings_get_default();
771 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
775 subsurface_open_conf();
776 if (subsurface_get_conf("feet", PREF_BOOL))
777 output_units.length = FEET;
778 if (subsurface_get_conf("psi", PREF_BOOL))
779 output_units.pressure = PSI;
780 if (subsurface_get_conf("cuft", PREF_BOOL))
781 output_units.volume = CUFT;
782 if (subsurface_get_conf("fahrenheit", PREF_BOOL))
783 output_units.temperature = FAHRENHEIT;
784 if (subsurface_get_conf("lbs", PREF_BOOL))
785 output_units.weight = LBS;
786 /* an unset key is FALSE - all these are hidden by default */
787 visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
788 visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
789 visible_cols.totalweight = PTR_TO_BOOL(subsurface_get_conf("TOTALWEIGHT", PREF_BOOL));
790 visible_cols.suit = PTR_TO_BOOL(subsurface_get_conf("SUIT", PREF_BOOL));
791 visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
792 visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
793 visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
795 divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
797 default_dive_computer_vendor = subsurface_get_conf("dive_computer_vendor", PREF_STRING);
798 default_dive_computer_product = subsurface_get_conf("dive_computer_product", PREF_STRING);
799 default_dive_computer_device = subsurface_get_conf("dive_computer_device", PREF_STRING);
801 error_info_bar = NULL;
802 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
803 g_set_application_name ("subsurface");
804 /* Let's check if the subsurface icon has been installed or if
805 * we need to try to load it from the current directory */
806 screen = gdk_screen_get_default();
808 icon_theme = gtk_icon_theme_get_for_screen(screen);
810 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
812 gtk_window_set_default_icon_name ("subsurface");
816 const char *icon_name = subsurface_icon_name();
817 if (!access(icon_name, R_OK))
818 gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
820 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
821 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
824 vbox = gtk_vbox_new(FALSE, 0);
825 gtk_container_add(GTK_CONTAINER(win), vbox);
828 ui_manager = gtk_ui_manager_new();
829 menubar = get_menubar_menu(win, ui_manager);
831 subsurface_ui_setup(settings, menubar, vbox, ui_manager);
833 vpane = gtk_vpaned_new();
834 gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
835 hpane = gtk_hpaned_new();
836 gtk_paned_add1(GTK_PANED(vpane), hpane);
837 g_signal_connect_after(G_OBJECT(vbox), "realize", G_CALLBACK(view_three), NULL);
839 /* Notebook for dive info vs profile vs .. */
840 notebook = gtk_notebook_new();
841 scrolled = gtk_scrolled_window_new(NULL, NULL);
842 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
843 gtk_paned_add1(GTK_PANED(hpane), scrolled);
844 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), notebook);
845 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
847 /* Create the actual divelist */
848 dive_list = dive_list_create();
849 gtk_widget_set_name(dive_list, "Dive List");
850 gtk_paned_add2(GTK_PANED(vpane), dive_list);
852 /* Frame for dive profile */
853 dive_profile = dive_profile_widget();
854 gtk_widget_set_name(dive_profile, "Dive Profile");
855 gtk_paned_add2(GTK_PANED(hpane), dive_profile);
857 /* Frame for extended dive info */
858 nb_page = extended_dive_info_widget();
859 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Notes"));
861 /* Frame for dive equipment */
862 nb_page = equipment_widget(W_IDX_PRIMARY);
863 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Equipment"));
865 /* Frame for single dive statistics */
866 nb_page = single_stats_widget();
867 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
869 /* Frame for total dive statistics */
870 nb_page = total_stats_widget();
871 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Stats"));
873 gtk_widget_set_app_paintable(win, TRUE);
874 gtk_widget_show_all(win);
886 subsurface_close_conf();
890 cairo_rectangle_int_t rect;
894 static tooltip_record_t *tooltip_rects;
897 void attach_tooltip(int x, int y, int w, int h, const char *text)
899 cairo_rectangle_int_t *rect;
900 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
901 rect = &tooltip_rects[tooltips].rect;
906 tooltip_rects[tooltips].text = text;
910 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
911 (_r.y <= _y) && (_r.y + _r.height >= _y))
913 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
914 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
917 cairo_rectangle_int_t *drawing_area = user_data;
918 gint tx = x - drawing_area->x; /* get transformed coordinates */
919 gint ty = y - drawing_area->y;
921 /* are we over an event marker ? */
922 for (i = 0; i < tooltips; i++) {
923 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
924 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
925 return TRUE; /* show tooltip */
928 return FALSE; /* don't show tooltip */
931 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
933 struct dive *dive = current_dive;
934 struct graphics_context gc = { .printer = 0 };
935 static cairo_rectangle_int_t drawing_area;
937 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
938 * so effective drawing area is width-2x * height-2y */
939 drawing_area.width = widget->allocation.width;
940 drawing_area.height = widget->allocation.height;
941 drawing_area.x = drawing_area.width / 20.0;
942 drawing_area.y = drawing_area.height / 20.0;
944 gc.cr = gdk_cairo_create(widget->window);
945 g_object_set(widget, "has-tooltip", TRUE, NULL);
946 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
947 init_profile_background(&gc);
953 tooltip_rects = NULL;
956 plot(&gc, &drawing_area, dive);
958 cairo_destroy(gc.cr);
963 GtkWidget *dive_profile_widget(void)
967 da = gtk_drawing_area_new();
968 gtk_widget_set_size_request(da, 350, 250);
969 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
974 int process_ui_events(void)
978 while (gtk_events_pending()) {
979 if (gtk_main_iteration_do(0)) {
987 static int fill_computer_list(GtkListStore *store)
991 dc_iterator_t *iterator = NULL;
992 dc_descriptor_t *descriptor = NULL;
995 dc_descriptor_iterator(&iterator);
996 while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) {
997 const char *vendor = dc_descriptor_get_vendor(descriptor);
998 const char *product = dc_descriptor_get_product(descriptor);
1000 gtk_list_store_append(store, &iter);
1001 gtk_list_store_set(store, &iter,
1004 if (is_default_dive_computer(vendor, product))
1008 dc_iterator_free(iterator);
1012 void render_dive_computer(GtkCellLayout *cell,
1013 GtkCellRenderer *renderer,
1014 GtkTreeModel *model,
1019 dc_descriptor_t *descriptor = NULL;
1020 const char *vendor, *product;
1022 gtk_tree_model_get(model, iter, 0, &descriptor, -1);
1023 vendor = dc_descriptor_get_vendor(descriptor);
1024 product = dc_descriptor_get_product(descriptor);
1025 snprintf(buffer, sizeof(buffer), "%s %s", vendor, product);
1026 g_object_set(renderer, "text", buffer, NULL);
1030 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
1032 GtkWidget *hbox, *combo_box, *frame;
1033 GtkListStore *model;
1034 GtkCellRenderer *renderer;
1037 hbox = gtk_hbox_new(FALSE, 6);
1038 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1040 model = gtk_list_store_new(1, G_TYPE_POINTER);
1041 default_index = fill_computer_list(model);
1043 frame = gtk_frame_new("Dive computer");
1044 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1046 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
1047 gtk_container_add(GTK_CONTAINER(frame), combo_box);
1049 renderer = gtk_cell_renderer_text_new();
1050 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
1051 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo_box), renderer, render_dive_computer, NULL, NULL);
1053 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
1055 return GTK_COMBO_BOX(combo_box);
1058 const char *subsurface_device_name()
1060 if (!default_dive_computer_device || !*default_dive_computer_device)
1061 return subsurface_USB_name();
1063 return default_dive_computer_device;
1066 static GtkEntry *dive_computer_device(GtkWidget *vbox)
1068 GtkWidget *hbox, *entry, *frame;
1070 hbox = gtk_hbox_new(FALSE, 6);
1071 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1073 frame = gtk_frame_new("Device name");
1074 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1076 entry = gtk_entry_new();
1077 gtk_container_add(GTK_CONTAINER(frame), entry);
1078 gtk_entry_set_text(GTK_ENTRY(entry), subsurface_device_name());
1080 return GTK_ENTRY(entry);
1083 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
1084 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
1086 GtkDialog *main_dialog = _data;
1088 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
1091 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
1093 GtkWidget *hbox, *frame, *chooser, *dialog;
1094 GtkFileFilter *filter;
1096 hbox = gtk_hbox_new(FALSE, 6);
1097 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1099 frame = gtk_frame_new("XML file name");
1100 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
1101 dialog = gtk_file_chooser_dialog_new("Open XML File",
1102 GTK_WINDOW(main_window),
1103 GTK_FILE_CHOOSER_ACTION_OPEN,
1104 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1105 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1107 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
1109 filter = gtk_file_filter_new();
1110 gtk_file_filter_add_pattern(filter, "*.xml");
1111 gtk_file_filter_add_pattern(filter, "*.XML");
1112 gtk_file_filter_add_pattern(filter, "*.sda");
1113 gtk_file_filter_add_pattern(filter, "*.SDA");
1114 gtk_file_filter_add_mime_type(filter, "text/xml");
1115 gtk_file_filter_set_name(filter, "XML file");
1116 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
1118 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
1119 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
1121 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
1122 gtk_container_add(GTK_CONTAINER(frame), chooser);
1127 static void do_import_file(gpointer data, gpointer user_data)
1129 GError *error = NULL;
1130 parse_file(data, &error);
1134 report_error(error);
1135 g_error_free(error);
1140 static GtkWidget *import_dive_computer(device_data_t *data, GtkDialog *dialog)
1143 GtkWidget *vbox, *info, *container, *label, *button;
1145 error = do_import(data);
1149 button = gtk_dialog_get_widget_for_response(dialog, GTK_RESPONSE_ACCEPT);
1150 gtk_button_set_use_stock(GTK_BUTTON(button), 0);
1151 gtk_button_set_label(GTK_BUTTON(button), "Retry");
1153 vbox = gtk_dialog_get_content_area(dialog);
1155 info = gtk_info_bar_new();
1156 container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
1157 label = gtk_label_new(error->message);
1158 gtk_container_add(GTK_CONTAINER(container), label);
1159 gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
1163 void import_dialog(GtkWidget *w, gpointer data)
1166 GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
1167 GtkComboBox *computer;
1169 GtkWidget *XMLchooser;
1170 device_data_t devicedata = {
1174 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1175 GTK_WINDOW(main_window),
1176 GTK_DIALOG_DESTROY_WITH_PARENT,
1177 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1178 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1181 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1182 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1183 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1184 XMLchooser = xml_file_selector(vbox, dialog);
1185 computer = dive_computer_selector(vbox);
1186 device = dive_computer_device(vbox);
1187 hbox = gtk_hbox_new(FALSE, 6);
1188 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1189 devicedata.progress.bar = gtk_progress_bar_new();
1190 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1193 gtk_widget_show_all(dialog);
1194 result = gtk_dialog_run(GTK_DIALOG(dialog));
1196 dc_descriptor_t *descriptor;
1198 GtkTreeModel *model;
1200 case GTK_RESPONSE_ACCEPT:
1201 /* what happened - did the user pick a file? In that case
1202 * we ignore whether a dive computer model was picked */
1204 gtk_widget_destroy(info);
1205 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1206 if (g_slist_length(list) == 0) {
1207 const char *vendor, *product;
1209 if (!gtk_combo_box_get_active_iter(computer, &iter))
1211 model = gtk_combo_box_get_model(computer);
1212 gtk_tree_model_get(model, &iter,
1216 vendor = dc_descriptor_get_vendor(descriptor);
1217 product = dc_descriptor_get_product(descriptor);
1219 devicedata.descriptor = descriptor;
1220 devicedata.vendor = vendor;
1221 devicedata.product = product;
1222 devicedata.devname = gtk_entry_get_text(device);
1223 set_default_dive_computer(vendor, product);
1224 set_default_dive_computer_device(devicedata.devname);
1225 info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
1229 g_slist_foreach(list,do_import_file,NULL);
1236 gtk_widget_destroy(dialog);
1241 void update_progressbar(progressbar_t *progress, double value)
1243 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1246 void update_progressbar_text(progressbar_t *progress, const char *text)
1248 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), text);
1251 void set_filename(const char *filename)
1253 if (existing_filename)
1254 free(existing_filename);
1255 existing_filename = NULL;
1257 existing_filename = strdup(filename);