Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / ui / gui / psppire-case-file.c
index 9d3e3a14b4c07e5affe31f0834e4dabbfa8fde4c..2edfec89ae0a036c3791309ba432e16567839706 100644 (file)
@@ -1,21 +1,18 @@
-/*
-    PSPPIRE --- A Graphical User Interface for PSPP
-    Copyright (C) 2006  Free Software Foundation
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2006  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
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301, USA. */
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 #include <string.h>
 
 #include <gtksheet/gtkextra-marshal.h>
 
+#include <data/format.h>
 #include <data/case.h>
 #include <data/data-in.h>
 #include <data/datasheet.h>
+#include <data/casereader.h>
 #include <math/sort.h>
 #include <libpspp/misc.h>
 
 #include "xalloc.h"
-#include "xallocsa.h"
+#include "xmalloca.h"
 
 /* --- prototypes --- */
 static void psppire_case_file_class_init       (PsppireCaseFileClass   *class);
@@ -82,18 +81,93 @@ psppire_case_file_get_type (void)
   return object_type;
 }
 
+/* Properties */
+enum
+{
+  PROP_0,
+  PROP_DATASHEET,
+  PROP_READER
+};
+
+
+
+
+static void
+psppire_case_file_set_property (GObject         *object,
+                               guint            prop_id,
+                               const GValue    *value,
+                               GParamSpec      *pspec)
+
+{
+  PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
+
+  switch (prop_id)
+    {
+    case PROP_READER:
+      cf->datasheet = datasheet_create (g_value_get_pointer (value));
+      cf->accessible = TRUE;
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    };
+}
+
+static void
+psppire_case_file_get_property (GObject         *object,
+                               guint            prop_id,
+                               GValue          *value,
+                               GParamSpec      *pspec)
+{
+  PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
+
+  switch (prop_id)
+    {
+    case PROP_DATASHEET:
+      g_value_set_pointer (value, cf->datasheet);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    };
+}
+
 
 static void
 psppire_case_file_class_init (PsppireCaseFileClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GParamSpec *datasheet_spec ;
+  GParamSpec *reader_spec ;
 
   parent_class = g_type_class_peek_parent (class);
 
   object_class->finalize = psppire_case_file_finalize;
 
+  datasheet_spec =
+    g_param_spec_pointer ("datasheet",
+                         "Datasheet",
+                         "A pointer to the datasheet belonging to this object",
+                         G_PARAM_READABLE );
+  reader_spec =
+    g_param_spec_pointer ("casereader",
+                         "CaseReader",
+                         "A pointer to the case reader from which this object is constructed",
+                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE );
+
+  object_class->set_property = psppire_case_file_set_property;
+  object_class->get_property = psppire_case_file_get_property;
+
+  g_object_class_install_property (object_class,
+                                   PROP_DATASHEET,
+                                   datasheet_spec);
+
+  g_object_class_install_property (object_class,
+                                   PROP_READER,
+                                   reader_spec);
+
   signals [CASE_CHANGED] =
-    g_signal_new ("case_changed",
+    g_signal_new ("case-changed",
                  G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
@@ -105,7 +179,7 @@ psppire_case_file_class_init (PsppireCaseFileClass *class)
 
 
   signals [CASE_INSERTED] =
-    g_signal_new ("case_inserted",
+    g_signal_new ("case-inserted",
                  G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
@@ -117,7 +191,7 @@ psppire_case_file_class_init (PsppireCaseFileClass *class)
 
 
   signals [CASES_DELETED] =
-    g_signal_new ("cases_deleted",
+    g_signal_new ("cases-deleted",
                  G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
@@ -133,7 +207,8 @@ psppire_case_file_finalize (GObject *object)
 {
   PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
 
-  datasheet_destroy (cf->datasheet);
+  if ( cf->accessible)
+    datasheet_destroy (cf->datasheet);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -142,6 +217,7 @@ static void
 psppire_case_file_init (PsppireCaseFile *cf)
 {
   cf->datasheet = NULL;
+  cf->accessible = FALSE;
 }
 
 
@@ -152,33 +228,27 @@ psppire_case_file_init (PsppireCaseFile *cf)
  * Creates a new #PsppireCaseFile.
  */
 PsppireCaseFile*
-psppire_case_file_new (void)
+psppire_case_file_new (struct casereader *reader)
 {
-  PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL);
-
-  cf->datasheet = datasheet_create (NULL);
-
-  return cf;
+  return g_object_new (G_TYPE_PSPPIRE_CASE_FILE,
+                      "casereader", reader,
+                      NULL);
 }
 
 
-void
-psppire_case_file_replace_datasheet (PsppireCaseFile *cf, struct datasheet *ds)
-{
-  cf->datasheet = ds;
-}
-
-
-
 gboolean
-psppire_case_file_delete_cases (PsppireCaseFile *cf, gint n_cases, gint first)
+psppire_case_file_delete_cases (PsppireCaseFile *cf, casenumber n_cases, casenumber first)
 {
   g_return_val_if_fail (cf, FALSE);
   g_return_val_if_fail (cf->datasheet, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
+
+  g_return_val_if_fail (first + n_cases <=
+                       psppire_case_file_get_case_count (cf), FALSE);
 
   datasheet_delete_rows (cf->datasheet, first, n_cases);
 
-  g_signal_emit (cf, signals [CASES_DELETED], 0, n_cases, first);
+  g_signal_emit (cf, signals [CASES_DELETED], 0, first, n_cases);
 
   return TRUE;
 }
@@ -186,14 +256,15 @@ psppire_case_file_delete_cases (PsppireCaseFile *cf, gint n_cases, gint first)
 /* Insert case CC into the case file before POSN */
 gboolean
 psppire_case_file_insert_case (PsppireCaseFile *cf,
-                             struct ccase *cc,
-                             gint posn)
+                              struct ccase *cc,
+                              casenumber posn)
 {
   struct ccase tmp;
   bool result ;
 
   g_return_val_if_fail (cf, FALSE);
   g_return_val_if_fail (cf->datasheet, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
 
   case_clone (&tmp, cc);
   result = datasheet_insert_rows (cf->datasheet, posn, &tmp, 1);
@@ -201,39 +272,17 @@ psppire_case_file_insert_case (PsppireCaseFile *cf,
   if ( result )
     g_signal_emit (cf, signals [CASE_INSERTED], 0, posn);
   else
-    g_warning ("Cannot insert case at position %d\n", posn);
+    g_warning ("Cannot insert case at position %ld\n", posn);
 
   return result;
 }
 
 
-/* Append a case to the case file */
-gboolean
-psppire_case_file_append_case (PsppireCaseFile *cf,
-                             struct ccase *c)
-{
-  struct ccase tmp;
-  bool result ;
-  gint posn ;
-
-  g_return_val_if_fail (cf, FALSE);
-  g_return_val_if_fail (cf->datasheet, FALSE);
-
-  posn = datasheet_get_row_cnt (cf->datasheet);
-
-  case_clone (&tmp, c);
-  result = datasheet_insert_rows (cf->datasheet, posn, &tmp, 1);
-
-  g_signal_emit (cf, signals [CASE_INSERTED], 0, posn);
-
-  return result;
-}
-
-
-inline gint
+casenumber
 psppire_case_file_get_case_count (const PsppireCaseFile *cf)
 {
   g_return_val_if_fail (cf, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
 
   if ( ! cf->datasheet)
     return 0;
@@ -283,7 +332,7 @@ psppire_case_file_clear (PsppireCaseFile *cf)
 /* Set the IDXth value of case C to V.
    Returns true if successful, false on I/O error. */
 gboolean
-psppire_case_file_set_value (PsppireCaseFile *cf, gint casenum, gint idx,
+psppire_case_file_set_value (PsppireCaseFile *cf, casenumber casenum, gint idx,
                            union value *v, gint width)
 {
   bool ok;
@@ -303,10 +352,10 @@ psppire_case_file_set_value (PsppireCaseFile *cf, gint casenum, gint idx,
 
 /* Set the IDXth value of case C using D_IN */
 gboolean
-psppire_case_file_data_in (PsppireCaseFile *cf, gint casenum, gint idx,
+psppire_case_file_data_in (PsppireCaseFile *cf, casenumber casenum, gint idx,
                           struct substring input, const struct fmt_spec *fmt)
 {
-  union value *value;
+  union value *value = NULL;
   int width;
   bool ok;
 
@@ -316,15 +365,15 @@ psppire_case_file_data_in (PsppireCaseFile *cf, gint casenum, gint idx,
   g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), FALSE);
 
   width = fmt_var_width (fmt);
-  value = xallocsa (value_cnt_from_width (width) * sizeof *value);
+  value = xmalloca (value_cnt_from_width (width) * sizeof *value);
   ok = (datasheet_get_value (cf->datasheet, casenum, idx, value, width)
-        && data_in (input, fmt->type, 0, 0, value, width)
+        && data_in (input, LEGACY_NATIVE, fmt->type, 0, 0, 0, value, width)
         && datasheet_put_value (cf->datasheet, casenum, idx, value, width));
 
   if (ok)
     g_signal_emit (cf, signals [CASE_CHANGED], 0, casenum);
 
-  freesa (value);
+  freea (value);
 
   return TRUE;
 }
@@ -347,29 +396,38 @@ psppire_case_file_sort (PsppireCaseFile *cf, struct case_ordering *ordering)
 
 
 /* Resize the cases in the casefile, by inserting N_VALUES into every
-   one of them. */
+   one of them at the position immediately preceeding WHERE.
+*/
 gboolean
 psppire_case_file_insert_values (PsppireCaseFile *cf,
-                                gint n_values, gint before)
+                                gint n_values, gint where)
 {
-  union value *values;
   g_return_val_if_fail (cf, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
+
+  if ( n_values == 0 )
+    return FALSE;
+
+  g_assert (n_values > 0);
 
   if ( ! cf->datasheet )
     cf->datasheet = datasheet_create (NULL);
 
-  values = xcalloc (n_values, sizeof *values);
-  datasheet_insert_columns (cf->datasheet, values, n_values, before);
-  free (values);
+  {
+    union value *values = xcalloc (n_values, sizeof *values);
+    datasheet_insert_columns (cf->datasheet, values, n_values, where);
+    free (values);
+  }
 
   return TRUE;
 }
 
+
 /* Fills C with the CASENUMth case.
    Returns true on success, false otherwise.
  */
 gboolean
-psppire_case_file_get_case (const PsppireCaseFile *cf, gint casenum,
+psppire_case_file_get_case (const PsppireCaseFile *cf, casenumber casenum,
                           struct ccase *c)
 {
   g_return_val_if_fail (cf, FALSE);
@@ -377,3 +435,14 @@ psppire_case_file_get_case (const PsppireCaseFile *cf, gint casenum,
 
   return datasheet_get_row (cf->datasheet, casenum, c);
 }
+
+
+
+struct casereader *
+psppire_case_file_make_reader (PsppireCaseFile *cf)
+{
+  struct casereader *r = datasheet_make_reader (cf->datasheet);
+  cf->accessible = FALSE;
+  return r;
+}
+