1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "psppire-value-entry.h"
19 #include "data/data-in.h"
20 #include "data/value-labels.h"
21 #include "data/variable.h"
22 #include "libpspp/cast.h"
23 #include "libpspp/i18n.h"
24 #include "ui/gui/helper.h"
25 #include "ui/gui/psppire-format.h"
27 static void psppire_value_entry_finalize (GObject *);
29 G_DEFINE_TYPE (PsppireValueEntry,
35 COL_LABEL, /* Value label string. */
36 COL_VALUE, /* union value *. */
43 PROP_SHOW_VALUE_LABEL,
51 enum {EDIT_DONE, /* Emitted when the entry has changed and is ready to be fetched */
54 static guint signals [n_SIGNALS];
58 psppire_value_entry_set_property (GObject *object,
63 PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (object);
67 case PROP_SHOW_VALUE_LABEL:
68 psppire_value_entry_set_show_value_label (obj,
69 g_value_get_boolean (value));
73 psppire_value_entry_set_variable (obj, g_value_get_pointer (value));
76 case PROP_VALUE_LABELS:
77 psppire_value_entry_set_value_labels (obj, g_value_get_pointer (value));
81 psppire_value_entry_set_format (obj, g_value_get_boxed (value));
85 psppire_value_entry_set_encoding (obj, g_value_get_string (value));
89 psppire_value_entry_set_width (obj, g_value_get_int (value));
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99 psppire_value_entry_get_property (GObject *object,
104 PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (object);
108 case PROP_SHOW_VALUE_LABEL:
109 g_value_set_boolean (value,
110 psppire_value_entry_get_show_value_label (obj));
114 g_return_if_reached ();
116 case PROP_VALUE_LABELS:
117 g_value_set_pointer (value, obj->val_labs);
121 g_value_set_boxed (value, &obj->format);
125 g_value_set_string (value, psppire_value_entry_get_encoding (obj));
129 g_value_set_int (value, psppire_value_entry_get_width (obj));
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 psppire_value_entry_text_changed (GtkEntryBuffer *buffer,
141 PsppireValueEntry *obj)
143 obj->cur_value = NULL;
147 on_entry_activate (GtkWidget *w)
149 g_signal_emit (w, signals [EDIT_DONE], 0);
153 on_realize (GtkWidget *w)
155 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (w)));
156 GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
158 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (w), COL_LABEL);
160 g_signal_connect (buffer, "notify::text",
161 G_CALLBACK (psppire_value_entry_text_changed), w);
163 g_signal_connect_swapped (entry, "activate",
164 G_CALLBACK (on_entry_activate), w);
166 gtk_widget_set_can_focus (GTK_WIDGET (entry), TRUE);
168 GTK_WIDGET_CLASS (psppire_value_entry_parent_class)->realize (w);
173 The "has-entry" property for the parent class (GTK_COMBO_BOX) is
174 a) Construct-only ; and b) defaults to FALSE.
175 We want it to default to TRUE. So we override it here.
178 my_constructor (GType type,
179 guint n_construct_properties,
180 GObjectConstructParam *construct_properties)
183 G_OBJECT_CLASS (psppire_value_entry_parent_class)->constructor
184 (type, n_construct_properties, construct_properties);
186 g_object_set (o, "has-entry", TRUE, NULL);
192 psppire_value_entry_class_init (PsppireValueEntryClass *class)
194 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
195 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
198 gobject_class->finalize = psppire_value_entry_finalize;
199 gobject_class->set_property = psppire_value_entry_set_property;
200 gobject_class->get_property = psppire_value_entry_get_property;
201 gobject_class->constructor = my_constructor;
202 widget_class->realize = on_realize;
204 g_object_class_install_property (
205 gobject_class, PROP_SHOW_VALUE_LABEL,
206 g_param_spec_boolean ("show-value-label",
208 "If true, a value that has a value label is shown "
209 "as the label. If false, all values are shown "
212 G_PARAM_WRITABLE | G_PARAM_READABLE));
214 g_object_class_install_property (
215 gobject_class, PROP_VARIABLE,
216 g_param_spec_pointer ("variable",
218 "Set to configure the PsppireValueEntry according "
219 "to the specified variable's value labels, format, "
220 "width, and encoding.",
223 g_object_class_install_property (
224 gobject_class, PROP_VALUE_LABELS,
225 g_param_spec_pointer ("value-labels",
227 "The set of value labels from which the user may "
228 "choose and which is used to display the value (if "
229 "value labels are to be displayed)",
230 G_PARAM_READABLE | G_PARAM_WRITABLE));
232 g_object_class_install_property (
233 gobject_class, PROP_FORMAT,
234 g_param_spec_boxed ("format",
236 "The format used to display values (that are not "
237 "displayed as value labels) and to interpret values "
240 G_PARAM_READABLE | G_PARAM_WRITABLE));
242 g_object_class_install_property (
243 gobject_class, PROP_ENCODING,
244 g_param_spec_string ("encoding",
246 "The encoding (e.g. \"UTF-8\") for string values. "
247 "For numeric values this setting has no effect.",
249 G_PARAM_READABLE | G_PARAM_WRITABLE));
251 g_object_class_install_property (
252 gobject_class, PROP_WIDTH,
253 g_param_spec_int ("width",
255 "Width of the value, either 0 for a numeric value or "
256 "a positive integer count of bytes for string values.",
259 G_PARAM_READABLE | G_PARAM_WRITABLE));
261 signals [EDIT_DONE] =
262 g_signal_new ("edit-done",
263 G_TYPE_FROM_CLASS (class),
267 g_cclosure_marshal_VOID__VOID,
273 psppire_value_entry_init (PsppireValueEntry *obj)
275 obj->show_value_label = true;
276 obj->val_labs = NULL;
278 obj->encoding = NULL;
279 obj->cur_value = NULL;
283 psppire_value_entry_finalize (GObject *gobject)
285 PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (gobject);
287 val_labs_destroy (obj->val_labs);
288 g_free (obj->encoding);
290 G_OBJECT_CLASS (psppire_value_entry_parent_class)->finalize (gobject);
294 psppire_value_entry_new (void)
296 return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_VALUE_ENTRY, NULL));
300 psppire_value_entry_refresh_model (PsppireValueEntry *obj)
303 GtkTreeModel *old_model;
305 if (val_labs_count (obj->val_labs) > 0)
307 const struct val_lab **vls = val_labs_sorted (obj->val_labs);
308 size_t n_vls = val_labs_count (obj->val_labs);
310 GtkListStore *list_store;
313 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
314 model = GTK_TREE_MODEL (list_store);
315 for (i = 0; i < n_vls; i++)
317 const struct val_lab *vl = vls[i];
320 gtk_list_store_append (list_store, &iter);
321 gtk_list_store_set (list_store, &iter,
322 COL_LABEL, val_lab_get_label (vl),
323 COL_VALUE, val_lab_get_value (vl),
331 old_model = gtk_combo_box_get_model (GTK_COMBO_BOX (obj));
333 if (old_model != model)
335 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
336 gtk_entry_set_text (entry, "");
339 gtk_combo_box_set_model (GTK_COMBO_BOX (obj), model);
341 g_object_unref (model);
345 psppire_value_entry_set_show_value_label (PsppireValueEntry *obj,
346 gboolean show_value_label)
348 if (obj->show_value_label != show_value_label)
350 obj->show_value_label = show_value_label;
351 g_object_notify (G_OBJECT (obj), "show-value-label");
356 psppire_value_entry_get_show_value_label (const PsppireValueEntry *obj)
358 return obj->show_value_label;
362 psppire_value_entry_set_variable (PsppireValueEntry *obj,
363 const struct variable *var)
367 psppire_value_entry_set_value_labels (obj, var_get_value_labels (var));
368 obj->format = *var_get_print_format (var);
369 psppire_value_entry_set_encoding (obj, var_get_encoding (var));
372 psppire_value_entry_set_value_labels (obj, NULL);
376 psppire_value_entry_set_value_labels (PsppireValueEntry *obj,
377 const struct val_labs *val_labs)
379 if (!val_labs_equal (obj->val_labs, val_labs))
381 obj->cur_value = NULL;
383 val_labs_destroy (obj->val_labs);
384 obj->val_labs = val_labs_clone (val_labs);
386 if (val_labs != NULL)
388 int width = val_labs_get_width (val_labs);
389 if (width != fmt_var_width (&obj->format))
390 obj->format = fmt_default_for_width (width);
393 psppire_value_entry_refresh_model (obj);
395 g_object_notify (G_OBJECT (obj), "value-labels");
399 const struct val_labs *
400 psppire_value_entry_get_value_labels (const PsppireValueEntry *obj)
402 return obj->val_labs;
406 psppire_value_entry_set_format (PsppireValueEntry *obj,
407 const struct fmt_spec *format)
409 if (!fmt_equal (format, &obj->format))
411 obj->cur_value = NULL;
412 obj->format = *format;
415 && val_labs_get_width (obj->val_labs) != fmt_var_width (format))
416 psppire_value_entry_set_value_labels (obj, NULL);
418 g_object_notify (G_OBJECT (obj), "format");
422 const struct fmt_spec *
423 psppire_value_entry_get_format (const PsppireValueEntry *obj)
429 psppire_value_entry_set_encoding (PsppireValueEntry *obj,
430 const gchar *encoding)
432 g_free (obj->encoding);
433 obj->encoding = encoding != NULL ? g_strdup (encoding) : NULL;
435 g_object_notify (G_OBJECT (obj), "encoding");
439 psppire_value_entry_get_encoding (const PsppireValueEntry *obj)
441 return obj->encoding ? obj->encoding : UTF8;
445 psppire_value_entry_set_width (PsppireValueEntry *obj, int width)
447 if (width != fmt_var_width (&obj->format))
449 struct fmt_spec format = fmt_default_for_width (width);
450 psppire_value_entry_set_format (obj, &format);
455 psppire_value_entry_get_width (const PsppireValueEntry *obj)
457 return fmt_var_width (&obj->format);
461 psppire_value_entry_set_value (PsppireValueEntry *obj,
462 const union value *value,
465 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
468 obj->cur_value = NULL;
473 if (obj->show_value_label)
475 struct val_lab *vl = val_labs_lookup (obj->val_labs, value);
478 gtk_entry_set_text (entry, val_lab_get_label (vl));
479 obj->cur_value = val_lab_get_value (vl);
484 string = value_to_text__ (*value, &obj->format, obj->encoding);
485 gtk_entry_set_text (entry, string);
490 psppire_value_entry_get_value (PsppireValueEntry *obj,
494 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
497 g_return_val_if_fail (fmt_var_width (&obj->format) == width, FALSE);
501 value_copy (value, obj->cur_value, width);
504 else if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (obj), &iter))
508 gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (obj)), &iter,
511 value_copy (value, v, width);
516 const gchar *new_text;
518 new_text = gtk_entry_get_text (entry);
519 return data_in_msg (ss_cstr (new_text), UTF8,
521 value, width, obj->encoding);