treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / ui / gui / psppire-dict.c
index 1efe9528056d570d36b7eeee773db1d8e258a4c0..215a1d3e5811cbe02a3c94589fb8d81d5cec178a 100644 (file)
@@ -1,5 +1,6 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012, 2016  Free Software Foundation
+   Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2012,
+   2016, 2017  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
@@ -33,8 +34,6 @@
 #include "ui/gui/psppire-marshal.h"
 #include "ui/gui/psppire-var-ptr.h"
 
-#include "ui/gui/efficient-sheet/jmd-datum.h"
-
 #include <gobject/genums.h>
 
 #include <gettext.h>
@@ -49,8 +48,6 @@ GType role_enum_type;
 
 
 enum  {
-  ITEMS_CHANGED,
-
   VARIABLE_CHANGED,
   VARIABLE_INSERTED,
   VARIABLE_DELETED,
@@ -58,13 +55,14 @@ enum  {
   WEIGHT_CHANGED,
   FILTER_CHANGED,
   SPLIT_CHANGED,
+
+  RESIZE_ITEM,
+
   n_SIGNALS
 };
 
 
 /* --- prototypes --- */
-static void psppire_dict_class_init    (PsppireDictClass       *class);
-static void psppire_dict_init  (PsppireDict            *dict);
 static void psppire_dict_dispose       (GObject                *object);
 
 static void dictionary_tree_model_init (GtkTreeModelIface *iface);
@@ -76,40 +74,58 @@ gni (GListModel *list)
 {
   PsppireDict *dict = PSPPIRE_DICT (list);
 
-  return psppire_dict_get_var_cnt (dict);
+  return psppire_dict_get_n_vars (dict);
 }
 
 static GType
 git (GListModel *list)
 {
-  return JMD_TYPE_DATUM;
+  return GTK_TYPE_BUTTON;
 }
 
 static gpointer
 gi (GListModel *list, guint id)
 {
-  JmdDatum *gd = JMD_DATUM (g_object_new (JMD_TYPE_DATUM, NULL));
+  GtkWidget *button = gtk_button_new ();
 
   PsppireDict *dict = PSPPIRE_DICT (list);
-  
-  if (id >= psppire_dict_get_var_cnt (dict))
+
+  if (id >= psppire_dict_get_n_vars (dict))
     {
-      gd->text = g_strdup (_("Var"));
+      gtk_button_set_label (GTK_BUTTON (button),  _("Var"));
     }
   else
     {
       const struct variable *v =  psppire_dict_get_variable (dict, id);
 
-      gd->text = g_strdup (var_get_name (v));
-      gd->label = g_strdup (var_get_label (v));
+      gtk_button_set_label (GTK_BUTTON (button),  var_get_name (v));
+      gtk_widget_set_tooltip_text (button, var_get_label (v));
+
+      {
+       PangoContext *context = gtk_widget_create_pango_context (button);
+       PangoLayout *layout = pango_layout_new (context);
+       PangoRectangle rect;
+
+       pango_layout_set_text (layout, "M", 1);
+
+       pango_layout_get_extents (layout, NULL, &rect);
+
+       g_object_unref (G_OBJECT (layout));
+       g_object_unref (G_OBJECT (context));
+
+       gtk_widget_set_size_request (button,
+                                    (0.25 + var_get_display_width (v))
+                                    * rect.width / PANGO_SCALE,
+                                    -1);
+      }
     }
 
-  return gd;
+  return button;
 }
 
 
 static void
-jmd_init_iface (GListModelInterface *iface)
+ssw_init_iface (GListModelInterface *iface)
 {
   iface->get_n_items = gni;
   iface->get_item = gi;
@@ -123,55 +139,12 @@ static GObjectClass     *parent_class = NULL;
 static guint signals [n_SIGNALS];
 
 /* --- functions --- */
-/**
- * psppire_dict_get_type:
- * @returns: the type ID for accelerator groups.
- */
-GType
-psppire_dict_get_type (void)
-{
-  static GType object_type = 0;
-
-  if (!object_type)
-    {
-      static const GTypeInfo object_info = {
-       sizeof (PsppireDictClass),
-       (GBaseInitFunc) NULL,
-       (GBaseFinalizeFunc) NULL,
-       (GClassInitFunc) psppire_dict_class_init,
-       NULL,   /* class_finalize */
-       NULL,   /* class_data */
-       sizeof (PsppireDict),
-       0,      /* n_preallocs */
-       (GInstanceInitFunc) psppire_dict_init,
-      };
-
-      static const GInterfaceInfo tree_model_info = {
-       (GInterfaceInitFunc) dictionary_tree_model_init,
-       NULL,
-       NULL
-      };
-
-      static const GInterfaceInfo list_model_info = {
-       (GInterfaceInitFunc) jmd_init_iface,
-       NULL,
-       NULL
-      };
-
-      object_type = g_type_register_static (G_TYPE_OBJECT,
-                                           "PsppireDict",
-                                           &object_info, 0);
-      
-      g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
-                                  &tree_model_info);
-
-      g_type_add_interface_static (object_type, G_TYPE_LIST_MODEL,
-                                  &list_model_info);
-    }
-
-  return object_type;
-}
 
+G_DEFINE_TYPE_WITH_CODE (PsppireDict, psppire_dict, G_TYPE_OBJECT,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
+                                               dictionary_tree_model_init)
+                        G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
+                                               ssw_init_iface))
 
 static void
 psppire_dict_class_init (PsppireDictClass *class)
@@ -181,20 +154,18 @@ psppire_dict_class_init (PsppireDictClass *class)
   parent_class = g_type_class_peek_parent (class);
 
   object_class->dispose = psppire_dict_dispose;
-  
-  signals [ITEMS_CHANGED] =
-    g_signal_new ("changed",
+
+  signals [RESIZE_ITEM] =
+    g_signal_new ("resize-item",
                  G_TYPE_FROM_CLASS (class),
-                 G_SIGNAL_RUN_FIRST,
+                 G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL,
-                 psppire_marshal_VOID__UINT_UINT_UINT,
-                 G_TYPE_NONE,
-                 3,
-                 G_TYPE_UINT,
-                 G_TYPE_UINT,
-                 G_TYPE_UINT);
-
+                 psppire_marshal_BOOLEAN__INT_INT,
+                 G_TYPE_BOOLEAN,
+                 2,
+                 G_TYPE_INT,
+                 G_TYPE_INT);
 
   signals [VARIABLE_CHANGED] =
     g_signal_new ("variable-changed",
@@ -207,10 +178,7 @@ psppire_dict_class_init (PsppireDictClass *class)
                  3,
                  G_TYPE_INT,
                  G_TYPE_UINT,
-                 G_TYPE_POINTER
-                 );
-
-
+                 G_TYPE_POINTER);
 
   signals [VARIABLE_INSERTED] =
     g_signal_new ("variable-inserted",
@@ -223,7 +191,6 @@ psppire_dict_class_init (PsppireDictClass *class)
                  1,
                  G_TYPE_INT);
 
-
   signals [VARIABLE_DELETED] =
     g_signal_new ("variable-deleted",
                  G_TYPE_FROM_CLASS (class),
@@ -237,7 +204,6 @@ psppire_dict_class_init (PsppireDictClass *class)
                  G_TYPE_INT,
                  G_TYPE_INT);
 
-
   signals [WEIGHT_CHANGED] =
     g_signal_new ("weight-changed",
                  G_TYPE_FROM_CLASS (class),
@@ -249,7 +215,6 @@ psppire_dict_class_init (PsppireDictClass *class)
                  1,
                  G_TYPE_INT);
 
-
   signals [FILTER_CHANGED] =
     g_signal_new ("filter-changed",
                  G_TYPE_FROM_CLASS (class),
@@ -261,7 +226,6 @@ psppire_dict_class_init (PsppireDictClass *class)
                  1,
                  G_TYPE_INT);
 
-
   signals [SPLIT_CHANGED] =
     g_signal_new ("split-changed",
                  G_TYPE_FROM_CLASS (class),
@@ -278,7 +242,13 @@ psppire_dict_dispose (GObject *object)
 {
   PsppireDict *d = PSPPIRE_DICT (object);
 
+  if (!d->dispose_has_run)
+    return;
+
+  d->dispose_has_run = TRUE;
+
   dict_set_callbacks (d->dict, NULL, NULL);
+  dict_unref (d->dict);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -290,10 +260,10 @@ addcb (struct dictionary *d, int idx, void *pd)
 {
   PsppireDict *dict = PSPPIRE_DICT (pd);
 
-  if ( ! dict->disable_insert_signal)
+  if (! dict->disable_insert_signal)
     {
       g_signal_emit (dict, signals [VARIABLE_INSERTED], 0, idx);
-      g_signal_emit (dict, signals [ITEMS_CHANGED], 0, idx, 1, 1);
+      g_signal_emit_by_name (dict, "items-changed", idx, 1, 1);
     }
 }
 
@@ -303,14 +273,14 @@ delcb (struct dictionary *d, const struct variable *var,
 {
   g_signal_emit (pd, signals [VARIABLE_DELETED], 0,
                  var, dict_idx, case_idx);
-  g_signal_emit (pd, signals [ITEMS_CHANGED], 0, dict_idx, 1, 0);
+  g_signal_emit_by_name (pd, "items-changed",  dict_idx, 1, 0);
 }
 
 static void
 mutcb (struct dictionary *d, int idx, unsigned int what, const struct variable *oldvar, void *pd)
 {
   g_signal_emit (pd, signals [VARIABLE_CHANGED], 0, idx, what, oldvar);
-  g_signal_emit (pd, signals [ITEMS_CHANGED], 0, idx, 1, 1);
+  g_signal_emit_by_name (pd, "items-changed", idx, 1, 1);
 }
 
 static void
@@ -342,10 +312,12 @@ static const struct dict_callbacks gui_callbacks =
   };
 
 static void
-psppire_dict_init (PsppireDict *psppire_dict)
+psppire_dict_init (PsppireDict *d)
 {
-  psppire_dict->stamp = g_random_int ();
-  psppire_dict->disable_insert_signal = FALSE;
+  d->dispose_has_run = FALSE;
+
+  d->stamp = g_random_int ();
+  d->disable_insert_signal = FALSE;
 }
 
 /**
@@ -358,8 +330,8 @@ PsppireDict*
 psppire_dict_new_from_dict (struct dictionary *d)
 {
   PsppireDict *new_dict = g_object_new (PSPPIRE_TYPE_DICT, NULL);
-  new_dict->dict = d;
-  
+  new_dict->dict = dict_ref (d);
+
   dict_set_callbacks (new_dict->dict, &gui_callbacks, new_dict);
 
   return new_dict;
@@ -369,12 +341,15 @@ psppire_dict_new_from_dict (struct dictionary *d)
 void
 psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
 {
-  struct variable *var =  dict_get_weight (d);
+  const struct variable *var =  dict_get_weight (d);
 
-  guint old_n = dict_get_var_cnt (dict->dict);
-  guint new_n = dict_get_var_cnt (d);
-  
-  dict->dict = d;
+  struct dictionary *old_dict = dict->dict;
+
+  guint old_n = dict_get_n_vars (dict->dict);
+  guint new_n = dict_get_n_vars (d);
+
+  dict->dict = dict_ref (d);
+  dict_unref (old_dict);
 
   weight_changed_callback (d, var ? var_get_dict_index (var) : -1, dict);
 
@@ -385,7 +360,7 @@ psppire_dict_replace_dictionary (PsppireDict *dict, struct dictionary *d)
 
   dict_set_callbacks (dict->dict, &gui_callbacks, dict);
 
-  g_signal_emit (dict, signals [ITEMS_CHANGED], 0, 0, old_n, new_n);
+  g_signal_emit_by_name (dict, "items-changed", 0, old_n, new_n);
 }
 
 
@@ -405,7 +380,7 @@ psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
          - The string may not contain whitespace.
          - The first character may not be '$'
          - The first character may not be a digit
-         - The final charactor may not be '.' or '_'
+         - The final character may not be '.' or '_'
       */
       len = snprintf (name, size, _("Var%04d"), d);
       if (len + 1 >= size)
@@ -420,7 +395,8 @@ psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
 
 /* Insert a new variable at posn IDX, with the name NAME, and return the
    new variable.
-   If NAME is null, then a name will be automatically assigned.
+   IDX may take the special value -1, which will be treated the same as
+   zero.   If NAME is null, then a name will be automatically assigned.
 */
 struct variable *
 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
@@ -428,7 +404,8 @@ psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
   struct variable *var;
   char tmpname[64];
 
-  g_return_val_if_fail (idx >= 0, NULL);
+  if (idx == -1)    /* Note bug #56392. */
+    idx = 0;
   g_return_val_if_fail (d, NULL);
   g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
 
@@ -449,8 +426,8 @@ psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
   d->disable_insert_signal = FALSE;
 
   g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
-  g_signal_emit (d, signals [ITEMS_CHANGED], 0, idx, 0, 1);
-  
+  g_signal_emit_by_name (d, "items-changed", idx, 0, 1);
+
   return var;
 }
 
@@ -458,39 +435,32 @@ psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
 void
 psppire_dict_delete_variables (PsppireDict *d, gint first, gint n)
 {
-  gint idx;
   g_return_if_fail (d);
   g_return_if_fail (d->dict);
   g_return_if_fail (PSPPIRE_IS_DICT (d));
+  size_t varcnt = dict_get_n_vars (d->dict);
+  g_return_if_fail (first < varcnt);
+  g_return_if_fail (first >= 0);
+  g_return_if_fail (n > 0);
+  g_return_if_fail (first + n <= varcnt);
 
-  for (idx = 0 ; idx < n ; ++idx )
-    {
-      struct variable *var;
-
-      /* Do nothing if it's out of bounds */
-      if ( first >= dict_get_var_cnt (d->dict))
-       break;
-
-      var = dict_get_var (d->dict, first);
-      dict_delete_var (d->dict, var);
-    }
+  dict_delete_consecutive_vars (d->dict, first, n);
 }
 
 
 gboolean
 psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
 {
-  struct variable *var;
   g_assert (d);
   g_assert (PSPPIRE_IS_DICT (d));
 
-  if ( ! dict_id_is_valid (d->dict, name, false))
+  if (! dict_id_is_valid (d->dict, name, false))
     return FALSE;
 
-  if ( idx < dict_get_var_cnt (d->dict))
+  if (idx < dict_get_n_vars (d->dict))
     {
       /* This is an existing variable? */
-      var = dict_get_var (d->dict, idx);
+      struct variable * var = dict_get_var (d->dict, idx);
       dict_rename_var (d->dict, var, name);
     }
   else
@@ -513,7 +483,7 @@ psppire_dict_get_variable (const PsppireDict *d, gint idx)
   g_return_val_if_fail (d, NULL);
   g_return_val_if_fail (d->dict, NULL);
 
-  if ( dict_get_var_cnt (d->dict) <= idx )
+  if (dict_get_n_vars (d->dict) <= idx)
     return NULL;
 
   return dict_get_var (d->dict, idx);
@@ -522,18 +492,18 @@ psppire_dict_get_variable (const PsppireDict *d, gint idx)
 
 /* Return the number of variables in the dictionary */
 gint
-psppire_dict_get_var_cnt (const PsppireDict *d)
+psppire_dict_get_n_vars (const PsppireDict *d)
 {
   g_return_val_if_fail (d, -1);
   g_return_val_if_fail (d->dict, -1);
 
-  return dict_get_var_cnt (d->dict);
+  return dict_get_n_vars (d->dict);
 }
 
 
 /* Return the number of `union value's in the dictionary */
 size_t
-psppire_dict_get_value_cnt (const PsppireDict *d)
+psppire_dict_get_n_values (const PsppireDict *d)
 {
   g_return_val_if_fail (d, -1);
   g_return_val_if_fail (d->dict, -1);
@@ -578,7 +548,7 @@ psppire_dict_clear (PsppireDict *d)
 }
 
 
-/* Return true is NAME would be a valid name of a variable to add to the
+/* Return true if NAME would be a valid name of a variable to add to the
    dictionary.  False otherwise.
    If REPORT is true, then invalid names will be reported as such as errors
 */
@@ -586,12 +556,12 @@ gboolean
 psppire_dict_check_name (const PsppireDict *dict,
                         const gchar *name, gboolean report)
 {
-  if ( ! dict_id_is_valid (dict->dict, name, report ) )
+  if (! dict_id_is_valid (dict->dict, name, report))
     return FALSE;
 
   if (psppire_dict_lookup_var (dict, name))
     {
-      if ( report )
+      if (report)
        msg (ME, _("Duplicate variable name."));
       return FALSE;
     }
@@ -697,21 +667,21 @@ tree_model_column_type (GtkTreeModel *model, gint index)
   g_return_val_if_fail (PSPPIRE_IS_DICT (model), (GType) 0);
 
   GType t = 0;
-  
+
   switch (index)
     {
     case DICT_TVM_COL_NAME:
-      t = G_TYPE_STRING;
-      break;
-    case DICT_TVM_COL_VAR:
-      t = PSPPIRE_VAR_PTR_TYPE;
-      break;
     case DICT_TVM_COL_LABEL:
       t = G_TYPE_STRING;
       break;
+    case DICT_TVM_COL_DECIMAL:
+    case DICT_TVM_COL_WIDTH:
     case DICT_TVM_COL_COLUMNS:
       t = G_TYPE_INT;
       break;
+    case DICT_TVM_COL_VAR:
+      t = PSPPIRE_VAR_PTR_TYPE;
+      break;
     case DICT_TVM_COL_ALIGNMENT:
       t = align_enum_type;
       break;
@@ -744,7 +714,7 @@ tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
 
   n = indices [0];
 
-  if ( n < 0 || n >= psppire_dict_get_var_cnt (dict))
+  if (n < 0 || n >= psppire_dict_get_n_vars (dict))
     {
       iter->stamp = 0;
       iter->user_data = NULL;
@@ -769,16 +739,16 @@ tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
   struct variable *var;
   gint idx;
 
-  g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
-
-  if ( iter == NULL || iter->user_data == NULL)
+  if (iter == NULL || iter->user_data == NULL)
     return FALSE;
 
+  g_return_val_if_fail (iter->stamp == dict->stamp, FALSE);
+
   var = iter->user_data;
 
   idx = var_get_dict_index (var);
 
-  if ( idx + 1 >= psppire_dict_get_var_cnt (dict))
+  if (idx + 1 >= psppire_dict_get_n_vars (dict))
     {
       iter->user_data = NULL;
       iter->stamp = 0;
@@ -825,7 +795,7 @@ tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
   var = iter->user_data;
 
   const struct fmt_spec *fs = var_get_write_format (var);
-  
+
   switch (column)
     {
     case DICT_TVM_COL_NAME:
@@ -886,7 +856,7 @@ tree_model_n_children (GtkTreeModel *model,
   PsppireDict *dict = PSPPIRE_DICT (model);
 
   if (iter == NULL)
-    return psppire_dict_get_var_cnt (dict);
+    return psppire_dict_get_n_vars (dict);
 
   return 0;
 }
@@ -901,16 +871,16 @@ tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
 
   dict = PSPPIRE_DICT (model);
 
-  if ( parent )
+  if (parent)
     return FALSE;
 
-  if ( n >= psppire_dict_get_var_cnt (dict) )
+  if (n >= psppire_dict_get_n_vars (dict))
     return FALSE;
 
   iter->stamp = dict->stamp;
   iter->user_data = psppire_dict_get_variable (dict, n);
 
-  if ( !iter->user_data)
+  if (!iter->user_data)
     return FALSE;
 
   return TRUE;
@@ -921,11 +891,11 @@ gboolean
 psppire_dict_rename_var (PsppireDict *dict, struct variable *v,
                         const gchar *name)
 {
-  if ( ! dict_id_is_valid (dict->dict, name, false))
+  if (! dict_id_is_valid (dict->dict, name, false))
     return FALSE;
 
   /* Make sure no other variable has this name */
-  if ( NULL != psppire_dict_lookup_var (dict, name))
+  if (NULL != psppire_dict_lookup_var (dict, name))
     return FALSE;
 
   dict_rename_var (dict->dict, v, name);
@@ -940,31 +910,6 @@ psppire_dict_get_weight_variable (const PsppireDict *dict)
   return dict_get_weight (dict->dict);
 }
 
-
-
-#if DEBUGGING
-void
-psppire_dict_dump (const PsppireDict *dict)
-{
-  gint i;
-  const struct dictionary *d = dict->dict;
-
-  for (i = 0; i < dict_get_var_cnt (d); ++i)
-    {
-      const struct variable *v = psppire_dict_get_variable (dict, i);
-      int di = var_get_dict_index (v);
-      g_print ("`%s' idx=%d, fv=%d\n",
-              var_get_name(v),
-              di,
-              var_get_case_index(v));
-
-    }
-}
-#endif
-
-
-
-
 const gchar *
 psppire_dict_encoding (const PsppireDict *dict)
 {