work on docs
[pspp] / src / ui / gui / psppire-data-editor.c
index 3f1f627dedd162cc42b974261df70b89ad26bef0..1928e10820106cef76636baa2f5767618cfd8e18 100644 (file)
@@ -1,6 +1,6 @@
 /* PSPPIRE - a graphical user interface for PSPP.
    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2016,
-   2017 Free Software Foundation, Inc.
+   2017, 2019 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
@@ -20,7 +20,6 @@
 #include "ui/gui/psppire-data-editor.h"
 
 #include <gtk/gtk.h>
-#include <gtk-contrib/gtkxpaned.h>
 
 #include "data/datasheet.h"
 #include "data/value-labels.h"
@@ -32,6 +31,7 @@
 #include "ui/gui/val-labs-dialog.h"
 #include "ui/gui/missing-val-dialog.h"
 #include "ui/gui/var-type-dialog.h"
+#include "ui/gui/value-variant.h"
 #include "ui/gui/psppire-dict.h"
 #include "ui/gui/psppire-data-store.h"
 #include "ui/gui/psppire-data-window.h"
 #include "ui/gui/psppire-data-sheet.h"
 
 
-#include "ui/gui/efficient-sheet/src/jmd-sheet.h"
+#include <ssw-sheet.h>
 
 #include <gettext.h>
 #define _(msgid) gettext (msgid)
 
-static void psppire_data_editor_class_init          (PsppireDataEditorClass *klass);
-static void psppire_data_editor_init                (PsppireDataEditor      *de);
-
 static void refresh_entry (PsppireDataEditor *);
 
-GType
-psppire_data_editor_get_type (void)
-{
-  static GType de_type = 0;
+G_DEFINE_TYPE (PsppireDataEditor, psppire_data_editor, GTK_TYPE_NOTEBOOK)
 
-  if (!de_type)
-    {
-      static const GTypeInfo de_info =
-      {
-       sizeof (PsppireDataEditorClass),
-       NULL, /* base_init */
-        NULL, /* base_finalize */
-       (GClassInitFunc) psppire_data_editor_class_init,
-        NULL, /* class_finalize */
-       NULL, /* class_data */
-        sizeof (PsppireDataEditor),
-       0,
-       (GInstanceInitFunc) psppire_data_editor_init,
-      };
-
-      de_type = g_type_register_static (GTK_TYPE_NOTEBOOK, "PsppireDataEditor",
-                                       &de_info, 0);
-    }
+static GObjectClass * parent_class = NULL;
 
-  return de_type;
-}
+static void
+psppire_data_editor_finalize (GObject *obj)
+{
+  PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (obj);
+  if (de->font)
+    pango_font_description_free (de->font);
 
-static GObjectClass * parent_class = NULL;
+  /* Chain up to the parent class */
+  G_OBJECT_CLASS (parent_class)->finalize (obj);
+}
 
 static void
 psppire_data_editor_dispose (GObject *obj)
 {
-  PsppireDataEditor *de = (PsppireDataEditor *) obj;
+  PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (obj);
 
-  if (de->data_store)
-    {
-      g_object_unref (de->data_store);
-      de->data_store = NULL;
-    }
+  if (de->dispose_has_run)
+    return;
 
-  if (de->dict)
-    {
-      g_object_unref (de->dict);
-      de->dict = NULL;
-    }
+  de->dispose_has_run = TRUE;
 
-  if (de->font != NULL)
-    {
-      pango_font_description_free (de->font);
-      de->font = NULL;
-    }
+  g_object_unref (de->data_store);
+  g_object_unref (de->dict);
 
   /* Chain up to the parent class */
   G_OBJECT_CLASS (parent_class)->dispose (obj);
@@ -138,7 +111,7 @@ psppire_data_editor_set_property (GObject         *object,
       g_object_set (de->var_sheet, "split", de->split, NULL);
       break;
     case PROP_DATA_STORE:
-      if ( de->data_store)
+      if (de->data_store)
         {
           g_signal_handlers_disconnect_by_func (de->data_store,
                                                 G_CALLBACK (refresh_entry),
@@ -168,7 +141,17 @@ psppire_data_editor_set_property (GObject         *object,
 
       g_object_set (de->var_sheet, "data-model", de->dict, NULL);
       break;
+
     case PROP_VALUE_LABELS:
+      {
+       gboolean l = g_value_get_boolean (value);
+       de->use_value_labels = l;
+       g_object_set (de->data_sheet, "forward-conversion",
+                     l ?
+                     psppire_data_store_value_to_string_with_labels :
+                     psppire_data_store_value_to_string,
+                     NULL);
+      }
       break;
 
     default:
@@ -197,6 +180,7 @@ psppire_data_editor_get_property (GObject         *object,
       g_value_set_pointer (value, de->dict);
       break;
     case PROP_VALUE_LABELS:
+      g_value_set_boolean (value, de->use_value_labels);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -210,7 +194,6 @@ psppire_data_editor_switch_page (GtkNotebook     *notebook,
                                  guint            page_num)
 {
   GTK_NOTEBOOK_CLASS (parent_class)->switch_page (notebook, w, page_num);
-
 }
 
 static void
@@ -218,7 +201,20 @@ psppire_data_editor_set_focus_child (GtkContainer *container,
                                      GtkWidget    *widget)
 {
   GTK_CONTAINER_CLASS (parent_class)->set_focus_child (container, widget);
+}
+
 
+static gboolean
+on_key_press (GtkWidget *w, GdkEventKey *e)
+{
+  PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (w);
+  if (e->keyval == GDK_KEY_F2 &&
+      PSPPIRE_DATA_EDITOR_DATA_VIEW == gtk_notebook_get_current_page (GTK_NOTEBOOK (de)))
+    {
+      gtk_widget_grab_focus (de->datum_entry);
+    }
+
+  return FALSE;
 }
 
 static void
@@ -232,22 +228,24 @@ psppire_data_editor_class_init (PsppireDataEditorClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
   GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
   parent_class = g_type_class_peek_parent (klass);
 
   object_class->dispose = psppire_data_editor_dispose;
+  object_class->finalize = psppire_data_editor_finalize;
   object_class->set_property = psppire_data_editor_set_property;
   object_class->get_property = psppire_data_editor_get_property;
 
   container_class->set_focus_child = psppire_data_editor_set_focus_child;
-
   notebook_class->switch_page = psppire_data_editor_switch_page;
+  widget_class->key_press_event = on_key_press;
 
   data_store_spec =
     g_param_spec_pointer ("data-store",
                          "Data Store",
                          "A pointer to the data store associated with this editor",
-                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
+                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE);
 
   g_object_class_install_property (object_class,
                                    PROP_DATA_STORE,
@@ -257,7 +255,7 @@ psppire_data_editor_class_init (PsppireDataEditorClass *klass)
     g_param_spec_pointer ("dictionary",
                          "Dictionary",
                          "A pointer to the dictionary associated with this editor",
-                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
+                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE);
 
   g_object_class_install_property (object_class,
                                    PROP_DICTIONARY,
@@ -295,19 +293,19 @@ on_var_sheet_var_double_clicked (void *var_sheet, gint dict_index,
   gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
                                  PSPPIRE_DATA_EDITOR_DATA_VIEW);
 
-  jmd_sheet_scroll_to (JMD_SHEET (de->data_sheet), dict_index, -1);
+  ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
 }
 
 
 static void
-on_data_sheet_var_double_clicked (JmdSheet *data_sheet, gint dict_index,
+on_data_sheet_var_double_clicked (SswSheet *data_sheet, gint dict_index,
                                  PsppireDataEditor *de)
 {
 
   gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
                                  PSPPIRE_DATA_EDITOR_VARIABLE_VIEW);
 
-  jmd_sheet_scroll_to (JMD_SHEET (de->var_sheet), -1, dict_index);
+  ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
 }
 
 
@@ -318,7 +316,7 @@ static void
 refresh_entry (PsppireDataEditor *de)
 {
   gint row, col;
-  if (jmd_sheet_get_active_cell (JMD_SHEET (de->data_sheet), &col, &row))
+  if (ssw_sheet_get_active_cell (SSW_SHEET (de->data_sheet), &col, &row))
     {
       union value val;
       const struct variable *var = psppire_dict_get_variable (de->dict, col);
@@ -339,14 +337,34 @@ refresh_entry (PsppireDataEditor *de)
 }
 
 static void
-on_datum_entry_activate (PsppireValueEntry *entry, PsppireDataEditor *de)
+on_datum_entry_activate (GtkEntry *entry, PsppireDataEditor *de)
 {
+  gint row, col;
+  if (ssw_sheet_get_active_cell (SSW_SHEET (de->data_sheet), &col, &row))
+    {
+      union value val;
+      const struct variable *var = psppire_dict_get_variable (de->dict, col);
+      if (var == NULL)
+       return;
+
+      int width = var_get_width (var);
+      value_init (&val, width);
+      if (psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (de->datum_entry),
+                                        &val, width))
+       {
+         psppire_data_store_set_value (de->data_store, row, var, &val);
+       }
+      value_destroy (&val, width);
+
+      gtk_widget_grab_focus (de->data_sheet);
+      ssw_sheet_set_active_cell (SSW_SHEET (de->data_sheet), col, row, NULL);
+    }
 }
 
 
 /* Called when the active cell or the selection in the data sheet changes */
 static void
-on_data_selection_change (PsppireDataEditor *de, JmdRange *sel)
+on_data_selection_change (PsppireDataEditor *de, SswRange *sel)
 {
   gchar *ref_cell_text = NULL;
 
@@ -395,18 +413,20 @@ static void set_font_recursively (GtkWidget *w, gpointer data);
 void
 psppire_data_editor_data_delete_variables (PsppireDataEditor *de)
 {
-  JmdRange *range = JMD_SHEET(de->data_sheet)->selection;
-
-  psppire_dict_delete_variables (de->dict, range->start_x,
-                                (range->end_x - range->start_x + 1));
-
-  gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
+  psppire_data_sheet_delete_variables (PSPPIRE_DATA_SHEET (de->data_sheet));
 }
 
 void
 psppire_data_editor_var_delete_variables (PsppireDataEditor *de)
 {
-  JmdRange *range = JMD_SHEET(de->var_sheet)->selection;
+  SswRange *range = SSW_SHEET(de->var_sheet)->selection;
+
+  if (range->start_x > range->end_x)
+    {
+      gint temp = range->start_x;
+      range->start_x = range->end_x;
+      range->end_x = temp;
+    }
 
   psppire_dict_delete_variables (de->dict, range->start_y,
                                 (range->end_y - range->start_y + 1));
@@ -417,6 +437,8 @@ psppire_data_editor_var_delete_variables (PsppireDataEditor *de)
 void
 psppire_data_editor_insert_new_case_at_posn  (PsppireDataEditor *de, gint posn)
 {
+  g_return_if_fail (posn >= 0);
+
   psppire_data_store_insert_new_case (de->data_store, posn);
 
   gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
@@ -425,11 +447,7 @@ psppire_data_editor_insert_new_case_at_posn  (PsppireDataEditor *de, gint posn)
 void
 psppire_data_editor_insert_new_variable_at_posn (PsppireDataEditor *de, gint posn)
 {
-  const struct variable *v = psppire_dict_insert_variable (de->dict, posn, NULL);
-  psppire_data_store_insert_value (de->data_store, var_get_width(v),
-                                  var_get_case_index (v));
-
-  gtk_widget_queue_draw (GTK_WIDGET (de));
+  psppire_data_sheet_insert_new_variable_at_posn (PSPPIRE_DATA_SHEET (de->data_sheet), posn);
 }
 
 static void
@@ -441,6 +459,8 @@ psppire_data_editor_init (PsppireDataEditor *de)
   GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (de));
   gtk_style_context_add_class (context, "psppire-data-editor");
 
+  de->dispose_has_run = FALSE;
+
   de->font = NULL;
 
   g_object_set (de, "tab-pos", GTK_POS_BOTTOM, NULL);
@@ -450,17 +470,18 @@ psppire_data_editor_init (PsppireDataEditor *de)
   gtk_widget_set_valign (de->cell_ref_label, GTK_ALIGN_CENTER);
 
   de->datum_entry = psppire_value_entry_new ();
-  g_signal_connect (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (de->datum_entry))),
-                    "activate", G_CALLBACK (on_datum_entry_activate), de);
+  g_signal_connect (de->datum_entry, "edit-done",
+                   G_CALLBACK (on_datum_entry_activate), de);
 
   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   gtk_box_pack_start (GTK_BOX (hbox), de->cell_ref_label, FALSE, FALSE, 0);
   gtk_box_pack_start (GTK_BOX (hbox), de->datum_entry, TRUE, TRUE, 0);
 
   de->split = FALSE;
+  de->use_value_labels = FALSE;
   de->data_sheet = psppire_data_sheet_new ();
 
-  GtkWidget *data_button = jmd_sheet_get_button (JMD_SHEET (de->data_sheet));
+  GtkWidget *data_button = ssw_sheet_get_button (SSW_SHEET (de->data_sheet));
   gtk_button_set_label (GTK_BUTTON (data_button), _("Case"));
   de->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
   gtk_box_pack_start (GTK_BOX (de->vbox), hbox, FALSE, FALSE, 0);
@@ -477,7 +498,7 @@ psppire_data_editor_init (PsppireDataEditor *de)
 
   de->var_sheet = psppire_variable_sheet_new ();
 
-  GtkWidget *var_button = jmd_sheet_get_button (JMD_SHEET (de->var_sheet));
+  GtkWidget *var_button = ssw_sheet_get_button (SSW_SHEET (de->var_sheet));
   gtk_button_set_label (GTK_BUTTON (var_button), _("Variable"));
 
   gtk_notebook_append_page (GTK_NOTEBOOK (de), de->var_sheet,
@@ -495,13 +516,14 @@ psppire_data_editor_init (PsppireDataEditor *de)
 
   if (psppire_conf_get_string (psppire_conf_new (),
                           "Data Editor", "font",
-                               &fontname) )
+                               &fontname))
     {
       de->font = pango_font_description_from_string (fontname);
       g_free (fontname);
       set_font_recursively (GTK_WIDGET (de), de->font);
     }
 
+  gtk_widget_add_events (GTK_WIDGET (de), GDK_KEY_PRESS_MASK);
 }
 
 GtkWidget*
@@ -519,8 +541,8 @@ psppire_data_editor_new (PsppireDict *dict,
 void
 psppire_data_editor_show_grid (PsppireDataEditor *de, gboolean grid_visible)
 {
-  g_object_set (JMD_SHEET (de->var_sheet), "gridlines", grid_visible, NULL);
-  g_object_set (JMD_SHEET (de->data_sheet), "gridlines", grid_visible, NULL);
+  g_object_set (SSW_SHEET (de->var_sheet), "gridlines", grid_visible, NULL);
+  g_object_set (SSW_SHEET (de->data_sheet), "gridlines", grid_visible, NULL);
 }
 
 
@@ -532,10 +554,15 @@ set_font_recursively (GtkWidget *w, gpointer data)
   GtkStyleContext *style = gtk_widget_get_style_context (w);
   GtkCssProvider *cssp = gtk_css_provider_new ();
 
-  gchar *str = pango_font_description_to_string (font_desc);
+  /* The Pango font description as string has a different syntax than the
+     css style description:
+     Pango: Courier Italic 12
+     CSS: italic 12pt Courier
+     I ignore Weight, Style and Variant and just take family and size */
+  const gchar *str = pango_font_description_get_family (font_desc);
+  gint size = pango_font_description_get_size (font_desc);
   gchar *css =
-    g_strdup_printf ("* {font: %s}", str);
-  g_free (str);
+    g_strdup_printf ("* {font: %dpt %s}", size/PANGO_SCALE, str);
 
   GError *err = NULL;
   gtk_css_provider_load_from_data (cssp, css, -1, &err);
@@ -552,7 +579,7 @@ set_font_recursively (GtkWidget *w, gpointer data)
   g_object_unref (cssp);
 
 
-  if ( GTK_IS_CONTAINER (w))
+  if (GTK_IS_CONTAINER (w))
     gtk_container_foreach (GTK_CONTAINER (w), set_font_recursively, font_desc);
 }
 
@@ -592,12 +619,12 @@ psppire_data_editor_goto_variable (PsppireDataEditor *de, gint dict_index)
   switch (page)
     {
       case PSPPIRE_DATA_EDITOR_DATA_VIEW:
-       jmd_sheet_scroll_to (JMD_SHEET (de->data_sheet), dict_index, -1);
-       jmd_sheet_set_active_cell (JMD_SHEET (de->data_sheet), dict_index, -1, NULL);
+       ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
+       ssw_sheet_set_active_cell (SSW_SHEET (de->data_sheet), dict_index, -1, NULL);
        break;
       case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
-       jmd_sheet_scroll_to (JMD_SHEET (de->var_sheet), -1, dict_index);
-       jmd_sheet_set_active_cell (JMD_SHEET (de->var_sheet), -1, dict_index, NULL);
+       ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
+       ssw_sheet_set_active_cell (SSW_SHEET (de->var_sheet), -1, dict_index, NULL);
        break;
     }
 }
@@ -620,10 +647,10 @@ store_set_datum (GtkTreeModel *model, gint col, gint row,
 void
 psppire_data_editor_paste (PsppireDataEditor *de)
 {
-  JmdSheet *sheet = JMD_SHEET (de->data_sheet);
+  SswSheet *sheet = SSW_SHEET (de->data_sheet);
   GtkClipboard *clip =
     gtk_clipboard_get_for_display (gtk_widget_get_display (GTK_WIDGET (sheet)),
                                   GDK_SELECTION_CLIPBOARD);
 
-  jmd_sheet_paste (sheet, clip, store_set_datum);
+  ssw_sheet_paste (sheet, clip, store_set_datum);
 }