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,
52 psppire_value_entry_set_property (GObject *object,
57 PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (object);
61 case PROP_SHOW_VALUE_LABEL:
62 psppire_value_entry_set_show_value_label (obj,
63 g_value_get_boolean (value));
67 psppire_value_entry_set_variable (obj, g_value_get_pointer (value));
70 case PROP_VALUE_LABELS:
71 psppire_value_entry_set_value_labels (obj, g_value_get_pointer (value));
75 psppire_value_entry_set_format (obj, g_value_get_boxed (value));
79 psppire_value_entry_set_encoding (obj, g_value_get_string (value));
83 psppire_value_entry_set_width (obj, g_value_get_int (value));
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 psppire_value_entry_get_property (GObject *object,
98 PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (object);
102 case PROP_SHOW_VALUE_LABEL:
103 g_value_set_boolean (value,
104 psppire_value_entry_get_show_value_label (obj));
108 g_return_if_reached ();
110 case PROP_VALUE_LABELS:
111 g_value_set_pointer (value, obj->val_labs);
115 g_value_set_boxed (value, &obj->format);
119 g_value_set_string (value, psppire_value_entry_get_encoding (obj));
123 g_value_set_int (value, psppire_value_entry_get_width (obj));
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 psppire_value_entry_text_changed (GtkEntryBuffer *buffer,
135 PsppireValueEntry *obj)
137 obj->cur_value = NULL;
142 on_realize (GtkWidget *w)
144 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (w)));
145 GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
147 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (w), COL_LABEL);
149 g_signal_connect (buffer, "notify::text",
150 G_CALLBACK (psppire_value_entry_text_changed), w);
152 gtk_widget_set_can_focus (GTK_WIDGET (entry), TRUE);
154 GTK_WIDGET_CLASS (psppire_value_entry_parent_class)->realize (w);
158 psppire_value_entry_class_init (PsppireValueEntryClass *class)
160 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
161 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
164 gobject_class->finalize = psppire_value_entry_finalize;
165 gobject_class->set_property = psppire_value_entry_set_property;
166 gobject_class->get_property = psppire_value_entry_get_property;
167 widget_class->realize = on_realize;
169 g_object_class_install_property (
170 gobject_class, PROP_SHOW_VALUE_LABEL,
171 g_param_spec_boolean ("show-value-label",
173 "If true, a value that has a value label is shown "
174 "as the label. If false, all values are shown "
177 G_PARAM_WRITABLE | G_PARAM_READABLE));
179 g_object_class_install_property (
180 gobject_class, PROP_VARIABLE,
181 g_param_spec_pointer ("variable",
183 "Set to configure the PsppireValueEntry according "
184 "to the specified variable's value labels, format, "
185 "width, and encoding.",
188 g_object_class_install_property (
189 gobject_class, PROP_VALUE_LABELS,
190 g_param_spec_pointer ("value-labels",
192 "The set of value labels from which the user may "
193 "choose and which is used to display the value (if "
194 "value labels are to be displayed)",
195 G_PARAM_READABLE | G_PARAM_WRITABLE));
197 g_object_class_install_property (
198 gobject_class, PROP_FORMAT,
199 g_param_spec_boxed ("format",
201 "The format used to display values (that are not "
202 "displayed as value labels) and to interpret values "
205 G_PARAM_READABLE | G_PARAM_WRITABLE));
207 g_object_class_install_property (
208 gobject_class, PROP_ENCODING,
209 g_param_spec_string ("encoding",
211 "The encoding (e.g. \"UTF-8\") for string values. "
212 "For numeric values this setting has no effect.",
214 G_PARAM_READABLE | G_PARAM_WRITABLE));
216 g_object_class_install_property (
217 gobject_class, PROP_WIDTH,
218 g_param_spec_int ("width",
220 "Width of the value, either 0 for a numeric value or "
221 "a positive integer count of bytes for string values.",
224 G_PARAM_READABLE | G_PARAM_WRITABLE));
228 psppire_value_entry_init (PsppireValueEntry *obj)
230 obj->show_value_label = true;
231 obj->val_labs = NULL;
233 obj->encoding = NULL;
234 obj->cur_value = NULL;
238 psppire_value_entry_finalize (GObject *gobject)
240 PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (gobject);
242 val_labs_destroy (obj->val_labs);
243 g_free (obj->encoding);
245 G_OBJECT_CLASS (psppire_value_entry_parent_class)->finalize (gobject);
249 psppire_value_entry_new (void)
251 return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_VALUE_ENTRY, "has-entry", TRUE, NULL));
255 psppire_value_entry_refresh_model (PsppireValueEntry *obj)
258 GtkTreeModel *old_model;
260 if (val_labs_count (obj->val_labs) > 0)
262 const struct val_lab **vls = val_labs_sorted (obj->val_labs);
263 size_t n_vls = val_labs_count (obj->val_labs);
265 GtkListStore *list_store;
268 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
269 model = GTK_TREE_MODEL (list_store);
270 for (i = 0; i < n_vls; i++)
272 const struct val_lab *vl = vls[i];
275 gtk_list_store_append (list_store, &iter);
276 gtk_list_store_set (list_store, &iter,
277 COL_LABEL, val_lab_get_label (vl),
278 COL_VALUE, val_lab_get_value (vl),
286 old_model = gtk_combo_box_get_model (GTK_COMBO_BOX (obj));
288 if (old_model != model)
290 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
291 gtk_entry_set_text (entry, "");
294 gtk_combo_box_set_model (GTK_COMBO_BOX (obj), model);
296 g_object_unref (model);
300 psppire_value_entry_set_show_value_label (PsppireValueEntry *obj,
301 gboolean show_value_label)
303 if (obj->show_value_label != show_value_label)
305 obj->show_value_label = show_value_label;
306 g_object_notify (G_OBJECT (obj), "show-value-label");
311 psppire_value_entry_get_show_value_label (const PsppireValueEntry *obj)
313 return obj->show_value_label;
317 psppire_value_entry_set_variable (PsppireValueEntry *obj,
318 const struct variable *var)
322 psppire_value_entry_set_value_labels (obj, var_get_value_labels (var));
323 obj->format = *var_get_print_format (var);
324 psppire_value_entry_set_encoding (obj, var_get_encoding (var));
327 psppire_value_entry_set_value_labels (obj, NULL);
331 psppire_value_entry_set_value_labels (PsppireValueEntry *obj,
332 const struct val_labs *val_labs)
334 if (!val_labs_equal (obj->val_labs, val_labs))
336 obj->cur_value = NULL;
338 val_labs_destroy (obj->val_labs);
339 obj->val_labs = val_labs_clone (val_labs);
341 if (val_labs != NULL)
343 int width = val_labs_get_width (val_labs);
344 if (width != fmt_var_width (&obj->format))
345 obj->format = fmt_default_for_width (width);
348 psppire_value_entry_refresh_model (obj);
350 g_object_notify (G_OBJECT (obj), "value-labels");
354 const struct val_labs *
355 psppire_value_entry_get_value_labels (const PsppireValueEntry *obj)
357 return obj->val_labs;
361 psppire_value_entry_set_format (PsppireValueEntry *obj,
362 const struct fmt_spec *format)
364 if (!fmt_equal (format, &obj->format))
366 obj->cur_value = NULL;
367 obj->format = *format;
370 && val_labs_get_width (obj->val_labs) != fmt_var_width (format))
371 psppire_value_entry_set_value_labels (obj, NULL);
373 g_object_notify (G_OBJECT (obj), "format");
377 const struct fmt_spec *
378 psppire_value_entry_get_format (const PsppireValueEntry *obj)
384 psppire_value_entry_set_encoding (PsppireValueEntry *obj,
385 const gchar *encoding)
387 g_free (obj->encoding);
388 obj->encoding = encoding != NULL ? g_strdup (encoding) : NULL;
390 g_object_notify (G_OBJECT (obj), "encoding");
394 psppire_value_entry_get_encoding (const PsppireValueEntry *obj)
396 return obj->encoding ? obj->encoding : UTF8;
400 psppire_value_entry_set_width (PsppireValueEntry *obj, int width)
402 if (width != fmt_var_width (&obj->format))
404 struct fmt_spec format = fmt_default_for_width (width);
405 psppire_value_entry_set_format (obj, &format);
410 psppire_value_entry_get_width (const PsppireValueEntry *obj)
412 return fmt_var_width (&obj->format);
416 psppire_value_entry_set_value (PsppireValueEntry *obj,
417 const union value *value,
420 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
423 obj->cur_value = NULL;
424 if (obj->show_value_label)
426 struct val_lab *vl = val_labs_lookup (obj->val_labs, value);
429 gtk_entry_set_text (entry, val_lab_get_label (vl));
430 obj->cur_value = val_lab_get_value (vl);
435 string = value_to_text__ (*value, &obj->format, obj->encoding);
436 gtk_entry_set_text (entry, string);
441 psppire_value_entry_get_value (PsppireValueEntry *obj,
445 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
448 g_return_val_if_fail (fmt_var_width (&obj->format) == width, FALSE);
452 value_copy (value, obj->cur_value, width);
455 else if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (obj), &iter))
459 gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (obj)), &iter,
462 value_copy (value, v, width);
467 const gchar *new_text;
469 new_text = gtk_entry_get_text (entry);
470 return data_in_msg (ss_cstr (new_text), UTF8,
472 value, width, obj->encoding);