]> git.tdb.fi Git - ext/subsurface.git/blobdiff - gtk-gui.c
Add some more cochran data parsing code/comments
[ext/subsurface.git] / gtk-gui.c
index 402e91a6e5f2f1f29c15e9ea07129f1e9a2df019..1e053e52b22837b3ed9df9554b337e25bf9da749 100644 (file)
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -31,6 +31,39 @@ static GtkWidget *dive_profile;
 
 visible_cols_t visible_cols = {TRUE, FALSE};
 
+static const char *default_dive_computer;
+static const char *default_dive_computer_device;
+
+static int is_default_dive_computer(const char *name)
+{
+       return default_dive_computer && !strcmp(name, default_dive_computer);
+}
+
+static int is_default_dive_computer_device(const char *name)
+{
+       return default_dive_computer_device && !strcmp(name, default_dive_computer_device);
+}
+
+static void set_default_dive_computer(const char *name)
+{
+       if (!name || !*name)
+               return;
+       if (is_default_dive_computer(name))
+               return;
+       default_dive_computer = name;
+       subsurface_set_conf("dive_computer", PREF_STRING, name);
+}
+
+static void set_default_dive_computer_device(const char *name)
+{
+       if (!name || !*name)
+               return;
+       if (is_default_dive_computer_device(name))
+               return;
+       default_dive_computer_device = name;
+       subsurface_set_conf("dive_computer_device", PREF_STRING, name);
+}
+
 void repaint_dive(void)
 {
        update_dive(current_dive);
@@ -296,6 +329,8 @@ UNITCALLBACK(set_liter, volume, LITER)
 UNITCALLBACK(set_cuft, volume, CUFT)
 UNITCALLBACK(set_celsius, temperature, CELSIUS)
 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
+UNITCALLBACK(set_kg, weight, KG)
+UNITCALLBACK(set_lbs, weight, LBS)
 
 #define OPTIONCALLBACK(name, option) \
 static void name(GtkWidget *w, gpointer data) \
@@ -357,6 +392,11 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
                "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
                NULL);
 
+       create_radio(box, "Weight:",
+               "kg", set_kg, (output_units.weight == KG),
+               "lbs",  set_lbs, (output_units.weight == LBS),
+               NULL);
+
        frame = gtk_frame_new("Columns");
        gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
 
@@ -409,13 +449,16 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
                subsurface_set_conf("psi", PREF_BOOL, BOOL_TO_PTR(output_units.pressure == PSI));
                subsurface_set_conf("cuft", PREF_BOOL, BOOL_TO_PTR(output_units.volume == CUFT));
                subsurface_set_conf("fahrenheit", PREF_BOOL, BOOL_TO_PTR(output_units.temperature == FAHRENHEIT));
+               subsurface_set_conf("lbs", PREF_BOOL, BOOL_TO_PTR(output_units.weight == LBS));
                subsurface_set_conf("TEMPERATURE", PREF_BOOL, BOOL_TO_PTR(visible_cols.temperature));
                subsurface_set_conf("CYLINDER", PREF_BOOL, BOOL_TO_PTR(visible_cols.cylinder));
                subsurface_set_conf("NITROX", PREF_BOOL, BOOL_TO_PTR(visible_cols.nitrox));
                subsurface_set_conf("SAC", PREF_BOOL, BOOL_TO_PTR(visible_cols.sac));
                subsurface_set_conf("OTU", PREF_BOOL, BOOL_TO_PTR(visible_cols.otu));
                subsurface_set_conf("divelist_font", PREF_STRING, divelist_font);
-               subsurface_close_conf();
+
+               /* Flush the changes out to the system */
+               subsurface_flush_conf();
        }
        gtk_widget_destroy(dialog);
 }
@@ -597,13 +640,13 @@ static const gchar* ui_string = " \
                                <menuitem name=\"Save\" action=\"SaveFile\" /> \
                                <menuitem name=\"Print\" action=\"Print\" /> \
                                <separator name=\"Separator1\"/> \
-                               <menuitem name=\"Import\" action=\"Import\" /> \
-                               <separator name=\"Separator2\"/> \
                                <menuitem name=\"Preferences\" action=\"Preferences\" /> \
-                               <separator name=\"Separator3\"/> \
+                               <separator name=\"Separator2\"/> \
                                <menuitem name=\"Quit\" action=\"Quit\" /> \
                        </menu> \
                        <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
+                               <menuitem name=\"Import\" action=\"Import\" /> \
+                               <separator name=\"Separator\"/> \
                                <menuitem name=\"Renumber\" action=\"Renumber\" /> \
                                <menu name=\"View\" action=\"ViewMenuAction\"> \
                                        <menuitem name=\"List\" action=\"ViewList\" /> \
@@ -670,6 +713,8 @@ void init_ui(int *argcp, char ***argvp)
                output_units.volume = CUFT;
        if (subsurface_get_conf("fahrenheit", PREF_BOOL))
                output_units.temperature = FAHRENHEIT;
+       if (subsurface_get_conf("lbs", PREF_BOOL))
+               output_units.weight = LBS;
        /* an unset key is FALSE - all these are hidden by default */
        visible_cols.cylinder = PTR_TO_BOOL(subsurface_get_conf("CYLINDER", PREF_BOOL));
        visible_cols.temperature = PTR_TO_BOOL(subsurface_get_conf("TEMPERATURE", PREF_BOOL));
@@ -679,6 +724,9 @@ void init_ui(int *argcp, char ***argvp)
 
        divelist_font = subsurface_get_conf("divelist_font", PREF_STRING);
 
+       default_dive_computer = subsurface_get_conf("dive_computer", PREF_STRING);
+       default_dive_computer_device = subsurface_get_conf("dive_computer_device", PREF_STRING);
+
        error_info_bar = NULL;
        win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        g_set_application_name ("subsurface");
@@ -759,6 +807,11 @@ void run_ui(void)
        gtk_main();
 }
 
+void exit_ui(void)
+{
+       subsurface_close_conf();
+}
+
 typedef struct {
        cairo_rectangle_int_t rect;
        const char *text;
@@ -857,19 +910,22 @@ int process_ui_events(void)
        return ret;
 }
 
-
-static void fill_computer_list(GtkListStore *store)
+static int fill_computer_list(GtkListStore *store)
 {
+       int index = -1, i;
        GtkTreeIter iter;
        struct device_list *list = device_list;
 
-       for (list = device_list ; list->name ; list++) {
+       for (list = device_list, i = 0 ; list->name ; list++, i++) {
                gtk_list_store_append(store, &iter);
                gtk_list_store_set(store, &iter,
                        0, list->name,
                        1, list->type,
                        -1);
+               if (is_default_dive_computer(list->name))
+                       index = i;
        }
+       return index;
 }
 
 static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
@@ -877,12 +933,13 @@ static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
        GtkWidget *hbox, *combo_box, *frame;
        GtkListStore *model;
        GtkCellRenderer *renderer;
+       int default_index;
 
        hbox = gtk_hbox_new(FALSE, 6);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
 
        model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
-       fill_computer_list(model);
+       default_index = fill_computer_list(model);
 
        frame = gtk_frame_new("Dive computer");
        gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3);
@@ -894,9 +951,19 @@ static GtkComboBox *dive_computer_selector(GtkWidget *vbox)
        gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
        gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text", 0, NULL);
 
+       gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index);
+
        return GTK_COMBO_BOX(combo_box);
 }
 
+const char *subsurface_device_name()
+{
+       if (!default_dive_computer_device || !*default_dive_computer_device)
+               return subsurface_USB_name();
+       else
+               return default_dive_computer_device;
+}
+
 static GtkEntry *dive_computer_device(GtkWidget *vbox)
 {
        GtkWidget *hbox, *entry, *frame;
@@ -909,7 +976,7 @@ static GtkEntry *dive_computer_device(GtkWidget *vbox)
 
        entry = gtk_entry_new();
        gtk_container_add(GTK_CONTAINER(frame), entry);
-       gtk_entry_set_text(GTK_ENTRY(entry), subsurface_USB_name());
+       gtk_entry_set_text(GTK_ENTRY(entry), subsurface_device_name());
 
        return GTK_ENTRY(entry);
 }
@@ -971,10 +1038,33 @@ static void do_import_file(gpointer data, gpointer user_data)
        }
 }
 
+static GtkWidget *import_dive_computer(device_data_t *data, GtkDialog *dialog)
+{
+       GError *error;
+       GtkWidget *vbox, *info, *container, *label, *button;
+
+       error = do_import(data);
+       if (!error)
+               return NULL;
+
+       button = gtk_dialog_get_widget_for_response(dialog, GTK_RESPONSE_ACCEPT);
+       gtk_button_set_use_stock(GTK_BUTTON(button), 0);
+       gtk_button_set_label(GTK_BUTTON(button), "Retry");
+
+       vbox = gtk_dialog_get_content_area(dialog);
+
+       info = gtk_info_bar_new();
+       container = gtk_info_bar_get_content_area(GTK_INFO_BAR(info));
+       label = gtk_label_new(error->message);
+       gtk_container_add(GTK_CONTAINER(container), label);
+       gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
+       return info;
+}
+
 void import_dialog(GtkWidget *w, gpointer data)
 {
        int result;
-       GtkWidget *dialog, *hbox, *vbox, *label;
+       GtkWidget *dialog, *hbox, *vbox, *label, *info = NULL;
        GtkComboBox *computer;
        GtkEntry *device;
        GtkWidget *XMLchooser;
@@ -1000,6 +1090,7 @@ void import_dialog(GtkWidget *w, gpointer data)
        devicedata.progress.bar = gtk_progress_bar_new();
        gtk_container_add(GTK_CONTAINER(hbox), devicedata.progress.bar);
 
+repeat:
        gtk_widget_show_all(dialog);
        result = gtk_dialog_run(GTK_DIALOG(dialog));
        switch (result) {
@@ -1011,6 +1102,8 @@ void import_dialog(GtkWidget *w, gpointer data)
        case GTK_RESPONSE_ACCEPT:
                /* what happened - did the user pick a file? In that case
                 * we ignore whether a dive computer model was picked */
+               if (info)
+                       gtk_widget_destroy(info);
                list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(XMLchooser));
                if (g_slist_length(list) == 0) {
                        if (!gtk_combo_box_get_active_iter(computer, &iter))
@@ -1023,7 +1116,11 @@ void import_dialog(GtkWidget *w, gpointer data)
                        devicedata.type = type;
                        devicedata.name = comp;
                        devicedata.devname = gtk_entry_get_text(device);
-                       do_import(&devicedata);
+                       set_default_dive_computer(devicedata.name);
+                       set_default_dive_computer_device(devicedata.devname);
+                       info = import_dive_computer(&devicedata, GTK_DIALOG(dialog));
+                       if (info)
+                               goto repeat;
                } else {
                        g_slist_foreach(list,do_import_file,NULL);
                        g_slist_free(list);
@@ -1042,6 +1139,10 @@ void update_progressbar(progressbar_t *progress, double value)
        gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), value);
 }
 
+void update_progressbar_text(progressbar_t *progress, const char *text)
+{
+       gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), text);
+}
 
 void set_filename(const char *filename)
 {