Merge remote-tracking branch 'origin/master' into gtk3
[pspp] / src / ui / gui / psppire-data-store.c
index 83933c474c8c241b7628b10d7fd3db6545689f7f..fbeb737cf3821113daa51af173222b33ef7f9293 100644 (file)
@@ -210,9 +210,42 @@ delete_variable_callback (GObject *obj, const struct variable *var UNUSED,
   datasheet_insert_column (store->datasheet, NULL, -1, case_index);
 }
 
+struct resize_datum_aux
+  {
+    const struct dictionary *dict;
+    const struct variable *new_variable;
+    const struct variable *old_variable;
+  };
+
 static void
-variable_changed_callback (GObject *obj, gint var_num, gpointer data)
+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));
+  free (data_in (ss_cstr (s), enc, newfmt->type, new, new_width, enc));
+  free (s);
+}
+
+static void
+variable_changed_callback (GObject *obj, gint var_num, guint what, const struct variable *oldvar,
+                          gpointer data)
+{
+  PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
+  struct variable *variable = psppire_dict_get_variable (store->dict, var_num);
+
+  if (what & VAR_TRAIT_WIDTH)
+    {
+      int posn = var_get_case_index (variable);
+      struct resize_datum_aux aux;
+      aux.old_variable = oldvar;
+      aux.new_variable = variable;
+      aux.dict = store->dict->dict;
+      datasheet_resize_column (store->datasheet, posn, var_get_width (variable),
+                               resize_datum, &aux);
+    }
 }
 
 static void
@@ -231,55 +264,6 @@ insert_variable_callback (GObject *obj, gint var_num, gpointer data)
   psppire_data_store_insert_value (store, var_get_width (variable), posn);
 }
 
-struct resize_datum_aux
-  {
-    int old_width;
-    int new_width;
-  };
-
-
-void
-resize_datum (const union value *old, union value *new, void *aux_)
-{
-  struct resize_datum_aux *aux = aux_;
-
-  if (aux->new_width == 0)
-    {
-      /* FIXME: try to parse string as number. */
-      new->f = SYSMIS;
-    }
-  else if (aux->old_width == 0)
-    {
-      /* FIXME: format number as string. */
-      value_set_missing (new, aux->new_width);
-    }
-  else
-    value_copy_rpad (new, aux->new_width, old, aux->old_width, ' ');
-}
-
-static void
-dict_size_change_callback (GObject *obj,
-                         gint var_num, gint old_width, gpointer data)
-{
-  PsppireDataStore *store  = PSPPIRE_DATA_STORE (data);
-  struct variable *variable;
-  int posn;
-
-  variable = psppire_dict_get_variable (store->dict, var_num);
-  posn = var_get_case_index (variable);
-
-  if (old_width != var_get_width (variable))
-    {
-      struct resize_datum_aux aux;
-      aux.old_width = old_width;
-      aux.new_width = var_get_width (variable);
-      datasheet_resize_column (store->datasheet, posn, aux.new_width,
-                               resize_datum, &aux);
-    }
-}
-
-
-
 /**
  * psppire_data_store_new:
  * @dict: The dictionary for this data_store.
@@ -364,11 +348,6 @@ psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *di
        g_signal_connect (dict, "variable-changed",
                          G_CALLBACK (variable_changed_callback),
                          data_store);
-
-      data_store->dict_handler_id [SIZE_CHANGED] =
-       g_signal_connect (dict, "dict-size-changed",
-                         G_CALLBACK (dict_size_change_callback),
-                         data_store);
     }
 
 
@@ -389,6 +368,13 @@ psppire_data_store_set_dictionary (PsppireDataStore *data_store, PsppireDict *di
 static void
 psppire_data_store_finalize (GObject *object)
 {
+  PsppireDataStore *ds = PSPPIRE_DATA_STORE (object);
+
+  if (ds->datasheet)
+    {
+      datasheet_destroy (ds->datasheet);
+      ds->datasheet = NULL;
+    }
 
   /* must chain up */
   (* parent_class->finalize) (object);
@@ -403,11 +389,6 @@ psppire_data_store_dispose (GObject *object)
   if (ds->dispose_has_run)
     return;
 
-  if (ds->datasheet)
-    {
-      datasheet_destroy (ds->datasheet);
-      ds->datasheet = NULL;
-    }
 
   /* must chain up */
   (* parent_class->dispose) (object);