91dcdd47931e1c93e6ebdeee5fe0751f95f2c582
[pspp] / src / ui / gui / psppire-value-entry.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
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"
26
27 static void psppire_value_entry_finalize (GObject *);
28
29 G_DEFINE_TYPE (PsppireValueEntry,
30                psppire_value_entry,
31                GTK_TYPE_COMBO_BOX_ENTRY);
32
33 enum
34   {
35     COL_LABEL,                  /* Value label string. */
36     COL_VALUE,                  /* union value *. */
37     N_COLUMNS
38   };
39
40 enum
41   {
42     PROP_0,
43     PROP_SHOW_VALUE_LABEL,
44     PROP_VARIABLE,
45     PROP_VALUE_LABELS,
46     PROP_FORMAT,
47     PROP_ENCODING,
48     PROP_WIDTH
49   };
50
51 static void
52 psppire_value_entry_set_property (GObject      *object,
53                                   guint         prop_id,
54                                   const GValue *value,
55                                   GParamSpec   *pspec)
56 {
57   PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (object);
58
59   switch (prop_id)
60     {
61     case PROP_SHOW_VALUE_LABEL:
62       psppire_value_entry_set_show_value_label (obj,
63                                                 g_value_get_boolean (value));
64       break;
65
66     case PROP_VARIABLE:
67       psppire_value_entry_set_variable (obj, g_value_get_pointer (value));
68       break;
69
70     case PROP_VALUE_LABELS:
71       psppire_value_entry_set_value_labels (obj, g_value_get_pointer (value));
72       break;
73
74     case PROP_FORMAT:
75       psppire_value_entry_set_format (obj, g_value_get_boxed (value));
76       break;
77
78     case PROP_ENCODING:
79       psppire_value_entry_set_encoding (obj, g_value_get_string (value));
80       break;
81
82     case PROP_WIDTH:
83       psppire_value_entry_set_width (obj, g_value_get_int (value));
84       break;
85
86     default:
87       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88       break;
89     }
90 }
91
92 static void
93 psppire_value_entry_get_property (GObject      *object,
94                                   guint         prop_id,
95                                   GValue       *value,
96                                   GParamSpec   *pspec)
97 {
98   PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (object);
99
100   switch (prop_id)
101     {
102     case PROP_SHOW_VALUE_LABEL:
103       g_value_set_boolean (value,
104                            psppire_value_entry_get_show_value_label (obj));
105       break;
106
107     case PROP_VARIABLE:
108       g_return_if_reached ();
109
110     case PROP_VALUE_LABELS:
111       g_value_set_pointer (value, obj->val_labs);
112       break;
113
114     case PROP_FORMAT:
115       g_value_set_boxed (value, &obj->format);
116       break;
117
118     case PROP_ENCODING:
119       g_value_set_string (value, psppire_value_entry_get_encoding (obj));
120       break;
121
122     case PROP_WIDTH:
123       g_value_set_int (value, psppire_value_entry_get_width (obj));
124       break;
125
126     default:
127       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128       break;
129     }
130 }
131
132 static void
133 psppire_value_entry_class_init (PsppireValueEntryClass *class)
134 {
135   GObjectClass *gobject_class;
136   gobject_class = G_OBJECT_CLASS (class);
137
138   gobject_class->finalize = psppire_value_entry_finalize;
139   gobject_class->set_property = psppire_value_entry_set_property;
140   gobject_class->get_property = psppire_value_entry_get_property;
141
142   g_object_class_install_property (
143     gobject_class, PROP_SHOW_VALUE_LABEL,
144     g_param_spec_boolean ("show-value-label",
145                           "Show Value Label",
146                           "If true, a value that has a value label is shown "
147                           "as the label.  If false, all values are shown "
148                           "literally.",
149                           TRUE,
150                           G_PARAM_WRITABLE | G_PARAM_READABLE));
151
152   g_object_class_install_property (
153     gobject_class, PROP_VARIABLE,
154     g_param_spec_pointer ("variable",
155                           "Variable",
156                           "Set to configure the PsppireValueEntry according "
157                           "to the specified variable's value labels, format, "
158                           "width, and encoding.",
159                           G_PARAM_WRITABLE));
160
161   g_object_class_install_property (
162     gobject_class, PROP_VALUE_LABELS,
163     g_param_spec_pointer ("value-labels",
164                           "Value Labels",
165                           "The set of value labels from which the user may "
166                           "choose and which is used to display the value (if "
167                           "value labels are to be displayed)",
168                           G_PARAM_READABLE | G_PARAM_WRITABLE));
169
170   g_object_class_install_property (
171     gobject_class, PROP_FORMAT,
172     g_param_spec_boxed ("format",
173                         "Format",
174                         "The format used to display values (that are not "
175                         "displayed as value labels) and to interpret values "
176                         "entered.",
177                         PSPPIRE_TYPE_FORMAT,
178                         G_PARAM_READABLE | G_PARAM_WRITABLE));
179
180   g_object_class_install_property (
181     gobject_class, PROP_ENCODING,
182     g_param_spec_string ("encoding",
183                          "Encoding",
184                          "The encoding (e.g. \"UTF-8\") for string values.  "
185                          "For numeric values this setting has no effect.",
186                          "UTF-8",
187                          G_PARAM_READABLE | G_PARAM_WRITABLE));
188
189   g_object_class_install_property (
190     gobject_class, PROP_WIDTH,
191     g_param_spec_int ("width",
192                       "Width",
193                       "Width of the value, either 0 for a numeric value or "
194                       "a positive integer count of bytes for string values.",
195                       0, MAX_STRING,
196                       0,
197                       G_PARAM_READABLE | G_PARAM_WRITABLE));
198 }
199
200 static void
201 psppire_value_entry_text_changed (GtkEntryBuffer *buffer,
202                                   GParamSpec *pspec,
203                                   PsppireValueEntry *obj)
204 {
205   obj->cur_value = NULL;
206 }
207
208 static void
209 psppire_value_entry_init (PsppireValueEntry *obj)
210 {
211   GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
212   GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
213
214   obj->show_value_label = true;
215   obj->val_labs = NULL;
216   obj->format = F_8_0;
217   obj->encoding = NULL;
218   obj->cur_value = NULL;
219   gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (obj), COL_LABEL);
220
221   g_signal_connect (buffer, "notify::text",
222                     G_CALLBACK (psppire_value_entry_text_changed), obj);
223 }
224
225 static void
226 psppire_value_entry_finalize (GObject *gobject)
227 {
228   PsppireValueEntry *obj = PSPPIRE_VALUE_ENTRY (gobject);
229
230   val_labs_destroy (obj->val_labs);
231   g_free (obj->encoding);
232
233   G_OBJECT_CLASS (psppire_value_entry_parent_class)->finalize (gobject);
234 }
235
236 GtkWidget *
237 psppire_value_entry_new (void)
238 {
239   return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_VALUE_ENTRY, NULL));
240 }
241
242 static void
243 psppire_value_entry_refresh_model (PsppireValueEntry *obj)
244 {
245   GtkTreeModel *model;
246   GtkTreeModel *old_model;
247
248   if (val_labs_count (obj->val_labs) > 0)
249     {
250       const struct val_lab **vls = val_labs_sorted (obj->val_labs);
251       size_t n_vls = val_labs_count (obj->val_labs);
252
253       GtkListStore *list_store;
254       size_t i;
255
256       list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
257       model = GTK_TREE_MODEL (list_store);
258       for (i = 0; i < n_vls; i++)
259         {
260           const struct val_lab *vl = vls[i];
261           GtkTreeIter iter;
262
263           gtk_list_store_append (list_store, &iter);
264           gtk_list_store_set (list_store, &iter,
265                               COL_LABEL, val_lab_get_label (vl),
266                               COL_VALUE, val_lab_get_value (vl),
267                               -1);
268         }
269       free (vls);
270     }
271   else
272     model = NULL;
273
274   old_model = gtk_combo_box_get_model (GTK_COMBO_BOX (obj));
275
276   if (old_model != model)
277     {
278       GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
279       gtk_entry_set_text (entry, "");
280     }
281
282   gtk_combo_box_set_model (GTK_COMBO_BOX (obj), model);
283   if ((model != NULL) && (model != old_model))
284     g_object_unref (model);
285 }
286
287 void
288 psppire_value_entry_set_show_value_label (PsppireValueEntry *obj,
289                                           gboolean show_value_label)
290 {
291   if (obj->show_value_label != show_value_label)
292     {
293       obj->show_value_label = show_value_label;
294       g_object_notify (G_OBJECT (obj), "show-value-label");
295     }
296 }
297
298 gboolean
299 psppire_value_entry_get_show_value_label (const PsppireValueEntry *obj)
300 {
301   return obj->show_value_label;
302 }
303
304 void
305 psppire_value_entry_set_variable (PsppireValueEntry *obj,
306                                   const struct variable *var)
307 {
308   if (var != NULL)
309     {
310       psppire_value_entry_set_value_labels (obj, var_get_value_labels (var));
311       obj->format = *var_get_print_format (var);
312       psppire_value_entry_set_encoding (obj, var_get_encoding (var));
313     }
314   else
315     psppire_value_entry_set_value_labels (obj, NULL);
316 }
317
318 void
319 psppire_value_entry_set_value_labels (PsppireValueEntry *obj,
320                                       const struct val_labs *val_labs)
321 {
322   if (!val_labs_equal (obj->val_labs, val_labs))
323     {
324       obj->cur_value = NULL;
325
326       val_labs_destroy (obj->val_labs);
327       obj->val_labs = val_labs_clone (val_labs);
328
329       if (val_labs != NULL)
330         {
331           int width = val_labs_get_width (val_labs);
332           if (width != fmt_var_width (&obj->format))
333             obj->format = fmt_default_for_width (width);
334         }
335
336       psppire_value_entry_refresh_model (obj);
337
338       g_object_notify (G_OBJECT (obj), "value-labels");
339     }
340 }
341
342 const struct val_labs *
343 psppire_value_entry_get_value_labels (const PsppireValueEntry *obj)
344 {
345   return obj->val_labs;
346 }
347
348 void
349 psppire_value_entry_set_format (PsppireValueEntry *obj,
350                                 const struct fmt_spec *format)
351 {
352   if (!fmt_equal (format, &obj->format))
353     {
354       obj->cur_value = NULL;
355       obj->format = *format;
356
357       if (obj->val_labs
358           && val_labs_get_width (obj->val_labs) != fmt_var_width (format))
359         psppire_value_entry_set_value_labels (obj, NULL);
360
361       g_object_notify (G_OBJECT (obj), "format");
362     }
363 }
364
365 const struct fmt_spec *
366 psppire_value_entry_get_format (const PsppireValueEntry *obj)
367 {
368   return &obj->format;
369 }
370
371 void
372 psppire_value_entry_set_encoding (PsppireValueEntry *obj,
373                                   const gchar *encoding)
374 {
375   g_free (obj->encoding);
376   obj->encoding = encoding != NULL ? g_strdup (encoding) : NULL;
377
378   g_object_notify (G_OBJECT (obj), "encoding");
379 }
380
381 const gchar *
382 psppire_value_entry_get_encoding (const PsppireValueEntry *obj)
383 {
384   return obj->encoding ? obj->encoding : UTF8;
385 }
386
387 void
388 psppire_value_entry_set_width (PsppireValueEntry *obj, int width)
389 {
390   if (width != fmt_var_width (&obj->format))
391     {
392       struct fmt_spec format = fmt_default_for_width (width);
393       psppire_value_entry_set_format (obj, &format);
394     }
395 }
396
397 int
398 psppire_value_entry_get_width (const PsppireValueEntry *obj)
399 {
400   return fmt_var_width (&obj->format);
401 }
402
403 void
404 psppire_value_entry_set_value (PsppireValueEntry *obj,
405                                const union value *value,
406                                int width)
407 {
408   GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
409   gchar *string;
410
411   obj->cur_value = NULL;
412   if (obj->show_value_label)
413     {
414       struct val_lab *vl = val_labs_lookup (obj->val_labs, value);
415       if (vl != NULL)
416         {
417           gtk_entry_set_text (entry, val_lab_get_label (vl));
418           obj->cur_value = val_lab_get_value (vl);
419           return;
420         }
421     }
422
423   string = value_to_text__ (*value, &obj->format, obj->encoding);
424   gtk_entry_set_text (entry, string);
425   g_free (string);
426 }
427
428 gboolean
429 psppire_value_entry_get_value (PsppireValueEntry *obj,
430                                union value *value,
431                                int width)
432 {
433   GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (obj)));
434   GtkTreeIter iter;
435
436   g_return_val_if_fail (fmt_var_width (&obj->format) == width, FALSE);
437
438   if (obj->cur_value)
439     {
440       value_copy (value, obj->cur_value, width);
441       return TRUE;
442     }
443   else if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (obj), &iter))
444     {
445       union value *v;
446
447       gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (obj)), &iter,
448                           COL_VALUE, &v,
449                           -1);
450       value_copy (value, v, width);
451       return TRUE;
452     }
453   else
454     {
455       const gchar *new_text;
456
457       new_text = gtk_entry_get_text (entry);
458       return data_in_msg (ss_cstr (new_text), UTF8,
459                           obj->format.type,
460                           value, width, obj->encoding);
461     }
462 }