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 /* Attach the buttons */
372 gtk_table_attach (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].rb),
374 0, GTK_EXPAND | GTK_FILL,
377 /* Attach the labels */
378 gtk_table_attach (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].label),
380 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
386 GtkWidget *fill = l->fill (l, &vr->rw[i]);
388 gtk_widget_set_sensitive (fill, FALSE);
390 gtk_table_attach_defaults (GTK_TABLE (table), fill, 1, 2,
394 g_signal_connect (vr->rw[i].rb, "toggled", G_CALLBACK (set_sensitivity_from_toggle), fill);
398 gtk_frame_set_shadow_type (GTK_FRAME (vr), GTK_SHADOW_ETCHED_IN);
400 gtk_container_add (GTK_CONTAINER (aln), table);
401 gtk_container_add (GTK_CONTAINER (vr), aln);
403 gtk_widget_show_all (aln);
408 psppire_val_chooser_new (void)
410 return GTK_WIDGET (g_object_new (psppire_val_chooser_get_type (), NULL));
416 psppire_val_chooser_realize (GtkWidget *w)
418 PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (w);
420 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(vr->rw[0].rb), TRUE);
421 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (vr->rw[0].rb));
423 /* Chain up to the parent class */
424 GTK_WIDGET_CLASS (parent_class)->realize (w);
430 /* A boxed type representing a value, or a range of values which may
431 potentially be replaced by something */
434 static struct old_value *
435 old_value_copy (struct old_value *ov)
437 struct old_value *copy = g_memdup (ov, sizeof (*copy));
439 if ( ov->type == OV_STRING )
440 copy->v.s = g_strdup (ov->v.s);
447 old_value_free (struct old_value *ov)
449 if (ov->type == OV_STRING)
455 old_value_to_string (const GValue *src, GValue *dest)
457 const struct old_value *ov = g_value_get_boxed (src);
463 gchar *text = g_strdup_printf ("%g", ov->v.v);
464 g_value_set_string (dest, text);
469 g_value_set_string (dest, ov->v.s);
472 g_value_set_string (dest, "MISSING");
475 g_value_set_string (dest, "SYSMIS");
478 g_value_set_string (dest, "ELSE");
483 char en_dash[6] = {0,0,0,0,0,0};
485 g_unichar_to_utf8 (0x2013, en_dash);
487 text = g_strdup_printf ("%g %s %g",
491 g_value_set_string (dest, text);
498 char en_dash[6] = {0,0,0,0,0,0};
500 g_unichar_to_utf8 (0x2013, en_dash);
502 text = g_strdup_printf ("LOWEST %s %g",
506 g_value_set_string (dest, text);
513 char en_dash[6] = {0,0,0,0,0,0};
515 g_unichar_to_utf8 (0x2013, en_dash);
517 text = g_strdup_printf ("%g %s HIGHEST",
521 g_value_set_string (dest, text);
526 g_warning ("Invalid type in old recode value");
527 g_value_set_string (dest, "???");
533 old_value_get_type (void)
539 t = g_boxed_type_register_static ("psppire-recode-old-values",
540 (GBoxedCopyFunc) old_value_copy,
541 (GBoxedFreeFunc) old_value_free);
543 g_value_register_transform_func (t, G_TYPE_STRING,
544 old_value_to_string);
552 /* Generate a syntax fragment for NV and append it to STR */
554 old_value_append_syntax (GString *str, const struct old_value *ov)
559 g_string_append_printf (str, "%g", ov->v.v);
563 struct string ds = DS_EMPTY_INITIALIZER;
564 syntax_gen_string (&ds, ss_cstr (ov->v.s));
565 g_string_append (str, ds_cstr (&ds));
570 g_string_append (str, "MISSING");
573 g_string_append (str, "SYSMIS");
576 g_string_append (str, "ELSE");
579 g_string_append_printf (str, "%g THRU %g",
584 g_string_append_printf (str, "LOWEST THRU %g",
588 g_string_append_printf (str, "%g THRU HIGHEST",
592 g_warning ("Invalid type in old recode value");
593 g_string_append (str, "???");
600 /* Set OV according to the current state of VR */
602 psppire_val_chooser_get_status (PsppireValChooser *vr, struct old_value *ov)
606 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
608 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (vr->rw[i].rb)))
614 range_opt[i].set (vr, ov, &vr->rw[i]);
617 /* This might need to be changed to something less naive.
618 In particular, what happends with dates, etc?
621 num_to_string (gdouble x)
623 return g_strdup_printf ("%g", x);
627 /* Set VR according to the value of OV */
629 psppire_val_chooser_set_status (PsppireValChooser *vr, const struct old_value *ov)
635 for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
638 gtk_entry_set_text (vr->rw[i].e1, "");
641 gtk_entry_set_text (vr->rw[i].e2, "");
647 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
648 gtk_entry_set_text (vr->rw[0].e1, ov->v.s);
654 gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
656 str = num_to_string (ov->v.v);
658 gtk_entry_set_text (vr->rw[0].e1, str);
664 gtk_toggle_button_set_active (vr->rw[VC_SYSMIS].rb, TRUE);
668 gtk_toggle_button_set_active (vr->rw[VC_MISSING].rb, TRUE);
673 gchar *str = num_to_string (ov->v.range[0]);
674 gtk_toggle_button_set_active (vr->rw[VC_RANGE].rb, TRUE);
675 gtk_entry_set_text (vr->rw[VC_RANGE].e1, str);
679 str = num_to_string (ov->v.range[1]);
680 gtk_entry_set_text (vr->rw[VC_RANGE].e2, str);
687 gchar *str = num_to_string (ov->v.range[1]);
689 gtk_toggle_button_set_active (vr->rw[VC_LOW_UP].rb, TRUE);
691 gtk_entry_set_text (vr->rw[VC_LOW_UP].e1, str);
700 gchar *str = num_to_string (ov->v.range[0]);
702 gtk_toggle_button_set_active (vr->rw[VC_HIGH_DOWN].rb, TRUE);
704 gtk_entry_set_text (vr->rw[VC_HIGH_DOWN].e1, str);
711 gtk_toggle_button_set_active (vr->rw[VC_ELSE].rb, TRUE);
715 g_warning ("Unknown old value type");