1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011, 2012 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"
24 #include "builder-wrapper.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"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
40 struct contrasts_subdialog;
41 struct oneway_anova_dialog;
44 static void run_contrasts_dialog (struct oneway_anova_dialog *);
45 static void push_new_store (GArray *, struct contrasts_subdialog *);
47 static void next (GtkWidget *, gpointer);
48 static void prev (GtkWidget *, gpointer);
51 struct contrasts_subdialog
53 /* Contrasts Dialog widgets */
54 GtkWidget *contrasts_dialog;
55 GtkWidget *stack_label;
59 /* Gets copied into contrasts when "Continue"
61 GArray *temp_contrasts;
63 /* Index into the temp_contrasts */
73 struct oneway_anova_dialog
77 GtkWidget *factor_entry;
78 GtkWidget *vars_treeview;
80 GArray *contrasts_array;
82 GtkToggleButton *descriptives;
83 GtkToggleButton *homogeneity;
85 struct contrasts_subdialog contrasts;
89 dialog_state_valid (gpointer data)
91 struct oneway_anova_dialog *ow = data;
94 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
98 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
101 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
107 static gchar * generate_syntax (const struct oneway_anova_dialog *);
111 refresh (struct oneway_anova_dialog *ow)
113 GtkTreeModel *model =
114 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
116 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
118 gtk_list_store_clear (GTK_LIST_STORE (model));
123 /* Pops up the dialog box */
125 oneway_anova_dialog (PsppireDataWindow *de)
129 PsppireVarStore *vs = NULL;
131 struct oneway_anova_dialog ow;
133 GtkBuilder *builder = builder_new ("oneway.ui");
135 GtkWidget *dict_view =
136 get_widget_assert (builder, "oneway-anova-treeview1");
138 GtkWidget *selector2 =
139 get_widget_assert (builder, "oneway-anova-selector2");
141 GtkWidget *contrasts_button =
142 get_widget_assert (builder, "contrasts-button");
145 g_signal_connect_swapped (contrasts_button, "clicked",
146 G_CALLBACK (run_contrasts_dialog), &ow);
149 ow.factor_entry = get_widget_assert (builder, "oneway-anova-entry");
151 get_widget_assert (builder, "oneway-anova-treeview2");
154 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton1"));
157 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton2"));
159 g_object_get (de->data_editor, "var-store", &vs, NULL);
161 g_object_get (vs, "dictionary", &ow.dict, NULL);
164 GTK_WINDOW (get_widget_assert (builder, "oneway-anova-dialog"));
166 gtk_window_set_transient_for (ow.dialog, GTK_WINDOW (de));
168 g_object_set (dict_view, "model", ow.dict, NULL);
171 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector2),
172 is_currently_in_entry);
175 g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh), &ow);
179 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
180 dialog_state_valid, &ow);
184 struct contrasts_subdialog *cd = &ow.contrasts;
185 GtkEntry *entry = GTK_ENTRY (get_widget_assert (builder, "entry1"));
187 cd->acr = PSPPIRE_ACR (get_widget_assert (builder, "psppire-acr1"));
188 cd->contrasts_dialog = get_widget_assert (builder, "contrasts-dialog");
190 cd->next = get_widget_assert (builder, "next-button");
191 cd->prev = get_widget_assert (builder, "prev-button");
192 cd->ctotal = get_widget_assert (builder, "entry2");
194 cd->stack_label = get_widget_assert (builder, "contrast-stack-label");
197 ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
199 g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
200 g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
202 psppire_acr_set_entry (cd->acr, entry);
204 gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
208 response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
212 case GTK_RESPONSE_OK:
213 g_free (execute_syntax_string (de, generate_syntax (&ow)));
215 case PSPPIRE_RESPONSE_PASTE:
216 g_free (paste_syntax_to_window (generate_syntax (&ow)));
222 g_array_free (ow.contrasts_array, FALSE);
224 g_object_unref (builder);
228 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
232 gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
233 gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
235 GString *str = g_string_new ("ONEWAY /VARIABLES=");
237 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ow->vars_treeview), 0, str);
239 g_string_append (str, " BY ");
241 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
243 if (descriptives || homogeneity )
245 g_string_append (str, "\n\t/STATISTICS=");
247 g_string_append (str, "DESCRIPTIVES ");
249 g_string_append (str, "HOMOGENEITY ");
252 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
254 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
258 g_string_append (str, "\n\t/CONTRAST=");
260 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
263 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
267 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
269 g_string_append_printf (str, " %g", v);
273 g_string_append (str, ".\n");
276 g_string_free (str, FALSE);
283 /* Contrasts stuff */
288 /* Callback for when the list store currently associated with the
289 treeview has changed. It sets the widgets of the subdialog
290 to reflect the store's new state.
293 list_store_changed (struct contrasts_subdialog *csd)
298 GtkTreeModel *ls = NULL;
300 g_strdup_printf (_("Contrast %d of %d"),
301 csd->c, csd->temp_contrasts->len);
303 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
307 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
310 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
312 psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
314 /* Sensitive iff the liststore has two items or more */
315 gtk_widget_set_sensitive (csd->next,
316 gtk_tree_model_iter_nth_child
317 (ls, &iter, NULL, 1));
319 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
321 ok = gtk_tree_model_iter_next (ls, &iter)
325 gtk_tree_model_get (ls, &iter, 0, &v, -1);
329 text = g_strdup_printf ("%g", total);
331 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
338 /* Copy the contrasts array into the local array */
340 clone_contrasts_array (GArray *src_array)
345 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
348 for (i = 0 ; i < src_array->len ; ++i )
351 GtkTreeIter src_iter;
352 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
355 /* Refuse to copy empty stores */
356 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
360 dest = clone_list_store (src);
362 g_array_append_val (dest_array, dest);
370 run_contrasts_dialog (struct oneway_anova_dialog *ow)
372 struct contrasts_subdialog *csd = &ow->contrasts;
375 csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
379 push_new_store (csd->temp_contrasts, csd);
381 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
383 if ( response == PSPPIRE_RESPONSE_CONTINUE )
385 ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
388 /* Destroy the temp contrasts here */
394 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
396 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
398 g_array_append_val (contrast_stack, ls);
400 g_signal_connect_swapped (ls, "row-deleted",
401 G_CALLBACK (list_store_changed), csd);
403 g_signal_connect_swapped (ls, "row-changed",
404 G_CALLBACK (list_store_changed), csd);
406 list_store_changed (csd);
410 next (GtkWidget *widget, gpointer data)
412 struct contrasts_subdialog *csd = data;
414 if (csd->c >= csd->temp_contrasts->len)
415 push_new_store (csd->temp_contrasts, csd);
419 list_store_changed (csd);
424 prev (GtkWidget *widget, gpointer data)
426 struct contrasts_subdialog *csd = data;
431 list_store_changed (csd);