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 dialog_state_valid (gpointer data)
52 struct chisquare_dialog *csd = data;
55 gtk_tree_view_get_model (GTK_TREE_VIEW (csd->var_view));
59 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
67 refresh (struct chisquare_dialog *csd)
69 GtkTreeModel *liststore =
70 gtk_tree_view_get_model (GTK_TREE_VIEW (csd->var_view));
72 gtk_list_store_clear (GTK_LIST_STORE (liststore));
74 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button1), TRUE);
76 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button2), TRUE);
82 generate_syntax (const struct chisquare_dialog *scd)
87 ds_init_cstr (&dss, "NPAR TEST\n\t/CHISQUARE=");
89 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (scd->var_view), 0, &dss);
91 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->range_button)))
93 ds_put_cstr (&dss, "(");
96 gtk_entry_get_text (GTK_ENTRY (scd->value_lower)));
98 ds_put_cstr (&dss, ", ");
101 gtk_entry_get_text (GTK_ENTRY (scd->value_upper)));
103 ds_put_cstr (&dss, ")");
106 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->values_button)))
108 GtkListStore *ls = scd->expected_list;
112 ds_put_cstr (&dss, "\n\t");
113 ds_put_cstr (&dss, "/EXPECTED = ");
116 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
119 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
123 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
125 ds_put_c_format (&dss, " %g", v);
129 ds_put_cstr (&dss, ".\n");
131 text = ds_steal_cstr (&dss);
140 /* Pops up the Chi-Square dialog box */
142 chisquare_dialog (PsppireDataWindow *dw)
146 struct chisquare_dialog csd;
148 GtkBuilder *xml = builder_new ("chi-square.ui");
150 GtkWidget *dialog = get_widget_assert (xml, "chisquare-dialog");
152 GtkWidget *range_table = get_widget_assert (xml, "range-table");
156 GtkWidget *values_acr = get_widget_assert (xml, "psppire-acr1");
157 GtkWidget *expected_value_entry =
158 get_widget_assert (xml, "expected-value-entry");
161 GtkWidget *dict_view = get_widget_assert (xml, "dict-view");
163 csd.expected_list = gtk_list_store_new (1, G_TYPE_DOUBLE);
165 csd.button1 = get_widget_assert (xml, "radiobutton1");
166 csd.button2 = get_widget_assert (xml, "radiobutton3");
167 csd.var_view = get_widget_assert (xml, "variables-treeview");
169 csd.range_button = get_widget_assert (xml, "radiobutton4");
170 csd.value_lower = get_widget_assert (xml, "entry1");
171 csd.value_upper = get_widget_assert (xml, "entry2");
173 csd.values_button = get_widget_assert (xml, "radiobutton2");
175 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (dw));
178 g_object_get (dw->data_editor, "dictionary", &csd.dict, NULL);
179 g_object_set (dict_view,
181 "predicate", var_is_numeric,
185 g_signal_connect (csd.range_button, "toggled", G_CALLBACK (set_sensitivity_from_toggle),
189 g_signal_connect (csd.values_button, "toggled", G_CALLBACK (set_sensitivity_from_toggle),
192 g_signal_connect (csd.values_button, "toggled", G_CALLBACK (set_sensitivity_from_toggle),
193 expected_value_entry);
196 psppire_acr_set_entry (PSPPIRE_ACR (values_acr),
197 GTK_ENTRY (expected_value_entry));
199 psppire_acr_set_model(PSPPIRE_ACR (values_acr), csd.expected_list);
201 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &csd);
203 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
204 dialog_state_valid, &csd);
206 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
211 case GTK_RESPONSE_OK:
212 g_free (execute_syntax_string (dw, generate_syntax (&csd)));
214 case PSPPIRE_RESPONSE_PASTE:
215 g_free (paste_syntax_to_window (generate_syntax (&csd)));
221 g_object_unref (csd.expected_list);
222 g_object_unref (xml);