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