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;
47 static void on_info_bar_response(GtkWidget *widget, gint response,
50 if (response == GTK_RESPONSE_OK)
52 gtk_widget_destroy(widget);
53 error_info_bar = NULL;
57 void report_error(GError* error)
64 if (error_info_bar == NULL)
67 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
70 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
71 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
74 error_label = gtk_label_new(error->message);
75 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
76 gtk_container_add(GTK_CONTAINER(container), error_label);
78 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
79 gtk_widget_show_all(main_vbox);
85 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
86 gtk_label_set(GTK_LABEL(error_label), buffer);
90 static void file_open(GtkWidget *w, gpointer data)
93 GtkFileFilter *filter;
95 dialog = gtk_file_chooser_dialog_new("Open File",
96 GTK_WINDOW(main_window),
97 GTK_FILE_CHOOSER_ACTION_OPEN,
98 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
99 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
101 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
103 filter = gtk_file_filter_new();
104 gtk_file_filter_add_pattern(filter, "*.xml");
105 gtk_file_filter_add_pattern(filter, "*.XML");
106 gtk_file_filter_add_mime_type(filter, "text/xml");
107 gtk_file_filter_set_name(filter, "XML file");
108 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
110 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
113 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
115 GError *error = NULL;
116 while(filenames != NULL) {
117 filename = (char *)filenames->data;
118 parse_xml_file(filename, &error);
127 filenames = g_slist_next(filenames);
129 g_slist_free(filenames);
131 dive_list_update_dives();
133 gtk_widget_destroy(dialog);
136 static void file_save(GtkWidget *w, gpointer data)
139 dialog = gtk_file_chooser_dialog_new("Save File",
140 GTK_WINDOW(main_window),
141 GTK_FILE_CHOOSER_ACTION_SAVE,
142 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
143 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
145 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
146 if (!existing_filename) {
147 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
149 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
151 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
153 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
154 save_dives(filename);
156 mark_divelist_changed(FALSE);
158 gtk_widget_destroy(dialog);
161 static void ask_save_changes()
163 GtkWidget *dialog, *label, *content;
164 dialog = gtk_dialog_new_with_buttons("Save Changes?",
165 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
166 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
167 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
169 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
170 label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
171 gtk_container_add (GTK_CONTAINER (content), label);
172 gtk_widget_show_all (dialog);
173 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
174 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
175 file_save(NULL,NULL);
177 gtk_widget_destroy(dialog);
180 static gboolean on_delete(GtkWidget* w, gpointer data)
182 /* Make sure to flush any modified dive data */
185 if (unsaved_changes())
188 return FALSE; /* go ahead, kill the program, we're good now */
191 static void on_destroy(GtkWidget* w, gpointer data)
196 static void quit(GtkWidget *w, gpointer data)
198 /* Make sure to flush any modified dive data */
201 if (unsaved_changes())
206 GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title,
207 data_func_t data_func, PangoAlignment align, gboolean visible)
209 GtkCellRenderer *renderer;
210 GtkTreeViewColumn *col;
211 double xalign = 0.0; /* left as default */
213 renderer = gtk_cell_renderer_text_new();
214 col = gtk_tree_view_column_new();
216 gtk_tree_view_column_set_title(col, title);
217 gtk_tree_view_column_set_sort_column_id(col, index);
218 gtk_tree_view_column_set_resizable(col, TRUE);
219 gtk_tree_view_column_pack_start(col, renderer, TRUE);
221 gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, (void *)(long)index, NULL);
223 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
224 gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
226 case PANGO_ALIGN_LEFT:
229 case PANGO_ALIGN_CENTER:
232 case PANGO_ALIGN_RIGHT:
236 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
237 gtk_tree_view_column_set_visible(col, visible);
238 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
242 static void create_radio(GtkWidget *vbox, const char *name, ...)
245 GtkRadioButton *group = NULL;
246 GtkWidget *box, *label;
248 box = gtk_hbox_new(TRUE, 10);
249 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
251 label = gtk_label_new(name);
252 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
254 va_start(args, name);
261 name = va_arg(args, char *);
264 callback_fn = va_arg(args, void *);
265 enabled = va_arg(args, int);
267 button = gtk_radio_button_new_with_label_from_widget(group, name);
268 group = GTK_RADIO_BUTTON(button);
269 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
270 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
271 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
276 #define UNITCALLBACK(name, type, value) \
277 static void name(GtkWidget *w, gpointer data) \
279 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
280 menu_units.type = value; \
283 static struct units menu_units;
285 UNITCALLBACK(set_meter, length, METERS)
286 UNITCALLBACK(set_feet, length, FEET)
287 UNITCALLBACK(set_bar, pressure, BAR)
288 UNITCALLBACK(set_psi, pressure, PSI)
289 UNITCALLBACK(set_liter, volume, LITER)
290 UNITCALLBACK(set_cuft, volume, CUFT)
291 UNITCALLBACK(set_celsius, temperature, CELSIUS)
292 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
294 #define OPTIONCALLBACK(name, option) \
295 static void name(GtkWidget *w, gpointer data) \
297 option = GTK_TOGGLE_BUTTON(w)->active; \
300 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
301 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
303 static void preferences_dialog(GtkWidget *w, gpointer data)
306 GtkWidget *dialog, *font, *frame, *box, *vbox, *button;
308 menu_units = output_units;
310 dialog = gtk_dialog_new_with_buttons("Preferences",
311 GTK_WINDOW(main_window),
312 GTK_DIALOG_DESTROY_WITH_PARENT,
313 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
314 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
317 frame = gtk_frame_new("Units");
318 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
319 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
321 box = gtk_vbox_new(FALSE, 6);
322 gtk_container_add(GTK_CONTAINER(frame), box);
324 create_radio(box, "Depth:",
325 "Meter", set_meter, (output_units.length == METERS),
326 "Feet", set_feet, (output_units.length == FEET),
329 create_radio(box, "Pressure:",
330 "Bar", set_bar, (output_units.pressure == BAR),
331 "PSI", set_psi, (output_units.pressure == PSI),
334 create_radio(box, "Volume:",
335 "Liter", set_liter, (output_units.volume == LITER),
336 "CuFt", set_cuft, (output_units.volume == CUFT),
339 create_radio(box, "Temperature:",
340 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
341 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
344 frame = gtk_frame_new("Columns");
345 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
347 box = gtk_hbox_new(FALSE, 6);
348 gtk_container_add(GTK_CONTAINER(frame), box);
350 button = gtk_check_button_new_with_label("Show SAC");
351 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
352 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
353 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
355 button = gtk_check_button_new_with_label("Show OTU");
356 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
357 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
358 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
360 font = gtk_font_button_new_with_font(divelist_font);
361 gtk_box_pack_start(GTK_BOX(vbox), font, FALSE, FALSE, 5);
363 gtk_widget_show_all(dialog);
364 result = gtk_dialog_run(GTK_DIALOG(dialog));
365 if (result == GTK_RESPONSE_ACCEPT) {
366 /* Make sure to flush any modified old dive data with old units */
369 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
370 set_divelist_font(divelist_font);
372 output_units = menu_units;
373 update_dive_list_units();
375 update_dive_list_col_visibility();
376 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
377 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
378 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
379 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
380 gconf_client_set_bool(gconf, GCONF_NAME(SAC), ! visible_cols.sac, NULL); /* inverted to get the correct default */
381 gconf_client_set_bool(gconf, GCONF_NAME(OTU), visible_cols.otu, NULL);
382 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
384 gtk_widget_destroy(dialog);
387 static void renumber_dialog(GtkWidget *w, gpointer data)
390 GtkWidget *dialog, *frame, *button, *vbox;
392 dialog = gtk_dialog_new_with_buttons("Renumber",
393 GTK_WINDOW(main_window),
394 GTK_DIALOG_DESTROY_WITH_PARENT,
395 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
396 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
399 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
401 frame = gtk_frame_new("New starting number");
402 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
404 button = gtk_spin_button_new_with_range(1, 50000, 1);
405 gtk_container_add(GTK_CONTAINER(frame), button);
407 gtk_widget_show_all(dialog);
408 result = gtk_dialog_run(GTK_DIALOG(dialog));
409 if (result == GTK_RESPONSE_ACCEPT) {
410 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
414 gtk_widget_destroy(dialog);
417 static void about_dialog(GtkWidget *w, gpointer data)
419 const char *logo_property = NULL;
420 GdkPixbuf *logo = NULL;
421 GtkWidget *image = gtk_image_new_from_file("icon.svg");
424 logo = gtk_image_get_pixbuf(GTK_IMAGE(image));
425 logo_property = "logo";
428 gtk_show_about_dialog(NULL,
429 "program-name", "SubSurface",
430 "comments", "Half-arsed divelog software in C",
432 "version", VERSION_STRING,
433 "copyright", "Linus Torvalds 2011",
439 static GtkActionEntry menu_items[] = {
440 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
441 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
442 { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
443 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
444 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
445 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
446 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
447 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
448 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
449 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
450 { "About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK(about_dialog) },
452 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
454 static const gchar* ui_string = " \
456 <menubar name=\"MainMenu\"> \
457 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
458 <menuitem name=\"Open\" action=\"OpenFile\" /> \
459 <menuitem name=\"Save\" action=\"SaveFile\" /> \
460 <menuitem name=\"Print\" action=\"Print\" /> \
461 <separator name=\"Separator1\"/> \
462 <menuitem name=\"Import\" action=\"Import\" /> \
463 <separator name=\"Separator2\"/> \
464 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
465 <separator name=\"Separator3\"/> \
466 <menuitem name=\"Quit\" action=\"Quit\" /> \
468 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
469 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
471 <menu name=\"Help\" action=\"HelpMenuAction\"> \
472 <menuitem name=\"About\" action=\"About\" /> \
478 static GtkWidget *get_menubar_menu(GtkWidget *window)
480 GtkActionGroup *action_group = gtk_action_group_new("Menu");
481 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
483 GtkUIManager *ui_manager = gtk_ui_manager_new();
484 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
486 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
488 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
489 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
494 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
499 static const char notebook_group[] = "123";
500 #define GRP_ID ((void *)notebook_group)
505 gulong delete_handler;
506 gulong destroy_handler;
509 static notebook_data_t nbd[2]; /* we rip at most two notebook pages off */
511 static GtkNotebook *create_new_notebook_window(GtkNotebook *source,
513 gint x, gint y, gpointer data)
515 GtkWidget *win, *notebook, *vbox;
516 notebook_data_t *nbdp;
518 /* pick the right notebook page data and return if both are detached */
519 if (nbd[0].widget == NULL)
521 else if (nbd[1].widget == NULL)
526 nbdp->name = strdup(gtk_widget_get_name(page));
527 nbdp->widget = win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
528 gtk_window_set_title(GTK_WINDOW(win), nbdp->name);
529 gtk_window_move(GTK_WINDOW(win), x, y);
531 /* Destroying the dive list will kill the application */
532 nbdp->delete_handler = g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
533 nbdp->destroy_handler = g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
535 nbdp->box = vbox = gtk_vbox_new(FALSE, 0);
536 gtk_container_add(GTK_CONTAINER(win), vbox);
538 notebook = gtk_notebook_new();
539 gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
540 gtk_widget_set_name(notebook, nbdp->name);
541 /* disallow drop events */
542 gtk_drag_dest_set(notebook, 0, NULL, 0, 0);
543 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
544 gtk_widget_set_size_request(notebook, 450, 350);
546 gtk_widget_show_all(win);
547 return GTK_NOTEBOOK(notebook);
550 static void drag_cb(GtkWidget *widget, GdkDragContext *context,
551 gint x, gint y, guint time,
555 notebook_data_t *nbdp;
557 source = gtk_drag_get_source_widget(context);
558 if (nbd[0].name && ! strcmp(nbd[0].name,gtk_widget_get_name(source)))
560 else if (nbd[1].name && ! strcmp(nbd[1].name,gtk_widget_get_name(source)))
566 gtk_drag_finish(context, TRUE, TRUE, time);
568 /* we no longer need the widget - but getting rid of this is hard;
569 * remove the signal handler, remove the notebook from the box
570 * then destroy the widget (and clear out our data structure) */
571 g_signal_handler_disconnect(nbdp->widget,nbdp->delete_handler);
572 g_signal_handler_disconnect(nbdp->widget,nbdp->destroy_handler);
573 gtk_container_remove(GTK_CONTAINER(nbdp->box), source);
574 gtk_widget_destroy(nbdp->widget);
580 void init_ui(int argc, char **argv)
584 GtkWidget *dive_info;
585 GtkWidget *dive_list;
586 GtkWidget *equipment;
589 static const GtkTargetEntry notebook_target = {
590 "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0
593 gtk_init(&argc, &argv);
596 gconf = gconf_client_get_default();
598 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
599 output_units.length = FEET;
600 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
601 output_units.pressure = PSI;
602 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
603 output_units.volume = CUFT;
604 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
605 output_units.temperature = FAHRENHEIT;
606 /* an unset key is FALSE - so in order to get the default behavior right we
607 invert the meaning of the SAC key */
608 visible_cols.otu = gconf_client_get_bool(gconf, GCONF_NAME(OTU), NULL);
609 visible_cols.sac = ! gconf_client_get_bool(gconf, GCONF_NAME(SAC), NULL);
611 divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
613 divelist_font = DIVELIST_DEFAULT_FONT;
615 error_info_bar = NULL;
616 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
617 gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
618 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(on_delete), NULL);
619 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
622 vbox = gtk_vbox_new(FALSE, 0);
623 gtk_container_add(GTK_CONTAINER(win), vbox);
626 menubar = get_menubar_menu(win);
627 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
629 /* Notebook for dive info vs profile vs .. */
630 notebook = gtk_notebook_new();
631 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 6);
632 gtk_notebook_set_group(GTK_NOTEBOOK(notebook), GRP_ID);
633 g_signal_connect(notebook, "create-window", G_CALLBACK(create_new_notebook_window), NULL);
634 gtk_drag_dest_set(notebook, GTK_DEST_DEFAULT_ALL, ¬ebook_target, 1, GDK_ACTION_MOVE);
635 g_signal_connect(notebook, "drag-drop", G_CALLBACK(drag_cb), notebook);
636 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
638 /* Create the actual divelist */
639 dive_list = dive_list_create();
640 gtk_widget_set_name(dive_list, "Dive List");
641 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_list, gtk_label_new("Dive List"));
642 gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_list, 1);
643 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_list, 1);
645 /* Frame for dive profile */
646 dive_profile = dive_profile_widget();
647 gtk_widget_set_name(dive_profile, "Dive Profile");
648 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
649 gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook), dive_profile, 1);
650 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), dive_profile, 1);
652 /* Frame for extended dive info */
653 dive_info = extended_dive_info_widget();
654 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
656 /* Frame for dive equipment */
657 equipment = equipment_widget();
658 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
660 gtk_widget_set_app_paintable(win, TRUE);
661 gtk_widget_show_all(win);
671 /* get the filenames the user selects and call the parsing function
673 * return 0 if the user cancelled the dialog
675 int open_import_file_dialog(char *filterpattern, char *filtertext,
676 void(* parse_function)(char *))
681 GtkFileFilter *filter = gtk_file_filter_new ();
682 gtk_file_filter_add_pattern (filter, filterpattern);
683 gtk_file_filter_set_name(filter, filtertext);
684 dialog = gtk_file_chooser_dialog_new("Open File",
685 GTK_WINDOW(main_window),
686 GTK_FILE_CHOOSER_ACTION_OPEN,
687 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
688 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
690 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
691 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
693 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
696 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
697 while(filenames != NULL) {
698 filename = (char *)filenames->data;
699 parse_function(filename);
701 filenames = g_slist_next(filenames);
703 g_slist_free(filenames);
706 gtk_widget_destroy(dialog);
711 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
713 struct dive *dive = current_dive;
714 struct graphics_context gc = { .printer = 0 };
717 w = widget->allocation.width;
718 h = widget->allocation.height;
720 gc.cr = gdk_cairo_create(widget->window);
721 set_source_rgb(&gc, 0, 0, 0);
725 plot(&gc, w, h, dive);
727 cairo_destroy(gc.cr);
732 GtkWidget *dive_profile_widget(void)
736 da = gtk_drawing_area_new();
737 gtk_widget_set_size_request(da, 350, 250);
738 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
743 int process_ui_events(void)
747 while (gtk_events_pending()) {
748 if (gtk_main_iteration_do(0)) {
757 static void fill_computer_list(GtkListStore *store)
760 struct device_list *list = device_list;
762 for (list = device_list ; list->name ; list++) {
763 gtk_list_store_append(store, &iter);
764 gtk_list_store_set(store, &iter,
771 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
773 GtkWidget *hbox, *combo_box, *frame;
775 GtkCellRenderer *renderer;
777 hbox = gtk_hbox_new(FALSE, 6);
778 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
780 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
781 fill_computer_list(model);
783 frame = gtk_frame_new("Dive computer");
784 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
786 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
787 gtk_container_add(GTK_CONTAINER(frame), combo_box);
789 renderer = gtk_cell_renderer_text_new();
790 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
791 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
793 return GTK_COMBO_BOX(combo_box);
796 static GtkEntry *dive_computer_device(GtkWidget *vbox)
798 GtkWidget *hbox, *entry, *frame;
800 hbox = gtk_hbox_new(FALSE, 6);
801 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
803 frame = gtk_frame_new("Device name");
804 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
806 entry = gtk_entry_new();
807 gtk_container_add(GTK_CONTAINER(frame), entry);
808 gtk_entry_set_text(GTK_ENTRY(entry), "/dev/ttyUSB0");
810 return GTK_ENTRY(entry);
813 void import_dialog(GtkWidget *w, gpointer data)
816 GtkWidget *dialog, *hbox, *vbox;
817 GtkComboBox *computer;
819 device_data_t devicedata = {
823 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
824 GTK_WINDOW(main_window),
825 GTK_DIALOG_DESTROY_WITH_PARENT,
826 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
827 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
830 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
832 computer = dive_computer_selector(vbox);
833 device = dive_computer_device(vbox);
835 hbox = gtk_hbox_new(FALSE, 6);
836 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 3);
837 devicedata.progress.bar = gtk_progress_bar_new();
838 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
840 gtk_widget_show_all(dialog);
841 result = gtk_dialog_run(GTK_DIALOG(dialog));
847 case GTK_RESPONSE_ACCEPT:
848 if (!gtk_combo_box_get_active_iter(computer, &iter))
850 model = gtk_combo_box_get_model(computer);
851 gtk_tree_model_get(model, &iter,
855 devicedata.type = type;
856 devicedata.name = comp;
857 devicedata.devname = gtk_entry_get_text(device);
858 do_import(&devicedata);
863 gtk_widget_destroy(dialog);
866 dive_list_update_dives();
869 void update_progressbar(progressbar_t *progress, double value)
871 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
875 void set_filename(const char *filename)
878 existing_filename = strdup(filename);