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/>.
21 #include "oneway-anova-dialog.h"
22 #include "psppire-dict.h"
23 #include "psppire-var-store.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"
33 #include <language/syntax-string-source.h>
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
42 struct contrasts_subdialog;
43 struct oneway_anova_dialog;
46 static void run_contrasts_dialog (struct oneway_anova_dialog *);
47 static void push_new_store (GArray *, struct contrasts_subdialog *);
49 static void next (GtkWidget *, gpointer);
50 static void prev (GtkWidget *, gpointer);
53 struct contrasts_subdialog
55 /* Contrasts Dialog widgets */
56 GtkWidget *contrasts_dialog;
57 GtkWidget *stack_label;
61 /* Gets copied into contrasts when "Continue"
63 GArray *temp_contrasts;
65 /* Index into the temp_contrasts */
75 struct oneway_anova_dialog
79 GtkWidget *factor_entry;
80 GtkWidget *vars_treeview;
82 GArray *contrasts_array;
84 GtkToggleButton *descriptives;
85 GtkToggleButton *homogeneity;
87 struct contrasts_subdialog contrasts;
91 dialog_state_valid (gpointer data)
93 struct oneway_anova_dialog *ow = data;
96 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
100 if ( !gtk_tree_model_get_iter_first (vars, ¬used) )
103 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (ow->factor_entry))))
109 static gchar * generate_syntax (const struct oneway_anova_dialog *);
113 refresh (struct oneway_anova_dialog *ow)
115 GtkTreeModel *model =
116 gtk_tree_view_get_model (GTK_TREE_VIEW (ow->vars_treeview));
118 gtk_entry_set_text (GTK_ENTRY (ow->factor_entry), "");
120 gtk_list_store_clear (GTK_LIST_STORE (model));
125 /* Pops up the dialog box */
127 oneway_anova_dialog (GObject *o, gpointer data)
130 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
132 PsppireVarStore *vs = NULL;
134 struct oneway_anova_dialog ow;
136 GtkBuilder *builder = builder_new ("oneway.ui");
138 GtkWidget *dict_view =
139 get_widget_assert (builder, "oneway-anova-treeview1");
141 GtkWidget *selector2 =
142 get_widget_assert (builder, "oneway-anova-selector2");
144 GtkWidget *selector1 =
145 get_widget_assert (builder, "oneway-anova-selector1");
147 GtkWidget *contrasts_button =
148 get_widget_assert (builder, "contrasts-button");
151 g_signal_connect_swapped (contrasts_button, "clicked",
152 G_CALLBACK (run_contrasts_dialog), &ow);
155 ow.factor_entry = get_widget_assert (builder, "oneway-anova-entry");
157 get_widget_assert (builder, "oneway-anova-treeview2");
160 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton1"));
163 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "checkbutton2"));
165 g_object_get (de->data_editor, "var-store", &vs, NULL);
170 GTK_WINDOW (get_widget_assert (builder, "oneway-anova-dialog"));
172 gtk_window_set_transient_for (ow.dialog, GTK_WINDOW (de));
174 g_object_set (dict_view, "dictionary", vs->dict, NULL);
176 set_dest_model (GTK_TREE_VIEW (ow.vars_treeview), vs->dict);
179 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
180 dict_view, ow.vars_treeview,
181 insert_source_row_into_tree_view,
186 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
187 dict_view, ow.factor_entry,
188 insert_source_row_into_entry,
189 is_currently_in_entry,
194 g_signal_connect_swapped (ow.dialog, "refresh", G_CALLBACK (refresh), &ow);
198 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (ow.dialog),
199 dialog_state_valid, &ow);
203 struct contrasts_subdialog *cd = &ow.contrasts;
204 GtkEntry *entry = GTK_ENTRY (get_widget_assert (builder, "entry1"));
206 cd->acr = PSPPIRE_ACR (get_widget_assert (builder, "psppire-acr1"));
207 cd->contrasts_dialog = get_widget_assert (builder, "contrasts-dialog");
209 cd->next = get_widget_assert (builder, "next-button");
210 cd->prev = get_widget_assert (builder, "prev-button");
211 cd->ctotal = get_widget_assert (builder, "entry2");
213 cd->stack_label = get_widget_assert (builder, "contrast-stack-label");
216 ow.contrasts_array = g_array_new (FALSE, FALSE, sizeof (GtkListStore *));
218 g_signal_connect (cd->next, "clicked", G_CALLBACK (next), cd);
219 g_signal_connect (cd->prev, "clicked", G_CALLBACK (prev), cd);
221 psppire_acr_set_entry (cd->acr, entry);
223 gtk_window_set_transient_for (GTK_WINDOW (cd->contrasts_dialog),
227 response = psppire_dialog_run (PSPPIRE_DIALOG (ow.dialog));
231 case GTK_RESPONSE_OK:
233 gchar *syntax = generate_syntax (&ow);
235 struct getl_interface *sss = create_syntax_string_source (syntax);
236 execute_syntax (sss);
241 case PSPPIRE_RESPONSE_PASTE:
243 gchar *syntax = generate_syntax (&ow);
244 paste_syntax_in_new_window (syntax);
253 g_array_free (ow.contrasts_array, FALSE);
255 g_object_unref (builder);
259 static gchar * generate_syntax (const struct oneway_anova_dialog *ow)
263 gboolean descriptives = gtk_toggle_button_get_active (ow->descriptives);
264 gboolean homogeneity = gtk_toggle_button_get_active (ow->homogeneity);
266 GString *str = g_string_new ("ONEWAY /VARIABLES=");
268 append_variable_names (str, ow->dict, GTK_TREE_VIEW (ow->vars_treeview), 0);
270 g_string_append (str, " BY ");
272 g_string_append (str, gtk_entry_get_text (GTK_ENTRY (ow->factor_entry)));
274 if (descriptives || homogeneity )
276 g_string_append (str, "\n\t/STATISTICS=");
278 g_string_append (str, "DESCRIPTIVES ");
280 g_string_append (str, "HOMOGENEITY ");
283 for (i = 0 ; i < ow->contrasts_array->len ; ++i )
285 GtkListStore *ls = g_array_index (ow->contrasts_array, GtkListStore*, i);
289 g_string_append (str, "\n\t/CONTRAST=");
291 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
294 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
298 gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
300 g_string_append_printf (str, " %g", v);
304 g_string_append (str, ".\n");
307 g_string_free (str, FALSE);
314 /* Contrasts stuff */
319 /* Callback for when the list store currently associated with the
320 treeview has changed. It sets the widgets of the subdialog
321 to reflect the store's new state.
324 list_store_changed (struct contrasts_subdialog *csd)
329 GtkTreeModel *ls = NULL;
331 g_strdup_printf (_("Contrast %d of %d"),
332 csd->c, csd->temp_contrasts->len);
334 gtk_label_set_label (GTK_LABEL (csd->stack_label), text);
338 gtk_widget_set_sensitive (csd->prev, csd->c > 1);
341 ls = g_array_index (csd->temp_contrasts, GtkTreeModel*, csd->c - 1);
343 psppire_acr_set_model (csd->acr, GTK_LIST_STORE (ls));
345 /* Sensitive iff the liststore has two items or more */
346 gtk_widget_set_sensitive (csd->next,
347 gtk_tree_model_iter_nth_child
348 (ls, &iter, NULL, 1));
350 for (ok = gtk_tree_model_get_iter_first (ls, &iter);
352 ok = gtk_tree_model_iter_next (ls, &iter)
356 gtk_tree_model_get (ls, &iter, 0, &v, -1);
360 text = g_strdup_printf ("%g", total);
362 gtk_entry_set_text (GTK_ENTRY (csd->ctotal), text);
369 /* Copy the contrasts array into the local array */
371 clone_contrasts_array (GArray *src_array)
376 g_array_sized_new (FALSE, FALSE, sizeof (GtkListStore *),
379 for (i = 0 ; i < src_array->len ; ++i )
382 GtkTreeIter src_iter;
383 GtkListStore *src = g_array_index (src_array, GtkListStore*, i);
386 /* Refuse to copy empty stores */
387 if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
391 dest = clone_list_store (src);
393 g_array_append_val (dest_array, dest);
401 run_contrasts_dialog (struct oneway_anova_dialog *ow)
403 struct contrasts_subdialog *csd = &ow->contrasts;
406 csd->temp_contrasts = clone_contrasts_array (ow->contrasts_array);
410 push_new_store (csd->temp_contrasts, csd);
412 response = psppire_dialog_run (PSPPIRE_DIALOG (csd->contrasts_dialog));
414 if ( response == PSPPIRE_RESPONSE_CONTINUE )
416 ow->contrasts_array = clone_contrasts_array (csd->temp_contrasts);
419 /* Destroy the temp contrasts here */
425 push_new_store (GArray *contrast_stack, struct contrasts_subdialog *csd)
427 GtkListStore *ls = gtk_list_store_new (1, G_TYPE_DOUBLE);
429 g_array_append_val (contrast_stack, ls);
431 g_signal_connect_swapped (ls, "row-deleted",
432 G_CALLBACK (list_store_changed), csd);
434 g_signal_connect_swapped (ls, "row-changed",
435 G_CALLBACK (list_store_changed), csd);
437 list_store_changed (csd);
441 next (GtkWidget *widget, gpointer data)
443 struct contrasts_subdialog *csd = data;
445 if (csd->c >= csd->temp_contrasts->len)
446 push_new_store (csd->temp_contrasts, csd);
450 list_store_changed (csd);
455 prev (GtkWidget *widget, gpointer data)
457 struct contrasts_subdialog *csd = data;
462 list_store_changed (csd);