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