Fix compiler warning
[pspp] / src / ui / gui / psppire-data-store.c
index 74677259f72ed65d0ab03744f810681ab82cac7f..77aa37f1f4530dcc3c42ccef66b8e7629ff4dde2 100644 (file)
@@ -44,7 +44,7 @@
 #include "xalloc.h"
 #include "xmalloca.h"
 
-
+#include "value-variant.h"
 
 static void psppire_data_store_init            (PsppireDataStore      *data_store);
 static void psppire_data_store_class_init      (PsppireDataStoreClass *class);
@@ -124,11 +124,57 @@ __iter_nth_child (GtkTreeModel *tree_model,
       return FALSE;
     }
   
-  iter->user_data = n;
+  iter->user_data = GINT_TO_POINTER (n);
+  iter->stamp = store->stamp;
+  
   return TRUE;
 }
 
+void
+myreversefunc (GtkTreeModel *model, gint col, gint row,
+              const gchar *in, GValue *out)
+{
+  PsppireDataStore *store  = PSPPIRE_DATA_STORE (model);
+
+  const struct variable *variable = psppire_dict_get_variable (store->dict, col);
+  g_return_if_fail (variable);
+
+  const struct fmt_spec *fmt = var_get_print_format (variable);
+
+  int width = var_get_width (variable);
+
+  union value val;
+  value_init (&val, width);
+  char *xx = 
+    data_in (ss_cstr (in), psppire_dict_encoding (store->dict),
+            fmt->type, &val, width, "UTF-8");
+
+  GVariant *vrnt = value_variant_new (&val, width);
+  value_destroy (&val, width);
+
+  g_value_init (out, G_TYPE_VARIANT);
+  g_value_set_variant (out, vrnt);
+  free (xx);
+}
+
+gchar *
+myconvfunc (GtkTreeModel *model, gint col, gint row, const GValue *v)
+{
+  PsppireDataStore *store  = PSPPIRE_DATA_STORE (model);
+
+  const struct variable *variable = psppire_dict_get_variable (store->dict, col);
+  g_return_val_if_fail (variable, g_strdup ("???"));
+
+  GVariant *vrnt = g_value_get_variant (v);
+  union value val;
+  value_variant_get (&val, vrnt);
 
+  const struct fmt_spec *fmt = var_get_print_format (variable);
+  char *out =  data_out (&val, psppire_dict_encoding (store->dict),  fmt);
+  value_destroy_from_variant (&val, vrnt);
+
+  return out;
+}
 
 static void
 __get_value (GtkTreeModel *tree_model,
@@ -138,48 +184,34 @@ __get_value (GtkTreeModel *tree_model,
 {
   PsppireDataStore *store  = PSPPIRE_DATA_STORE (tree_model);
 
+  g_return_if_fail (iter->stamp == store->stamp);
+  
   const struct variable *variable = psppire_dict_get_variable (store->dict, column);
+  if (NULL == variable)
+    return;
 
-  if (var_is_numeric (variable))
-    g_value_init (value, G_TYPE_DOUBLE);
-  else
-    g_value_init (value, G_TYPE_STRING);
+  g_value_init (value, G_TYPE_VARIANT);
 
   gint row = GPOINTER_TO_INT (iter->user_data);
 
   struct ccase *cc = datasheet_get_row (store->datasheet, row);
-  
-  if (var_is_numeric (variable))
-    g_value_set_double (value, case_data_idx (cc, column)->f);
-  else
-    {
-      const gchar *ss = value_str (case_data_idx (cc, column),
-                            var_get_width (variable));
-      g_value_set_string (value, ss);
-    }
-  case_unref (cc);
-}
-
 
-static GType
-__get_type (GtkTreeModel *tree_model,   gint idx)
-{
-  PsppireDataStore *store  = PSPPIRE_DATA_STORE (tree_model);
+  const union value *val = case_data_idx (cc, column);
 
-  const struct variable *variable = psppire_dict_get_variable (store->dict, idx);
+  GVariant *vv = value_variant_new (val, var_get_width (variable));
 
-  if (var_is_numeric (variable))
-    return G_TYPE_DOUBLE;
-
-  return G_TYPE_STRING;
+  g_value_set_variant (value, vv);
+  
+  case_unref (cc);
 }
 
+
 static void
 __tree_model_init (GtkTreeModelIface *iface)
 {
   iface->get_flags       = __tree_model_get_flags;
   iface->get_n_columns   = __tree_model_get_n_columns ;
-  iface->get_column_type = __get_type;
+  iface->get_column_type = NULL;
   iface->get_iter        = NULL; 
   iface->iter_next       = NULL; 
   iface->get_path        = NULL; 
@@ -321,6 +353,7 @@ psppire_data_store_init (PsppireDataStore *data_store)
   data_store->dict = NULL;
   data_store->datasheet = NULL;
   data_store->dispose_has_run = FALSE;
+  data_store->stamp = g_random_int ();
 }
 
 /*
@@ -559,6 +592,27 @@ psppire_data_store_insert_new_case (PsppireDataStore *ds, casenumber posn)
   return result;
 }
 
+gboolean
+psppire_data_store_get_value (PsppireDataStore *store,
+                             glong row, const struct variable *var,
+                             union value *val)
+{
+  g_return_val_if_fail (store != NULL, FALSE);
+  g_return_val_if_fail (store->datasheet != NULL, FALSE);
+  g_return_val_if_fail (var != NULL, FALSE);
+
+  if (row < 0 || row >= datasheet_get_n_rows (store->datasheet))
+    return FALSE;
+
+  int width = var_get_width (var);
+  value_init (val, width);
+  datasheet_get_value (store->datasheet, row, var_get_case_index (var), val);
+
+  return TRUE;
+}
+
+  
+
 gchar *
 psppire_data_store_get_string (PsppireDataStore *store,
                                glong row, const struct variable *var,
@@ -566,19 +620,10 @@ psppire_data_store_get_string (PsppireDataStore *store,
 {
   gchar *string;
   union value v;
-  int width;
-
-  g_return_val_if_fail (store != NULL, NULL);
-  g_return_val_if_fail (store->datasheet != NULL, NULL);
-  g_return_val_if_fail (var != NULL, NULL);
-
-  if (row < 0 || row >= datasheet_get_n_rows (store->datasheet))
+  int width = var_get_width (var);
+  if (! psppire_data_store_get_value (store, row, var, &v))
     return NULL;
-
-  width = var_get_width (var);
-  value_init (&v, width);
-  datasheet_get_value (store->datasheet, row, var_get_case_index (var), &v);
-
+  
   string = NULL;
   if (use_value_label)
     {