1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011, 2014, 2022 Free Software Foundation
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/>. */
21 #include "dialog-common.h"
22 #include "psppire-val-chooser.h"
24 #include "libpspp/str.h"
25 #include "ui/gui/glibfix.h"
27 #include "ui/syntax-gen.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
33 static void psppire_val_chooser_class_init (PsppireValChooserClass *class);
34 static void psppire_val_chooser_init (PsppireValChooser *vc);
36 static void psppire_val_chooser_realize (GtkWidget *w);
38 G_DEFINE_TYPE (PsppireValChooser, psppire_val_chooser, GTK_TYPE_FRAME)
41 psppire_val_chooser_finalize (GObject *object)
67 psppire_val_chooser_set_property (GObject *object,
72 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (object);
78 gboolean x = g_value_get_boolean (value);
79 gtk_widget_set_visible (GTK_WIDGET (vr->rw[VC_ELSE].rb), x);
80 gtk_widget_set_visible (GTK_WIDGET (vr->rw[VC_ELSE].label), x);
84 vr->input_var_is_string = g_value_get_boolean (value);
85 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_SYSMIS].rb), !vr->input_var_is_string);
86 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_MISSING].rb), !vr->input_var_is_string);
87 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_RANGE].rb), !vr->input_var_is_string);
88 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_LOW_UP].rb), !vr->input_var_is_string);
89 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_HIGH_DOWN].rb), !vr->input_var_is_string);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99 psppire_val_chooser_get_property (GObject *object,
104 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (object);
111 gtk_widget_get_visible (GTK_WIDGET (vr->rw[VC_ELSE].rb));
112 g_value_set_boolean (value, x);
116 g_value_set_boolean (value, vr->input_var_is_string);
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125 static GObjectClass * parent_class = NULL;
128 psppire_val_chooser_class_init (PsppireValChooserClass *class)
130 GObjectClass *object_class = G_OBJECT_CLASS (class);
132 object_class->finalize = psppire_val_chooser_finalize;
133 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
135 GParamSpec *is_string_spec =
136 g_param_spec_boolean ("is-string",
138 "Should the value range be a string value",
142 GParamSpec *show_else_spec =
143 g_param_spec_boolean ("show-else",
145 "Should the \"All other values\" item be visible",
150 parent_class = g_type_class_peek_parent (class);
152 object_class->set_property = psppire_val_chooser_set_property;
153 object_class->get_property = psppire_val_chooser_get_property;
155 widget_class->realize = psppire_val_chooser_realize;
157 g_object_class_install_property (object_class,
161 g_object_class_install_property (object_class,
167 /* Set the focus of B to follow the sensitivity of A */
169 focus_follows_sensitivity (GtkWidget *a, GParamSpec *pspec, GtkWidget *b)
171 gboolean sens = gtk_widget_get_sensitive (a);
173 g_object_set (b, "has-focus", sens, NULL);
178 typedef GtkWidget *filler_f (struct layout *, struct range_widgets *);
179 typedef void set_f (PsppireValChooser *, struct old_value *, const struct range_widgets *);
190 static void simple_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
192 const gchar *text = gtk_entry_get_text (rw->e1);
194 if (vr->input_var_is_string)
196 ov->type = OV_STRING;
197 ov->v.s = g_strdup (text);
201 ov->type = OV_NUMERIC;
202 ov->v.v = g_strtod (text, 0);
206 static void lo_up_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
208 const gchar *text = gtk_entry_get_text (rw->e1);
210 ov->type = OV_LOW_UP;
211 ov->v.range[1] = g_strtod (text, 0);
215 static void hi_down_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
217 const gchar *text = gtk_entry_get_text (rw->e1);
219 ov->type = OV_HIGH_DOWN;
220 ov->v.range[0] = g_strtod (text, 0);
223 static void missing_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
225 ov->type = OV_MISSING;
229 static void sysmis_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
231 ov->type = OV_SYSMIS;
234 static void else_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
240 static void range_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
242 const gchar *text = gtk_entry_get_text (rw->e1);
245 ov->v.range[0] = g_strtod (text, 0);
247 text = gtk_entry_get_text (rw->e2);
248 ov->v.range[1] = g_strtod (text, 0);
251 static GtkWidget * range_entry (struct layout *l, struct range_widgets *rw)
253 GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 3);
254 GtkWidget *entrylo = gtk_entry_new ();
255 GtkWidget *label = gtk_label_new (_("through"));
256 GtkWidget *entryhi = gtk_entry_new ();
258 rw->e1 = GTK_ENTRY (entrylo);
259 rw->e2 = GTK_ENTRY (entryhi);
261 g_object_set (G_OBJECT (label),
262 "valign", GTK_ALIGN_CENTER,
263 "halign", GTK_ALIGN_START,
267 g_signal_connect (vbox, "notify::sensitive", G_CALLBACK (focus_follows_sensitivity), entrylo);
269 gtk_box_pack_start (GTK_BOX (vbox), entrylo, TRUE, TRUE, 0);
270 gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
271 gtk_box_pack_start (GTK_BOX (vbox), entryhi, TRUE, TRUE, 0);
275 static GtkWidget * simple_entry (struct layout *l, struct range_widgets *rw)
277 GtkWidget *entry = gtk_entry_new ();
279 rw->e1 = GTK_ENTRY (entry);
281 g_signal_connect (entry, "notify::sensitive", G_CALLBACK (focus_follows_sensitivity), entry);
286 static struct layout range_opt[n_VAL_CHOOSER_BUTTONS]=
288 {N_("_Value:"), simple_entry, simple_set },
289 {N_("_System Missing"), NULL, sysmis_set },
290 {N_("System _or User Missing"), NULL, missing_set},
291 {N_("_Range:"), range_entry, range_set },
292 {N_("Range, _LOWEST thru value"), simple_entry, lo_up_set },
293 {N_("Range, value thru _HIGHEST"), simple_entry, hi_down_set},
294 {N_("_All other values"), NULL, else_set }
298 psppire_val_chooser_init (PsppireValChooser *vr)
301 GtkWidget *grid = gtk_grid_new ();
302 GSList *group = NULL;
305 g_object_set (G_OBJECT (grid),
310 vr->input_var_is_string = FALSE;
312 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
314 struct layout *l = &range_opt[i];
315 vr->rw[i].label = GTK_LABEL (gtk_label_new (gettext (l->label)));
316 gtk_label_set_use_underline (vr->rw[i].label, TRUE);
317 vr->rw[i].rb = GTK_TOGGLE_BUTTON (gtk_radio_button_new (group));
318 gtk_label_set_mnemonic_widget (vr->rw[i].label, GTK_WIDGET (vr->rw[i].rb));
320 g_object_set (G_OBJECT (vr->rw[i].label),
321 "valign", GTK_ALIGN_CENTER,
322 "halign", GTK_ALIGN_START,
325 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (vr->rw[i].rb));
327 /* Attach the buttons */
328 gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET (vr->rw[i].rb),
331 gtk_widget_set_hexpand (GTK_WIDGET (vr->rw[i].rb), FALSE);
333 /* Attach the labels */
334 gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET (vr->rw[i].label),
337 gtk_widget_set_hexpand (GTK_WIDGET (vr->rw[i].label), TRUE);
344 GtkWidget *fill = l->fill (l, &vr->rw[i]);
346 gtk_widget_set_sensitive (fill, FALSE);
348 gtk_grid_attach (GTK_GRID (grid), fill, 1, row, 1, 1);
350 gtk_widget_set_hexpand (fill, TRUE);
354 g_signal_connect (vr->rw[i].rb, "toggled", G_CALLBACK (set_sensitivity_from_toggle), fill);
358 gtk_frame_set_shadow_type (GTK_FRAME (vr), GTK_SHADOW_ETCHED_IN);
360 gtk_container_add (GTK_CONTAINER (vr), grid);
362 gtk_widget_show_all (grid);
366 psppire_val_chooser_realize (GtkWidget *w)
368 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (w);
370 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(vr->rw[0].rb), TRUE);
371 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (vr->rw[0].rb));
373 /* Chain up to the parent class */
374 GTK_WIDGET_CLASS (parent_class)->realize (w);
380 /* A boxed type representing a value, or a range of values which may
381 potentially be replaced by something */
384 static struct old_value *
385 old_value_copy (struct old_value *ov)
387 struct old_value *copy = g_memdup2 (ov, sizeof (*copy));
389 if (ov->type == OV_STRING)
390 copy->v.s = g_strdup (ov->v.s);
397 old_value_free (struct old_value *ov)
399 if (ov->type == OV_STRING)
405 old_value_to_string (const GValue *src, GValue *dest)
407 const struct old_value *ov = g_value_get_boxed (src);
413 gchar *text = g_strdup_printf ("%.*g", DBL_DIG + 1, ov->v.v);
414 g_value_set_string (dest, text);
419 g_value_set_string (dest, ov->v.s);
422 g_value_set_string (dest, "MISSING");
425 g_value_set_string (dest, "SYSMIS");
428 g_value_set_string (dest, "ELSE");
433 char en_dash[6] = {0,0,0,0,0,0};
435 g_unichar_to_utf8 (0x2013, en_dash);
437 text = g_strdup_printf ("%.*g %s %.*g",
438 DBL_DIG + 1, ov->v.range[0],
440 DBL_DIG + 1, ov->v.range[1]);
441 g_value_set_string (dest, text);
448 char en_dash[6] = {0,0,0,0,0,0};
450 g_unichar_to_utf8 (0x2013, en_dash);
452 text = g_strdup_printf ("LOWEST %s %.*g",
454 DBL_DIG + 1, ov->v.range[1]);
456 g_value_set_string (dest, text);
463 char en_dash[6] = {0,0,0,0,0,0};
465 g_unichar_to_utf8 (0x2013, en_dash);
467 text = g_strdup_printf ("%.*g %s HIGHEST",
468 DBL_DIG + 1, ov->v.range[0],
471 g_value_set_string (dest, text);
476 g_warning ("Invalid type in old recode value");
477 g_value_set_string (dest, "???");
483 old_value_get_type (void)
489 t = g_boxed_type_register_static ("psppire-recode-old-values",
490 (GBoxedCopyFunc) old_value_copy,
491 (GBoxedFreeFunc) old_value_free);
493 g_value_register_transform_func (t, G_TYPE_STRING,
494 old_value_to_string);
502 /* Generate a syntax fragment for NV and append it to STR */
504 old_value_append_syntax (struct string *str, const struct old_value *ov)
509 ds_put_c_format (str, "%.*g", DBL_DIG + 1, ov->v.v);
513 struct string ds = DS_EMPTY_INITIALIZER;
514 syntax_gen_string (&ds, ss_cstr (ov->v.s));
515 ds_put_cstr (str, ds_cstr (&ds));
520 ds_put_cstr (str, "MISSING");
523 ds_put_cstr (str, "SYSMIS");
526 ds_put_cstr (str, "ELSE");
529 ds_put_c_format (str, "%.*g THRU %.*g",
530 DBL_DIG + 1, ov->v.range[0],
531 DBL_DIG + 1, ov->v.range[1]);
534 ds_put_c_format (str, "LOWEST THRU %.*g",
535 DBL_DIG + 1, ov->v.range[1]);
538 ds_put_c_format (str, "%.*g THRU HIGHEST",
539 DBL_DIG + 1, ov->v.range[0]);
542 g_warning ("Invalid type in old recode value");
543 ds_put_cstr (str, "???");
550 /* Set OV according to the current state of VR */
552 psppire_val_chooser_get_status (PsppireValChooser *vr, struct old_value *ov)
556 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
558 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (vr->rw[i].rb)))
560 range_opt[i].set (vr, ov, &vr->rw[i]);
566 /* This might need to be changed to something less naive.
567 In particular, what happends with dates, etc?
570 num_to_string (gdouble x)
572 return g_strdup_printf ("%.*g", DBL_DIG + 1, x);
576 /* Set VR according to the value of OV */
578 psppire_val_chooser_set_status (PsppireValChooser *vr, const struct old_value *ov)
584 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
587 gtk_entry_set_text (vr->rw[i].e1, "");
590 gtk_entry_set_text (vr->rw[i].e2, "");
596 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
597 gtk_entry_set_text (vr->rw[0].e1, ov->v.s);
603 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
605 str = num_to_string (ov->v.v);
607 gtk_entry_set_text (vr->rw[0].e1, str);
613 gtk_toggle_button_set_active (vr->rw[VC_SYSMIS].rb, TRUE);
617 gtk_toggle_button_set_active (vr->rw[VC_MISSING].rb, TRUE);
622 gchar *str = num_to_string (ov->v.range[0]);
623 gtk_toggle_button_set_active (vr->rw[VC_RANGE].rb, TRUE);
624 gtk_entry_set_text (vr->rw[VC_RANGE].e1, str);
628 str = num_to_string (ov->v.range[1]);
629 gtk_entry_set_text (vr->rw[VC_RANGE].e2, str);
636 gchar *str = num_to_string (ov->v.range[1]);
638 gtk_toggle_button_set_active (vr->rw[VC_LOW_UP].rb, TRUE);
640 gtk_entry_set_text (vr->rw[VC_LOW_UP].e1, str);
649 gchar *str = num_to_string (ov->v.range[0]);
651 gtk_toggle_button_set_active (vr->rw[VC_HIGH_DOWN].rb, TRUE);
653 gtk_entry_set_text (vr->rw[VC_HIGH_DOWN].e1, str);
660 gtk_toggle_button_set_active (vr->rw[VC_ELSE].rb, TRUE);
664 g_warning ("Unknown old value type");