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-view.h"
23 #include "builder-wrapper.h"
24 #include "psppire-data-window.h"
25 #include "psppire-dialog.h"
26 #include "dialog-common.h"
27 #include "psppire-acr.h"
28 #include "psppire-selector.h"
29 #include "dict-display.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
39 struct contrasts_subdialog;
40 struct oneway_anova_dialog;
43 static void run_contrasts_dialog (struct oneway_anova_dialog *);
44 static void push_new_store (GArray *, struct contrasts_subdialog *);
46 static void next (GtkWidget *, gpointer);
47 static void prev (GtkWidget *, gpointer);
50 struct contrasts_subdialog
52 /* Contrasts Dialog widgets */
53 GtkWidget *contrasts_dialog;
54 GtkWidget *stack_label;
58 /* Gets copied into contrasts when "Continue"
60 GArray *temp_contrasts;
62 /* Index into the temp_contrasts */
72 struct oneway_anova_dialog
76 GtkWidget *factor_entry;
77 GtkWidget *vars_treeview;
79 GArray *contrasts_array;
81 GtkToggleButton *descriptives;
82 GtkToggleButton *homogeneity;
84 struct contrasts_subdialog contrasts;
88 dialog_state_valid (gpointer data)
90 struct oneway_anova_dialog *ow = data;
93 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
97 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
100 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
106 static gchar * generate_syntax (const struct oneway_anova_dialog *);
110 refresh (struct oneway_anova_dialog *ow)
112 GtkTreeModel *model =
113 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
115 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
117 gtk_list_store_clear (GTK_LIST_STORE (model));
122 /* Pops up the dialog box */
124 oneway_anova_dialog (PsppireDataWindow *de)
128 struct oneway_anova_dialog ow;
130 GtkBuilder *builder = builder_new ("oneway.ui");
132 GtkWidget *dict_view =
133 get_widget_assert (builder, "oneway-anova-treeview1");
135 GtkWidget *selector2 =
136 get_widget_assert (builder, "oneway-anova-selector2");
138 GtkWidget *contrasts_button =
139 get_widget_assert (builder, "contrasts-button");
142 g_signal_connect_swapped (contrasts_button, "clicked",
143 G_CALLBACK (run_contrasts_dialog), &ow);
146 ow.factor_entry = get_widget_assert (builder, "oneway-anova-entry");
148 get_widget_assert (builder, "oneway-anova-treeview2");
151 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton1"));
154 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton2"));
156 g_object_get (de->data_editor, "dictionary", &ow.dict, NULL);
159 GTK_WINDOW (get_widget_assert (builder, "oneway-anova-dialog"));
161 gtk_window_set_transient_for (ow.dialog, GTK_WINDOW (de));
163 g_object_set (dict_view, "model", ow.dict, NULL);
166 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (selector2),
167 is_currently_in_entry);
170 g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh), &ow);
174 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
175 dialog_state_valid, &ow);
179 struct contrasts_subdialog *cd = &ow.contrasts;
180 GtkEntry *entry = GTK_ENTRY (get_widget_assert (builder, "entry1"));
182 cd->acr = PSPPIRE_ACR (get_widget_assert (builder, "psppire-acr1"));
183 cd->contrasts_dialog = get_widget_assert (builder, "contrasts-dialog");
185 cd->next = get_widget_assert (builder, "next-button");
186 cd->prev = get_widget_assert (builder, "prev-button");
187 cd->ctotal = get_widget_assert (builder, "entry2");
189 cd->stack_label = get_widget_assert (builder, "contrast-stack-label");
192 ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
194 g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
195 g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
197 psppire_acr_set_entry (cd->acr, entry);
199 gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
203 response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
207 case GTK_RESPONSE_OK:
208 g_free (execute_syntax_string (de, generate_syntax (&ow)));
210 case PSPPIRE_RESPONSE_PASTE:
211 g_free (paste_syntax_to_window (generate_syntax (&ow)));
217 g_array_free (ow.contrasts_array, FALSE);
219 g_object_unref (builder);
223 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
227 gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
228 gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
231 ds_init_cstr (&dss, "ONEWAY /VARIABLES=");
233 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (ow->vars_treeview), 0, &dss);
235 ds_put_cstr (&dss, " BY ");
237 ds_put_cstr (&dss, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
239 if (descriptives || homogeneity )
241 ds_put_cstr (&dss, "\n\t/STATISTICS=");
243 ds_put_cstr (&dss, "DESCRIPTIVES ");
245 ds_put_cstr (&dss, "HOMOGENEITY ");
248 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
250 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
254 ds_put_cstr (&dss, "\n\t/CONTRAST=");
256 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
259 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
263 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
265 ds_put_c_format (&dss, " %g", v);
269 ds_put_cstr (&dss, ".\n");
271 text = ds_steal_cstr (&dss);
279 /* Contrasts stuff */
284 /* Callback for when the list store currently associated with the
285 treeview has changed. It sets the widgets of the subdialog
286 to reflect the store's new state.
289 list_store_changed (struct contrasts_subdialog *csd)
294 GtkTreeModel *ls = NULL;
296 g_strdup_printf (_("Contrast %d of %d"),
297 csd->c, csd->temp_contrasts->len);
299 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
303 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
306 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
308 psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
310 /* Sensitive iff the liststore has two items or more */
311 gtk_widget_set_sensitive (csd->next,
312 gtk_tree_model_iter_nth_child
313 (ls, &iter, NULL, 1));
315 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
317 ok = gtk_tree_model_iter_next (ls, &iter)
321 gtk_tree_model_get (ls, &iter, 0, &v, -1);
325 text = g_strdup_printf ("%g", total);
327 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
334 /* Copy the contrasts array into the local array */
336 clone_contrasts_array (GArray *src_array)
341 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
344 for (i = 0 ; i < src_array->len ; ++i )
347 GtkTreeIter src_iter;
348 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
351 /* Refuse to copy empty stores */
352 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
356 dest = clone_list_store (src);
358 g_array_append_val (dest_array, dest);
366 run_contrasts_dialog (struct oneway_anova_dialog *ow)
368 struct contrasts_subdialog *csd = &ow->contrasts;
371 csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
375 push_new_store (csd->temp_contrasts, csd);
377 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
379 if ( response == PSPPIRE_RESPONSE_CONTINUE )
381 ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
384 /* Destroy the temp contrasts here */
390 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
392 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
394 g_array_append_val (contrast_stack, ls);
396 g_signal_connect_swapped (ls, "row-deleted",
397 G_CALLBACK (list_store_changed), csd);
399 g_signal_connect_swapped (ls, "row-changed",
400 G_CALLBACK (list_store_changed), csd);
402 list_store_changed (csd);
406 next (GtkWidget *widget, gpointer data)
408 struct contrasts_subdialog *csd = data;
410 if (csd->c >= csd->temp_contrasts->len)
411 push_new_store (csd->temp_contrasts, csd);
415 list_store_changed (csd);
420 prev (GtkWidget *widget, gpointer data)
422 struct contrasts_subdialog *csd = data;
427 list_store_changed (csd);