1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2011 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/>. */
20 #include "psppire-val-chooser.h"
22 #include "libpspp/str.h"
25 #include "ui/syntax-gen.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
31 static void psppire_val_chooser_base_finalize (PsppireValChooserClass *, gpointer);
32 static void psppire_val_chooser_base_init (PsppireValChooserClass *class);
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);
41 psppire_val_chooser_get_type (void)
43 static GType psppire_val_chooser_type = 0;
45 if (!psppire_val_chooser_type)
47 static const GTypeInfo psppire_val_chooser_info =
49 sizeof (PsppireValChooserClass),
50 (GBaseInitFunc) psppire_val_chooser_base_init,
51 (GBaseFinalizeFunc) psppire_val_chooser_base_finalize,
52 (GClassInitFunc)psppire_val_chooser_class_init,
53 (GClassFinalizeFunc) NULL,
55 sizeof (PsppireValChooser),
57 (GInstanceInitFunc) psppire_val_chooser_init,
60 psppire_val_chooser_type =
61 g_type_register_static (GTK_TYPE_FRAME, "PsppireValChooser",
62 &psppire_val_chooser_info, 0);
65 return psppire_val_chooser_type;
70 psppire_val_chooser_finalize (GObject *object)
96 psppire_val_chooser_set_property (GObject *object,
101 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (object);
107 gboolean x = g_value_get_boolean (value);
108 gtk_widget_set_visible (GTK_WIDGET (vr->rw[VC_ELSE].rb), x);
109 gtk_widget_set_visible (GTK_WIDGET (vr->rw[VC_ELSE].label), x);
113 vr->input_var_is_string = g_value_get_boolean (value);
114 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_SYSMIS].rb), !vr->input_var_is_string);
115 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_MISSING].rb), !vr->input_var_is_string);
116 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_RANGE].rb), !vr->input_var_is_string);
117 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_LOW_UP].rb), !vr->input_var_is_string);
118 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_HIGH_DOWN].rb), !vr->input_var_is_string);
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 psppire_val_chooser_get_property (GObject *object,
133 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (object);
140 gtk_widget_get_visible (GTK_WIDGET (vr->rw[VC_ELSE].rb));
141 g_value_set_boolean (value, x);
145 g_value_set_boolean (value, vr->input_var_is_string);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 static GObjectClass * parent_class = NULL;
156 psppire_val_chooser_class_init (PsppireValChooserClass *class)
158 GObjectClass *object_class = G_OBJECT_CLASS (class);
159 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
161 GParamSpec *is_string_spec =
162 g_param_spec_boolean ("is-string",
164 "Should the value range be a string value",
168 GParamSpec *show_else_spec =
169 g_param_spec_boolean ("show-else",
171 "Should the \"All other values\" item be visible",
176 parent_class = g_type_class_peek_parent (class);
178 object_class->set_property = psppire_val_chooser_set_property;
179 object_class->get_property = psppire_val_chooser_get_property;
181 widget_class->realize = psppire_val_chooser_realize;
183 g_object_class_install_property (object_class,
187 g_object_class_install_property (object_class,
194 psppire_val_chooser_base_init (PsppireValChooserClass *class)
196 GObjectClass *object_class = G_OBJECT_CLASS (class);
198 object_class->finalize = psppire_val_chooser_finalize;
204 psppire_val_chooser_base_finalize (PsppireValChooserClass *class,
211 /* Set the focus of B to follow the sensitivity of A */
213 focus_follows_sensitivity (GtkWidget *a, GParamSpec *pspec, GtkWidget *b)
215 gboolean sens = gtk_widget_get_sensitive (a);
217 g_object_set (b, "has-focus", sens, NULL);
222 typedef GtkWidget *filler_f (struct layout *, struct range_widgets *);
223 typedef void set_f (PsppireValChooser *, struct old_value *, const struct range_widgets *);
234 static void simple_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
236 const gchar *text = gtk_entry_get_text (rw->e1);
238 if ( vr->input_var_is_string)
240 ov->type = OV_STRING;
241 ov->v.s = g_strdup (text);
245 ov->type = OV_NUMERIC;
246 ov->v.v = g_strtod (text, 0);
250 static void lo_up_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
252 const gchar *text = gtk_entry_get_text (rw->e1);
254 ov->type = OV_LOW_UP;
255 ov->v.range[1] = g_strtod (text, 0);
259 static void hi_down_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
261 const gchar *text = gtk_entry_get_text (rw->e1);
263 ov->type = OV_HIGH_DOWN;
264 ov->v.range[0] = g_strtod (text, 0);
267 static void missing_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
269 ov->type = OV_MISSING;
273 static void sysmis_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
275 ov->type = OV_SYSMIS;
278 static void else_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
284 static void range_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
286 const gchar *text = gtk_entry_get_text (rw->e1);
289 ov->v.range[0] = g_strtod (text, 0);
291 text = gtk_entry_get_text (rw->e2);
292 ov->v.range[1] = g_strtod (text, 0);
295 static GtkWidget * range_entry (struct layout *l, struct range_widgets *rw)
297 GtkWidget *vbox = gtk_vbox_new (3, FALSE);
298 GtkWidget *entrylo = gtk_entry_new ();
299 GtkWidget *label = gtk_label_new (_("through"));
300 GtkWidget *entryhi = gtk_entry_new ();
302 rw->e1 = GTK_ENTRY (entrylo);
303 rw->e2 = GTK_ENTRY (entryhi);
305 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
307 g_signal_connect (vbox, "notify::sensitive", G_CALLBACK (focus_follows_sensitivity), entrylo);
309 gtk_box_pack_start (GTK_BOX (vbox), entrylo, TRUE, TRUE, 0);
310 gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
311 gtk_box_pack_start (GTK_BOX (vbox), entryhi, TRUE, TRUE, 0);
315 static GtkWidget * simple_entry (struct layout *l, struct range_widgets *rw)
317 GtkWidget *entry = gtk_entry_new ();
319 rw->e1 = GTK_ENTRY (entry);
321 g_signal_connect (entry, "notify::sensitive", G_CALLBACK (focus_follows_sensitivity), entry);
326 static struct layout range_opt[n_VAL_CHOOSER_BUTTONS]=
328 {N_("Value:"), simple_entry, simple_set },
329 {N_("System Missing"), NULL, sysmis_set },
330 {N_("System or User Missing"), NULL, missing_set},
331 {N_("Range:"), range_entry, range_set },
332 {N_("Range, LOWEST thru value"), simple_entry, lo_up_set },
333 {N_("Range, value thru HIGHEST"), simple_entry, hi_down_set},
334 {N_("All other values"), NULL, else_set }
338 set_sensitivity_from_toggle (GtkToggleButton *togglebutton, GtkWidget *w)
340 gboolean active = gtk_toggle_button_get_active (togglebutton);
342 gtk_widget_set_sensitive (w, active);
346 psppire_val_chooser_init (PsppireValChooser *vr)
349 GtkWidget *aln = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
350 GtkWidget *table = gtk_table_new (11, 2, FALSE);
351 GSList *group = NULL;
354 gtk_alignment_set_padding (GTK_ALIGNMENT (aln), 0, 0, 5, 5);
356 vr->input_var_is_string = FALSE;
358 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
360 struct layout *l = &range_opt[i];
361 vr->rw[i].label = GTK_LABEL (gtk_label_new (gettext (l->label)));
362 vr->rw[i].rb = GTK_TOGGLE_BUTTON (gtk_radio_button_new (group));
364 gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[i].label), FALSE);
365 g_signal_connect (vr->rw[i].rb, "toggled", G_CALLBACK (set_sensitivity_from_toggle), vr->rw[i].label);
367 gtk_misc_set_alignment (GTK_MISC (vr->rw[i].label), 0, 0.5);
369 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (vr->rw[i].rb));
371 gtk_table_attach_defaults (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].rb), 0, 1,
375 gtk_table_attach_defaults (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].label), 1, 2,
381 GtkWidget *fill = l->fill (l, &vr->rw[i]);
383 gtk_widget_set_sensitive (fill, FALSE);
385 gtk_table_attach_defaults (GTK_TABLE (table), fill, 1, 2,
389 g_signal_connect (vr->rw[i].rb, "toggled", G_CALLBACK (set_sensitivity_from_toggle), fill);
393 gtk_frame_set_shadow_type (GTK_FRAME (vr), GTK_SHADOW_ETCHED_IN);
395 gtk_container_add (GTK_CONTAINER (aln), table);
396 gtk_container_add (GTK_CONTAINER (vr), aln);
398 gtk_widget_show_all (aln);
403 psppire_val_chooser_new (void)
405 return GTK_WIDGET (g_object_new (psppire_val_chooser_get_type (), NULL));
411 psppire_val_chooser_realize (GtkWidget *w)
413 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (w);
415 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(vr->rw[0].rb), TRUE);
416 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (vr->rw[0].rb));
418 /* Chain up to the parent class */
419 GTK_WIDGET_CLASS (parent_class)->realize (w);
425 /* A boxed type representing a value, or a range of values which may
426 potentially be replaced by something */
429 static struct old_value *
430 old_value_copy (struct old_value *ov)
432 struct old_value *copy = g_memdup (ov, sizeof (*copy));
434 if ( ov->type == OV_STRING )
435 copy->v.s = g_strdup (ov->v.s);
442 old_value_free (struct old_value *ov)
444 if (ov->type == OV_STRING)
450 old_value_to_string (const GValue *src, GValue *dest)
452 const struct old_value *ov = g_value_get_boxed (src);
458 gchar *text = g_strdup_printf ("%g", ov->v.v);
459 g_value_set_string (dest, text);
464 g_value_set_string (dest, ov->v.s);
467 g_value_set_string (dest, "MISSING");
470 g_value_set_string (dest, "SYSMIS");
473 g_value_set_string (dest, "ELSE");
478 char en_dash[6] = {0,0,0,0,0,0};
480 g_unichar_to_utf8 (0x2013, en_dash);
482 text = g_strdup_printf ("%g %s %g",
486 g_value_set_string (dest, text);
493 char en_dash[6] = {0,0,0,0,0,0};
495 g_unichar_to_utf8 (0x2013, en_dash);
497 text = g_strdup_printf ("LOWEST %s %g",
501 g_value_set_string (dest, text);
508 char en_dash[6] = {0,0,0,0,0,0};
510 g_unichar_to_utf8 (0x2013, en_dash);
512 text = g_strdup_printf ("%g %s HIGHEST",
516 g_value_set_string (dest, text);
521 g_warning ("Invalid type in old recode value");
522 g_value_set_string (dest, "???");
528 old_value_get_type (void)
534 t = g_boxed_type_register_static ("psppire-recode-old-values",
535 (GBoxedCopyFunc) old_value_copy,
536 (GBoxedFreeFunc) old_value_free);
538 g_value_register_transform_func (t, G_TYPE_STRING,
539 old_value_to_string);
547 /* Generate a syntax fragment for NV and append it to STR */
549 old_value_append_syntax (GString *str, const struct old_value *ov)
554 g_string_append_printf (str, "%g", ov->v.v);
558 struct string ds = DS_EMPTY_INITIALIZER;
559 syntax_gen_string (&ds, ss_cstr (ov->v.s));
560 g_string_append (str, ds_cstr (&ds));
565 g_string_append (str, "MISSING");
568 g_string_append (str, "SYSMIS");
571 g_string_append (str, "ELSE");
574 g_string_append_printf (str, "%g THRU %g",
579 g_string_append_printf (str, "LOWEST THRU %g",
583 g_string_append_printf (str, "%g THRU HIGHEST",
587 g_warning ("Invalid type in old recode value");
588 g_string_append (str, "???");
595 /* Set OV according to the current state of VR */
597 psppire_val_chooser_get_status (PsppireValChooser *vr, struct old_value *ov)
601 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
603 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (vr->rw[i].rb)))
609 range_opt[i].set (vr, ov, &vr->rw[i]);
612 /* This might need to be changed to something less naive.
613 In particular, what happends with dates, etc?
616 num_to_string (gdouble x)
618 return g_strdup_printf ("%g", x);
622 /* Set VR according to the value of OV */
624 psppire_val_chooser_set_status (PsppireValChooser *vr, const struct old_value *ov)
630 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
633 gtk_entry_set_text (vr->rw[i].e1, "");
636 gtk_entry_set_text (vr->rw[i].e2, "");
642 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
643 gtk_entry_set_text (vr->rw[0].e1, ov->v.s);
649 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
651 str = num_to_string (ov->v.v);
653 gtk_entry_set_text (vr->rw[0].e1, str);
659 gtk_toggle_button_set_active (vr->rw[VC_SYSMIS].rb, TRUE);
663 gtk_toggle_button_set_active (vr->rw[VC_MISSING].rb, TRUE);
668 gchar *str = num_to_string (ov->v.range[0]);
669 gtk_toggle_button_set_active (vr->rw[VC_RANGE].rb, TRUE);
670 gtk_entry_set_text (vr->rw[VC_RANGE].e1, str);
674 str = num_to_string (ov->v.range[1]);
675 gtk_entry_set_text (vr->rw[VC_RANGE].e2, str);
682 gchar *str = num_to_string (ov->v.range[1]);
684 gtk_toggle_button_set_active (vr->rw[VC_LOW_UP].rb, TRUE);
686 gtk_entry_set_text (vr->rw[VC_LOW_UP].e1, str);
695 gchar *str = num_to_string (ov->v.range[0]);
697 gtk_toggle_button_set_active (vr->rw[VC_HIGH_DOWN].rb, TRUE);
699 gtk_entry_set_text (vr->rw[VC_HIGH_DOWN].e1, str);
706 gtk_toggle_button_set_active (vr->rw[VC_ELSE].rb, TRUE);
710 g_warning ("Unknown old value type");