6 #include <gconf/gconf-client.h>
12 GtkWidget *main_window;
14 GtkWidget *error_info_bar;
15 GtkWidget *error_label;
18 #define DIVELIST_DEFAULT_FONT "Sans 8"
19 const char *divelist_font;
22 struct units output_units;
24 #define GCONF_NAME(x) "/apps/subsurface/" #x
26 static int sortfn(const void *_a, const void *_b)
28 const struct dive *a = *(void **)_a;
29 const struct dive *b = *(void **)_b;
31 if (a->when < b->when)
33 if (a->when > b->when)
39 * This doesn't really report anything at all. We just sort the
40 * dives, the GUI does the reporting
42 void report_dives(void)
46 qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
48 for (i = 1; i < dive_table.nr; i++) {
49 struct dive **pp = &dive_table.dives[i-1];
50 struct dive *prev = pp[0];
51 struct dive *dive = pp[1];
54 if (prev->when + prev->duration.seconds < dive->when)
57 merged = try_to_merge(prev, dive);
65 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
67 /* Redo the new 'i'th dive */
72 static void parse_argument(const char *arg)
74 const char *p = arg+1;
82 fprintf(stderr, "Bad argument '%s'\n", arg);
88 static void on_destroy(GtkWidget* w, gpointer data)
93 static GtkWidget *dive_profile;
95 void update_dive(struct dive *new_dive)
97 static struct dive *buffered_dive;
98 struct dive *old_dive = buffered_dive;
101 flush_dive_info_changes(old_dive);
102 flush_dive_equipment_changes(old_dive);
103 flush_divelist(old_dive);
106 show_dive_info(new_dive);
107 show_dive_equipment(new_dive);
109 buffered_dive = new_dive;
112 void repaint_dive(void)
114 update_dive(current_dive);
115 gtk_widget_queue_draw(dive_profile);
118 static char *existing_filename;
120 static void on_info_bar_response(GtkWidget *widget, gint response,
123 if (response == GTK_RESPONSE_OK)
125 gtk_widget_destroy(widget);
126 error_info_bar = NULL;
130 void report_error(GError* error)
137 if (error_info_bar == NULL)
140 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
143 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
144 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
147 error_label = gtk_label_new(error->message);
148 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
149 gtk_container_add(GTK_CONTAINER(container), error_label);
151 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
152 gtk_widget_show_all(main_vbox);
158 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
159 gtk_label_set(GTK_LABEL(error_label), buffer);
163 static void file_open(GtkWidget *w, gpointer data)
166 dialog = gtk_file_chooser_dialog_new("Open File",
167 GTK_WINDOW(main_window),
168 GTK_FILE_CHOOSER_ACTION_OPEN,
169 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
170 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
172 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
174 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
177 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
179 GError *error = NULL;
180 while(filenames != NULL) {
181 filename = (char *)filenames->data;
182 parse_xml_file(filename, &error);
191 filenames = g_slist_next(filenames);
193 g_slist_free(filenames);
195 dive_list_update_dives();
197 gtk_widget_destroy(dialog);
200 static void file_save(GtkWidget *w, gpointer data)
203 dialog = gtk_file_chooser_dialog_new("Save File",
204 GTK_WINDOW(main_window),
205 GTK_FILE_CHOOSER_ACTION_SAVE,
206 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
207 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
209 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
210 if (!existing_filename) {
211 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
213 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
215 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
217 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
218 save_dives(filename);
221 gtk_widget_destroy(dialog);
224 static void quit(GtkWidget *w, gpointer data)
229 static void create_radio(GtkWidget *vbox, const char *name, ...)
232 GtkRadioButton *group = NULL;
233 GtkWidget *box, *label;
235 box = gtk_hbox_new(TRUE, 10);
236 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
238 label = gtk_label_new(name);
239 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
241 va_start(args, name);
248 name = va_arg(args, char *);
251 callback_fn = va_arg(args, void *);
252 enabled = va_arg(args, int);
254 button = gtk_radio_button_new_with_label_from_widget(group, name);
255 group = GTK_RADIO_BUTTON(button);
256 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
257 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
258 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
263 #define UNITCALLBACK(name, type, value) \
264 static void name(GtkWidget *w, gpointer data) \
266 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
267 menu_units.type = value; \
270 static struct units menu_units;
272 UNITCALLBACK(set_meter, length, METERS)
273 UNITCALLBACK(set_feet, length, FEET)
274 UNITCALLBACK(set_bar, pressure, BAR)
275 UNITCALLBACK(set_psi, pressure, PSI)
276 UNITCALLBACK(set_liter, volume, LITER)
277 UNITCALLBACK(set_cuft, volume, CUFT)
278 UNITCALLBACK(set_celsius, temperature, CELSIUS)
279 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
281 static void preferences_dialog(GtkWidget *w, gpointer data)
284 GtkWidget *dialog, *font, *frame, *box;
286 menu_units = output_units;
288 dialog = gtk_dialog_new_with_buttons("Preferences",
289 GTK_WINDOW(main_window),
290 GTK_DIALOG_DESTROY_WITH_PARENT,
291 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
292 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
295 frame = gtk_frame_new("Units");
296 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
298 box = gtk_vbox_new(FALSE, 6);
299 gtk_container_add(GTK_CONTAINER(frame), box);
301 create_radio(box, "Depth:",
302 "Meter", set_meter, (output_units.length == METERS),
303 "Feet", set_feet, (output_units.length == FEET),
306 create_radio(box, "Pressure:",
307 "Bar", set_bar, (output_units.pressure == BAR),
308 "PSI", set_psi, (output_units.pressure == PSI),
311 create_radio(box, "Volume:",
312 "Liter", set_liter, (output_units.volume == LITER),
313 "CuFt", set_cuft, (output_units.volume == CUFT),
316 create_radio(box, "Temperature:",
317 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
318 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
321 font = gtk_font_button_new_with_font(divelist_font);
322 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), font, FALSE, FALSE, 5);
324 gtk_widget_show_all(dialog);
325 result = gtk_dialog_run(GTK_DIALOG(dialog));
326 if (result == GTK_RESPONSE_ACCEPT) {
327 /* Make sure to flush any modified old dive data with old units */
330 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
331 set_divelist_font(divelist_font);
333 output_units = menu_units;
334 update_dive_list_units();
336 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
337 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
338 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
339 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
340 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
342 gtk_widget_destroy(dialog);
345 static void renumber_dives(int nr)
349 for (i = 0; i < dive_table.nr; i++) {
350 struct dive *dive = dive_table.dives[i];
351 dive->number = nr + i;
355 static void renumber_dialog(GtkWidget *w, gpointer data)
358 GtkWidget *dialog, *frame, *button;
360 dialog = gtk_dialog_new_with_buttons("Renumber",
361 GTK_WINDOW(main_window),
362 GTK_DIALOG_DESTROY_WITH_PARENT,
363 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
364 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
367 frame = gtk_frame_new("New starting number");
368 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame);
370 button = gtk_spin_button_new_with_range(1, 50000, 1);
371 gtk_container_add(GTK_CONTAINER(frame), button);
373 gtk_widget_show_all(dialog);
374 result = gtk_dialog_run(GTK_DIALOG(dialog));
375 if (result == GTK_RESPONSE_ACCEPT) {
376 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
380 gtk_widget_destroy(dialog);
383 static GtkActionEntry menu_items[] = {
384 { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
385 { "LogMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
386 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
387 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
388 { "Print", GTK_STOCK_PRINT, NULL, "<control>P", NULL, G_CALLBACK(do_print) },
389 { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
390 { "Preferences", NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
391 { "Renumber", NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
392 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
394 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
396 static const gchar* ui_string = " \
398 <menubar name=\"MainMenu\"> \
399 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
400 <menuitem name=\"Open\" action=\"OpenFile\" /> \
401 <menuitem name=\"Save\" action=\"SaveFile\" /> \
402 <menuitem name=\"Print\" action=\"Print\" /> \
403 <separator name=\"Separator1\"/> \
404 <menuitem name=\"Import\" action=\"Import\" /> \
405 <separator name=\"Separator2\"/> \
406 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
407 <separator name=\"Separator3\"/> \
408 <menuitem name=\"Quit\" action=\"Quit\" /> \
410 <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
411 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
417 static GtkWidget *get_menubar_menu(GtkWidget *window)
419 GtkActionGroup *action_group = gtk_action_group_new("Menu");
420 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
422 GtkUIManager *ui_manager = gtk_ui_manager_new();
423 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
425 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
427 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
428 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
433 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
438 int main(int argc, char **argv)
445 GtkWidget *dive_info;
446 GtkWidget *dive_list;
447 GtkWidget *equipment;
451 output_units = SI_units;
454 gtk_init(&argc, &argv);
457 gconf = gconf_client_get_default();
459 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
460 output_units.length = FEET;
461 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
462 output_units.pressure = PSI;
463 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
464 output_units.volume = CUFT;
465 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
466 output_units.temperature = FAHRENHEIT;
468 divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
470 divelist_font = DIVELIST_DEFAULT_FONT;
472 error_info_bar = NULL;
473 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
474 gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
475 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
478 vbox = gtk_vbox_new(FALSE, 0);
479 gtk_container_add(GTK_CONTAINER(win), vbox);
482 menubar = get_menubar_menu(win);
483 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
485 /* HPane for left the dive list, and right the dive info */
486 paned = gtk_vpaned_new();
487 gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
489 /* Create the actual divelist */
490 dive_list = dive_list_create();
491 gtk_paned_add2(GTK_PANED(paned), dive_list);
493 /* VBox for dive info, and tabs */
494 info_box = gtk_vbox_new(FALSE, 6);
495 gtk_paned_add1(GTK_PANED(paned), info_box);
497 /* Notebook for dive info vs profile vs .. */
498 notebook = gtk_notebook_new();
499 g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
500 gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
502 /* Frame for dive profile */
503 dive_profile = dive_profile_widget();
504 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
506 /* Frame for extended dive info */
507 dive_info = extended_dive_info_widget();
508 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
510 /* Frame for dive equipment */
511 equipment = equipment_widget();
512 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
514 gtk_widget_set_app_paintable(win, TRUE);
515 gtk_widget_show_all(win);
517 for (i = 1; i < argc; i++) {
518 const char *a = argv[i];
524 GError *error = NULL;
525 parse_xml_file(a, &error);
536 dive_list_update_dives();