gui: Properly manage ref counts of combo box and tree view models.
[pspp] / src / ui / gui / psppire-dictview.c
index f63ea0ba6fe578367d492fc2ff724487695d9034..dc67b7c070fdb66811ffda29f9c7f40b6d0dc59b 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2009  Free Software Foundation
+   Copyright (C) 2009, 2010, 2011, 2012  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
@@ -16,7 +16,7 @@
 
 #include <config.h>
 
-#include <gtk/gtktreeview.h>
+#include <gtk/gtk.h>
 #include "psppire-dictview.h"
 #include "psppire-dict.h"
 #include "psppire-conf.h"
@@ -68,14 +68,13 @@ psppire_dict_view_finalize (GObject *object)
 {
   PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
 
-  g_object_unref (dict_view->menu);
+  gtk_widget_destroy (dict_view->menu);
 }
 
 /* Properties */
 enum
 {
   PROP_0,
-  PROP_MODEL,
   PROP_DICTIONARY,
   PROP_PREDICATE,
   PROP_SELECTION_MODE
@@ -106,7 +105,10 @@ set_model (PsppireDictView *dict_view)
 {
   GtkTreeModel *model ;
 
-  if ( dict_view->predicate )
+  if ( dict_view->dict == NULL)
+    return;
+
+   if ( dict_view->predicate )
     {
       model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict_view->dict),
                                         NULL);
@@ -122,6 +124,7 @@ set_model (PsppireDictView *dict_view)
     }
 
   gtk_tree_view_set_model (GTK_TREE_VIEW (dict_view), model);
+  g_object_unref (model);
 }
 
 static void
@@ -137,11 +140,6 @@ psppire_dict_view_set_property (GObject         *object,
     case PROP_DICTIONARY:
       dict_view->dict = g_value_get_object (value);
       break;
-    case PROP_MODEL:
-      g_critical ("Don't set the \"model\" property on %s. "
-                 "Use the \"dictionary\" property instead.",
-                 G_OBJECT_TYPE_NAME (dict_view));
-      break;
     case PROP_PREDICATE:
       dict_view->predicate = g_value_get_pointer (value);
       break;
@@ -201,45 +199,27 @@ psppire_dict_view_class_init (PsppireDictViewClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
 
-  GParamSpec *dictionary_spec =
-    g_param_spec_object ("dictionary",
-                        "Dictionary",
-                        _("The dictionary to be displayed by this widget"),
-                        PSPPIRE_TYPE_DICT,
-                        G_PARAM_READABLE | G_PARAM_WRITABLE);
-
   GParamSpec *predicate_spec =
     g_param_spec_pointer ("predicate",
                          "Predicate",
-                         _("A predicate function"),
+                         "A predicate function",
                          G_PARAM_READABLE | G_PARAM_WRITABLE);
 
 
   GParamSpec *selection_mode_spec =
     g_param_spec_enum ("selection-mode",
                       "Selection Mode",
-                      _("How many things can be selected"),
+                      "How many things can be selected",
                       GTK_TYPE_SELECTION_MODE,
                       GTK_SELECTION_MULTIPLE,
                       G_PARAM_CONSTRUCT | G_PARAM_READABLE | G_PARAM_WRITABLE);
 
-
-  GParamSpec *dummy_spec =
-    g_param_spec_pointer ("model",
-                         "Model",
-                         "Don't set the property",
-                         G_PARAM_WRITABLE);
-
   object_class->set_property = psppire_dict_view_set_property;
   object_class->get_property = psppire_dict_view_get_property;
 
-  g_object_class_install_property (object_class,
-                                   PROP_MODEL,
-                                   dummy_spec);
-
-  g_object_class_install_property (object_class,
-                                   PROP_DICTIONARY,
-                                   dictionary_spec);
+  g_object_class_override_property (object_class,
+                                   PROP_DICTIONARY,
+                                   "model");
 
   g_object_class_install_property (object_class,
                                    PROP_PREDICATE,
@@ -271,8 +251,7 @@ psppire_dict_view_base_finalize (PsppireDictViewClass *class,
 
 static void
 dv_get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
-               GtkTreeModel **model, GtkTreeIter *iter
-               )
+               GtkTreeModel **model, GtkTreeIter *iter)
 {
   *model = top_model;
 
@@ -336,23 +315,16 @@ var_description_cell_data_func (GtkTreeViewColumn *col,
 
   if ( var_has_label (var) && dv->prefer_labels)
     {
-      gchar *text = g_strdup_printf (
+      gchar *text = g_markup_printf_escaped (
                                     "<span stretch=\"condensed\">%s</span>",
                                     var_get_label (var));
 
-      char *utf8 = recode_string (UTF8, psppire_dict_encoding (dict),
-                                 text, -1);
-
+      g_object_set (cell, "markup", text, NULL);
       g_free (text);
-      g_object_set (cell, "markup", utf8, NULL);
-      g_free (utf8);
     }
   else
     {
-      char *name = recode_string (UTF8, psppire_dict_encoding (dict),
-                                 var_get_name (var), -1);
-      g_object_set (cell, "text", name, NULL);
-      g_free (name);
+      g_object_set (cell, "text", var_get_name (var), NULL);
     }
 }
 
@@ -376,7 +348,7 @@ var_icon_cell_data_func (GtkTreeViewColumn *col,
     }
   else
     {
-      const struct fmt_spec *fs = var_get_write_format (var);
+      const struct fmt_spec *fs = var_get_print_format (var);
       int cat = fmt_get_category (fs->type);
       switch ( var_get_measure (var))
        {
@@ -439,7 +411,7 @@ set_tooltip_for_variable (GtkTreeView  *treeview,
     return FALSE;
 
   {
-    gchar *tip ;
+    const gchar *tip ;
     GtkTreeModel *m;
     PsppireDict *dict;
 
@@ -447,15 +419,11 @@ set_tooltip_for_variable (GtkTreeView  *treeview,
     dict = PSPPIRE_DICT (m);
 
     if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
-      tip = recode_string (UTF8, psppire_dict_encoding (dict),
-                          var_get_name (var), -1);
+      tip = var_get_name (var);
     else
-      tip = recode_string (UTF8, psppire_dict_encoding (dict),
-                          var_get_label (var), -1);
+      tip = var_get_label (var);
 
     gtk_tooltip_set_text (tooltip, tip);
-
-    g_free (tip);
   }
 
   return TRUE;