Added the Independent Samples T Test dialog
[pspp-builds.git] / src / ui / gui / t-test-independent-samples-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
18
19 #include <glade/glade.h>
20 #include <gtk/gtk.h>
21 #include "t-test-independent-samples-dialog.h"
22 #include "psppire-dict.h"
23 #include "psppire-var-store.h"
24 #include "helper.h"
25 #include <gtksheet/gtksheet.h>
26 #include "data-editor.h"
27 #include "psppire-dialog.h"
28 #include "dialog-common.h"
29 #include "dict-display.h"
30 #include "widget-io.h"
31
32 #include <language/syntax-string-source.h>
33 #include "syntax-editor.h"
34
35 #include <gettext.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
39
40 struct tt_indep_samples_dialog
41 {
42   GladeXML *xml;  /* The xml that generated the widgets */
43   PsppireDict *dict;
44   gboolean groups_defined;
45   gboolean non_default_options;
46   gdouble confidence_interval;
47 };
48
49
50 static gchar *
51 generate_syntax (const struct tt_indep_samples_dialog *d)
52 {
53   gchar *text;
54   GtkWidget *entry =
55     get_widget_assert (d->xml, "indep-samples-t-test-entry");
56
57   GtkWidget *tv =
58     get_widget_assert (d->xml, "indep-samples-t-test-treeview2");
59
60   GString *str = g_string_new ("T-TEST /VARIABLES=");
61
62   append_variable_names (str, d->dict, GTK_TREE_VIEW (tv));
63
64   g_string_append (str, "\n\t/GROUPS=");
65
66   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (entry)));
67
68   if ( d->groups_defined )
69     {
70       GtkWidget *entry1 = get_widget_assert (d->xml, "group1-entry");
71       GtkWidget *entry2 = get_widget_assert (d->xml, "group2-entry");
72
73       g_string_append (str, "(");
74       g_string_append (str, gtk_entry_get_text (GTK_ENTRY (entry1)));
75       g_string_append (str, ",");
76       g_string_append (str, gtk_entry_get_text (GTK_ENTRY (entry2)));
77       g_string_append (str, ")");
78     }
79
80   if ( d->non_default_options )
81     {
82       GtkToggleButton *analysis =
83         GTK_TOGGLE_BUTTON (get_widget_assert (d->xml, "radiobutton1"));
84
85       g_string_append (str, "\n\t");
86       g_string_append_printf (str, "/CRITERIA=CIN(%g)",
87                               d->confidence_interval/100.0);
88
89
90       g_string_append (str, "\n\t");
91       g_string_append_printf (str, "/MISSING=%s",
92                        gtk_toggle_button_get_active (analysis) ?
93                        "ANALYSIS" : "LISTWISE");
94     }
95
96   g_string_append (str, ".\n");
97
98   text = str->str;
99
100   g_string_free (str, FALSE);
101
102   return text;
103 }
104
105
106
107 static void
108 refresh (GladeXML *xml)
109 {
110   GtkWidget *entry =
111     get_widget_assert (xml, "indep-samples-t-test-entry");
112
113   GtkWidget *tv =
114     get_widget_assert (xml, "indep-samples-t-test-treeview2");
115
116   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (tv));
117
118   gtk_entry_set_text (GTK_ENTRY (entry), "");
119
120   gtk_list_store_clear (GTK_LIST_STORE (model));
121 }
122
123
124 static gboolean
125 define_groups_state_valid (gpointer data)
126 {
127   struct tt_indep_samples_dialog *d = data;
128
129   GtkWidget *entry1 = get_widget_assert (d->xml, "group1-entry");
130   GtkWidget *entry2 = get_widget_assert (d->xml, "group2-entry");
131
132   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry1))))
133     return FALSE;
134
135   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry2))))
136     return FALSE;
137
138   return TRUE;
139 }
140
141 static void
142 run_define_groups (struct tt_indep_samples_dialog *ttd)
143 {
144   gint response;
145   GtkWidget *dialog =
146     get_widget_assert (ttd->xml, "define-groups-dialog");
147
148
149   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
150                                       define_groups_state_valid, ttd);
151
152   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
153
154   ttd->groups_defined = (response == PSPPIRE_RESPONSE_CONTINUE);
155 }
156
157
158 static void
159 run_options (struct tt_indep_samples_dialog *ttd)
160 {
161   gint response;
162   GtkWidget *dialog =
163     get_widget_assert (ttd->xml, "options-dialog");
164
165   GtkWidget *box =
166     get_widget_assert (ttd->xml, "vbox1");
167
168   GtkSpinButton *conf_percent = NULL;
169
170   GtkWidget *confidence =
171     widget_scanf (_("Confidence Interval: %2d %%"),
172                   &conf_percent);
173
174   gtk_spin_button_set_value (conf_percent, ttd->confidence_interval);
175
176   gtk_widget_show (confidence);
177
178   gtk_box_pack_start_defaults (GTK_BOX (box), confidence);
179
180   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
181
182   if ( response == PSPPIRE_RESPONSE_CONTINUE)
183     {
184       ttd->non_default_options = TRUE;
185       ttd->confidence_interval = gtk_spin_button_get_value (conf_percent);
186     }
187
188   gtk_container_remove (GTK_CONTAINER (box), confidence);
189 }
190
191
192
193
194 static gboolean
195 dialog_state_valid (gpointer data)
196 {
197   struct tt_indep_samples_dialog *tt_d = data;
198
199   GtkWidget *entry =
200     get_widget_assert (tt_d->xml, "indep-samples-t-test-entry");
201
202   GtkWidget *tv_vars =
203     get_widget_assert (tt_d->xml, "indep-samples-t-test-treeview2");
204
205   GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (tv_vars));
206
207   GtkTreeIter notused;
208
209   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))))
210     return FALSE;
211
212
213   if ( 0 == gtk_tree_model_get_iter_first (vars, &notused))
214     return FALSE;
215
216   return TRUE;
217 }
218
219
220 /* Pops up the dialog box */
221 void
222 t_test_independent_samples_dialog (GObject *o, gpointer data)
223 {
224   struct tt_indep_samples_dialog tt_d;
225   gint response;
226   struct data_editor *de = data;
227
228   PsppireVarStore *vs;
229
230   GladeXML *xml = XML_NEW ("t-test.glade");
231
232   GtkWidget *dialog = get_widget_assert (xml,
233                                          "t-test-independent-samples-dialog");
234
235   GtkSheet *var_sheet =
236     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
237
238   GtkWidget *dict_view =
239     get_widget_assert (xml, "indep-samples-t-test-treeview1");
240
241   GtkWidget *test_variables_treeview =
242     get_widget_assert (xml, "indep-samples-t-test-treeview2");
243
244   GtkWidget *selector2 =
245     get_widget_assert (xml, "indep-samples-t-test-selector2");
246
247   GtkWidget *selector1 =
248     get_widget_assert (xml, "indep-samples-t-test-selector1");
249
250
251   GtkWidget *entry =
252     get_widget_assert (xml, "indep-samples-t-test-entry");
253
254   GtkWidget *define_groups_button =
255     get_widget_assert (xml, "define-groups-button");
256
257   GtkWidget *options_button =
258     get_widget_assert (xml, "options-button");
259
260   vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
261
262   tt_d.xml = xml;
263   tt_d.dict = vs->dict;
264   tt_d.groups_defined = FALSE;
265   tt_d.non_default_options = FALSE;
266   tt_d.confidence_interval = 95.0;
267
268   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
269
270   attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
271                                  vs->dict,
272                                  GTK_SELECTION_MULTIPLE, NULL);
273
274   set_dest_model (GTK_TREE_VIEW (test_variables_treeview), vs->dict);
275
276
277   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
278                                  dict_view, test_variables_treeview,
279                                  insert_source_row_into_tree_view,
280                                  NULL);
281
282
283   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
284                                  dict_view, entry,
285                                  insert_source_row_into_entry,
286                                  is_currently_in_entry);
287
288   g_signal_connect_swapped (define_groups_button, "clicked",
289                             G_CALLBACK (run_define_groups), &tt_d);
290
291
292   g_signal_connect_swapped (options_button, "clicked",
293                             G_CALLBACK (run_options), &tt_d);
294
295
296   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  xml);
297
298
299   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
300                                       dialog_state_valid, &tt_d);
301
302   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
303
304   switch (response)
305     {
306     case GTK_RESPONSE_OK:
307       {
308         gchar *syntax = generate_syntax (&tt_d);
309         struct getl_interface *sss = create_syntax_string_source (syntax);
310         execute_syntax (sss);
311
312         g_free (syntax);
313       }
314       break;
315     case PSPPIRE_RESPONSE_PASTE:
316       {
317         gchar *syntax = generate_syntax (&tt_d);
318
319         struct syntax_editor *se =
320           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
321
322         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
323
324         g_free (syntax);
325       }
326       break;
327     default:
328       break;
329     }
330
331
332   g_object_unref (xml);
333 }
334
335