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