1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2010, 2011, 2012 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/>. */
19 #include "chi-square-dialog.h"
21 #include "psppire-dialog.h"
22 #include "psppire-var-view.h"
23 #include "psppire-acr.h"
24 #include "dialog-common.h"
26 #include "builder-wrapper.h"
32 struct chisquare_dialog
40 GtkWidget *range_button;
41 GtkWidget *value_lower;
42 GtkWidget *value_upper;
44 GtkWidget *values_button;
46 GtkListStore *expected_list;
50 set_sensitivity (GtkToggleButton *button, GtkWidget *w)
52 gboolean state = gtk_toggle_button_get_active (button);
53 gtk_widget_set_sensitive (w, state);
58 dialog_state_valid (gpointer data)
60 struct chisquare_dialog *csd = data;
63 gtk_tree_view_get_model (GTK_TREE_VIEW (csd->var_view));
67 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
75 refresh (struct chisquare_dialog *csd)
77 GtkTreeModel *liststore =
78 gtk_tree_view_get_model (GTK_TREE_VIEW (csd->var_view));
80 gtk_list_store_clear (GTK_LIST_STORE (liststore));
82 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button1), TRUE);
84 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button2), TRUE);
90 generate_syntax (const struct chisquare_dialog *scd)
96 string = g_string_new ("NPAR TEST\n\t/CHISQUARE=");
98 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (scd->var_view), 0, string);
101 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->range_button)))
103 g_string_append (string, "(");
105 g_string_append (string,
106 gtk_entry_get_text (GTK_ENTRY (scd->value_lower)));
108 g_string_append (string, ", ");
110 g_string_append (string,
111 gtk_entry_get_text (GTK_ENTRY (scd->value_upper)));
113 g_string_append (string, ")");
119 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->values_button)))
121 GtkListStore *ls = scd->expected_list;
125 g_string_append (string, "\n\t");
126 g_string_append (string, "/EXPECTED = ");
129 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
132 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
136 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
138 g_string_append_printf (string, " %g", v);
145 g_string_append (string, ".\n");
149 g_string_free (string, FALSE);
156 /* Pops up the Chi-Square dialog box */
158 chisquare_dialog (PsppireDataWindow *dw)
162 struct chisquare_dialog csd;
164 GtkBuilder *xml = builder_new ("chi-square.ui");
167 GtkWidget *dialog = get_widget_assert (xml, "chisquare-dialog");
169 GtkWidget *range_table = get_widget_assert (xml, "range-table");
173 GtkWidget *values_acr = get_widget_assert (xml, "psppire-acr1");
174 GtkWidget *expected_value_entry =
175 get_widget_assert (xml, "expected-value-entry");
178 GtkWidget *dict_view = get_widget_assert (xml, "dict-view");
180 csd.expected_list = gtk_list_store_new (1, G_TYPE_DOUBLE);
182 csd.button1 = get_widget_assert (xml, "radiobutton1");
183 csd.button2 = get_widget_assert (xml, "radiobutton3");
184 csd.var_view = get_widget_assert (xml, "variables-treeview");
186 csd.range_button = get_widget_assert (xml, "radiobutton4");
187 csd.value_lower = get_widget_assert (xml, "entry1");
188 csd.value_upper = get_widget_assert (xml, "entry2");
190 csd.values_button = get_widget_assert (xml, "radiobutton2");
192 g_object_get (dw->data_editor, "var-store", &vs, NULL);
194 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (dw));
197 g_object_get (vs, "dictionary", &csd.dict, NULL);
198 g_object_set (dict_view,
200 "predicate", var_is_numeric,
204 g_signal_connect (csd.range_button, "toggled", G_CALLBACK (set_sensitivity),
208 g_signal_connect (csd.values_button, "toggled", G_CALLBACK (set_sensitivity),
211 g_signal_connect (csd.values_button, "toggled", G_CALLBACK (set_sensitivity),
212 expected_value_entry);
215 psppire_acr_set_entry (PSPPIRE_ACR (values_acr),
216 GTK_ENTRY (expected_value_entry));
218 psppire_acr_set_model(PSPPIRE_ACR (values_acr), csd.expected_list);
220 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &csd);
222 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
223 dialog_state_valid, &csd);
225 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
230 case GTK_RESPONSE_OK:
231 g_free (execute_syntax_string (dw, generate_syntax (&csd)));
233 case PSPPIRE_RESPONSE_PASTE:
234 g_free (paste_syntax_to_window (generate_syntax (&csd)));
240 g_object_unref (csd.expected_list);
241 g_object_unref (xml);