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