GUI: Find dialog: Compare only to the decimal places of the print format.
[pspp] / src / ui / gui / find-dialog.c
index c8f8451bd85be1cf8492ddc3391320afeb77d9c9..2ff619a60a25c0c974dcc62092a97a7a40d9f540 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2007, 2009, 2011, 2012, 2015  Free Software Foundation
+   Copyright (C) 2007, 2009, 2011, 2012, 2015, 2020  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -25,6 +25,7 @@ which match particular strings */
 #include <regex.h>
 #include <stdlib.h>
 #include <sys/types.h>
+#include <math.h>
 
 #include "data/data-in.h"
 #include "data/datasheet.h"
@@ -40,6 +41,7 @@ which match particular strings */
 #include "ui/gui/psppire-data-window.h"
 #include "ui/gui/psppire-dialog.h"
 #include "ui/gui/psppire-selector.h"
+#include <ssw-sheet.h>
 
 #include "gl/xalloc.h"
 
@@ -101,18 +103,18 @@ do_find (GObject *obj, const struct find_dialog *fd)
 {
   casenumber x = -1;
   gint column = -1;
-  glong row;
-
-  
-  row = 10;
+  glong row = -1;
 
   find_value (fd, row, &x, &column);
 
-  if ( x != -1)
+  if (x != -1)
     {
+      SswSheet *sheet = SSW_SHEET (fd->de->data_editor->data_sheet);
       gtk_notebook_set_current_page (GTK_NOTEBOOK (fd->de->data_editor),
                                     PSPPIRE_DATA_EDITOR_DATA_VIEW);
 
+      ssw_sheet_scroll_to (sheet, column, x);
+      ssw_sheet_set_active_cell (sheet, column, x, NULL);
     }
 }
 
@@ -268,7 +270,7 @@ forward (casenumber *i, struct datasheet *data UNUSED)
 static void
 forward_wrap (casenumber *i, struct datasheet *data)
 {
-  if ( ++*i >=  datasheet_get_n_rows (data) ) *i = 0;
+  if (++*i >=  datasheet_get_n_rows (data)) *i = 0;
 }
 
 static void
@@ -281,7 +283,7 @@ backward (casenumber *i, struct datasheet *data UNUSED)
 static void
 backward_wrap (casenumber *i, struct datasheet *data)
 {
-  if ( --*i < 0 )
+  if (--*i < 0)
     *i = datasheet_get_n_rows (data) - 1;
 }
 
@@ -386,16 +388,16 @@ get_iteration_params (const struct find_dialog *fd)
   gboolean reverse = gtk_toggle_button_get_active
     (GTK_TOGGLE_BUTTON (get_widget_assert (fd->xml, "find-backwards")));
 
-  if ( wrap )
+  if (wrap)
     {
-      if ( reverse )
+      if (reverse)
        return &ip[REVERSE_WRAP];
       else
        return &ip[FORWARD_WRAP];
     }
   else
     {
-      if ( reverse )
+      if (reverse)
        return &ip[REVERSE];
       else
        return &ip[FORWARD];
@@ -427,11 +429,13 @@ struct comparator
 };
 
 
-/* A comparator which operates on the unadulterated union values */
-struct value_comparator
+/* A comparator which operates on the numerical values,
+   rounded to the number of decimal places indicated by
+   the variable's format.  */
+struct numeric_comparator
 {
   struct comparator parent;
-  union value pattern;
+  double rounded_ref;
 };
 
 /* A comparator which matches string values or parts thereof */
@@ -453,8 +457,12 @@ static bool
 value_compare (const struct comparator *cmptr,
               const union value *v)
 {
-  const struct value_comparator *vc = (const struct value_comparator *) cmptr;
-  return 0 == value_compare_3way (v, &vc->pattern, var_get_width (cmptr->var));
+  const struct numeric_comparator *nc = (const struct numeric_comparator *) cmptr;
+  const struct fmt_spec *fs = var_get_print_format (cmptr->var);
+
+  double c = nearbyint (v->f * exp10 (fs->d));
+
+  return c == nc->rounded_ref;
 }
 
 
@@ -474,11 +482,11 @@ string_label_compare (const struct comparator *cmptr,
 
   width = strlen (text);
 
-  assert ( cmptr->flags & STR_CMP_LABELS);
+  assert (cmptr->flags & STR_CMP_LABELS);
 
   g_return_val_if_fail (width > 0, false);
 
-  if ( cmptr->flags & STR_CMP_SUBSTR)
+  if (cmptr->flags & STR_CMP_SUBSTR)
     return (NULL != g_strstr_len (text, width, ssc->pattern));
   else
     return (0 == strncmp (text, ssc->pattern, width));
@@ -496,11 +504,11 @@ string_value_compare (const struct comparator *cmptr,
 
   int width = var_get_width (cmptr->var);
   g_return_val_if_fail (width > 0, false);
-  assert ( ! (cmptr->flags & STR_CMP_LABELS));
+  assert (! (cmptr->flags & STR_CMP_LABELS));
 
   text = value_to_text (*val, cmptr->var);
 
-  if ( cmptr->flags & STR_CMP_SUBSTR)
+  if (cmptr->flags & STR_CMP_SUBSTR)
     found =  (NULL != g_strstr_len (text, width, ssc->pattern));
   else
     found = (0 == strncmp (text, ssc->pattern, width));
@@ -523,7 +531,7 @@ regexp_value_compare (const struct comparator *cmptr,
 
   int width = var_get_width (cmptr->var);
 
-  assert  ( ! (cmptr->flags & STR_CMP_LABELS) );
+  assert  (! (cmptr->flags & STR_CMP_LABELS));
 
   g_return_val_if_fail (width > 0, false);
 
@@ -550,7 +558,7 @@ regexp_label_compare (const struct comparator *cmptr,
 
   int width ;
 
-  assert ( cmptr->flags & STR_CMP_LABELS);
+  assert (cmptr->flags & STR_CMP_LABELS);
 
   text = var_lookup_value_label (cmptr->var, val);
   width = strlen (text);
@@ -571,27 +579,21 @@ regexp_destroy (struct comparator *cmptr)
   regfree (&rec->re);
 }
 
-static void
-cmptr_value_destroy (struct comparator *cmptr)
-{
-  struct value_comparator *vc
-    = UP_CAST (cmptr, struct value_comparator, parent);
-  value_destroy (&vc->pattern, var_get_width (cmptr->var));
-}
-
-
 static struct comparator *
-value_comparator_create (const struct variable *var, const char *target)
+numeric_comparator_create (const struct variable *var, const char *target)
 {
-  struct value_comparator *vc = xzalloc (sizeof (*vc));
-  struct comparator *cmptr = &vc->parent;
+  struct numeric_comparator *nc = xzalloc (sizeof (*nc));
+  struct comparator *cmptr = &nc->parent;
 
   cmptr->flags = 0;
   cmptr->var = var;
-  cmptr->compare  = value_compare ;
-  cmptr->destroy = cmptr_value_destroy;
+  cmptr->compare  = value_compare;
+  const struct fmt_spec *fs = var_get_write_format (var);
 
-  text_to_value (target, var, &vc->pattern);
+  union value val;
+  text_to_value (target, var, &val);
+  nc->rounded_ref = nearbyint (val.f * exp10 (fs->d));
+  value_destroy (&val, var_get_width (var));
 
   return cmptr;
 }
@@ -606,7 +608,7 @@ string_comparator_create (const struct variable *var, const char *target,
   cmptr->flags = flags;
   cmptr->var = var;
 
-  if ( flags & STR_CMP_LABELS)
+  if (flags & STR_CMP_LABELS)
     cmptr->compare = string_label_compare;
   else
     cmptr->compare = string_value_compare;
@@ -633,7 +635,7 @@ regexp_comparator_create (const struct variable *var, const char *target,
   cmptr->destroy  = regexp_destroy;
 
   code = regcomp (&rec->re, target, 0);
-  if ( code != 0 )
+  if (code != 0)
     {
       char *errbuf = NULL;
       size_t errbuf_size = regerror (code, &rec->re, errbuf,  0);
@@ -644,7 +646,7 @@ regexp_comparator_create (const struct variable *var, const char *target,
 
       msg (ME, _("Bad regular expression: %s"), errbuf);
 
-      free ( cmptr);
+      free (cmptr);
       free (errbuf);
       return NULL;
     }
@@ -665,10 +667,10 @@ comparator_compare (const struct comparator *cmptr,
 static void
 comparator_destroy (struct comparator *cmptr)
 {
-  if ( ! cmptr )
+  if (! cmptr)
     return ;
 
-  if ( cmptr->destroy )
+  if (cmptr->destroy)
     cmptr->destroy (cmptr);
 
   free (cmptr);
@@ -679,13 +681,13 @@ static struct comparator *
 comparator_factory (const struct variable *var, const char *str,
                    enum string_cmp_flags flags)
 {
-  if ( flags & STR_CMP_REGEXP )
+  if (flags & STR_CMP_REGEXP)
     return regexp_comparator_create (var, str, flags);
 
-  if ( flags & (STR_CMP_SUBSTR | STR_CMP_LABELS) )
+  if (flags & (STR_CMP_SUBSTR | STR_CMP_LABELS))
     return string_comparator_create (var, str, flags);
 
-  return value_comparator_create (var, str);
+  return numeric_comparator_create (var, str);
 }
 
 
@@ -705,7 +707,7 @@ find_value (const struct find_dialog *fd, casenumber current_row,
   enum string_cmp_flags flags = 0;
 
   var = dict_lookup_var (fd->dict->dict, var_name);
-  if ( ! var )
+  if (! var)
     return ;
 
   width = var_get_width (var);
@@ -713,15 +715,15 @@ find_value (const struct find_dialog *fd, casenumber current_row,
   *column = var_get_dict_index (var);
   *row = -1;
 
-  if ( gtk_toggle_button_get_active
+  if (gtk_toggle_button_get_active
        (GTK_TOGGLE_BUTTON (fd->match_substring_checkbox)))
     flags |= STR_CMP_SUBSTR;
 
-  if ( gtk_toggle_button_get_active
+  if (gtk_toggle_button_get_active
        (GTK_TOGGLE_BUTTON (fd->match_regexp_checkbox)))
     flags |= STR_CMP_REGEXP;
 
-  if ( gtk_toggle_button_get_active
+  if (gtk_toggle_button_get_active
        (GTK_TOGGLE_BUTTON (fd->value_labels_checkbox)))
     flags |= STR_CMP_LABELS;
 
@@ -733,7 +735,7 @@ find_value (const struct find_dialog *fd, casenumber current_row,
       comparator_factory (var, target_string, flags);
 
     value_init (&val, width);
-    if ( ! cmptr)
+    if (! cmptr)
       goto finish;
 
     for (i = ip->start (current_row, fd->data);
@@ -742,7 +744,7 @@ find_value (const struct find_dialog *fd, casenumber current_row,
       {
        datasheet_get_value (fd->data, i, var_get_case_index (var), &val);
 
-       if ( comparator_compare (cmptr, &val))
+       if (comparator_compare (cmptr, &val))
          {
            *row = i;
            break;