Applied patch #6428
[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 = NULL;
133
134   GladeXML *xml = XML_NEW ("oneway.glade");
135
136   struct oneway_anova_dialog ow;
137
138   GtkWidget *dict_view =
139     get_widget_assert (xml, "oneway-anova-treeview1");
140
141   GtkWidget *selector2 =
142     get_widget_assert (xml, "oneway-anova-selector2");
143
144   GtkWidget *selector1 =
145     get_widget_assert (xml, "oneway-anova-selector1");
146
147   GtkWidget *contrasts_button =
148     get_widget_assert (xml, "contrasts-button");
149
150
151   g_signal_connect_swapped (contrasts_button, "clicked",
152                     G_CALLBACK (run_contrasts_dialog), &ow);
153
154
155   ow.factor_entry = get_widget_assert (xml, "oneway-anova-entry");
156   ow.vars_treeview =
157     get_widget_assert (xml, "oneway-anova-treeview2");
158
159   ow.descriptives =
160     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton1"));
161
162   ow.homogeneity =
163     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton2"));
164
165   g_object_get (de->data_editor, "var-store", &vs, NULL);
166
167   ow.dict = vs->dict;
168
169   ow.dialog =
170     GTK_WINDOW (get_widget_assert (xml, "oneway-anova-dialog"));
171
172   gtk_window_set_transient_for (ow.dialog, de->parent.window);
173
174   attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
175                                  vs->dict,
176                                  GTK_SELECTION_MULTIPLE, NULL);
177
178   set_dest_model (GTK_TREE_VIEW (ow.vars_treeview), vs->dict);
179
180
181   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
182                                  dict_view, ow.vars_treeview,
183                                  insert_source_row_into_tree_view,
184                                  NULL,
185                                  NULL);
186
187
188   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
189                                  dict_view, ow.factor_entry,
190                                  insert_source_row_into_entry,
191                                  is_currently_in_entry,
192                                  NULL);
193
194
195
196   g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh),  &ow);
197
198
199
200   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
201                                       dialog_state_valid, &ow);
202
203
204   {
205     struct contrasts_subdialog *cd = &ow.contrasts;
206     GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
207
208     cd->acr = PSPPIRE_ACR (get_widget_assert (xml, "psppire-acr1"));
209     cd->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
210
211     cd->next = get_widget_assert (xml, "next-button");
212     cd->prev = get_widget_assert (xml, "prev-button");
213     cd->ctotal = get_widget_assert (xml, "entry2");
214
215     cd->stack_label = get_widget_assert (xml, "contrast-stack-label");
216
217     /* Contrasts */
218     ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
219
220     g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
221     g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
222
223     psppire_acr_set_entry (cd->acr, entry);
224
225     gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
226                                   de->parent.window);
227   }
228
229   response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
230
231   switch (response)
232     {
233     case GTK_RESPONSE_OK:
234       {
235         gchar *syntax = generate_syntax (&ow);
236         struct getl_interface *sss = create_syntax_string_source (syntax);
237         execute_syntax (sss);
238
239         g_free (syntax);
240       }
241       break;
242     case PSPPIRE_RESPONSE_PASTE:
243       {
244         gchar *syntax = generate_syntax (&ow);
245
246         struct syntax_editor *se =
247           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
248
249         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
250
251         g_free (syntax);
252       }
253       break;
254     default:
255       break;
256     }
257
258   g_array_free (ow.contrasts_array, FALSE);
259
260   g_object_unref (xml);
261 }
262
263
264 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
265 {
266   gchar *text;
267   gint i;
268   gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
269   gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
270
271   GString *str = g_string_new ("ONEWAY /VARIABLES=");
272
273   append_variable_names (str, ow->dict, GTK_TREE_VIEW (ow->vars_treeview), 0);
274
275   g_string_append (str, " BY ");
276
277   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
278
279   if (descriptives || homogeneity )
280     {
281       g_string_append (str, "\n\t/STATISTICS=");
282       if (descriptives)
283         g_string_append (str, "DESCRIPTIVES ");
284       if (homogeneity)
285         g_string_append (str, "HOMOGENEITY ");
286     }
287
288   for (i = 0 ; i < ow->contrasts_array->len ; ++i )
289     {
290       GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
291       GtkTreeIter iter;
292       gboolean ok;
293
294       g_string_append (str, "\n\t/CONTRAST=");
295
296       for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
297                                                &iter);
298            ok;
299            ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
300         {
301           gdouble v;
302
303           gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
304
305           g_string_append_printf (str, " %g", v);
306         }
307     }
308
309   g_string_append (str, ".\n");
310
311   text = str->str;
312   g_string_free (str, FALSE);
313
314   return text;
315 }
316
317
318 \f
319 /* Contrasts stuff */
320
321
322
323
324 /* Callback for when the list store currently associated with the
325    treeview has changed.  It sets the widgets of the subdialog
326    to reflect the store's new state.
327 */
328 static void
329 list_store_changed (struct contrasts_subdialog *csd)
330 {
331   gboolean ok;
332   gdouble total = 0.0;
333   GtkTreeIter iter;
334   GtkTreeModel *ls = NULL;
335   gchar *text =
336     g_strdup_printf (_("Contrast %d of %d"),
337                      csd->c, csd->temp_contrasts->len);
338
339   gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
340
341   g_free (text);
342
343   gtk_widget_set_sensitive (csd->prev, csd->c > 1);
344
345   if ( csd->c > 0 )
346     ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
347
348   psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
349
350   /* Sensitive iff the liststore has two items or more */
351   gtk_widget_set_sensitive (csd->next,
352                             gtk_tree_model_iter_nth_child
353                             (ls, &iter,  NULL, 1));
354
355   for (ok = gtk_tree_model_get_iter_first (ls, &iter);
356        ok;
357        ok = gtk_tree_model_iter_next (ls, &iter)
358        )
359     {
360       gdouble v;
361       gtk_tree_model_get (ls, &iter, 0, &v, -1);
362       total += v;
363     }
364
365   text = g_strdup_printf ("%g", total);
366
367   gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
368
369   g_free (text);
370 }
371
372
373
374 /* Copy the contrasts array into the local array */
375 static GArray *
376 clone_contrasts_array (GArray *src_array)
377 {
378   gint i;
379
380   GArray *dest_array =
381     g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
382                        src_array->len);
383
384   for (i = 0 ; i < src_array->len ; ++i )
385     {
386
387       GtkTreeIter src_iter;
388       GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
389       GtkListStore *dest;
390
391       /* Refuse to copy empty stores */
392       if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
393                                            &src_iter))
394         continue;
395
396       dest = clone_list_store (src);
397
398       g_array_append_val (dest_array, dest);
399     }
400
401   return dest_array;
402 }
403
404
405 static void
406 run_contrasts_dialog (struct oneway_anova_dialog *ow)
407 {
408   struct contrasts_subdialog *csd = &ow->contrasts;
409   gint response;
410
411   csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
412
413   csd->c = 1;
414
415   push_new_store (csd->temp_contrasts, csd);
416
417   response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
418
419   if ( response == PSPPIRE_RESPONSE_CONTINUE )
420     {
421       ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
422     }
423
424   /* Destroy the temp contrasts here */
425
426 }
427
428
429 static void
430 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
431 {
432   GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
433
434   g_array_append_val (contrast_stack, ls);
435
436   g_signal_connect_swapped (ls, "row-deleted",
437                             G_CALLBACK (list_store_changed), csd);
438
439   g_signal_connect_swapped (ls, "row-changed",
440                             G_CALLBACK (list_store_changed), csd);
441
442   list_store_changed (csd);
443 }
444
445 static void
446 next (GtkWidget *widget, gpointer data)
447 {
448   struct contrasts_subdialog *csd = data;
449
450   if (csd->c >= csd->temp_contrasts->len)
451     push_new_store (csd->temp_contrasts, csd);
452
453   csd->c++;
454
455   list_store_changed (csd);
456 }
457
458
459 static void
460 prev (GtkWidget *widget, gpointer data)
461 {
462   struct contrasts_subdialog *csd = data;
463
464   if ( csd->c > 0 )
465     --csd->c;
466
467   list_store_changed (csd);
468 }
469
470