Merge 'master' into 'psppsheet'.
[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  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   GtkTreeModel *model = gtk_tree_view_get_model (tv);
66
67   gint n_rows = gtk_tree_model_iter_n_children  (model, NULL);
68
69   if ( n_rows == 0 )
70     return FALSE;
71
72   return TRUE;
73 }
74
75 static gchar *
76 generate_syntax (PsppireDialogAction *act)
77
78 {
79   const struct variable *var =
80     psppire_dict_view_get_selected_variable (PSPPIRE_DICT_VIEW (act->source));
81
82   if ( NULL == var)
83     return g_strdup ("");
84
85   return g_strdup (var_get_name (var));
86 }
87
88
89
90 static void
91 populate_text (PsppireDictView *treeview, gpointer data)
92 {
93   gchar *text = NULL;
94   GString *gstring;
95   PsppireDict *dict;
96
97   GtkTextBuffer *textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data));
98   const struct variable *var =
99     psppire_dict_view_get_selected_variable (treeview);
100
101   if ( var == NULL)
102     return;
103
104   g_object_get (treeview, "model", &dict,
105                 NULL);
106
107   gstring = g_string_sized_new (200);
108   g_string_assign (gstring, var_get_name (var));
109   g_string_append (gstring, "\n");
110
111
112   g_string_append_printf (gstring, _("Label: %s\n"), label_to_string (var));
113   {
114     const struct fmt_spec *fmt = var_get_print_format (var);
115     char buffer[FMT_STRING_LEN_MAX + 1];
116
117     fmt_to_string (fmt, buffer);
118     /* No conversion necessary.  buffer will always be ascii */
119     g_string_append_printf (gstring, _("Type: %s\n"), buffer);
120   }
121
122   text = missing_values_to_string (dict, var, NULL);
123   g_string_append_printf (gstring, _("Missing Values: %s\n"),
124                           text);
125   g_free (text);
126
127   g_string_append_printf (gstring, _("Measurement Level: %s\n"),
128                           measure_to_string (var_get_measure (var)));
129
130
131   /* Value Labels */
132   if ( var_has_value_labels (var))
133     {
134       const struct val_labs *vls = var_get_value_labels (var);
135       const struct val_lab **labels;
136       size_t n_labels;
137       size_t i;
138
139       g_string_append (gstring, "\n");
140       g_string_append (gstring, _("Value Labels:\n"));
141
142       labels = val_labs_sorted (vls);
143       n_labels = val_labs_count (vls);
144       for (i = 0; i < n_labels; i++)
145         {
146           const struct val_lab *vl = labels[i];
147           gchar *const vstr  = value_to_text (vl->value,  var);
148
149           g_string_append_printf (gstring, _("%s %s\n"),
150                                   vstr, val_lab_get_escaped_label (vl));
151
152           g_free (vstr);
153         }
154       free (labels);
155     }
156
157   gtk_text_buffer_set_text (textbuffer, gstring->str, gstring->len);
158
159   g_string_free (gstring, TRUE);
160 }
161
162
163 static void
164 jump_to (PsppireDialog *d, gint response, gpointer data)
165 {
166   PsppireDataWindow *dw;
167   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (data);
168   const struct variable *var;
169
170   if (response !=  PSPPIRE_RESPONSE_GOTO)
171     return;
172
173   var = psppire_dict_view_get_selected_variable (PSPPIRE_DICT_VIEW (pda->source));
174
175   if ( NULL == var)
176     return;
177
178   g_object_get (pda, "top-level", &dw, NULL);
179
180   psppire_data_editor_goto_variable (dw->data_editor,
181                                      var_get_dict_index (var));
182 }
183
184 static void
185 psppire_dialog_action_var_info_activate (GtkAction *a)
186 {
187   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
188
189   GtkBuilder *xml = builder_new ("variable-info.ui");
190   GtkWidget *textview = get_widget_assert (xml, "textview1");  
191
192   pda->dialog = get_widget_assert (xml, "variable-info-dialog");
193   pda->source = get_widget_assert (xml, "treeview2");
194
195   g_object_set (pda->source,
196                 "selection-mode", GTK_SELECTION_SINGLE,
197                 NULL);
198
199   g_signal_connect (pda->source, "cursor-changed", G_CALLBACK (populate_text),
200                     textview);
201
202
203   g_signal_connect (pda->dialog, "response", G_CALLBACK (jump_to),
204                     pda);
205
206   psppire_dialog_action_set_valid_predicate (pda,
207                                              treeview_item_selected);
208
209   g_object_unref (xml);
210
211   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_var_info_parent_class)->activate)
212     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_var_info_parent_class)->activate (pda);
213 }
214
215 static void
216 psppire_dialog_action_var_info_class_init (PsppireDialogActionVarInfoClass *class)
217 {
218   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
219
220   action_class->activate = psppire_dialog_action_var_info_activate;
221   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
222 }
223
224
225 static void
226 psppire_dialog_action_var_info_init (PsppireDialogActionVarInfo *act)
227 {
228 }