Add "dictionary" property to PsppireVarStore and use it.
[pspp-builds.git] / src / ui / gui / psppire-var-sheet.c
index 60b968ef811eb84f97ad9a8b172bb2f9328e1276..a4f082cca50bddb354b2559ce725896ba9c8c4fe 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008, 2009 Free Software Foundation, Inc.
 
    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
 
 #include <config.h>
 #include "psppire-var-sheet.h"
+#include <ui/gui/sheet/psppire-axis.h>
 
-#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"
@@ -31,6 +31,9 @@
 
 static void psppire_var_sheet_class_init  (PsppireVarSheetClass *klass);
 static void psppire_var_sheet_init        (PsppireVarSheet      *vs);
+static void psppire_var_sheet_realize     (GtkWidget *w);
+static void psppire_var_sheet_unrealize   (GtkWidget *w);
+
 
 enum
   {
@@ -57,7 +60,7 @@ psppire_var_sheet_get_type (void)
        (GInstanceInitFunc) psppire_var_sheet_init,
       };
 
-      vs_type = g_type_register_static (GTK_TYPE_SHEET, "PsppireVarSheet",
+      vs_type = g_type_register_static (PSPPIRE_TYPE_SHEET, "PsppireVarSheet",
                                        &vs_info, 0);
     }
 
@@ -95,20 +98,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]={
@@ -135,9 +124,7 @@ create_label_list (const gchar *const *labels)
   gint i = 0;
   GtkTreeIter iter;
 
-  GtkListStore *list_store;
-  list_store = gtk_list_store_new (1, G_TYPE_STRING);
-
+  GtkListStore *list_store = gtk_list_store_new (1, G_TYPE_STRING);
 
   while ( (s = labels[i++]))
     {
@@ -192,17 +179,19 @@ psppire_var_sheet_get_property (GObject      *object,
 }
 
 
-
 static void
 psppire_var_sheet_class_init (PsppireVarSheetClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_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;
+  widget_class->realize = psppire_var_sheet_realize;
+  widget_class->unrealize = psppire_var_sheet_unrealize;
   object_class->set_property = psppire_var_sheet_set_property;
   object_class->get_property = psppire_var_sheet_get_property;
 
@@ -253,29 +242,28 @@ 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)
+traverse_cell_callback (PsppireSheet *sheet,
+                       const PsppireSheetCell *existing_cell,
+                       PsppireSheetCell *new_cell)
 {
   PsppireVarSheet *var_sheet = PSPPIRE_VAR_SHEET (sheet);
-  PsppireVarStore *var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
+  PsppireVarStore *var_store = PSPPIRE_VAR_STORE (psppire_sheet_get_model (sheet));
 
   gint n_vars = psppire_var_store_get_var_cnt (var_store);
 
-  if (*new_row >= n_vars && !var_sheet->may_create_vars)
+  if (new_cell->row >= n_vars && !var_sheet->may_create_vars)
     return TRUE;
 
-
-  if ( row == n_vars && *new_row >= n_vars)
+  if ( existing_cell->row == n_vars && new_cell->row >= n_vars)
     {
-      GtkEntry *entry = GTK_ENTRY (gtk_sheet_get_entry (sheet));
+      GtkEntry *entry = psppire_sheet_get_entry (sheet);
 
       const gchar *name = gtk_entry_get_text (entry);
 
-      if (! psppire_dict_check_name (var_store->dict, name, TRUE))
+      if (! psppire_dict_check_name (var_store->dictionary, name, TRUE))
        return TRUE;
 
-      psppire_dict_insert_variable (var_store->dictrow, name);
+      psppire_dict_insert_variable (var_store->dictionary, existing_cell->row, name);
 
       return FALSE;
     }
@@ -284,22 +272,20 @@ traverse_cell_callback (GtkSheet *sheet,
   /* 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 )
-       psppire_dict_insert_variable (var_store->dict, i, NULL);
+      for ( i = n_vars ; i <= new_cell->row; ++i )
+       psppire_dict_insert_variable (var_store->dictionary, i, NULL);
     }
 
-
-
   return FALSE;
 }
 
 
 
-
 /*
    Callback whenever the active cell changes on the var sheet.
 */
@@ -309,37 +295,34 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
                              gint oldrow, gint oldcolumn,
                              gpointer data)
 {
-  GtkSheetCellAttr attributes;
   PsppireVarStore *var_store;
   PsppireVarSheetClass *vs_class =
     PSPPIRE_VAR_SHEET_CLASS(G_OBJECT_GET_CLASS (vs));
 
   struct variable *var ;
-  GtkSheet *sheet = GTK_SHEET (vs);
+  PsppireSheet *sheet = PSPPIRE_SHEET (vs);
 
   g_return_if_fail (sheet != NULL);
 
-  var_store = PSPPIRE_VAR_STORE (gtk_sheet_get_model (sheet));
+  var_store = PSPPIRE_VAR_STORE (psppire_sheet_get_model (sheet));
 
   g_assert (var_store);
 
   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)
     {
     case PSPPIRE_VAR_STORE_COL_ALIGN:
       {
+       GtkEntry *entry;
        static GtkListStore *list_store = NULL;
        GtkComboBoxEntry *cbe;
-       gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
-       cbe =
-         GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
-
+       psppire_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
+       entry = psppire_sheet_get_entry (sheet);
+       cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
 
        if ( ! list_store) list_store = create_label_list (alignments);
 
@@ -348,26 +331,25 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
 
        gtk_combo_box_entry_set_text_column (cbe, 0);
 
-       g_signal_connect (G_OBJECT (cbe),"changed",
+       g_signal_connect (cbe, "changed",
                         G_CALLBACK (change_alignment), var);
       }
       break;
 
     case PSPPIRE_VAR_STORE_COL_MEASURE:
       {
+       GtkEntry *entry;
        GtkComboBoxEntry *cbe;
-       gtk_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
-       cbe =
-         GTK_COMBO_BOX_ENTRY (gtk_sheet_get_entry (sheet)->parent);
-
-
+       psppire_sheet_change_entry (sheet, GTK_TYPE_COMBO_BOX_ENTRY);
+       entry = psppire_sheet_get_entry (sheet);
+       cbe = GTK_COMBO_BOX_ENTRY (GTK_WIDGET (entry)->parent);
 
        gtk_combo_box_set_model (GTK_COMBO_BOX (cbe),
                                GTK_TREE_MODEL (vs_class->measure_list));
 
        gtk_combo_box_entry_set_text_column (cbe, 0);
 
-       g_signal_connect (G_OBJECT (cbe),"changed",
+       g_signal_connect (cbe, "changed",
                          G_CALLBACK (change_measure), var);
       }
       break;
@@ -376,15 +358,10 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
       {
        PsppireCustomEntry *customEntry;
 
-       gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
+       psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
 
        customEntry =
-         PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
-
-       if ( var_is_long_string (var))
-         g_object_set (customEntry,
-                       "editable", FALSE,
-                       NULL);
+         PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
 
        val_labs_dialog_set_target_variable (vs->val_labs_dialog, var);
 
@@ -399,20 +376,16 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
       {
        PsppireCustomEntry *customEntry;
 
-       gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
+       psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
 
        customEntry =
-         PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
-
-       if ( var_is_long_string (var))
-         g_object_set (customEntry,
-                       "editable", FALSE,
-                       NULL);
-
+         PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
 
        vs->missing_val_dialog->pv =
          psppire_var_store_get_var (var_store, row);
 
+       vs->missing_val_dialog->dict = var_store->dictionary;
+
        g_signal_connect_swapped (customEntry,
                                  "clicked",
                                  G_CALLBACK (missing_val_dialog_show),
@@ -424,10 +397,10 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
       {
        PsppireCustomEntry *customEntry;
 
-       gtk_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
+       psppire_sheet_change_entry (sheet, PSPPIRE_CUSTOM_ENTRY_TYPE);
 
        customEntry =
-         PSPPIRE_CUSTOM_ENTRY (gtk_sheet_get_entry (sheet));
+         PSPPIRE_CUSTOM_ENTRY (psppire_sheet_get_entry (sheet));
 
 
        /* Popup the Variable Type dialog box */
@@ -444,11 +417,12 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
     case PSPPIRE_VAR_STORE_COL_DECIMALS:
     case PSPPIRE_VAR_STORE_COL_COLUMNS:
       {
-       if ( attributes.is_editable)
+       if ( psppire_sheet_model_is_editable (PSPPIRE_SHEET_MODEL(var_store),
+                                             row, column))
          {
            gint r_min, r_max;
 
-           const gchar *s = gtk_sheet_cell_get_text (sheet, row, column);
+           const gchar *s = psppire_sheet_cell_get_text (sheet, row, column);
 
            if (s)
              {
@@ -480,10 +454,10 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
                                         1.0, 1.0, 1.0 /* steps */
                                         );
 
-               gtk_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
+               psppire_sheet_change_entry (sheet, GTK_TYPE_SPIN_BUTTON);
 
                spinButton =
-                 GTK_SPIN_BUTTON (gtk_sheet_get_entry (sheet));
+                 GTK_SPIN_BUTTON (psppire_sheet_get_entry (sheet));
 
                gtk_spin_button_set_adjustment (spinButton, GTK_ADJUSTMENT (adj));
                gtk_spin_button_set_digits (spinButton, 0);
@@ -493,41 +467,56 @@ var_sheet_change_active_cell (PsppireVarSheet *vs,
       break;
 
     default:
-      gtk_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
+      psppire_sheet_change_entry (sheet, GTK_TYPE_ENTRY);
       break;
     }
 }
 
 
+static void
+psppire_var_sheet_realize (GtkWidget *w)
+{
+  PsppireVarSheet *vs = PSPPIRE_VAR_SHEET (w);
+
+  GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (vs));
+
+  vs->val_labs_dialog = val_labs_dialog_create (GTK_WINDOW (toplevel),
+                                               PSPPIRE_VAR_STORE (psppire_sheet_get_model (PSPPIRE_SHEET (vs))));
+
+  vs->missing_val_dialog = missing_val_dialog_create (GTK_WINDOW (toplevel));
+  vs->var_type_dialog = var_type_dialog_create (GTK_WINDOW (toplevel));
+
+  /* Chain up to the parent class */
+  GTK_WIDGET_CLASS (parent_class)->realize (w);
+}
+
+static void
+psppire_var_sheet_unrealize (GtkWidget *w)
+{
+  PsppireVarSheet *vs = PSPPIRE_VAR_SHEET (w);
+
+  g_free (vs->val_labs_dialog);
+  g_free (vs->missing_val_dialog);
+  g_free (vs->var_type_dialog);
+
+  /* Chain up to the parent class */
+  GTK_WIDGET_CLASS (parent_class)->unrealize (w);
+}
+
 
 
 static void
 psppire_var_sheet_init (PsppireVarSheet *vs)
 {
-  gint i;
-  GObject *geo = g_sheet_hetero_column_new (75, PSPPIRE_VAR_STORE_n_COLS);
-  GladeXML *xml = XML_NEW ("data-editor.glade");
+  GtkBuilder *builder = builder_new ("data-editor.ui");
 
-  vs->val_labs_dialog = val_labs_dialog_create (xml);
-  vs->missing_val_dialog = missing_val_dialog_create (xml);
-  vs->var_type_dialog = var_type_dialog_create (xml);
+  connect_help (builder);
 
-  g_object_unref (xml);
+  g_object_unref (builder);
 
   vs->dispose_has_run = FALSE;
   vs->may_create_vars = TRUE;
 
-  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);
-
   g_signal_connect (vs, "activate",
                    G_CALLBACK (var_sheet_change_active_cell),
                    NULL);
@@ -537,8 +526,42 @@ 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 *ha = psppire_axis_new ();
+  PsppireAxis *va = psppire_axis_new ();
+
+  GtkWidget *w = g_object_new (psppire_var_sheet_get_type (), NULL);
+
+  for (i = 0 ; i < 10 ; ++i)
+    psppire_axis_append (ha, column_def[i].width);
+
+  g_object_set (va,
+               "default-size", 25,
+               NULL);
+
+  g_object_set (ha, "minimum-extent", 0,
+               NULL);
+
+  g_object_set (w,
+               "horizontal-axis", ha,
+               "vertical-axis", va,
+               NULL);
+
+  return w;
 }