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