1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2005, 2006 Free Software Foundation
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.
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.
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/>. */
17 /* This module describes the behaviour of the Missing Values dialog box,
18 used for input of the missing values in the variable sheet */
22 #define _(msgid) gettext (msgid)
23 #define N_(msgid) msgid
27 #include "missing-val-dialog.h"
28 #include <data/missing-values.h>
29 #include <data/variable.h>
30 #include <data/data-in.h>
34 #include <glade/glade.h>
39 /* A simple (sub) dialog box for displaying user input errors */
41 err_dialog (const gchar *msg, GtkWindow *window)
44 GtkWidget *label = gtk_label_new (msg);
47 gtk_dialog_new_with_buttons ("PSPP",
50 GTK_DIALOG_DESTROY_WITH_PARENT |
51 GTK_DIALOG_NO_SEPARATOR,
57 GtkWidget *icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR,
58 GTK_ICON_SIZE_DIALOG);
60 g_signal_connect_swapped (dialog,
62 G_CALLBACK (gtk_widget_destroy),
65 hbox = gtk_hbox_new (FALSE, 10);
67 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
70 gtk_box_pack_start (GTK_BOX (hbox), icon, TRUE, FALSE, 10);
71 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 10);
73 gtk_widget_show_all (dialog);
77 /* Callback which occurs when the OK button is clicked */
79 missing_val_dialog_accept (GtkWidget *w, gpointer data)
81 struct missing_val_dialog *dialog = data;
83 const struct fmt_spec *write_spec = var_get_write_format (dialog->pv);
85 if ( gtk_toggle_button_get_active (dialog->button_discrete))
90 mv_clear(&dialog->mvl);
91 for(i = 0 ; i < 3 ; ++i )
94 g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->mv[i])));
97 if ( !text || strlen (g_strstrip (text)) == 0 )
103 if ( text_to_value (text, &v, *write_spec))
106 mv_add_value (&dialog->mvl, &v);
112 if ( nvals == 0 || badvals > 0 )
114 err_dialog (_("Incorrect value for variable type"),
115 GTK_WINDOW (dialog->window));
120 if (gtk_toggle_button_get_active (dialog->button_range))
122 gchar *discrete_text ;
124 union value low_val ;
125 union value high_val;
126 const gchar *low_text = gtk_entry_get_text (GTK_ENTRY (dialog->low));
127 const gchar *high_text = gtk_entry_get_text (GTK_ENTRY (dialog->high));
129 if ( text_to_value (low_text, &low_val, *write_spec)
131 text_to_value (high_text, &high_val, *write_spec) )
133 if ( low_val.f > high_val.f )
135 err_dialog (_("Incorrect range specification"),
136 GTK_WINDOW (dialog->window));
142 err_dialog (_("Incorrect range specification"),
143 GTK_WINDOW (dialog->window));
148 g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->discrete)));
150 mv_clear (&dialog->mvl);
151 mv_add_num_range (&dialog->mvl, low_val.f, high_val.f);
153 if ( discrete_text && strlen (g_strstrip (discrete_text)) > 0 )
155 union value discrete_val;
156 if ( !text_to_value (discrete_text, &discrete_val,
159 err_dialog (_("Incorrect value for variable type"),
160 GTK_WINDOW (dialog->window) );
161 g_free (discrete_text);
164 mv_add_value (&dialog->mvl, &discrete_val);
166 g_free (discrete_text);
170 if (gtk_toggle_button_get_active (dialog->button_none))
171 mv_clear (&dialog->mvl);
173 var_set_missing_values (dialog->pv, &dialog->mvl);
175 gtk_widget_hide (dialog->window);
179 /* Callback which occurs when the 'discrete' radiobutton is toggled */
181 discrete (GtkToggleButton *button, gpointer data)
184 struct missing_val_dialog *dialog = data;
186 for (i = 0 ; i < 3 ; ++i )
188 gtk_widget_set_sensitive (dialog->mv[i],
189 gtk_toggle_button_get_active (button));
193 /* Callback which occurs when the 'range' radiobutton is toggled */
195 range (GtkToggleButton *button, gpointer data)
197 struct missing_val_dialog *dialog = data;
199 const gboolean active = gtk_toggle_button_get_active (button);
201 gtk_widget_set_sensitive (dialog->low, active);
202 gtk_widget_set_sensitive (dialog->high, active);
203 gtk_widget_set_sensitive (dialog->discrete, active);
208 /* Callback for when the Missing Value dialog is closed using
209 the window delete button.*/
211 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
213 struct missing_val_dialog *dialog = data;
215 gtk_widget_hide (dialog->window);
221 /* Creates the dialog structure from the xml */
222 struct missing_val_dialog *
223 missing_val_dialog_create (GladeXML *xml)
225 struct missing_val_dialog *dialog = g_malloc (sizeof (*dialog));
229 dialog->window = get_widget_assert (xml, "missing_values_dialog");
231 gtk_window_set_transient_for
232 (GTK_WINDOW (dialog->window),
233 GTK_WINDOW (get_widget_assert (xml, "data_editor")));
236 g_signal_connect_swapped (get_widget_assert (xml, "missing_val_cancel"),
237 "clicked", G_CALLBACK (gtk_widget_hide), dialog->window);
239 g_signal_connect (get_widget_assert (xml, "missing_val_ok"),
240 "clicked", G_CALLBACK (missing_val_dialog_accept), dialog);
242 g_signal_connect (GTK_OBJECT (dialog->window), "delete-event",
243 G_CALLBACK (on_delete), dialog);
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");
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");
254 dialog->button_none =
255 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "no_missing"));
257 dialog->button_discrete =
258 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "discrete_missing"));
260 dialog->button_range =
261 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "range_missing"));
264 g_signal_connect (G_OBJECT (dialog->button_discrete), "toggled",
265 G_CALLBACK (discrete), dialog);
267 g_signal_connect (G_OBJECT (dialog->button_range), "toggled",
268 G_CALLBACK (range), dialog);
273 /* Shows the dialog box and sets default values */
275 missing_val_dialog_show (struct missing_val_dialog *dialog)
277 const struct fmt_spec *write_spec ;
280 g_return_if_fail (dialog);
281 g_return_if_fail (dialog->pv);
283 mv_copy (&dialog->mvl, var_get_missing_values (dialog->pv));
285 write_spec = var_get_write_format (dialog->pv);
287 /* Blank all entry boxes and make them insensitive */
288 gtk_entry_set_text (GTK_ENTRY (dialog->low), "");
289 gtk_entry_set_text (GTK_ENTRY (dialog->high), "");
290 gtk_entry_set_text (GTK_ENTRY (dialog->discrete), "");
291 gtk_widget_set_sensitive (dialog->low, FALSE);
292 gtk_widget_set_sensitive (dialog->high, FALSE);
293 gtk_widget_set_sensitive (dialog->discrete, FALSE);
295 gtk_widget_set_sensitive (GTK_WIDGET (dialog->button_range),
296 var_is_numeric (dialog->pv));
299 for (i = 0 ; i < 3 ; ++i )
301 gtk_entry_set_text (GTK_ENTRY (dialog->mv[i]), "");
302 gtk_widget_set_sensitive (dialog->mv[i], FALSE);
305 if ( mv_has_range (&dialog->mvl))
307 union value low, high;
310 mv_peek_range (&dialog->mvl, &low.f, &high.f);
312 low_text = value_to_text (low, *write_spec);
313 high_text = value_to_text (high, *write_spec);
315 gtk_entry_set_text (GTK_ENTRY (dialog->low), low_text);
316 gtk_entry_set_text (GTK_ENTRY (dialog->high), high_text);
320 if ( mv_has_value (&dialog->mvl))
324 mv_peek_value (&dialog->mvl, &value, 0);
325 text = value_to_text (value, *write_spec);
326 gtk_entry_set_text (GTK_ENTRY (dialog->discrete), text);
330 gtk_toggle_button_set_active (dialog->button_range, TRUE);
331 gtk_widget_set_sensitive (dialog->low, TRUE);
332 gtk_widget_set_sensitive (dialog->high, TRUE);
333 gtk_widget_set_sensitive (dialog->discrete, TRUE);
336 else if ( mv_has_value (&dialog->mvl))
338 const int n = mv_n_values (&dialog->mvl);
340 for (i = 0 ; i < 3 ; ++i )
347 mv_peek_value (&dialog->mvl, &value, i);
348 text = value_to_text (value, *write_spec);
349 gtk_entry_set_text (GTK_ENTRY (dialog->mv[i]), text);
352 gtk_widget_set_sensitive (dialog->mv[i], TRUE);
354 gtk_toggle_button_set_active (dialog->button_discrete, TRUE);
356 else if ( mv_is_empty (&dialog->mvl))
358 gtk_toggle_button_set_active (dialog->button_none, TRUE);
361 gtk_widget_show (dialog->window);