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