separate sack integration test binary
[pspp] / src / ui / gui / psppire-data-store.c
index c91e589dfcb848a53f05b47856aef7c4dcf25283..173bf0e10f5a6efb602f8cbfc90abf51815d7bcd 100644 (file)
@@ -46,9 +46,6 @@
 
 #include "value-variant.h"
 
-static void psppire_data_store_init            (PsppireDataStore      *data_store);
-static void psppire_data_store_class_init      (PsppireDataStoreClass *class);
-
 static void psppire_data_store_finalize        (GObject           *object);
 static void psppire_data_store_dispose        (GObject           *object);
 
@@ -60,7 +57,7 @@ static gboolean psppire_data_store_insert_case (PsppireDataStore *ds,
 static gboolean psppire_data_store_data_in (PsppireDataStore *ds,
                                            casenumber casenum, gint idx,
                                            struct substring input,
-                                           const struct fmt_spec *fmt);
+                                           struct fmt_spec);
 
 static GObjectClass *parent_class = NULL;
 
@@ -101,7 +98,7 @@ __tree_model_get_n_columns (GtkTreeModel *tree_model)
 {
   PsppireDataStore *store  = PSPPIRE_DATA_STORE (tree_model);
 
-  return psppire_dict_get_var_cnt (store->dict);
+  return psppire_dict_get_n_vars (store->dict);
 }
 
 
@@ -137,11 +134,11 @@ psppire_data_store_string_to_value (GtkTreeModel *model, gint col, gint row,
 {
   PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
 
-  while (col >= psppire_dict_get_var_cnt (store->dict))
+  while (col >= psppire_dict_get_n_vars (store->dict))
     {
       const struct variable *var =
        psppire_dict_insert_variable (store->dict,
-                                     psppire_dict_get_var_cnt (store->dict),
+                                     psppire_dict_get_n_vars (store->dict),
                                      NULL);
       g_return_val_if_fail (var, FALSE);
     }
@@ -149,15 +146,27 @@ psppire_data_store_string_to_value (GtkTreeModel *model, gint col, gint row,
   const struct variable *variable = psppire_dict_get_variable (store->dict, col);
   g_return_val_if_fail (variable, FALSE);
 
-  const struct fmt_spec *fmt = var_get_print_format (variable);
+  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");
+  const struct val_labs *value_labels = var_get_value_labels (variable);
+  const union value *vp = NULL;
+  if (value_labels)
+    {
+      vp = val_labs_find_value (value_labels, in);
+      if (vp)
+       value_copy (&val, vp, width);
+    }
+  char *xx = NULL;
+  if (vp == NULL)
+    {
+      xx = data_in (ss_cstr (in), psppire_dict_encoding (store->dict),
+                   fmt.type, settings_get_fmt_settings (),
+                    &val, width, "UTF-8");
+    }
 
   GVariant *vrnt = value_variant_new (&val, width);
   value_destroy (&val, width);
@@ -171,8 +180,12 @@ psppire_data_store_string_to_value (GtkTreeModel *model, gint col, gint row,
 static char *
 unlabeled_value (PsppireDataStore *store, const struct variable *variable, const union value *val)
 {
-  const struct fmt_spec *fmt = var_get_print_format (variable);
-  return data_out (val, psppire_dict_encoding (store->dict),  fmt);
+  if (var_is_numeric (variable) &&
+      var_is_value_missing (variable, val) == MV_SYSTEM)
+    return g_strdup ("");
+
+  struct fmt_spec fmt = var_get_print_format (variable);
+  return value_to_text__ (*val, fmt, psppire_dict_encoding (store->dict));
 }
 
 gchar *
@@ -240,7 +253,7 @@ __get_value (GtkTreeModel *tree_model,
 
   g_value_init (value, G_TYPE_VARIANT);
 
-  const union value *val = case_data_idx (cc, var_get_case_index (variable));
+  const union value *val = case_data_idx (cc, var_get_dict_index (variable));
 
   GVariant *vv = value_variant_new (val, var_get_width (variable));
 
@@ -268,44 +281,9 @@ __tree_model_init (GtkTreeModelIface *iface)
   iface->iter_parent     = NULL;
 }
 
-
-GType
-psppire_data_store_get_type (void)
-{
-  static GType data_store_type = 0;
-
-  if (!data_store_type)
-    {
-      static const GTypeInfo data_store_info =
-      {
-       sizeof (PsppireDataStoreClass),
-       NULL,           /* base_init */
-       NULL,           /* base_finalize */
-        (GClassInitFunc) psppire_data_store_class_init,
-       NULL,           /* class_finalize */
-       NULL,           /* class_data */
-        sizeof (PsppireDataStore),
-       0,
-        (GInstanceInitFunc) psppire_data_store_init,
-      };
-
-      static const GInterfaceInfo tree_model_info = {
-       (GInterfaceInitFunc) __tree_model_init,
-       NULL,
-       NULL
-      };
-
-      data_store_type = g_type_register_static (G_TYPE_OBJECT,
-                                               "PsppireDataStore",
-                                               &data_store_info, 0);
-
-      g_type_add_interface_static (data_store_type, GTK_TYPE_TREE_MODEL,
-                                  &tree_model_info);
-    }
-
-  return data_store_type;
-}
-
+G_DEFINE_TYPE_WITH_CODE (PsppireDataStore, psppire_data_store, G_TYPE_OBJECT,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
+                                               __tree_model_init))
 
 static void
 psppire_data_store_class_init (PsppireDataStoreClass *class)
@@ -313,7 +291,7 @@ psppire_data_store_class_init (PsppireDataStoreClass *class)
   GObjectClass *object_class;
 
   parent_class = g_type_class_peek_parent (class);
-  object_class = (GObjectClass*) class;
+  object_class = G_OBJECT_CLASS (class);
 
   object_class->finalize = psppire_data_store_finalize;
   object_class->dispose = psppire_data_store_dispose;
@@ -354,7 +332,7 @@ psppire_data_store_get_case_count (const PsppireDataStore *store)
 size_t
 psppire_data_store_get_value_count (const PsppireDataStore *store)
 {
-  return psppire_dict_get_value_cnt (store->dict);
+  return psppire_dict_get_n_vars (store->dict);
 }
 
 const struct caseproto *
@@ -374,25 +352,36 @@ psppire_data_store_init (PsppireDataStore *data_store)
 
 
 static void
-psppire_data_store_delete_value (PsppireDataStore *store, gint case_index)
+psppire_data_store_delete_values (PsppireDataStore *store, gint case_index,
+                                  guint n)
 {
   g_return_if_fail (store->datasheet);
-  datasheet_delete_columns (store->datasheet, case_index, 1);
-  datasheet_insert_column (store->datasheet, NULL, -1, case_index);
+  g_return_if_fail (case_index < datasheet_get_n_columns (store->datasheet));
+
+  datasheet_delete_columns (store->datasheet, case_index, n);
 }
 
 
 /*
-   A callback which occurs after a variable has been deleted.
+   A callback which occurs after variables have been deleted.
+ */
+static void
+delete_variables_callback (GObject *obj, gint dict_index, unsigned int n, gpointer data)
+{
+  PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
+
+  psppire_data_store_delete_values (store, dict_index, n);
+}
+
+/*
+   A callback which occurs after variables have been deleted.
  */
 static void
-delete_variable_callback (GObject *obj, const struct variable *var UNUSED,
-                          gint dict_index, gint case_index,
-                          gpointer data)
+move_variable_callback (GObject *obj, gint new_dict_index, int old_dict_index, gpointer data)
 {
   PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
 
-  psppire_data_store_delete_value (store, case_index);
+  datasheet_move_columns (store->datasheet, old_dict_index, new_dict_index, 1);
 }
 
 struct resize_datum_aux
@@ -408,12 +397,14 @@ resize_datum (const union value *old, union value *new, const void *aux_)
   const struct resize_datum_aux *aux = aux_;
   int new_width = var_get_width (aux->new_variable);
   const char *enc = dict_get_encoding (aux->dict);
-  const struct fmt_spec *newfmt = var_get_print_format (aux->new_variable);
-  char *s = data_out (old, enc, var_get_print_format (aux->old_variable));
-  enum fmt_type type = (fmt_usable_for_input (newfmt->type)
-                        ? newfmt->type
+  struct fmt_spec newfmt = var_get_print_format (aux->new_variable);
+  char *s = data_out (old, enc, var_get_print_format (aux->old_variable),
+                      settings_get_fmt_settings ());
+  enum fmt_type type = (fmt_usable_for_input (newfmt.type)
+                        ? newfmt.type
                         : FMT_DOLLAR);
-  free (data_in (ss_cstr (s), enc, type, new, new_width, enc));
+  free (data_in (ss_cstr (s), enc, type, settings_get_fmt_settings (),
+                 new, new_width, enc));
   free (s);
 }
 
@@ -426,7 +417,7 @@ variable_changed_callback (GObject *obj, gint var_num, guint what, const struct
 
   if (what & VAR_TRAIT_WIDTH)
     {
-      int posn = var_get_case_index (variable);
+      int posn = var_get_dict_index (variable);
       struct resize_datum_aux aux;
       aux.old_variable = oldvar;
       aux.new_variable = variable;
@@ -448,7 +439,7 @@ insert_variable_callback (GObject *obj, gint var_num, gpointer data)
   store  = PSPPIRE_DATA_STORE (data);
 
   variable = psppire_dict_get_variable (store->dict, var_num);
-  posn = var_get_case_index (variable);
+  posn = var_get_dict_index (variable);
   psppire_data_store_insert_value (store, var_get_width (variable), posn);
 }
 
@@ -477,7 +468,7 @@ psppire_data_store_set_reader (PsppireDataStore *ds,
 {
   gint i;
   gint old_n = 0;
-  if ( ds->datasheet)
+  if (ds->datasheet)
     {
       old_n = datasheet_get_n_rows (ds->datasheet);
       datasheet_destroy (ds->datasheet);
@@ -487,10 +478,10 @@ psppire_data_store_set_reader (PsppireDataStore *ds,
 
   gint new_n = datasheet_get_n_rows (ds->datasheet);
 
-  if ( ds->dict )
-    for (i = 0 ; i < n_dict_signals; ++i )
+  if (ds->dict)
+    for (i = 0 ; i < n_dict_signals; ++i)
       {
-       if ( ds->dict_handler_id [i] > 0)
+       if (ds->dict_handler_id [i] > 0)
          {
            g_signal_handler_unblock (ds->dict,
                                      ds->dict_handler_id[i]);
@@ -515,8 +506,8 @@ psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *di
   int i;
 
   /* Disconnect any existing handlers */
-  if ( data_store->dict )
-    for (i = 0 ; i < n_dict_signals; ++i )
+  if (data_store->dict)
+    for (i = 0 ; i < n_dict_signals; ++i)
       {
        g_signal_handler_disconnect (data_store->dict,
                                     data_store->dict_handler_id[i]);
@@ -524,7 +515,7 @@ psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *di
 
   data_store->dict = dict;
 
-  if ( dict != NULL)
+  if (dict != NULL)
     {
 
       data_store->dict_handler_id [VARIABLE_INSERTED] =
@@ -532,9 +523,14 @@ psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *di
                          G_CALLBACK (insert_variable_callback),
                          data_store);
 
-      data_store->dict_handler_id [VARIABLE_DELETED] =
-       g_signal_connect (dict, "variable-deleted",
-                         G_CALLBACK (delete_variable_callback),
+      data_store->dict_handler_id [VARIABLES_DELETED] =
+       g_signal_connect (dict, "variables-deleted",
+                         G_CALLBACK (delete_variables_callback),
+                         data_store);
+
+      data_store->dict_handler_id [VARIABLE_MOVED] =
+       g_signal_connect (dict, "variable-moved",
+                         G_CALLBACK (move_variable_callback),
                          data_store);
 
       data_store->dict_handler_id [VARIABLE_CHANGED] =
@@ -547,10 +543,10 @@ psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *di
 
   /* The entire model has changed */
 
-  if ( data_store->dict )
-    for (i = 0 ; i < n_dict_signals; ++i )
+  if (data_store->dict)
+    for (i = 0 ; i < n_dict_signals; ++i)
       {
-       if ( data_store->dict_handler_id [i] > 0)
+       if (data_store->dict_handler_id [i] > 0)
          {
            g_signal_handler_block (data_store->dict,
                                    data_store->dict_handler_id[i]);
@@ -629,7 +625,7 @@ psppire_data_store_get_value (PsppireDataStore *store,
 
   int width = var_get_width (var);
   value_init (val, width);
-  datasheet_get_value (store->datasheet, row, var_get_case_index (var), val);
+  datasheet_get_value (store->datasheet, row, var_get_dict_index (var), val);
 
   return TRUE;
 }
@@ -687,7 +683,7 @@ psppire_data_store_set_string (PsppireDataStore *store,
   if (row == n_cases)
     psppire_data_store_insert_new_case (store, row);
 
-  case_index = var_get_case_index (var);
+  case_index = var_get_dict_index (var);
   if (use_value_label)
     {
       const struct val_labs *vls = var_get_value_labels (var);
@@ -725,11 +721,10 @@ psppire_data_store_clear (PsppireDataStore *ds)
 struct casereader *
 psppire_data_store_get_reader (PsppireDataStore *ds)
 {
-  int i;
   struct casereader *reader ;
 
-  if ( ds->dict )
-    for (i = 0 ; i < n_dict_signals; ++i )
+  if (ds->dict)
+    for (int i = 0 ; i < n_dict_signals; ++i)
       {
        g_signal_handler_block (ds->dict,
                                ds->dict_handler_id[i]);
@@ -790,7 +785,7 @@ psppire_data_store_insert_case (PsppireDataStore *ds,
   cc = case_ref (cc);
   result = datasheet_insert_rows (ds->datasheet, posn, &cc, 1);
 
-  if ( result )
+  if (result)
     {
       g_signal_emit (ds, signals[ITEMS_CHANGED], 0, posn, 0, 1);
     }
@@ -815,13 +810,13 @@ psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
   g_return_val_if_fail (ds->datasheet, FALSE);
 
   n_cases = psppire_data_store_get_case_count (ds);
-  if ( casenum > n_cases)
+  if (casenum > n_cases)
     return FALSE;
 
   if (casenum == n_cases)
     psppire_data_store_insert_new_case (ds, casenum);
 
-  ok = datasheet_put_value (ds->datasheet, casenum, var_get_case_index (var),
+  ok = datasheet_put_value (ds->datasheet, casenum, var_get_dict_index (var),
                             v);
   if (ok)
     {
@@ -838,7 +833,7 @@ psppire_data_store_set_value (PsppireDataStore *ds, casenumber casenum,
 /* Set the IDXth value of case C using D_IN */
 static gboolean
 psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
-                           struct substring input, const struct fmt_spec *fmt)
+                           struct substring input, struct fmt_spec fmt)
 {
   union value value;
   int width;
@@ -859,8 +854,8 @@ psppire_data_store_data_in (PsppireDataStore *ds, casenumber casenum, gint idx,
                         FALSE);
   value_init (&value, width);
   ok = (datasheet_get_value (ds->datasheet, casenum, idx, &value)
-        && data_in_msg (input, UTF8, fmt->type, &value, width,
-                        dict_get_encoding (dict->dict))
+        && data_in_msg (input, UTF8, fmt.type, settings_get_fmt_settings (),
+                        &value, width, dict_get_encoding (dict->dict))
         && datasheet_put_value (ds->datasheet, casenum, idx, &value));
   value_destroy (&value, width);
 
@@ -881,7 +876,7 @@ psppire_data_store_insert_value (PsppireDataStore *ds,
 
   g_assert (width >= 0);
 
-  if ( ! ds->datasheet )
+  if (! ds->datasheet)
     ds->datasheet = datasheet_create (NULL);
 
   value_init (&value, width);
@@ -902,20 +897,20 @@ psppire_data_store_filtered (PsppireDataStore *ds,
   const struct dictionary *dict;
   const struct variable *filter;
 
-  if ( row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
+  if (row < 0 || row >= datasheet_get_n_rows (ds->datasheet))
     return FALSE;
 
   dict = ds->dict->dict;
   g_return_val_if_fail (dict, FALSE);
   filter = dict_get_filter (dict);
-  if ( ! filter)
+  if (! filter)
     return FALSE;
 
   g_return_val_if_fail (var_is_numeric (filter), FALSE);
   value_init (&val, 0);
-  if ( ! datasheet_get_value (ds->datasheet, row,
-                              var_get_case_index (filter),
-                              &val) )
+  if (! datasheet_get_value (ds->datasheet, row,
+                              var_get_dict_index (filter),
+                              &val))
     return FALSE;
 
   return (val.f == 0.0);