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