1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013 Free Software Foundation
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.
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.
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/>. */
21 #include "ui/gui/psppire-dialog-action-var-info.h"
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"
36 static void psppire_dialog_action_var_info_init (PsppireDialogActionVarInfo *act);
37 static void psppire_dialog_action_var_info_class_init (PsppireDialogActionVarInfoClass *class);
39 G_DEFINE_TYPE (PsppireDialogActionVarInfo, psppire_dialog_action_var_info, PSPPIRE_TYPE_DIALOG_ACTION);
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
46 static const gchar none[] = N_("None");
50 label_to_string (const struct variable *var)
52 const char *label = var_get_label (var);
54 if (NULL == label) return g_strdup (none);
61 treeview_item_selected (gpointer data)
63 PsppireDialogAction *pda = data;
64 GtkTreeView *tv = GTK_TREE_VIEW (pda->source);
65 GtkTreeSelection *selection = gtk_tree_view_get_selection (tv);
67 return gtk_tree_selection_count_selected_rows (selection) > 0;
71 generate_syntax (PsppireDialogAction *act)
74 struct variable **vars;
81 psppire_dict_view_get_selected_variables (PSPPIRE_DICT_VIEW (act->source),
84 s = g_string_new ("");
86 for (i = 0; i < n_vars; i++)
88 const char *name = var_get_name (vars[i]);
89 size_t name_len = strlen (name);
93 if (line_len + 1 + name_len > 69)
95 g_string_append_c (s, '\n');
100 g_string_append_c (s, ' ');
105 g_string_append (s, name);
106 line_len += name_len;
112 g_string_free (s, FALSE);
117 populate_text_var (GString *gstring, const struct variable *var)
121 g_string_append (gstring, var_get_name (var));
122 g_string_append (gstring, "\n");
125 g_string_append_printf (gstring, _("Label: %s\n"), label_to_string (var));
127 const struct fmt_spec *fmt = var_get_print_format (var);
128 char buffer[FMT_STRING_LEN_MAX + 1];
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);
135 text = missing_values_to_string (var, NULL);
136 g_string_append_printf (gstring, _("Missing Values: %s\n"),
140 g_string_append_printf (gstring, _("Measurement Level: %s\n"),
141 measure_to_string (var_get_measure (var)));
145 if ( var_has_value_labels (var))
147 const struct val_labs *vls = var_get_value_labels (var);
148 const struct val_lab **labels;
152 g_string_append (gstring, "\n");
153 g_string_append (gstring, _("Value Labels:\n"));
155 labels = val_labs_sorted (vls);
156 n_labels = val_labs_count (vls);
157 for (i = 0; i < n_labels; i++)
159 const struct val_lab *vl = labels[i];
160 gchar *const vstr = value_to_text (vl->value, var);
162 g_string_append_printf (gstring, _("%s %s\n"),
163 vstr, val_lab_get_escaped_label (vl));
173 populate_text (GtkTreeSelection *selection, gpointer data)
175 GtkTreeView *treeview = gtk_tree_selection_get_tree_view (selection);
181 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data));
182 struct variable **vars;
184 g_object_get (treeview, "model", &dict,
187 gstring = g_string_sized_new (200);
189 psppire_dict_view_get_selected_variables (PSPPIRE_DICT_VIEW (treeview),
191 for (i = 0; i < n_vars; i++)
194 g_string_append_c (gstring, '\n');
195 populate_text_var (gstring, vars[i]);
199 gtk_text_buffer_set_text (textbuffer, gstring->str, gstring->len);
201 g_string_free (gstring, TRUE);
206 jump_to (PsppireDialog *d, gint response, gpointer data)
208 PsppireDataWindow *dw;
209 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (data);
210 struct variable **vars;
213 if (response != PSPPIRE_RESPONSE_GOTO)
216 psppire_dict_view_get_selected_variables (PSPPIRE_DICT_VIEW (pda->source),
220 g_object_get (pda, "top-level", &dw, NULL);
222 psppire_data_editor_goto_variable (dw->data_editor,
223 var_get_dict_index (vars[0]));
229 psppire_dialog_action_var_info_activate (GtkAction *a)
231 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
233 GtkBuilder *xml = builder_new ("variable-info.ui");
234 GtkWidget *textview = get_widget_assert (xml, "textview1");
236 pda->dialog = get_widget_assert (xml, "variable-info-dialog");
237 pda->source = get_widget_assert (xml, "treeview2");
239 g_object_set (pda->source,
240 "selection-mode", GTK_SELECTION_MULTIPLE,
243 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (pda->source)),
244 "changed", G_CALLBACK (populate_text),
248 g_signal_connect (pda->dialog, "response", G_CALLBACK (jump_to),
251 psppire_dialog_action_set_valid_predicate (pda,
252 treeview_item_selected);
254 g_object_unref (xml);
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);
261 psppire_dialog_action_var_info_class_init (PsppireDialogActionVarInfoClass *class)
263 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
265 action_class->activate = psppire_dialog_action_var_info_activate;
266 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
271 psppire_dialog_action_var_info_init (PsppireDialogActionVarInfo *act)