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 void on_destroy(GtkWidget* w, gpointer data)
39 static GtkWidget *dive_profile;
41 void repaint_dive(void)
43 update_dive(current_dive);
44 gtk_widget_queue_draw(dive_profile);
47 static char *existing_filename;
49 static void on_info_bar_response(GtkWidget *widget, gint response,
52 if (response == GTK_RESPONSE_OK)
54 gtk_widget_destroy(widget);
55 error_info_bar = NULL;
59 void report_error(GError* error)
66 if (error_info_bar == NULL)
69 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
72 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
73 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
76 error_label = gtk_label_new(error->message);
77 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
78 gtk_container_add(GTK_CONTAINER(container), error_label);
80 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
81 gtk_widget_show_all(main_vbox);
87 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
88 gtk_label_set(GTK_LABEL(error_label), buffer);
92 static void file_open(GtkWidget *w, gpointer data)
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 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
106 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
108 GError *error = NULL;
109 while(filenames != NULL) {
110 filename = (char *)filenames->data;
111 parse_xml_file(filename, &error);
120 filenames = g_slist_next(filenames);
122 g_slist_free(filenames);
124 dive_list_update_dives();
126 gtk_widget_destroy(dialog);
129 static void file_save(GtkWidget *w, gpointer data)
132 dialog = gtk_file_chooser_dialog_new("Save File",
133 GTK_WINDOW(main_window),
134 GTK_FILE_CHOOSER_ACTION_SAVE,
135 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
136 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
138 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
139 if (!existing_filename) {
140 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
142 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
144 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
146 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
147 save_dives(filename);
150 gtk_widget_destroy(dialog);
153 static void quit(GtkWidget *w, gpointer data)
158 static void create_radio(GtkWidget *vbox, const char *name, ...)
161 GtkRadioButton *group = NULL;
162 GtkWidget *box, *label;
164 box = gtk_hbox_new(TRUE, 10);
165 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
167 label = gtk_label_new(name);
168 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
170 va_start(args, name);
177 name = va_arg(args, char *);
180 callback_fn = va_arg(args, void *);
181 enabled = va_arg(args, int);
183 button = gtk_radio_button_new_with_label_from_widget(group, name);
184 group = GTK_RADIO_BUTTON(button);
185 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
186 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
187 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
192 #define UNITCALLBACK(name, type, value) \
193 static void name(GtkWidget *w, gpointer data) \
195 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
196 menu_units.type = value; \
199 static struct units menu_units;
201 UNITCALLBACK(set_meter, length, METERS)
202 UNITCALLBACK(set_feet, length, FEET)
203 UNITCALLBACK(set_bar, pressure, BAR)
204 UNITCALLBACK(set_psi, pressure, PSI)
205 UNITCALLBACK(set_liter, volume, LITER)
206 UNITCALLBACK(set_cuft, volume, CUFT)
207 UNITCALLBACK(set_celsius, temperature, CELSIUS)
208 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
210 static void preferences_dialog(GtkWidget *w, gpointer data)
213 GtkWidget *dialog, *font, *frame, *box;
215 menu_units = output_units;
217 dialog = gtk_dialog_new_with_buttons("Preferences",
218 GTK_WINDOW(main_window),
219 GTK_DIALOG_DESTROY_WITH_PARENT,
220 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
221 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
224 frame = gtk_frame_new("Units");
225 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
227 box = gtk_vbox_new(FALSE, 6);
228 gtk_container_add(GTK_CONTAINER(frame), box);
230 create_radio(box, "Depth:",
231 "Meter", set_meter, (output_units.length == METERS),
232 "Feet", set_feet, (output_units.length == FEET),
235 create_radio(box, "Pressure:",
236 "Bar", set_bar, (output_units.pressure == BAR),
237 "PSI", set_psi, (output_units.pressure == PSI),
240 create_radio(box, "Volume:",
241 "Liter", set_liter, (output_units.volume == LITER),
242 "CuFt", set_cuft, (output_units.volume == CUFT),
245 create_radio(box, "Temperature:",
246 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
247 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
250 font = gtk_font_button_new_with_font(divelist_font);
251 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), font, FALSE, FALSE, 5);
253 gtk_widget_show_all(dialog);
254 result = gtk_dialog_run(GTK_DIALOG(dialog));
255 if (result == GTK_RESPONSE_ACCEPT) {
256 /* Make sure to flush any modified old dive data with old units */
259 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
260 set_divelist_font(divelist_font);
262 output_units = menu_units;
263 update_dive_list_units();
265 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
266 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
267 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
268 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
269 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
271 gtk_widget_destroy(dialog);
274 static void renumber_dialog(GtkWidget *w, gpointer data)
277 GtkWidget *dialog, *frame, *button;
279 dialog = gtk_dialog_new_with_buttons("Renumber",
280 GTK_WINDOW(main_window),
281 GTK_DIALOG_DESTROY_WITH_PARENT,
282 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
283 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
286 frame = gtk_frame_new("New starting number");
287 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame);
289 button = gtk_spin_button_new_with_range(1, 50000, 1);
290 gtk_container_add(GTK_CONTAINER(frame), button);
292 gtk_widget_show_all(dialog);
293 result = gtk_dialog_run(GTK_DIALOG(dialog));
294 if (result == GTK_RESPONSE_ACCEPT) {
295 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
299 gtk_widget_destroy(dialog);
302 static GtkActionEntry menu_items[] = {
303 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
304 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
305 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
306 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
307 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
308 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
309 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
310 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
311 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
313 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
315 static const gchar* ui_string = " \
317 <menubar name=\"MainMenu\"> \
318 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
319 <menuitem name=\"Open\" action=\"OpenFile\" /> \
320 <menuitem name=\"Save\" action=\"SaveFile\" /> \
321 <menuitem name=\"Print\" action=\"Print\" /> \
322 <separator name=\"Separator1\"/> \
323 <menuitem name=\"Import\" action=\"Import\" /> \
324 <separator name=\"Separator2\"/> \
325 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
326 <separator name=\"Separator3\"/> \
327 <menuitem name=\"Quit\" action=\"Quit\" /> \
329 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
330 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
336 static GtkWidget *get_menubar_menu(GtkWidget *window)
338 GtkActionGroup *action_group = gtk_action_group_new("Menu");
339 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
341 GtkUIManager *ui_manager = gtk_ui_manager_new();
342 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
344 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
346 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
347 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
352 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
357 void init_ui(int argc, char **argv)
363 GtkWidget *dive_info;
364 GtkWidget *dive_list;
365 GtkWidget *equipment;
369 gtk_init(&argc, &argv);
372 gconf = gconf_client_get_default();
374 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
375 output_units.length = FEET;
376 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
377 output_units.pressure = PSI;
378 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
379 output_units.volume = CUFT;
380 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
381 output_units.temperature = FAHRENHEIT;
383 divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
385 divelist_font = DIVELIST_DEFAULT_FONT;
387 error_info_bar = NULL;
388 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
389 gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
390 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
393 vbox = gtk_vbox_new(FALSE, 0);
394 gtk_container_add(GTK_CONTAINER(win), vbox);
397 menubar = get_menubar_menu(win);
398 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
400 /* HPane for left the dive list, and right the dive info */
401 paned = gtk_vpaned_new();
402 gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
404 /* Create the actual divelist */
405 dive_list = dive_list_create();
406 gtk_paned_add2(GTK_PANED(paned), dive_list);
408 /* VBox for dive info, and tabs */
409 info_box = gtk_vbox_new(FALSE, 6);
410 gtk_paned_add1(GTK_PANED(paned), info_box);
412 /* Notebook for dive info vs profile vs .. */
413 notebook = gtk_notebook_new();
414 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
415 gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
417 /* Frame for dive profile */
418 dive_profile = dive_profile_widget();
419 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
421 /* Frame for extended dive info */
422 dive_info = extended_dive_info_widget();
423 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
425 /* Frame for dive equipment */
426 equipment = equipment_widget();
427 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
429 gtk_widget_set_app_paintable(win, TRUE);
430 gtk_widget_show_all(win);
440 /* get the filenames the user selects and call the parsing function
442 * return 0 if the user cancelled the dialog
444 int open_import_file_dialog(char *filterpattern, char *filtertext,
445 void(* parse_function)(char *))
450 GtkFileFilter *filter = gtk_file_filter_new ();
451 gtk_file_filter_add_pattern (filter, filterpattern);
452 gtk_file_filter_set_name(filter, filtertext);
453 dialog = gtk_file_chooser_dialog_new("Open File",
454 GTK_WINDOW(main_window),
455 GTK_FILE_CHOOSER_ACTION_OPEN,
456 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
457 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
459 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
460 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
462 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
465 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
466 while(filenames != NULL) {
467 filename = (char *)filenames->data;
468 parse_function(filename);
470 filenames = g_slist_next(filenames);
472 g_slist_free(filenames);
475 gtk_widget_destroy(dialog);
480 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
482 struct dive *dive = current_dive;
483 struct graphics_context gc = { .printer = 0 };
486 w = widget->allocation.width;
487 h = widget->allocation.height;
489 gc.cr = gdk_cairo_create(widget->window);
490 set_source_rgb(&gc, 0, 0, 0);
494 plot(&gc, w, h, dive);
496 cairo_destroy(gc.cr);
501 GtkWidget *dive_profile_widget(void)
505 da = gtk_drawing_area_new();
506 gtk_widget_set_size_request(da, 350, 250);
507 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
512 int process_ui_events(void)
516 while (gtk_events_pending()) {
517 if (gtk_main_iteration_do(0)) {
526 static void fill_computer_list(GtkListStore *store)
529 struct device_list *list = device_list;
531 for (list = device_list ; list->name ; list++) {
532 gtk_list_store_append(store, &iter);
533 gtk_list_store_set(store, &iter,
540 static GtkComboBox *dive_computer_selector(GtkWidget *dialog)
542 GtkWidget *hbox, *combo_box;
544 GtkCellRenderer *renderer;
546 hbox = gtk_hbox_new(FALSE, 6);
547 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 3);
549 model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
550 fill_computer_list(model);
552 combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
553 gtk_box_pack_start(GTK_BOX(hbox), combo_box, FALSE, TRUE, 3);
555 renderer = gtk_cell_renderer_text_new();
556 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
557 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
559 return GTK_COMBO_BOX(combo_box);
562 void import_dialog(GtkWidget *w, gpointer data)
565 GtkWidget *dialog, *hbox;
566 GtkComboBox *computer;
567 device_data_t devicedata = {
568 .devname = "/dev/ttyUSB0",
571 dialog = gtk_dialog_new_with_buttons("Import from dive computer",
572 GTK_WINDOW(main_window),
573 GTK_DIALOG_DESTROY_WITH_PARENT,
574 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
575 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
578 computer = dive_computer_selector(dialog);
580 hbox = gtk_hbox_new(FALSE, 6);
581 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, TRUE, 3);
582 devicedata.progress->bar = gtk_progress_bar_new();
583 gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress->bar);
585 gtk_widget_show_all(dialog);
586 result = gtk_dialog_run(GTK_DIALOG(dialog));
592 case GTK_RESPONSE_ACCEPT:
593 if (!gtk_combo_box_get_active_iter(computer, &iter))
595 model = gtk_combo_box_get_model(computer);
596 gtk_tree_model_get(model, &iter,
600 devicedata.type = type;
601 devicedata.name = comp;
602 do_import(&devicedata);
607 gtk_widget_destroy(dialog);
610 dive_list_update_dives();
613 void update_progressbar(progressbar_t *progress, double value)
615 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);