Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / ui / gui / oneway-anova-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 "oneway-anova-dialog.h"
23 #include "psppire-dict.h"
24 #include "psppire-var-store.h"
25 #include "helper.h"
26 #include "data-editor.h"
27 #include "psppire-dialog.h"
28 #include "dialog-common.h"
29 #include "dict-display.h"
30 #include "psppire-acr.h"
31
32
33 #include <language/syntax-string-source.h>
34 #include "syntax-editor.h"
35
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41
42 struct contrasts_subdialog;
43 struct oneway_anova_dialog;
44
45
46 static void run_contrasts_dialog (struct oneway_anova_dialog *);
47 static void push_new_store (GArray *, struct contrasts_subdialog *);
48
49 static void next (GtkWidget *, gpointer);
50 static void prev (GtkWidget *, gpointer);
51
52
53 struct contrasts_subdialog
54 {
55   /* Contrasts Dialog widgets */
56   GtkWidget *contrasts_dialog;
57   GtkWidget *stack_label;
58   PsppireAcr *acr;
59
60
61   /* Gets copied into contrasts when "Continue"
62      is clicked */
63   GArray *temp_contrasts;
64
65   /* Index into the temp_contrasts */
66   guint c;
67
68   GtkWidget *prev;
69   GtkWidget *next;
70
71   GtkWidget *ctotal;
72 };
73
74
75 struct oneway_anova_dialog
76 {
77   PsppireDict *dict;
78
79   GtkWidget *factor_entry;
80   GtkWidget *vars_treeview;
81   GtkWindow *dialog;
82   GArray *contrasts_array;
83
84   GtkToggleButton *descriptives;
85   GtkToggleButton *homogeneity;
86
87   struct contrasts_subdialog contrasts;
88 };
89
90 static gboolean
91 dialog_state_valid (gpointer data)
92 {
93   struct oneway_anova_dialog *ow = data;
94
95   GtkTreeModel *vars =
96     gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
97
98   GtkTreeIter notused;
99
100   if ( !gtk_tree_model_get_iter_first (vars, &notused) )
101     return FALSE;
102
103   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
104     return FALSE;
105
106   return TRUE;
107 }
108
109 static gchar * generate_syntax (const struct oneway_anova_dialog *);
110
111
112 static void
113 refresh (struct oneway_anova_dialog *ow)
114 {
115   GtkTreeModel *model =
116     gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
117
118   gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
119
120   gtk_list_store_clear (GTK_LIST_STORE (model));
121 }
122
123
124 /* Pops up the dialog box */
125 void
126 oneway_anova_dialog (GObject *o, gpointer data)
127 {
128   gint response;
129   struct data_editor *de = data;
130
131   PsppireVarStore *vs = NULL;
132
133   GladeXML *xml = XML_NEW ("oneway.glade");
134
135   struct oneway_anova_dialog ow;
136
137   GtkWidget *dict_view =
138     get_widget_assert (xml, "oneway-anova-treeview1");
139
140   GtkWidget *selector2 =
141     get_widget_assert (xml, "oneway-anova-selector2");
142
143   GtkWidget *selector1 =
144     get_widget_assert (xml, "oneway-anova-selector1");
145
146   GtkWidget *contrasts_button =
147     get_widget_assert (xml, "contrasts-button");
148
149
150   g_signal_connect_swapped (contrasts_button, "clicked",
151                     G_CALLBACK (run_contrasts_dialog), &ow);
152
153
154   ow.factor_entry = get_widget_assert (xml, "oneway-anova-entry");
155   ow.vars_treeview =
156     get_widget_assert (xml, "oneway-anova-treeview2");
157
158   ow.descriptives =
159     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton1"));
160
161   ow.homogeneity =
162     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton2"));
163
164   g_object_get (de->data_editor, "var-store", &vs, NULL);
165
166   ow.dict = vs->dict;
167
168   ow.dialog =
169     GTK_WINDOW (get_widget_assert (xml, "oneway-anova-dialog"));
170
171   gtk_window_set_transient_for (ow.dialog, de->parent.window);
172
173   attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
174                                  vs->dict,
175                                  GTK_SELECTION_MULTIPLE, NULL);
176
177   set_dest_model (GTK_TREE_VIEW (ow.vars_treeview), vs->dict);
178
179
180   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
181                                  dict_view, ow.vars_treeview,
182                                  insert_source_row_into_tree_view,
183                                  NULL,
184                                  NULL);
185
186
187   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
188                                  dict_view, ow.factor_entry,
189                                  insert_source_row_into_entry,
190                                  is_currently_in_entry,
191                                  NULL);
192
193
194
195   g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh),  &ow);
196
197
198
199   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
200                                       dialog_state_valid, &ow);
201
202
203   {
204     struct contrasts_subdialog *cd = &ow.contrasts;
205     GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
206
207     cd->acr = PSPPIRE_ACR (get_widget_assert (xml, "psppire-acr1"));
208     cd->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
209
210     cd->next = get_widget_assert (xml, "next-button");
211     cd->prev = get_widget_assert (xml, "prev-button");
212     cd->ctotal = get_widget_assert (xml, "entry2");
213
214     cd->stack_label = get_widget_assert (xml, "contrast-stack-label");
215
216     /* Contrasts */
217     ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
218
219     g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
220     g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
221
222     psppire_acr_set_entry (cd->acr, entry);
223
224     gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
225                                   de->parent.window);
226   }
227
228   response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
229
230   switch (response)
231     {
232     case GTK_RESPONSE_OK:
233       {
234         gchar *syntax = generate_syntax (&ow);
235         struct getl_interface *sss = create_syntax_string_source (syntax);
236         execute_syntax (sss);
237
238         g_free (syntax);
239       }
240       break;
241     case PSPPIRE_RESPONSE_PASTE:
242       {
243         gchar *syntax = generate_syntax (&ow);
244
245         struct syntax_editor *se =
246           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
247
248         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
249
250         g_free (syntax);
251       }
252       break;
253     default:
254       break;
255     }
256
257   g_array_free (ow.contrasts_array, FALSE);
258
259   g_object_unref (xml);
260 }
261
262
263 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
264 {
265   gchar *text;
266   gint i;
267   gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
268   gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
269
270   GString *str = g_string_new ("ONEWAY /VARIABLES=");
271
272   append_variable_names (str, ow->dict, GTK_TREE_VIEW (ow->vars_treeview), 0);
273
274   g_string_append (str, " BY ");
275
276   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
277
278   if (descriptives || homogeneity )
279     {
280       g_string_append (str, "\n\t/STATISTICS=");
281       if (descriptives)
282         g_string_append (str, "DESCRIPTIVES ");
283       if (homogeneity)
284         g_string_append (str, "HOMOGENEITY ");
285     }
286
287   for (i = 0 ; i < ow->contrasts_array->len ; ++i )
288     {
289       GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
290       GtkTreeIter iter;
291       gboolean ok;
292
293       g_string_append (str, "\n\t/CONTRAST=");
294
295       for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
296                                                &iter);
297            ok;
298            ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
299         {
300           gdouble v;
301
302           gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
303
304           g_string_append_printf (str, " %g", v);
305         }
306     }
307
308   g_string_append (str, ".\n");
309
310   text = str->str;
311   g_string_free (str, FALSE);
312
313   return text;
314 }
315
316
317 \f
318 /* Contrasts stuff */
319
320
321
322
323 /* Callback for when the list store currently associated with the
324    treeview has changed.  It sets the widgets of the subdialog
325    to reflect the store's new state.
326 */
327 static void
328 list_store_changed (struct contrasts_subdialog *csd)
329 {
330   gboolean ok;
331   gdouble total = 0.0;
332   GtkTreeIter iter;
333   GtkTreeModel *ls = NULL;
334   gchar *text =
335     g_strdup_printf (_("Contrast %d of %d"),
336                      csd->c, csd->temp_contrasts->len);
337
338   gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
339
340   g_free (text);
341
342   gtk_widget_set_sensitive (csd->prev, csd->c > 1);
343
344   if ( csd->c > 0 )
345     ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
346
347   psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
348
349   /* Sensitive iff the liststore has two items or more */
350   gtk_widget_set_sensitive (csd->next,
351                             gtk_tree_model_iter_nth_child
352                             (ls, &iter,  NULL, 1));
353
354   for (ok = gtk_tree_model_get_iter_first (ls, &iter);
355        ok;
356        ok = gtk_tree_model_iter_next (ls, &iter)
357        )
358     {
359       gdouble v;
360       gtk_tree_model_get (ls, &iter, 0, &v, -1);
361       total += v;
362     }
363
364   text = g_strdup_printf ("%g", total);
365
366   gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
367
368   g_free (text);
369 }
370
371
372
373 /* Copy the contrasts array into the local array */
374 static GArray *
375 clone_contrasts_array (GArray *src_array)
376 {
377   gint i;
378
379   GArray *dest_array =
380     g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
381                        src_array->len);
382
383   for (i = 0 ; i < src_array->len ; ++i )
384     {
385
386       GtkTreeIter src_iter;
387       GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
388       GtkListStore *dest;
389
390       /* Refuse to copy empty stores */
391       if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
392                                            &src_iter))
393         continue;
394
395       dest = clone_list_store (src);
396
397       g_array_append_val (dest_array, dest);
398     }
399
400   return dest_array;
401 }
402
403
404 static void
405 run_contrasts_dialog (struct oneway_anova_dialog *ow)
406 {
407   struct contrasts_subdialog *csd = &ow->contrasts;
408   gint response;
409
410   csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
411
412   csd->c = 1;
413
414   push_new_store (csd->temp_contrasts, csd);
415
416   response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
417
418   if ( response == PSPPIRE_RESPONSE_CONTINUE )
419     {
420       ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
421     }
422
423   /* Destroy the temp contrasts here */
424
425 }
426
427
428 static void
429 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
430 {
431   GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
432
433   g_array_append_val (contrast_stack, ls);
434
435   g_signal_connect_swapped (ls, "row-deleted",
436                             G_CALLBACK (list_store_changed), csd);
437
438   g_signal_connect_swapped (ls, "row-changed",
439                             G_CALLBACK (list_store_changed), csd);
440
441   list_store_changed (csd);
442 }
443
444 static void
445 next (GtkWidget *widget, gpointer data)
446 {
447   struct contrasts_subdialog *csd = data;
448
449   if (csd->c >= csd->temp_contrasts->len)
450     push_new_store (csd->temp_contrasts, csd);
451
452   csd->c++;
453
454   list_store_changed (csd);
455 }
456
457
458 static void
459 prev (GtkWidget *widget, gpointer data)
460 {
461   struct contrasts_subdialog *csd = data;
462
463   if ( csd->c > 0 )
464     --csd->c;
465
466   list_store_changed (csd);
467 }
468
469