T-TEST Independent Samples Dialog: Converted to a PsppireDialogAction object
[pspp] / src / ui / gui / psppire-dialog-action-indep-samps.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2009, 2010, 2011, 2012  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 #include <config.h>
19
20 #include "psppire-dialog-action-indep-samps.h"
21 #include "psppire-value-entry.h"
22
23 #include "dialog-common.h"
24 #include <ui/syntax-gen.h>
25 #include "psppire-var-view.h"
26
27 #include "t-test-options.h"
28
29 #include "psppire-dialog.h"
30 #include "builder-wrapper.h"
31
32 #include "psppire-dict.h"
33 #include "libpspp/str.h"
34
35 static void
36 psppire_dialog_action_indep_samps_class_init (PsppireDialogActionIndepSampsClass *class);
37
38 G_DEFINE_TYPE (PsppireDialogActionIndepSamps, psppire_dialog_action_indep_samps, PSPPIRE_TYPE_DIALOG_ACTION);
39
40 static gboolean
41 dialog_state_valid (gpointer data)
42 {
43   PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (data);
44
45   GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (act->test_vars_tv));
46
47   GtkTreeIter notused;
48
49   if (NULL == act->grp_var)
50     return FALSE;
51
52   if ( 0 == gtk_tree_model_get_iter_first (vars, &notused))
53     return FALSE;
54
55   if ( act->group_defn == GROUPS_UNDEF)
56     return FALSE;
57
58   return TRUE;
59 }
60
61
62 static void
63 refresh (PsppireDialogAction *da)
64 {
65   PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (da);
66
67   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (act->test_vars_tv));
68
69   act->group_defn = GROUPS_UNDEF;
70
71   if (act->grp_var)
72     {
73       int width = var_get_width (act->grp_var);
74       value_destroy (&act->cut_point, width);
75       value_destroy (&act->grp_val[0], width);
76       value_destroy (&act->grp_val[1], width);
77       act->grp_var = NULL;
78     }
79
80   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[0]), NULL);
81   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[1]), NULL);
82   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_cut_point_entry), NULL);
83
84   gtk_entry_set_text (GTK_ENTRY (act->group_var_entry), "");
85
86   gtk_list_store_clear (GTK_LIST_STORE (model));
87
88   gtk_widget_set_sensitive (act->define_groups_button, FALSE);
89 }
90
91 /* Return TRUE if VE contains a text which is not valid for VAR or if it
92    contains the SYSMIS value */
93 static gboolean
94 value_entry_contains_invalid (PsppireValueEntry *ve, const struct variable *var)
95 {
96   gboolean result = FALSE;
97
98   if (var) 
99     {
100       union value val;
101       const int width = var_get_width (var);
102       value_init (&val, width);
103
104       if ( psppire_value_entry_get_value (ve, &val, width))
105         {
106           if (var_is_value_missing (var, &val, MV_SYSTEM))
107             {
108               result = TRUE;
109             }
110         }
111       else
112         result = TRUE;
113
114       value_destroy (&val, width);
115     }
116
117   return result;
118 }
119
120 /* Returns TRUE iff the define groups subdialog has a
121    state which defines a valid group criterion */
122 static gboolean
123 define_groups_state_valid (gpointer data)
124 {
125   PsppireDialogActionIndepSamps *act = data;
126
127   if (gtk_toggle_button_get_active
128       (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button)))
129     {
130       if (value_entry_contains_invalid (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[0]),
131                                         act->grp_var))
132         return FALSE;
133
134       if (value_entry_contains_invalid (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[1]),
135                                         act->grp_var))
136         return FALSE;
137     }
138   else
139     {
140       if (value_entry_contains_invalid (PSPPIRE_VALUE_ENTRY (act->dg_cut_point_entry),
141                                         act->grp_var))
142         return FALSE;
143     }
144
145   return TRUE;
146 }
147
148
149 static void
150 run_define_groups (PsppireDialogActionIndepSamps *act)
151 {
152   gint response;
153   PsppireDialogAction *da = PSPPIRE_DIALOG_ACTION (act);
154
155   if ( act->dg_table2->parent)
156     gtk_container_remove (GTK_CONTAINER (act->dg_table2->parent), act->dg_table2);
157
158   if ( act->dg_table1->parent)
159     gtk_container_remove (GTK_CONTAINER (act->dg_table1->parent), act->dg_table1);
160
161
162   if ( var_is_numeric (act->grp_var))
163     {
164       gtk_table_attach_defaults (GTK_TABLE (act->dg_table1), act->dg_table2,
165                                  1, 2, 1, 2);
166
167       gtk_container_add (GTK_CONTAINER (act->dg_box), act->dg_table1);
168     }
169   else
170     {
171       gtk_container_add (GTK_CONTAINER (act->dg_box), act->dg_table2);
172       act->group_defn = GROUPS_VALUES;
173     }
174
175
176   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (act->dg_dialog),
177                                       define_groups_state_valid, act);
178
179   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[0]), act->grp_var);
180   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[1]), act->grp_var);
181   psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_cut_point_entry), act->grp_var);
182
183   if ( act->group_defn != GROUPS_CUT_POINT )
184     {
185       gtk_toggle_button_set_active
186         (GTK_TOGGLE_BUTTON (act->dg_cut_point_toggle_button), TRUE);
187
188       gtk_toggle_button_set_active
189         (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button), TRUE);
190     }
191   else
192     {
193       gtk_toggle_button_set_active
194         (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button), TRUE);
195
196       gtk_toggle_button_set_active
197         (GTK_TOGGLE_BUTTON (act->dg_cut_point_toggle_button), TRUE);
198     }
199
200   g_signal_emit_by_name (act->dg_grp_entry[0], "changed");
201   g_signal_emit_by_name (act->dg_grp_entry[1], "changed");
202   g_signal_emit_by_name (act->dg_cut_point_entry, "changed");
203
204   response = psppire_dialog_run (PSPPIRE_DIALOG (act->def_grps_dialog));
205
206   if (response == PSPPIRE_RESPONSE_CONTINUE)
207     {
208       const int width = var_get_width (act->grp_var);
209
210       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button)))
211         {
212           act->group_defn = GROUPS_VALUES;
213
214           psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[0]),
215                                          &act->grp_val[0], width);
216
217           psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[1]),
218                                          &act->grp_val[1], width);
219         }
220       else
221         {
222           act->group_defn = GROUPS_CUT_POINT;
223
224           psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (act->dg_cut_point_entry),
225                                          &act->cut_point, width);
226         }
227
228       psppire_dialog_notify_change (PSPPIRE_DIALOG (da->dialog));
229     }
230 }
231
232 /* Called whenever the group variable entry widget's contents change */
233 static void
234 on_grp_var_change (GtkEntry *entry, PsppireDialogActionIndepSamps *act)
235 {
236   PsppireDialogAction *da = PSPPIRE_DIALOG_ACTION (act);
237   const gchar *text = gtk_entry_get_text (entry);
238
239   const struct variable *v = psppire_dict_lookup_var (da->dict, text);
240
241   gtk_widget_set_sensitive (act->define_groups_button, v != NULL);
242
243   if (act->grp_var)
244     {
245       int width = var_get_width (act->grp_var);
246       value_destroy (&act->cut_point, width);
247       value_destroy (&act->grp_val[0], width);
248       value_destroy (&act->grp_val[1], width);
249     }
250
251   if (v)
252     {
253       const int width = var_get_width (v);
254       value_init (&act->cut_point, width);
255       value_init (&act->grp_val[0], width);
256       value_init (&act->grp_val[1], width);
257
258       if (width == 0)
259         {
260           act->cut_point.f  = SYSMIS;
261           act->grp_val[0].f = SYSMIS;
262           act->grp_val[1].f = SYSMIS;
263         }
264       else
265         {
266           act->cut_point.short_string[0] = '\0';
267           act->grp_val[0].short_string[0] = '\0';
268           act->grp_val[1].short_string[0] = '\0';
269         }
270     }
271
272   act->grp_var = v;
273 }
274
275 static void
276 set_group_criterion_type (GtkToggleButton *button,
277                           PsppireDialogActionIndepSamps *act)
278 {
279   gboolean by_values = gtk_toggle_button_get_active (button);
280
281   gtk_widget_set_sensitive (act->dg_label, by_values);
282   gtk_widget_set_sensitive (act->dg_table2, by_values);
283
284   gtk_widget_set_sensitive (act->dg_hbox1, !by_values);
285 }
286
287
288 static void
289 psppire_dialog_action_indep_samps_activate (GtkAction *a)
290 {
291   PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (a);
292   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
293
294   GtkBuilder *xml = builder_new ("t-test.ui");
295
296   pda->dialog = get_widget_assert (xml,"t-test-independent-samples-dialog"); 
297   pda->source = get_widget_assert (xml, "indep-samples-t-test-treeview1");
298   act->define_groups_button = get_widget_assert (xml, "define-groups-button");
299   act->options_button = get_widget_assert (xml, "indep-samples-t-test-options-button");
300
301   act->def_grps_dialog = get_widget_assert (xml, "define-groups-dialog");
302   act->group_var_entry = get_widget_assert (xml, "indep-samples-t-test-entry");
303   act->test_vars_tv = get_widget_assert (xml, "indep-samples-t-test-treeview2");
304
305   act->dg_dialog = get_widget_assert (xml, "define-groups-dialog");
306   act->dg_grp_entry[0] = get_widget_assert (xml, "group1-entry");
307   act->dg_grp_entry[1] = get_widget_assert (xml, "group2-entry");
308   act->dg_cut_point_entry = get_widget_assert (xml, "cut-point-entry");
309   act->dg_box = get_widget_assert (xml, "dialog-hbox2");
310
311   act->dg_table1 = get_widget_assert (xml, "table1");
312   act->dg_table2 = get_widget_assert (xml, "table2");
313   act->dg_label  = get_widget_assert (xml, "label4");
314   act->dg_hbox1  = get_widget_assert (xml, "hbox1");
315   act->dg_values_toggle_button = get_widget_assert (xml, "radiobutton3");
316   act->dg_cut_point_toggle_button = get_widget_assert (xml, "radiobutton4");
317
318   act->opts = tt_options_dialog_create (GTK_WINDOW (pda->toplevel));
319
320   g_object_ref (act->dg_table1);
321   g_object_ref (act->dg_table2);
322
323   g_signal_connect (act->dg_values_toggle_button, "toggled",
324                     G_CALLBACK (set_group_criterion_type), act);
325
326
327   g_object_unref (xml);
328
329   psppire_dialog_action_set_refresh (pda, refresh);
330
331   psppire_dialog_action_set_valid_predicate (pda,
332                                         dialog_state_valid);
333
334   g_signal_connect_swapped (act->define_groups_button, "clicked",
335                             G_CALLBACK (run_define_groups), act);
336
337   g_signal_connect_swapped (act->options_button, "clicked",
338                             G_CALLBACK (tt_options_dialog_run), act->opts);
339
340
341   g_signal_connect (act->group_var_entry, "changed",
342                     G_CALLBACK (on_grp_var_change), act);
343
344   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_indep_samps_parent_class)->activate)
345     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_indep_samps_parent_class)->activate (pda);
346 }
347
348
349
350 static char *
351 generate_syntax (PsppireDialogAction *a)
352 {
353   PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (a);
354   gchar *text;
355
356   GString *str = g_string_new ("T-TEST /VARIABLES=");
357
358   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (act->test_vars_tv), 0, str);
359
360   g_string_append (str, "\n\t/GROUPS=");
361
362   g_string_append (str, var_get_name (act->grp_var));
363
364   if (act->group_defn != GROUPS_UNDEF)
365     {
366       g_string_append (str, "(");
367
368       {
369         const union value *val = 
370           (act->group_defn == GROUPS_VALUES) ?
371           &act->grp_val[0] :
372           &act->cut_point;
373
374         struct string strx;        
375         ds_init_empty (&strx);
376         syntax_gen_value (&strx, val, var_get_width (act->grp_var),
377                           var_get_print_format (act->grp_var));
378       
379         g_string_append (str, ds_cstr (&strx));
380         ds_destroy (&strx);
381       }
382
383       if (act->group_defn == GROUPS_VALUES)
384         {
385           g_string_append (str, ",");
386
387           {
388             struct string strx;
389             ds_init_empty (&strx);
390             
391             syntax_gen_value (&strx, &act->grp_val[1], var_get_width (act->grp_var),
392                               var_get_print_format (act->grp_var));
393             
394             g_string_append (str, ds_cstr (&strx));
395             ds_destroy (&strx);
396           }
397         }
398
399       g_string_append (str, ")");
400     }
401
402   tt_options_dialog_append_syntax (act->opts, str);
403
404   g_string_append (str, ".\n");
405
406   text = str->str;
407
408   g_string_free (str, FALSE);
409
410   return text;
411 }
412
413 static void
414 psppire_dialog_action_indep_samps_class_init (PsppireDialogActionIndepSampsClass *class)
415 {
416   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
417
418   action_class->activate = psppire_dialog_action_indep_samps_activate;
419
420   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
421 }
422
423
424 static void
425 psppire_dialog_action_indep_samps_init (PsppireDialogActionIndepSamps *act)
426 {
427   act->grp_var = NULL;
428   act->group_defn = GROUPS_UNDEF;
429 }
430