1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009, 2010, 2011, 2012 Free Software Foundation
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.
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.
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/>. */
20 #include "psppire-dialog-action-indep-samps.h"
21 #include "psppire-value-entry.h"
23 #include "dialog-common.h"
24 #include <ui/syntax-gen.h>
25 #include "psppire-var-view.h"
27 #include "t-test-options.h"
29 #include "psppire-dialog.h"
30 #include "builder-wrapper.h"
32 #include "psppire-dict.h"
33 #include "libpspp/str.h"
36 psppire_dialog_action_indep_samps_class_init (PsppireDialogActionIndepSampsClass *class);
38 G_DEFINE_TYPE (PsppireDialogActionIndepSamps, psppire_dialog_action_indep_samps, PSPPIRE_TYPE_DIALOG_ACTION);
41 dialog_state_valid (gpointer data)
43 PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (data);
45 GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (act->test_vars_tv));
49 if (NULL == act->grp_var)
52 if ( 0 == gtk_tree_model_get_iter_first (vars, ¬used))
55 if ( act->group_defn == GROUPS_UNDEF)
63 refresh (PsppireDialogAction *da)
65 PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (da);
67 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (act->test_vars_tv));
69 act->group_defn = GROUPS_UNDEF;
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);
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);
84 gtk_entry_set_text (GTK_ENTRY (act->group_var_entry), "");
86 gtk_list_store_clear (GTK_LIST_STORE (model));
88 gtk_widget_set_sensitive (act->define_groups_button, FALSE);
91 /* Return TRUE if VE contains a text which is not valid for VAR or if it
92 contains the SYSMIS value */
94 value_entry_contains_invalid (PsppireValueEntry *ve, const struct variable *var)
96 gboolean result = FALSE;
101 const int width = var_get_width (var);
102 value_init (&val, width);
104 if ( psppire_value_entry_get_value (ve, &val, width))
106 if (var_is_value_missing (var, &val, MV_SYSTEM))
114 value_destroy (&val, width);
120 /* Returns TRUE iff the define groups subdialog has a
121 state which defines a valid group criterion */
123 define_groups_state_valid (gpointer data)
125 PsppireDialogActionIndepSamps *act = data;
127 if (gtk_toggle_button_get_active
128 (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button)))
130 if (value_entry_contains_invalid (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[0]),
134 if (value_entry_contains_invalid (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[1]),
140 if (value_entry_contains_invalid (PSPPIRE_VALUE_ENTRY (act->dg_cut_point_entry),
150 run_define_groups (PsppireDialogActionIndepSamps *act)
153 PsppireDialogAction *da = PSPPIRE_DIALOG_ACTION (act);
154 GtkWidget *parent1 = gtk_widget_get_parent (act->dg_table1);
155 GtkWidget *parent2 = gtk_widget_get_parent (act->dg_table2);
158 gtk_container_remove (GTK_CONTAINER (parent1), act->dg_table1);
161 gtk_container_remove (GTK_CONTAINER (parent2), act->dg_table2);
163 if ( var_is_numeric (act->grp_var))
165 gtk_grid_attach (GTK_GRID (act->dg_table1), act->dg_table2,
168 gtk_container_add (GTK_CONTAINER (act->dg_box), act->dg_table1);
172 gtk_container_add (GTK_CONTAINER (act->dg_box), act->dg_table2);
173 act->group_defn = GROUPS_VALUES;
177 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (act->dg_dialog),
178 define_groups_state_valid, act);
180 psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[0]), act->grp_var);
181 psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[1]), act->grp_var);
182 psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (act->dg_cut_point_entry), act->grp_var);
184 if ( act->group_defn != GROUPS_CUT_POINT )
186 gtk_toggle_button_set_active
187 (GTK_TOGGLE_BUTTON (act->dg_cut_point_toggle_button), TRUE);
189 gtk_toggle_button_set_active
190 (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button), TRUE);
194 gtk_toggle_button_set_active
195 (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button), TRUE);
197 gtk_toggle_button_set_active
198 (GTK_TOGGLE_BUTTON (act->dg_cut_point_toggle_button), TRUE);
201 g_signal_emit_by_name (act->dg_grp_entry[0], "changed");
202 g_signal_emit_by_name (act->dg_grp_entry[1], "changed");
203 g_signal_emit_by_name (act->dg_cut_point_entry, "changed");
205 response = psppire_dialog_run (PSPPIRE_DIALOG (act->def_grps_dialog));
207 if (response == PSPPIRE_RESPONSE_CONTINUE)
209 const int width = var_get_width (act->grp_var);
211 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (act->dg_values_toggle_button)))
213 act->group_defn = GROUPS_VALUES;
215 psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[0]),
216 &act->grp_val[0], width);
218 psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (act->dg_grp_entry[1]),
219 &act->grp_val[1], width);
223 act->group_defn = GROUPS_CUT_POINT;
225 psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (act->dg_cut_point_entry),
226 &act->cut_point, width);
229 psppire_dialog_notify_change (PSPPIRE_DIALOG (da->dialog));
233 /* Called whenever the group variable entry widget's contents change */
235 on_grp_var_change (GtkEntry *entry, PsppireDialogActionIndepSamps *act)
237 PsppireDialogAction *da = PSPPIRE_DIALOG_ACTION (act);
238 const gchar *text = gtk_entry_get_text (entry);
240 const struct variable *v = psppire_dict_lookup_var (da->dict, text);
242 gtk_widget_set_sensitive (act->define_groups_button, v != NULL);
246 int width = var_get_width (act->grp_var);
247 value_destroy (&act->cut_point, width);
248 value_destroy (&act->grp_val[0], width);
249 value_destroy (&act->grp_val[1], width);
254 const int width = var_get_width (v);
255 value_init (&act->cut_point, width);
256 value_init (&act->grp_val[0], width);
257 value_init (&act->grp_val[1], width);
261 act->cut_point.f = SYSMIS;
262 act->grp_val[0].f = SYSMIS;
263 act->grp_val[1].f = SYSMIS;
267 act->cut_point.short_string[0] = '\0';
268 act->grp_val[0].short_string[0] = '\0';
269 act->grp_val[1].short_string[0] = '\0';
277 set_group_criterion_type (GtkToggleButton *button,
278 PsppireDialogActionIndepSamps *act)
280 gboolean by_values = gtk_toggle_button_get_active (button);
282 gtk_widget_set_sensitive (act->dg_label, by_values);
283 gtk_widget_set_sensitive (act->dg_table2, by_values);
285 gtk_widget_set_sensitive (act->dg_hbox1, !by_values);
290 psppire_dialog_action_indep_samps_activate (GtkAction *a)
292 PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (a);
293 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
295 GtkBuilder *xml = builder_new ("indep-samples.ui");
297 pda->dialog = get_widget_assert (xml,"independent-samples-dialog");
298 pda->source = get_widget_assert (xml, "indep-samples-treeview1");
299 act->define_groups_button = get_widget_assert (xml, "define-groups-button");
300 act->options_button = get_widget_assert (xml, "indep-samples-options-button");
302 act->def_grps_dialog = get_widget_assert (xml, "define-groups-dialog");
303 act->group_var_entry = get_widget_assert (xml, "indep-samples-entry");
304 act->test_vars_tv = get_widget_assert (xml, "indep-samples-treeview2");
306 act->dg_dialog = get_widget_assert (xml, "define-groups-dialog");
307 act->dg_grp_entry[0] = get_widget_assert (xml, "group1-entry");
308 act->dg_grp_entry[1] = get_widget_assert (xml, "group2-entry");
309 act->dg_cut_point_entry = get_widget_assert (xml, "cut-point-entry");
310 act->dg_box = get_widget_assert (xml, "dialog-hbox2");
312 act->dg_table1 = get_widget_assert (xml, "table1");
313 act->dg_table2 = get_widget_assert (xml, "table2");
314 act->dg_label = get_widget_assert (xml, "label4");
315 act->dg_hbox1 = get_widget_assert (xml, "hbox1");
316 act->dg_values_toggle_button = get_widget_assert (xml, "radiobutton3");
317 act->dg_cut_point_toggle_button = get_widget_assert (xml, "radiobutton4");
319 act->opts = tt_options_dialog_create (GTK_WINDOW (pda->toplevel));
321 g_object_ref (act->dg_table1);
322 g_object_ref (act->dg_table2);
324 g_signal_connect (act->dg_values_toggle_button, "toggled",
325 G_CALLBACK (set_group_criterion_type), act);
328 g_object_unref (xml);
330 psppire_dialog_action_set_refresh (pda, refresh);
332 psppire_dialog_action_set_valid_predicate (pda,
335 g_signal_connect_swapped (act->define_groups_button, "clicked",
336 G_CALLBACK (run_define_groups), act);
338 g_signal_connect_swapped (act->options_button, "clicked",
339 G_CALLBACK (tt_options_dialog_run), act->opts);
342 g_signal_connect (act->group_var_entry, "changed",
343 G_CALLBACK (on_grp_var_change), act);
345 if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_indep_samps_parent_class)->activate)
346 PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_indep_samps_parent_class)->activate (pda);
352 generate_syntax (PsppireDialogAction *a)
354 PsppireDialogActionIndepSamps *act = PSPPIRE_DIALOG_ACTION_INDEP_SAMPS (a);
357 GString *str = g_string_new ("T-TEST /VARIABLES=");
359 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (act->test_vars_tv), 0, str);
361 g_string_append (str, "\n\t/GROUPS=");
363 g_string_append (str, var_get_name (act->grp_var));
365 if (act->group_defn != GROUPS_UNDEF)
367 g_string_append (str, "(");
370 const union value *val =
371 (act->group_defn == GROUPS_VALUES) ?
376 ds_init_empty (&strx);
377 syntax_gen_value (&strx, val, var_get_width (act->grp_var),
378 var_get_print_format (act->grp_var));
380 g_string_append (str, ds_cstr (&strx));
384 if (act->group_defn == GROUPS_VALUES)
386 g_string_append (str, ",");
390 ds_init_empty (&strx);
392 syntax_gen_value (&strx, &act->grp_val[1], var_get_width (act->grp_var),
393 var_get_print_format (act->grp_var));
395 g_string_append (str, ds_cstr (&strx));
400 g_string_append (str, ")");
403 tt_options_dialog_append_syntax (act->opts, str);
405 g_string_append (str, ".\n");
409 g_string_free (str, FALSE);
415 psppire_dialog_action_indep_samps_class_init (PsppireDialogActionIndepSampsClass *class)
417 psppire_dialog_action_set_activation (class, psppire_dialog_action_indep_samps_activate);
419 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
424 psppire_dialog_action_indep_samps_init (PsppireDialogActionIndepSamps *act)
427 act->group_defn = GROUPS_UNDEF;