Weight cases dialog: Convert from add hoc to PsppireDialogAction
[pspp] / src / ui / gui / psppire-value-entry.c
index 675df83966ef3aa7e49e27e644bfec7cec59b99f..731b6b3fb16e688a90738fda591ca4472a035d7c 100644 (file)
@@ -28,7 +28,7 @@ static void psppire_value_entry_finalize (GObject *);
 
 G_DEFINE_TYPE (PsppireValueEntry,
                psppire_value_entry,
-               GTK_TYPE_COMBO_BOX_ENTRY);
+               GTK_TYPE_COMBO_BOX);
 
 enum
   {
@@ -129,15 +129,42 @@ psppire_value_entry_get_property (GObject      *object,
     }
 }
 
+static void
+psppire_value_entry_text_changed (GtkEntryBuffer *buffer,
+                                  GParamSpec *pspec,
+                                  PsppireValueEntry *obj)
+{
+  obj->cur_value = NULL;
+}
+
+
+static void
+on_realize (GtkWidget *w)
+{
+  GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (w)));
+  GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
+
+  gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (w), COL_LABEL);
+  
+  g_signal_connect (buffer, "notify::text",
+                    G_CALLBACK (psppire_value_entry_text_changed), w);
+
+  gtk_widget_set_can_focus (GTK_WIDGET (entry), TRUE);
+
+  GTK_WIDGET_CLASS (psppire_value_entry_parent_class)->realize (w);
+}
+
 static void
 psppire_value_entry_class_init (PsppireValueEntryClass *class)
 {
-  GObjectClass *gobject_class;
-  gobject_class = G_OBJECT_CLASS (class);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+
 
   gobject_class->finalize = psppire_value_entry_finalize;
   gobject_class->set_property = psppire_value_entry_set_property;
   gobject_class->get_property = psppire_value_entry_get_property;
+  widget_class->realize = on_realize;
 
   g_object_class_install_property (
     gobject_class, PROP_SHOW_VALUE_LABEL,
@@ -197,28 +224,14 @@ psppire_value_entry_class_init (PsppireValueEntryClass *class)
                       G_PARAM_READABLE | G_PARAM_WRITABLE));
 }
 
-static void
-psppire_value_entry_text_changed (GtkEntryBuffer *buffer,
-                                  GParamSpec *pspec,
-                                  PsppireValueEntry *obj)
-{
-  obj->cur_value = NULL;
-}
-
 static void
 psppire_value_entry_init (PsppireValueEntry *obj)
 {
-  GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
-  GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
-
   obj->show_value_label = true;
   obj->val_labs = NULL;
   obj->format = F_8_0;
   obj->encoding = NULL;
   obj->cur_value = NULL;
-
-  g_signal_connect (buffer, "notify::text",
-                    G_CALLBACK (psppire_value_entry_text_changed), obj);
 }
 
 static void
@@ -235,14 +248,14 @@ psppire_value_entry_finalize (GObject *gobject)
 GtkWidget *
 psppire_value_entry_new (void)
 {
-  return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_VALUE_ENTRY, NULL));
+  return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_VALUE_ENTRY, "has-entry", TRUE, NULL));
 }
 
 static void
 psppire_value_entry_refresh_model (PsppireValueEntry *obj)
 {
-  GtkWidget *entry = gtk_bin_get_child (GTK_BIN (obj));
   GtkTreeModel *model;
+  GtkTreeModel *old_model;
 
   if (val_labs_count (obj->val_labs) > 0)
     {
@@ -270,9 +283,17 @@ psppire_value_entry_refresh_model (PsppireValueEntry *obj)
   else
     model = NULL;
 
+  old_model = gtk_combo_box_get_model (GTK_COMBO_BOX (obj));
+
+  if (old_model != model)
+    {
+      GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
+      gtk_entry_set_text (entry, "");
+    }
+
   gtk_combo_box_set_model (GTK_COMBO_BOX (obj), model);
-  gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (obj), COL_LABEL);
-  gtk_widget_set_sensitive (entry, model != NULL);
+  if (model != NULL)
+    g_object_unref (model);
 }
 
 void
@@ -310,9 +331,7 @@ void
 psppire_value_entry_set_value_labels (PsppireValueEntry *obj,
                                       const struct val_labs *val_labs)
 {
-  if (val_labs != NULL
-      ? obj->val_labs == NULL || !val_labs_equal (obj->val_labs, val_labs)
-      : obj->val_labs != NULL)
+  if (!val_labs_equal (obj->val_labs, val_labs))
     {
       obj->cur_value = NULL;