Move data-editor.c to psppire-data-window.c
[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 "psppire-data-window.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 "psppire-syntax-window.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   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (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, GTK_WINDOW (de));
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                                   GTK_WINDOW (de));
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         GtkWidget *se = psppire_syntax_window_new ();
246
247         gtk_text_buffer_insert_at_cursor (PSPPIRE_SYNTAX_WINDOW (se)->buffer, syntax, -1);
248
249         gtk_widget_show (se);
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