Added a dict parameter to data_in and dealt with the consequences.
[pspp-builds.git] / src / ui / gui / missing-val-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2005, 2006, 2009  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 /*  This module describes the behaviour of the Missing Values dialog box,
18     used for input of the missing values in the variable sheet */
19
20 #include <config.h>
21 #include <gettext.h>
22 #define _(msgid) gettext (msgid)
23 #define N_(msgid) msgid
24
25
26 #include "helper.h"
27 #include <data/format.h>
28 #include "missing-val-dialog.h"
29 #include <data/missing-values.h>
30 #include <data/variable.h>
31 #include <data/data-in.h>
32
33
34 #include <gtk/gtk.h>
35
36 #include <string.h>
37
38
39 /* A simple (sub) dialog box for displaying user input errors */
40 static void
41 err_dialog (const gchar *msg, GtkWindow *window)
42 {
43   GtkWidget *hbox ;
44   GtkWidget *label = gtk_label_new (msg);
45
46   GtkWidget *dialog =
47     gtk_dialog_new_with_buttons ("PSPP",
48                                  window,
49                                  GTK_DIALOG_MODAL |
50                                  GTK_DIALOG_DESTROY_WITH_PARENT |
51                                  GTK_DIALOG_NO_SEPARATOR,
52                                  GTK_STOCK_OK,
53                                  GTK_RESPONSE_ACCEPT,
54                                  NULL);
55
56
57   GtkWidget *icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR,
58                                              GTK_ICON_SIZE_DIALOG);
59
60   g_signal_connect_swapped (dialog,
61                             "response",
62                             G_CALLBACK (gtk_widget_destroy),
63                             dialog);
64
65   hbox = gtk_hbox_new (FALSE, 10);
66
67   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
68                      hbox);
69
70   gtk_box_pack_start (GTK_BOX (hbox), icon, TRUE, FALSE, 10);
71   gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 10);
72
73   gtk_widget_show_all (dialog);
74 }
75
76
77 /* Callback which occurs when the OK button is clicked */
78 static void
79 missing_val_dialog_accept (GtkWidget *w, gpointer data)
80 {
81   struct missing_val_dialog *dialog = data;
82
83   const struct fmt_spec *write_spec = var_get_write_format (dialog->pv);
84
85   if ( gtk_toggle_button_get_active (dialog->button_discrete))
86     {
87       gint nvals = 0;
88       gint badvals = 0;
89       gint i;
90       mv_clear(&dialog->mvl);
91       for(i = 0 ; i < 3 ; ++i )
92         {
93           gchar *text =
94             g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->mv[i])));
95
96           union value v;
97           if ( !text || strlen (g_strstrip (text)) == 0 )
98             {
99               g_free (text);
100               continue;
101             }
102
103           if ( text_to_value (text, &v, 
104                               dialog->dict, *write_spec))
105             {
106               nvals++;
107               mv_add_value (&dialog->mvl, &v);
108             }
109           else
110               badvals++;
111           g_free (text);
112         }
113       if ( nvals == 0 || badvals > 0 )
114         {
115           err_dialog (_("Incorrect value for variable type"),
116                      GTK_WINDOW (dialog->window));
117           return ;
118         }
119     }
120
121   if (gtk_toggle_button_get_active (dialog->button_range))
122     {
123       gchar *discrete_text ;
124
125       union value low_val ;
126       union value high_val;
127       const gchar *low_text = gtk_entry_get_text (GTK_ENTRY (dialog->low));
128       const gchar *high_text = gtk_entry_get_text (GTK_ENTRY (dialog->high));
129
130       if ( text_to_value (low_text, &low_val, dialog->dict, *write_spec)
131            &&
132            text_to_value (high_text, &high_val, dialog->dict, *write_spec) )
133         {
134           if ( low_val.f > high_val.f )
135             {
136               err_dialog (_("Incorrect range specification"),
137                           GTK_WINDOW (dialog->window));
138               return ;
139             }
140         }
141       else
142         {
143           err_dialog (_("Incorrect range specification"),
144                       GTK_WINDOW (dialog->window));
145           return;
146         }
147
148       discrete_text =
149         g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->discrete)));
150
151       mv_clear (&dialog->mvl);
152       mv_add_range (&dialog->mvl, low_val.f, high_val.f);
153
154       if ( discrete_text && strlen (g_strstrip (discrete_text)) > 0 )
155         {
156           union value discrete_val;
157           if ( !text_to_value (discrete_text, &discrete_val,
158                                dialog->dict,
159                               *write_spec))
160             {
161               err_dialog (_("Incorrect value for variable type"),
162                          GTK_WINDOW (dialog->window) );
163               g_free (discrete_text);
164               return;
165             }
166           mv_add_value (&dialog->mvl, &discrete_val);
167         }
168       g_free (discrete_text);
169     }
170
171
172   if (gtk_toggle_button_get_active (dialog->button_none))
173     mv_clear (&dialog->mvl);
174
175   var_set_missing_values (dialog->pv, &dialog->mvl);
176
177   gtk_widget_hide (dialog->window);
178 }
179
180
181 /* Callback which occurs when the 'discrete' radiobutton is toggled */
182 static void
183 discrete (GtkToggleButton *button, gpointer data)
184 {
185   gint i;
186   struct missing_val_dialog *dialog = data;
187
188   for (i = 0 ; i < 3 ; ++i )
189     {
190       gtk_widget_set_sensitive (dialog->mv[i],
191                                gtk_toggle_button_get_active (button));
192     }
193 }
194
195 /* Callback which occurs when the 'range' radiobutton is toggled */
196 static void
197 range (GtkToggleButton *button, gpointer data)
198 {
199   struct missing_val_dialog *dialog = data;
200
201   const gboolean active = gtk_toggle_button_get_active (button);
202
203   gtk_widget_set_sensitive (dialog->low, active);
204   gtk_widget_set_sensitive (dialog->high, active);
205   gtk_widget_set_sensitive (dialog->discrete, active);
206 }
207
208
209
210 /* Callback for when the Missing Value dialog is closed using
211    the window delete button.*/
212 static gint
213 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
214 {
215   struct missing_val_dialog *dialog = data;
216
217   gtk_widget_hide (dialog->window);
218
219   return TRUE;
220 }
221
222
223 /* Creates the dialog structure */
224 struct missing_val_dialog *
225 missing_val_dialog_create (GtkWindow *toplevel)
226 {
227   GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
228
229   struct missing_val_dialog *dialog = g_malloc (sizeof (*dialog));
230
231   dialog->window = get_widget_assert (xml, "missing_values_dialog");
232
233   gtk_window_set_transient_for
234     (GTK_WINDOW (dialog->window), toplevel);
235
236   g_signal_connect_swapped (get_widget_assert (xml, "missing_val_cancel"),
237                    "clicked", G_CALLBACK (gtk_widget_hide), dialog->window);
238
239   g_signal_connect (get_widget_assert (xml, "missing_val_ok"),
240                    "clicked", G_CALLBACK (missing_val_dialog_accept), dialog);
241
242   g_signal_connect (dialog->window, "delete-event",
243                     G_CALLBACK (on_delete), dialog);
244
245   dialog->mv[0] = get_widget_assert (xml, "mv0");
246   dialog->mv[1] = get_widget_assert (xml, "mv1");
247   dialog->mv[2] = get_widget_assert (xml, "mv2");
248
249   dialog->low = get_widget_assert (xml, "mv-low");
250   dialog->high = get_widget_assert (xml, "mv-high");
251   dialog->discrete = get_widget_assert (xml, "mv-discrete");
252
253
254   dialog->button_none     =
255     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "no_missing"));
256
257   dialog->button_discrete =
258     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "discrete_missing"));
259
260   dialog->button_range    =
261     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "range_missing"));
262
263
264   g_signal_connect (dialog->button_discrete, "toggled",
265                    G_CALLBACK (discrete), dialog);
266
267   g_signal_connect (dialog->button_range, "toggled",
268                    G_CALLBACK (range), dialog);
269
270   g_object_unref (xml);
271
272   return dialog;
273 }
274
275 /* Shows the dialog box and sets default values */
276 void
277 missing_val_dialog_show (struct missing_val_dialog *dialog)
278 {
279   const struct fmt_spec *write_spec ;
280
281   gint i;
282   g_return_if_fail (dialog);
283   g_return_if_fail (dialog->pv);
284
285   mv_copy (&dialog->mvl, var_get_missing_values (dialog->pv));
286
287   write_spec = var_get_write_format (dialog->pv);
288
289   /* Blank all entry boxes and make them insensitive */
290   gtk_entry_set_text (GTK_ENTRY (dialog->low), "");
291   gtk_entry_set_text (GTK_ENTRY (dialog->high), "");
292   gtk_entry_set_text (GTK_ENTRY (dialog->discrete), "");
293   gtk_widget_set_sensitive (dialog->low, FALSE);
294   gtk_widget_set_sensitive (dialog->high, FALSE);
295   gtk_widget_set_sensitive (dialog->discrete, FALSE);
296
297   gtk_widget_set_sensitive (GTK_WIDGET (dialog->button_range),
298                            var_is_numeric (dialog->pv));
299
300
301   for (i = 0 ; i < 3 ; ++i )
302     {
303       gtk_entry_set_text (GTK_ENTRY (dialog->mv[i]), "");
304       gtk_widget_set_sensitive (dialog->mv[i], FALSE);
305     }
306
307   if ( mv_has_range (&dialog->mvl))
308     {
309       union value low, high;
310       gchar *low_text;
311       gchar *high_text;
312       mv_get_range (&dialog->mvl, &low.f, &high.f);
313
314
315       low_text = value_to_text (low, dialog->dict, *write_spec);
316       high_text = value_to_text (high, dialog->dict,  *write_spec);
317
318       gtk_entry_set_text (GTK_ENTRY (dialog->low), low_text);
319       gtk_entry_set_text (GTK_ENTRY (dialog->high), high_text);
320       g_free (low_text);
321       g_free (high_text);
322
323       if ( mv_has_value (&dialog->mvl))
324         {
325           gchar *text;
326           text = value_to_text (*mv_get_value (&dialog->mvl, 0), dialog->dict, *write_spec);
327           gtk_entry_set_text (GTK_ENTRY (dialog->discrete), text);
328           g_free (text);
329         }
330
331       gtk_toggle_button_set_active (dialog->button_range, TRUE);
332       gtk_widget_set_sensitive (dialog->low, TRUE);
333       gtk_widget_set_sensitive (dialog->high, TRUE);
334       gtk_widget_set_sensitive (dialog->discrete, TRUE);
335
336     }
337   else if ( mv_has_value (&dialog->mvl))
338     {
339       const int n = mv_n_values (&dialog->mvl);
340
341       for (i = 0 ; i < 3 ; ++i )
342         {
343           if ( i < n)
344             {
345               gchar *text ;
346
347               text = value_to_text (*mv_get_value (&dialog->mvl, i), dialog->dict,
348                                     *write_spec);
349               gtk_entry_set_text (GTK_ENTRY (dialog->mv[i]), text);
350               g_free (text);
351             }
352           gtk_widget_set_sensitive (dialog->mv[i], TRUE);
353         }
354       gtk_toggle_button_set_active (dialog->button_discrete, TRUE);
355     }
356   else if ( mv_is_empty (&dialog->mvl))
357     {
358       gtk_toggle_button_set_active (dialog->button_none, TRUE);
359     }
360
361   gtk_widget_show (dialog->window);
362 }