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)
305 static void preferences_dialog(GtkWidget *w, gpointer data)
308 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
310 menu_units = output_units;
312 dialog = gtk_dialog_new_with_buttons("Preferences",
313 GTK_WINDOW(main_window),
314 GTK_DIALOG_DESTROY_WITH_PARENT,
315 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
316 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
319 frame = gtk_frame_new("Units");
320 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
321 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
323 box = gtk_vbox_new(FALSE, 6);
324 gtk_container_add(GTK_CONTAINER(frame), box);
326 create_radio(box, "Depth:",
327 "Meter", set_meter, (output_units.length == METERS),
328 "Feet", set_feet, (output_units.length == FEET),
331 create_radio(box, "Pressure:",
332 "Bar", set_bar, (output_units.pressure == BAR),
333 "PSI", set_psi, (output_units.pressure == PSI),
336 create_radio(box, "Volume:",
337 "Liter", set_liter, (output_units.volume == LITER),
338 "CuFt", set_cuft, (output_units.volume == CUFT),
341 create_radio(box, "Temperature:",
342 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
343 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
346 frame = gtk_frame_new("Columns");
347 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
349 box = gtk_hbox_new(FALSE, 6);
350 gtk_container_add(GTK_CONTAINER(frame), box);
352 button = gtk_check_button_new_with_label("Show SAC");
353 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
354 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
355 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
357 button = gtk_check_button_new_with_label("Show OTU");
358 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
359 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
360 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
362 font = gtk_font_button_new_with_font(divelist_font);
363 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
365 gtk_widget_show_all(dialog);
366 result = gtk_dialog_run(GTK_DIALOG(dialog));
367 if (result == GTK_RESPONSE_ACCEPT) {
368 /* Make sure to flush any modified old dive data with old units */
371 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
372 set_divelist_font(divelist_font);
374 output_units = menu_units;
375 update_dive_list_units();
377 update_dive_list_col_visibility();
378 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
379 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
380 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
381 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
382 gconf_client_set_bool(gconf, GCONF_NAME(SAC), ! visible_cols.sac, NULL); /* inverted to get the correct default */
383 gconf_client_set_bool(gconf, GCONF_NAME(OTU), visible_cols.otu, NULL);
384 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
386 gtk_widget_destroy(dialog);
389 static void renumber_dialog(GtkWidget *w, gpointer data)
393 GtkWidget *dialog, *frame, *button, *vbox;
395 dialog = gtk_dialog_new_with_buttons("Renumber",
396 GTK_WINDOW(main_window),
397 GTK_DIALOG_DESTROY_WITH_PARENT,
398 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
399 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
402 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
404 frame = gtk_frame_new("New starting number");
405 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
407 button = gtk_spin_button_new_with_range(1, 50000, 1);
408 gtk_container_add(GTK_CONTAINER(frame), button);
411 * Do we have a number for the first dive already? Use that
415 if (dive && dive->number)
416 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), dive->number);
418 gtk_widget_show_all(dialog);
419 result = gtk_dialog_run(GTK_DIALOG(dialog));
420 if (result == GTK_RESPONSE_ACCEPT) {
421 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
425 gtk_widget_destroy(dialog);
428 static void about_dialog(GtkWidget *w, gpointer data)
430 const char *logo_property = NULL;
431 GdkPixbuf *logo = NULL;
434 GtkWidget *image = gtk_image_new_from_file("subsurface.svg");
437 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
438 logo_property = "logo";
442 gtk_show_about_dialog(NULL,
443 "program-name", "SubSurface",
444 "comments", "Half-arsed divelog software in C",
446 "version", VERSION_STRING,
447 "copyright", "Linus Torvalds 2011",
448 "logo-icon-name", "subsurface",
454 static GtkActionEntry menu_items[] = {
455 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
456 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
457 { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
458 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
459 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
460 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
461 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
462 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
463 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
464 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
465 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
467 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
469 static const gchar* ui_string = " \
471 <menubar name=\"MainMenu\"> \
472 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
473 <menuitem name=\"Open\" action=\"OpenFile\" /> \
474 <menuitem name=\"Save\" action=\"SaveFile\" /> \
475 <menuitem name=\"Print\" action=\"Print\" /> \
476 <separator name=\"Separator1\"/> \
477 <menuitem name=\"Import\" action=\"Import\" /> \
478 <separator name=\"Separator2\"/> \
479 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
480 <separator name=\"Separator3\"/> \
481 <menuitem name=\"Quit\" action=\"Quit\" /> \
483 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
484 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
486 <menu name=\"Help\" action=\"HelpMenuAction\"> \
487 <menuitem name=\"About\" action=\"About\" /> \
493 static GtkWidget *get_menubar_menu(GtkWidget *window)
495 GtkActionGroup *action_group = gtk_action_group_new("Menu");
496 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
498 GtkUIManager *ui_manager = gtk_ui_manager_new();
499 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
501 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
503 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
504 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
509 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
514 static const char notebook_group[] = "123";
515 #define GRP_ID ((void *)notebook_group)
520 gulong delete_handler;
521 gulong destroy_handler;
524 static notebook_data_t nbd[2]; /* we rip at most two notebook pages off */
526 static GtkNotebook *create_new_notebook_window(GtkNotebook *source,
528 gint x, gint y, gpointer data)
530 GtkWidget *win, *notebook, *vbox;
531 notebook_data_t *nbdp;
532 GtkAllocation allocation;
534 /* pick the right notebook page data and return if both are detached */
535 if (nbd[0].widget == NULL)
537 else if (nbd[1].widget == NULL)
542 nbdp->name = strdup(gtk_widget_get_name(page));
543 nbdp->widget = win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
544 gtk_window_set_title(GTK_WINDOW(win), nbdp->name);
545 gtk_window_move(GTK_WINDOW(win), x, y);
547 /* Destroying the dive list will kill the application */
548 nbdp->delete_handler = g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
549 nbdp->destroy_handler = g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
551 nbdp->box = vbox = gtk_vbox_new(FALSE, 0);
552 gtk_container_add(GTK_CONTAINER(win), vbox);
554 notebook = gtk_notebook_new();
555 gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
556 gtk_widget_set_name(notebook, nbdp->name);
557 /* disallow drop events */
558 gtk_drag_dest_set(notebook, 0, NULL, 0, 0);
559 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
560 gtk_widget_get_allocation(page, &allocation);
561 gtk_widget_set_size_request(notebook, allocation.width, allocation.height);
563 gtk_widget_show_all(win);
564 return GTK_NOTEBOOK(notebook);
567 static void drag_cb(GtkWidget *widget, GdkDragContext *context,
568 gint x, gint y, guint time,
572 notebook_data_t *nbdp;
574 source = gtk_drag_get_source_widget(context);
575 if (nbd[0].name && ! strcmp(nbd[0].name,gtk_widget_get_name(source)))
577 else if (nbd[1].name && ! strcmp(nbd[1].name,gtk_widget_get_name(source)))
583 gtk_drag_finish(context, TRUE, TRUE, time);
585 /* we no longer need the widget - but getting rid of this is hard;
586 * remove the signal handler, remove the notebook from the box
587 * then destroy the widget (and clear out our data structure) */
588 g_signal_handler_disconnect(nbdp->widget,nbdp->delete_handler);
589 g_signal_handler_disconnect(nbdp->widget,nbdp->destroy_handler);
590 gtk_container_remove(GTK_CONTAINER(nbdp->box), source);
591 gtk_widget_destroy(nbdp->widget);
597 void init_ui(int argc, char **argv)
601 GtkWidget *dive_info;
602 GtkWidget *dive_list;
603 GtkWidget *equipment;
607 GtkIconTheme *icon_theme=NULL;
608 GtkSettings *settings;
609 static const GtkTargetEntry notebook_target = {
610 "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0
613 gtk_init(&argc, &argv);
614 settings = gtk_settings_get_default();
615 gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "subsurface setting");
618 gconf = gconf_client_get_default();
620 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
621 output_units.length = FEET;
622 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
623 output_units.pressure = PSI;
624 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
625 output_units.volume = CUFT;
626 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
627 output_units.temperature = FAHRENHEIT;
628 /* an unset key is FALSE - so in order to get the default behavior right we
629 invert the meaning of the SAC key */
630 visible_cols.otu = gconf_client_get_bool(gconf, GCONF_NAME(OTU), NULL);
631 visible_cols.sac = ! gconf_client_get_bool(gconf, GCONF_NAME(SAC), NULL);
633 divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
635 divelist_font = DIVELIST_DEFAULT_FONT;
637 error_info_bar = NULL;
638 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
639 g_set_application_name ("subsurface");
640 /* Let's check if the subsurface icon has been installed or if
641 * we need to try to load it from the current directory */
642 screen = gdk_screen_get_default();
644 icon_theme = gtk_icon_theme_get_for_screen(screen);
646 if (gtk_icon_theme_has_icon(icon_theme, "subsurface")) {
648 gtk_window_set_default_icon_name ("subsurface");
652 gtk_window_set_icon_from_file(GTK_WINDOW(win), "subsurface.svg", NULL);
653 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
654 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
657 vbox = gtk_vbox_new(FALSE, 0);
658 gtk_container_add(GTK_CONTAINER(win), vbox);
661 menubar = get_menubar_menu(win);
662 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
664 /* Notebook for dive info vs profile vs .. */
665 notebook = gtk_notebook_new();
666 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
667 gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
668 g_signal_connect(notebook, "create-window", G_CALLBACK(create_new_notebook_window), NULL);
669 gtk_drag_dest_set(notebook, GTK_DEST_DEFAULT_ALL, ¬ebook_target, 1, GDK_ACTION_MOVE);
670 g_signal_connect(notebook, "drag-drop", G_CALLBACK(drag_cb), notebook);
671 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
673 /* Create the actual divelist */
674 dive_list = dive_list_create();
675 gtk_widget_set_name(dive_list, "Dive List");
676 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_list, gtk_label_new("Dive List"));
677 gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_list, 1);
678 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_list, 1);
680 /* Frame for dive profile */
681 dive_profile = dive_profile_widget();
682 gtk_widget_set_name(dive_profile, "Dive Profile");
683 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
684 gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_profile, 1);
685 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_profile, 1);
687 /* Frame for extended dive info */
688 dive_info = extended_dive_info_widget();
689 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
691 /* Frame for dive equipment */
692 equipment = equipment_widget();
693 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
695 gtk_widget_set_app_paintable(win, TRUE);
696 gtk_widget_show_all(win);
707 cairo_rectangle_int_t rect;
711 static tooltip_record_t *tooltip_rects;
714 void attach_tooltip(int x, int y, int w, int h, const char *text)
716 cairo_rectangle_int_t *rect;
717 tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t));
718 rect = &tooltip_rects[tooltips].rect;
723 tooltip_rects[tooltips].text = text;
727 #define INSIDE_RECT(_r,_x,_y) ((_r.x <= _x) && (_r.x + _r.width >= _x) && \
728 (_r.y <= _y) && (_r.y + _r.height >= _y))
730 static gboolean profile_tooltip (GtkWidget *widget, gint x, gint y,
731 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
734 cairo_rectangle_int_t *drawing_area = user_data;
735 gint tx = x - drawing_area->x; /* get transformed coordinates */
736 gint ty = y - drawing_area->y;
738 /* are we over an event marker ? */
739 for (i = 0; i < tooltips; i++) {
740 if (INSIDE_RECT(tooltip_rects[i].rect, tx, ty)) {
741 gtk_tooltip_set_text(tooltip,tooltip_rects[i].text);
742 return TRUE; /* show tooltip */
745 return FALSE; /* don't show tooltip */
748 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
750 struct dive *dive = current_dive;
751 struct graphics_context gc = { .printer = 0 };
752 static cairo_rectangle_int_t drawing_area;
754 /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset
755 * so effective drawing area is width-2x * height-2y */
756 drawing_area.width = widget->allocation.width;
757 drawing_area.height = widget->allocation.height;
758 drawing_area.x = drawing_area.width / 20.0;
759 drawing_area.y = drawing_area.height / 20.0;
761 gc.cr = gdk_cairo_create(widget->window);
762 g_object_set(widget, "has-tooltip", TRUE, NULL);
763 g_signal_connect(widget, "query-tooltip", G_CALLBACK(profile_tooltip), &drawing_area);
764 set_source_rgb(&gc, 0, 0, 0);
770 tooltip_rects = NULL;
773 plot(&gc, &drawing_area, dive);
775 cairo_destroy(gc.cr);
780 GtkWidget *dive_profile_widget(void)
784 da = gtk_drawing_area_new();
785 gtk_widget_set_size_request(da, 350, 250);
786 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
791 int process_ui_events(void)
795 while (gtk_events_pending()) {
796 if (gtk_main_iteration_do(0)) {
805 static void fill_computer_list(GtkListStore *store)
808 struct device_list *list = device_list;
810 for (list = device_list ; list->name ; list++) {
811 gtk_list_store_append(store, &iter);
812 gtk_list_store_set(store, &iter,
819 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
821 GtkWidget *hbox, *combo_box, *frame;
823 GtkCellRenderer *renderer;
825 hbox = gtk_hbox_new(FALSE, 6);
826 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
828 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
829 fill_computer_list(model);
831 frame = gtk_frame_new("Dive computer");
832 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
834 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
835 gtk_container_add(GTK_CONTAINER(frame), combo_box);
837 renderer = gtk_cell_renderer_text_new();
838 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
839 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
841 return GTK_COMBO_BOX(combo_box);
844 static GtkEntry *dive_computer_device(GtkWidget *vbox)
846 GtkWidget *hbox, *entry, *frame;
848 hbox = gtk_hbox_new(FALSE, 6);
849 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
851 frame = gtk_frame_new("Device name");
852 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
854 entry = gtk_entry_new();
855 gtk_container_add(GTK_CONTAINER(frame), entry);
856 gtk_entry_set_text(GTK_ENTRY(entry), "/dev/ttyUSB0");
858 return GTK_ENTRY(entry);
861 /* once a file is selected in the FileChooserButton we want to exit the import dialog */
862 static void on_file_set(GtkFileChooserButton *widget, gpointer _data)
864 GtkDialog *main_dialog = _data;
866 gtk_dialog_response(main_dialog, GTK_RESPONSE_ACCEPT);
869 static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
871 GtkWidget *hbox, *frame, *chooser, *dialog;
872 GtkFileFilter *filter;
874 hbox = gtk_hbox_new(FALSE, 6);
875 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
877 frame = gtk_frame_new("XML file name");
878 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
879 dialog = gtk_file_chooser_dialog_new("Open XML File",
880 GTK_WINDOW(main_window),
881 GTK_FILE_CHOOSER_ACTION_OPEN,
882 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
883 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
885 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
887 filter = gtk_file_filter_new();
888 gtk_file_filter_add_pattern(filter, "*.xml");
889 gtk_file_filter_add_pattern(filter, "*.XML");
890 gtk_file_filter_add_pattern(filter, "*.sda");
891 gtk_file_filter_add_pattern(filter, "*.SDA");
892 gtk_file_filter_add_mime_type(filter, "text/xml");
893 gtk_file_filter_set_name(filter, "XML file");
894 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
896 chooser = gtk_file_chooser_button_new_with_dialog(dialog);
897 g_signal_connect(G_OBJECT(chooser), "file-set", G_CALLBACK(on_file_set), main_dialog);
899 gtk_file_chooser_button_set_width_chars(GTK_FILE_CHOOSER_BUTTON(chooser), 30);
900 gtk_container_add(GTK_CONTAINER(frame), chooser);
905 static void do_import_file(gpointer data, gpointer user_data)
907 GError *error = NULL;
908 parse_xml_file(data, &error);
918 void import_dialog(GtkWidget *w, gpointer data)
921 GtkWidget *dialog, *hbox, *vbox, *label;
922 GtkComboBox *computer;
924 GtkWidget *XMLchooser;
925 device_data_t devicedata = {
929 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
930 GTK_WINDOW(main_window),
931 GTK_DIALOG_DESTROY_WITH_PARENT,
932 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
933 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
936 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
937 label = gtk_label_new("Import: \nLoad XML file or import directly from dive computer");
938 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 3);
939 XMLchooser = xml_file_selector(vbox, dialog);
940 computer = dive_computer_selector(vbox);
941 device = dive_computer_device(vbox);
942 hbox = gtk_hbox_new(FALSE, 6);
943 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
944 devicedata.progress.bar = gtk_progress_bar_new();
945 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
947 gtk_widget_show_all(dialog);
948 result = gtk_dialog_run(GTK_DIALOG(dialog));
955 case GTK_RESPONSE_ACCEPT:
956 /* what happened - did the user pick a file? In that case
957 * we ignore whether a dive computer model was picked */
958 list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
959 if (g_slist_length(list) == 0) {
960 if (!gtk_combo_box_get_active_iter(computer, &iter))
962 model = gtk_combo_box_get_model(computer);
963 gtk_tree_model_get(model, &iter,
967 devicedata.type = type;
968 devicedata.name = comp;
969 devicedata.devname = gtk_entry_get_text(device);
970 do_import(&devicedata);
972 g_slist_foreach(list,do_import_file,NULL);
979 gtk_widget_destroy(dialog);
984 void update_progressbar(progressbar_t *progress, double value)
986 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
990 void set_filename(const char *filename)
993 existing_filename = strdup(filename);