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 "oneway-anova-dialog.h"
21 #include "psppire-dict.h"
22 #include "psppire-var-store.h"
23 #include "psppire-var-view.h"
25 #include "psppire-data-window.h"
26 #include "psppire-dialog.h"
27 #include "dialog-common.h"
28 #include "psppire-acr.h"
29 #include "psppire-selector.h"
30 #include "dict-display.h"
32 #include <language/syntax-string-source.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
41 struct contrasts_subdialog;
42 struct oneway_anova_dialog;
45 static void run_contrasts_dialog (struct oneway_anova_dialog *);
46 static void push_new_store (GArray *, struct contrasts_subdialog *);
48 static void next (GtkWidget *, gpointer);
49 static void prev (GtkWidget *, gpointer);
52 struct contrasts_subdialog
54 /* Contrasts Dialog widgets */
55 GtkWidget *contrasts_dialog;
56 GtkWidget *stack_label;
60 /* Gets copied into contrasts when "Continue"
62 GArray *temp_contrasts;
64 /* Index into the temp_contrasts */
74 struct oneway_anova_dialog
78 GtkWidget *factor_entry;
79 GtkWidget *vars_treeview;
81 GArray *contrasts_array;
83 GtkToggleButton *descriptives;
84 GtkToggleButton *homogeneity;
86 struct contrasts_subdialog contrasts;
90 dialog_state_valid (gpointer data)
92 struct oneway_anova_dialog *ow = data;
95 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
99 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
102 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
108 static gchar * generate_syntax (const struct oneway_anova_dialog *);
112 refresh (struct oneway_anova_dialog *ow)
114 GtkTreeModel *model =
115 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
117 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
119 gtk_list_store_clear (GTK_LIST_STORE (model));
124 /* Pops up the dialog box */
126 oneway_anova_dialog (PsppireDataWindow *de)
130 PsppireVarStore *vs = NULL;
132 struct oneway_anova_dialog ow;
134 GtkBuilder *builder = builder_new ("oneway.ui");
136 GtkWidget *dict_view =
137 get_widget_assert (builder, "oneway-anova-treeview1");
139 GtkWidget *selector2 =
140 get_widget_assert (builder, "oneway-anova-selector2");
142 GtkWidget *contrasts_button =
143 get_widget_assert (builder, "contrasts-button");
146 g_signal_connect_swapped (contrasts_button, "clicked",
147 G_CALLBACK (run_contrasts_dialog), &ow);
150 ow.factor_entry = get_widget_assert (builder, "oneway-anova-entry");
152 get_widget_assert (builder, "oneway-anova-treeview2");
155 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton1"));
158 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton2"));
160 g_object_get (de->data_editor, "var-store", &vs, NULL);
162 g_object_get (vs, "dictionary", &ow.dict, NULL);
165 GTK_WINDOW (get_widget_assert (builder, "oneway-anova-dialog"));
167 gtk_window_set_transient_for (ow.dialog, GTK_WINDOW (de));
169 g_object_set (dict_view, "model", ow.dict, NULL);
172 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector2),
173 is_currently_in_entry);
176 g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh), &ow);
180 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
181 dialog_state_valid, &ow);
185 struct contrasts_subdialog *cd = &ow.contrasts;
186 GtkEntry *entry = GTK_ENTRY (get_widget_assert (builder, "entry1"));
188 cd->acr = PSPPIRE_ACR (get_widget_assert (builder, "psppire-acr1"));
189 cd->contrasts_dialog = get_widget_assert (builder, "contrasts-dialog");
191 cd->next = get_widget_assert (builder, "next-button");
192 cd->prev = get_widget_assert (builder, "prev-button");
193 cd->ctotal = get_widget_assert (builder, "entry2");
195 cd->stack_label = get_widget_assert (builder, "contrast-stack-label");
198 ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
200 g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
201 g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
203 psppire_acr_set_entry (cd->acr, entry);
205 gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
209 response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
213 case GTK_RESPONSE_OK:
215 gchar *syntax = generate_syntax (&ow);
217 struct getl_interface *sss = create_syntax_string_source (syntax);
218 execute_syntax (sss);
223 case PSPPIRE_RESPONSE_PASTE:
225 gchar *syntax = generate_syntax (&ow);
226 paste_syntax_to_window (syntax);
235 g_array_free (ow.contrasts_array, FALSE);
237 g_object_unref (builder);
241 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
245 gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
246 gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
248 GString *str = g_string_new ("ONEWAY /VARIABLES=");
250 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ow->vars_treeview), 0, str);
252 g_string_append (str, " BY ");
254 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
256 if (descriptives || homogeneity )
258 g_string_append (str, "\n\t/STATISTICS=");
260 g_string_append (str, "DESCRIPTIVES ");
262 g_string_append (str, "HOMOGENEITY ");
265 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
267 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
271 g_string_append (str, "\n\t/CONTRAST=");
273 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
276 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
280 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
282 g_string_append_printf (str, " %g", v);
286 g_string_append (str, ".\n");
289 g_string_free (str, FALSE);
296 /* Contrasts stuff */
301 /* Callback for when the list store currently associated with the
302 treeview has changed. It sets the widgets of the subdialog
303 to reflect the store's new state.
306 list_store_changed (struct contrasts_subdialog *csd)
311 GtkTreeModel *ls = NULL;
313 g_strdup_printf (_("Contrast %d of %d"),
314 csd->c, csd->temp_contrasts->len);
316 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
320 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
323 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
325 psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
327 /* Sensitive iff the liststore has two items or more */
328 gtk_widget_set_sensitive (csd->next,
329 gtk_tree_model_iter_nth_child
330 (ls, &iter, NULL, 1));
332 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
334 ok = gtk_tree_model_iter_next (ls, &iter)
338 gtk_tree_model_get (ls, &iter, 0, &v, -1);
342 text = g_strdup_printf ("%g", total);
344 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
351 /* Copy the contrasts array into the local array */
353 clone_contrasts_array (GArray *src_array)
358 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
361 for (i = 0 ; i < src_array->len ; ++i )
364 GtkTreeIter src_iter;
365 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
368 /* Refuse to copy empty stores */
369 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
373 dest = clone_list_store (src);
375 g_array_append_val (dest_array, dest);
383 run_contrasts_dialog (struct oneway_anova_dialog *ow)
385 struct contrasts_subdialog *csd = &ow->contrasts;
388 csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
392 push_new_store (csd->temp_contrasts, csd);
394 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
396 if ( response == PSPPIRE_RESPONSE_CONTINUE )
398 ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
401 /* Destroy the temp contrasts here */
407 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
409 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
411 g_array_append_val (contrast_stack, ls);
413 g_signal_connect_swapped (ls, "row-deleted",
414 G_CALLBACK (list_store_changed), csd);
416 g_signal_connect_swapped (ls, "row-changed",
417 G_CALLBACK (list_store_changed), csd);
419 list_store_changed (csd);
423 next (GtkWidget *widget, gpointer data)
425 struct contrasts_subdialog *csd = data;
427 if (csd->c >= csd->temp_contrasts->len)
428 push_new_store (csd->temp_contrasts, csd);
432 list_store_changed (csd);
437 prev (GtkWidget *widget, gpointer data)
439 struct contrasts_subdialog *csd = data;
444 list_store_changed (csd);