Implemented a dialog box for the ONEWAY command.
[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
189
190   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
191                                  dict_view, ow.factor_entry,
192                                  insert_source_row_into_entry,
193                                  is_currently_in_entry);
194
195
196
197   g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh),  &ow);
198
199
200
201   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
202                                       dialog_state_valid, &ow);
203
204
205   {
206     struct contrasts_subdialog *cd = &ow.contrasts;
207     GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
208
209     cd->acr = PSPPIRE_ACR (get_widget_assert (xml, "psppire-acr1"));
210     cd->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
211
212     cd->next = get_widget_assert (xml, "next-button");
213     cd->prev = get_widget_assert (xml, "prev-button");
214     cd->ctotal = get_widget_assert (xml, "entry2");
215
216     cd->stack_label = get_widget_assert (xml, "contrast-stack-label");
217
218     /* Contrasts */
219     ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
220
221     g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
222     g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
223
224     psppire_acr_set_entry (cd->acr, entry);
225
226     gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
227                                   de->parent.window);
228   }
229
230   response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
231
232   switch (response)
233     {
234     case GTK_RESPONSE_OK:
235       {
236         gchar *syntax = generate_syntax (&ow);
237         struct getl_interface *sss = create_syntax_string_source (syntax);
238         execute_syntax (sss);
239
240         g_free (syntax);
241       }
242       break;
243     case PSPPIRE_RESPONSE_PASTE:
244       {
245         gchar *syntax = generate_syntax (&ow);
246
247         struct syntax_editor *se =
248           (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
249
250         gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
251
252         g_free (syntax);
253       }
254       break;
255     default:
256       break;
257     }
258
259   g_array_free (ow.contrasts_array, FALSE);
260
261   g_object_unref (xml);
262 }
263
264
265 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
266 {
267   gchar *text;
268   gint i;
269   gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
270   gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
271
272   GString *str = g_string_new ("ONEWAY /VARIABLES=");
273
274   append_variable_names (str, ow->dict, GTK_TREE_VIEW (ow->vars_treeview));
275
276   g_string_append (str, " BY ");
277
278   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
279
280   if (descriptives || homogeneity )
281     {
282       g_string_append (str, "\n\t/STATISTICS=");
283       if (descriptives)
284         g_string_append (str, "DESCRIPTIVES ");
285       if (homogeneity)
286         g_string_append (str, "HOMOGENEITY ");
287     }
288
289   for (i = 0 ; i < ow->contrasts_array->len ; ++i )
290     {
291       GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
292       GtkTreeIter iter;
293       gboolean ok;
294
295       g_string_append (str, "\n\t/CONTRAST=");
296
297       for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
298                                                &iter);
299            ok;
300            ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
301         {
302           gdouble v;
303
304           gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
305
306           g_string_append_printf (str, " %g", v);
307         }
308     }
309
310   g_string_append (str, ".\n");
311
312   text = str->str;
313   g_string_free (str, FALSE);
314
315   return text;
316 }
317
318
319 \f
320 /* Contrasts stuff */
321
322
323
324
325 /* Callback for when the list store currently associated with the
326    treeview has changed.  It sets the widgets of the subdialog
327    to reflect the store's new state.
328 */
329 static void
330 list_store_changed (struct contrasts_subdialog *csd)
331 {
332   gboolean ok;
333   gdouble total = 0.0;
334   GtkTreeIter iter;
335   GtkTreeModel *ls = NULL;
336   gchar *text =
337     g_strdup_printf (_("Contrast %d of %d"),
338                      csd->c, csd->temp_contrasts->len);
339
340   gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
341
342   g_free (text);
343
344   gtk_widget_set_sensitive (csd->prev, csd->c > 1);
345
346   if ( csd->c > 0 )
347     ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
348
349   psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
350
351   /* Sensitive iff the liststore has two items or more */
352   gtk_widget_set_sensitive (csd->next,
353                             gtk_tree_model_iter_nth_child
354                             (ls, &iter,  NULL, 1));
355
356   for (ok = gtk_tree_model_get_iter_first (ls, &iter);
357        ok;
358        ok = gtk_tree_model_iter_next (ls, &iter)
359        )
360     {
361       gdouble v;
362       gtk_tree_model_get (ls, &iter, 0, &v, -1);
363       total += v;
364     }
365
366   text = g_strdup_printf ("%g", total);
367
368   gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
369
370   g_free (text);
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       gboolean ok;
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 = gtk_list_store_new (1, G_TYPE_DOUBLE);
397
398       for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(src),
399                                                &src_iter);
400            ok;
401            ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (src), &src_iter))
402         {
403           gdouble v;
404           GtkTreeIter dest_iter;
405
406           gtk_tree_model_get (GTK_TREE_MODEL (src), &src_iter, 0, &v, -1);
407
408           gtk_list_store_append  (dest, &dest_iter);
409           gtk_list_store_set (dest, &dest_iter, 0, v, -1);
410         }
411
412       g_array_append_val (dest_array, dest);
413     }
414
415   return dest_array;
416 }
417
418
419 static void
420 run_contrasts_dialog (struct oneway_anova_dialog *ow)
421 {
422   struct contrasts_subdialog *csd = &ow->contrasts;
423   gint response;
424
425   csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
426
427   csd->c = 1;
428
429   push_new_store (csd->temp_contrasts, csd);
430
431   response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
432
433   if ( response == PSPPIRE_RESPONSE_CONTINUE )
434     {
435       ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
436     }
437
438   /* Destroy the temp contrasts here */
439
440 }
441
442
443 static void
444 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
445 {
446   GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
447
448   g_array_append_val (contrast_stack, ls);
449
450   g_signal_connect_swapped (ls, "row-deleted",
451                             G_CALLBACK (list_store_changed), csd);
452
453   g_signal_connect_swapped (ls, "row-changed",
454                             G_CALLBACK (list_store_changed), csd);
455
456   list_store_changed (csd);
457 }
458
459 static void
460 next (GtkWidget *widget, gpointer data)
461 {
462   struct contrasts_subdialog *csd = data;
463
464   if (csd->c >= csd->temp_contrasts->len)
465     push_new_store (csd->temp_contrasts, csd);
466
467   csd->c++;
468
469   list_store_changed (csd);
470 }
471
472
473 static void
474 prev (GtkWidget *widget, gpointer data)
475 {
476   struct contrasts_subdialog *csd = data;
477
478   if ( csd->c > 0 )
479     --csd->c;
480
481   list_store_changed (csd);
482 }
483
484