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