1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 Free Software Foundation
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.
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.
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/>.
20 #include <glade/glade.h>
22 #include "oneway-anova-dialog.h"
23 #include "psppire-dict.h"
24 #include "psppire-var-store.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"
34 #include <language/syntax-string-source.h>
35 #include "syntax-editor.h"
39 #define _(msgid) gettext (msgid)
40 #define N_(msgid) msgid
43 struct contrasts_subdialog;
44 struct oneway_anova_dialog;
47 static void run_contrasts_dialog (struct oneway_anova_dialog *);
48 static void push_new_store (GArray *, struct contrasts_subdialog *);
50 static void next (GtkWidget *, gpointer);
51 static void prev (GtkWidget *, gpointer);
54 struct contrasts_subdialog
56 /* Contrasts Dialog widgets */
57 GtkWidget *contrasts_dialog;
58 GtkWidget *stack_label;
62 /* Gets copied into contrasts when "Continue"
64 GArray *temp_contrasts;
66 /* Index into the temp_contrasts */
76 struct oneway_anova_dialog
80 GtkWidget *factor_entry;
81 GtkWidget *vars_treeview;
83 GArray *contrasts_array;
85 GtkToggleButton *descriptives;
86 GtkToggleButton *homogeneity;
88 struct contrasts_subdialog contrasts;
92 dialog_state_valid (gpointer data)
94 struct oneway_anova_dialog *ow = data;
97 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
101 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
104 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
110 static gchar * generate_syntax (const struct oneway_anova_dialog *);
114 refresh (struct oneway_anova_dialog *ow)
116 GtkTreeModel *model =
117 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
119 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
121 gtk_list_store_clear (GTK_LIST_STORE (model));
125 /* Pops up the dialog box */
127 oneway_anova_dialog (GObject *o, gpointer data)
130 struct data_editor *de = data;
134 GladeXML *xml = XML_NEW ("oneway.glade");
136 struct oneway_anova_dialog ow;
138 GtkSheet *var_sheet =
139 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
141 GtkWidget *dict_view =
142 get_widget_assert (xml, "oneway-anova-treeview1");
144 GtkWidget *selector2 =
145 get_widget_assert (xml, "oneway-anova-selector2");
147 GtkWidget *selector1 =
148 get_widget_assert (xml, "oneway-anova-selector1");
150 GtkWidget *contrasts_button =
151 get_widget_assert (xml, "contrasts-button");
154 g_signal_connect_swapped (contrasts_button, "clicked",
155 G_CALLBACK (run_contrasts_dialog), &ow);
158 ow.factor_entry = get_widget_assert (xml, "oneway-anova-entry");
160 get_widget_assert (xml, "oneway-anova-treeview2");
163 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton1"));
166 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "checkbutton2"));
168 vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
173 GTK_WINDOW (get_widget_assert (xml, "oneway-anova-dialog"));
175 gtk_window_set_transient_for (ow.dialog, de->parent.window);
177 attach_dictionary_to_treeview (GTK_TREE_VIEW (dict_view),
179 GTK_SELECTION_MULTIPLE, NULL);
181 set_dest_model (GTK_TREE_VIEW (ow.vars_treeview), vs->dict);
184 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
185 dict_view, ow.vars_treeview,
186 insert_source_row_into_tree_view,
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);
197 g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh), &ow);
201 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
202 dialog_state_valid, &ow);
206 struct contrasts_subdialog *cd = &ow.contrasts;
207 GtkEntry *entry = GTK_ENTRY (get_widget_assert (xml, "entry1"));
209 cd->acr = PSPPIRE_ACR (get_widget_assert (xml, "psppire-acr1"));
210 cd->contrasts_dialog = get_widget_assert (xml, "contrasts-dialog");
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");
216 cd->stack_label = get_widget_assert (xml, "contrast-stack-label");
219 ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
221 g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
222 g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
224 psppire_acr_set_entry (cd->acr, entry);
226 gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
230 response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
234 case GTK_RESPONSE_OK:
236 gchar *syntax = generate_syntax (&ow);
237 struct getl_interface *sss = create_syntax_string_source (syntax);
238 execute_syntax (sss);
243 case PSPPIRE_RESPONSE_PASTE:
245 gchar *syntax = generate_syntax (&ow);
247 struct syntax_editor *se =
248 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
250 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
259 g_array_free (ow.contrasts_array, FALSE);
261 g_object_unref (xml);
265 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
269 gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
270 gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
272 GString *str = g_string_new ("ONEWAY /VARIABLES=");
274 append_variable_names (str, ow->dict, GTK_TREE_VIEW (ow->vars_treeview));
276 g_string_append (str, " BY ");
278 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
280 if (descriptives || homogeneity )
282 g_string_append (str, "\n\t/STATISTICS=");
284 g_string_append (str, "DESCRIPTIVES ");
286 g_string_append (str, "HOMOGENEITY ");
289 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
291 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
295 g_string_append (str, "\n\t/CONTRAST=");
297 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
300 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
304 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
306 g_string_append_printf (str, " %g", v);
310 g_string_append (str, ".\n");
313 g_string_free (str, FALSE);
320 /* Contrasts stuff */
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.
330 list_store_changed (struct contrasts_subdialog *csd)
335 GtkTreeModel *ls = NULL;
337 g_strdup_printf (_("Contrast %d of %d"),
338 csd->c, csd->temp_contrasts->len);
340 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
344 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
347 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
349 psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
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));
356 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
358 ok = gtk_tree_model_iter_next (ls, &iter)
362 gtk_tree_model_get (ls, &iter, 0, &v, -1);
366 text = g_strdup_printf ("%g", total);
368 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
374 /* Copy the contrasts array into the local array */
376 clone_contrasts_array (GArray *src_array)
381 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
384 for (i = 0 ; i < src_array->len ; ++i )
387 GtkTreeIter src_iter;
388 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
391 /* Refuse to copy empty stores */
392 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
396 dest = gtk_list_store_new (1, G_TYPE_DOUBLE);
398 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(src),
401 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (src), &src_iter))
404 GtkTreeIter dest_iter;
406 gtk_tree_model_get (GTK_TREE_MODEL (src), &src_iter, 0, &v, -1);
408 gtk_list_store_append (dest, &dest_iter);
409 gtk_list_store_set (dest, &dest_iter, 0, v, -1);
412 g_array_append_val (dest_array, dest);
420 run_contrasts_dialog (struct oneway_anova_dialog *ow)
422 struct contrasts_subdialog *csd = &ow->contrasts;
425 csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
429 push_new_store (csd->temp_contrasts, csd);
431 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
433 if ( response == PSPPIRE_RESPONSE_CONTINUE )
435 ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
438 /* Destroy the temp contrasts here */
444 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
446 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
448 g_array_append_val (contrast_stack, ls);
450 g_signal_connect_swapped (ls, "row-deleted",
451 G_CALLBACK (list_store_changed), csd);
453 g_signal_connect_swapped (ls, "row-changed",
454 G_CALLBACK (list_store_changed), csd);
456 list_store_changed (csd);
460 next (GtkWidget *widget, gpointer data)
462 struct contrasts_subdialog *csd = data;
464 if (csd->c >= csd->temp_contrasts->len)
465 push_new_store (csd->temp_contrasts, csd);
469 list_store_changed (csd);
474 prev (GtkWidget *widget, gpointer data)
476 struct contrasts_subdialog *csd = data;
481 list_store_changed (csd);