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);
41 gtk_widget_queue_draw(dive_profile);
44 static char *existing_filename;
46 static void on_info_bar_response(GtkWidget *widget, gint response,
49 if (response == GTK_RESPONSE_OK)
51 gtk_widget_destroy(widget);
52 error_info_bar = NULL;
56 void report_error(GError* error)
63 if (error_info_bar == NULL)
66 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
69 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
70 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
73 error_label = gtk_label_new(error->message);
74 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
75 gtk_container_add(GTK_CONTAINER(container), error_label);
77 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
78 gtk_widget_show_all(main_vbox);
84 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
85 gtk_label_set(GTK_LABEL(error_label), buffer);
89 static void file_open(GtkWidget *w, gpointer data)
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 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
103 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
105 GError *error = NULL;
106 while(filenames != NULL) {
107 filename = (char *)filenames->data;
108 parse_xml_file(filename, &error);
117 filenames = g_slist_next(filenames);
119 g_slist_free(filenames);
121 dive_list_update_dives();
123 gtk_widget_destroy(dialog);
126 static void file_save(GtkWidget *w, gpointer data)
129 dialog = gtk_file_chooser_dialog_new("Save File",
130 GTK_WINDOW(main_window),
131 GTK_FILE_CHOOSER_ACTION_SAVE,
132 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
133 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
135 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
136 if (!existing_filename) {
137 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
139 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
141 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
143 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
144 save_dives(filename);
146 mark_divelist_changed(FALSE);
148 gtk_widget_destroy(dialog);
151 static void ask_save_changes()
153 GtkWidget *dialog, *label, *content;
154 dialog = gtk_dialog_new_with_buttons("Save Changes?",
155 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
156 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
157 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
159 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
160 label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
161 gtk_container_add (GTK_CONTAINER (content), label);
162 gtk_widget_show_all (dialog);
163 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
164 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
165 file_save(NULL,NULL);
167 gtk_widget_destroy(dialog);
170 static gboolean on_delete(GtkWidget* w, gpointer data)
172 /* Make sure to flush any modified dive data */
175 if (unsaved_changes())
178 return FALSE; /* go ahead, kill the program, we're good now */
181 static void on_destroy(GtkWidget* w, gpointer data)
186 static void quit(GtkWidget *w, gpointer data)
188 /* Make sure to flush any modified dive data */
191 if (unsaved_changes())
196 static void create_radio(GtkWidget *vbox, const char *name, ...)
199 GtkRadioButton *group = NULL;
200 GtkWidget *box, *label;
202 box = gtk_hbox_new(TRUE, 10);
203 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
205 label = gtk_label_new(name);
206 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
208 va_start(args, name);
215 name = va_arg(args, char *);
218 callback_fn = va_arg(args, void *);
219 enabled = va_arg(args, int);
221 button = gtk_radio_button_new_with_label_from_widget(group, name);
222 group = GTK_RADIO_BUTTON(button);
223 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
224 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
225 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
230 #define UNITCALLBACK(name, type, value) \
231 static void name(GtkWidget *w, gpointer data) \
233 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
234 menu_units.type = value; \
237 static struct units menu_units;
239 UNITCALLBACK(set_meter, length, METERS)
240 UNITCALLBACK(set_feet, length, FEET)
241 UNITCALLBACK(set_bar, pressure, BAR)
242 UNITCALLBACK(set_psi, pressure, PSI)
243 UNITCALLBACK(set_liter, volume, LITER)
244 UNITCALLBACK(set_cuft, volume, CUFT)
245 UNITCALLBACK(set_celsius, temperature, CELSIUS)
246 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
248 #define OPTIONCALLBACK(name, option) \
249 static void name(GtkWidget *w, gpointer data) \
251 option = GTK_TOGGLE_BUTTON(w)->active; \
254 OPTIONCALLBACK(otu_toggle, visible_cols.otu)
255 OPTIONCALLBACK(sac_toggle, visible_cols.sac)
257 static void preferences_dialog(GtkWidget *w, gpointer data)
260 GtkWidget *dialog, *font, *frame, *box, *button;
262 menu_units = output_units;
264 dialog = gtk_dialog_new_with_buttons("Preferences",
265 GTK_WINDOW(main_window),
266 GTK_DIALOG_DESTROY_WITH_PARENT,
267 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
268 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
271 frame = gtk_frame_new("Units");
272 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
274 box = gtk_vbox_new(FALSE, 6);
275 gtk_container_add(GTK_CONTAINER(frame), box);
277 create_radio(box, "Depth:",
278 "Meter", set_meter, (output_units.length == METERS),
279 "Feet", set_feet, (output_units.length == FEET),
282 create_radio(box, "Pressure:",
283 "Bar", set_bar, (output_units.pressure == BAR),
284 "PSI", set_psi, (output_units.pressure == PSI),
287 create_radio(box, "Volume:",
288 "Liter", set_liter, (output_units.volume == LITER),
289 "CuFt", set_cuft, (output_units.volume == CUFT),
292 create_radio(box, "Temperature:",
293 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
294 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
297 frame = gtk_frame_new("Columns");
298 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
300 box = gtk_hbox_new(FALSE, 6);
301 gtk_container_add(GTK_CONTAINER(frame), box);
303 button = gtk_check_button_new_with_label("Show SAC");
304 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.sac);
305 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
306 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(sac_toggle), NULL);
308 button = gtk_check_button_new_with_label("Show OTU");
309 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.otu);
310 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
311 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(otu_toggle), NULL);
313 font = gtk_font_button_new_with_font(divelist_font);
314 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), font, FALSE, FALSE, 5);
316 gtk_widget_show_all(dialog);
317 result = gtk_dialog_run(GTK_DIALOG(dialog));
318 if (result == GTK_RESPONSE_ACCEPT) {
319 /* Make sure to flush any modified old dive data with old units */
322 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
323 set_divelist_font(divelist_font);
325 output_units = menu_units;
326 update_dive_list_units();
328 update_dive_list_col_visibility();
329 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
330 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
331 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
332 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
333 gconf_client_set_bool(gconf, GCONF_NAME(SAC), visible_cols.sac, NULL);
334 gconf_client_set_bool(gconf, GCONF_NAME(OTU), visible_cols.otu, NULL);
335 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
337 gtk_widget_destroy(dialog);
340 static void renumber_dialog(GtkWidget *w, gpointer data)
343 GtkWidget *dialog, *frame, *button;
345 dialog = gtk_dialog_new_with_buttons("Renumber",
346 GTK_WINDOW(main_window),
347 GTK_DIALOG_DESTROY_WITH_PARENT,
348 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
349 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
352 frame = gtk_frame_new("New starting number");
353 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame);
355 button = gtk_spin_button_new_with_range(1, 50000, 1);
356 gtk_container_add(GTK_CONTAINER(frame), button);
358 gtk_widget_show_all(dialog);
359 result = gtk_dialog_run(GTK_DIALOG(dialog));
360 if (result == GTK_RESPONSE_ACCEPT) {
361 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
365 gtk_widget_destroy(dialog);
368 static GtkActionEntry menu_items[] = {
369 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
370 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
371 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
372 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
373 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
374 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
375 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
376 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
377 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
379 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
381 static const gchar* ui_string = " \
383 <menubar name=\"MainMenu\"> \
384 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
385 <menuitem name=\"Open\" action=\"OpenFile\" /> \
386 <menuitem name=\"Save\" action=\"SaveFile\" /> \
387 <menuitem name=\"Print\" action=\"Print\" /> \
388 <separator name=\"Separator1\"/> \
389 <menuitem name=\"Import\" action=\"Import\" /> \
390 <separator name=\"Separator2\"/> \
391 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
392 <separator name=\"Separator3\"/> \
393 <menuitem name=\"Quit\" action=\"Quit\" /> \
395 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
396 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
402 static GtkWidget *get_menubar_menu(GtkWidget *window)
404 GtkActionGroup *action_group = gtk_action_group_new("Menu");
405 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
407 GtkUIManager *ui_manager = gtk_ui_manager_new();
408 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
410 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
412 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
413 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
418 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
423 void init_ui(int argc, char **argv)
429 GtkWidget *dive_info;
430 GtkWidget *dive_list;
431 GtkWidget *equipment;
435 gtk_init(&argc, &argv);
438 gconf = gconf_client_get_default();
440 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
441 output_units.length = FEET;
442 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
443 output_units.pressure = PSI;
444 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
445 output_units.volume = CUFT;
446 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
447 output_units.temperature = FAHRENHEIT;
449 divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
451 divelist_font = DIVELIST_DEFAULT_FONT;
453 error_info_bar = NULL;
454 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
455 gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
456 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK (on_delete), NULL);
457 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
460 vbox = gtk_vbox_new(FALSE, 0);
461 gtk_container_add(GTK_CONTAINER(win), vbox);
464 menubar = get_menubar_menu(win);
465 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
467 /* HPane for left the dive list, and right the dive info */
468 paned = gtk_vpaned_new();
469 gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
471 /* Create the actual divelist */
472 dive_list = dive_list_create();
473 gtk_paned_add2(GTK_PANED(paned), dive_list);
475 /* VBox for dive info, and tabs */
476 info_box = gtk_vbox_new(FALSE, 6);
477 gtk_paned_add1(GTK_PANED(paned), info_box);
479 /* Notebook for dive info vs profile vs .. */
480 notebook = gtk_notebook_new();
481 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
482 gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
484 /* Frame for dive profile */
485 dive_profile = dive_profile_widget();
486 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
488 /* Frame for extended dive info */
489 dive_info = extended_dive_info_widget();
490 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
492 /* Frame for dive equipment */
493 equipment = equipment_widget();
494 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
496 gtk_widget_set_app_paintable(win, TRUE);
497 gtk_widget_show_all(win);
507 /* get the filenames the user selects and call the parsing function
509 * return 0 if the user cancelled the dialog
511 int open_import_file_dialog(char *filterpattern, char *filtertext,
512 void(* parse_function)(char *))
517 GtkFileFilter *filter = gtk_file_filter_new ();
518 gtk_file_filter_add_pattern (filter, filterpattern);
519 gtk_file_filter_set_name(filter, filtertext);
520 dialog = gtk_file_chooser_dialog_new("Open File",
521 GTK_WINDOW(main_window),
522 GTK_FILE_CHOOSER_ACTION_OPEN,
523 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
524 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
526 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
527 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
529 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
532 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
533 while(filenames != NULL) {
534 filename = (char *)filenames->data;
535 parse_function(filename);
537 filenames = g_slist_next(filenames);
539 g_slist_free(filenames);
542 gtk_widget_destroy(dialog);
547 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
549 struct dive *dive = current_dive;
550 struct graphics_context gc = { .printer = 0 };
553 w = widget->allocation.width;
554 h = widget->allocation.height;
556 gc.cr = gdk_cairo_create(widget->window);
557 set_source_rgb(&gc, 0, 0, 0);
561 plot(&gc, w, h, dive);
563 cairo_destroy(gc.cr);
568 GtkWidget *dive_profile_widget(void)
572 da = gtk_drawing_area_new();
573 gtk_widget_set_size_request(da, 350, 250);
574 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
579 int process_ui_events(void)
583 while (gtk_events_pending()) {
584 if (gtk_main_iteration_do(0)) {
593 static void fill_computer_list(GtkListStore *store)
596 struct device_list *list = device_list;
598 for (list = device_list ; list->name ; list++) {
599 gtk_list_store_append(store, &iter);
600 gtk_list_store_set(store, &iter,
607 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
609 GtkWidget *hbox, *combo_box;
611 GtkCellRenderer *renderer;
613 hbox = gtk_hbox_new(FALSE, 6);
614 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 3);
616 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
617 fill_computer_list(model);
619 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
620 gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3);
622 renderer = gtk_cell_renderer_text_new();
623 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
624 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
626 return GTK_COMBO_BOX(combo_box);
629 void import_dialog(GtkWidget *w, gpointer data)
632 GtkWidget *dialog, *hbox;
633 GtkComboBox *computer;
634 device_data_t devicedata = {
635 .devname = "/dev/ttyUSB0",
638 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
639 GTK_WINDOW(main_window),
640 GTK_DIALOG_DESTROY_WITH_PARENT,
641 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
642 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
645 computer = dive_computer_selector(dialog);
647 hbox = gtk_hbox_new(FALSE, 6);
648 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, TRUE, 3);
649 devicedata.progress->bar = gtk_progress_bar_new();
650 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress->bar);
652 gtk_widget_show_all(dialog);
653 result = gtk_dialog_run(GTK_DIALOG(dialog));
659 case GTK_RESPONSE_ACCEPT:
660 if (!gtk_combo_box_get_active_iter(computer, &iter))
662 model = gtk_combo_box_get_model(computer);
663 gtk_tree_model_get(model, &iter,
667 devicedata.type = type;
668 devicedata.name = comp;
669 do_import(&devicedata);
674 gtk_widget_destroy(dialog);
677 dive_list_update_dives();
680 void update_progressbar(progressbar_t *progress, double value)
682 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
686 void set_filename(const char *filename)
689 existing_filename = strdup(filename);