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
11 #include <gconf/gconf-client.h>
16 #include "display-gtk.h"
18 #include "libdivecomputer.h"
20 GtkWidget *main_window;
22 GtkWidget *error_info_bar;
23 GtkWidget *error_label;
26 #define DIVELIST_DEFAULT_FONT "Sans 8"
27 const char *divelist_font;
30 struct units output_units;
32 #define GCONF_NAME(x) "/apps/subsurface/" #x
34 static GtkWidget *dive_profile;
36 visible_cols_t visible_cols = {TRUE, FALSE};
38 void repaint_dive(void)
40 update_dive(current_dive);
42 gtk_widget_queue_draw(dive_profile);
45 static char *existing_filename;
46 static gboolean need_icon = TRUE;
48 static void on_info_bar_response(GtkWidget *widget, gint response,
51 if (response == GTK_RESPONSE_OK)
53 gtk_widget_destroy(widget);
54 error_info_bar = NULL;
58 void report_error(GError* error)
65 if (error_info_bar == NULL)
68 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
71 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
72 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
75 error_label = gtk_label_new(error->message);
76 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
77 gtk_container_add(GTK_CONTAINER(container), error_label);
79 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
80 gtk_widget_show_all(main_vbox);
86 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
87 gtk_label_set(GTK_LABEL(error_label), buffer);
91 static void file_open(GtkWidget *w, gpointer data)
94 GtkFileFilter *filter;
96 dialog = gtk_file_chooser_dialog_new("Open File",
97 GTK_WINDOW(main_window),
98 GTK_FILE_CHOOSER_ACTION_OPEN,
99 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
100 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
102 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
104 filter = gtk_file_filter_new();
105 gtk_file_filter_add_pattern(filter, "*.xml");
106 gtk_file_filter_add_pattern(filter, "*.XML");
107 gtk_file_filter_add_pattern(filter, "*.sda");
108 gtk_file_filter_add_pattern(filter, "*.SDA");
109 gtk_file_filter_add_mime_type(filter, "text/xml");
110 gtk_file_filter_set_name(filter, "XML file");
111 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
113 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
116 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
118 GError *error = NULL;
119 while(filenames != NULL) {
120 filename = filenames->data;
121 parse_xml_file(filename, &error);
130 filenames = g_slist_next(filenames);
132 g_slist_free(filenames);
135 gtk_widget_destroy(dialog);
138 static void file_save(GtkWidget *w, gpointer data)
141 dialog = gtk_file_chooser_dialog_new("Save File",
142 GTK_WINDOW(main_window),
143 GTK_FILE_CHOOSER_ACTION_SAVE,
144 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
145 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
147 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
148 if (!existing_filename) {
149 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
151 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
153 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
155 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
156 save_dives(filename);
158 mark_divelist_changed(FALSE);
160 gtk_widget_destroy(dialog);
163 static void ask_save_changes()
165 GtkWidget *dialog, *label, *content;
166 dialog = gtk_dialog_new_with_buttons("Save Changes?",
167 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
168 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
169 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
171 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
172 label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
173 gtk_container_add (GTK_CONTAINER (content), label);
174 gtk_widget_show_all (dialog);
175 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
176 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
177 file_save(NULL,NULL);
179 gtk_widget_destroy(dialog);
182 static gboolean on_delete(GtkWidget* w, gpointer data)
184 /* Make sure to flush any modified dive data */
187 if (unsaved_changes())
190 return FALSE; /* go ahead, kill the program, we're good now */
193 static void on_destroy(GtkWidget* w, gpointer data)
198 static void quit(GtkWidget *w, gpointer data)
200 /* Make sure to flush any modified dive data */
203 if (unsaved_changes())
208 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
209 data_func_t data_func, PangoAlignment align, gboolean visible)
211 GtkCellRenderer *renderer;
212 GtkTreeViewColumn *col;
213 double xalign = 0.0; /* left as default */
215 renderer = gtk_cell_renderer_text_new();
216 col = gtk_tree_view_column_new();
218 gtk_tree_view_column_set_title(col, title);
219 gtk_tree_view_column_set_sort_column_id(col, index);
220 gtk_tree_view_column_set_resizable(col, TRUE);
221 gtk_tree_view_column_pack_start(col, renderer, TRUE);
223 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
225 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
226 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
228 case PANGO_ALIGN_LEFT:
231 case PANGO_ALIGN_CENTER:
234 case PANGO_ALIGN_RIGHT:
238 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
239 gtk_tree_view_column_set_visible(col, visible);
240 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
244 static void create_radio(GtkWidget *vbox, const char *name, ...)
247 GtkRadioButton *group = NULL;
248 GtkWidget *box, *label;
250 box = gtk_hbox_new(TRUE, 10);
251 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
253 label = gtk_label_new(name);
254 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
256 va_start(args, name);
263 name = va_arg(args, char *);
266 callback_fn = va_arg(args, void *);
267 enabled = va_arg(args, int);
269 button = gtk_radio_button_new_with_label_from_widget(group, name);
270 group = GTK_RADIO_BUTTON(button);
271 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
272 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
273 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
278 #define UNITCALLBACK(name, type, value) \
279 static void name(GtkWidget *w, gpointer data) \
281 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
282 menu_units.type = value; \
285 static struct units menu_units;
287 UNITCALLBACK(set_meter, length, METERS)
288 UNITCALLBACK(set_feet, length, FEET)
289 UNITCALLBACK(set_bar, pressure, BAR)
290 UNITCALLBACK(set_psi, pressure, PSI)
291 UNITCALLBACK(set_liter, volume, LITER)
292 UNITCALLBACK(set_cuft, volume, CUFT)
293 UNITCALLBACK(set_celsius, temperature, CELSIUS)
294 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
296 #define OPTIONCALLBACK(name, option) \
297 static void name(GtkWidget *w, gpointer data) \
299 option = GTK_TOGGLE_BUTTON(w)->active; \
302 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
303 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
304 OPTIONCALLBACK(nitrox_toggle, visible_cols.nitrox)
305 OPTIONCALLBACK(temperature_toggle, visible_cols.temperature)
306 OPTIONCALLBACK(cylinder_toggle, visible_cols.cylinder)
308 static void event_toggle(GtkWidget *w, gpointer _data)
310 gboolean *plot_ev = _data;
312 *plot_ev = GTK_TOGGLE_BUTTON(w)->active;
315 static void preferences_dialog(GtkWidget *w, gpointer data)
318 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
320 menu_units = output_units;
322 dialog = gtk_dialog_new_with_buttons("Preferences",
323 GTK_WINDOW(main_window),
324 GTK_DIALOG_DESTROY_WITH_PARENT,
325 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
326 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
329 frame = gtk_frame_new("Units");
330 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
331 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
333 box = gtk_vbox_new(FALSE, 6);
334 gtk_container_add(GTK_CONTAINER(frame), box);
336 create_radio(box, "Depth:",
337 "Meter", set_meter, (output_units.length == METERS),
338 "Feet", set_feet, (output_units.length == FEET),
341 create_radio(box, "Pressure:",
342 "Bar", set_bar, (output_units.pressure == BAR),
343 "PSI", set_psi, (output_units.pressure == PSI),
346 create_radio(box, "Volume:",
347 "Liter", set_liter, (output_units.volume == LITER),
348 "CuFt", set_cuft, (output_units.volume == CUFT),
351 create_radio(box, "Temperature:",
352 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
353 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
356 frame = gtk_frame_new("Columns");
357 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
359 box = gtk_hbox_new(FALSE, 6);
360 gtk_container_add(GTK_CONTAINER(frame), box);
362 button = gtk_check_button_new_with_label("Show Temp");
363 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.temperature);
364 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
365 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(temperature_toggle), NULL);
367 button = gtk_check_button_new_with_label("Show Cyl");
368 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.cylinder);
369 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
370 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(cylinder_toggle), NULL);
372 button = gtk_check_button_new_with_label("Show O" UTF8_SUBSCRIPT_2 "%");
373 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.nitrox);
374 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
375 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(nitrox_toggle), NULL);
377 button = gtk_check_button_new_with_label("Show SAC");
378 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
379 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
380 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
382 button = gtk_check_button_new_with_label("Show OTU");
383 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
384 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
385 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
387 font = gtk_font_button_new_with_font(divelist_font);
388 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
390 gtk_widget_show_all(dialog);
391 result = gtk_dialog_run(GTK_DIALOG(dialog));
392 if (result == GTK_RESPONSE_ACCEPT) {
393 /* Make sure to flush any modified old dive data with old units */
396 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
397 set_divelist_font(divelist_font);
399 output_units = menu_units;
400 update_dive_list_units();
402 update_dive_list_col_visibility();
403 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
404 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
405 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
406 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
407 gconf_client_set_bool(gconf, GCONF_NAME(TEMPERATURE), visible_cols.temperature, NULL);
408 gconf_client_set_bool(gconf, GCONF_NAME(CYLINDER), visible_cols.cylinder, NULL);
409 gconf_client_set_bool(gconf, GCONF_NAME(NITROX), visible_cols.nitrox, NULL);
410 gconf_client_set_bool(gconf, GCONF_NAME(SAC), visible_cols.sac, NULL);
411 gconf_client_set_bool(gconf, GCONF_NAME(OTU), visible_cols.otu, NULL);
412 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
414 gtk_widget_destroy(dialog);
417 static void create_toggle(const char* label, int *on, void *_data)
419 GtkWidget *button, *table = _data;
420 int rows, cols, x, y;
424 /* magic way to reset the number of toggle buttons
425 * that we have already added - call this before you
426 * create the dialog */
430 g_object_get(G_OBJECT(table), "n-columns", &cols, "n-rows", &rows, NULL);
431 if (count > rows * cols) {
432 gtk_table_resize(GTK_TABLE(table),rows+1,cols);
437 button = gtk_check_button_new_with_label(label);
438 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *on);
439 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
440 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(event_toggle), on);
444 static void selectevents_dialog(GtkWidget *w, gpointer data)
447 GtkWidget *dialog, *frame, *vbox, *table;
449 dialog = gtk_dialog_new_with_buttons("SelectEvents",
450 GTK_WINDOW(main_window),
451 GTK_DIALOG_DESTROY_WITH_PARENT,
452 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
453 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
455 /* initialize the function that fills the table */
456 create_toggle(NULL, NULL, NULL);
458 frame = gtk_frame_new("Enable / Disable Events");
459 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
460 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
462 table = gtk_table_new(1, 4, TRUE);
463 gtk_container_add(GTK_CONTAINER(frame), table);
465 evn_foreach(&create_toggle, table);
467 gtk_widget_show_all(dialog);
468 result = gtk_dialog_run(GTK_DIALOG(dialog));
469 if (result == GTK_RESPONSE_ACCEPT) {
472 gtk_widget_destroy(dialog);
475 static void renumber_dialog(GtkWidget *w, gpointer data)
479 GtkWidget *dialog, *frame, *button, *vbox;
481 dialog = gtk_dialog_new_with_buttons("Renumber",
482 GTK_WINDOW(main_window),
483 GTK_DIALOG_DESTROY_WITH_PARENT,
484 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
485 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
488 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
490 frame = gtk_frame_new("New starting number");
491 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
493 button = gtk_spin_button_new_with_range(1, 50000, 1);
494 gtk_container_add(GTK_CONTAINER(frame), button);
497 * Do we have a number for the first dive already? Use that
501 if (dive && dive->number)
502 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
504 gtk_widget_show_all(dialog);
505 result = gtk_dialog_run(GTK_DIALOG(dialog));
506 if (result == GTK_RESPONSE_ACCEPT) {
507 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
511 gtk_widget_destroy(dialog);
514 static void about_dialog(GtkWidget *w, gpointer data)
516 const char *logo_property = NULL;
517 GdkPixbuf *logo = NULL;
520 GtkWidget *image = gtk_image_new_from_file("subsurface.svg");
523 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
524 logo_property = "logo";
528 gtk_show_about_dialog(NULL,
529 "program-name", "SubSurface",
530 "comments", "Half-arsed divelog software in C",
532 "version", VERSION_STRING,
533 "copyright", "Linus Torvalds 2011",
534 "logo-icon-name", "subsurface",
540 static GtkActionEntry menu_items[] = {
541 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
542 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
543 { "FilterMenuAction", GTK_STOCK_FILE, "Filter", NULL, NULL, NULL},
544 { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
545 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
546 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
547 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
548 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
549 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
550 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
551 { "SelectEvents", NULL, "SelectEvents", NULL, NULL, G_CALLBACK(selectevents_dialog) },
552 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
553 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
555 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
557 static const gchar* ui_string = " \
559 <menubar name=\"MainMenu\"> \
560 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
561 <menuitem name=\"Open\" action=\"OpenFile\" /> \
562 <menuitem name=\"Save\" action=\"SaveFile\" /> \
563 <menuitem name=\"Print\" action=\"Print\" /> \
564 <separator name=\"Separator1\"/> \
565 <menuitem name=\"Import\" action=\"Import\" /> \
566 <separator name=\"Separator2\"/> \
567 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
568 <separator name=\"Separator3\"/> \
569 <menuitem name=\"Quit\" action=\"Quit\" /> \
571 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
572 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
574 <menu name=\"FilterMenu\" action=\"FilterMenuAction\"> \
575 <menuitem name=\"SelectEvents\" action=\"SelectEvents\" /> \
577 <menu name=\"Help\" action=\"HelpMenuAction\"> \
578 <menuitem name=\"About\" action=\"About\" /> \
584 static GtkWidget *get_menubar_menu(GtkWidget *window)
586 GtkActionGroup *action_group = gtk_action_group_new("Menu");
587 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
589 GtkUIManager *ui_manager = gtk_ui_manager_new();
590 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
592 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
594 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
595 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
600 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
605 static const char notebook_group[] = "123";
606 #define GRP_ID ((void *)notebook_group)
611 gulong delete_handler;
612 gulong destroy_handler;
615 static notebook_data_t nbd[2]; /* we rip at most two notebook pages off */
617 static GtkNotebook *create_new_notebook_window(GtkNotebook *source,
619 gint x, gint y, gpointer data)
621 GtkWidget *win, *notebook, *vbox;
622 notebook_data_t *nbdp;
623 GtkAllocation allocation;
625 /* pick the right notebook page data and return if both are detached */
626 if (nbd[0].widget == NULL)
628 else if (nbd[1].widget == NULL)
633 nbdp->name = strdup(gtk_widget_get_name(page));
634 nbdp->widget = win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
635 gtk_window_set_title(GTK_WINDOW(win), nbdp->name);
636 gtk_window_move(GTK_WINDOW(win), x, y);
638 /* Destroying the dive list will kill the application */
639 nbdp->delete_handler = g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
640 nbdp->destroy_handler = g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
642 nbdp->box = vbox = gtk_vbox_new(FALSE, 0);
643 gtk_container_add(GTK_CONTAINER(win), vbox);
645 notebook = gtk_notebook_new();
646 gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
647 gtk_widget_set_name(notebook, nbdp->name);
648 /* disallow drop events */
649 gtk_drag_dest_set(notebook, 0, NULL, 0, 0);
650 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
651 gtk_widget_get_allocation(page, &allocation);
652 gtk_window_set_default_size(GTK_WINDOW(win), allocation.width, allocation.height);
654 gtk_widget_show_all(win);
655 return GTK_NOTEBOOK(notebook);
658 static void drag_cb(GtkWidget *widget, GdkDragContext *context,
659 gint x, gint y, guint time,
663 notebook_data_t *nbdp;
665 source = gtk_drag_get_source_widget(context);
666 if (nbd[0].name && ! strcmp(nbd[0].name,gtk_widget_get_name(source)))
668 else if (nbd[1].name && ! strcmp(nbd[1].name,gtk_widget_get_name(source)))
674 gtk_drag_finish(context, TRUE, TRUE, time);
676 /* we no longer need the widget - but getting rid of this is hard;
677 * remove the signal handler, remove the notebook from the box
678 * then destroy the widget (and clear out our data structure) */
679 g_signal_handler_disconnect(nbdp->widget,nbdp->delete_handler);
680 g_signal_handler_disconnect(nbdp->widget,nbdp->destroy_handler);
681 gtk_container_remove(GTK_CONTAINER(nbdp->box), source);
682 gtk_widget_destroy(nbdp->widget);
688 void init_ui(int argc, char **argv)
692 GtkWidget *dive_info;
693 GtkWidget *dive_list;
694 GtkWidget *equipment;
698 GtkIconTheme *icon_theme=NULL;
699 GtkSettings *settings;
700 static const GtkTargetEntry notebook_target = {
701 "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0
704 gtk_init(&argc, &argv);
705 settings = gtk_settings_get_default();
706 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
709 gconf = gconf_client_get_default();
711 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
712 output_units.length = FEET;
713 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
714 output_units.pressure = PSI;
715 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
716 output_units.volume = CUFT;
717 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
718 output_units.temperature = FAHRENHEIT;
719 /* an unset key is FALSE - all these are hidden by default */
720 visible_cols.cylinder = gconf_client_get_bool(gconf, GCONF_NAME(CYLINDER), NULL);
721 visible_cols.temperature = gconf_client_get_bool(gconf, GCONF_NAME(TEMPERATURE), NULL);
722 visible_cols.nitrox = gconf_client_get_bool(gconf, GCONF_NAME(NITROX), NULL);
723 visible_cols.otu = gconf_client_get_bool(gconf, GCONF_NAME(OTU), NULL);
724 visible_cols.sac = gconf_client_get_bool(gconf, GCONF_NAME(SAC), NULL);
726 divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
728 divelist_font = DIVELIST_DEFAULT_FONT;
730 error_info_bar = NULL;
731 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
732 g_set_application_name ("subsurface");
733 /* Let's check if the subsurface icon has been installed or if
734 * we need to try to load it from the current directory */
735 screen = gdk_screen_get_default();
737 icon_theme = gtk_icon_theme_get_for_screen(screen);
739 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
741 gtk_window_set_default_icon_name ("subsurface");
745 gtk_window_set_icon_from_file(GTK_WINDOW(win), "subsurface.svg", NULL);
746 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
747 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
750 vbox = gtk_vbox_new(FALSE, 0);
751 gtk_container_add(GTK_CONTAINER(win), vbox);
754 menubar = get_menubar_menu(win);
755 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
757 /* Notebook for dive info vs profile vs .. */
758 notebook = gtk_notebook_new();
759 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
760 gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
761 g_signal_connect(notebook, "create-window", G_CALLBACK(create_new_notebook_window), NULL);
762 gtk_drag_dest_set(notebook, GTK_DEST_DEFAULT_ALL, ¬ebook_target, 1, GDK_ACTION_MOVE);
763 g_signal_connect(notebook, "drag-drop", G_CALLBACK(drag_cb), notebook);
764 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
766 /* Create the actual divelist */
767 dive_list = dive_list_create();
768 gtk_widget_set_name(dive_list, "Dive List");
769 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_list, gtk_label_new("Dive List"));
770 gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_list, 1);
771 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_list, 1);
773 /* Frame for dive profile */
774 dive_profile = dive_profile_widget();
775 gtk_widget_set_name(dive_profile, "Dive Profile");
776 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
777 gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_profile, 1);
778 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_profile, 1);
780 /* Frame for extended dive info */
781 dive_info = extended_dive_info_widget();
782 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
784 /* Frame for dive equipment */
785 equipment = equipment_widget();
786 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
788 gtk_widget_set_app_paintable(win, TRUE);
789 gtk_widget_show_all(win);
800 cairo_rectangle_int_t rect;
804 static tooltip_record_t *tooltip_rects;
807 void attach_tooltip(int x, int y, int w, int h, const char *text)
809 cairo_rectangle_int_t *rect;
810 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
811 rect = &tooltip_rects[tooltips].rect;
816 tooltip_rects[tooltips].text = text;
820 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
821 (_r.y <= _y) && (_r.y + _r.height >= _y))
823 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
824 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
827 cairo_rectangle_int_t *drawing_area = user_data;
828 gint tx = x - drawing_area->x; /* get transformed coordinates */
829 gint ty = y - drawing_area->y;
831 /* are we over an event marker ? */
832 for (i = 0; i < tooltips; i++) {
833 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
834 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
835 return TRUE; /* show tooltip */
838 return FALSE; /* don't show tooltip */
841 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
843 struct dive *dive = current_dive;
844 struct graphics_context gc = { .printer = 0 };
845 static cairo_rectangle_int_t drawing_area;
847 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
848 * so effective drawing area is width-2x * height-2y */
849 drawing_area.width = widget->allocation.width;
850 drawing_area.height = widget->allocation.height;
851 drawing_area.x = drawing_area.width / 20.0;
852 drawing_area.y = drawing_area.height / 20.0;
854 gc.cr = gdk_cairo_create(widget->window);
855 g_object_set(widget, "has-tooltip", TRUE, NULL);
856 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
857 set_source_rgb(&gc, 0, 0, 0);
863 tooltip_rects = NULL;
866 plot(&gc, &drawing_area, dive);
868 cairo_destroy(gc.cr);
873 GtkWidget *dive_profile_widget(void)
877 da = gtk_drawing_area_new();
878 gtk_widget_set_size_request(da, 350, 250);
879 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
884 int process_ui_events(void)
888 while (gtk_events_pending()) {
889 if (gtk_main_iteration_do(0)) {
898 static void fill_computer_list(GtkListStore *store)
901 struct device_list *list = device_list;
903 for (list = device_list ; list->name ; list++) {
904 gtk_list_store_append(store, &iter);
905 gtk_list_store_set(store, &iter,
912 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
914 GtkWidget *hbox, *combo_box, *frame;
916 GtkCellRenderer *renderer;
918 hbox = gtk_hbox_new(FALSE, 6);
919 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
921 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
922 fill_computer_list(model);
924 frame = gtk_frame_new("Dive computer");
925 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
927 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
928 gtk_container_add(GTK_CONTAINER(frame), combo_box);
930 renderer = gtk_cell_renderer_text_new();
931 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
932 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
934 return GTK_COMBO_BOX(combo_box);
937 static GtkEntry *dive_computer_device(GtkWidget *vbox)
939 GtkWidget *hbox, *entry, *frame;
941 hbox = gtk_hbox_new(FALSE, 6);
942 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
944 frame = gtk_frame_new("Device name");
945 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
947 entry = gtk_entry_new();
948 gtk_container_add(GTK_CONTAINER(frame), entry);
949 gtk_entry_set_text(GTK_ENTRY(entry), "/dev/ttyUSB0");
951 return GTK_ENTRY(entry);
954 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
955 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
957 GtkDialog *main_dialog = _data;
959 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
962 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
964 GtkWidget *hbox, *frame, *chooser, *dialog;
965 GtkFileFilter *filter;
967 hbox = gtk_hbox_new(FALSE, 6);
968 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
970 frame = gtk_frame_new("XML file name");
971 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
972 dialog = gtk_file_chooser_dialog_new("Open XML File",
973 GTK_WINDOW(main_window),
974 GTK_FILE_CHOOSER_ACTION_OPEN,
975 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
976 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
978 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
980 filter = gtk_file_filter_new();
981 gtk_file_filter_add_pattern(filter, "*.xml");
982 gtk_file_filter_add_pattern(filter, "*.XML");
983 gtk_file_filter_add_pattern(filter, "*.sda");
984 gtk_file_filter_add_pattern(filter, "*.SDA");
985 gtk_file_filter_add_mime_type(filter, "text/xml");
986 gtk_file_filter_set_name(filter, "XML file");
987 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
989 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
990 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
992 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
993 gtk_container_add(GTK_CONTAINER(frame), chooser);
998 static void do_import_file(gpointer data, gpointer user_data)
1000 GError *error = NULL;
1001 parse_xml_file(data, &error);
1005 report_error(error);
1006 g_error_free(error);
1011 void import_dialog(GtkWidget *w, gpointer data)
1014 GtkWidget *dialog, *hbox, *vbox, *label;
1015 GtkComboBox *computer;
1017 GtkWidget *XMLchooser;
1018 device_data_t devicedata = {
1022 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
1023 GTK_WINDOW(main_window),
1024 GTK_DIALOG_DESTROY_WITH_PARENT,
1025 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1026 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1029 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1030 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
1031 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
1032 XMLchooser = xml_file_selector(vbox, dialog);
1033 computer = dive_computer_selector(vbox);
1034 device = dive_computer_device(vbox);
1035 hbox = gtk_hbox_new(FALSE, 6);
1036 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
1037 devicedata.progress.bar = gtk_progress_bar_new();
1038 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
1040 gtk_widget_show_all(dialog);
1041 result = gtk_dialog_run(GTK_DIALOG(dialog));
1045 GtkTreeModel *model;
1048 case GTK_RESPONSE_ACCEPT:
1049 /* what happened - did the user pick a file? In that case
1050 * we ignore whether a dive computer model was picked */
1051 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
1052 if (g_slist_length(list) == 0) {
1053 if (!gtk_combo_box_get_active_iter(computer, &iter))
1055 model = gtk_combo_box_get_model(computer);
1056 gtk_tree_model_get(model, &iter,
1060 devicedata.type = type;
1061 devicedata.name = comp;
1062 devicedata.devname = gtk_entry_get_text(device);
1063 do_import(&devicedata);
1065 g_slist_foreach(list,do_import_file,NULL);
1072 gtk_widget_destroy(dialog);
1077 void update_progressbar(progressbar_t *progress, double value)
1079 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
1083 void set_filename(const char *filename)
1085 if (!existing_filename && filename)
1086 existing_filename = strdup(filename);