Merge branch 'rewrite-sheet' of ssh://jmd@git.sv.gnu.org/srv/git/pspp into rewrite...
[pspp] / src / ui / gui / psppire-var-sheet.c
index a4260405c82ce1e823d4e478d631be0d32679c13..033652025a870812214b84dcf630702cb04942a2 100644 (file)
@@ -1,4 +1,3 @@
-
 /* PSPPIRE - a graphical user interface for PSPP.
    Copyright (C) 2008 Free Software Foundation, Inc.
 
@@ -20,7 +19,7 @@
 
 #include <glade/glade.h>
 #include "helper.h"
-#include <gtksheet/gsheet-hetero-column.h>
+
 #include "customentry.h"
 #include <data/variable.h>
 #include "psppire-var-store.h"
 static void psppire_var_sheet_class_init  (PsppireVarSheetClass *klass);
 static void psppire_var_sheet_init        (PsppireVarSheet      *vs);
 
+enum
+  {
+    PSPPIRE_VAR_SHEET_MAY_CREATE_VARS = 1
+  };
 
 GType
 psppire_var_sheet_get_type (void)
@@ -92,20 +95,6 @@ struct column_parameters
   gint width ;
 };
 
-static const struct column_parameters column_def[] = {
-  { N_("Name"),    80},
-  { N_("Type"),    100},
-  { N_("Width"),   57},
-  { N_("Decimals"),91},
-  { N_("Label"),   95},
-  { N_("Values"),  103},
-  { N_("Missing"), 95},
-  { N_("Columns"), 80},
-  { N_("Align"),   69},
-  { N_("Measure"), 99},
-};
-
-
 #define n_ALIGNMENTS 3
 
 const gchar *const alignments[n_ALIGNMENTS + 1]={
@@ -148,18 +137,69 @@ create_label_list (const gchar *const *labels)
 }
 
 
+static void
+psppire_var_sheet_set_property (GObject      *object,
+                                guint         property_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+  PsppireVarSheet *self = (PsppireVarSheet *) object;
+
+  switch (property_id)
+    {
+    case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
+      self->may_create_vars = g_value_get_boolean (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+psppire_var_sheet_get_property (GObject      *object,
+                                guint         property_id,
+                                GValue       *value,
+                                GParamSpec   *pspec)
+{
+  PsppireVarSheet *self = (PsppireVarSheet *) object;
+
+  switch (property_id)
+    {
+    case PSPPIRE_VAR_SHEET_MAY_CREATE_VARS:
+      g_value_set_boolean (value, self->may_create_vars);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+      break;
+    }
+}
+
 
 
 static void
 psppire_var_sheet_class_init (PsppireVarSheetClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GParamSpec *pspec;
 
   parent_class = g_type_class_peek_parent (klass);
 
   object_class->dispose = psppire_var_sheet_dispose;
   object_class->finalize = psppire_var_sheet_finalize;
-
+  object_class->set_property = psppire_var_sheet_set_property;
+  object_class->get_property = psppire_var_sheet_get_property;
+
+  pspec = g_param_spec_boolean ("may-create-vars",
+                                "May create variables",
+                                "Whether the user may create more variables",
+                                TRUE,
+                                G_PARAM_READWRITE);
+  g_object_class_install_property (object_class,
+                                   PSPPIRE_VAR_SHEET_MAY_CREATE_VARS,
+                                   pspec);
 
   klass->measure_list = create_label_list (measures);
   klass->alignment_list = create_label_list (alignments);
@@ -196,87 +236,82 @@ change_measure (GtkComboBox *cb,
 }
 
 
-
+/* Moves the focus to a new cell.
+   Returns TRUE iff the move should be disallowed */
 static gboolean
 traverse_cell_callback (GtkSheet *sheet,
-                       gint row, gint column,
-                       gint *new_row, gint *new_column
-                       )
+                       const GtkSheetCell *existing_cell,
+                       GtkSheetCell *new_cell)
 {
+  PsppireVarSheet *var_sheet = PSPPIRE_VAR_SHEET (sheet);
   PsppireVarStore *var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
 
   gint n_vars = psppire_var_store_get_var_cnt (var_store);
 
-  if ( row == n_vars && *new_row >= n_vars)
+  if (new_cell->row >= n_vars && !var_sheet->may_create_vars)
+    return TRUE;
+
+  if ( existing_cell->row == n_vars && new_cell->row >= n_vars)
     {
       GtkEntry *entry = GTK_ENTRY (gtk_sheet_get_entry (sheet));
 
       const gchar *name = gtk_entry_get_text (entry);
 
       if (! psppire_dict_check_name (var_store->dict, name, TRUE))
-       return FALSE;
+       return TRUE;
 
-      psppire_dict_insert_variable (var_store->dict, row, name);
+      psppire_dict_insert_variable (var_store->dict, existing_cell->row, name);
 
-      return TRUE;
+      return FALSE;
     }
 
+
   /* If the destination cell is outside the current  variables, then
      automatically create variables for the new rows.
   */
-  if ( (*new_row > n_vars) ||
-       (*new_row == n_vars && *new_column != PSPPIRE_VAR_STORE_COL_NAME) )
+  if ( ((new_cell->row > n_vars) ||
+        (new_cell->row == n_vars &&
+        new_cell->col != PSPPIRE_VAR_STORE_COL_NAME)) )
     {
       gint i;
-      for ( i = n_vars ; i <= *new_row; ++i )
+      for ( i = n_vars ; i <= new_cell->row; ++i )
        psppire_dict_insert_variable (var_store->dict, i, NULL);
     }
 
-  return TRUE;
+  return FALSE;
 }
 
 
 
 
 /*
-   Callback whenever the pointer leaves a cell on the var sheet.
-*/
-static gboolean
-var_sheet_cell_entry_leave (GtkSheet * sheet, gint row, gint column,
-                           gpointer data)
-{
-  gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
-  return TRUE;
-}
-
-
-/*
-   Callback whenever the pointer enters a cell on the var sheet.
+   Callback whenever the active cell changes on the var sheet.
 */
-static gboolean
-var_sheet_cell_entry_enter (PsppireVarSheet *vs, gint row, gint column,
-                           gpointer data)
+static void
+var_sheet_change_active_cell (PsppireVarSheet *vs,
+                             gint row, gint column,
+                             gint oldrow, gint oldcolumn,
+                             gpointer data)
 {
   GtkSheetCellAttr attributes;
-  PsppireVarStore *var_store ;
+  PsppireVarStore *var_store;
   PsppireVarSheetClass *vs_class =
     PSPPIRE_VAR_SHEET_CLASS(G_OBJECT_GET_CLASS (vs));
 
   struct variable *var ;
   GtkSheet *sheet = GTK_SHEET (vs);
 
-  g_return_val_if_fail (sheet != NULL, FALSE);
+  g_return_if_fail (sheet != NULL);
 
   var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
 
   g_assert (var_store);
 
-  if ( row >= psppire_var_store_get_var_cnt (var_store))
-    return TRUE;
+  g_return_if_fail (oldcolumn == PSPPIRE_VAR_STORE_COL_NAME ||
+                   row < psppire_var_store_get_var_cnt (var_store));
 
   gtk_sheet_get_attributes (sheet, row, column, &attributes);
 
-
   var = psppire_var_store_get_var (var_store, row);
 
   switch (column)
@@ -445,9 +480,6 @@ var_sheet_cell_entry_enter (PsppireVarSheet *vs, gint row, gint column,
       gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
       break;
     }
-
-
-  return TRUE;
 }
 
 
@@ -456,8 +488,7 @@ var_sheet_cell_entry_enter (PsppireVarSheet *vs, gint row, gint column,
 static void
 psppire_var_sheet_init (PsppireVarSheet *vs)
 {
-  gint i;
-  GObject *geo = g_sheet_hetero_column_new (75, PSPPIRE_VAR_STORE_n_COLS);
+  // gint i;
   GladeXML *xml = XML_NEW ("data-editor.glade");
 
   vs->val_labs_dialog = val_labs_dialog_create (xml);
@@ -467,25 +498,18 @@ psppire_var_sheet_init (PsppireVarSheet *vs)
   g_object_unref (xml);
 
   vs->dispose_has_run = FALSE;
+  vs->may_create_vars = TRUE;
 
+#if 0
   for (i = 0 ; i < PSPPIRE_VAR_STORE_n_COLS ; ++i )
     {
-      g_sheet_hetero_column_set_button_label (G_SHEET_HETERO_COLUMN (geo), i,
-                                             gettext (column_def[i].label));
-
       g_sheet_hetero_column_set_width (G_SHEET_HETERO_COLUMN (geo), i,
                                       column_def[i].width);
     }
-
-  g_object_set (vs, "column-geometry", geo, NULL);
-
+#endif
 
   g_signal_connect (vs, "activate",
-                   G_CALLBACK (var_sheet_cell_entry_enter),
-                   NULL);
-
-  g_signal_connect (vs, "deactivate",
-                   G_CALLBACK (var_sheet_cell_entry_leave),
+                   G_CALLBACK (var_sheet_change_active_cell),
                    NULL);
 
   g_signal_connect (vs, "traverse",
@@ -493,8 +517,32 @@ psppire_var_sheet_init (PsppireVarSheet *vs)
 }
 
 
+static const struct column_parameters column_def[] = {
+  { N_("Name"),    80},
+  { N_("Type"),    100},
+  { N_("Width"),   57},
+  { N_("Decimals"),91},
+  { N_("Label"),   95},
+  { N_("Values"),  103},
+  { N_("Missing"), 95},
+  { N_("Columns"), 80},
+  { N_("Align"),   69},
+  { N_("Measure"), 99},
+};
+
 GtkWidget*
 psppire_var_sheet_new (void)
 {
-  return GTK_WIDGET (g_object_new (psppire_var_sheet_get_type (), NULL));
+  gint i;
+  PsppireAxis *a = psppire_axis_new ();
+  GtkWidget *w = g_object_new (psppire_var_sheet_get_type (), NULL);
+
+  for (i = 0 ; i < 10 ; ++i)
+    psppire_axis_append (a, column_def[i].width);
+
+  g_object_set (w,
+               "horizontal-axis", a,
+               NULL);
+
+  return w;
 }