Change many %g format specifiers to %.*g with precision DBL_DIG + 1.
[pspp] / src / ui / gui / psppire-val-chooser.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011, 2014  Free Software Foundation
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
19 #include <float.h>
20 #include <gtk/gtk.h>
21 #include "dialog-common.h"
22 #include "psppire-val-chooser.h"
23
24 #include "libpspp/str.h"
25
26
27 #include "ui/syntax-gen.h"
28
29 #include <gettext.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_val_chooser_base_finalize (PsppireValChooserClass *, gpointer);
34 static void psppire_val_chooser_base_init     (PsppireValChooserClass *class);
35 static void psppire_val_chooser_class_init    (PsppireValChooserClass *class);
36 static void psppire_val_chooser_init          (PsppireValChooser      *vc);
37
38 static void psppire_val_chooser_realize       (GtkWidget *w);
39
40
41
42 GType
43 psppire_val_chooser_get_type (void)
44 {
45   static GType psppire_val_chooser_type = 0;
46
47   if (!psppire_val_chooser_type)
48     {
49       static const GTypeInfo psppire_val_chooser_info =
50       {
51         sizeof (PsppireValChooserClass),
52         (GBaseInitFunc) psppire_val_chooser_base_init,
53         (GBaseFinalizeFunc) psppire_val_chooser_base_finalize,
54         (GClassInitFunc)psppire_val_chooser_class_init,
55         (GClassFinalizeFunc) NULL,
56         NULL,
57         sizeof (PsppireValChooser),
58         0,
59         (GInstanceInitFunc) psppire_val_chooser_init,
60       };
61
62       psppire_val_chooser_type =
63         g_type_register_static (GTK_TYPE_FRAME, "PsppireValChooser",
64                                 &psppire_val_chooser_info, 0);
65     }
66
67   return psppire_val_chooser_type;
68 }
69
70
71 static void
72 psppire_val_chooser_finalize (GObject *object)
73 {
74
75 }
76
77 /* Properties */
78 enum
79 {
80   PROP_0,
81   PROP_IS_STRING,
82   PROP_SHOW_ELSE
83 };
84
85
86 enum 
87   {
88     VC_VALUE,
89     VC_SYSMIS,
90     VC_MISSING,
91     VC_RANGE,
92     VC_LOW_UP,
93     VC_HIGH_DOWN,
94     VC_ELSE
95   };
96
97 static void
98 psppire_val_chooser_set_property (GObject         *object,
99                                guint            prop_id,
100                                const GValue    *value,
101                                GParamSpec      *pspec)
102 {
103   PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (object);
104
105   switch (prop_id)
106     {
107     case PROP_SHOW_ELSE:
108       {
109         gboolean x = g_value_get_boolean (value);
110         gtk_widget_set_visible (GTK_WIDGET (vr->rw[VC_ELSE].rb), x);
111         gtk_widget_set_visible (GTK_WIDGET (vr->rw[VC_ELSE].label), x);
112       }
113       break;
114     case PROP_IS_STRING:
115       vr->input_var_is_string = g_value_get_boolean (value);
116       gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_SYSMIS].rb), !vr->input_var_is_string);
117       gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_MISSING].rb), !vr->input_var_is_string);
118       gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_RANGE].rb), !vr->input_var_is_string);
119       gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_LOW_UP].rb), !vr->input_var_is_string);      
120       gtk_widget_set_sensitive (GTK_WIDGET (vr->rw[VC_HIGH_DOWN].rb), !vr->input_var_is_string);
121       break;
122     default:
123       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124       break;
125     };
126 }
127
128
129 static void
130 psppire_val_chooser_get_property (GObject         *object,
131                                guint            prop_id,
132                                GValue          *value,
133                                GParamSpec      *pspec)
134 {
135   PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (object);
136
137   switch (prop_id)
138     {
139     case PROP_SHOW_ELSE:
140       {
141         gboolean x =
142           gtk_widget_get_visible (GTK_WIDGET (vr->rw[VC_ELSE].rb));
143         g_value_set_boolean (value, x);
144       }
145       break;
146     case PROP_IS_STRING:
147       g_value_set_boolean (value, vr->input_var_is_string);
148     default:
149       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150       break;
151     };
152 }
153
154
155 static GObjectClass * parent_class = NULL;
156
157 static void
158 psppire_val_chooser_class_init (PsppireValChooserClass *class)
159 {
160   GObjectClass *object_class = G_OBJECT_CLASS (class);
161   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
162
163   GParamSpec *is_string_spec =
164     g_param_spec_boolean ("is-string",
165                           "String Value",
166                           "Should the value range be a string value",
167                           FALSE,
168                           G_PARAM_READWRITE);
169
170   GParamSpec *show_else_spec =
171     g_param_spec_boolean ("show-else",
172                           "Show Else",
173                           "Should the \"All other values\" item be visible",
174                           TRUE,
175                           G_PARAM_READWRITE);
176
177
178   parent_class = g_type_class_peek_parent (class);
179
180   object_class->set_property = psppire_val_chooser_set_property;
181   object_class->get_property = psppire_val_chooser_get_property;
182
183   widget_class->realize = psppire_val_chooser_realize;
184
185   g_object_class_install_property (object_class,
186                                    PROP_IS_STRING,
187                                    is_string_spec);
188
189   g_object_class_install_property (object_class,
190                                    PROP_SHOW_ELSE,
191                                    show_else_spec);
192 }
193
194
195 static void
196 psppire_val_chooser_base_init (PsppireValChooserClass *class)
197 {
198   GObjectClass *object_class = G_OBJECT_CLASS (class);
199
200   object_class->finalize = psppire_val_chooser_finalize;
201 }
202
203
204
205 static void
206 psppire_val_chooser_base_finalize (PsppireValChooserClass *class,
207                                  gpointer class_data)
208 {
209
210 }
211
212
213 /* Set the focus of B to follow the sensitivity of A */
214 static void
215 focus_follows_sensitivity (GtkWidget *a, GParamSpec *pspec, GtkWidget *b)
216 {
217   gboolean sens = gtk_widget_get_sensitive (a);
218
219   g_object_set (b, "has-focus", sens, NULL);
220 }
221
222
223 struct layout;
224 typedef GtkWidget *filler_f (struct layout *, struct range_widgets *);
225 typedef void set_f (PsppireValChooser *, struct old_value *, const struct range_widgets *);
226
227 struct layout
228 {
229   const gchar *label;
230   filler_f *fill;
231   set_f *set;
232 };
233
234
235
236 static void simple_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
237 {
238   const gchar *text = gtk_entry_get_text (rw->e1);
239
240   if ( vr->input_var_is_string)
241     {
242       ov->type = OV_STRING;
243       ov->v.s = g_strdup (text);
244     }
245   else
246     {
247       ov->type = OV_NUMERIC;
248       ov->v.v = g_strtod (text, 0);
249     }
250 }
251
252 static void lo_up_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets  *rw)
253 {
254   const gchar *text = gtk_entry_get_text (rw->e1);
255   
256   ov->type = OV_LOW_UP;
257   ov->v.range[1] = g_strtod (text, 0);
258 }
259
260
261 static void hi_down_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
262 {
263   const gchar *text = gtk_entry_get_text (rw->e1);
264   
265   ov->type = OV_HIGH_DOWN;
266   ov->v.range[0] = g_strtod (text, 0);
267 }
268
269 static void missing_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
270 {
271   ov->type = OV_MISSING;
272 }
273
274
275 static void sysmis_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
276 {
277   ov->type = OV_SYSMIS;
278 }
279
280 static void else_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *l)
281 {
282   ov->type = OV_ELSE;
283 }
284
285
286 static void range_set (PsppireValChooser *vr, struct old_value *ov, const struct range_widgets *rw)
287 {
288   const gchar *text = gtk_entry_get_text (rw->e1);
289
290   ov->type = OV_RANGE;
291   ov->v.range[0] = g_strtod (text, 0);
292   
293   text = gtk_entry_get_text (rw->e2);
294   ov->v.range[1] = g_strtod (text, 0);
295 }
296
297 static GtkWidget * range_entry (struct layout *l, struct range_widgets *rw)
298 {
299   GtkWidget *vbox = gtk_vbox_new (3, FALSE);
300   GtkWidget *entrylo = gtk_entry_new ();
301   GtkWidget *label = gtk_label_new (_("through"));
302   GtkWidget *entryhi = gtk_entry_new ();
303
304   rw->e1 = GTK_ENTRY (entrylo);
305   rw->e2 = GTK_ENTRY (entryhi);
306
307   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
308
309   g_signal_connect (vbox, "notify::sensitive", G_CALLBACK (focus_follows_sensitivity), entrylo);
310
311   gtk_box_pack_start (GTK_BOX (vbox), entrylo, TRUE, TRUE, 0);
312   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
313   gtk_box_pack_start (GTK_BOX (vbox), entryhi, TRUE, TRUE, 0);
314   return vbox;
315 }
316
317 static GtkWidget * simple_entry (struct layout *l, struct range_widgets *rw)
318 {
319   GtkWidget *entry = gtk_entry_new ();
320
321   rw->e1 = GTK_ENTRY (entry);
322
323   g_signal_connect (entry, "notify::sensitive", G_CALLBACK (focus_follows_sensitivity), entry);
324   return entry;
325 }
326
327
328 static struct layout range_opt[n_VAL_CHOOSER_BUTTONS]= 
329   {
330     {N_("_Value:"),                    simple_entry, simple_set },
331     {N_("_System Missing"),            NULL,         sysmis_set },
332     {N_("System _or User Missing"),    NULL,         missing_set},
333     {N_("_Range:"),                    range_entry,  range_set  },
334     {N_("Range, _LOWEST thru value"),  simple_entry, lo_up_set  },
335     {N_("Range, value thru _HIGHEST"), simple_entry, hi_down_set},
336     {N_("_All other values"),          NULL,         else_set   }
337   };
338
339 static void
340 psppire_val_chooser_init (PsppireValChooser *vr)
341 {
342   gint i;
343   GtkWidget *aln = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
344   GtkWidget *table = gtk_table_new (11, 2, FALSE);
345   GSList *group = NULL;
346   gint row = 0;
347
348   gtk_alignment_set_padding (GTK_ALIGNMENT (aln), 0, 0, 5, 5);
349
350   vr->input_var_is_string = FALSE;
351
352   for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
353     {
354       struct layout *l = &range_opt[i];
355       vr->rw[i].label = GTK_LABEL (gtk_label_new (gettext (l->label)));
356       gtk_label_set_use_underline (vr->rw[i].label, TRUE);
357       vr->rw[i].rb = GTK_TOGGLE_BUTTON (gtk_radio_button_new (group));
358       gtk_label_set_mnemonic_widget (vr->rw[i].label, GTK_WIDGET (vr->rw[i].rb));
359
360       gtk_misc_set_alignment (GTK_MISC (vr->rw[i].label), 0, 0.5);
361
362       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (vr->rw[i].rb));
363
364       /* Attach the buttons */
365       gtk_table_attach (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].rb),
366                         0, 1,   row, row + 1,
367                         0, GTK_EXPAND | GTK_FILL,
368                         0, 0);
369
370       /* Attach the labels */
371       gtk_table_attach (GTK_TABLE (table), GTK_WIDGET (vr->rw[i].label),
372                         1, 2,   row, row + 1,
373                         GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
374                         0, 0);
375       ++row;
376
377       if (l->fill)
378         {
379           GtkWidget *fill = l->fill (l, &vr->rw[i]);
380
381           gtk_widget_set_sensitive (fill, FALSE);
382
383           gtk_table_attach_defaults (GTK_TABLE (table), fill, 1, 2,
384                                  row, row + 1);
385           ++row;
386
387           g_signal_connect (vr->rw[i].rb, "toggled", G_CALLBACK (set_sensitivity_from_toggle), fill);
388         }
389     }
390
391   gtk_frame_set_shadow_type (GTK_FRAME (vr), GTK_SHADOW_ETCHED_IN);
392
393   gtk_container_add (GTK_CONTAINER (aln), table);
394   gtk_container_add (GTK_CONTAINER (vr), aln);
395
396   gtk_widget_show_all (aln);
397 }
398
399
400 GtkWidget*
401 psppire_val_chooser_new (void)
402 {
403   return GTK_WIDGET (g_object_new (psppire_val_chooser_get_type (), NULL));
404 }
405
406
407
408 static void
409 psppire_val_chooser_realize (GtkWidget *w)
410 {
411   PsppireValChooser *vr = PSPPIRE_VAL_CHOOSER (w);
412
413   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(vr->rw[0].rb), TRUE);
414   gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (vr->rw[0].rb));
415
416   /* Chain up to the parent class */
417   GTK_WIDGET_CLASS (parent_class)->realize (w);
418 }
419
420
421 \f
422
423 /* A boxed type representing a value, or a range of values which may
424    potentially be replaced by something */
425
426
427 static struct old_value *
428 old_value_copy (struct old_value *ov)
429 {
430   struct old_value *copy = g_memdup (ov, sizeof (*copy));
431
432   if ( ov->type == OV_STRING )
433     copy->v.s = g_strdup (ov->v.s);
434
435   return copy;
436 }
437
438
439 static void
440 old_value_free (struct old_value *ov)
441 {
442   if (ov->type == OV_STRING)
443     g_free (ov->v.s);
444   g_free (ov);
445 }
446
447 static void
448 old_value_to_string (const GValue *src, GValue *dest)
449 {
450   const struct old_value *ov = g_value_get_boxed (src);
451
452   switch (ov->type)
453     {
454     case OV_NUMERIC:
455       {
456         gchar *text = g_strdup_printf ("%.*g", DBL_DIG + 1, ov->v.v);
457         g_value_set_string (dest, text);
458         g_free (text);
459       }
460       break;
461     case OV_STRING:
462       g_value_set_string (dest, ov->v.s);
463       break;
464     case OV_MISSING:
465       g_value_set_string (dest, "MISSING");
466       break;
467     case OV_SYSMIS:
468       g_value_set_string (dest, "SYSMIS");
469       break;
470     case OV_ELSE:
471       g_value_set_string (dest, "ELSE");
472       break;
473     case OV_RANGE:
474       {
475         gchar *text;
476         char en_dash[6] = {0,0,0,0,0,0};
477
478         g_unichar_to_utf8 (0x2013, en_dash);
479
480         text = g_strdup_printf ("%.*g %s %.*g",
481                                 DBL_DIG + 1, ov->v.range[0],
482                                 en_dash,
483                                 DBL_DIG + 1, ov->v.range[1]);
484         g_value_set_string (dest, text);
485         g_free (text);
486       }
487       break;
488     case OV_LOW_UP:
489       {
490         gchar *text;
491         char en_dash[6] = {0,0,0,0,0,0};
492
493         g_unichar_to_utf8 (0x2013, en_dash);
494
495         text = g_strdup_printf ("LOWEST %s %.*g",
496                                 en_dash,
497                                 DBL_DIG + 1, ov->v.range[1]);
498
499         g_value_set_string (dest, text);
500         g_free (text);
501       }
502       break;
503     case OV_HIGH_DOWN:
504       {
505         gchar *text;
506         char en_dash[6] = {0,0,0,0,0,0};
507
508         g_unichar_to_utf8 (0x2013, en_dash);
509
510         text = g_strdup_printf ("%.*g %s HIGHEST",
511                                 DBL_DIG + 1, ov->v.range[0],
512                                 en_dash);
513
514         g_value_set_string (dest, text);
515         g_free (text);
516       }
517       break;
518     default:
519       g_warning ("Invalid type in old recode value");
520       g_value_set_string (dest, "???");
521       break;
522     };
523 }
524
525 GType
526 old_value_get_type (void)
527 {
528   static GType t = 0;
529
530   if (t == 0 )
531     {
532       t = g_boxed_type_register_static  ("psppire-recode-old-values",
533                                          (GBoxedCopyFunc) old_value_copy,
534                                          (GBoxedFreeFunc) old_value_free);
535
536       g_value_register_transform_func     (t, G_TYPE_STRING,
537                                            old_value_to_string);
538     }
539
540   return t;
541 }
542
543 \f
544
545 /* Generate a syntax fragment for NV and append it to STR */
546 void
547 old_value_append_syntax (struct string *str, const struct old_value *ov)
548 {
549   switch (ov->type)
550     {
551     case OV_NUMERIC:
552       ds_put_c_format (str, "%.*g", DBL_DIG + 1, ov->v.v);
553       break;
554     case OV_STRING:
555       {
556         struct string ds = DS_EMPTY_INITIALIZER;
557         syntax_gen_string (&ds, ss_cstr (ov->v.s));
558         ds_put_cstr (str, ds_cstr (&ds));
559         ds_destroy (&ds);
560       }
561       break;
562     case OV_MISSING:
563       ds_put_cstr (str, "MISSING");
564       break;
565     case OV_SYSMIS:
566       ds_put_cstr (str, "SYSMIS");
567       break;
568     case OV_ELSE:
569       ds_put_cstr (str, "ELSE");
570       break;
571     case OV_RANGE:
572       ds_put_c_format (str, "%.*g THRU %.*g",
573                        DBL_DIG + 1, ov->v.range[0],
574                        DBL_DIG + 1, ov->v.range[1]);
575       break;
576     case OV_LOW_UP:
577       ds_put_c_format (str, "LOWEST THRU %*gg",
578                        DBL_DIG + 1, ov->v.range[1]);
579       break;
580     case OV_HIGH_DOWN:
581       ds_put_c_format (str, "%.*g THRU HIGHEST",
582                        DBL_DIG + 1, ov->v.range[0]);
583       break;
584     default:
585       g_warning ("Invalid type in old recode value");
586       ds_put_cstr (str, "???");
587       break;
588     };
589 }
590
591
592
593 /* Set OV according to the current state of VR */
594 void
595 psppire_val_chooser_get_status (PsppireValChooser *vr, struct old_value *ov)
596 {
597   int i;
598
599   for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
600     {
601       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (vr->rw[i].rb)))
602         {
603           break;
604         }
605     }
606
607   range_opt[i].set (vr, ov, &vr->rw[i]);
608 }
609
610 /* This might need to be changed to something less naive.
611    In particular, what happends with dates, etc?
612  */
613 static gchar *
614 num_to_string (gdouble x)
615 {
616   return g_strdup_printf ("%.*g", DBL_DIG + 1, x);
617 }
618
619
620 /* Set VR according to the value of OV */
621 void
622 psppire_val_chooser_set_status (PsppireValChooser *vr, const struct old_value *ov)
623 {
624   gint i;
625   if ( !ov )
626     return;
627
628   for (i = 0; i < n_VAL_CHOOSER_BUTTONS; ++i)
629     {
630       if (vr->rw[i].e1)
631         gtk_entry_set_text (vr->rw[i].e1, "");
632
633       if (vr->rw[i].e2)
634         gtk_entry_set_text (vr->rw[i].e2, "");
635     }
636
637   switch (ov->type)
638     {
639     case OV_STRING:
640       gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
641       gtk_entry_set_text (vr->rw[0].e1, ov->v.s);
642       break;
643       
644     case OV_NUMERIC:
645       {
646         gchar *str;
647         gtk_toggle_button_set_active (vr->rw[0].rb, TRUE);
648         
649         str = num_to_string (ov->v.v);
650         
651         gtk_entry_set_text (vr->rw[0].e1, str);
652         g_free (str);
653       }
654       break;
655
656       case OV_SYSMIS:
657         gtk_toggle_button_set_active (vr->rw[VC_SYSMIS].rb, TRUE);
658         break;
659
660       case OV_MISSING:
661         gtk_toggle_button_set_active (vr->rw[VC_MISSING].rb, TRUE);
662         break;
663
664       case OV_RANGE:
665         {
666           gchar *str = num_to_string (ov->v.range[0]);
667           gtk_toggle_button_set_active (vr->rw[VC_RANGE].rb, TRUE);
668           gtk_entry_set_text (vr->rw[VC_RANGE].e1, str);
669
670           g_free (str);
671
672           str = num_to_string (ov->v.range[1]);
673           gtk_entry_set_text (vr->rw[VC_RANGE].e2, str);
674           g_free (str);
675         }
676         break;
677
678       case OV_LOW_UP:
679         {
680           gchar *str = num_to_string (ov->v.range[1]);
681
682           gtk_toggle_button_set_active (vr->rw[VC_LOW_UP].rb, TRUE);
683
684           gtk_entry_set_text (vr->rw[VC_LOW_UP].e1, str);
685
686           g_free (str);
687         }
688         break;
689
690
691       case OV_HIGH_DOWN:
692         {
693           gchar *str = num_to_string (ov->v.range[0]);
694
695           gtk_toggle_button_set_active (vr->rw[VC_HIGH_DOWN].rb, TRUE);
696
697           gtk_entry_set_text (vr->rw[VC_HIGH_DOWN].e1, str);
698
699           g_free (str);
700         }
701         break;
702
703       case OV_ELSE:
704         gtk_toggle_button_set_active (vr->rw[VC_ELSE].rb, TRUE);
705         break;
706
707     default:
708       g_warning ("Unknown old value type");
709       break;
710     };
711 }