]> git.tdb.fi Git - ext/subsurface.git/blobdiff - main.c
Start "output unit management" support
[ext/subsurface.git] / main.c
diff --git a/main.c b/main.c
index c9cda2b472ec56db5398b2aa729e768ed4cb8bc3..9ef4fa0b4b6b9eff42fa0b28d3c506bc6d9b35a6 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,9 +4,17 @@
 #include <time.h>
 
 #include "dive.h"
+#include "divelist.h"
 #include "display.h"
 
 GtkWidget *main_window;
+GtkWidget *main_vbox;
+GtkWidget *error_info_bar;
+GtkWidget *error_label;
+int        error_count;
+struct DiveList   dive_list;
+
+struct units output_units;
 
 static int sortfn(const void *_a, const void *_b)
 {
@@ -85,6 +93,49 @@ void repaint_dive(void)
 
 static char *existing_filename;
 
+static void on_info_bar_response(GtkWidget *widget, gint response,
+                                 gpointer data)
+{
+       if (response == GTK_RESPONSE_OK)
+       {
+               gtk_widget_destroy(widget);
+               error_info_bar = NULL;
+       }
+}
+
+static void report_error(GError* error)
+{
+       if (error == NULL)
+       {
+               return;
+       }
+       
+       if (error_info_bar == NULL)
+       {
+               error_count = 1;
+               error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
+                                                              GTK_RESPONSE_OK,
+                                                              NULL);
+               g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
+               gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
+                                             GTK_MESSAGE_ERROR);
+               
+               error_label = gtk_label_new(error->message);
+               GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
+               gtk_container_add(GTK_CONTAINER(container), error_label);
+               
+               gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
+               gtk_widget_show_all(main_vbox);
+       }
+       else
+       {
+               error_count++;
+               char buffer[256];
+               snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
+               gtk_label_set(GTK_LABEL(error_label), buffer);
+       }
+}
+
 static void file_open(GtkWidget *w, gpointer data)
 {
        GtkWidget *dialog;
@@ -94,12 +145,30 @@ static void file_open(GtkWidget *w, gpointer data)
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                NULL);
+       gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
 
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+               GSList *filenames;
                char *filename;
-               filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
-               printf("Open: '%s'\n", filename);
-               g_free(filename);
+               filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
+               
+               GError *error = NULL;
+               while(filenames != NULL) {
+                       filename = (char *)filenames->data;
+                       parse_xml_file(filename, &error);
+                       if (error != NULL)
+                       {
+                               report_error(error);
+                               g_error_free(error);
+                               error = NULL;
+                       }
+                       
+                       g_free(filename);
+                       filenames = g_slist_next(filenames);
+               }
+               g_slist_free(filenames);
+               report_dives();
+               dive_list_update_dives(dive_list);
        }
        gtk_widget_destroy(dialog);
 }
@@ -133,11 +202,23 @@ static void quit(GtkWidget *w, gpointer data)
        gtk_main_quit();
 }
 
+static void imperial(GtkWidget *w, gpointer data)
+{
+       output_units = IMPERIAL_units;
+}
+
+static void metric(GtkWidget *w, gpointer data)
+{
+       output_units = SI_units;
+}
+
 static GtkActionEntry menu_items[] = {
-       { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
+       { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
        { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
        { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
        { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
+       { "Metric",         NULL, "Metric", NULL, NULL, G_CALLBACK(metric) },
+       { "Imperial",       NULL, "Imperial", NULL, NULL, G_CALLBACK(imperial) },
 };
 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
 
@@ -147,6 +228,10 @@ static const gchar* ui_string = " \
                        <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
                                <menuitem name=\"Open\" action=\"OpenFile\" /> \
                                <menuitem name=\"Save\" action=\"SaveFile\" /> \
+                               <separator name=\"Seperator\"/> \
+                               <menuitem name=\"Metric\" action=\"Metric\" /> \
+                               <menuitem name=\"Imperial\" action=\"Imperial\" /> \
+                               <separator name=\"Seperator\"/> \
                                <menuitem name=\"Quit\" action=\"Quit\" /> \
                        </menu> \
                </menubar> \
@@ -173,70 +258,82 @@ int main(int argc, char **argv)
 {
        int i;
        GtkWidget *win;
-       GtkWidget *divelist;
-       GtkWidget *table;
+       GtkWidget *paned;
+       GtkWidget *info_box;
        GtkWidget *notebook;
        GtkWidget *frame;
+       GtkWidget *dive_info;
        GtkWidget *menubar;
        GtkWidget *vbox;
 
+       output_units = SI_units;
        parse_xml_init();
 
        gtk_init(&argc, &argv);
 
-       for (i = 1; i < argc; i++) {
-               const char *a = argv[i];
-
-               if (a[0] == '-') {
-                       parse_argument(a);
-                       continue;
-               }
-               parse_xml_file(a);
-       }
-
-       report_dives();
-
+       error_info_bar = NULL;
        win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
        main_window = win;
 
        vbox = gtk_vbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER(win), vbox);
+       main_vbox = vbox;
 
        menubar = get_menubar_menu(win);
        gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
 
-       /* Table for the list of dives, cairo window, and dive info */
-       table = gtk_table_new(2, 2, FALSE);
-       gtk_container_set_border_width(GTK_CONTAINER(table), 5);
-       gtk_box_pack_end(GTK_BOX(vbox), table, TRUE, TRUE, 0);
-       gtk_widget_show(table);
+       /* HPane for left the dive list, and right the dive info */
+       paned = gtk_hpaned_new();
+       gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
 
-       /* Create the atual divelist */
-       divelist = create_dive_list();
-       gtk_table_attach(GTK_TABLE(table), divelist, 0, 1, 0, 2,
-               0, GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0);
+       /* Create the actual divelist */
+       dive_list = dive_list_create();
+       gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget);
+
+       /* VBox for dive info, and tabs */
+       info_box = gtk_vbox_new(FALSE, 6);
+       gtk_paned_add2(GTK_PANED(paned), info_box);
 
        /* Frame for minimal dive info */
        frame = dive_info_frame();
-       gtk_table_attach(GTK_TABLE(table), frame, 1, 2, 0, 1,
-                GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
+       gtk_box_pack_start(GTK_BOX(info_box), frame, FALSE, TRUE, 6);
 
        /* Notebook for dive info vs profile vs .. */
        notebook = gtk_notebook_new();
-       gtk_table_attach_defaults(GTK_TABLE(table), notebook, 1, 2, 1, 2);
+       gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
 
        /* Frame for dive profile */
-       frame = dive_profile_frame();
-       gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, gtk_label_new("Dive Profile"));
-       dive_profile = frame;
+       dive_profile = dive_profile_widget();
+       gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
 
        /* Frame for extended dive info */
-       frame = extended_dive_info_frame();
-       gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, gtk_label_new("Extended dive Info"));
+       dive_info = extended_dive_info_widget();
+       gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Extended Dive Info"));
 
        gtk_widget_set_app_paintable(win, TRUE);
        gtk_widget_show_all(win);
+       
+       for (i = 1; i < argc; i++) {
+               const char *a = argv[i];
+
+               if (a[0] == '-') {
+                       parse_argument(a);
+                       continue;
+               }
+               GError *error = NULL;
+               parse_xml_file(a, &error);
+               
+               if (error != NULL)
+               {
+                       report_error(error);
+                       g_error_free(error);
+                       error = NULL;
+               }
+       }
+
+       report_dives();
+       dive_list_update_dives(dive_list);
 
        gtk_main();
        return 0;