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 void repaint_dive(void)
38 update_dive(current_dive);
39 gtk_widget_queue_draw(dive_profile);
42 static char *existing_filename;
44 static void on_info_bar_response(GtkWidget *widget, gint response,
47 if (response == GTK_RESPONSE_OK)
49 gtk_widget_destroy(widget);
50 error_info_bar = NULL;
54 void report_error(GError* error)
61 if (error_info_bar == NULL)
64 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
67 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
68 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
71 error_label = gtk_label_new(error->message);
72 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
73 gtk_container_add(GTK_CONTAINER(container), error_label);
75 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
76 gtk_widget_show_all(main_vbox);
82 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
83 gtk_label_set(GTK_LABEL(error_label), buffer);
87 static void file_open(GtkWidget *w, gpointer data)
90 dialog = gtk_file_chooser_dialog_new("Open File",
91 GTK_WINDOW(main_window),
92 GTK_FILE_CHOOSER_ACTION_OPEN,
93 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
94 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
96 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
98 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
101 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
103 GError *error = NULL;
104 while(filenames != NULL) {
105 filename = (char *)filenames->data;
106 parse_xml_file(filename, &error);
115 filenames = g_slist_next(filenames);
117 g_slist_free(filenames);
119 dive_list_update_dives();
121 gtk_widget_destroy(dialog);
124 static void file_save(GtkWidget *w, gpointer data)
127 dialog = gtk_file_chooser_dialog_new("Save File",
128 GTK_WINDOW(main_window),
129 GTK_FILE_CHOOSER_ACTION_SAVE,
130 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
131 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
133 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
134 if (!existing_filename) {
135 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
137 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
139 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
141 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
142 save_dives(filename);
144 mark_divelist_changed(FALSE);
146 gtk_widget_destroy(dialog);
149 static void ask_save_changes()
151 GtkWidget *dialog, *label, *content;
152 dialog = gtk_dialog_new_with_buttons("Save Changes?",
153 GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
154 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
155 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
157 content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
158 label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?");
159 gtk_container_add (GTK_CONTAINER (content), label);
160 gtk_widget_show_all (dialog);
161 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
162 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
163 file_save(NULL,NULL);
165 gtk_widget_destroy(dialog);
168 static gboolean on_delete(GtkWidget* w, gpointer data)
170 /* Make sure to flush any modified dive data */
173 if (unsaved_changes())
176 return FALSE; /* go ahead, kill the program, we're good now */
179 static void on_destroy(GtkWidget* w, gpointer data)
184 static void quit(GtkWidget *w, gpointer data)
186 /* Make sure to flush any modified dive data */
189 if (unsaved_changes())
194 static void create_radio(GtkWidget *vbox, const char *name, ...)
197 GtkRadioButton *group = NULL;
198 GtkWidget *box, *label;
200 box = gtk_hbox_new(TRUE, 10);
201 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
203 label = gtk_label_new(name);
204 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
206 va_start(args, name);
213 name = va_arg(args, char *);
216 callback_fn = va_arg(args, void *);
217 enabled = va_arg(args, int);
219 button = gtk_radio_button_new_with_label_from_widget(group, name);
220 group = GTK_RADIO_BUTTON(button);
221 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
222 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
223 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
228 #define UNITCALLBACK(name, type, value) \
229 static void name(GtkWidget *w, gpointer data) \
231 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
232 menu_units.type = value; \
235 static struct units menu_units;
237 UNITCALLBACK(set_meter, length, METERS)
238 UNITCALLBACK(set_feet, length, FEET)
239 UNITCALLBACK(set_bar, pressure, BAR)
240 UNITCALLBACK(set_psi, pressure, PSI)
241 UNITCALLBACK(set_liter, volume, LITER)
242 UNITCALLBACK(set_cuft, volume, CUFT)
243 UNITCALLBACK(set_celsius, temperature, CELSIUS)
244 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
246 static void preferences_dialog(GtkWidget *w, gpointer data)
249 GtkWidget *dialog, *font, *frame, *box;
251 menu_units = output_units;
253 dialog = gtk_dialog_new_with_buttons("Preferences",
254 GTK_WINDOW(main_window),
255 GTK_DIALOG_DESTROY_WITH_PARENT,
256 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
257 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
260 frame = gtk_frame_new("Units");
261 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
263 box = gtk_vbox_new(FALSE, 6);
264 gtk_container_add(GTK_CONTAINER(frame), box);
266 create_radio(box, "Depth:",
267 "Meter", set_meter, (output_units.length == METERS),
268 "Feet", set_feet, (output_units.length == FEET),
271 create_radio(box, "Pressure:",
272 "Bar", set_bar, (output_units.pressure == BAR),
273 "PSI", set_psi, (output_units.pressure == PSI),
276 create_radio(box, "Volume:",
277 "Liter", set_liter, (output_units.volume == LITER),
278 "CuFt", set_cuft, (output_units.volume == CUFT),
281 create_radio(box, "Temperature:",
282 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
283 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
286 font = gtk_font_button_new_with_font(divelist_font);
287 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), font, FALSE, FALSE, 5);
289 gtk_widget_show_all(dialog);
290 result = gtk_dialog_run(GTK_DIALOG(dialog));
291 if (result == GTK_RESPONSE_ACCEPT) {
292 /* Make sure to flush any modified old dive data with old units */
295 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
296 set_divelist_font(divelist_font);
298 output_units = menu_units;
299 update_dive_list_units();
301 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
302 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
303 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
304 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
305 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
307 gtk_widget_destroy(dialog);
310 static void renumber_dialog(GtkWidget *w, gpointer data)
313 GtkWidget *dialog, *frame, *button;
315 dialog = gtk_dialog_new_with_buttons("Renumber",
316 GTK_WINDOW(main_window),
317 GTK_DIALOG_DESTROY_WITH_PARENT,
318 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
319 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
322 frame = gtk_frame_new("New starting number");
323 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame);
325 button = gtk_spin_button_new_with_range(1, 50000, 1);
326 gtk_container_add(GTK_CONTAINER(frame), button);
328 gtk_widget_show_all(dialog);
329 result = gtk_dialog_run(GTK_DIALOG(dialog));
330 if (result == GTK_RESPONSE_ACCEPT) {
331 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
335 gtk_widget_destroy(dialog);
338 static GtkActionEntry menu_items[] = {
339 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
340 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
341 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
342 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
343 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
344 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
345 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
346 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
347 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
349 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
351 static const gchar* ui_string = " \
353 <menubar name=\"MainMenu\"> \
354 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
355 <menuitem name=\"Open\" action=\"OpenFile\" /> \
356 <menuitem name=\"Save\" action=\"SaveFile\" /> \
357 <menuitem name=\"Print\" action=\"Print\" /> \
358 <separator name=\"Separator1\"/> \
359 <menuitem name=\"Import\" action=\"Import\" /> \
360 <separator name=\"Separator2\"/> \
361 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
362 <separator name=\"Separator3\"/> \
363 <menuitem name=\"Quit\" action=\"Quit\" /> \
365 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
366 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
372 static GtkWidget *get_menubar_menu(GtkWidget *window)
374 GtkActionGroup *action_group = gtk_action_group_new("Menu");
375 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
377 GtkUIManager *ui_manager = gtk_ui_manager_new();
378 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
380 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
382 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
383 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
388 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
393 void init_ui(int argc, char **argv)
399 GtkWidget *dive_info;
400 GtkWidget *dive_list;
401 GtkWidget *equipment;
405 gtk_init(&argc, &argv);
408 gconf = gconf_client_get_default();
410 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
411 output_units.length = FEET;
412 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
413 output_units.pressure = PSI;
414 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
415 output_units.volume = CUFT;
416 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
417 output_units.temperature = FAHRENHEIT;
419 divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
421 divelist_font = DIVELIST_DEFAULT_FONT;
423 error_info_bar = NULL;
424 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
425 gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
426 g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK (on_delete), NULL);
427 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
430 vbox = gtk_vbox_new(FALSE, 0);
431 gtk_container_add(GTK_CONTAINER(win), vbox);
434 menubar = get_menubar_menu(win);
435 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
437 /* HPane for left the dive list, and right the dive info */
438 paned = gtk_vpaned_new();
439 gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
441 /* Create the actual divelist */
442 dive_list = dive_list_create();
443 gtk_paned_add2(GTK_PANED(paned), dive_list);
445 /* VBox for dive info, and tabs */
446 info_box = gtk_vbox_new(FALSE, 6);
447 gtk_paned_add1(GTK_PANED(paned), info_box);
449 /* Notebook for dive info vs profile vs .. */
450 notebook = gtk_notebook_new();
451 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
452 gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
454 /* Frame for dive profile */
455 dive_profile = dive_profile_widget();
456 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
458 /* Frame for extended dive info */
459 dive_info = extended_dive_info_widget();
460 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
462 /* Frame for dive equipment */
463 equipment = equipment_widget();
464 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
466 gtk_widget_set_app_paintable(win, TRUE);
467 gtk_widget_show_all(win);
477 /* get the filenames the user selects and call the parsing function
479 * return 0 if the user cancelled the dialog
481 int open_import_file_dialog(char *filterpattern, char *filtertext,
482 void(* parse_function)(char *))
487 GtkFileFilter *filter = gtk_file_filter_new ();
488 gtk_file_filter_add_pattern (filter, filterpattern);
489 gtk_file_filter_set_name(filter, filtertext);
490 dialog = gtk_file_chooser_dialog_new("Open File",
491 GTK_WINDOW(main_window),
492 GTK_FILE_CHOOSER_ACTION_OPEN,
493 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
494 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
496 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
497 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
499 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
502 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
503 while(filenames != NULL) {
504 filename = (char *)filenames->data;
505 parse_function(filename);
507 filenames = g_slist_next(filenames);
509 g_slist_free(filenames);
512 gtk_widget_destroy(dialog);
517 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
519 struct dive *dive = current_dive;
520 struct graphics_context gc = { .printer = 0 };
523 w = widget->allocation.width;
524 h = widget->allocation.height;
526 gc.cr = gdk_cairo_create(widget->window);
527 set_source_rgb(&gc, 0, 0, 0);
531 plot(&gc, w, h, dive);
533 cairo_destroy(gc.cr);
538 GtkWidget *dive_profile_widget(void)
542 da = gtk_drawing_area_new();
543 gtk_widget_set_size_request(da, 350, 250);
544 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
549 int process_ui_events(void)
553 while (gtk_events_pending()) {
554 if (gtk_main_iteration_do(0)) {
563 static void fill_computer_list(GtkListStore *store)
566 struct device_list *list = device_list;
568 for (list = device_list ; list->name ; list++) {
569 gtk_list_store_append(store, &iter);
570 gtk_list_store_set(store, &iter,
577 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
579 GtkWidget *hbox, *combo_box;
581 GtkCellRenderer *renderer;
583 hbox = gtk_hbox_new(FALSE, 6);
584 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 3);
586 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
587 fill_computer_list(model);
589 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
590 gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3);
592 renderer = gtk_cell_renderer_text_new();
593 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
594 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
596 return GTK_COMBO_BOX(combo_box);
599 void import_dialog(GtkWidget *w, gpointer data)
602 GtkWidget *dialog, *hbox;
603 GtkComboBox *computer;
604 device_data_t devicedata = {
605 .devname = "/dev/ttyUSB0",
608 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
609 GTK_WINDOW(main_window),
610 GTK_DIALOG_DESTROY_WITH_PARENT,
611 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
612 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
615 computer = dive_computer_selector(dialog);
617 hbox = gtk_hbox_new(FALSE, 6);
618 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, TRUE, 3);
619 devicedata.progress->bar = gtk_progress_bar_new();
620 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress->bar);
622 gtk_widget_show_all(dialog);
623 result = gtk_dialog_run(GTK_DIALOG(dialog));
629 case GTK_RESPONSE_ACCEPT:
630 if (!gtk_combo_box_get_active_iter(computer, &iter))
632 model = gtk_combo_box_get_model(computer);
633 gtk_tree_model_get(model, &iter,
637 devicedata.type = type;
638 devicedata.name = comp;
639 do_import(&devicedata);
644 gtk_widget_destroy(dialog);
647 dive_list_update_dives();
650 void update_progressbar(progressbar_t *progress, double value)
652 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
656 void set_filename(const char *filename)
659 existing_filename = strdup(filename);