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