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 gtk_label_set_use_underline (vr->rw[i].label, TRUE);
363 vr->rw[i].rb = GTK_TOGGLE_BUTTON (gtk_radio_button_new (group));
364 gtk_label_set_mnemonic_widget (vr->rw[i].label, GTK_WIDGET (vr->rw[i].rb));
366 gtk_misc_set_alignment (GTK_MISC (vr->rw[i].label), 0, 0.5);
368 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (vr->rw[i].rb));
370 /* Attach the buttons */
371 gtk_table_attach (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].rb),
373 0, GTK_EXPAND | GTK_FILL,
376 /* Attach the labels */
377 gtk_table_attach (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].label),
379 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
385 GtkWidget *fill = l->fill (l, &vr->rw[i]);
387 gtk_widget_set_sensitive (fill, FALSE);
389 gtk_table_attach_defaults (GTK_TABLE (table), fill, 1, 2,
393 g_signal_connect (vr->rw[i].rb, "toggled", G_CALLBACK (set_sensitivity_from_toggle), fill);
397 gtk_frame_set_shadow_type (GTK_FRAME (vr), GTK_SHADOW_ETCHED_IN);
399 gtk_container_add (GTK_CONTAINER (aln), table);
400 gtk_container_add (GTK_CONTAINER (vr), aln);
402 gtk_widget_show_all (aln);
407 psppire_val_chooser_new (void)
409 return GTK_WIDGET (g_object_new (psppire_val_chooser_get_type (), NULL));
415 psppire_val_chooser_realize (GtkWidget *w)
417 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (w);
419 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(vr->rw[0].rb), TRUE);
420 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (vr->rw[0].rb));
422 /* Chain up to the parent class */
423 GTK_WIDGET_CLASS (parent_class)->realize (w);
429 /* A boxed type representing a value, or a range of values which may
430 potentially be replaced by something */
433 static struct old_value *
434 old_value_copy (struct old_value *ov)
436 struct old_value *copy = g_memdup (ov, sizeof (*copy));
438 if ( ov->type == OV_STRING )
439 copy->v.s = g_strdup (ov->v.s);
446 old_value_free (struct old_value *ov)
448 if (ov->type == OV_STRING)
454 old_value_to_string (const GValue *src, GValue *dest)
456 const struct old_value *ov = g_value_get_boxed (src);
462 gchar *text = g_strdup_printf ("%g", ov->v.v);
463 g_value_set_string (dest, text);
468 g_value_set_string (dest, ov->v.s);
471 g_value_set_string (dest, "MISSING");
474 g_value_set_string (dest, "SYSMIS");
477 g_value_set_string (dest, "ELSE");
482 char en_dash[6] = {0,0,0,0,0,0};
484 g_unichar_to_utf8 (0x2013, en_dash);
486 text = g_strdup_printf ("%g %s %g",
490 g_value_set_string (dest, text);
497 char en_dash[6] = {0,0,0,0,0,0};
499 g_unichar_to_utf8 (0x2013, en_dash);
501 text = g_strdup_printf ("LOWEST %s %g",
505 g_value_set_string (dest, text);
512 char en_dash[6] = {0,0,0,0,0,0};
514 g_unichar_to_utf8 (0x2013, en_dash);
516 text = g_strdup_printf ("%g %s HIGHEST",
520 g_value_set_string (dest, text);
525 g_warning ("Invalid type in old recode value");
526 g_value_set_string (dest, "???");
532 old_value_get_type (void)
538 t = g_boxed_type_register_static ("psppire-recode-old-values",
539 (GBoxedCopyFunc) old_value_copy,
540 (GBoxedFreeFunc) old_value_free);
542 g_value_register_transform_func (t, G_TYPE_STRING,
543 old_value_to_string);
551 /* Generate a syntax fragment for NV and append it to STR */
553 old_value_append_syntax (GString *str, const struct old_value *ov)
558 g_string_append_printf (str, "%g", ov->v.v);
562 struct string ds = DS_EMPTY_INITIALIZER;
563 syntax_gen_string (&ds, ss_cstr (ov->v.s));
564 g_string_append (str, ds_cstr (&ds));
569 g_string_append (str, "MISSING");
572 g_string_append (str, "SYSMIS");
575 g_string_append (str, "ELSE");
578 g_string_append_printf (str, "%g THRU %g",
583 g_string_append_printf (str, "LOWEST THRU %g",
587 g_string_append_printf (str, "%g THRU HIGHEST",
591 g_warning ("Invalid type in old recode value");
592 g_string_append (str, "???");
599 /* Set OV according to the current state of VR */
601 psppire_val_chooser_get_status (PsppireValChooser *vr, struct old_value *ov)
605 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
607 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (vr->rw[i].rb)))
613 range_opt[i].set (vr, ov, &vr->rw[i]);
616 /* This might need to be changed to something less naive.
617 In particular, what happends with dates, etc?
620 num_to_string (gdouble x)
622 return g_strdup_printf ("%g", x);
626 /* Set VR according to the value of OV */
628 psppire_val_chooser_set_status (PsppireValChooser *vr, const struct old_value *ov)
634 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
637 gtk_entry_set_text (vr->rw[i].e1, "");
640 gtk_entry_set_text (vr->rw[i].e2, "");
646 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
647 gtk_entry_set_text (vr->rw[0].e1, ov->v.s);
653 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
655 str = num_to_string (ov->v.v);
657 gtk_entry_set_text (vr->rw[0].e1, str);
663 gtk_toggle_button_set_active (vr->rw[VC_SYSMIS].rb, TRUE);
667 gtk_toggle_button_set_active (vr->rw[VC_MISSING].rb, TRUE);
672 gchar *str = num_to_string (ov->v.range[0]);
673 gtk_toggle_button_set_active (vr->rw[VC_RANGE].rb, TRUE);
674 gtk_entry_set_text (vr->rw[VC_RANGE].e1, str);
678 str = num_to_string (ov->v.range[1]);
679 gtk_entry_set_text (vr->rw[VC_RANGE].e2, str);
686 gchar *str = num_to_string (ov->v.range[1]);
688 gtk_toggle_button_set_active (vr->rw[VC_LOW_UP].rb, TRUE);
690 gtk_entry_set_text (vr->rw[VC_LOW_UP].e1, str);
699 gchar *str = num_to_string (ov->v.range[0]);
701 gtk_toggle_button_set_active (vr->rw[VC_HIGH_DOWN].rb, TRUE);
703 gtk_entry_set_text (vr->rw[VC_HIGH_DOWN].e1, str);
710 gtk_toggle_button_set_active (vr->rw[VC_ELSE].rb, TRUE);
714 g_warning ("Unknown old value type");