PsppireValueEntry: New signal - edit-done
[pspp] / src / ui / gui / psppire-value-entry.c
index 5ce18414bbb29a6d4ef7c4cec14ba8d0e5b5d9e3..30b18219d785cd74de6c159352ca7ebcddd78004 100644 (file)
@@ -48,6 +48,12 @@ enum
     PROP_WIDTH
   };
 
+enum  {EDIT_DONE, /* Emitted when the entry has changed and is ready to be fetched */
+       n_SIGNALS};
+
+static guint signals [n_SIGNALS];
+
+
 static void
 psppire_value_entry_set_property (GObject      *object,
                                   guint         prop_id,
@@ -137,6 +143,12 @@ psppire_value_entry_text_changed (GtkEntryBuffer *buffer,
   obj->cur_value = NULL;
 }
 
+static void
+on_entry_activate (GtkWidget *w)
+{
+  PsppireValueEntry *ve = PSPPIRE_VALUE_ENTRY (w);
+  g_signal_emit (w, signals [EDIT_DONE], 0);
+}
 
 static void
 on_realize (GtkWidget *w)
@@ -145,13 +157,38 @@ on_realize (GtkWidget *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);
 
+  g_signal_connect_swapped (entry, "activate",
+                    G_CALLBACK (on_entry_activate), w);
+
+  gtk_widget_set_can_focus (GTK_WIDGET (entry), TRUE);
+
   GTK_WIDGET_CLASS (psppire_value_entry_parent_class)->realize (w);
 }
 
+
+/*
+ The "has-entry" property for the parent class (GTK_COMBO_BOX) is
+ a) Construct-only ; and b) defaults to FALSE.
+ We want it to default to TRUE.  So we override it here.
+*/
+static  GObject*
+my_constructor (GType                  type,
+               guint                  n_construct_properties,
+               GObjectConstructParam *construct_properties)
+{
+  GObject *o =
+    G_OBJECT_CLASS (psppire_value_entry_parent_class)->constructor
+    (type, n_construct_properties, construct_properties);
+
+  g_object_set (o, "has-entry", TRUE, NULL);
+
+  return o;
+}
+
 static void
 psppire_value_entry_class_init (PsppireValueEntryClass *class)
 {
@@ -162,6 +199,7 @@ psppire_value_entry_class_init (PsppireValueEntryClass *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;
+  gobject_class->constructor = my_constructor;
   widget_class->realize = on_realize;
 
   g_object_class_install_property (
@@ -220,6 +258,16 @@ psppire_value_entry_class_init (PsppireValueEntryClass *class)
                       0, MAX_STRING,
                       0,
                       G_PARAM_READABLE | G_PARAM_WRITABLE));
+
+  signals [EDIT_DONE] =
+    g_signal_new ("edit-done",
+                 G_TYPE_FROM_CLASS (class),
+                 G_SIGNAL_RUN_FIRST,
+                 0,
+                 NULL, NULL,
+                 g_cclosure_marshal_VOID__VOID,
+                 G_TYPE_NONE,
+                 0);
 }
 
 static void
@@ -246,7 +294,7 @@ psppire_value_entry_finalize (GObject *gobject)
 GtkWidget *
 psppire_value_entry_new (void)
 {
-  return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_VALUE_ENTRY, "has-entry", TRUE, NULL));
+  return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_VALUE_ENTRY, NULL));
 }
 
 static void
@@ -419,6 +467,10 @@ psppire_value_entry_set_value (PsppireValueEntry *obj,
   gchar *string;
 
   obj->cur_value = NULL;
+
+  if (value == NULL)
+    return;
+
   if (obj->show_value_label)
     {
       struct val_lab *vl = val_labs_lookup (obj->val_labs, value);