5b60814186151549fc10cecf23f0a6cfe49c9fa4
[pspp] / src / ui / gui / psppire-dialog-action-var-info.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
3
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18
19 #include <config.h>
20
21 #include "ui/gui/psppire-dialog-action-var-info.h"
22
23 #include <gtk/gtk.h>
24
25 #include "data/format.h"
26 #include "data/value-labels.h"
27 #include "data/variable.h"
28 #include "libpspp/i18n.h"
29 #include "ui/gui/builder-wrapper.h"
30 #include "ui/gui/helper.h"
31 #include "ui/gui/psppire-data-window.h"
32 #include "ui/gui/psppire-dialog.h"
33 #include "ui/gui/psppire-dictview.h"
34 #include "ui/gui/var-display.h"
35
36 static void psppire_dialog_action_var_info_init            (PsppireDialogActionVarInfo      *act);
37 static void psppire_dialog_action_var_info_class_init      (PsppireDialogActionVarInfoClass *class);
38
39 G_DEFINE_TYPE (PsppireDialogActionVarInfo, psppire_dialog_action_var_info, PSPPIRE_TYPE_DIALOG_ACTION);
40
41 #include <gettext.h>
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
44
45
46 static const gchar none[] = N_("None");
47
48
49 static const gchar *
50 label_to_string (const struct variable *var)
51 {
52   const char *label = var_get_label (var);
53
54   if (NULL == label) return g_strdup (none);
55
56   return label;
57 }
58
59
60 static gboolean
61 treeview_item_selected (gpointer data)
62 {
63   PsppireDialogAction *pda = data;
64   GtkTreeView *tv = GTK_TREE_VIEW (pda->source);
65   GtkTreeSelection *selection = gtk_tree_view_get_selection (tv);
66
67   return gtk_tree_selection_count_selected_rows (selection) > 0;
68 }
69
70 static gchar *
71 generate_syntax (PsppireDialogAction *act)
72
73 {
74   struct variable **vars;
75   size_t n_vars;
76   size_t line_len;
77   GString *s;
78   char *str;
79   size_t i;
80
81   psppire_dict_view_get_selected_variables (PSPPIRE_DICT_VIEW (act->source),
82                                             &vars, &n_vars);
83
84   s = g_string_new ("");
85   line_len = 0;
86   for (i = 0; i < n_vars; i++)
87     {
88       const char *name = var_get_name (vars[i]);
89       size_t name_len = strlen (name);
90
91       if (line_len > 0)
92         {
93           if (line_len + 1 + name_len > 69)
94             {
95               g_string_append_c (s, '\n');
96               line_len = 0;
97             }
98           else
99             {
100               g_string_append_c (s, ' ');
101               line_len++;
102             }
103         }
104
105       g_string_append (s, name);
106       line_len += name_len;
107     }
108
109   g_free (vars);
110
111   str = s->str;
112   g_string_free (s, FALSE);
113   return str;
114 }
115
116 static void
117 populate_text_var (GString *gstring, const struct variable *var)
118 {
119   gchar *text = NULL;
120
121   g_string_append (gstring, var_get_name (var));
122   g_string_append (gstring, "\n");
123
124
125   g_string_append_printf (gstring, _("Label: %s\n"), label_to_string (var));
126   {
127     const struct fmt_spec *fmt = var_get_print_format (var);
128     char buffer[FMT_STRING_LEN_MAX + 1];
129
130     fmt_to_string (fmt, buffer);
131     /* No conversion necessary.  buffer will always be ascii */
132     g_string_append_printf (gstring, _("Type: %s\n"), buffer);
133   }
134
135   text = missing_values_to_string (var, NULL);
136   g_string_append_printf (gstring, _("Missing Values: %s\n"),
137                           text);
138   g_free (text);
139
140   g_string_append_printf (gstring, _("Measurement Level: %s\n"),
141                           measure_to_string (var_get_measure (var)));
142
143
144   /* Value Labels */
145   if ( var_has_value_labels (var))
146     {
147       const struct val_labs *vls = var_get_value_labels (var);
148       const struct val_lab **labels;
149       size_t n_labels;
150       size_t i;
151
152       g_string_append (gstring, "\n");
153       g_string_append (gstring, _("Value Labels:\n"));
154
155       labels = val_labs_sorted (vls);
156       n_labels = val_labs_count (vls);
157       for (i = 0; i < n_labels; i++)
158         {
159           const struct val_lab *vl = labels[i];
160           gchar *const vstr  = value_to_text (vl->value,  var);
161
162           g_string_append_printf (gstring, _("%s %s\n"),
163                                   vstr, val_lab_get_escaped_label (vl));
164
165           g_free (vstr);
166         }
167       free (labels);
168     }
169 }
170
171
172 static void
173 populate_text (GtkTreeSelection *selection, gpointer data)
174 {
175   GtkTreeView *treeview = gtk_tree_selection_get_tree_view (selection);
176   GString *gstring;
177   PsppireDict *dict;
178   size_t n_vars;
179   size_t i;
180
181   GtkTextBuffer *textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data));
182   struct variable **vars;
183
184   g_object_get (treeview, "model", &dict,
185                 NULL);
186
187   gstring = g_string_sized_new (200);
188
189   psppire_dict_view_get_selected_variables (PSPPIRE_DICT_VIEW (treeview),
190                                             &vars, &n_vars);
191   for (i = 0; i < n_vars; i++)
192     {
193       if (i > 0)
194         g_string_append_c (gstring, '\n');
195       populate_text_var (gstring, vars[i]);
196     }
197   g_free (vars);
198
199   gtk_text_buffer_set_text (textbuffer, gstring->str, gstring->len);
200
201   g_string_free (gstring, TRUE);
202 }
203
204
205 static void
206 jump_to (PsppireDialog *d, gint response, gpointer data)
207 {
208   PsppireDataWindow *dw;
209   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (data);
210   struct variable **vars;
211   size_t n_vars;
212
213   if (response !=  PSPPIRE_RESPONSE_GOTO)
214     return;
215
216   psppire_dict_view_get_selected_variables (PSPPIRE_DICT_VIEW (pda->source),
217                                             &vars, &n_vars);
218   if (n_vars > 0)
219     {
220       g_object_get (pda, "top-level", &dw, NULL);
221
222       psppire_data_editor_goto_variable (dw->data_editor,
223                                          var_get_dict_index (vars[0]));
224     }
225   g_free (vars);
226 }
227
228 static void
229 psppire_dialog_action_var_info_activate (GtkAction *a)
230 {
231   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
232
233   GtkBuilder *xml = builder_new ("variable-info.ui");
234   GtkWidget *textview = get_widget_assert (xml, "textview1");  
235
236   pda->dialog = get_widget_assert (xml, "variable-info-dialog");
237   pda->source = get_widget_assert (xml, "treeview2");
238
239   g_object_set (pda->source,
240                 "selection-mode", GTK_SELECTION_MULTIPLE,
241                 NULL);
242
243   g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (pda->source)),
244                     "changed", G_CALLBACK (populate_text),
245                     textview);
246
247
248   g_signal_connect (pda->dialog, "response", G_CALLBACK (jump_to),
249                     pda);
250
251   psppire_dialog_action_set_valid_predicate (pda,
252                                              treeview_item_selected);
253
254   g_object_unref (xml);
255
256   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_var_info_parent_class)->activate)
257     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_var_info_parent_class)->activate (pda);
258 }
259
260 static void
261 psppire_dialog_action_var_info_class_init (PsppireDialogActionVarInfoClass *class)
262 {
263   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
264
265   action_class->activate = psppire_dialog_action_var_info_activate;
266   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
267 }
268
269
270 static void
271 psppire_dialog_action_var_info_init (PsppireDialogActionVarInfo *act)
272 {
273 }