]> git.tdb.fi Git - ext/subsurface.git/blobdiff - divelist.c
Fix minor coding standard issues introduced by my last commit
[ext/subsurface.git] / divelist.c
index bb2a3e3a101a44017b28a7a6640a7d350e74218e..81779f06d25af2a4e10a05dfa62e953cdec0be7c 100644 (file)
@@ -25,8 +25,8 @@ struct DiveList {
        GtkWidget    *tree_view;
        GtkWidget    *container_widget;
        GtkListStore *model;
-       GtkTreeViewColumn *date, *depth, *duration, *location;
-       GtkTreeViewColumn *temperature, *cylinder, *nitrox, *sac;
+       GtkTreeViewColumn *nr, *date, *depth, *duration, *location;
+       GtkTreeViewColumn *temperature, *cylinder, *nitrox, *sac, *otu;
        int changed;
 };
 
@@ -38,6 +38,7 @@ static struct DiveList dive_list;
  */
 enum {
        DIVE_INDEX = 0,
+       DIVE_NR,                /* int: dive->nr */
        DIVE_DATE,              /* time_t: dive->when */
        DIVE_DEPTH,             /* int: dive->maxdepth in mm */
        DIVE_DURATION,          /* int: in seconds */
@@ -50,17 +51,42 @@ enum {
        DIVELIST_COLUMNS
 };
 
+static GList *selected_dives;
+
 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
 {
        GtkTreeIter iter;
        GValue value = {0, };
+       GtkTreePath *path;
 
-       if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
-               return;
+       int nr_selected = gtk_tree_selection_count_selected_rows(selection);
 
-       gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
-       selected_dive = g_value_get_int(&value);
-       repaint_dive();
+       if (selected_dives) {
+               g_list_foreach (selected_dives, (GFunc) gtk_tree_path_free, NULL);
+               g_list_free (selected_dives);
+       }
+       selected_dives = gtk_tree_selection_get_selected_rows(selection, NULL);
+
+       switch (nr_selected) {
+       case 0: /* keep showing the last selected dive */
+               return;
+       case 1: 
+               /* just pick that dive as selected */
+               path = g_list_nth_data(selected_dives, 0);
+               if (gtk_tree_model_get_iter(model, &iter, path)) {
+                       gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
+                       selected_dive = g_value_get_int(&value);
+                       repaint_dive();
+               }
+               return;
+       default: /* multiple selections - what now? At this point I
+                 * don't want to change the selected dive unless
+                 * there is exactly one dive selected; not sure this
+                 * is the most intuitive solution.
+                 * I do however want to keep around which dives have
+                 * been selected */
+               return;
+       }
 }
 
 static void date_data_func(GtkTreeViewColumn *col,
@@ -197,7 +223,6 @@ static void sac_data_func(GtkTreeViewColumn *col,
                          gpointer data)
 {
        int value;
-       const double liters_per_cuft = 28.317;
        const char *fmt;
        char buffer[16];
        double sac;
@@ -216,7 +241,7 @@ static void sac_data_func(GtkTreeViewColumn *col,
                break;
        case CUFT:
                fmt = "%4.2f";
-               sac /= liters_per_cuft;
+               sac = ml_to_cuft(sac * 1000);
                break;
        }
        snprintf(buffer, sizeof(buffer), fmt, sac);
@@ -274,6 +299,7 @@ static double calculate_airuse(struct dive *dive)
        int i;
 
        for (i = 0; i < MAX_CYLINDERS; i++) {
+               pressure_t start, end;
                cylinder_t *cyl = dive->cylinder + i;
                int size = cyl->type.size.mliter;
                double kilo_atm;
@@ -281,7 +307,9 @@ static double calculate_airuse(struct dive *dive)
                if (!size)
                        continue;
 
-               kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
+               start = cyl->start.mbar ? cyl->start : cyl->sample_start;
+               end = cyl->end.mbar ? cyl->end : cyl->sample_end;
+               kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
 
                /* Liters of air at 1 atm == milliliters at 1k atm*/
                airuse += kilo_atm * size;
@@ -289,23 +317,30 @@ static double calculate_airuse(struct dive *dive)
        return airuse;
 }
 
-static void get_sac(struct dive *dive, int *val)
+static int calculate_sac(struct dive *dive)
 {
        double airuse, pressure, sac;
 
-       *val = 0;
        airuse = calculate_airuse(dive);
        if (!airuse)
-               return;
+               return 0;
        if (!dive->duration.seconds)
-               return;
+               return 0;
 
        /* Mean pressure in atm: 1 atm per 10m */
        pressure = 1 + (dive->meandepth.mm / 10000.0);
        sac = airuse / pressure * 60 / dive->duration.seconds;
 
        /* milliliters per minute.. */
-       *val = sac * 1000;
+       return sac * 1000;
+}
+
+void update_cylinder_related_info(struct dive *dive)
+{
+       if (dive != NULL) {
+               dive->sac = calculate_sac(dive);
+               dive->otu = calculate_otu(dive);
+       }
 }
 
 static void get_string(char **str, const char *s)
@@ -338,21 +373,20 @@ static void fill_one_dive(struct dive *dive,
                          GtkTreeModel *model,
                          GtkTreeIter *iter)
 {
-       int sac;
        char *location, *cylinder;
 
        get_cylinder(dive, &cylinder);
        get_location(dive, &location);
-       get_sac(dive, &sac);
 
        /*
         * We only set the fields that changed: the strings.
         * The core data itself is unaffected by units
         */
        gtk_list_store_set(GTK_LIST_STORE(model), iter,
+               DIVE_NR, dive->number,
                DIVE_LOCATION, location,
                DIVE_CYLINDER, cylinder,
-               DIVE_SAC, sac,
+               DIVE_SAC, dive->sac,
                DIVE_OTU, dive->otu,
                -1);
 }
@@ -396,32 +430,25 @@ void update_dive_list_units(void)
        const char *unit;
        GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
 
-       switch (output_units.length) {
-       case METERS:
-               unit = "m";
-               break;
-       case FEET:
-               unit = "ft";
-               break;
-       }
+       (void) get_depth_units(0, NULL, &unit);
        gtk_tree_view_column_set_title(dive_list.depth, unit);
 
-       switch (output_units.temperature) {
-       case CELSIUS:
-               unit = UTF8_DEGREE "C";
-               break;
-       case FAHRENHEIT:
-               unit = UTF8_DEGREE "F";
-               break;
-       case KELVIN:
-               unit = "Kelvin";
-               break;
-       }
+       (void) get_temp_units(0, &unit);
        gtk_tree_view_column_set_title(dive_list.temperature, unit);
 
        gtk_tree_model_foreach(model, set_one_dive, NULL);
 }
 
+void update_dive_list_col_visibility(void)
+{
+       gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
+       gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
+       gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
+       gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
+       gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
+       return;
+}
+
 static void fill_dive_list(void)
 {
        int i;
@@ -433,10 +460,11 @@ static void fill_dive_list(void)
        for (i = 0; i < dive_table.nr; i++) {
                struct dive *dive = dive_table.dives[i];
 
-               dive->otu = calculate_otu(dive);
+               update_cylinder_related_info(dive);
                gtk_list_store_append(store, &iter);
                gtk_list_store_set(store, &iter,
                        DIVE_INDEX, i,
+                       DIVE_NR, dive->number,
                        DIVE_DATE, dive->when,
                        DIVE_DEPTH, dive->maxdepth,
                        DIVE_DURATION, dive->duration.seconds,
@@ -462,46 +490,10 @@ void dive_list_update_dives(void)
        repaint_dive();
 }
 
-typedef void (*data_func_t)(GtkTreeViewColumn *col,
-                           GtkCellRenderer *renderer,
-                           GtkTreeModel *model,
-                           GtkTreeIter *iter,
-                           gpointer data);
-
 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, int index, const char *title,
-                                       data_func_t data_func, PangoAlignment align, gboolean visible)
+                               data_func_t data_func, PangoAlignment align, gboolean visible)
 {
-       GtkCellRenderer *renderer;
-       GtkTreeViewColumn *col;
-       double xalign = 0.0; /* left as default */
-
-       renderer = gtk_cell_renderer_text_new();
-       col = gtk_tree_view_column_new();
-
-       gtk_tree_view_column_set_title(col, title);
-       gtk_tree_view_column_set_sort_column_id(col, index);
-       gtk_tree_view_column_set_resizable(col, TRUE);
-       gtk_tree_view_column_pack_start(col, renderer, TRUE);
-       if (data_func)
-               gtk_tree_view_column_set_cell_data_func(col, renderer, data_func, NULL, NULL);
-       else
-               gtk_tree_view_column_add_attribute(col, renderer, "text", index);
-       gtk_object_set(GTK_OBJECT(renderer), "alignment", align, NULL);
-       switch (align) {
-       case PANGO_ALIGN_LEFT:
-               xalign = 0.0;
-               break;
-       case PANGO_ALIGN_CENTER:
-               xalign = 0.5;
-               break;
-       case PANGO_ALIGN_RIGHT:
-               xalign = 1.0;
-               break;
-       }
-       gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
-       gtk_tree_view_column_set_visible(col, visible);
-       gtk_tree_view_append_column(GTK_TREE_VIEW(dl->tree_view), col);
-       return col;
+       return tree_view_column(dl->tree_view, index, title, data_func, align, visible);
 }
 
 /*
@@ -519,6 +511,7 @@ GtkWidget *dive_list_create(void)
 
        dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
                                G_TYPE_INT,                     /* index */
+                               G_TYPE_INT,                     /* nr */
                                G_TYPE_INT,                     /* Date */
                                G_TYPE_INT,                     /* Depth */
                                G_TYPE_INT,                     /* Duration */
@@ -534,17 +527,19 @@ GtkWidget *dive_list_create(void)
 
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
 
-       gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
+       gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
        gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
 
+       dive_list.nr = divelist_column(&dive_list, DIVE_NR, "#", NULL, PANGO_ALIGN_RIGHT, TRUE);
+       gtk_tree_view_column_set_sort_column_id(dive_list.nr, -1);
        dive_list.date = divelist_column(&dive_list, DIVE_DATE, "Date", date_data_func, PANGO_ALIGN_LEFT, TRUE);
        dive_list.depth = divelist_column(&dive_list, DIVE_DEPTH, "ft", depth_data_func, PANGO_ALIGN_RIGHT, TRUE);
        dive_list.duration = divelist_column(&dive_list, DIVE_DURATION, "min", duration_data_func, PANGO_ALIGN_RIGHT, TRUE);
-       dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, TRUE);
-       dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, TRUE);
-       dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, TRUE);
-       dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, TRUE);
-       dive_list.sac = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, FALSE);
+       dive_list.temperature = divelist_column(&dive_list, DIVE_TEMPERATURE, UTF8_DEGREE "F", temperature_data_func, PANGO_ALIGN_RIGHT, visible_cols.temperature);
+       dive_list.cylinder = divelist_column(&dive_list, DIVE_CYLINDER, "Cyl", NULL, PANGO_ALIGN_CENTER, visible_cols.cylinder);
+       dive_list.nitrox = divelist_column(&dive_list, DIVE_NITROX, "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, PANGO_ALIGN_CENTER, visible_cols.nitrox);
+       dive_list.sac = divelist_column(&dive_list, DIVE_SAC, "SAC", sac_data_func, PANGO_ALIGN_CENTER, visible_cols.sac);
+       dive_list.otu = divelist_column(&dive_list, DIVE_OTU, "OTU", otu_data_func, PANGO_ALIGN_CENTER, visible_cols.otu);
        dive_list.location = divelist_column(&dive_list, DIVE_LOCATION, "Location", NULL, PANGO_ALIGN_LEFT, TRUE);
 
        fill_dive_list();