178a834c7a0d8357c642997c649f81f9612488b6
[pspp-builds.git] / src / ui / gui / variable-info-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2009  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 #include <gtk/gtk.h>
19
20 #include "var-display.h"
21 #include <data/variable.h>
22 #include <data/format.h>
23 #include <data/value-labels.h>
24 #include "psppire-data-window.h"
25 #include "psppire-dialog.h"
26 #include "psppire-var-store.h"
27 #include "psppire-dictview.h"
28 #include "helper.h"
29
30 #include <language/syntax-string-source.h>
31 #include <libpspp/i18n.h>
32 #include "helper.h"
33
34
35 #include <gettext.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
39
40 static const gchar none[] = N_("None");
41
42
43 static const gchar *
44 label_to_string (const struct variable *var)
45 {
46   const char *label = var_get_label (var);
47
48   if (NULL == label) return g_strdup (none);
49
50   return label;
51 }
52
53
54 static void
55 populate_text (PsppireDictView *treeview, gpointer data)
56 {
57   gchar *text = 0;
58   GString *gstring;
59   PsppireDict *dict;
60
61   GtkTextBuffer *textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data));
62   const struct variable *var =
63     psppire_dict_view_get_selected_variable (treeview);
64
65   if ( var == NULL)
66     return;
67
68   g_object_get (treeview,
69                 "dictionary", &dict,
70                 NULL);
71
72   gstring = g_string_sized_new (200);
73   g_string_assign (gstring, var_get_name (var));
74   g_string_append (gstring, "\n");
75
76
77   g_string_append_printf (gstring, _("Label: %s\n"), label_to_string (var));
78   {
79     const struct fmt_spec *fmt = var_get_print_format (var);
80     char buffer[FMT_STRING_LEN_MAX + 1];
81
82     fmt_to_string (fmt, buffer);
83     /* No conversion necessary.  buffer will always be ascii */
84     g_string_append_printf (gstring, _("Type: %s\n"), buffer);
85   }
86
87   text = missing_values_to_string (dict, var, NULL);
88   g_string_append_printf (gstring, _("Missing Values: %s\n"),
89                           text);
90   g_free (text);
91
92   text = measure_to_string (var, NULL);
93   g_string_append_printf (gstring, _("Measurement Level: %s\n"),
94                           text);
95   g_free (text);
96
97
98
99   /* Value Labels */
100   if ( var_has_value_labels (var))
101     {
102       const struct val_labs *vls = var_get_value_labels (var);
103       const struct val_lab **labels;
104       size_t n_labels;
105       size_t i;
106
107       g_string_append (gstring, "\n");
108       g_string_append (gstring, _("Value Labels:\n"));
109
110       labels = val_labs_sorted (vls);
111       n_labels = val_labs_count (vls);
112       for (i = 0; i < n_labels; i++)
113         {
114           const struct val_lab *vl = labels[i];
115           gchar *const vstr  =
116             value_to_text (vl->value,  dict, *var_get_print_format (var));
117
118           g_string_append_printf (gstring, _("%s %s\n"), vstr, val_lab_get_label (vl));
119
120           g_free (vstr);
121         }
122       free (labels);
123     }
124
125   gtk_text_buffer_set_text (textbuffer, gstring->str, gstring->len);
126
127   g_string_free (gstring, TRUE);
128 }
129
130 static gboolean
131 treeview_item_selected (gpointer data)
132 {
133   GtkTreeView *tv = GTK_TREE_VIEW (data);
134   GtkTreeModel *model = gtk_tree_view_get_model (tv);
135
136   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
137
138   if ( n_rows == 0 )
139     return FALSE;
140
141   return TRUE;
142 }
143
144
145 static gchar * generate_syntax (PsppireDictView *treeview);
146
147
148 void
149 variable_info_dialog (GObject *o, gpointer data)
150 {
151   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
152
153   gint response ;
154
155   GtkBuilder *xml = builder_new ("variable-info-dialog.ui");
156
157   GtkWidget *dialog = get_widget_assert (xml, "variable-info-dialog");
158   GtkWidget *treeview = get_widget_assert (xml, "treeview2");
159   GtkWidget *textview = get_widget_assert (xml, "textview1");
160
161   PsppireVarStore *vs = NULL;
162
163   g_object_get (de->data_editor, "var-store", &vs, NULL);
164
165   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
166
167   g_object_set (treeview,
168                 "dictionary", vs->dict,
169                 "selection-mode", GTK_SELECTION_SINGLE,
170                 NULL);
171
172   g_signal_connect (treeview, "cursor-changed", G_CALLBACK (populate_text),
173                     textview);
174
175
176   gtk_text_view_set_indent (GTK_TEXT_VIEW (textview), -5);
177
178   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
179                                       treeview_item_selected, treeview);
180
181   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
182
183   switch (response)
184     {
185     case PSPPIRE_RESPONSE_GOTO:
186       {
187         const struct variable *var =
188           psppire_dict_view_get_selected_variable (PSPPIRE_DICT_VIEW (treeview));
189
190         if ( NULL == var)
191           goto done;
192
193         g_object_set (de->data_editor,
194                       "current-variable", var_get_dict_index (var),
195                       NULL);
196       }
197
198       break;
199     case PSPPIRE_RESPONSE_PASTE:
200       {
201         gchar *syntax = generate_syntax (PSPPIRE_DICT_VIEW (treeview));
202         paste_syntax_in_new_window (syntax);
203
204         g_free (syntax);
205       }
206       break;
207     default:
208       break;
209     }
210
211  done:
212   g_object_unref (xml);
213 }
214
215 static gchar *
216 generate_syntax (PsppireDictView *treeview)
217 {
218   const struct variable *var =
219     psppire_dict_view_get_selected_variable (treeview);
220
221   if ( NULL == var)
222     return g_strdup ("");
223
224   return g_strdup (var_get_name (var));
225 }
226