]> git.tdb.fi Git - ext/subsurface.git/blobdiff - divelist.c
Add more typecasts for Windows`
[ext/subsurface.git] / divelist.c
index bf853ec0e49fce534853c3b59e98abb0d9a314a1..a8afd3ac2e5aac5540bd0faef71d3863c33d8c4c 100644 (file)
@@ -320,6 +320,7 @@ static double calculate_airuse(struct dive *dive)
 static int calculate_sac(struct dive *dive)
 {
        double airuse, pressure, sac;
+       int duration, i;
 
        airuse = calculate_airuse(dive);
        if (!airuse)
@@ -327,9 +328,25 @@ static int calculate_sac(struct dive *dive)
        if (!dive->duration.seconds)
                return 0;
 
+       /* find and eliminate long surface intervals */
+       duration = dive->duration.seconds;
+       for (i = 0; i < dive->samples; i++) {
+               if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
+                       int end = i + 1;
+                       while (end < dive->samples && dive->sample[end].depth.mm < 100)
+                               end++;
+                       /* we only want the actual surface time during a dive */
+                       if (end < dive->samples) {
+                               end--;
+                               duration -= dive->sample[end].time.seconds -
+                                               dive->sample[i].time.seconds;
+                               i = end + 1;
+                       }
+               }
+       }
        /* Mean pressure in atm: 1 atm per 10m */
        pressure = 1 + (dive->meandepth.mm / 10000.0);
-       sac = airuse / pressure * 60 / dive->duration.seconds;
+       sac = airuse / pressure * 60 / duration;
 
        /* milliliters per minute.. */
        return sac * 1000;
@@ -337,7 +354,7 @@ static int calculate_sac(struct dive *dive)
 
 void update_cylinder_related_info(struct dive *dive)
 {
-       if(dive != NULL) {
+       if (dive != NULL) {
                dive->sac = calculate_sac(dive);
                dive->otu = calculate_otu(dive);
        }
@@ -351,8 +368,8 @@ static void get_string(char **str, const char *s)
        if (!s)
                s = "";
        len = strlen(s);
-       if (len > 40)
-               len = 40;
+       if (len > 60)
+               len = 60;
        n = malloc(len+1);
        memcpy(n, s, len);
        n[len] = 0;
@@ -457,7 +474,8 @@ static void fill_dive_list(void)
 
        store = GTK_LIST_STORE(dive_list.model);
 
-       for (i = 0; i < dive_table.nr; i++) {
+       i = dive_table.nr;
+       while (--i >= 0) {
                struct dive *dive = dive_table.dives[i];
 
                update_cylinder_related_info(dive);
@@ -505,6 +523,20 @@ static void realize_cb(GtkWidget *tree_view, gpointer userdata)
        gtk_widget_grab_focus(tree_view);
 }
 
+static void row_activated_cb(GtkTreeView *tree_view,
+                       GtkTreePath *path,
+                       GtkTreeViewColumn *column,
+                       GtkTreeModel *model)
+{
+       int index;
+       GtkTreeIter iter;
+
+       if (!gtk_tree_model_get_iter(model, &iter, path))
+               return;
+       gtk_tree_model_get(model, &iter, DIVE_INDEX, &index, -1);
+       edit_dive_info(get_dive(index));
+}
+
 GtkWidget *dive_list_create(void)
 {
        GtkTreeSelection  *selection;
@@ -550,6 +582,7 @@ GtkWidget *dive_list_create(void)
                                          NULL);
 
        g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
+       g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), dive_list.model);
        g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
 
        dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);