Patch #6262. New developers guide and resulting fixes and cleanups.
[pspp-builds.git] / src / ui / gui / var-display.c
1 #include <config.h>
2 #include "var-display.h"
3
4 #include <data/variable.h>
5 #include <stdlib.h>
6
7 #include "var-sheet.h"
8 #include <gettext.h>
9 #define _(msgid) gettext (msgid)
10 #define N_(msgid) msgid
11
12 #include "helper.h"
13
14 const static gchar none[] = N_("None");
15
16 gchar *
17 name_to_string (const struct variable *var, GError **err)
18 {
19   const char *name = var_get_name (var);
20   g_assert (name);
21
22   return pspp_locale_to_utf8 (name, -1, err);
23 }
24
25
26 gchar *
27 label_to_string (const struct variable *var, GError **err)
28 {
29   const char *label = var_get_label (var);
30
31   if ( ! label ) return g_strdup (none);
32
33   return pspp_locale_to_utf8 (label, -1, err);
34 }
35
36 gchar *
37 measure_to_string (const struct variable *var, GError **err)
38 {
39   const gint measure = var_get_measure (var);
40
41   g_assert (measure < n_MEASURES);
42   return g_locale_to_utf8 (gettext (measures[measure]),
43                            -1, 0, 0, err);
44 }
45
46
47 gchar *
48 missing_values_to_string (const struct variable *pv, GError **err)
49 {
50   const struct fmt_spec *fmt =  var_get_print_format (pv);
51   gchar *s;
52   const struct missing_values *miss = var_get_missing_values (pv);
53   if ( mv_is_empty (miss))
54     return g_locale_to_utf8 (gettext (none), -1, 0, 0, err);
55   else
56     {
57       if ( ! mv_has_range (miss))
58         {
59           GString *gstr = g_string_sized_new (10);
60           const int n = mv_n_values (miss);
61           gchar *mv[4] = {0,0,0,0};
62           gint i;
63           for (i = 0 ; i < n; ++i )
64             {
65               union value v;
66               mv_get_value (miss, &v, i);
67               mv[i] = value_to_text (v, *fmt);
68               if ( i > 0 )
69                 g_string_append (gstr, ", ");
70               g_string_append (gstr, mv[i]);
71               g_free (mv[i]);
72             }
73           s = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
74           g_string_free (gstr, TRUE);
75         }
76       else
77         {
78           GString *gstr = g_string_sized_new (10);
79           gchar *l, *h;
80           union value low, high;
81           mv_get_range (miss, &low.f, &high.f);
82
83           l = value_to_text (low, *fmt);
84           h = value_to_text (high, *fmt);
85
86           g_string_printf (gstr, "%s - %s", l, h);
87           g_free (l);
88           g_free (h);
89
90           if ( mv_has_value (miss))
91             {
92               gchar *ss = 0;
93               union value v;
94               mv_get_value (miss, &v, 0);
95
96               ss = value_to_text (v, *fmt);
97
98               g_string_append (gstr, ", ");
99               g_string_append (gstr, ss);
100               free (ss);
101             }
102           s = pspp_locale_to_utf8 (gstr->str, gstr->len, err);
103           g_string_free (gstr, TRUE);
104         }
105
106       return s;
107     }
108 }