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