Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / ui / gui / data-editor.c
index 5efa3ca6ada343af791439671f04f542cc46b3d2..ada84b1a66061580a7cf6f2587fdc1e8e5c66e61 100644 (file)
@@ -1,21 +1,18 @@
-/*
-    PSPPIRE --- A Graphical User Interface for PSPP
-    Copyright (C) 2006, 2007  Free Software Foundation
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2006, 2007  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 <stdlib.h>
@@ -36,6 +33,7 @@
 #include "transpose-dialog.h"
 #include "sort-cases-dialog.h"
 #include "compute-dialog.h"
+#include "comments-dialog.h"
 #include "variable-info-dialog.h"
 #include "dict-display.h"
 
 #include "psppire-data-store.h"
 #include "psppire-var-store.h"
 
-static void register_data_editor_actions (struct data_editor *de);
 
-static void insert_variable (GtkCheckMenuItem *m, gpointer data);
+/* Update the data_ref_entry with the reference of the active cell */
+static gint update_data_ref_entry (const GtkSheet *sheet,
+                                  gint row, gint col, gpointer data);
+
+static void register_data_editor_actions (struct data_editor *de);
 
+static void insert_variable (GtkAction *, gpointer data);
 
 
 /* Switch between the VAR SHEET and the DATA SHEET */
@@ -166,7 +168,22 @@ on_recent_files_select (GtkMenuShell *menushell,   gpointer user_data)
 
 #endif
 
+static void
+datum_entry_activate (GtkEntry *entry, gpointer data)
+{
+  gint row, column;
+  GtkSheet *data_sheet = GTK_SHEET (data);
+  PsppireDataStore *store = PSPPIRE_DATA_STORE (gtk_sheet_get_model (data_sheet));
+
+  const char *text = gtk_entry_get_text (entry);
+
+  gtk_sheet_get_active_cell (data_sheet, &row, &column);
 
+  if ( row == -1 || column == -1)
+    return;
+
+  psppire_data_store_set_string (store, text, row, column);
+}
 
 /*
   Create a new data editor.
@@ -177,7 +194,9 @@ new_data_editor (void)
   struct data_editor *de ;
   struct editor_window *e;
   GtkSheet *var_sheet ;
+  GtkSheet *data_sheet ;
   PsppireVarStore *vs;
+  GtkWidget *datum_entry;
 
   de = g_malloc0 (sizeof (*de));
 
@@ -186,11 +205,22 @@ new_data_editor (void)
   de->xml = XML_NEW ("data-editor.glade");
 
   var_sheet = GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
+  data_sheet = GTK_SHEET (get_widget_assert (de->xml, "data_sheet"));
 
   vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
 
   g_assert(vs); /* Traps a possible bug in win32 build */
 
+  g_signal_connect (G_OBJECT (data_sheet), "activate",
+                   G_CALLBACK (update_data_ref_entry),
+                   de->xml);
+
+  datum_entry = get_widget_assert (de->xml, "datum_entry");
+
+  g_signal_connect (G_OBJECT (datum_entry), "activate",
+                   G_CALLBACK (datum_entry_activate),
+                   data_sheet);
+
   g_signal_connect (vs->dict, "weight-changed",
                    G_CALLBACK (on_weight_change),
                    de);
@@ -208,6 +238,24 @@ new_data_editor (void)
 
   register_data_editor_actions (de);
 
+  de->insert_variable =
+    gtk_action_new ("insert-variable",
+                   _("Insert Variable"),
+                   _("Create a new variable at the current position"),
+                   "pspp-insert-variable");
+
+  g_signal_connect (de->insert_variable, "activate",
+                   G_CALLBACK (insert_variable), de);
+
+
+  gtk_action_connect_proxy (de->insert_variable,
+                           get_widget_assert (de->xml, "button-insert-variable")
+                           );
+
+  gtk_action_connect_proxy (de->insert_variable,
+                           get_widget_assert (de->xml, "data_insert-variable")
+                           );
+
   de->invoke_weight_cases_dialog =
     gtk_action_new ("weight-cases-dialog",
                    _("Weights"),
@@ -260,6 +308,15 @@ new_data_editor (void)
   g_signal_connect (de->invoke_compute_dialog, "activate",
                    G_CALLBACK (compute_dialog), de);
 
+  de->invoke_comments_dialog =
+    gtk_action_new ("commments-dialog",
+                   _("Data File Comments"),
+                   _("Commentary text for the data file"),
+                   NULL);
+
+  g_signal_connect (de->invoke_comments_dialog, "activate",
+                   G_CALLBACK (comments_dialog), de);
+
   de->invoke_variable_info_dialog  =
     gtk_action_new ("variable-info-dialog",
                    _("Variables"),
@@ -282,9 +339,6 @@ new_data_editor (void)
                            de->action_data_open);
 
 
-
-
-
 #if RECENT_LISTS_AVAILABLE
   {
     GtkRecentManager *rm = gtk_recent_manager_get_default ();
@@ -350,10 +404,6 @@ new_data_editor (void)
                    de);
 
 
-  g_signal_connect (get_widget_assert (de->xml,"data_insert-variable"),
-                   "activate",
-                   G_CALLBACK (insert_variable),
-                   de);
 
   gtk_action_connect_proxy (de->invoke_weight_cases_dialog,
                            get_widget_assert (de->xml, "data_weight-cases")
@@ -375,11 +425,14 @@ new_data_editor (void)
                            get_widget_assert (de->xml, "transform_compute")
                            );
 
+  gtk_action_connect_proxy (de->invoke_comments_dialog,
+                           get_widget_assert (de->xml, "utilities_comments")
+                           );
+
   gtk_action_connect_proxy (de->invoke_variable_info_dialog,
                            get_widget_assert (de->xml, "utilities_variables")
                            );
 
-
   g_signal_connect (get_widget_assert (de->xml,"help_about"),
                    "activate",
                    G_CALLBACK (about_new),
@@ -391,20 +444,16 @@ new_data_editor (void)
                    G_CALLBACK (reference_manual),
                    e->window);
 
-
-
   g_signal_connect (get_widget_assert (de->xml,"data_sheet"),
                    "double-click-column",
                    G_CALLBACK (click2column),
                    de);
 
-
   g_signal_connect (get_widget_assert (de->xml, "variable_sheet"),
                    "double-click-row",
                    GTK_SIGNAL_FUNC (click2row),
                    de);
 
-
   g_signal_connect (get_widget_assert (de->xml, "variable_sheet"),
                    "select-row",
                    GTK_SIGNAL_FUNC (enable_edit_clear),
@@ -500,6 +549,7 @@ static gboolean
 click2row (GtkWidget *w, gint row, gpointer data)
 {
   struct data_editor *de = data;
+  GtkSheetRange visible_range;
 
   gint current_row, current_column;
 
@@ -512,6 +562,14 @@ click2row (GtkWidget *w, gint row, gpointer data)
 
   gtk_sheet_set_active_cell (GTK_SHEET (data_sheet), current_row, row);
 
+  gtk_sheet_get_visible_range (GTK_SHEET (data_sheet), &visible_range);
+
+  if ( row < visible_range.col0 || row > visible_range.coli)
+    {
+      gtk_sheet_moveto (GTK_SHEET (data_sheet),
+                       current_row, row, 0, 0);
+    }
+
   return FALSE;
 }
 
@@ -781,10 +839,10 @@ on_clear_activate (GtkMenuItem *menuitem, gpointer data)
 /* Insert a new variable before the current row in the variable sheet,
    or before the current column in the data sheet, whichever is selected */
 static void
-insert_variable (GtkCheckMenuItem *m, gpointer data)
+insert_variable (GtkAction *action, gpointer data)
 {
   struct data_editor *de = data;
-  gint posn;
+  gint posn = -1;
 
   GtkWidget *notebook = get_widget_assert (de->xml, "notebook");
 
@@ -814,6 +872,8 @@ insert_variable (GtkCheckMenuItem *m, gpointer data)
       g_assert_not_reached ();
     }
 
+  if ( posn == -1 ) posn = 0;
+
   psppire_dict_insert_variable (vs->dict, posn, NULL);
 }
 
@@ -1215,3 +1275,64 @@ open_data_dialog (GtkAction *action, struct data_editor *de)
 
   gtk_widget_destroy (dialog);
 }
+
+
+
+/* Update the data_ref_entry with the reference of the active cell */
+static gint
+update_data_ref_entry (const GtkSheet *sheet, gint row, gint col, gpointer data)
+{
+  GladeXML *data_editor_xml = data;
+
+  PsppireDataStore *data_store =
+    PSPPIRE_DATA_STORE (gtk_sheet_get_model (sheet));
+
+  g_return_val_if_fail (data_editor_xml, FALSE);
+
+  if (data_store)
+    {
+      const struct variable *var =
+       psppire_dict_get_variable (data_store->dict, col);
+
+      /* The entry where the reference to the current cell is displayed */
+      GtkEntry *cell_ref_entry =
+       GTK_ENTRY (get_widget_assert (data_editor_xml,
+                                     "cell_ref_entry"));
+      GtkEntry *datum_entry =
+       GTK_ENTRY (get_widget_assert (data_editor_xml,
+                                     "datum_entry"));
+
+      if ( var )
+       {
+         gchar *text = g_strdup_printf ("%d: %s", row,
+                                        var_get_name (var));
+
+         gchar *s = pspp_locale_to_utf8 (text, -1, 0);
+
+         g_free (text);
+
+         gtk_entry_set_text (cell_ref_entry, s);
+
+         g_free (s);
+       }
+      else
+       gtk_entry_set_text (cell_ref_entry, "");
+
+
+      if ( var )
+       {
+         gchar *text =
+           psppire_data_store_get_string (data_store, row,
+                                          var_get_dict_index(var));
+         g_strchug (text);
+
+         gtk_entry_set_text (datum_entry, text);
+
+         free (text);
+       }
+      else
+       gtk_entry_set_text (datum_entry, "");
+    }
+
+  return FALSE;
+}