2 /* gtk UI implementation */
3 /* creates the window and overall layout
4 * divelist, dive info, equipment and printing are handled in their own source files
15 #include "display-gtk.h"
17 #include "libdivecomputer.h"
19 GtkWidget *main_window;
21 GtkWidget *error_info_bar;
22 GtkWidget *error_label;
23 GtkWidget *vpane, *hpane;
26 const char *divelist_font;
28 struct units output_units;
30 static GtkWidget *dive_profile;
32 visible_cols_t visible_cols = {TRUE, FALSE};
34 void repaint_dive(void)
36 update_dive(current_dive);
38 gtk_widget_queue_draw(dive_profile);
41 static char *existing_filename;
42 static gboolean need_icon = TRUE;
44 static void on_info_bar_response(GtkWidget *widget, gint response,
47 if (response == GTK_RESPONSE_OK)
49 gtk_widget_destroy(widget);
50 error_info_bar = NULL;
54 void report_error(GError* error)
61 if (error_info_bar == NULL)
64 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
67 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
68 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
71 error_label = gtk_label_new(error->message);
72 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
73 gtk_container_add(GTK_CONTAINER(container), error_label);
75 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
76 gtk_widget_show_all(main_vbox);
82 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
83 gtk_label_set(GTK_LABEL(error_label), buffer);
87 static void file_open(GtkWidget *w, gpointer data)
90 GtkFileFilter *filter;
92 dialog = gtk_file_chooser_dialog_new("Open File",
93 GTK_WINDOW(main_window),
94 GTK_FILE_CHOOSER_ACTION_OPEN,
95 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
96 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
98 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
100 filter = gtk_file_filter_new();
101 gtk_file_filter_add_pattern(filter, "*.xml");
102 gtk_file_filter_add_pattern(filter, "*.XML");
103 gtk_file_filter_add_pattern(filter, "*.sda");
104 gtk_file_filter_add_pattern(filter, "*.SDA");
105 gtk_file_filter_add_mime_type(filter, "text/xml");
106 gtk_file_filter_set_name(filter, "XML file");
107 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
109 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
110 GSList *filenames, *fn_glist;
112 filenames = fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
114 GError *error = NULL;
115 while(filenames != NULL) {
116 filename = filenames->data;
117 parse_file(filename, &error);
126 filenames = g_slist_next(filenames);
128 g_slist_free(fn_glist);
131 gtk_widget_destroy(dialog);
134 static void file_save(GtkWidget *w, gpointer data)
137 dialog = gtk_file_chooser_dialog_new("Save File",
138 GTK_WINDOW(main_window),
139 GTK_FILE_CHOOSER_ACTION_SAVE,
140 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
141 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
143 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
144 if (!existing_filename) {
145 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
147 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
149 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
151 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
152 save_dives(filename);
154 mark_divelist_changed(FALSE);
156 gtk_widget_destroy(dialog);
159 static void ask_save_changes()
161 GtkWidget *dialog, *label, *content;
162 dialog = gtk_dialog_new_with_buttons("Save Changes?",
163 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
164 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
165 GTK_STOCK_NO, GTK_RESPONSE_NO,
167 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
168 label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
169 gtk_container_add (GTK_CONTAINER (content), label);
170 gtk_widget_show_all (dialog);
171 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
172 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
173 file_save(NULL,NULL);
175 gtk_widget_destroy(dialog);
178 static gboolean on_delete(GtkWidget* w, gpointer data)
180 /* Make sure to flush any modified dive data */
183 if (unsaved_changes())
186 return FALSE; /* go ahead, kill the program, we're good now */
189 static void on_destroy(GtkWidget* w, gpointer data)
194 static void quit(GtkWidget *w, gpointer data)
196 /* Make sure to flush any modified dive data */
199 if (unsaved_changes())
204 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
205 data_func_t data_func, unsigned int flags)
207 GtkCellRenderer *renderer;
208 GtkTreeViewColumn *col;
209 double xalign = 0.0; /* left as default */
210 PangoAlignment align;
213 align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
214 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
216 visible = !(flags & INVISIBLE);
218 renderer = gtk_cell_renderer_text_new();
219 col = gtk_tree_view_column_new();
221 gtk_tree_view_column_set_title(col, title);
222 if (!(flags & UNSORTABLE))
223 gtk_tree_view_column_set_sort_column_id(col, index);
224 gtk_tree_view_column_set_resizable(col, TRUE);
225 gtk_tree_view_column_pack_start(col, renderer, TRUE);
227 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
229 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
230 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
232 case PANGO_ALIGN_LEFT:
235 case PANGO_ALIGN_CENTER:
238 case PANGO_ALIGN_RIGHT:
242 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
243 gtk_tree_view_column_set_visible(col, visible);
244 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
248 static void create_radio(GtkWidget *vbox, const char *w_name, ...)
251 GtkRadioButton *group = NULL;
252 GtkWidget *box, *label;
254 box = gtk_hbox_new(TRUE, 10);
255 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
257 label = gtk_label_new(w_name);
258 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
260 va_start(args, w_name);
267 name = va_arg(args, char *);
270 callback_fn = va_arg(args, void *);
271 enabled = va_arg(args, int);
273 button = gtk_radio_button_new_with_label_from_widget(group, name);
274 group = GTK_RADIO_BUTTON(button);
275 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
276 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
277 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
282 #define UNITCALLBACK(name, type, value) \
283 static void name(GtkWidget *w, gpointer data) \
285 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
286 menu_units.type = value; \
289 static struct units menu_units;
291 UNITCALLBACK(set_meter, length, METERS)
292 UNITCALLBACK(set_feet, length, FEET)
293 UNITCALLBACK(set_bar, pressure, BAR)
294 UNITCALLBACK(set_psi, pressure, PSI)
295 UNITCALLBACK(set_liter, volume, LITER)
296 UNITCALLBACK(set_cuft, volume, CUFT)
297 UNITCALLBACK(set_celsius, temperature, CELSIUS)
298 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
299 UNITCALLBACK(set_kg, weight, KG)
300 UNITCALLBACK(set_lbs, weight, LBS)
302 #define OPTIONCALLBACK(name, option) \
303 static void name(GtkWidget *w, gpointer data) \
305 option = GTK_TOGGLE_BUTTON(w)->active; \
308 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
309 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
310 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
311 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
312 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
314 static void event_toggle(GtkWidget *w, gpointer _data)
316 gboolean *plot_ev = _data;
318 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
321 static void preferences_dialog(GtkWidget *w, gpointer data)
324 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
326 menu_units = output_units;
328 dialog = gtk_dialog_new_with_buttons("Preferences",
329 GTK_WINDOW(main_window),
330 GTK_DIALOG_DESTROY_WITH_PARENT,
331 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
332 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
335 frame = gtk_frame_new("Units");
336 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
337 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
339 box = gtk_vbox_new(FALSE, 6);
340 gtk_container_add(GTK_CONTAINER(frame), box);
342 create_radio(box, "Depth:",
343 "Meter", set_meter, (output_units.length == METERS),
344 "Feet", set_feet, (output_units.length == FEET),
347 create_radio(box, "Pressure:",
348 "Bar", set_bar, (output_units.pressure == BAR),
349 "PSI", set_psi, (output_units.pressure == PSI),
352 create_radio(box, "Volume:",
353 "Liter", set_liter, (output_units.volume == LITER),
354 "CuFt", set_cuft, (output_units.volume == CUFT),
357 create_radio(box, "Temperature:",
358 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
359 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
362 create_radio(box, "Weight:",
363 "kg", set_kg, (output_units.weight == KG),
364 "lbs", set_lbs, (output_units.weight == LBS),
367 frame = gtk_frame_new("Columns");
368 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
370 box = gtk_hbox_new(FALSE, 6);
371 gtk_container_add(GTK_CONTAINER(frame), box);
373 button = gtk_check_button_new_with_label("Show Temp");
374 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
375 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
376 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
378 button = gtk_check_button_new_with_label("Show Cyl");
379 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
380 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
381 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
383 button = gtk_check_button_new_with_label("Show O" UTF8_SUBSCRIPT_2 "%");
384 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
385 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
386 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
388 button = gtk_check_button_new_with_label("Show SAC");
389 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
390 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
391 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
393 button = gtk_check_button_new_with_label("Show OTU");
394 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
395 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
396 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
398 font = gtk_font_button_new_with_font(divelist_font);
399 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
401 gtk_widget_show_all(dialog);
402 result = gtk_dialog_run(GTK_DIALOG(dialog));
403 if (result == GTK_RESPONSE_ACCEPT) {
404 /* Make sure to flush any modified old dive data with old units */
407 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
408 set_divelist_font(divelist_font);
410 output_units = menu_units;
411 update_dive_list_units();
413 update_dive_list_col_visibility();
415 subsurface_set_conf("feet", PREF_BOOL, BOOL_TO_PTR(output_units.length == FEET));
416 subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
417 subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
418 subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
419 subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
420 subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
421 subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
422 subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
423 subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
424 subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
425 subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
427 /* Flush the changes out to the system */
428 subsurface_flush_conf();
430 gtk_widget_destroy(dialog);
433 static void create_toggle(const char* label, int *on, void *_data)
435 GtkWidget *button, *table = _data;
436 int rows, cols, x, y;
440 /* magic way to reset the number of toggle buttons
441 * that we have already added - call this before you
442 * create the dialog */
446 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
447 if (count > rows * cols) {
448 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
453 button = gtk_check_button_new_with_label(label);
454 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
455 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
456 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
460 static void selectevents_dialog(GtkWidget *w, gpointer data)
463 GtkWidget *dialog, *frame, *vbox, *table;
465 dialog = gtk_dialog_new_with_buttons("SelectEvents",
466 GTK_WINDOW(main_window),
467 GTK_DIALOG_DESTROY_WITH_PARENT,
468 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
469 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
471 /* initialize the function that fills the table */
472 create_toggle(NULL, NULL, NULL);
474 frame = gtk_frame_new("Enable / Disable Events");
475 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
476 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
478 table = gtk_table_new(1, 4, TRUE);
479 gtk_container_add(GTK_CONTAINER(frame), table);
481 evn_foreach(&create_toggle, table);
483 gtk_widget_show_all(dialog);
484 result = gtk_dialog_run(GTK_DIALOG(dialog));
485 if (result == GTK_RESPONSE_ACCEPT) {
488 gtk_widget_destroy(dialog);
491 static void renumber_dialog(GtkWidget *w, gpointer data)
495 GtkWidget *dialog, *frame, *button, *vbox;
497 dialog = gtk_dialog_new_with_buttons("Renumber",
498 GTK_WINDOW(main_window),
499 GTK_DIALOG_DESTROY_WITH_PARENT,
500 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
501 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
504 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
506 frame = gtk_frame_new("New starting number");
507 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
509 button = gtk_spin_button_new_with_range(1, 50000, 1);
510 gtk_container_add(GTK_CONTAINER(frame), button);
513 * Do we have a number for the first dive already? Use that
517 if (dive && dive->number)
518 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
520 gtk_widget_show_all(dialog);
521 result = gtk_dialog_run(GTK_DIALOG(dialog));
522 if (result == GTK_RESPONSE_ACCEPT) {
523 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
527 gtk_widget_destroy(dialog);
530 static void about_dialog(GtkWidget *w, gpointer data)
532 const char *logo_property = NULL;
533 GdkPixbuf *logo = NULL;
536 GtkWidget *image = gtk_image_new_from_file(subsurface_icon_name());
539 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
540 logo_property = "logo";
544 gtk_show_about_dialog(NULL,
545 "program-name", "SubSurface",
546 "comments", "Half-arsed divelog software in C",
548 "version", VERSION_STRING,
549 "copyright", "Linus Torvalds 2011",
550 "logo-icon-name", "subsurface",
556 static void view_list(GtkWidget *w, gpointer data)
558 gtk_paned_set_position(GTK_PANED(vpane), 0);
561 static void view_profile(GtkWidget *w, gpointer data)
563 gtk_paned_set_position(GTK_PANED(hpane), 0);
564 gtk_paned_set_position(GTK_PANED(vpane), 65535);
567 static void view_info(GtkWidget *w, gpointer data)
569 gtk_paned_set_position(GTK_PANED(vpane), 65535);
570 gtk_paned_set_position(GTK_PANED(hpane), 65535);
573 /* Ooh. I don't know how to get the half-way size. So I'm just using random numbers */
574 static void view_three(GtkWidget *w, gpointer data)
576 gtk_paned_set_position(GTK_PANED(hpane), 400);
577 gtk_paned_set_position(GTK_PANED(vpane), 200);
580 static GtkActionEntry menu_items[] = {
581 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
582 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
583 { "ViewMenuAction", GTK_STOCK_FILE, "View", NULL, NULL, NULL},
584 { "FilterMenuAction", GTK_STOCK_FILE, "Filter", NULL, NULL, NULL},
585 { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
586 { "OpenFile", GTK_STOCK_OPEN, NULL, CTRLCHAR "O", NULL, G_CALLBACK(file_open) },
587 { "SaveFile", GTK_STOCK_SAVE, NULL, CTRLCHAR "S", NULL, G_CALLBACK(file_save) },
588 { "Print", GTK_STOCK_PRINT, NULL, CTRLCHAR "P", NULL, G_CALLBACK(do_print) },
589 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
590 { "Preferences", NULL, "Preferences", PREFERENCE_ACCEL, NULL, G_CALLBACK(preferences_dialog) },
591 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
592 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
593 { "Quit", GTK_STOCK_QUIT, NULL, CTRLCHAR "Q", NULL, G_CALLBACK(quit) },
594 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
595 { "ViewList", NULL, "List", CTRLCHAR "1", NULL, G_CALLBACK(view_list) },
596 { "ViewProfile", NULL, "Profile", CTRLCHAR "2", NULL, G_CALLBACK(view_profile) },
597 { "ViewInfo", NULL, "Info", CTRLCHAR "3", NULL, G_CALLBACK(view_info) },
598 { "ViewThree", NULL, "Three", CTRLCHAR "4", NULL, G_CALLBACK(view_three) },
600 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
602 static const gchar* ui_string = " \
604 <menubar name=\"MainMenu\"> \
605 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
606 <menuitem name=\"Open\" action=\"OpenFile\" /> \
607 <menuitem name=\"Save\" action=\"SaveFile\" /> \
608 <menuitem name=\"Print\" action=\"Print\" /> \
609 <separator name=\"Separator1\"/> \
610 <menuitem name=\"Import\" action=\"Import\" /> \
611 <separator name=\"Separator2\"/> \
612 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
613 <separator name=\"Separator3\"/> \
614 <menuitem name=\"Quit\" action=\"Quit\" /> \
616 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
617 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
618 <menu name=\"View\" action=\"ViewMenuAction\"> \
619 <menuitem name=\"List\" action=\"ViewList\" /> \
620 <menuitem name=\"Profile\" action=\"ViewProfile\" /> \
621 <menuitem name=\"Info\" action=\"ViewInfo\" /> \
622 <menuitem name=\"Paned\" action=\"ViewThree\" /> \
625 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
626 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
628 <menu name=\"Help\" action=\"HelpMenuAction\"> \
629 <menuitem name=\"About\" action=\"About\" /> \
635 static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
637 GtkActionGroup *action_group = gtk_action_group_new("Menu");
638 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
640 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
642 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
644 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
645 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
650 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
655 void init_ui(int *argcp, char ***argvp)
660 GtkWidget *dive_list;
664 GtkIconTheme *icon_theme=NULL;
665 GtkSettings *settings;
666 GtkUIManager *ui_manager;
668 gtk_init(argcp, argvp);
669 settings = gtk_settings_get_default();
670 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
674 subsurface_open_conf();
675 if (subsurface_get_conf("feet", PREF_BOOL))
676 output_units.length = FEET;
677 if (subsurface_get_conf("psi", PREF_BOOL))
678 output_units.pressure = PSI;
679 if (subsurface_get_conf("cuft", PREF_BOOL))
680 output_units.volume = CUFT;
681 if (subsurface_get_conf("fahrenheit", PREF_BOOL))
682 output_units.temperature = FAHRENHEIT;
683 if (subsurface_get_conf("lbs", PREF_BOOL))
684 output_units.weight = LBS;
685 /* an unset key is FALSE - all these are hidden by default */
686 visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
687 visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
688 visible_cols.nitrox = PTR_TO_BOOL(subsurface_get_conf("NITROX", PREF_BOOL));
689 visible_cols.otu = PTR_TO_BOOL(subsurface_get_conf("OTU", PREF_BOOL));
690 visible_cols.sac = PTR_TO_BOOL(subsurface_get_conf("SAC", PREF_BOOL));
692 divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
694 error_info_bar = NULL;
695 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
696 g_set_application_name ("subsurface");
697 /* Let's check if the subsurface icon has been installed or if
698 * we need to try to load it from the current directory */
699 screen = gdk_screen_get_default();
701 icon_theme = gtk_icon_theme_get_for_screen(screen);
703 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
705 gtk_window_set_default_icon_name ("subsurface");
709 const char *icon_name = subsurface_icon_name();
710 if (!access(icon_name, R_OK))
711 gtk_window_set_icon_from_file(GTK_WINDOW(win), icon_name, NULL);
713 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
714 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
717 vbox = gtk_vbox_new(FALSE, 0);
718 gtk_container_add(GTK_CONTAINER(win), vbox);
721 ui_manager = gtk_ui_manager_new();
722 menubar = get_menubar_menu(win, ui_manager);
724 subsurface_ui_setup(settings, menubar, vbox, ui_manager);
726 vpane = gtk_vpaned_new();
727 gtk_box_pack_start(GTK_BOX(vbox), vpane, TRUE, TRUE, 3);
729 hpane = gtk_hpaned_new();
730 gtk_paned_add1(GTK_PANED(vpane), hpane);
732 /* Notebook for dive info vs profile vs .. */
733 notebook = gtk_notebook_new();
734 gtk_paned_add1(GTK_PANED(hpane), notebook);
735 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
737 /* Create the actual divelist */
738 dive_list = dive_list_create();
739 gtk_widget_set_name(dive_list, "Dive List");
740 gtk_paned_add2(GTK_PANED(vpane), dive_list);
742 /* Frame for dive profile */
743 dive_profile = dive_profile_widget();
744 gtk_widget_set_name(dive_profile, "Dive Profile");
745 gtk_paned_add2(GTK_PANED(hpane), dive_profile);
747 /* Frame for extended dive info */
748 nb_page = extended_dive_info_widget();
749 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Notes"));
751 /* Frame for dive equipment */
752 nb_page = equipment_widget();
753 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Equipment"));
755 /* Frame for single dive statistics */
756 nb_page = single_stats_widget();
757 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Dive Info"));
759 /* Frame for total dive statistics */
760 nb_page = total_stats_widget();
761 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nb_page, gtk_label_new("Stats"));
763 gtk_widget_set_app_paintable(win, TRUE);
764 gtk_widget_show_all(win);
776 subsurface_close_conf();
780 cairo_rectangle_int_t rect;
784 static tooltip_record_t *tooltip_rects;
787 void attach_tooltip(int x, int y, int w, int h, const char *text)
789 cairo_rectangle_int_t *rect;
790 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
791 rect = &tooltip_rects[tooltips].rect;
796 tooltip_rects[tooltips].text = text;
800 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
801 (_r.y <= _y) && (_r.y + _r.height >= _y))
803 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
804 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
807 cairo_rectangle_int_t *drawing_area = user_data;
808 gint tx = x - drawing_area->x; /* get transformed coordinates */
809 gint ty = y - drawing_area->y;
811 /* are we over an event marker ? */
812 for (i = 0; i < tooltips; i++) {
813 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
814 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
815 return TRUE; /* show tooltip */
818 return FALSE; /* don't show tooltip */
821 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
823 struct dive *dive = current_dive;
824 struct graphics_context gc = { .printer = 0 };
825 static cairo_rectangle_int_t drawing_area;
827 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
828 * so effective drawing area is width-2x * height-2y */
829 drawing_area.width = widget->allocation.width;
830 drawing_area.height = widget->allocation.height;
831 drawing_area.x = drawing_area.width / 20.0;
832 drawing_area.y = drawing_area.height / 20.0;
834 gc.cr = gdk_cairo_create(widget->window);
835 g_object_set(widget, "has-tooltip", TRUE, NULL);
836 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
837 init_profile_background(&gc);
843 tooltip_rects = NULL;
846 plot(&gc, &drawing_area, dive);
848 cairo_destroy(gc.cr);
853 GtkWidget *dive_profile_widget(void)
857 da = gtk_drawing_area_new();
858 gtk_widget_set_size_request(da, 350, 250);
859 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
864 int process_ui_events(void)
868 while (gtk_events_pending()) {
869 if (gtk_main_iteration_do(0)) {
878 static void fill_computer_list(GtkListStore *store)
881 struct device_list *list = device_list;
883 for (list = device_list ; list->name ; list++) {
884 gtk_list_store_append(store, &iter);
885 gtk_list_store_set(store, &iter,
892 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
894 GtkWidget *hbox, *combo_box, *frame;
896 GtkCellRenderer *renderer;
898 hbox = gtk_hbox_new(FALSE, 6);
899 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
901 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
902 fill_computer_list(model);
904 frame = gtk_frame_new("Dive computer");
905 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
907 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
908 gtk_container_add(GTK_CONTAINER(frame), combo_box);
910 renderer = gtk_cell_renderer_text_new();
911 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
912 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
914 return GTK_COMBO_BOX(combo_box);
917 static GtkEntry *dive_computer_device(GtkWidget *vbox)
919 GtkWidget *hbox, *entry, *frame;
921 hbox = gtk_hbox_new(FALSE, 6);
922 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
924 frame = gtk_frame_new("Device name");
925 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
927 entry = gtk_entry_new();
928 gtk_container_add(GTK_CONTAINER(frame), entry);
929 gtk_entry_set_text(GTK_ENTRY(entry), subsurface_USB_name());
931 return GTK_ENTRY(entry);
934 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
935 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
937 GtkDialog *main_dialog = _data;
939 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
942 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
944 GtkWidget *hbox, *frame, *chooser, *dialog;
945 GtkFileFilter *filter;
947 hbox = gtk_hbox_new(FALSE, 6);
948 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
950 frame = gtk_frame_new("XML file name");
951 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
952 dialog = gtk_file_chooser_dialog_new("Open XML File",
953 GTK_WINDOW(main_window),
954 GTK_FILE_CHOOSER_ACTION_OPEN,
955 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
956 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
958 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
960 filter = gtk_file_filter_new();
961 gtk_file_filter_add_pattern(filter, "*.xml");
962 gtk_file_filter_add_pattern(filter, "*.XML");
963 gtk_file_filter_add_pattern(filter, "*.sda");
964 gtk_file_filter_add_pattern(filter, "*.SDA");
965 gtk_file_filter_add_mime_type(filter, "text/xml");
966 gtk_file_filter_set_name(filter, "XML file");
967 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
969 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
970 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
972 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
973 gtk_container_add(GTK_CONTAINER(frame), chooser);
978 static void do_import_file(gpointer data, gpointer user_data)
980 GError *error = NULL;
981 parse_file(data, &error);
991 void import_dialog(GtkWidget *w, gpointer data)
994 GtkWidget *dialog, *hbox, *vbox, *label;
995 GtkComboBox *computer;
997 GtkWidget *XMLchooser;
998 device_data_t devicedata = {
1002 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1003 GTK_WINDOW(main_window),
1004 GTK_DIALOG_DESTROY_WITH_PARENT,
1005 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1006 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1009 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1010 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1011 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1012 XMLchooser = xml_file_selector(vbox, dialog);
1013 computer = dive_computer_selector(vbox);
1014 device = dive_computer_device(vbox);
1015 hbox = gtk_hbox_new(FALSE, 6);
1016 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1017 devicedata.progress.bar = gtk_progress_bar_new();
1018 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1020 gtk_widget_show_all(dialog);
1021 result = gtk_dialog_run(GTK_DIALOG(dialog));
1025 GtkTreeModel *model;
1028 case GTK_RESPONSE_ACCEPT:
1029 /* what happened - did the user pick a file? In that case
1030 * we ignore whether a dive computer model was picked */
1031 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1032 if (g_slist_length(list) == 0) {
1033 if (!gtk_combo_box_get_active_iter(computer, &iter))
1035 model = gtk_combo_box_get_model(computer);
1036 gtk_tree_model_get(model, &iter,
1040 devicedata.type = type;
1041 devicedata.name = comp;
1042 devicedata.devname = gtk_entry_get_text(device);
1043 do_import(&devicedata);
1045 g_slist_foreach(list,do_import_file,NULL);
1052 gtk_widget_destroy(dialog);
1057 void update_progressbar(progressbar_t *progress, double value)
1059 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1063 void set_filename(const char *filename)
1065 if (!existing_filename && filename)
1066 existing_filename = strdup(filename);